Solved

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

Posted on 2001-07-20
10
1,255 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

792 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