1 /*********************************************************************
3 * Copyright 2011 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 *********************************************************************/
33 #include "os_string.hpp"
34 #include "os_process.hpp"
35 #include "trace_tools.hpp"
42 #if defined(__APPLE__)
43 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
44 #define GL_TRACE_WRAPPER "OpenGL"
46 #define GL_TRACE_WRAPPER "opengl32.dll"
48 #define TRACE_VARIABLE "LD_PRELOAD"
49 #define GL_TRACE_WRAPPER "glxtrace.so"
50 #define EGL_TRACE_WRAPPER "egltrace.so"
55 findWrapper(const char *wrapperFilename)
57 os::String wrapperPath;
59 os::String processDir = os::getProcessName();
60 processDir.trimFilename();
62 // Try relative build directory
63 // XXX: Just make build and install directory layout match
64 wrapperPath = processDir;
65 wrapperPath.join("wrappers");
66 wrapperPath.join(wrapperFilename);
67 if (wrapperPath.exists()) {
71 // Try relative install directory
72 wrapperPath = processDir;
74 wrapperPath.join("..\\lib\\wrappers");
75 #elif defined(__APPLE__)
76 wrapperPath.join("../lib/wrappers");
78 wrapperPath.join("../lib/apitrace/wrappers");
80 wrapperPath.join(wrapperFilename);
81 if (wrapperPath.exists()) {
86 // Try absolute install directory
87 wrapperPath = APITRACE_WRAPPERS_INSTALL_DIR;
88 wrapperPath.join(wrapperFilename);
89 if (wrapperPath.exists()) {
104 const char *wrapperFilename;
107 * TODO: simplify code
112 wrapperFilename = GL_TRACE_WRAPPER;
114 #ifdef EGL_TRACE_WRAPPER
116 wrapperFilename = EGL_TRACE_WRAPPER;
121 wrapperFilename = "ddraw.dll";
124 wrapperFilename = "d3d8.dll";
127 wrapperFilename = "d3d9.dll";
130 wrapperFilename = "d3d10.dll";
133 wrapperFilename = "d3d10_1.dll";
136 wrapperFilename = "d3d11.dll";
140 std::cerr << "error: unsupported API\n";
144 os::String wrapperPath = findWrapper(wrapperFilename);
146 if (!wrapperPath.length()) {
147 std::cerr << "error: failed to find " << wrapperFilename << "\n";
152 /* On Windows copy the wrapper to the program directory.
154 os::String tmpWrapper(argv[0]);
155 tmpWrapper.trimFilename();
156 tmpWrapper.join(wrapperFilename);
159 std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
162 if (tmpWrapper.exists()) {
163 std::cerr << "error: not overwriting " << tmpWrapper << "\n";
167 if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
168 std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
173 #if defined(__APPLE__)
174 /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
175 * directory, not the file. */
176 wrapperPath.trimFilename();
179 #if defined(TRACE_VARIABLE)
181 std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
183 /* FIXME: Don't modify the current environment */
184 os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
185 #endif /* TRACE_VARIABLE */
188 os::setEnvironment("TRACE_FILE", output);
192 const char *sep = "";
193 for (char * const * arg = argv; *arg; ++arg) {
194 std::cerr << *arg << sep;
200 int status = os::execute(argv);
202 #if defined(TRACE_VARIABLE)
203 os::unsetEnvironment(TRACE_VARIABLE);
206 os::removeFile(tmpWrapper);
210 os::unsetEnvironment("TRACE_FILE");
218 } /* namespace trace */