Solved

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

Posted on 2001-07-20
10
1,261 Views
Last Modified: 2013-11-23
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
Comment
Question by:bhiggs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6303201
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
 

Author Comment

by:bhiggs
ID: 6303288
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
 
LVL 3

Expert Comment

by:superschlonz
ID: 6303523
Can't you throw an exception, catch it at the same place and print the stack trace there ?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6303546
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
 
LVL 92

Expert Comment

by:objects
ID: 6304227
And why create an exception when you can call dumpStack()?
0
 

Author Comment

by:bhiggs
ID: 6304763
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
 
LVL 92

Expert Comment

by:objects
ID: 6305480
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6308827
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
 
LVL 5

Expert Comment

by:vemul
ID: 7669579
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
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7714513
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question