]> git.cworth.org Git - apitrace/blob - common/trace_tools_trace.cpp
Add an "--api" option to "apitrace trace" allowing to choose egl.
[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_path.hpp"
34 #include "trace_tools.hpp"
35
36
37
38 namespace trace {
39
40
41 #if defined(__APPLE__)
42 #define TRACE_VARIABLE "DYLD_LIBRARY_PATH"
43 #define GL_TRACE_WRAPPER  "OpenGL"
44 #elif defined(_WIN32)
45 #define TRACE_VARIABLE ""
46 #define GL_TRACE_WRAPPER  "opengl32.dll"
47 #else
48 #define TRACE_VARIABLE "LD_PRELOAD"
49 #define GL_TRACE_WRAPPER  "glxtrace.so"
50 #define EGL_TRACE_WRAPPER  "egltrace.so"
51 #endif
52
53
54 os::Path
55 findFile(const char *relPath,
56          const char *absPath,
57          bool verbose)
58 {
59     os::Path complete;
60
61     /* First look in the same directory from which this process is
62      * running, (to support developers running a compiled program that
63      * has not been installed. */
64     os::Path process_dir = os::getProcessName();
65
66     process_dir.trimFilename();
67
68     complete = process_dir;
69     complete.join(relPath);
70
71     if (complete.exists())
72         return complete;
73
74     /* Second, look in the directory for installed wrappers. */
75     complete = absPath;
76     if (complete.exists())
77         return complete;
78
79     if (verbose) {
80         std::cerr << "error: cannot find " << relPath << " or " << absPath << "\n";
81     }
82
83     return "";
84 }
85
86
87 int
88 traceProgram(API api,
89              char * const *argv,
90              const char *output,
91              bool verbose)
92 {
93     const char *relPath;
94     const char *absPath;
95
96     switch (api) {
97     case API_GL:
98         relPath = "wrappers/" GL_TRACE_WRAPPER;
99         absPath = APITRACE_WRAPPER_INSTALL_DIR "/" GL_TRACE_WRAPPER;
100         break;
101     case API_EGL:
102 #ifndef EGL_TRACE_WRAPPER
103         std::cerr << "error: unsupported API\n";
104         return 1;
105 #else
106         relPath = "wrappers/" EGL_TRACE_WRAPPER;
107         absPath = APITRACE_WRAPPER_INSTALL_DIR "/" EGL_TRACE_WRAPPER;
108         break;
109 #endif
110     default:
111         std::cerr << "error: invalid API\n";
112         return 1;
113     }
114
115     os::Path wrapper;
116     wrapper = findFile(relPath, absPath, verbose);
117
118     if (!wrapper.length()) {
119         return 1;
120     }
121
122 #if defined(_WIN32)
123
124     std::cerr <<
125         "The 'apitrace trace' command is not supported for this operating system.\n"
126         "Instead, you will need to copy opengl32.dll, d3d8.dll, or d3d9.dll from\n"
127         APITRACE_WRAPPER_INSTALL_DIR "\n"
128         "to the directory with the application to trace, then run the application.\n";
129
130 #else
131
132 #if defined(__APPLE__)
133     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
134      * directory, not the file. */
135     wrapper.trimFilename();
136 #endif
137
138     if (verbose) {
139         std::cerr << TRACE_VARIABLE << "=" << wrapper.str() << "\n";
140     }
141
142     /* FIXME: Don't modify the current environment */
143     setenv(TRACE_VARIABLE, wrapper.str(), 1);
144
145     if (output) {
146         setenv("TRACE_FILE", output, 1);
147     }
148
149     if (verbose) {
150         const char *sep = "";
151         for (char * const * arg = argv; *arg; ++arg) {
152             std::cerr << *arg << sep;
153             sep = " ";
154         }
155         std::cerr << "\n";
156     }
157
158     execvp(argv[0], argv);
159
160     unsetenv(TRACE_VARIABLE);
161     if (output) {
162         unsetenv("TRACE_FILE");
163     }
164
165     std::cerr << "error: Failed to execute " << argv[0] << "\n";
166 #endif
167
168     return 1;
169 }
170
171
172 } /* namespace trace */