1 /**************************************************************************
3 * Copyright 2010-2011 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
38 #if defined(__linux__)
39 #include <linux/limits.h> // PATH_MAX
43 #include <sys/syslimits.h> // PATH_MAX
44 #include <mach-o/dyld.h>
48 #include <android/log.h>
49 #include <sys/types.h>
52 #include <sys/system_properties.h>
56 #warning PATH_MAX undefined
61 #include "os_string.hpp"
71 size_t size = PATH_MAX;
72 char *buf = path.buf(size);
74 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
77 if (_NSGetExecutablePath(buf, &len) != 0) {
84 len = readlink("/proc/self/exe", buf, size - 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);
90 len = read(fd, buf, size - 1);
95 snprintf(buf, size, "%i", (int)getpid());
106 getZygoteProcessName(void)
109 size_t size = PATH_MAX;
110 char *buf = path.buf(size);
113 int fd = open("/proc/self/cmdline", O_RDONLY);
116 len = read(fd, buf, size - 1);
123 static bool isZygoteProcess(void)
125 os::String proc_name;
127 proc_name = getProcessName();
128 proc_name.trimDirectory();
130 return strcmp(proc_name, "app_process") == 0;
133 bool apitrace_enabled(void)
135 static pid_t cached_pid;
140 if (cached_pid == pid)
144 if (!isZygoteProcess()) {
145 os::log("apitrace[%d]: enabled for standalone %s", pid,
146 (const char *)getProcessName());
151 char target_proc_name[PROP_VALUE_MAX] = "";
152 os::String proc_name;
154 proc_name = getZygoteProcessName();
155 proc_name.trimDirectory();
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);
170 size_t size = PATH_MAX;
171 char *buf = path.buf(size);
181 String::exists(void) const
186 err = stat(str(), &st);
191 if (!S_ISREG(st.st_mode))
197 int execute(char * const * args)
202 execvp(args[0], args);
203 fprintf(stderr, "error: failed to execute %s\n", args[0]);
208 fprintf(stderr, "error: failed to fork\n");
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;
226 static volatile bool logging = false;
229 log(const char *format, ...)
233 va_start(ap, format);
236 __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
238 vfprintf(stderr, format, ap);
244 #if defined(__APPLE__)
245 long long timeFrequency = 0LL;
255 static void (*gCallback)(void) = NULL;
257 #define NUM_SIGNALS 16
259 struct sigaction old_actions[NUM_SIGNALS];
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
268 signalHandler(int sig, siginfo_t *info, void *context)
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.
280 static int recursion_count = 0;
282 log("apitrace: warning: caught signal %i\n", sig);
284 if (recursion_count) {
285 log("apitrace: warning: recursion handling signal %i\n", sig);
294 struct sigaction *old_action;
295 if (sig >= NUM_SIGNALS) {
296 /* This should never happen */
297 log("error: unexpected signal %i\n", sig);
300 old_action = &old_actions[sig];
302 if (old_action->sa_flags & SA_SIGINFO) {
303 // Handler is in sa_sigaction
304 old_action->sa_sigaction(sig, info, context);
306 if (old_action->sa_handler == SIG_DFL) {
307 log("apitrace: info: taking default action for signal %i\n", sig);
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);
320 } else if (old_action->sa_handler == SIG_IGN) {
323 /* dispatch to handler */
324 old_action->sa_handler(sig);
330 setExceptionCallback(void (*callback)(void))
334 gCallback = callback;
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;
342 for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
343 // SIGKILL and SIGSTOP can't be handled.
344 if (sig == SIGKILL || sig == SIGSTOP) {
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
357 if (sig == SIGPIPE) {
361 if (sigaction(sig, NULL, &old_actions[sig]) >= 0) {
362 sigaction(sig, &new_action, NULL);
369 resetExceptionCallback(void)