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 **************************************************************************/
40 #if defined(__linux__)
41 #include <linux/limits.h> // PATH_MAX
45 #include <sys/syslimits.h> // PATH_MAX
46 #include <mach-o/dyld.h>
50 #warning PATH_MAX undefined
55 #include "os_path.hpp"
61 static pthread_mutex_t
62 mutex = PTHREAD_MUTEX_INITIALIZER;
68 pthread_mutex_lock(&mutex);
75 pthread_mutex_unlock(&mutex);
83 size_t size = PATH_MAX;
84 char *buf = path.buf(size);
86 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
89 if (_NSGetExecutablePath(buf, &len) != 0) {
96 len = readlink("/proc/self/exe", buf, size - 1);
98 // /proc/self/exe is not available on setuid processes, so fallback to
99 // /proc/self/cmdline.
100 int fd = open("/proc/self/cmdline", O_RDONLY);
102 len = read(fd, buf, size - 1);
107 snprintf(buf, size, "%i", (int)getpid());
120 size_t size = PATH_MAX;
121 char *buf = path.buf(size);
131 Path::exists(void) const
136 err = stat(str(), &st);
141 if (!S_ISREG(st.st_mode))
147 int execute(char * const * args)
152 execvp(args[0], args);
153 fprintf(stderr, "error: failed to execute %s\n", args[0]);
158 fprintf(stderr, "error: failed to fork\n");
162 waitpid(pid, &status, 0);
168 log(const char *format, ...)
171 va_start(ap, format);
173 vfprintf(stderr, format, ap);
181 gettimeofday(&tv, NULL);
182 return tv.tv_usec + tv.tv_sec*1000000LL;
192 static void (*gCallback)(void) = NULL;
194 #define NUM_SIGNALS 16
196 struct sigaction old_actions[NUM_SIGNALS];
201 * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
202 * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
205 signalHandler(int sig, siginfo_t *info, void *context)
207 static int recursion_count = 0;
209 fprintf(stderr, "apitrace: warning: caught signal %i\n", sig);
211 if (recursion_count) {
212 fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig);
221 struct sigaction *old_action;
222 if (sig >= NUM_SIGNALS) {
223 /* This should never happen */
224 fprintf(stderr, "error: unexpected signal %i\n", sig);
227 old_action = &old_actions[sig];
229 if (old_action->sa_flags & SA_SIGINFO) {
230 // Handler is in sa_sigaction
231 old_action->sa_sigaction(sig, info, context);
233 if (old_action->sa_handler == SIG_DFL) {
234 fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig);
237 struct sigaction dfl_action;
238 dfl_action.sa_handler = SIG_DFL;
239 sigemptyset (&dfl_action.sa_mask);
240 dfl_action.sa_flags = 0;
241 sigaction(sig, &dfl_action, NULL);
247 } else if (old_action->sa_handler == SIG_IGN) {
250 /* dispatch to handler */
251 old_action->sa_handler(sig);
257 setExceptionCallback(void (*callback)(void))
261 gCallback = callback;
263 struct sigaction new_action;
264 new_action.sa_sigaction = signalHandler;
265 sigemptyset(&new_action.sa_mask);
266 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
269 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
270 // SIGKILL and SIGSTOP can't be handled
271 if (sig != SIGKILL && sig != SIGSTOP) {
272 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
273 sigaction(sig, &new_action, NULL);
281 resetExceptionCallback(void)