]> git.cworth.org Git - apitrace/blob - cli/cli_trace.cpp
cli: Don't use the DLL injection method for OpenGL on Windows 8.
[apitrace] / cli / cli_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 <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33
34 #include <iostream>
35
36 #include "os_string.hpp"
37 #include "os_process.hpp"
38
39 #include "cli.hpp"
40 #include "cli_resources.hpp"
41
42
43 #if defined(__APPLE__)
44 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
45 #define GL_TRACE_WRAPPER  "OpenGL"
46 #elif defined(_WIN32)
47 #define GL_TRACE_WRAPPER  "opengl32.dll"
48 #else
49 #define TRACE_VARIABLE "LD_PRELOAD"
50 #define GL_TRACE_WRAPPER  "glxtrace.so"
51 #define EGL_TRACE_WRAPPER  "egltrace.so"
52 #endif
53
54
55 static inline bool
56 copyWrapper(const os::String & wrapperPath,
57             const char *programPath,
58             bool verbose)
59 {
60     os::String wrapperFilename(wrapperPath);
61     wrapperFilename.trimDirectory();
62
63     os::String tmpWrapper(programPath);
64     tmpWrapper.trimFilename();
65     tmpWrapper.join(wrapperFilename);
66
67     if (verbose) {
68         std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
69     }
70
71     if (tmpWrapper.exists()) {
72         std::cerr << "error: not overwriting " << tmpWrapper << "\n";
73         return false;
74     }
75
76     if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
77         std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
78         return false;
79     }
80
81     return true;
82 }
83
84
85 static int
86 traceProgram(trace::API api,
87              char * const *argv,
88              const char *output,
89              bool verbose)
90 {
91     const char *wrapperFilename;
92     std::vector<const char *> args;
93     int status = 1;
94
95     /*
96      * TODO: simplify code
97      */
98
99     bool useInject = false;
100     switch (api) {
101     case trace::API_GL:
102         wrapperFilename = GL_TRACE_WRAPPER;
103         break;
104 #ifdef EGL_TRACE_WRAPPER
105     case trace::API_EGL:
106         wrapperFilename = EGL_TRACE_WRAPPER;
107         break;
108 #endif
109 #ifdef _WIN32
110     case trace::API_D3D7:
111         wrapperFilename = "ddraw.dll";
112         break;
113     case trace::API_D3D8:
114         wrapperFilename = "d3d8.dll";
115         break;
116     case trace::API_D3D9:
117         wrapperFilename = "d3d9.dll";
118         break;
119     case trace::API_DXGI:
120         wrapperFilename = "dxgitrace.dll";
121         useInject = true;
122         break;
123 #endif
124     default:
125         std::cerr << "error: unsupported API\n";
126         return 1;
127     }
128
129     os::String wrapperPath = findWrapper(wrapperFilename, verbose);
130     if (!wrapperPath.length()) {
131         std::cerr << "error: failed to find " << wrapperFilename << " wrapper\n";
132         goto exit;
133     }
134
135 #if defined(_WIN32)
136     /*
137      * Use DLL injection method on Windows, even for APIs that don't stricly
138      * need it.  Except when tracing OpenGL on Windows 8, as the injection
139      * method seems to have troubles tracing the internal
140      * gdi32.dll!SwapBuffers -> opengl32.dll!wglSwapBuffer calls, per github
141      * issue #172.
142      */
143     {
144         OSVERSIONINFO osvi;
145         ZeroMemory(&osvi, sizeof osvi);
146         osvi.dwOSVersionInfoSize = sizeof osvi;
147         GetVersionEx(&osvi);
148         BOOL bIsWindows8orLater =
149             osvi.dwMajorVersion > 6 ||
150             (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 2);
151         if (api != trace::API_GL || !bIsWindows8orLater) {
152             useInject = true;
153         }
154     }
155
156     if (useInject) {
157         args.push_back("inject");
158         args.push_back(wrapperPath);
159     } else {
160         /* On Windows copy the wrapper to the program directory.
161          */
162         if (!copyWrapper(wrapperPath, argv[0], verbose)) {
163             goto exit;
164         }
165     }
166 #else  /* !_WIN32 */
167     (void)useInject;
168 #endif /* !_WIN32 */
169
170 #if defined(__APPLE__)
171     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
172      * directory, not the file. */
173     wrapperPath.trimFilename();
174 #endif
175
176     /*
177      * Spawn child process.
178      */
179
180     {
181 #if defined(TRACE_VARIABLE)
182         const char *oldEnvVarValue = getenv(TRACE_VARIABLE);
183         if (oldEnvVarValue) {
184             wrapperPath.append(OS_PATH_SEP);
185             wrapperPath.append(oldEnvVarValue);
186         }
187
188         /* FIXME: Don't modify our (ie parent) environment */
189         os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
190
191         if (verbose) {
192             std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
193         }
194 #endif /* TRACE_VARIABLE */
195
196         if (output) {
197             os::setEnvironment("TRACE_FILE", output);
198         }
199
200         for (char * const * arg = argv; *arg; ++arg) {
201             args.push_back(*arg);
202         }
203
204         if (verbose) {
205             const char *sep = "";
206             for (unsigned i = 0; i < args.size(); ++i) {
207                 std::cerr << sep << args[i];
208                 sep = " ";
209             }
210             std::cerr << "\n";
211         }
212
213         args.push_back(NULL);
214
215         status = os::execute((char * const *)&args[0]);
216
217 #if defined(TRACE_VARIABLE)
218         if (oldEnvVarValue) {
219             os::setEnvironment(TRACE_VARIABLE, oldEnvVarValue);
220         } else {
221             os::unsetEnvironment(TRACE_VARIABLE);
222         }
223 #endif
224     }
225
226 exit:
227 #if defined(_WIN32)
228     if (!useInject) {
229         os::String tmpWrapper(argv[0]);
230         tmpWrapper.trimFilename();
231         tmpWrapper.join(wrapperFilename);
232         os::removeFile(tmpWrapper);
233     }
234 #endif
235
236     if (output) {
237         os::unsetEnvironment("TRACE_FILE");
238     }
239     
240     return status;
241
242 }
243
244
245 static const char *synopsis = "Generate a new trace by executing the given program.";
246
247 static void
248 usage(void)
249 {
250     std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
251         << synopsis << "\n"
252         "\n"
253         "    The given program will be executed with the given arguments.\n"
254         "    During execution, all OpenGL calls will be captured to a trace\n"
255         "    file. That trace file can then be used\n"
256         "    with other apitrace utilities for replay or analysis.\n"
257         "\n"
258         "    -v, --verbose       verbose output\n"
259         "    -a, --api=API       specify API to trace ("
260 #ifdef _WIN32
261                                                       "gl, d3d7, d3d8, d3d9, or dxgi (for d3d10 and higher) "
262 #else
263                                                       "gl or egl"
264 #endif
265                                                       ");\n"
266         "                        default is `gl`\n"
267         "    -o, --output=TRACE  specify output trace file;\n"
268         "                        default is `PROGRAM.trace`\n";
269 }
270
271 const static char *
272 shortOptions = "+hva:o:";
273
274 const static struct option
275 longOptions[] = {
276     {"help", no_argument, 0, 'h'},
277     {"verbose", no_argument, 0, 'v'},
278     {"api", required_argument, 0, 'a'},
279     {"output", required_argument, 0, 'o'},
280     {0, 0, 0, 0}
281 };
282
283 static int
284 command(int argc, char *argv[])
285 {
286     bool verbose = false;
287     trace::API api = trace::API_GL;
288     const char *output = NULL;
289
290     int opt;
291     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
292         switch (opt) {
293         case 'h':
294             usage();
295             return 0;
296         case 'v':
297             verbose = true;
298             break;
299         case 'a':
300             if (strcmp(optarg, "gl") == 0) {
301                 api = trace::API_GL;
302             } else if (strcmp(optarg, "egl") == 0) {
303                 api = trace::API_EGL;
304             } else if (strcmp(optarg, "d3d7") == 0) {
305                 api = trace::API_D3D7;
306             } else if (strcmp(optarg, "d3d8") == 0) {
307                 api = trace::API_D3D8;
308             } else if (strcmp(optarg, "d3d9") == 0) {
309                 api = trace::API_D3D9;
310             } else if (strcmp(optarg, "dxgi") == 0 ||
311                        strcmp(optarg, "d3d10") == 0 ||
312                        strcmp(optarg, "d3d10_1") == 0 ||
313                        strcmp(optarg, "d3d11") == 0 ||
314                        strcmp(optarg, "d3d11_1") == 0) {
315                 api = trace::API_DXGI;
316             } else {
317                 std::cerr << "error: unknown API `" << optarg << "`\n";
318                 usage();
319                 return 1;
320             }
321             break;
322         case 'o':
323             output = optarg;
324             break;
325         default:
326             std::cerr << "error: unexpected option `" << opt << "`\n";
327             usage();
328             return 1;
329         }
330     }
331
332     if (optind == argc) {
333         std::cerr << "error: no command specified\n";
334         usage();
335         return 1;
336     }
337
338     assert(argv[argc] == 0);
339     return traceProgram(api, argv + optind, output, verbose);
340 }
341
342 const Command trace_command = {
343     "trace",
344     synopsis,
345     usage,
346     command
347 };