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"
36 #include "trace_resource.hpp"
43 #if defined(__APPLE__)
44 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
45 #define GL_TRACE_WRAPPER "OpenGL"
47 #define GL_TRACE_WRAPPER "opengl32.dll"
49 #define TRACE_VARIABLE "LD_PRELOAD"
50 #define GL_TRACE_WRAPPER "glxtrace.so"
51 #define EGL_TRACE_WRAPPER "egltrace.so"
56 copyWrapper(const os::String & wrapperPath,
57 const char *programPath,
60 os::String wrapperFilename(wrapperPath);
61 wrapperFilename.trimDirectory();
63 os::String tmpWrapper(programPath);
64 tmpWrapper.trimFilename();
65 tmpWrapper.join(wrapperFilename);
68 std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
71 if (tmpWrapper.exists()) {
72 std::cerr << "error: not overwriting " << tmpWrapper << "\n";
76 if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
77 std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
85 static const char *glWrappers[] = {
90 #ifdef EGL_TRACE_WRAPPER
91 static const char *eglWrappers[] = {
98 static const char *d3d7Wrappers[] = {
103 static const char *d3d8Wrappers[] = {
108 static const char *d3d9Wrappers[] = {
113 static const char *dxgiWrappers[] = {
124 traceProgram(API api,
129 const char **wrapperFilenames;
130 unsigned numWrappers;
134 * TODO: simplify code
139 wrapperFilenames = glWrappers;
141 #ifdef EGL_TRACE_WRAPPER
143 wrapperFilenames = eglWrappers;
148 wrapperFilenames = d3d7Wrappers;
151 wrapperFilenames = d3d8Wrappers;
154 wrapperFilenames = d3d9Wrappers;
159 wrapperFilenames = dxgiWrappers;
163 std::cerr << "error: unsupported API\n";
168 while (wrapperFilenames[numWrappers]) {
173 for (i = 0; i < numWrappers; ++i) {
174 const char *wrapperFilename = wrapperFilenames[i];
176 os::String wrapperPath = findWrapper(wrapperFilename);
178 if (!wrapperPath.length()) {
179 std::cerr << "error: failed to find " << wrapperFilename << "\n";
184 /* On Windows copy the wrapper to the program directory.
186 if (!copyWrapper(wrapperPath, argv[0], verbose)) {
191 #if defined(__APPLE__)
192 /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
193 * directory, not the file. */
194 wrapperPath.trimFilename();
197 #if defined(TRACE_VARIABLE)
198 assert(numWrappers == 1);
200 std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
202 /* FIXME: Don't modify the current environment */
203 os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
204 #endif /* TRACE_VARIABLE */
208 os::setEnvironment("TRACE_FILE", output);
212 const char *sep = "";
213 for (char * const * arg = argv; *arg; ++arg) {
214 std::cerr << *arg << sep;
220 status = os::execute(argv);
223 #if defined(TRACE_VARIABLE)
224 os::unsetEnvironment(TRACE_VARIABLE);
227 for (unsigned j = 0; j < i; ++j) {
228 const char *wrapperFilename = wrapperFilenames[j];
229 os::String tmpWrapper(argv[0]);
230 tmpWrapper.trimFilename();
231 tmpWrapper.join(wrapperFilename);
232 os::removeFile(tmpWrapper);
237 os::unsetEnvironment("TRACE_FILE");
245 } /* namespace trace */