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 #include <mach-o/dyld.h>
49 static pthread_mutex_t
50 mutex = PTHREAD_MUTEX_INITIALIZER;
56 pthread_mutex_lock(&mutex);
63 pthread_mutex_unlock(&mutex);
68 GetProcessName(char *str, size_t size)
70 char szProcessPath[PATH_MAX + 1];
73 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
75 uint32_t len = sizeof szProcessPath;
76 if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
82 len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 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 len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
93 snprintf(str, size, "%i", (int)getpid());
97 szProcessPath[len] = 0;
99 lpProcessName = strrchr(szProcessPath, '/');
100 lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
102 strncpy(str, lpProcessName, size);
110 GetCurrentDir(char *str, size_t size)
113 ret = getcwd(str, size);
115 return ret ? true : false;
119 DebugMessage(const char *format, ...)
122 va_start(ap, format);
124 vfprintf(stderr, format, ap);
128 long long GetTime(void)
131 gettimeofday(&tv, NULL);
132 return tv.tv_usec + tv.tv_sec*1000000LL;
142 static void (*gCallback)(void) = NULL;
144 #define NUM_SIGNALS 16
146 struct sigaction old_actions[NUM_SIGNALS];
151 * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
152 * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
154 static void signal_handler(int sig, siginfo_t *info, void *context)
156 static int recursion_count = 0;
158 fprintf(stderr, "signal_handler: sig = %i\n", sig);
160 if (recursion_count) {
161 fprintf(stderr, "recursion with sig %i\n", sig);
170 struct sigaction *old_action;
171 if (sig >= NUM_SIGNALS) {
172 /* This should never happen */
173 fprintf(stderr, "Unexpected signal %i\n", sig);
176 old_action = &old_actions[sig];
178 if (old_action->sa_flags & SA_SIGINFO) {
179 // Handler is in sa_sigaction
180 old_action->sa_sigaction(sig, info, context);
182 if (old_action->sa_handler == SIG_DFL) {
183 fprintf(stderr, "taking default action for signal %i\n", sig);
186 struct sigaction dfl_action;
187 dfl_action.sa_handler = SIG_DFL;
188 sigemptyset (&dfl_action.sa_mask);
189 dfl_action.sa_flags = 0;
190 sigaction(sig, &dfl_action, NULL);
196 } else if (old_action->sa_handler == SIG_IGN) {
199 /* dispatch to handler */
200 old_action->sa_handler(sig);
206 SetExceptionCallback(void (*callback)(void))
210 gCallback = callback;
212 struct sigaction new_action;
213 new_action.sa_sigaction = signal_handler;
214 sigemptyset(&new_action.sa_mask);
215 new_action.sa_flags = SA_SIGINFO | SA_RESTART;
218 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
219 // SIGKILL and SIGSTOP can't be handled
220 if (sig != SIGKILL && sig != SIGSTOP) {
221 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
222 sigaction(sig, &new_action, NULL);
230 ResetExceptionCallback(void)