How to make Java Thread Dump in Android?
Asked Answered
D

6

18

I just want to get all dumps from java virtual machine threads, to look at what the threads lock and what threads waiting to unlock some resources. Something like is described here. I've tryed to kill Zygote process but with no results.

Derogative answered 27/11, 2012 at 16:50 Comment(0)
B
19

The easiest way is with DDMS, or the ADT plugin in Eclipse. See http://developer.android.com/tools/debugging/ddms.html for basic instructions. In short, go into the Device view, select the application you're interested in, make sure thread updates are enabled, and switch to the Threads view. You will get a live-updated list of threads in that process. Double-clicking on a thread will grab a snapshot of the current stack state.

You can use select-all and copy in the thread dump to copy & paste the stack trace.

If you have a developer / rooted device, you can ask the Dalvik VM to dump thread stacks by sending a SIGQUIT to the app process you're interested in. For example, if you wanted to see the stacks for all threads in the Calendar app, you could do something like this:

% adb shell ps | grep android.calendar
u0_a6     2596  127   912804 48296 ffffffff b6f62c10 S com.google.android.calendar
# 2596 is the process ID
% adb shell run-as com.google.android.colendar kill -3 2596

The logcat output will say something like:

I/dalvikvm( 2596): Wrote stack traces to '/data/anr/traces.txt'

So, pull that:

% adb pull /data/anr/traces.txt .

Every time you signal a process, the logs are appended to that file. There may be other stuff in there, so you need to search for pid 2596:

----- pid 2596 at 2012-11-27 12:48:38 -----
Cmd line: com.google.android.calendar

DALVIK THREADS:
...

The advantage of doing this over the DDMS thread view is that, if the thread is stuck on a monitor, the stack dump will give you an indication of what object is locked and which thread currently holds the lock.

The zygote process isn't relevant here; by definition it isn't running an app. Since it doesn't have a JDWP thread, and doesn't listen for SIGQUIT, you can't get a stack trace out of it anyway.

Brickbat answered 27/11, 2012 at 21:0 Comment(2)
Note: I found my trace at /data/anr/traces.txt.bugreportBeckmann
This returns "run-as: package not debuggable: $package" for non debuggable apps.Skepticism
F
13

Just debug your app on your phone in Android Studio; then in the "Debug view" Alt+5 just press the "Camera" button at the bottom left corner, to obtain a dump of all stacktraces, including locks they are holding.

Forborne answered 24/11, 2017 at 6:49 Comment(1)
Best answer (easiest solution) for those using IntelliJ IDEA or Android Studio.Cigarillo
P
3

Taking the commands from the answer, and putting it all up together, here is the following script. Putting it into the dump.sh file and executing, it will find the needed PID, create a new file with current timestamp and next it will fetch the Thread-Dump into it. This command can be useful when one has a very short period of time to fetch the dump. Before using it, make sure that traces are put into the file /data/anr/traces.txt or replace this value in the script.

#!/bin/sh

pid=`./adb shell ps | grep android.calendar | awk '{print $2}'`
echo $pid
f=$(date +%s%N)
echo $f
./adb shell run-as com.google.android.calendar kill -3 $pid
./adb pull /data/anr/traces.txt $f

As an alternative file name, one can use f=$(date +"%T.%6N") to get human readable timestamp. It would be easier to find the needed file.

Polly answered 22/11, 2017 at 9:2 Comment(1)
Sometimes you cannot use breakpoints. For those cases you use this script.Polly
L
3

If you don't have a rooted device and your app isn't a debug build, you can still generate a bug report from developer options to get the thread dump. The dumps are under the VM TRACES JUST NOW section of the bugreport*.txt file.

It's quicker if you know the ID of the thread you're suspecting. You can get the PID of the app by calling:

adb shell ps -A | grep com.example.myapp

and the thread ID with:

adb shell ps -T | grep <pid>
Ley answered 7/11, 2019 at 11:34 Comment(0)
L
0

If you switch to the DDMS view in Eclipse you have some tools to look at threads. Is that what you're looking for?

Lori answered 27/11, 2012 at 17:12 Comment(3)
Almost Yes. I have yet to know what the object is locked, for example: - locked <0x0000000780b7e688> (a java.io.InputStreamReader)Derogative
This answer is obsolete since there's no Eclipse anymore.Forborne
Eclipse is gone but DDMS is still here and has no adequate replacement.Canonicate
T
0

I'm guessing you need the threads within an app. For this, you can use the DDMS view on the ADT eclipse plugin. Here's the doc http://developer.android.com/tools/debugging/ddms.html#thread

Thiazole answered 27/11, 2012 at 17:13 Comment(3)
Almost Yes. I have yet to know what the object is locked, for example: - locked <0x0000000780b7e688> (a java.io.InputStreamReader)Derogative
Eclipse and DDMS are deprecated, and Android Profiler won't show anything. Read below for my answer.Forborne
Eclipse is gone but DDMS is still here and has no adequate replacement.Canonicate

© 2022 - 2024 — McMap. All rights reserved.