]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
ef329d08082c74885c130d8d9449e201a4ecf2f5
[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 #if defined(__linux__)
40 #include <linux/limits.h> // PATH_MAX
41 #endif
42
43 #ifdef __APPLE__
44 #include <mach-o/dyld.h>
45 #endif
46
47 #ifndef PATH_MAX
48 #warning PATH_MAX undefined
49 #define PATH_MAX 4096
50 #endif
51
52 #include "os.hpp"
53 #include "os_path.hpp"
54
55
56 namespace os {
57
58
59 static pthread_mutex_t 
60 mutex = PTHREAD_MUTEX_INITIALIZER;
61
62
63 void
64 acquireMutex(void)
65 {
66     pthread_mutex_lock(&mutex);
67 }
68
69
70 void
71 releaseMutex(void)
72 {
73     pthread_mutex_unlock(&mutex);
74 }
75
76
77 Path
78 getProcessName(void)
79 {
80     Path path;
81     size_t size = PATH_MAX;
82     char *buf = path.buf(size);
83
84     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
85 #ifdef __APPLE__
86     uint32_t len = size;
87     if (_NSGetExecutablePath(buf, &len) != 0) {
88         *buf = 0;
89         return path;
90     }
91 #else
92     ssize_t len;
93     len = readlink("/proc/self/exe", buf, size - 1);
94     if (len == -1) {
95         // /proc/self/exe is not available on setuid processes, so fallback to
96         // /proc/self/cmdline.
97         int fd = open("/proc/self/cmdline", O_RDONLY);
98         if (fd >= 0) {
99             len = read(fd, buf, size - 1);
100             close(fd);
101         }
102     }
103     if (len <= 0) {
104         snprintf(buf, size, "%i", (int)getpid());
105         return path;
106     }
107 #endif
108     path.truncate(len);
109
110     return path;
111 }
112
113 Path
114 getCurrentDir(void)
115 {
116     Path path;
117     size_t size = PATH_MAX;
118     char *buf = path.buf(size);
119
120     getcwd(buf, size);
121     buf[size - 1] = 0;
122     
123     path.truncate();
124     return path;
125 }
126
127 void
128 log(const char *format, ...)
129 {
130     va_list ap;
131     va_start(ap, format);
132     fflush(stdout);
133     vfprintf(stderr, format, ap);
134     va_end(ap);
135 }
136
137 long long
138 getTime(void)
139 {
140     struct timeval tv;
141     gettimeofday(&tv, NULL);
142     return tv.tv_usec + tv.tv_sec*1000000LL;
143 }
144
145 void
146 abort(void)
147 {
148     exit(0);
149 }
150
151
152 static void (*gCallback)(void) = NULL;
153
154 #define NUM_SIGNALS 16
155
156 struct sigaction old_actions[NUM_SIGNALS];
157
158
159 /*
160  * See also:
161  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
162  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
163  */
164 static void
165 signalHandler(int sig, siginfo_t *info, void *context)
166 {
167     static int recursion_count = 0;
168
169     fprintf(stderr, "apitrace: warning: caught signal %i\n", sig);
170
171     if (recursion_count) {
172         fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig);
173     } else {
174         if (gCallback) {
175             ++recursion_count;
176             gCallback();
177             --recursion_count;
178         }
179     }
180
181     struct sigaction *old_action;
182     if (sig >= NUM_SIGNALS) {
183         /* This should never happen */
184         fprintf(stderr, "error: unexpected signal %i\n", sig);
185         raise(SIGKILL);
186     }
187     old_action = &old_actions[sig];
188
189     if (old_action->sa_flags & SA_SIGINFO) {
190         // Handler is in sa_sigaction
191         old_action->sa_sigaction(sig, info, context);
192     } else {
193         if (old_action->sa_handler == SIG_DFL) {
194             fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig);
195
196 #if 1
197             struct sigaction dfl_action;
198             dfl_action.sa_handler = SIG_DFL;
199             sigemptyset (&dfl_action.sa_mask);
200             dfl_action.sa_flags = 0;
201             sigaction(sig, &dfl_action, NULL);
202
203             raise(sig);
204 #else
205             raise(SIGKILL);
206 #endif
207         } else if (old_action->sa_handler == SIG_IGN) {
208             /* ignore */
209         } else {
210             /* dispatch to handler */
211             old_action->sa_handler(sig);
212         }
213     }
214 }
215
216 void
217 setExceptionCallback(void (*callback)(void))
218 {
219     assert(!gCallback);
220     if (!gCallback) {
221         gCallback = callback;
222
223         struct sigaction new_action;
224         new_action.sa_sigaction = signalHandler;
225         sigemptyset(&new_action.sa_mask);
226         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
227
228
229         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
230             // SIGKILL and SIGSTOP can't be handled
231             if (sig != SIGKILL && sig != SIGSTOP) {
232                 if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
233                     sigaction(sig,  &new_action, NULL);
234                 }
235             }
236         }
237     }
238 }
239
240 void
241 resetExceptionCallback(void)
242 {
243     gCallback = NULL;
244 }
245
246 } /* namespace os */
247