• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

StackTrace of all threads

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic