• Post Reply Bookmark Topic Watch Topic
  • New Topic

Identifying the caller function for a particular function at runtime.  RSS feed

 
sumit anand kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While code debugging, i need to know the caller of a particular function. There are n different places from where my function is getting called. Is there a way i can determine inside my function , the place or function from where it got called at runtime.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thread class has methods that let you inspect the stack trace of the current thread, from which you can get that information. Changing your code like that's not a very good strategy for debugging, though -- in general, it's better to simply step through with a debugger that shows you the information.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use an IDE that keeps a stack trace, or use a logging mechanism judiciously to instrument your code, or...

I am instantly suspicious when someone thinks they need a COMEFROM statement, because Intercal isn't something we should aspire to, even while debugging.
 
sumit anand kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah IDE is the best option. But in this project i am working on, we build our project in maven. And then manually place the EAR in deploy folder of JBOSS to check.
I asked it in "Java in General" because in our business logic code we are using only core java. And the only way to debug is through System.out.println or e.printStackTrace().
Printing the stack trace in this case would help only when an error is getting generated. Otherwise i am left with writing println in all the possible calling places i think of.

So is there some code or open source tool that could do the task for me?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sksumit kumar wrote:
Printing the stack trace in this case would help only when an error is getting generated. Otherwise i am left with writing println in all the possible calling places i think of.


First, note that I didn't say anything about exceptions; I said the java.lang.Thread class has methods that let you examine the current runtime stack -- i.e., find out which function is calling what, which is exactly what you want. Go look at the Javadocs.

Second, note that you can print an exception stack trace any time you want, as a debugging aid, and I do this myself occasionally. You can just write

new Exception("Here I am!").printStackTrace();

Nobody says you have to throw an exception, just because you've constructed an Exception object!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!