如何在 Android 中进行 Java 线程转储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13589074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Java Thread Dump in Android?
提问by Divano
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.
我只想从 java 虚拟机线程中获取所有转储,以查看线程锁定的内容以及等待解锁某些资源的线程。这里描述了类似的东西。我试图杀死 Zygote 进程,但没有结果。
回答by fadden
The easiest way is with DDMS, or the ADT plugin in Eclipse. See http://developer.android.com/tools/debugging/ddms.htmlfor 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.
最简单的方法是使用 DDMS,或 Eclipse 中的 ADT 插件。有关基本说明,请参阅 http://developer.android.com/tools/debugging/ddms.html。简而言之,进入设备视图,选择您感兴趣的应用程序,确保线程更新已启用,然后切换到线程视图。您将在该过程中获得实时更新的线程列表。双击线程将获取当前堆栈状态的快照。
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:
如果你有一个开发者/root 设备,你可以通过向SIGQUIT
你感兴趣的应用进程发送 a来让 Dalvik VM 转储线程堆栈。例如,如果你想查看日历应用程序中所有线程的堆栈,你可以做这样的事情:
% 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:
logcat 输出将显示如下内容:
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
:
----- 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.
与 DDMS 线程视图相比,这样做的优势在于,如果线程卡在监视器上,堆栈转储将指示哪个对象被锁定以及哪个线程当前持有该锁。
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.
合子过程在这里不相关;根据定义,它不是在运行应用程序。由于它没有 JDWP 线程,也不侦听 SIGQUIT,因此无论如何您都无法从中获取堆栈跟踪。
回答by Martin Vysny
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.
只需在 Android Studio 中的手机上调试您的应用程序;然后在“调试视图”中,Alt+5
只需按下左下角的“相机”按钮,即可获得所有堆栈跟踪的转储,包括他们持有的锁。
回答by Dmitrii Semenov
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.
从答案中获取命令,并将它们放在一起,这是以下脚本。将其放入 dump.sh 文件并执行,它将找到所需的 PID,创建一个具有当前时间戳的新文件,然后将 Thread-Dump 提取到其中。当获取转储的时间很短时,此命令很有用。在使用它之前,请确保将跟踪放入文件/data/anr/traces.txt
或替换脚本中的此值。
#!/bin/sh
pid=`./adb shell ps | grep android.calendar | awk '{print }'`
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.
作为替代文件名,可以f=$(date +"%T.%6N")
用来获取人类可读的时间戳。找到所需的文件会更容易。
回答by Christiaan
If you switch to the DDMS view in Eclipse you have some tools to look at threads. Is that what you're looking for?
如果您在 Eclipse 中切换到 DDMS 视图,您将有一些工具可以查看线程。这就是你要找的吗?
回答by Robert Estivill
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
我猜你需要应用程序中的线程。为此,您可以使用 ADT eclipse 插件上的 DDMS 视图。这是文档http://developer.android.com/tools/debugging/ddms.html#thread
回答by black
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.
如果您没有 root 设备并且您的应用程序不是调试版本,您仍然可以从开发人员选项生成错误报告以获取线程转储。转储位于 bugreport*.txt 文件的 VM TRACES JUST NOW 部分下。
It's quicker if you know the ID of the thread you're suspecting. You can get the PID of the app by calling:
如果您知道您怀疑的线程的 ID,它会更快。您可以通过调用获取应用程序的 PID:
adb shell ps -A | grep com.example.myapp
and the thread ID with:
和线程ID:
adb shell ps -T | grep <pid>