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