Stack traces
By Calvin Austin
July 1998
It's the night before your product release. you run your final tests and then it happens -- you get a java stack trace. you look through your Java programming books, but this seems to be an area that's missing! Well no need to panic, just follow the simple steps suggested here and you'll be home before midnight! This article shows you step by step how to recognize and collect the clues in a stack trace to solve your Java software problems.
What is a java stack trace? A java stack trace is a user-friendly snapshot of the threads and monitors in a java1 Virtual Machine (JVM ). depending on how complex your application or applet is, a stack trace can range from bytes ty lines to thousands of lines of diagnostics.
However, regardless of the size of the stack trace there are a few key things that anyone can find to help diagnose most Java software problems, whether you are a Java programming expert or very new to the Java platform.
How are java stack traces generated?
There are three popular ways to generate a java stack trace: sending a signal to the Java Virtual Machine; the Java Virtual Machine generates a stack trace for you; or using debugging tools or Java api cils.
Sending a signal to the Java Virtual Machine
On UNIX platforms you can send a signal to a program by using the kill command. This is the quit signal, which is handled by the JVM. For example, on Solaris you can use the commandkill -QUIT process_id
, Whereprocess_id
Is the process number of your Java program.
Alternatively you can enter the key sequence<ctrl>/
In the window where the Java program was started. Sending this signal instructs a signal handler in the JVM, to recursively print out all the information on the threads and monitors inside the JVM.
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, or click the close button on the window.
The Java Virtual Machine generates a stack trace for you
If the JVM experienced an internal error, for example a segmentation violation or an illegal page fault, it will call its own signal handler to print out the threads and monitors information.
Using debugging tools or Java API CILS
You can generate a partial java stack trace, which in this case is only the threads information, by usingThread.dumpStack
Method, orprintStackTrace
Method of the throwable class. You can also obtain similar information by entering the command"where
"Inside the Java debugger.
If you are successful at generating a stack trace you shoshould see something like this: $ sigquit 3 * Quit
Si_signo [3]: sigquit 3 * Quit
Si_errno [0]: error 0
Si_code [0]: si_user [pid: 11927, uid: 26432]
Stackbase = ee292000, stackpointer = ee291878
Full thread dump:
"Thread-5" (TID: 0xee703b78, sys_thread_t: 0xee261db8, state: R) PRIO = 5
Mythread. stopper (exec3.java: 10)
Mythread. Run (exec3.java: 16)
"Thread-4" (TID: 0xee703bb8, sys_thread_t: 0xee291db8, state: R) PRIO = 5 * Current thread *
Mythread. stopper (exec3.java: 10)
Mythread. Run (exec3.java: 16)
"Finalizer thread" (TID: 0xee700220, sys_thread_t: 0xee2c1d B8, state: R) PRIO = 1
"Async Garbage Collector" (TID: 0xee700268, sys_thread_t: 0 xee2f1db8, state: R) PRIO = 1
"Idle thread" (TID: 0xee7002b0, sys_thread_t: 0xee3c1db8, state: R) PRIO = 0
"Clock" (TID: 0xee700088, sys_thread_t: 0xee3f1db8, state: CW) PRIO = 12
"Main" (TID: 0xee7000b0, sys_thread_t: 0x693a0, state: CW) PRIO = 5
Exec3.main (exec3.java: 32)
Monitor Cache dump:
Mythread @ ee703bb8/ee74e190: Owner "thread-4" (0xee291db8, 1 entry)
Mythread @ ee703b78/ee74e270: Owner "thread-5" (0xee261db8, 1 entry)
<Unknown key> (0x693a0): <unowned>
Waiting to be notified:
"Main" (0x693a0)
Registered monitor dump:
Thread queue lock: <unowned>
Name and type hash table lock: <unowned>
String intern lock: <unowned>
JNI pinning lock: <unowned>
JNI global reference lock: <unowned>
Binclass lock: <unowned>
Class loading lock: <unowned>
Java stack lock: <unowned>
Code rewrite lock: <unowned>
Heap lock: <unowned>
Has Finalization queue lock: <unowned>
Finalize me queue lock: <unowned>
Monitor Io lock: <unowned>
Child Death monitor: <unowned>
Event monitor: <unowned>
I/O monitor: <unowned>
Alarm monitor: <unowned>
Waiting to be notified:
"Clock" (0xee3f1db8)
Sbrk lock: <unowned>
Monitor Registry: Owner "thread-4" (0xee291db8, 1 entry)
Thread alarm Q:
Sys_thread_t 0x693a0 [timeout in 9997374 MS]
What are the first things to look?
By following the next three steps you might find the solution to the application's problem without any more analysis. however, if you don't, the evidence you'll gain from this exercise is vital to any further investigation.
Finding the current thread
You now have a java stack trace. the first piece of information you need to look for is the current thread. in theory the current thread shocould be the last thread that was running when the snapshot was taken. if you have seen a java stack trace before you may have noticed that often the current thread is labeled * Current thread * next to the appropriate thread.
Is that the current thread you ask? Most of the time Yes, but unfortunately this is only a good guess by the signal handler. sometimes the signal handler will be marked as the current thread, which doesn't help your diagnosis at all! And on some platforms none of the threads will be marked as the current thread! But don't despair, more evidence about which thread is the current thread is explained in the section, "examining monitors ".
Note:If you see the labelCompiled Code
Next to the current thread then this stack trace came from a JVM using the JIT compiler. If possible generate another stack trace without the JIT enabled using-nojit
Parameter to the JVM.
Runnable threads
Next you need to track down all the threads that have a state of R, which stands for runnable. threads in the r state were running, or were ready to run the next time the threads were scheduled. make a note of these, because they cocould indicate where your problem lies.
There is more evidence about which thread is the current thread explained in the section, "examining monitors ".
Core files
If the JVM generated the stack trace because of an internal error then some native code in your own application, or the JVM was probably to blame. if you are using UNIX, and you find a core file, run the following command to find out which JDK software it came from:
strings core | grep JAVA_HOME
In the JDK 1.2 software release, threads that called methods resulting in a call to native code are indicated in the stack trace.
Which software release and which platform generated the stack trace?
Wouldn't it be easier if the stack trace contained a JDK version string? Well, there is a request for a version string to be written in any stack trace in bug number 4047300. When this request is implemented it will eliminate the need for the next section!
However, without a version string you can take a pretty good guess as which release this stack trace came from. obviusly if you generated the stack trace yourself this shouldn't be much of an issue, but you may see a stack trace posted on a newsgroup or in an email article.
Here are some hints to help in your detective work:
First identify where the registered monitor dump section is in the stack trace: If you seeutf8 hash table lock
In the registered monitor dump then this is a JDK 1.2 stack trace. If you seeJNI pinning lock
And noutf8 hash lock
Then this is a JDK 1.1 + release. If neither of these appears in the registered monitor dump then it is probably a JDK 1.0.2 release.
What platform did the stack trace come from?
You can also find out if the stack trace came from a Windows 95, an NT, or a unix machine by looking for any waiting threads. on a unix machine the waiting threads are named explicitly. on a Windows 95, or NT machine only a count of the waiting threads is displayed:
Windows 95/NT:
Finalize me queue lock: <unowned> Writer: 1UNIX:Finalize me queue lock: <unowned>
Waiting to be notified "finalizer thread"
Determining which thread package was used.
Windows 95 and Windows NT Java virtual machines are by default native thread JVMs. UNIX JVMs are by default green thread JVMs, they use a pseudo thread implementation. to make your JVM use native threads you need to supply-native
Parameter, for example,java -native MyClass
.
By verifying the existence ofAlarm monitor
In the stack trace output you can identify that this stack trace came from a green threads JVM.
Determining the thread states
You will see your different threads in your different states in a snapshot from a JVM stack trace. The key used is:
R |
Running or runnable thread |
S |
Sushortded thread |
CW |
Thread waiting on a condition variable |
MW |
Thread waiting on a monitor lock |
MS |
Thread suincluded waiting on a monitor lock |
Normally only threads in R, S, CW or MW shoshould appear in the stack trace. if you see a thread in State MS, report it to Sun Microsystems, via the bug parade as there is a good chance it is a bug, because most of the time a thread in monitor wait state will appear in the S state when it is suincluded. monitors (covered in the next section) are used to manage access to code that shoshould only be run by a single thread at a time.
Examining monitors
This brings us to the other part of the stack trace: The monitor dump. if you consider that the threads section of a stack trace identifies the multithreaded part of your application then the monitors Section represents the parts of your application that are single threaded.
It may be easier to imagine a monitor as a car wash. in most car types es, only one car can be in the wash at a time. in your Java code only one thread can have the lock to a synchronized piece of code at a time. all the other threads queue up to enter the synchronized code just as cars queue up to enter the car wash.
A monitor can be thought of as a lock on an object, and every object has a monitor. when you generate a stack trace, monitors are listed as being either registered or not registered. in the majority of cases these registered monitors, or system monitors, shouldn't be the cause of your software problems. but it helps to be able to understand and recognize them. the following table describes the common registered monitors:
Thread queue lock |
Protects the queue of active threads. |
Name and type hash table lock |
Protects the JVM hash tables of constants and their types. |
String intern lock |
Locks the hashtable of defined Strings that were loaded from the class constant pool. |
JNI pinning lock |
Protects block copies of arrays to native method code. |
JNI global reference lock |
Locks the global reference table which holds values that need to be explicitly freed, and will outlive the life time of the native method call. |
Binclass lock |
Locks access to the loaded and resolved Classes list. |
Class loading lock |
Ensures only one thread loads a class at a time. |
Java stack lock |
Protects the free stack segments list. |
Code rewrite lock |
Protects code when an optimization is attempted. |
Heap lock |
Protects the Java heap During Heap Memory Management |
Has Finalization queue lock |
Protects the list of queue lock objects that have been garbage-collected, and deemed to need finalization. They are copied to the finalize me queue. |
Finalize me queue lock |
Protects a list of objects that can be finalized at leisure. |
Monitor Io lock |
Protects physical I/O for example, Open and read. |
The monitor registry itself is protected by a monitor. this means that the thread that owns the lock is the last thread to use a monitor. it's very likely that this thread may also be the current thread.
Because only one thread can enter a synchronized block at a time, other threads queue up at the start of the synchronized code and appear as thread state mW. in the monitor Cache dump they will be denoted as "waiting to enter" threads. in User code a monitor is called into action wherever a synchronized block or method is used.
Any code waiting on an object or event, that is a wait method, also has to be inside a synchronized block. however once the wait method is called, the lock on the synchronized object is given up.
When the thread in the wait state is notified of an event to the object, it has to compete for exclusive access to that object, and it has to obtain the monitor. even when a thread has sent a "Running y event" to the waiting threads, none of the waiting threads can actually gain control of the monitor lock until the waiting Ying thread has left its synchronized code block.
You will see "waiting to be notified" for threads at the wait Method
Putting the steps into practice
Consider a real life problem: Bug 4098756 for example. You can find details on this bug in JDC bug parade. This bug details ents a problem that occurred when using a choice component on Windows 95.
When the user selected one of the choices from the choice component using the mouse, everything was fine. however when the user tried to use an arrow key to move up or down the list of choices the Java application froze.
Fortunately this problem was reproducible and there was a java stack trace to help track down the problem. the full stack trace is in the bug report page, but you only need to focus on two key threads, for example:
"AWT-Windows" (TID:0xf54b70, sys_thread_t:0x875a80,Win32ID:0x67,
state:MW) prio=5
java.awt.Choice.select(Choice .java:293)
sun.awt.windows.WChoicePeer.handleAction(WChoicePeer.java:86)
"AWT-EventQueue-0" (TID:0xf54a 98,sys_thread_t:0x875c20,
Win32ID:0x8f, state:R) prio=5
java.awt.Choice.remove(Choice.java:228)
java.awt.Choice.removeAll(Choice.java:246)
TheAWT-EventQueue-0
Thread is in a runnable state insideremove
Method.Remove
Is synchronized so that explains why the other thread,AWT-Windows
, Cannot enter the select method.AWT-Windows
Thread is in state mW, monitor wait.
However, if you keep taking stack traces this situation does not change, and the GUI appears to have frozen. this indicates that the remove call never returned. by following the code path to the choicepeer class, you can see that this is making a native MFC call that does not return. that's where the real problem lies and is a bug in the Java core classes. the user's code was OK.
Expert's Checklist
This covers the theory about java stack traces, And You shoshould now know what to look for next time you see one. to save yourself time, be sure to make full use of the JDC bug search to see if the problem you are having has already been reported.
To summarize, here are the steps to take when you next come into ss a problem Java program:
For hanging, deadlocked or frozen programs:If you think your program is hanging, generate a stack trace and examine the threads in States MW or CW. if the program is deadlocked then some of the system threads will probably show up as the current threads, because there is nothing else for the JVM to do.
For crashed, aborted programs:On UNIX look for a core file. You can analyze this file in a native debugging tool suchgdb
Ordbx
. Look for threads that have called Native methods. because Java technology uses a Safe Memory Model, any signature uption probably occurred in the native code. remember that the JVM also uses native code, so it may not necessarily be a bug in your application.
For busy programs:The best course of action you can take for busy programs is to generate frequent stack traces. this will narrow down the code path that is causing the errors, and you can then start your investigation from there.
Good luck and happy debugging
Calvin Austin is a software engineer on the Java Developer Connection team. This article is based on a presentation he delivered at the 1998 javaonesm Developer Conference.
_______
1 As used on this web site, the terms "Java Virtual Machine" or "JVM" mean a virtual machine for the Java platform.