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 #warning PATH_MAX undefined
54 #include "os_string.hpp"
64 size_t size = PATH_MAX;
65 char *buf = path.buf(size);
67 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
70 if (_NSGetExecutablePath(buf, &len) != 0) {
77 len = readlink("/proc/self/exe", buf, size - 1);
79 // /proc/self/exe is not available on setuid processes, so fallback to
80 // /proc/self/cmdline.
81 int fd = open("/proc/self/cmdline", O_RDONLY);
83 len = read(fd, buf, size - 1);
88 snprintf(buf, size, "%i", (int)getpid());
101 size_t size = PATH_MAX;
102 char *buf = path.buf(size);
112 String::exists(void) const
117 err = stat(str(), &st);
122 if (!S_ISREG(st.st_mode))
128 int execute(char * const * args)
133 execvp(args[0], args);
134 fprintf(stderr, "error: failed to execute %s\n", args[0]);
139 fprintf(stderr, "error: failed to fork\n");
143 waitpid(pid, &status, 0);
148 static volatile bool logging = false;
151 log(const char *format, ...)
155 va_start(ap, format);
157 vfprintf(stderr, format, ap);
166 gettimeofday(&tv, NULL);
167 return tv.tv_usec + tv.tv_sec*1000000LL;
177 static void (*gCallback)(void) = NULL;
179 #define NUM_SIGNALS 16
181 struct sigaction old_actions[NUM_SIGNALS];
186 * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
187 * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
190 signalHandler(int sig, siginfo_t *info, void *context)
193 * There are several signals that can happen when logging to stdout/stderr.
194 * For example, SIGPIPE will be emitted if stderr is a pipe with no
195 * readers. Therefore ignore any signal while logging by returning
196 * immediately, to prevent deadlocks.
202 static int recursion_count = 0;
204 log("apitrace: warning: caught signal %i\n", sig);
206 if (recursion_count) {
207 log("apitrace: warning: recursion handling signal %i\n", sig);
216 struct sigaction *old_action;
217 if (sig >= NUM_SIGNALS) {
218 /* This should never happen */
219 log("error: unexpected signal %i\n", sig);
222 old_action = &old_actions[sig];
224 if (old_action->sa_flags & SA_SIGINFO) {
225 // Handler is in sa_sigaction
226 old_action->sa_sigaction(sig, info, context);
228 if (old_action->sa_handler == SIG_DFL) {
229 log("apitrace: info: taking default action for signal %i\n", sig);
232 struct sigaction dfl_action;
233 dfl_action.sa_handler = SIG_DFL;
234 sigemptyset (&dfl_action.sa_mask);
235 dfl_action.sa_flags = 0;
236 sigaction(sig, &dfl_action, NULL);
242 } else if (old_action->sa_handler == SIG_IGN) {
245 /* dispatch to handler */
246 old_action->sa_handler(sig);
252 setExceptionCallback(void (*callback)(void))
256 gCallback = callback;
258 struct sigaction new_action;
259 new_action.sa_sigaction = signalHandler;
260 sigemptyset(&new_action.sa_mask);
261 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
264 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
265 // SIGKILL and SIGSTOP can't be handled.
266 if (sig == SIGKILL || sig == SIGSTOP) {
271 * SIGPIPE can be emitted when writing to stderr that is redirected
272 * to a pipe without readers. It is also very unlikely to ocurr
273 * inside graphics APIs, and most applications where it can occur
274 * normally already ignore it. In summary, it is unlikely that a
275 * SIGPIPE will cause abnormal termination, which it is likely that
276 * intercepting here will cause problems, so simple don't intercept
279 if (sig == SIGPIPE) {
283 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
284 sigaction(sig, &new_action, NULL);
291 resetExceptionCallback(void)