1 /**************************************************************************
3 * Copyright 2010-2011 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
38 #if defined(__linux__)
39 #include <linux/limits.h> // PATH_MAX
43 #include <sys/syslimits.h> // PATH_MAX
44 #include <mach-o/dyld.h>
48 #include <android/log.h>
52 #warning PATH_MAX undefined
57 #include "os_string.hpp"
67 size_t size = PATH_MAX;
68 char *buf = path.buf(size);
70 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
73 if (_NSGetExecutablePath(buf, &len) != 0) {
80 len = readlink("/proc/self/exe", buf, size - 1);
82 // /proc/self/exe is not available on setuid processes, so fallback to
83 // /proc/self/cmdline.
84 int fd = open("/proc/self/cmdline", O_RDONLY);
86 len = read(fd, buf, size - 1);
91 snprintf(buf, size, "%i", (int)getpid());
104 size_t size = PATH_MAX;
105 char *buf = path.buf(size);
115 createDirectory(const String &path)
117 return mkdir(path, 0777) == 0;
121 String::exists(void) const
126 err = stat(str(), &st);
131 if (!S_ISREG(st.st_mode))
137 int execute(char * const * args)
142 execvp(args[0], args);
143 fprintf(stderr, "error: failed to execute:");
144 for (unsigned i = 0; args[i]; ++i) {
145 fprintf(stderr, " %s", args[i]);
147 fprintf(stderr, "\n");
152 fprintf(stderr, "error: failed to fork\n");
157 waitpid(pid, &status, 0);
158 if (WIFEXITED(status)) {
159 ret = WEXITSTATUS(status);
160 } else if (WIFSIGNALED(status)) {
161 // match shell return code
162 ret = WTERMSIG(status) + 128;
170 static volatile bool logging = false;
173 log(const char *format, ...)
177 va_start(ap, format);
180 __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
182 static FILE *log = NULL;
184 // Duplicate stderr file descriptor, to prevent applications from
185 // redirecting our debug messages to somewhere else.
187 // Another alternative would be to log to /dev/tty when available.
188 log = fdopen(dup(STDERR_FILENO), "at");
190 vfprintf(log, format, ap);
197 #if defined(__APPLE__)
198 long long timeFrequency = 0LL;
208 static void (*gCallback)(void) = NULL;
210 #define NUM_SIGNALS 16
212 struct sigaction old_actions[NUM_SIGNALS];
217 * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
218 * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
221 signalHandler(int sig, siginfo_t *info, void *context)
224 * There are several signals that can happen when logging to stdout/stderr.
225 * For example, SIGPIPE will be emitted if stderr is a pipe with no
226 * readers. Therefore ignore any signal while logging by returning
227 * immediately, to prevent deadlocks.
233 static int recursion_count = 0;
235 log("apitrace: warning: caught signal %i\n", sig);
237 if (recursion_count) {
238 log("apitrace: warning: recursion handling signal %i\n", sig);
247 struct sigaction *old_action;
248 if (sig >= NUM_SIGNALS) {
249 /* This should never happen */
250 log("error: unexpected signal %i\n", sig);
253 old_action = &old_actions[sig];
255 if (old_action->sa_flags & SA_SIGINFO) {
256 // Handler is in sa_sigaction
257 old_action->sa_sigaction(sig, info, context);
259 if (old_action->sa_handler == SIG_DFL) {
260 log("apitrace: info: taking default action for signal %i\n", sig);
263 struct sigaction dfl_action;
264 dfl_action.sa_handler = SIG_DFL;
265 sigemptyset (&dfl_action.sa_mask);
266 dfl_action.sa_flags = 0;
267 sigaction(sig, &dfl_action, NULL);
273 } else if (old_action->sa_handler == SIG_IGN) {
276 /* dispatch to handler */
277 old_action->sa_handler(sig);
283 setExceptionCallback(void (*callback)(void))
287 gCallback = callback;
289 struct sigaction new_action;
290 new_action.sa_sigaction = signalHandler;
291 sigemptyset(&new_action.sa_mask);
292 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
295 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
296 // SIGKILL and SIGSTOP can't be handled.
297 if (sig == SIGKILL || sig == SIGSTOP) {
302 * SIGPIPE can be emitted when writing to stderr that is redirected
303 * to a pipe without readers. It is also very unlikely to ocurr
304 * inside graphics APIs, and most applications where it can occur
305 * normally already ignore it. In summary, it is unlikely that a
306 * SIGPIPE will cause abnormal termination, which it is likely that
307 * intercepting here will cause problems, so simple don't intercept
310 if (sig == SIGPIPE) {
314 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
315 sigaction(sig, &new_action, NULL);
322 resetExceptionCallback(void)