• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackTrace of all threads  RSS feed

 
Christian Presslmayr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I'd need the possibility to get the stack trace of each thread in my application (just as CTRL+BREAK does).
BUT: I'd like to access the information by using the new StackTraceElement-class as of JDK 1.4, because I want to process the StackTraceElements and find out the main class (the class with the main method) which started my threads.
Does anybody know how to do this? Is there an API for this?
Thanks for your help!
Best regards,
Chris
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One could work backwards in the
API Documentation. Looking at StackTraceElement, we see it is returned by Throwable.getStackTrace(). We can create an instance of Throwable and get the stack trace for the current thread. Thread and ThreadGroup have methods which can enumerate and get parent, so one could find the top thread and enumerate all the VM's threads. However, I seem to remember that when iterating over threads and doing stack dumps, the dump wasn't of the thread object being handled, but of the thread of execution (the iterator thread). Your mileage may vary.
 
Christian Presslmayr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, that's what I tried. I used the enumerate method to get all threads. But when calling the static method Thread.dumpStack() it justs prints the stack trace for the currently active thread.
The best solution for my situation would be an instance method in class Thread, e.g. getStackTrace(), which returns stack trace info as StackTraceElement[] (just as Throwable.getStackTrace() does) for any thread in my application.
Unfortunately such a method does not exist
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why don't you create an instance of Throwable and call getStackTrace() on it?
Answer: Because the Throwable's stack trace will still be that of the iterator Thread.
Your best bet is to use a debugger. JDB or JSwat should fit the bill if you don't have an IDE.
[ November 25, 2003: Message edited by: Joe Ess ]
 
Christian Presslmayr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, I can't create a Throwable of any thread just for fun. This is, again, only possible for the currently active thread.
Debugging is also not a solution for my problem, because I'd need the stack trace during the application run for some other purposes: I wan't to find out the main class of my application, i.e. the class which has the main method that started all the threads around. And in any of these threads I have to access these information.
Do you know any other possibility for this?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, I can't create a Throwable of any thread just for fun. This is, again, only possible for the currently active thread.
Agreed.
I know you can't use a debugger, but perhaps the Java Debug Interface (part of your JDK) can help. I haven't used it. but it looks like you should be able to programmatically access a ThreadReference for the thread you want to know about - then frames() should have all the stack info you may want. The main downside I see is that JDI is implememented in com.sun packages, indicating that the API is still evolving. You might get a solution that works great on JDK 1.4.2, but not on 1.4.3 or 1.5. Still, maybe this route will still be useful to you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!