tags:

views:

207

answers:

1

I want to know how to do method tracing for Android applications. I mean, a sequence of calls on each object, not a stack trace.

It's very similar to this question (Call trace in java), but on different platforms (jvm-PC vs dvm-Android). I have no control over the start arguments of dalvik, thus I cannot specify a java agent (or am I wrong here?). Is there another way to do method tracing?

Thanks!

+1  A: 

The Runtime.traceMethodCalls() method mentioned in the answers to the linked question is in fact wired up in Android. It just starts the method profiling feature with default arguments. For more details, read the article about method profiling and traceview in the Android documentation.

fadden
Thanks. Yes, Runtime.traceMethodCalls() does the job. Yet there're reasons why I'm here for an alternative: 1. It generates lots of unwanted information. 2. Trace file has to be pulled out first, you cannot do analysis on-the-fly. 3. You cannot see method-trace in the context of logcat outputs. Some efforts need to be made before you know what's happening before/after a method is invoked.
DenMark
Writing to the "logcat" log likely won't work. The data is written to a 64KB circular buffer in the kernel. If it fills up faster than logcat pulls the data out, messages will be dropped, and you'll waste time trying to figure out why your method isn't being called when it actually is. :-) You'd need the method names and the log messages to be written to a file on disk. There's no in-built mechanism for doing this.
fadden
First, thanks again. Sure, unfiltered trace is overwhelming for logcat. But I'm only interested in traces generated by methods of the application (and hopefully in context of logcat outputs on-the-fly as mentioned in last comment). I wonder if there is a way to do this within the application...
DenMark
Another way you may be able to do this is by attaching jdb and using the "trace methods" feature.
fadden
I've not used jdb yet. I'll try and see if it works. But if we're using trace methods feature of the VM, it depends on the VM for supporting filtering of which methods to trace. I doubt if Dalvik supporots this.
DenMark
Actually, it's probably not going to do what you want. It's printing the method name on entry, which is good, but it's also pausing the VM. I don't know if there's a way to convince the debugger to print the name and continue automatically.
fadden
Thanks for your kind help. I'll keep looking and post back if I get a way to do this. BTW, I tried jdb, but did not successfully connect to dalvik.. Either get a 'java.io.IOException: shmemBase_attach failed', or there is no response.
DenMark
Normally in Eclipse you'd use "debug remote process on port 8700" to connect through DDMS. For jdb, use "jdb -attach localhost:8700". After that it works the same as any other Java debugger, just much harder to use. :-)
fadden