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 **************************************************************************/
39 #if defined(__linux__)
40 #include <linux/limits.h> // PATH_MAX
44 #include <sys/syslimits.h> // PATH_MAX
45 #include <mach-o/dyld.h>
49 #include <android/log.h>
53 #warning PATH_MAX undefined
58 #include "os_string.hpp"
68 size_t size = PATH_MAX;
69 char *buf = path.buf(size);
71 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
74 if (_NSGetExecutablePath(buf, &len) != 0) {
77 _NSGetExecutablePath(buf, &len);
82 len = readlink("/proc/self/exe", buf, size - 1);
84 // /proc/self/exe is not available on setuid processes, so fallback to
85 // /proc/self/cmdline.
86 int fd = open("/proc/self/cmdline", O_RDONLY);
88 // buf already includes trailing zero
89 len = read(fd, buf, size);
97 // fallback to process ID
98 len = snprintf(buf, size, "%i", (int)getpid());
113 size_t size = PATH_MAX;
114 char *buf = path.buf(size);
124 createDirectory(const String &path)
126 return mkdir(path, 0777) == 0;
130 String::exists(void) const
135 err = stat(str(), &st);
143 int execute(char * const * args)
148 execvp(args[0], args);
149 fprintf(stderr, "error: failed to execute:");
150 for (unsigned i = 0; args[i]; ++i) {
151 fprintf(stderr, " %s", args[i]);
153 fprintf(stderr, "\n");
158 fprintf(stderr, "error: failed to fork\n");
163 waitpid(pid, &status, 0);
164 if (WIFEXITED(status)) {
165 ret = WEXITSTATUS(status);
166 } else if (WIFSIGNALED(status)) {
167 // match shell return code
168 ret = WTERMSIG(status) + 128;
176 static volatile bool logging = false;
179 log(const char *format, ...)
183 va_start(ap, format);
186 __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
188 static FILE *log = NULL;
190 // Duplicate stderr file descriptor, to prevent applications from
191 // redirecting our debug messages to somewhere else.
193 // Another alternative would be to log to /dev/tty when available.
194 log = fdopen(dup(STDERR_FILENO), "at");
196 vfprintf(log, format, ap);
203 #if defined(__APPLE__)
204 long long timeFrequency = 0LL;
214 static void (*gCallback)(void) = NULL;
216 #define NUM_SIGNALS 16
218 struct sigaction old_actions[NUM_SIGNALS];
223 * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
224 * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
227 signalHandler(int sig, siginfo_t *info, void *context)
230 * There are several signals that can happen when logging to stdout/stderr.
231 * For example, SIGPIPE will be emitted if stderr is a pipe with no
232 * readers. Therefore ignore any signal while logging by returning
233 * immediately, to prevent deadlocks.
239 static int recursion_count = 0;
241 log("apitrace: warning: caught signal %i\n", sig);
243 if (recursion_count) {
244 log("apitrace: warning: recursion handling signal %i\n", sig);
253 struct sigaction *old_action;
254 if (sig >= NUM_SIGNALS) {
255 /* This should never happen */
256 log("error: unexpected signal %i\n", sig);
259 old_action = &old_actions[sig];
261 if (old_action->sa_flags & SA_SIGINFO) {
262 // Handler is in sa_sigaction
263 old_action->sa_sigaction(sig, info, context);
265 if (old_action->sa_handler == SIG_DFL) {
266 log("apitrace: info: taking default action for signal %i\n", sig);
269 struct sigaction dfl_action;
270 dfl_action.sa_handler = SIG_DFL;
271 sigemptyset (&dfl_action.sa_mask);
272 dfl_action.sa_flags = 0;
273 sigaction(sig, &dfl_action, NULL);
279 } else if (old_action->sa_handler == SIG_IGN) {
282 /* dispatch to handler */
283 old_action->sa_handler(sig);
289 setExceptionCallback(void (*callback)(void))
293 gCallback = callback;
295 struct sigaction new_action;
296 new_action.sa_sigaction = signalHandler;
297 sigemptyset(&new_action.sa_mask);
298 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
301 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
302 // SIGKILL and SIGSTOP can't be handled.
303 if (sig == SIGKILL || sig == SIGSTOP) {
308 * SIGPIPE can be emitted when writing to stderr that is redirected
309 * to a pipe without readers. It is also very unlikely to ocurr
310 * inside graphics APIs, and most applications where it can occur
311 * normally already ignore it. In summary, it is unlikely that a
312 * SIGPIPE will cause abnormal termination, which it is likely that
313 * intercepting here will cause problems, so simple don't intercept
316 if (sig == SIGPIPE) {
320 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
321 sigaction(sig, &new_action, NULL);
328 resetExceptionCallback(void)
335 #endif // !defined(_WIN32)