JVM stack introspection
Is there a way to inspect the contents of the stack (both in terms of the values and the type of the values, and the current instruction point) programmatically on the JVM (eve开发者_C百科n if it's vendor-specific)?
For example, I would like to inspect the current activation frame and extract the method name it belongs to, as well as stack variables. Furthermore, I would like to be able to iterate activation frames in this way.
Is this possible? At a first glance, the JVMTI seems to allow this, but its meant to be used as a native interface. It has been used to implement a Java library that can do these things, apparently - but this seems to be a bit dated. I was wondering if there is a solution integrated into the JVM api, or some other cross-platform JVM library that allows this.
The closest I have found is Javaflow which saves the stack with local variables as an Object. You can also use it to restore the stack to a saved state.
I believe Java Platform Debugger Architecture (JPDA) is what you are looking for.
What is wrong with
Thread.currentThread().getStackTrace()
? as poiinted out here: stack overflow comment
Check out this page: http://download.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/tools.html
They have a jstack utility listed:
This utility can obtain Java and native stack information from a Java process. On Solaris OS and Linux the utility can get the information also from a core file or a remote debug server. See 2.11 jstack Utility.
I've never used it, but I have used the Visual VM tool that comes with the jdk.
HTH, James
精彩评论