0

I have a thread with this run method:

  public void run(){    
    MAPTable t1 = new MAPTable();
    t1.init();
    while(true){
       try {       
           t1.refresh();
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }
       try {
           sleep(10000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }            
    }        
  }

How can i have i trace of all the functions executed by this thread, NOTE that init() an refresh() methods can call many others functions based on many external conditions.

I have tried to make a NullPointerException or placed one of those lines of code referenced here in the middle of the run() method, but i have always just the last trace which is the run() method (the same line i have used to print the trace).

 Thread.currentThread().getStackTrace()

or

 for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.println(ste + "\n");
 }

or

Thread.dumpStack()

PS: I'm not so familiar with the debugger and i don't know if this can be done with it.

Community
  • 1
  • 1
imanis_tn
  • 1,150
  • 1
  • 12
  • 33
  • What is your actual end goal? Why do you want this trace of methods that have been called? – unholysampler Mar 31 '12 at 22:03
  • Well i want to have the list of all executed functions by the thread just to check ithat every things OK – imanis_tn Mar 31 '12 at 22:09
  • 2
    Naive way would be to add logging in each possible called functions, showing the thread id - not sure there is a better way. – assylias Mar 31 '12 at 22:11
  • yea, this how i use to do, but it become so difficule to handel since i have so many functions and conditions. – imanis_tn Mar 31 '12 at 22:15
  • @IMAnis_tn I'd say that what you're trying to do is an overkill. Your application will spend more time logging than actually executing useful code. – Kiril Mar 31 '12 at 22:41
  • 1
    Not clear: Do you want a "stack trace" -- the list of the currently called methods, or do you want a "call trace" -- a list of every method called? For the former you use `getStackTrace` or one of the other equivalent interfaces. But it needs to be called from within the innermost method you want traced. For the latter you need to use the debugging facilities. – Hot Licks Mar 31 '12 at 22:44
  • (Though there is also `Thread.getAllStackTraces`, which, if called from a separate thread, will take a "snapshot" of other threads. Used multiple times you can create a sort of density graph of the methods being invoked, but you're nowhere near being guaranteed to catch every invoked method.) – Hot Licks Mar 31 '12 at 22:46
  • It seems like i have to start learning about the debugger, because i want a list of all the method called. until that logging is solution. – imanis_tn Mar 31 '12 at 22:56
  • There are generally "canned" trace tools that will capture the method calls and build histograms, etc. Most IDEs have these built in. – Hot Licks Mar 31 '12 at 23:16

1 Answers1

2

You may find some useful information in http://blog.zvikico.com/2007/11/five-ways-for-t.html

I think some of the profilers may be able to provide options to generate all call traces.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19