4

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 (even 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.

axel22
  • 32,045
  • 9
  • 125
  • 137

4 Answers4

3

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks, this looks nice. I wonder if Javaflow saves the stack context into the continuation object eagerly (i.e. every time a new method or a continuation operator is called) or does so only when the operator `Continuation.suspend` gets called. – axel22 Sep 16 '11 at 15:24
  • I imagine its only when called, otherwise the restore won't be much use. ;) – Peter Lawrey Sep 16 '11 at 15:25
  • 1
    By `restore` you mean `continueWith`. Ah, yes, if `continueWith` can be used multiple times, then probably it saves the stack only when asked. I was concerned with performance, that's why I asked. But it seems that Javaflow instruments the bytecode during classloading. I thought instrumentation would occur at runtime (after classloading), when needed. I wonder what are the performance implications, if every method gets instrumented. – axel22 Sep 16 '11 at 15:37
  • I think the only way to know is to try it. – Peter Lawrey Sep 16 '11 at 15:42
  • No arguments there :) I'll try it out! – axel22 Sep 16 '11 at 15:43
2

I believe Java Platform Debugger Architecture (JPDA) is what you are looking for.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
1

What is wrong with Thread.currentThread().getStackTrace() ? as poiinted out here: stack overflow comment

Community
  • 1
  • 1
Angel O'Sphere
  • 2,642
  • 20
  • 18
  • 1
    This doesn't include variables on the stack – Steve McLeod Sep 16 '11 at 15:31
  • This would be more appropriate for debugging purposes, as it only returns the source code line, and the name of the class and the method of the class being invoked, not the actual contents of the stack frame. – axel22 Sep 16 '11 at 15:32
1

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

James Drinkard
  • 15,342
  • 16
  • 114
  • 137