]> git.cworth.org Git - apitrace/blob - common/trace_tools_trace.cpp
Fix/cleanup wrapper search on Windows.
[apitrace] / common / trace_tools_trace.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Intel Corporation
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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
24  * SOFTWARE.
25  *
26  *********************************************************************/
27
28
29 #include <stdlib.h>
30
31 #include <iostream>
32
33 #include "os_string.hpp"
34 #include "os_process.hpp"
35 #include "trace_tools.hpp"
36
37
38
39 namespace trace {
40
41
42 #if defined(__APPLE__)
43 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
44 #define GL_TRACE_WRAPPER  "OpenGL"
45 #elif defined(_WIN32)
46 #define GL_TRACE_WRAPPER  "opengl32.dll"
47 #else
48 #define TRACE_VARIABLE "LD_PRELOAD"
49 #define GL_TRACE_WRAPPER  "glxtrace.so"
50 #define EGL_TRACE_WRAPPER  "egltrace.so"
51 #endif
52
53
54 static os::String
55 findWrapper(const char *wrapperFilename)
56 {
57     os::String wrapperPath;
58
59     os::String processDir = os::getProcessName();
60     processDir.trimFilename();
61
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()) {
68         return wrapperPath;
69     }
70
71     // Try relative install directory
72     wrapperPath = processDir;
73 #ifdef _WIN32
74     wrapperPath.join("..\\lib\\apitrace\\wrappers");
75 #else
76     wrapperPath.join("../lib/apitrace/wrappers");
77 #endif
78     wrapperPath.join(wrapperFilename);
79     if (wrapperPath.exists()) {
80         return wrapperPath;
81     }
82
83 #ifndef _WIN32
84     // Try absolute install directory
85     wrapperPath = APITRACE_WRAPPER_INSTALL_DIR;
86     wrapperPath.join(wrapperFilename);
87     if (wrapperPath.exists()) {
88         return wrapperPath;
89     }
90 #endif
91
92     return "";
93 }
94
95
96 int
97 traceProgram(API api,
98              char * const *argv,
99              const char *output,
100              bool verbose)
101 {
102     const char *wrapperFilename;
103
104     /*
105      * TODO: simplify code
106      */
107
108     switch (api) {
109     case API_GL:
110         wrapperFilename = GL_TRACE_WRAPPER;
111         break;
112 #ifdef EGL_TRACE_WRAPPER
113     case API_EGL:
114         wrapperFilename = EGL_TRACE_WRAPPER;
115         break;
116 #endif
117 #ifdef _WIN32
118     case API_D3D7:
119         wrapperFilename = "ddraw.dll";
120         break;
121     case API_D3D8:
122         wrapperFilename = "d3d8.dll";
123         break;
124     case API_D3D9:
125         wrapperFilename = "d3d9.dll";
126         break;
127     case API_D3D10:
128         wrapperFilename = "d3d10.dll";
129         break;
130 #endif
131     default:
132         std::cerr << "error: unsupported API\n";
133         return 1;
134     }
135
136     os::String wrapperPath = findWrapper(wrapperFilename);
137
138     if (!wrapperPath.length()) {
139         std::cerr << "error: failed to find " << wrapperFilename << "\n";
140         return 1;
141     }
142
143 #if defined(_WIN32)
144     /* On Windows copy the wrapper to the program directory.
145      */
146     os::String wrapperName (wrapperPath);
147     wrapperName.trimDirectory();
148
149     os::String tmpWrapper(argv[0]);
150     tmpWrapper.trimFilename();
151     tmpWrapper.join(wrapperName);
152
153     if (verbose) {
154         std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
155     }
156
157     if (tmpWrapper.exists()) {
158         std::cerr << "error: not overwriting " << tmpWrapper << "\n";
159         return 1;
160     }
161
162     if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
163         std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
164         return 1;
165     }
166 #endif /* _WIN32 */
167
168 #if defined(__APPLE__)
169     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
170      * directory, not the file. */
171     wrapperPath.trimFilename();
172 #endif
173
174 #if defined(TRACE_VARIABLE)
175     if (verbose) {
176         std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
177     }
178     /* FIXME: Don't modify the current environment */
179     os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
180 #endif /* TRACE_VARIABLE */
181
182     if (output) {
183         os::setEnvironment("TRACE_FILE", output);
184     }
185
186     if (verbose) {
187         const char *sep = "";
188         for (char * const * arg = argv; *arg; ++arg) {
189             std::cerr << *arg << sep;
190             sep = " ";
191         }
192         std::cerr << "\n";
193     }
194
195     int status = os::execute(argv);
196
197 #if defined(TRACE_VARIABLE)
198     os::unsetEnvironment(TRACE_VARIABLE);
199 #endif
200 #if defined(_WIN32)
201     os::removeFile(tmpWrapper);
202 #endif
203
204     if (output) {
205         os::unsetEnvironment("TRACE_FILE");
206     }
207     
208     return status;
209
210 }
211
212
213 } /* namespace trace */