Solved

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

Posted on 2001-07-20
10
1,239 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now