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