JDK-4615612 : Ability to generate thread dump from Java land
  • Type: Enhancement
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 1.3.1
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2001-12-19
  • Updated: 2002-02-19
  • Resolved: 2002-02-19
Related Reports
Duplicate :  
Description
Customer problem description:
------------------------------

The main thing we want is the ability to get a remote thread dump, as if you
typed Control-\ or Control-Break at the console.  What we've done today,
with JDK118 and JDK122 is the next best thing: a list of all the threads and
stack traces.  But what's missing even in those cases is: monitor locks,
thread state (waiting to enter, waiting for a notify, running).  APIs for
all those things would be fantastic.

With Java 1 we copied the part of the VM code that fills in the
stack trace, and we did it ourselves, filling in a Throwable ourselves ...
It didn't work with compiled methods (JIT methods) because following the
stack frame requires access to nonpublic methods that know how to walk JIT
stack frames.

Now with Java 2 that ability is no longer available. We have come
up with two suggested possibilities

*	Expose DumpThreads() and/or DumpMonitors() as routines we can call
from a JNI method.
*	Create a new, exposed method in JNI which takes a thread (possibly
suspended) and a Throwable, and it fills in the stack trace for that thread.


What I would like most is a method in class Thread which takes a Throwable
object and fills in its current stack trace into that throwable, so I can
then do whatever I need to do with the result.  

A method in Thread that takes a Throwable as an argument, and sticks
the thread's current stack trace into that throwable.

		Thread.fillInStackTrace(Throwable t)

OR

		Throwable.fillInStackTrace(Thread t);

If modifying the language spec to support these methods is not possible,
then I would settle quite happily for something in JNI land that I can call.

I heard that there will be something in 1.4 that will dump all stacks on demand. That's not really want we want, because we need better control over where the output is sent. A version of that which takes an output stream as an argument, something like:

	System.dumpAllStacks(OutputStream out)

would be perfect.

This would give us maximum flexibility to do what we would like to do, which
is look at all the running threads in a large, server-based Java
application.  We have diagnosed many MANY problems with this tool without
having to send engineers on site, and our customers realize it and are
reluctant to move to a platform that doesn't support this debugging tool.


Comments
EVALUATION Ck - For 1.4 (currently in beta3) we recently added some stack trace and related apis which we think may fill Jonathan's request. Would you please let him know what we've added and find out for us if this gives him what he needs? This is what we added: java.lang.StackTraceElement see documents at the Java Developer Connection, e.g. http://java.sun.com/j2se/1.4/docs/api/java/lang/StackTraceElement.html In addition the java.lang.Thread class has been extended to add methods such as holdsLock which can tell you if a thread holds a lock on the specified java object. There has been a dumpStack method in the Thread class for several releases which he may also find useful. see http://java.sun.com/j2se/1.4/docs/api/java/lang/Thread.html
11-06-2004

WORK AROUND Applications started from the command line can make the following command: GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0); in native code that will cause a threads stack dump in Java. But, this is not acceptable because: We cannot control -->>where the output goes. Most of our customers don't run -->>with a console window open, and so on windows the output -->>goes to /dev/null.
11-06-2004