]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
e797220189f4373f27198ef46ad6b12eb42bd304
[apitrace] / common / os_posix.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010-2011 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <pthread.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <signal.h>
38
39 #ifdef __APPLE__
40 #include <mach-o/dyld.h>
41 #endif
42
43 #include "os.hpp"
44
45
46 namespace os {
47
48
49 static pthread_mutex_t 
50 mutex = PTHREAD_MUTEX_INITIALIZER;
51
52
53 void
54 acquireMutex(void)
55 {
56     pthread_mutex_lock(&mutex);
57 }
58
59
60 void
61 releaseMutex(void)
62 {
63     pthread_mutex_unlock(&mutex);
64 }
65
66
67 bool
68 getProcessName(char *str, size_t size)
69 {
70     char szProcessPath[PATH_MAX + 1];
71     char *lpProcessName;
72
73     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
74 #ifdef __APPLE__
75     uint32_t len = sizeof szProcessPath;
76     if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
77         *str = 0;
78         return false;
79     }
80 #else
81     ssize_t len;
82     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
83     if (len == -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);
87         if (fd >= 0) {
88             len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
89             close(fd);
90         }
91     }
92     if (len <= 0) {
93         snprintf(str, size, "%i", (int)getpid());
94         return true;
95     }
96 #endif
97     szProcessPath[len] = 0;
98
99     lpProcessName = strrchr(szProcessPath, '/');
100     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
101
102     strncpy(str, lpProcessName, size);
103     if (size)
104         str[size - 1] = 0;
105
106     return true;
107 }
108
109 bool
110 getCurrentDir(char *str, size_t size)
111 {
112     char *ret;
113     ret = getcwd(str, size);
114     str[size - 1] = 0;
115     return ret ? true : false;
116 }
117
118 void
119 log(const char *format, ...)
120 {
121     va_list ap;
122     va_start(ap, format);
123     fflush(stdout);
124     vfprintf(stderr, format, ap);
125     va_end(ap);
126 }
127
128 long long
129 getTime(void)
130 {
131     struct timeval tv;
132     gettimeofday(&tv, NULL);
133     return tv.tv_usec + tv.tv_sec*1000000LL;
134 }
135
136 void
137 abort(void)
138 {
139     exit(0);
140 }
141
142
143 static void (*gCallback)(void) = NULL;
144
145 #define NUM_SIGNALS 16
146
147 struct sigaction old_actions[NUM_SIGNALS];
148
149
150 /*
151  * See also:
152  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
153  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
154  */
155 static void
156 signalHandler(int sig, siginfo_t *info, void *context)
157 {
158     static int recursion_count = 0;
159
160     fprintf(stderr, "apitrace: warning: caught signal %i\n", sig);
161
162     if (recursion_count) {
163         fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig);
164     } else {
165         if (gCallback) {
166             ++recursion_count;
167             gCallback();
168             --recursion_count;
169         }
170     }
171
172     struct sigaction *old_action;
173     if (sig >= NUM_SIGNALS) {
174         /* This should never happen */
175         fprintf(stderr, "error: unexpected signal %i\n", sig);
176         raise(SIGKILL);
177     }
178     old_action = &old_actions[sig];
179
180     if (old_action->sa_flags & SA_SIGINFO) {
181         // Handler is in sa_sigaction
182         old_action->sa_sigaction(sig, info, context);
183     } else {
184         if (old_action->sa_handler == SIG_DFL) {
185             fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig);
186
187 #if 1
188             struct sigaction dfl_action;
189             dfl_action.sa_handler = SIG_DFL;
190             sigemptyset (&dfl_action.sa_mask);
191             dfl_action.sa_flags = 0;
192             sigaction(sig, &dfl_action, NULL);
193
194             raise(sig);
195 #else
196             raise(SIGKILL);
197 #endif
198         } else if (old_action->sa_handler == SIG_IGN) {
199             /* ignore */
200         } else {
201             /* dispatch to handler */
202             old_action->sa_handler(sig);
203         }
204     }
205 }
206
207 void
208 setExceptionCallback(void (*callback)(void))
209 {
210     assert(!gCallback);
211     if (!gCallback) {
212         gCallback = callback;
213
214         struct sigaction new_action;
215         new_action.sa_sigaction = signalHandler;
216         sigemptyset(&new_action.sa_mask);
217         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
218
219
220         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
221             // SIGKILL and SIGSTOP can't be handled
222             if (sig != SIGKILL && sig != SIGSTOP) {
223                 if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
224                     sigaction(sig,  &new_action, NULL);
225                 }
226             }
227         }
228     }
229 }
230
231 void
232 resetExceptionCallback(void)
233 {
234     gCallback = NULL;
235 }
236
237 } /* namespace os */
238