How to Analyze Stack Trace Info of a Thread

Can I get the stack traces of all threads in my c# app?

There is a tool on CodePlex called Managed Stack Explorer (that I believe originated from Microsoft). It uses the debugging and profiling API to capture the stack traces of the threads in a running .Net application, without the need to modify the application.

You could run your application until you experience the issue, then analyse it using this tool to capture the current stack traces of all running threads. The benefit of this approach is that you leave your application unmodified (instrumenting it may change its behaviour), and the tool is free.

How can I get the current stack trace in Java?

You can use Thread.currentThread().getStackTrace().

That returns an array of StackTraceElements that represent the current stack trace of a program.

Analysis of java stack trace, what threads are running

The term Runnable may not mean 'currently running' but rather 'is in a state of running'. The differences as you eluded to is that the maximum number of threads that can be running at any given time is equal to your number of cores.

To understand it in more details, you would need to know what the other two mean. WAITING would mean that a thread has been suspended on some object.

while(<condition holds>)
obj.wait()

Here, the current thread is currently WAITING on obj's monitor. This would force that thread to become suspended in which the OS can schedule another thread to run.

TIMED_WAITING is simply suspending a thread for a certain amount of time.

So if a thread isn't waiting or sleeping it is considered running.

To give more insights from the documentation.

Thread state for a runnable thread. A
thread in the runnable state is
executing in the Java virtual machine
but it may be waiting for other
resources from the operating system
such as processor.

That last part is what I think you are looking for. Physically the OS cannot run more threads in parallel than the amount of the cores.

All 9 of your threads may run concurrently but only n will run in parallel (n being the number of cores).

Similar is true for Condition.await

Edit:

Sorry richs - Should've linked my resource http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html#RUNNABLE

Getting the Java thread id and stack trace of run-away Java thread

It looks like the nid in the jstack output is the Linux LWP id.

"http-342.877.573.944-8080-360" daemon prio=10 tid=0x0000002adaba9c00 nid=0x754c in Object.wait() [0x00000000595bc000..0x00000000595bccb0]

Convert the nid to decimal and you have the LWP id. In your case 0x754c is 30028. This process is not shown in our ps output, but it was probably one of the LWPs you have omitted to save space.

Here's a little a Perl snippet you can use to pipe the output of jstack to:

#!/usr/bin/perl -w
while (<>) {
if (/nid=(0x[[:xdigit:]]+)/) {
$lwp = hex($1);
s/nid=/lwp=$lwp nid=/;
}
print;
}

Is there any way to see the full stack trace across multiple threads?

In general the answer is No, as StackTrace by definition cannot contain information from other stack. However, if you debugging your application in Visual studio, it do some work for you (this is C++, but it's similar for all the languages):

Sample Image

Here, external code is dimmer than your, and you can review some "parent" thread information. However, usually this screen isn't much helpful. Visual studio creates vshost.exe file for gather as much debug information as possible.

Also, if you create the task withh attaching them to parent, and there is some exception, you'll get full stacktrace with exception's ToString method, but still it's not exactly what you want.



Related Topics



Leave a reply



Submit