]> git.cworth.org Git - apitrace/blob - cli/cli_trace.cpp
5903bdc5f7865cd7327a71b9b14f50d97a567e37
[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     useInject = true;
137     if (useInject) {
138         args.push_back("inject");
139         args.push_back(wrapperPath);
140     } else {
141         /* On Windows copy the wrapper to the program directory.
142          */
143         if (!copyWrapper(wrapperPath, argv[0], verbose)) {
144             goto exit;
145         }
146     }
147 #else  /* !_WIN32 */
148     (void)useInject;
149 #endif /* !_WIN32 */
150
151 #if defined(__APPLE__)
152     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
153      * directory, not the file. */
154     wrapperPath.trimFilename();
155 #endif
156
157     /*
158      * Spawn child process.
159      */
160
161     {
162 #if defined(TRACE_VARIABLE)
163         const char *oldEnvVarValue = getenv(TRACE_VARIABLE);
164         if (oldEnvVarValue) {
165             wrapperPath.append(OS_PATH_SEP);
166             wrapperPath.append(oldEnvVarValue);
167         }
168
169         /* FIXME: Don't modify our (ie parent) environment */
170         os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
171
172         if (verbose) {
173             std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
174         }
175 #endif /* TRACE_VARIABLE */
176
177         if (output) {
178             os::setEnvironment("TRACE_FILE", output);
179         }
180
181         for (char * const * arg = argv; *arg; ++arg) {
182             args.push_back(*arg);
183         }
184
185         if (verbose) {
186             const char *sep = "";
187             for (unsigned i = 0; i < args.size(); ++i) {
188                 std::cerr << sep << args[i];
189                 sep = " ";
190             }
191             std::cerr << "\n";
192         }
193
194         args.push_back(NULL);
195
196         status = os::execute((char * const *)&args[0]);
197
198 #if defined(TRACE_VARIABLE)
199         if (oldEnvVarValue) {
200             os::setEnvironment(TRACE_VARIABLE, oldEnvVarValue);
201         } else {
202             os::unsetEnvironment(TRACE_VARIABLE);
203         }
204 #endif
205     }
206
207 exit:
208 #if defined(_WIN32)
209     if (!useInject) {
210         os::String tmpWrapper(argv[0]);
211         tmpWrapper.trimFilename();
212         tmpWrapper.join(wrapperFilename);
213         os::removeFile(tmpWrapper);
214     }
215 #endif
216
217     if (output) {
218         os::unsetEnvironment("TRACE_FILE");
219     }
220     
221     return status;
222
223 }
224
225
226 static const char *synopsis = "Generate a new trace by executing the given program.";
227
228 static void
229 usage(void)
230 {
231     std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
232         << synopsis << "\n"
233         "\n"
234         "    The given program will be executed with the given arguments.\n"
235         "    During execution, all OpenGL calls will be captured to a trace\n"
236         "    file. That trace file can then be used\n"
237         "    with other apitrace utilities for replay or analysis.\n"
238         "\n"
239         "    -v, --verbose       verbose output\n"
240         "    -a, --api=API       specify API to trace ("
241 #ifdef _WIN32
242                                                       "gl, d3d7, d3d8, d3d9, or dxgi (for d3d10 and higher) "
243 #else
244                                                       "gl or egl"
245 #endif
246                                                       ");\n"
247         "                        default is `gl`\n"
248         "    -o, --output=TRACE  specify output trace file;\n"
249         "                        default is `PROGRAM.trace`\n";
250 }
251
252 const static char *
253 shortOptions = "+hva:o:";
254
255 const static struct option
256 longOptions[] = {
257     {"help", no_argument, 0, 'h'},
258     {"verbose", no_argument, 0, 'v'},
259     {"api", required_argument, 0, 'a'},
260     {"output", required_argument, 0, 'o'},
261     {0, 0, 0, 0}
262 };
263
264 static int
265 command(int argc, char *argv[])
266 {
267     bool verbose = false;
268     trace::API api = trace::API_GL;
269     const char *output = NULL;
270
271     int opt;
272     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
273         switch (opt) {
274         case 'h':
275             usage();
276             return 0;
277         case 'v':
278             verbose = true;
279             break;
280         case 'a':
281             if (strcmp(optarg, "gl") == 0) {
282                 api = trace::API_GL;
283             } else if (strcmp(optarg, "egl") == 0) {
284                 api = trace::API_EGL;
285             } else if (strcmp(optarg, "d3d7") == 0) {
286                 api = trace::API_D3D7;
287             } else if (strcmp(optarg, "d3d8") == 0) {
288                 api = trace::API_D3D8;
289             } else if (strcmp(optarg, "d3d9") == 0) {
290                 api = trace::API_D3D9;
291             } else if (strcmp(optarg, "dxgi") == 0 ||
292                        strcmp(optarg, "d3d10") == 0 ||
293                        strcmp(optarg, "d3d10_1") == 0 ||
294                        strcmp(optarg, "d3d11") == 0 ||
295                        strcmp(optarg, "d3d11_1") == 0) {
296                 api = trace::API_DXGI;
297             } else {
298                 std::cerr << "error: unknown API `" << optarg << "`\n";
299                 usage();
300                 return 1;
301             }
302             break;
303         case 'o':
304             output = optarg;
305             break;
306         default:
307             std::cerr << "error: unexpected option `" << opt << "`\n";
308             usage();
309             return 1;
310         }
311     }
312
313     if (optind == argc) {
314         std::cerr << "error: no command specified\n";
315         usage();
316         return 1;
317     }
318
319     assert(argv[argc] == 0);
320     return traceProgram(api, argv + optind, output, verbose);
321 }
322
323 const Command trace_command = {
324     "trace",
325     synopsis,
326     usage,
327     command
328 };