[PATCH 6/9] Android: add support for dynamically enable/disable tracing

Imre Deak imre.deak at intel.com
Wed Apr 11 08:19:36 PDT 2012


To trace applications started by the Dalvik VM we have to wrap the main
Dalvik process, zygote (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 add
support to enable tracing during runtime, and on Android do this
enabling whenever the current process name matches that of a user
configured "runtime property" value.

Signed-off-by: Imre Deak <imre.deak at intel.com>
---
 trace.py |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 1 deletions(-)

diff --git a/trace.py b/trace.py
index 397fa9d..371fef6 100644
--- a/trace.py
+++ b/trace.py
@@ -335,6 +335,46 @@ class Tracer:
             print header
         print
 
+        print '#if defined(ANDROID)'
+        print 'static bool apitrace_enabled(void)'
+        print '{'
+        print '        static int enabled = -1;'
+        print '        int fd;'
+        print '        int len;'
+        print
+        print '        if (enabled != -1)'
+        print '                return enabled;'
+        print
+        print '        char value[PROP_VALUE_MAX] = "";'
+        print '        char proc_name[PROP_VALUE_MAX];'
+        print
+        print '        fd = open("/proc/self/cmdline", O_RDONLY);'
+        print '        if (fd < 0) {'
+        print '                enabled = 0;'
+        print '                return 0;'
+        print '        }'
+        print '        len = read(fd, proc_name, sizeof proc_name);'
+        print '        close(fd);'
+        print '        if (len <= 0 || len == sizeof proc_name) {'
+        print '                enabled = 0;'
+        print '                return 0;'
+        print '        }'
+        print '        proc_name[sizeof proc_name - 1] = \'\\0\';'
+        print '        __system_property_get("debug.apitrace.procname", value);'
+        print '        enabled = !strcmp(value, proc_name);'
+        print '        os::log("apitrace: %s for %s",'
+        print '                enabled ? "enabled" : "disabled", proc_name);'
+        print
+        print '        return enabled;'
+        print '}'
+        print '#else'
+        print 'static int apitrace_enabled(void)'
+        print '{'
+        print '        return 1;'
+        print '}'
+        print '#endif'
+        print
+
         # Generate the serializer functions
         types = api.getAllTypes()
         visitor = ComplexValueSerializer(self.serializerFactory())
@@ -355,7 +395,13 @@ class Tracer:
         self.footer(api)
 
     def header(self, api):
-        pass
+        print '#if defined(ANDROID)'
+        print '#include <sys/types.h>'
+        print '#include <sys/stat.h>'
+        print '#include <fcntl.h>'
+        print '#include <sys/system_properties.h>'
+        print '#endif'
+        print
 
     def footer(self, api):
         pass
@@ -381,6 +427,14 @@ class Tracer:
         print function.prototype() + ' {'
         if function.type is not stdapi.Void:
             print '    %s __result;' % function.type
+        print '    if (!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:
             self.wrapRet(function, "__result")
-- 
1.7.5.4



More information about the apitrace mailing list