]> git.cworth.org Git - apitrace/blob - common/trace_tools_trace.cpp
Split common functionality out of cli.
[apitrace] / common / trace_tools_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 <iostream>
30
31 #include "os_path.hpp"
32 #include "trace_tools.hpp"
33
34
35
36 namespace trace {
37
38
39 #ifdef __APPLE__
40 #define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH"
41 #define CLI_TRACE_WRAPPER  "OpenGL"
42 #else
43 #define CLI_TRACE_VARIABLE "LD_PRELOAD"
44 #define CLI_TRACE_WRAPPER  "glxtrace.so"
45 #endif
46
47
48 static os::Path
49 findWrapper(const char *filename, bool verbose)
50 {
51     os::Path complete;
52
53     /* First look in the same directory from which this process is
54      * running, (to support developers running a compiled program that
55      * has not been installed. */
56 #if 1
57     os::Path process_dir = os::getProcessName();
58
59     process_dir.trimFilename();
60
61     complete = process_dir;
62     complete.join("wrappers");
63     complete.join(filename);
64 #else
65     complete = APITRACE_BINARY_DIR "/wrappers";
66     complete.join(filename);
67 #endif
68
69     if (complete.exists())
70         return complete;
71
72     /* Second, look in the directory for installed wrappers. */
73     complete = APITRACE_WRAPPER_INSTALL_DIR;
74     complete.join(filename);
75
76     if (complete.exists())
77         return complete;
78
79     std::cerr << "error: cannot find " << filename << " (looked in " <<
80         APITRACE_WRAPPER_INSTALL_DIR << ")\n";
81     exit(1);
82
83     return "";
84 }
85
86 int
87 traceProgram(char * const *argv,
88              const char *output,
89              bool verbose)
90 {
91 #ifdef _WIN32
92
93     std::cerr <<
94         "The 'apitrace trace' command is not supported for this operating system.\n"
95         "Instead, you will need to copy opengl32.dll, d3d8.dll, or d3d9.dll from\n"
96         APITRACE_WRAPPER_INSTALL_DIR "\n"
97         "to the directory with the application to trace, then run the application.\n";
98     return 1;
99
100 #else
101     os::Path binary = findWrapper(CLI_TRACE_WRAPPER, verbose);
102
103     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
104      * directory, not the file. */
105 #ifdef __APPLE__
106     binary.trimFilename();
107 #endif
108
109     if (verbose) {
110         std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
111     }
112
113     /* FIXME: Don't modify the current environment */
114     setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
115
116     if (output) {
117         setenv("TRACE_FILE", output, 1);
118     }
119
120     if (verbose) {
121         const char *sep = "";
122         for (char * const * arg = argv; *arg; ++arg) {
123             std::cerr << *arg << sep;
124             sep = " ";
125         }
126         std::cerr << "\n";
127     }
128
129     execvp(argv[0], argv);
130     
131     unsetenv(CLI_TRACE_VARIABLE);
132     if (output) {
133         unsetenv("TRACE_FILE");
134     }
135
136     std::cerr << "error: Failed to execute " << argv[0] << "\n";
137 #endif
138
139     return 1;
140 }
141
142
143 } /* namespace trace */