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