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