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