以编程方式转储线程/JDI(Java 调试器接口)
我喜欢以编程方式生成线程转储。 我了解到基本上有两种方法可以做到这一点:
- 使用“Java虚拟机工具接口”JVM-TI
- 使用更高抽象的“Java调试器接口”JDI
对于JVM-TI,我能够找到一些有用的信息,但我必须编写一个 JNI-DLL,至少目前我想避免这样做。 通过 JDI,我可以使用 Java,而且似乎我可以在应用程序中使用它。 但我无法找到某种教程或方法。 我能找到的唯一文档是 Java 文档 http: //java.sun.com/j2se/1.5.0/docs/guide/jpda/jdi/ 这不是很有帮助,因为它没有向我展示如何使用此类。
那么,有人知道我可以阅读的好教程/书籍吗?
感谢您的帮助!
I like to generate a thread dump programmatically. I've learned that there a basically two ways to do it:
- Use the "Java Virtual Machine Tool Interface" JVM-TI
- Use the higher abstracted "Java Debugger Interface" JDI
For the JVM-TI I was able to find some useful information, but I would have to write a JNI-DLL which, at least for the moment, I would like to avoid. With the JDI I can use Java and it seems I'm able to use it from within the application. But I wasn't able to find some kind of tutorial or HOWTO for it. The only documentation I could find, were the Java-Docs http://java.sun.com/j2se/1.5.0/docs/guide/jpda/jdi/ which isn't very helpful, because it doesn't show me how to use this classes.
So, does anybody know of a good tutorial/book I could read?
Thx for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有第三种方法: Thread.getAllStackTraces()
http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#getAllStackTraces()
这比调试器界面容易得多...
There is a third way: Thread.getAllStackTraces()
http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#getAllStackTraces()
This is much easier than the debugger interface...
您可以获得所需的所有线程信息,包括来自 http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
You can get just about all the Thread info you need including deadlocks from http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
Thread.getAllStackTraces() 仅转储所有线程的执行跟踪,但不提供特定线程已获取的对象锁或特定线程一直在等待的锁的信息。 基本上,我们无法以此解决僵局。
Thread.getAllStackTraces() dumps only the execution trace of all the threads, but doesn't give the information of object locks that have been obtained by a particular thread or the lock on which a particular thread has been waiting. Basically, we'll not be able to nail down deadlocks with this.
您是否考虑过远程替代方案? 即 VisualVM
jps 和 jstack< /a> 也是 JDK 5 中包含的有用工具,提供快速命令行方法来获取所有当前线程的堆栈跟踪。
本文建议 JDI 也可以用作远程工具< /a>.
所以我不确定你是否可以在自己的程序中触发线程转储,而是找到一种在 Unix 平台上向自己发送 SIGQUIT 信号 (kill -3) 的方法,或者在 Unix 上按 Ctrl-\ 键或 Ctrl-Break在 Windows 平台上。
另外,JDI 并不打算用于调试相同的进程JDI 客户端正在运行。 我刚刚链接到的这个线程仍然是我发现的在同一程序中实际使用 JDI 最接近的线程。
Did you consider the remote alternative ? I.e. VisualVM
jps and jstack are also useful tools included in JDK 5, providing a quick command line method for obtaining stack traces of all current threads.
This article suggest JDI is also used as a remote tool.
So I am not sure you can triggers a thread dump within your own program, instead you find a way to send to yourself a SIGQUIT signal (kill -3) on Unix platforms, or press the Ctrl-\ key on Unix or Ctrl-Break on Windows platforms.
Plus, JDI wasn't intended to be used to debug the same process in which the JDI client is running. Still this thread I just linked to is the closest I have found to actually use JDI within the same program.