Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I dump the current thread stack variables from a live JVM?

I need to peek into the stack of 2 deadlocked threads to analyze the situation. The JVM is live right now and the data is there, but I need some kind of tool to extract it from the process. I only care about 6 variables in the stack of type String. Any ideas are greatly appreciated. JVM versions 6_35, it's a linux, JMX is enabled, but I dont have a profiler/debugger connection configured on it. It's very difficult to reproduce.

like image 900
Vladimir Ralev Avatar asked Nov 13 '12 18:11

Vladimir Ralev


People also ask

How do you take a thread dump of a JVM?

To take a thread dump, navigate to the console used to launch the Java application, and press the CTRL and Break keys together. It's worth noting that, on some keyboards, the Break key isn't available. Therefore, in such cases, a thread dump can be captured using the CTRL, SHIFT, and Pause keys together.

Does thread dump stop JVM?

Yes, thread dump and heap dump are stop-the-world operations in JDK 8. HotSpot JVM performs them at the global safepoint. See this and this answers for more information. Save this answer.

What is thread dump from JVM?

A thread dump is a snapshot of the state of all threads that are part of the process. The state of each thread is presented with a so called stack trace, which shows the contents of a thread's stack. Some of the threads belong to the Java application you are running, while others are JVM internal threads.

Does heap dump include thread dump?

If it is a thread dump, then can we say a Heap Dump contains a Thread Dump? Hi Vish, Yes, some heap dumps contain also thread information.


2 Answers

I found a little trick using a heap dump viewer (YourKit in this instance, but may be others work as well). Basically you enumerate all instances of the Thread class, then you find the thread you want by name and open it. The stack variables are marked as < local variable > like this:

enter image description here

Not all variables are here, but all that are passed as arguments to method are displayed. I wonder if the profilers can address this issue even better?

like image 140
Vladimir Ralev Avatar answered Oct 19 '22 22:10

Vladimir Ralev


You can't do this easily. Normal jstack tool will only dump stack. Technically you can try dumping whole heap (using jmap) but looking for this particular variables can be a pain if possible.

Note that this is not easily doable for security reasons. Stack traces can contain credentials or other sensitive data.

like image 28
Tomasz Nurkiewicz Avatar answered Oct 19 '22 22:10

Tomasz Nurkiewicz