• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1294
  • Last Modified:

How to force a stack trace from within a Java program?

We have a need for a mechanism to force a threads stack trace and monitor dump from within the Java program itself.  I know that we can force the stack trace from outside the program on UNIX systems by sending a SIGQUIT signal to the Java program.  This can be done by typing CTRL\

I have implemented a simple JNI method in C that does:

kill( getpid(), SIGQUIT );

which seems to work from within the program.  However, I don't know how to do the equivalent on Windows NT.   Windows doesn't appear to have a kill function, and doesn't have a full implementation of UNIX signals, I believe.

Can anyone help?
0
bhiggs
Asked:
bhiggs
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Jim CakalicSenior Developer/ArchitectCommented:
To generate a stack trace on Windows 95, or Windows NT platforms, enter the key sequence <ctrl><break> in the window where the Java program is running.

As far as a kill 'function' or utility, have a look at the cygwin distribution. It is a very stable and robust implementation of GNU tools and utilities for the Windows platform. And its free. It includes an implementation of kill -- although I should warn you that I haven't been able to get kill to cause the Sun JVM to dump stack trace.
    http://www.cygwin.com/

Best regards,
Jim Cakalic
0
 
bhiggsAuthor Commented:
Thanks, Jim.

I knew that a CTRL/Break key sequence forced a Java stack trace, but our need is to be able to force it from within the program.

It sounds like the kill/SIGQUIT mechanism isn't what Java uses on Windows.  I've asked in various places on the Sun Java website, but no responses yet.

Regards

Bryan
0
 
superschlonzCommented:
Can't you throw an exception, catch it at the same place and print the stack trace there ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jim CakalicSenior Developer/ArchitectCommented:
Definitely not the same as printing the full thread dump. I was playing with Thread and ThreadGroup thinking I could simulate the JVM-provided dump. But not all the information in the dump is accessible using objects of those classes. BTW, you don't have to throw the Exception -- just instantiate it.

Jim
0
 
objectsCommented:
And why create an exception when you can call dumpStack()?
0
 
bhiggsAuthor Commented:
Thread.dumpStack() and Throwable.printStackTrace() are pretty much equivalent, except that the latter is more flexible, since you can decide where the output goes.  Instantiating a Throwable (or any subclass thereof) also provides a convenient stack trace as of the point where the instance was created.  This can be very useful for later debugging.

But that's beside the point.  I'm not talking about printing a single thread's stack.  I'm talking about forcing a dump of *every* stack's call stack, *plus* the monitor dump.   (Jim Cakalic had the right idea.)  

For those who aren't familiar with this, run a Java program (using the java launcher, *not* the jre launcher) on Windows from an MS-DOS console window and then type CTRL/BREAK in that console window.  You'll see the kind of stack trace I'm looking for.
0
 
objectsCommented:
Why not just run dumpStack on every thread?
If you maintain a reference to the main thread, then you can use the various enumerate methods on ThreadGroup to grab a list of all threads.


0
 
Jim CakalicSenior Developer/ArchitectCommented:
Unfortunately, dumpStack is a Thread class method that prints the stack of the _current_ thread to System.err. In fact, the implementation is simply:

    new Exception("Stack trace").printStackTrace();

So if your application had 7 threads, you can certainly use ThreadGroup.enumerate to get Thread objects corresponding to each. And you can get from those Thread objects their name, priority, and whether it is a daemon. But if you also called dumpStack on each Thread object, you would simply get your current program location printed 7 times, not the stack for each enumerated Thread.

Jim
0
 
vemulCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now