]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
967d12eba4116ddccd8f1c3389f165c7c8edc4c0
[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 #ifndef _WIN32
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <unistd.h>
34 #include <sys/wait.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 #ifdef ANDROID
49 #include <android/log.h>
50 #endif
51
52 #ifndef PATH_MAX
53 #warning PATH_MAX undefined
54 #define PATH_MAX 4096
55 #endif
56
57 #include "os.hpp"
58 #include "os_string.hpp"
59
60
61 namespace os {
62
63
64 String
65 getProcessName(void)
66 {
67     String path;
68     size_t size = PATH_MAX;
69     char *buf = path.buf(size);
70
71     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
72 #ifdef __APPLE__
73     uint32_t len = size;
74     if (_NSGetExecutablePath(buf, &len) != 0) {
75         // grow buf and retry
76         buf = path.buf(len);
77         _NSGetExecutablePath(buf, &len);
78     }
79     len = strlen(buf);
80 #else
81     ssize_t len;
82     len = readlink("/proc/self/exe", buf, size - 1);
83     if (len <= 0) {
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             // buf already includes trailing zero
89             len = read(fd, buf, size);
90             close(fd);
91             if (len >= 0) {
92                 len = strlen(buf);
93             }
94         }
95     }
96     if (len <= 0) {
97         // fallback to process ID
98         len = snprintf(buf, size, "%i", (int)getpid());
99         if (len >= size) {
100             len = size - 1;
101         }
102     }
103 #endif
104     path.truncate(len);
105
106     return path;
107 }
108
109 String
110 getCurrentDir(void)
111 {
112     String path;
113     size_t size = PATH_MAX;
114     char *buf = path.buf(size);
115
116     getcwd(buf, size);
117     buf[size - 1] = 0;
118     
119     path.truncate();
120     return path;
121 }
122
123 bool
124 createDirectory(const String &path)
125 {
126     return mkdir(path, 0777) == 0;
127 }
128
129 bool
130 String::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     return true;
141 }
142
143 int execute(char * const * args)
144 {
145     pid_t pid = fork();
146     if (pid == 0) {
147         // child
148         execvp(args[0], args);
149         fprintf(stderr, "error: failed to execute:");
150         for (unsigned i = 0; args[i]; ++i) {
151             fprintf(stderr, " %s", args[i]);
152         }
153         fprintf(stderr, "\n");
154         exit(-1);
155     } else {
156         // parent
157         if (pid == -1) {
158             fprintf(stderr, "error: failed to fork\n");
159             return -1;
160         }
161         int status = -1;
162         int ret;
163         waitpid(pid, &status, 0);
164         if (WIFEXITED(status)) {
165             ret = WEXITSTATUS(status);
166         } else if (WIFSIGNALED(status)) {
167             // match shell return code
168             ret = WTERMSIG(status) + 128;
169         } else {
170             ret = 128;
171         }
172         return ret;
173     }
174 }
175
176 static volatile bool logging = false;
177
178 void
179 log(const char *format, ...)
180 {
181     logging = true;
182     va_list ap;
183     va_start(ap, format);
184     fflush(stdout);
185 #ifdef ANDROID
186     __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
187 #else
188     static FILE *log = NULL;
189     if (!log) {
190         // Duplicate stderr file descriptor, to prevent applications from
191         // redirecting our debug messages to somewhere else.
192         //
193         // Another alternative would be to log to /dev/tty when available.
194         log = fdopen(dup(STDERR_FILENO), "at");
195     }
196     vfprintf(log, format, ap);
197     fflush(log);
198 #endif
199     va_end(ap);
200     logging = false;
201 }
202
203 #if defined(__APPLE__)
204 long long timeFrequency = 0LL;
205 #endif
206
207 void
208 abort(void)
209 {
210     _exit(1);
211 }
212
213
214 static void (*gCallback)(void) = NULL;
215
216 #define NUM_SIGNALS 16
217
218 struct sigaction old_actions[NUM_SIGNALS];
219
220
221 /*
222  * See also:
223  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
224  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
225  */
226 static void
227 signalHandler(int sig, siginfo_t *info, void *context)
228 {
229     /*
230      * There are several signals that can happen when logging to stdout/stderr.
231      * For example, SIGPIPE will be emitted if stderr is a pipe with no
232      * readers.  Therefore ignore any signal while logging by returning
233      * immediately, to prevent deadlocks.
234      */
235     if (logging) {
236         return;
237     }
238
239     static int recursion_count = 0;
240
241     log("apitrace: warning: caught signal %i\n", sig);
242
243     if (recursion_count) {
244         log("apitrace: warning: recursion handling signal %i\n", sig);
245     } else {
246         if (gCallback) {
247             ++recursion_count;
248             gCallback();
249             --recursion_count;
250         }
251     }
252
253     struct sigaction *old_action;
254     if (sig >= NUM_SIGNALS) {
255         /* This should never happen */
256         log("error: unexpected signal %i\n", sig);
257         raise(SIGKILL);
258     }
259     old_action = &old_actions[sig];
260
261     if (old_action->sa_flags & SA_SIGINFO) {
262         // Handler is in sa_sigaction
263         old_action->sa_sigaction(sig, info, context);
264     } else {
265         if (old_action->sa_handler == SIG_DFL) {
266             log("apitrace: info: taking default action for signal %i\n", sig);
267
268 #if 1
269             struct sigaction dfl_action;
270             dfl_action.sa_handler = SIG_DFL;
271             sigemptyset (&dfl_action.sa_mask);
272             dfl_action.sa_flags = 0;
273             sigaction(sig, &dfl_action, NULL);
274
275             raise(sig);
276 #else
277             raise(SIGKILL);
278 #endif
279         } else if (old_action->sa_handler == SIG_IGN) {
280             /* ignore */
281         } else {
282             /* dispatch to handler */
283             old_action->sa_handler(sig);
284         }
285     }
286 }
287
288 void
289 setExceptionCallback(void (*callback)(void))
290 {
291     assert(!gCallback);
292     if (!gCallback) {
293         gCallback = callback;
294
295         struct sigaction new_action;
296         new_action.sa_sigaction = signalHandler;
297         sigemptyset(&new_action.sa_mask);
298         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
299
300
301         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
302             // SIGKILL and SIGSTOP can't be handled.
303             if (sig == SIGKILL || sig == SIGSTOP) {
304                 continue;
305             }
306
307             /*
308              * SIGPIPE can be emitted when writing to stderr that is redirected
309              * to a pipe without readers.  It is also very unlikely to ocurr
310              * inside graphics APIs, and most applications where it can occur
311              * normally already ignore it.  In summary, it is unlikely that a
312              * SIGPIPE will cause abnormal termination, which it is likely that
313              * intercepting here will cause problems, so simple don't intercept
314              * it here.
315              */
316             if (sig == SIGPIPE) {
317                 continue;
318             }
319
320             if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
321                 sigaction(sig,  &new_action, NULL);
322             }
323         }
324     }
325 }
326
327 void
328 resetExceptionCallback(void)
329 {
330     gCallback = NULL;
331 }
332
333 } /* namespace os */
334
335 #endif // !defined(_WIN32)