]> git.cworth.org Git - apitrace/commitdiff
Android: add support for dynamically enable/disable tracing
authorImre Deak <imre.deak@intel.com>
Fri, 30 Mar 2012 12:46:26 +0000 (15:46 +0300)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 7 Jul 2012 11:08:07 +0000 (12:08 +0100)
To trace applications started by the Dalvik VM we have to wrap the main
Dalvik process zygote (or app_process) which is started at boot time and
never stopped afterwards. We would still want to restrict tracing to a
single process which will be forked by zygote.

To achieve this we'll use the fact that the forked process image is
contained in the same file as zygote, pointed to by /proc/self/exe. So
if this is 'app_process' we know it's a zygote process. To distinguish
between zygote processes we'll check /proc/self/cmdline which will in
turn be set to a unique application specific string (for example
com.android.settings). The user has to set the debug.apitrace.procname
to this string in order to enable tracing.

For non-zygote processes tracing will be always enabled.

Signed-off-by: Imre Deak <imre.deak@intel.com>
common/os.hpp
common/os_posix.cpp
wrappers/trace.py

index cc72a0ea5b288184ffeb1759894344d2f5f5de3a..7f624517432838f1a8fd38515cbf0c871b15af6d 100644 (file)
@@ -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));
index 7d39d8ada5e0ffafc0f730bcdd3e913744f734d2..e4de967b045fd67c3ff5837127a5ddc18ccf055a 100644 (file)
 
 #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)
 {
index 5f6f52b4d3f446803e41007582d9df5007eaa84a..aab99d793c1ce433bf6ad19d97d7afd89bfbac15 100644 (file)
@@ -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;'