diff options
-rw-r--r-- | common/os.hpp | 9 | ||||
-rw-r--r-- | common/os_posix.cpp | 66 | ||||
-rw-r--r-- | wrappers/trace.py | 8 |
3 files changed, 83 insertions, 0 deletions
diff --git a/common/os.hpp b/common/os.hpp index cc72a0ea..7f624517 100644 --- a/common/os.hpp +++ b/common/os.hpp @@ -70,6 +70,15 @@ void log(const char *format, ...) #endif #endif +#ifdef ANDROID +bool apitrace_enabled(void); +#else +static inline bool apitrace_enabled(void) +{ + return true; +} +#endif + void abort(void); void setExceptionCallback(void (*callback)(void)); diff --git a/common/os_posix.cpp b/common/os_posix.cpp index 7d39d8ad..e4de967b 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -46,6 +46,10 @@ #ifdef ANDROID #include <android/log.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/system_properties.h> #endif #ifndef PATH_MAX @@ -97,6 +101,68 @@ getProcessName(void) return path; } +#ifdef ANDROID +static String +getZygoteProcessName(void) +{ + String path; + size_t size = PATH_MAX; + char *buf = path.buf(size); + ssize_t len; + + int fd = open("/proc/self/cmdline", O_RDONLY); + + assert(fd >= 0); + len = read(fd, buf, size - 1); + close(fd); + path.truncate(len); + + return path; +} + +static bool isZygoteProcess(void) +{ + os::String proc_name; + + proc_name = getProcessName(); + proc_name.trimDirectory(); + + return strcmp(proc_name, "app_process") == 0; +} + +bool apitrace_enabled(void) +{ + static pid_t cached_pid; + static bool enabled; + pid_t pid; + + pid = getpid(); + if (cached_pid == pid) + return enabled; + cached_pid = pid; + + if (!isZygoteProcess()) { + os::log("apitrace[%d]: enabled for standalone %s", pid, + (const char *)getProcessName()); + enabled = true; + return true; + } + + char target_proc_name[PROP_VALUE_MAX] = ""; + os::String proc_name; + + proc_name = getZygoteProcessName(); + proc_name.trimDirectory(); + + __system_property_get("debug.apitrace.procname", target_proc_name); + enabled = !strcmp(target_proc_name, proc_name); + os::log("apitrace[%d]: %s for %s", + pid, enabled ? "enabled" : "disabled", (const char *)proc_name); + + return enabled; +} +#endif + String getCurrentDir(void) { diff --git a/wrappers/trace.py b/wrappers/trace.py index 5f6f52b4..aab99d79 100644 --- a/wrappers/trace.py +++ b/wrappers/trace.py @@ -422,6 +422,14 @@ class Tracer: print function.prototype() + ' {' if function.type is not stdapi.Void: print ' %s _result;' % function.type + print ' if (!os::apitrace_enabled()) {' + Tracer.invokeFunction(self, function) + if function.type is not stdapi.Void: + print ' return _result;' + else: + print ' return;' + print ' }' + print self.traceFunctionImplBody(function) if function.type is not stdapi.Void: print ' return _result;' |