]> git.cworth.org Git - apitrace/blob - common/trace_tools_trace.cpp
Support D3D apis on apitrace trace command.
[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 <stdlib.h>
30
31 #include <iostream>
32
33 #include "os_string.hpp"
34 #include "os_process.hpp"
35 #include "trace_resource.hpp"
36 #include "trace_tools.hpp"
37
38
39
40 namespace trace {
41
42
43 #if defined(__APPLE__)
44 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
45 #define GL_TRACE_WRAPPER  "OpenGL"
46 #elif defined(_WIN32)
47 #define TRACE_VARIABLE ""
48 #define GL_TRACE_WRAPPER  "opengl32.dll"
49 #else
50 #define TRACE_VARIABLE "LD_PRELOAD"
51 #define GL_TRACE_WRAPPER  "glxtrace.so"
52 #define EGL_TRACE_WRAPPER  "egltrace.so"
53 #endif
54
55
56 int
57 traceProgram(API api,
58              char * const *argv,
59              const char *output,
60              bool verbose)
61 {
62     const char *relPath;
63     const char *absPath;
64
65     /*
66      * TODO: simplify code
67      */
68
69     switch (api) {
70     case API_GL:
71         relPath = "wrappers/" GL_TRACE_WRAPPER;
72         absPath = APITRACE_WRAPPER_INSTALL_DIR "/" GL_TRACE_WRAPPER;
73         break;
74     case API_EGL:
75 #ifndef EGL_TRACE_WRAPPER
76         std::cerr << "error: unsupported API\n";
77         return 1;
78 #else
79         relPath = "wrappers/" EGL_TRACE_WRAPPER;
80         absPath = APITRACE_WRAPPER_INSTALL_DIR "/" EGL_TRACE_WRAPPER;
81         break;
82 #endif
83 #ifdef _WIN32
84     case API_D3D7:
85         relPath = "wrappers\\ddraw.dll";
86         absPath = APITRACE_WRAPPER_INSTALL_DIR "\\ddraw.dll";
87         break;
88     case API_D3D8:
89         relPath = "wrappers\\d3d8.dll";
90         absPath = APITRACE_WRAPPER_INSTALL_DIR "\\d3d8.dll";
91         break;
92     case API_D3D9:
93         relPath = "wrappers\\d3d9.dll";
94         absPath = APITRACE_WRAPPER_INSTALL_DIR "\\d3d9.dll";
95         break;
96     case API_D3D10:
97         relPath = "wrappers\\d3d10.dll";
98         absPath = APITRACE_WRAPPER_INSTALL_DIR "\\d3d10.dll";
99         break;
100 #endif
101     default:
102         std::cerr << "error: unsupported API\n";
103         return 1;
104     }
105
106     os::String wrapper;
107     wrapper = findFile(relPath, absPath, verbose);
108
109     if (!wrapper.length()) {
110         return 1;
111     }
112
113 #if defined(_WIN32)
114     /* On Windows copt the wrapper to the program directory.
115      */
116     os::String wrapperName (wrapper);
117     wrapperName.trimDirectory();
118
119     os::String tmpWrapper(argv[0]);
120     tmpWrapper.trimFilename();
121     tmpWrapper.join(wrapperName);
122
123     if (tmpWrapper.exists()) {
124         std::cerr << "error: not overwriting " << tmpWrapper << "\n";
125         return 1;
126     }
127
128     if (!os::copyFile(wrapper, tmpWrapper, false)) {
129         std::cerr << "error: failed to copy " << wrapper << " into " << tmpWrapper << "\n";
130         return 1;
131     }
132 #endif /* _WIN32 */
133
134 #if defined(__APPLE__)
135     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
136      * directory, not the file. */
137     wrapper.trimFilename();
138 #endif
139
140 #if defined(TRACE_VARIABLE)
141
142     if (verbose) {
143         std::cerr << TRACE_VARIABLE << "=" << wrapper.str() << "\n";
144     }
145
146     /* FIXME: Don't modify the current environment */
147     os::setEnvironment(TRACE_VARIABLE, wrapper.str());
148
149 #endif /* TRACE_VARIABLE */
150
151     if (output) {
152         os::setEnvironment("TRACE_FILE", output);
153     }
154
155     if (verbose) {
156         const char *sep = "";
157         for (char * const * arg = argv; *arg; ++arg) {
158             std::cerr << *arg << sep;
159             sep = " ";
160         }
161         std::cerr << "\n";
162     }
163
164     int status = os::execute(argv);
165
166 #if defined(TRACE_VARIABLE)
167     os::unsetEnvironment(TRACE_VARIABLE);
168 #endif
169 #if defined(_WIN32)
170     os::removeFile(tmpWrapper);
171 #endif
172
173     if (output) {
174         os::unsetEnvironment("TRACE_FILE");
175     }
176     
177     return status;
178
179 }
180
181
182 } /* namespace trace */