1

How do we get the stacktrace for a successfully executed line in Java? It's needed to debug an issue.

I dont want a normal stacktrace, I want to know what a particular line is doing behind the scenes.

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("/opt/data/ws_server.xml"));    
serviceHelper = (ServiceHelper)factory.getBean("serviceHelper");
//Assuming no exceptions, print/view stack trace of above line (factory.getBean). 

I want to see the stacktrace for factory.getBean - like below, to understand what factory.getBean is doing.

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:757)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:721)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
- locked <0xffffffff58100608> (a java.util.concurrent.ConcurrentHashMap)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
krish B
  • 43
  • 6
  • 2
    Do you use an IDE? The easiest way would probably be to start up a debugger, put a breakpoint on the line, and run it. From there, your debugger should allow you to view the call stack. – Riaan Nel Mar 22 '19 at 04:37
  • What this “particular line is doing behind the scenes”, talking about a method invocation, may be an arbitrary number of entirely different operations, impossible to express with a single stack trace. – Holger Mar 22 '19 at 07:42
  • 1
    Stacktrace is brilliant. We dont need to wade through hundreds of files in many third party jars to find out what is happening. – krish B Mar 23 '19 at 18:22
  • At present we can get stacktrace only when there is an exception while executing a line. I wanted to get expert opinion if its possible to get trace for a line where there is no exceptions and seems its not possible. I am closing this question, Thanks for all your time and support. – krish B Mar 23 '19 at 18:29
  • @krishB Consider looking into AspectJ to add functionality to existing byte code, likedding "print stack trace" to the entry or exit of `getBean` , – Thorbjørn Ravn Andersen Apr 10 '20 at 15:26

2 Answers2

2

In the end, you are asking for some sort of instrumentation. In other words: you want to tell the jvm to keep track of the call stack and more importantly, make that information available to you programmatically.

And even when you only want that to happen for specific methods, the jvm still has to track all method invocations, as it can't know whether one of the methods to track is called in the end. Thus there is no way of tracking method invocations easily without performance impacts. And the tools I know that can keep that performance impact on a reasonable level, like XRebel are for later evaluation, not for programmatic consumption.

In other words: the only solutions to hang situations are:

  • doing a thread dump and analyzing it
  • doing extensive logging/tracing while your code is running, to analyze that in case or hangs

Just to be clear: what you are asking for, to get a stack trace of already executed code after the fact is impossible to achieve!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thank you, lets say I did a therad dump and found the problem - factory.getBean doing an IO (searching for classpth etc) for each invocation. Now I fix the issue and while testing how do I know factory.getBean is not doing an IO, I need the stack trace to see what its doing after doing fix. (Going to file system vs using cache). – krish B Mar 22 '19 at 04:54
  • 1
    @krishB this is a great example of [the xy problem](https://meta.stackexchange.com/a/66378/242352). Instead of asking “How to find out whether method a calls method b?” or “…whether `getBean` does I/O?”, you are asking something about stack traces. – Holger Mar 22 '19 at 08:20
  • @krishB Again, the JVM does **not** keep track of method invocations **after** a call is done. From that point of view, you should consider if one of the answers here is satisfactory ... and maybe accept that. And then write up a new question when you have a *better* understanding what your actual problem is that you intend to solve! Holger is fully correct: focus on what the *real* problem is you have to solve. Then see if your question asks for that. If not, decide how to continue from here. – GhostCat Mar 22 '19 at 09:52
  • 1
    Thanks Holger , GhostCat, factory.getbean() was an example. stacktrace is the easiest way to find what a particular line is doing, Instead of looking in to multiple code and finding out what were the values at runtime. – krish B Mar 22 '19 at 15:57
  • This is a good job for "dtrace" which is possible for Java on Solaris and MacOS. – Thorbjørn Ravn Andersen Apr 10 '20 at 15:26
1

Please look in following answer.

Get current stack trace in Java

Basically it is Thread.currentThread().getStackTrace()

vavasthi
  • 922
  • 5
  • 14
  • Thanks for the reply But Thread.currentThread().getStackTrace() will not give full stacktrace for previous line. – krish B Mar 22 '19 at 04:35
  • The `getStackTrace` would give you the stack trace of the line where it is called. It is impossible to find the stack trace of some other line. You can put this on the next line and that will give you pretty much the same stack trace except the top of stack. – vavasthi Mar 22 '19 at 04:37
  • Yes, that is my question, without doing a thread dump when it was hanging, how to get full stack trace of a particular line that was successfully executed. – krish B Mar 22 '19 at 04:40