Skip to content
Newer
Older
100644 219 lines (181 sloc) 5.41 KB
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
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
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
29 #include <stdlib.h>
30
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
31 #include <iostream>
32
447b3d5 @jrfonseca Cleanup and generalize os string class.
jrfonseca authored
33 #include "os_string.hpp"
03c5d3d @jrfonseca Abstract execv().
jrfonseca authored
34 #include "os_process.hpp"
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
35 #include "trace_tools.hpp"
36
37
38
39 namespace trace {
40
41
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
42 #if defined(__APPLE__)
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
43 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
44 #define GL_TRACE_WRAPPER "OpenGL"
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
45 #elif defined(_WIN32)
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
46 #define GL_TRACE_WRAPPER "opengl32.dll"
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
47 #else
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
48 #define TRACE_VARIABLE "LD_PRELOAD"
49 #define GL_TRACE_WRAPPER "glxtrace.so"
50 #define EGL_TRACE_WRAPPER "egltrace.so"
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
51 #endif
52
53
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
54 static os::String
55 findWrapper(const char *wrapperFilename)
56 {
57 os::String wrapperPath;
58
59 os::String processDir = os::getProcessName();
60 processDir.trimFilename();
61
62 // Try relative build directory
63 // XXX: Just make build and install directory layout match
64 wrapperPath = processDir;
65 wrapperPath.join("wrappers");
66 wrapperPath.join(wrapperFilename);
67 if (wrapperPath.exists()) {
68 return wrapperPath;
69 }
70
71 // Try relative install directory
72 wrapperPath = processDir;
ab6ded7 @jrfonseca Don't install in per-architecture wrapper directories.
jrfonseca authored
73 #if defined(_WIN32)
74 wrapperPath.join("..\\lib\\wrappers");
75 #elif defined(__APPLE__)
76 wrapperPath.join("../lib/wrappers");
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
77 #else
78 wrapperPath.join("../lib/apitrace/wrappers");
79 #endif
80 wrapperPath.join(wrapperFilename);
81 if (wrapperPath.exists()) {
82 return wrapperPath;
83 }
84
85 #ifndef _WIN32
86 // Try absolute install directory
96851e0 @hramrach Add multiarch support.
hramrach authored
87 wrapperPath = APITRACE_WRAPPERS_INSTALL_DIR;
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
88 wrapperPath.join(wrapperFilename);
89 if (wrapperPath.exists()) {
90 return wrapperPath;
91 }
92 #endif
93
94 return "";
95 }
96
97
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
98 int
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
99 traceProgram(API api,
100 char * const *argv,
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
101 const char *output,
102 bool verbose)
103 {
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
104 const char *wrapperFilename;
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
105
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
106 /*
107 * TODO: simplify code
108 */
109
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
110 switch (api) {
111 case API_GL:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
112 wrapperFilename = GL_TRACE_WRAPPER;
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
113 break;
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
114 #ifdef EGL_TRACE_WRAPPER
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
115 case API_EGL:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
116 wrapperFilename = EGL_TRACE_WRAPPER;
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
117 break;
118 #endif
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
119 #ifdef _WIN32
120 case API_D3D7:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
121 wrapperFilename = "ddraw.dll";
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
122 break;
123 case API_D3D8:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
124 wrapperFilename = "d3d8.dll";
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
125 break;
126 case API_D3D9:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
127 wrapperFilename = "d3d9.dll";
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
128 break;
129 case API_D3D10:
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
130 wrapperFilename = "d3d10.dll";
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
131 break;
889d32c @jrfonseca Detect/handle more APIs in CLI/GUI.
jrfonseca authored
132 case API_D3D10_1:
133 wrapperFilename = "d3d10_1.dll";
134 break;
135 case API_D3D11:
136 wrapperFilename = "d3d11.dll";
137 break;
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
138 #endif
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
139 default:
386cc5f @jrfonseca Support D3D apis on apitrace trace command.
jrfonseca authored
140 std::cerr << "error: unsupported API\n";
8f28cd8 @jrfonseca Add an "--api" option to "apitrace trace" allowing to choose egl.
jrfonseca authored
141 return 1;
142 }
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
143
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
144 os::String wrapperPath = findWrapper(wrapperFilename);
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
145
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
146 if (!wrapperPath.length()) {
147 std::cerr << "error: failed to find " << wrapperFilename << "\n";
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
148 return 1;
149 }
150
151 #if defined(_WIN32)
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
152 /* On Windows copy the wrapper to the program directory.
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
153 */
154 os::String tmpWrapper(argv[0]);
155 tmpWrapper.trimFilename();
7f456db @jrfonseca Don't compute wrapper filename twice.
jrfonseca authored
156 tmpWrapper.join(wrapperFilename);
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
157
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
158 if (verbose) {
159 std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
160 }
161
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
162 if (tmpWrapper.exists()) {
163 std::cerr << "error: not overwriting " << tmpWrapper << "\n";
164 return 1;
165 }
03c5d3d @jrfonseca Abstract execv().
jrfonseca authored
166
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
167 if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
168 std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
169 return 1;
170 }
171 #endif /* _WIN32 */
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
172
bb8bb9f @jrfonseca Fix typo.
jrfonseca authored
173 #if defined(__APPLE__)
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
174 /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
175 * directory, not the file. */
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
176 wrapperPath.trimFilename();
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
177 #endif
178
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
179 #if defined(TRACE_VARIABLE)
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
180 if (verbose) {
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
181 std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
182 }
183 /* FIXME: Don't modify the current environment */
f7824e5 @jrfonseca Fix/cleanup wrapper search on Windows.
jrfonseca authored
184 os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
185 #endif /* TRACE_VARIABLE */
186
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
187 if (output) {
22bcfce @jrfonseca Add abstractions for (un)setting OS environ variables.
jrfonseca authored
188 os::setEnvironment("TRACE_FILE", output);
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
189 }
190
191 if (verbose) {
192 const char *sep = "";
193 for (char * const * arg = argv; *arg; ++arg) {
194 std::cerr << *arg << sep;
195 sep = " ";
196 }
197 std::cerr << "\n";
198 }
199
03c5d3d @jrfonseca Abstract execv().
jrfonseca authored
200 int status = os::execute(argv);
09879ec @jrfonseca Make findFile non-static, given it will be necessary for other commands.
jrfonseca authored
201
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
202 #if defined(TRACE_VARIABLE)
22bcfce @jrfonseca Add abstractions for (un)setting OS environ variables.
jrfonseca authored
203 os::unsetEnvironment(TRACE_VARIABLE);
8ba4a4a @jrfonseca Copy wrapper dll to executable directory on windows in apitrace trace.
jrfonseca authored
204 #endif
205 #if defined(_WIN32)
206 os::removeFile(tmpWrapper);
207 #endif
208
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
209 if (output) {
22bcfce @jrfonseca Add abstractions for (un)setting OS environ variables.
jrfonseca authored
210 os::unsetEnvironment("TRACE_FILE");
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
211 }
03c5d3d @jrfonseca Abstract execv().
jrfonseca authored
212
213 return status;
efb9e17 @jrfonseca Split common functionality out of cli.
jrfonseca authored
214
215 }
216
217
218 } /* namespace trace */
Something went wrong with that request. Please try again.