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 *********************************************************************/
32 #include "os_path.hpp"
35 static int verbose = 1;
37 static const char *synopsis = "Generate a new trace by executing the given program.";
42 std::cout << "usage: apitrace trace <program> [<args> ...]\n"
45 " The given program will be executed with the given arguments.\n"
46 " During execution, all OpenGL calls will be captured to a trace\n"
47 " file named <program>.trace. That trace file can then be used\n"
48 " with other apitrace utilities for replay or analysis.\n";
51 /* We only support "apitrace trace" on POSIX-like systems (not WIN32) */
56 #define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH"
57 #define CLI_TRACE_WRAPPER "OpenGL"
59 #define CLI_TRACE_VARIABLE "LD_PRELOAD"
60 #define CLI_TRACE_WRAPPER "glxtrace.so"
64 find_wrapper(const char *filename)
68 /* First look in the same directory from which this process is
69 * running, (to support developers running a compiled program that
70 * has not been installed. */
72 os::Path process_dir = os::getProcessName();
74 process_dir.trimFilename();
76 complete = process_dir;
77 complete.join("wrappers");
78 complete.join(filename);
80 complete = APITRACE_BINARY_DIR "/wrappers";
81 complete.join(filename);
84 if (complete.exists())
87 /* Second, look in the directory for installed wrappers. */
88 complete = APITRACE_WRAPPER_INSTALL_DIR;
89 complete.join(filename);
91 if (complete.exists())
94 std::cerr << "error: cannot find " << filename << " (looked in " <<
95 APITRACE_WRAPPER_INSTALL_DIR << ")\n";
102 do_trace_posix(int argc, char *argv[])
104 os::Path binary = find_wrapper(CLI_TRACE_WRAPPER);
106 /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
107 * directory, not the file. */
109 binary.trimFilename();
113 std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
116 setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
119 const char *sep = "";
120 for (char **arg = argv; *arg; ++arg) {
121 std::cerr << *arg << sep;
127 execvp(argv[0], argv);
129 std::cerr << "Error: Failed to execute " << argv[0] << "\n";
137 command(int argc, char *argv[])
143 "The 'apitrace trace' command is not supported for this operating system.\n"
144 "Instead, you will need to copy opengl32.dll, d3d8.dll, or d3d9.dll from\n"
145 APITRACE_WRAPPER_INSTALL_DIR "\n"
146 "to the directory with the application to trace, then run the application.\n";
153 for (i = 0; i < argc; ++i) {
154 const char *arg = argv[i];
160 if (!strcmp(arg, "--")) {
163 } else if (!strcmp(arg, "--help")) {
167 std::cerr << "error: unknown option " << arg << "\n";
174 std::cerr << "Error: Need a command name to execute (see 'apitrace trace --help')\n";
178 return do_trace_posix(argc - i, argv + i);
183 const Command trace_command = {