]> git.cworth.org Git - apitrace/blob - cli/cli_trace.cpp
Add a new "apitrace trace" command to the command-line interface.
[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 #include <iostream>
29
30 #include "cli.hpp"
31
32 #include "os_path.hpp"
33
34
35 static int verbose = 1;
36
37 static const char *synopsis = "Generate a new trace by executing the given program.";
38
39 static void
40 usage(void)
41 {
42     std::cout << "usage: apitrace trace <program> [<args> ...]\n"
43         << synopsis << "\n"
44         "\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";
49 }
50
51 /* We only support "apitrace trace" on POSIX-like systems (not WIN32) */
52 #ifndef _WIN32
53
54 #include <sys/stat.h>
55
56 #ifdef __APPLE__
57 #define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH"
58 #define CLI_TRACE_WRAPPER  "OpenGL"
59 #else
60 #define CLI_TRACE_VARIABLE "LD_PRELOAD"
61 #define CLI_TRACE_WRAPPER  "glxtrace.so"
62 #endif
63
64 static int
65 file_exists(const char *path)
66 {
67     struct stat st;
68     int err;
69
70     err = stat(path, &st);
71     if (err)
72         return 0;
73
74     if (! S_ISREG(st.st_mode))
75         return 0;
76
77     return 1;
78 }
79
80 static os::Path
81 find_wrapper(const char *filename)
82 {
83     os::Path complete;
84
85     /* First look in the same directory from which this process is
86      * running, (to support developers running a compiled program that
87      * has not been installed. */
88 #if 1
89     os::Path process_dir = os::getProcessName();
90
91     process_dir.trimFilename();
92
93     complete = process_dir;
94     complete.join("wrappers");
95     complete.join(filename);
96 #else
97     complete = APITRACE_BINARY_DIR "/wrappers";
98     complete.join(filename);
99 #endif
100
101     if (file_exists(complete))
102         return complete;
103
104     /* Second, look in the directory for installed wrappers. */
105     complete = APITRACE_WRAPPER_INSTALL_DIR;
106     complete.join(filename);
107
108     if (file_exists(complete))
109         return complete;
110
111     std::cerr << "error: cannot find " << filename << " (looked in " <<
112         APITRACE_WRAPPER_INSTALL_DIR << ")\n";
113     exit(1);
114
115     return "";
116 }
117
118 static int
119 do_trace_posix(int argc, char *argv[])
120 {
121     os::Path binary = find_wrapper(CLI_TRACE_WRAPPER);
122
123     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
124      * directory, not the file. */
125 #ifdef __APPLE__
126     binary.trimFilename();
127 #endif
128
129     if (verbose) {
130         std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
131     }
132
133     setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
134
135     if (verbose) {
136         const char *sep = "";
137         for (char **arg = argv; *arg; ++arg) {
138             std::cerr << *arg << sep;
139             sep = " ";
140         }
141         std::cerr << "\n";
142     }
143
144     execvp(argv[0], argv);
145
146     std::cerr << "Error: Failed to execute " << argv[0] << "\n";
147
148     return 1;
149 }
150
151 #endif
152
153 static int
154 command(int argc, char *argv[])
155 {
156
157 #ifdef _WIN32
158
159     std::cerr <<
160         "The 'apitrace trace' command is not supported for this operating system.\n"
161         "Instead, you will need to copy opengl32.dll, d3d8.dll, or d3d9.dll from\n"
162         APITRACE_WRAPPER_INSTALL_DIR "\n"
163         "to the directory with the application to trace, then run the application.\n";
164     return 1;
165
166 #else
167
168     int i;
169
170     for (i = 0; i < argc; ++i) {
171         const char *arg = argv[i];
172
173         if (arg[0] != '-') {
174             break;
175         }
176
177         if (!strcmp(arg, "--")) {
178             i++;
179             break;
180         } else if (!strcmp(arg, "--help")) {
181             usage();
182             return 0;
183         } else {
184             std::cerr << "error: unknown option " << arg << "\n";
185             usage();
186             return 1;
187         }
188     }
189
190     if (i == argc) {
191         std::cerr << "Error: Need a command name to execute (see 'apitrace trace --help')\n";
192         return 1;
193     }
194
195     return do_trace_posix(argc - i, argv + i);
196
197 #endif
198 }
199
200 const Command trace_command = {
201     "trace",
202     synopsis,
203     usage,
204     command
205 };