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

Posted on 2001-07-20
Medium Priority
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?
Question by:bhiggs
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
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.

Best regards,
Jim Cakalic

Author Comment

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.



Expert Comment

ID: 6303523
Can't you throw an exception, catch it at the same place and print the stack trace there ?
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.

LVL 92

Expert Comment

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

Author Comment

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.
LVL 92

Expert Comment

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.

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.


Expert Comment

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.


Cleanup Volunteer

Accepted Solution

SpideyMod earned 0 total points
ID: 7714513
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

800 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