简体   繁体   中英

How do I get stacktrace of a particular line of Java program

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)

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!

Please look in following answer.

Get current stack trace in Java

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM