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