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 *********************************************************************/
36 #include "os_string.hpp"
37 #include "os_process.hpp"
40 #include "cli_resources.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";
86 traceProgram(trace::API api,
91 const char *wrapperFilename;
92 std::vector<const char *> args;
99 bool useInject = false;
102 wrapperFilename = GL_TRACE_WRAPPER;
104 #ifdef EGL_TRACE_WRAPPER
106 wrapperFilename = EGL_TRACE_WRAPPER;
110 case trace::API_D3D7:
111 wrapperFilename = "ddraw.dll";
113 case trace::API_D3D8:
114 wrapperFilename = "d3d8.dll";
116 case trace::API_D3D9:
117 wrapperFilename = "d3d9.dll";
119 case trace::API_DXGI:
120 wrapperFilename = "dxgitrace.dll";
125 std::cerr << "error: unsupported API\n";
129 os::String wrapperPath = findWrapper(wrapperFilename);
130 if (!wrapperPath.length()) {
131 std::cerr << "error: failed to find " << wrapperFilename << "\n";
137 args.push_back("inject");
138 args.push_back(wrapperPath);
140 /* On Windows copy the wrapper to the program directory.
142 if (!copyWrapper(wrapperPath, argv[0], verbose)) {
150 #if defined(__APPLE__)
151 /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
152 * directory, not the file. */
153 wrapperPath.trimFilename();
156 #if defined(TRACE_VARIABLE)
158 std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
160 /* FIXME: Don't modify the current environment */
161 os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
162 #endif /* TRACE_VARIABLE */
165 os::setEnvironment("TRACE_FILE", output);
168 for (char * const * arg = argv; *arg; ++arg) {
169 args.push_back(*arg);
171 args.push_back(NULL);
174 const char *sep = "";
175 for (unsigned i = 0; i < args.size(); ++i) {
176 std::cerr << sep << args[i];
182 status = os::execute((char * const *)&args[0]);
185 #if defined(TRACE_VARIABLE)
186 os::unsetEnvironment(TRACE_VARIABLE);
190 os::String tmpWrapper(argv[0]);
191 tmpWrapper.trimFilename();
192 tmpWrapper.join(wrapperFilename);
193 os::removeFile(tmpWrapper);
198 os::unsetEnvironment("TRACE_FILE");
206 static const char *synopsis = "Generate a new trace by executing the given program.";
211 std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
214 " The given program will be executed with the given arguments.\n"
215 " During execution, all OpenGL calls will be captured to a trace\n"
216 " file. That trace file can then be used\n"
217 " with other apitrace utilities for replay or analysis.\n"
219 " -v, --verbose verbose output\n"
220 " -a, --api=API specify API to trace ("
222 "gl, d3d7, d3d8, d3d9, or d3d10"
228 " -o, --output=TRACE specify output trace file;\n"
229 " default is `PROGRAM.trace`\n";
233 shortOptions = "+hva:o:";
235 const static struct option
237 {"help", no_argument, 0, 'h'},
238 {"verbose", no_argument, 0, 'v'},
239 {"api", required_argument, 0, 'a'},
240 {"output", required_argument, 0, 'o'},
245 command(int argc, char *argv[])
247 bool verbose = false;
248 trace::API api = trace::API_GL;
249 const char *output = NULL;
252 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
261 if (strcmp(optarg, "gl") == 0) {
263 } else if (strcmp(optarg, "egl") == 0) {
264 api = trace::API_EGL;
265 } else if (strcmp(optarg, "d3d7") == 0) {
266 api = trace::API_D3D7;
267 } else if (strcmp(optarg, "d3d8") == 0) {
268 api = trace::API_D3D8;
269 } else if (strcmp(optarg, "d3d9") == 0) {
270 api = trace::API_D3D9;
271 } else if (strcmp(optarg, "dxgi") == 0 ||
272 strcmp(optarg, "d3d10") == 0 ||
273 strcmp(optarg, "d3d10_1") == 0 ||
274 strcmp(optarg, "d3d11") == 0 ||
275 strcmp(optarg, "d3d11_1") == 0) {
276 api = trace::API_DXGI;
278 std::cerr << "error: unknown API `" << optarg << "`\n";
287 std::cerr << "error: unexpected option `" << opt << "`\n";
293 if (optind == argc) {
294 std::cerr << "error: no command specified\n";
299 assert(argv[argc] == 0);
300 return traceProgram(api, argv + optind, output, verbose);
303 const Command trace_command = {