Drawing activation stack for program with Exception
Hi.
I'm a beginner to Java programming and I'm having a hard time grasping how the activation stack works.
I need help drawing the activation stack for this program:
Code :
1 public class Q2 {
2 public static void main(String[] args) {
3 String inputStr;
4 int input = 8;
5
6 System.out.print("Input: ");
7 inputStr = StdIn.readLine();
8 try {
9 input = Integer.parseInt(inputStr);
10 } catch (NumberFormatException ex) {
11 }
12 a(input);
13 return;
14 }
15
16
17 static void a(int limit) {
18 int[] array;
19 int size = 10;
20
21 array = new int[size];
22 for (int i=0; i<=limit; i++) {
23 array[i] = 3*i;
24 }
25 }
26 }
When the program is given an input of 20, it crashes with this report:
Code :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Q2.a(Q2.java:23)
at Q2.main(Q2.java:12)
I need to draw the activation stack as it exists at the moment of the crash. Indicate the name
and value of every variable in each activation record. If the variable is an object
reference, indicate that fact and draw the object’s value(s) off to the side of the
stack (since object values are stored in the heap, not the stack).
Thanks.
Re: Drawing activation stack for program with Exception
by "activation stack" Do you mean the call stack?
What are you trying to draw? Can you give an example?
Quote:
draw the object’s value(s) off to the side of the stack
I've never seen any way to access the local variables that the JVM would save on the stack. That is not a normal topic for these forums.
Re: Drawing activation stack for program with Exception
Yes, the call stack.
When a method is called("invoked"), an activation record(region of memory where all that method's local variables are stored) for it is pushed onto the top of the stack.
So as you return, you remove information from top of the stack.
Here are some notes on the topic:
Wikisend: free file sharing service
It gives an idea what the stack looks like.
"off to the side of the stack" is referring to the heap where the object values are stored.
Re: Drawing activation stack for program with Exception
I understand the concept and have worked with the stack and heap in assembly language.
I don't know of any way to get that information from the JVM.
Re: Drawing activation stack for program with Exception
I think I need to identify the variables and their values at the moment of the crash.
Re: Drawing activation stack for program with Exception
Sounds like a useful tool. No idea where to get the data from.
I think I've heard of a debug interface into the JVM but have never seen any samples of it being used.
Re: Drawing activation stack for program with Exception
Okay. I'll see what I can come up with.
Thanks for your time.
Re: Drawing activation stack for program with Exception
Be sure to let us know what you find and if you are able to get the names and values of the variables on the stack.
Re: Drawing activation stack for program with Exception
Norm: You can get a stack trace trivially by creating an Exception (but not throwing it): its stacktracelements will all be there. As for the local variables, not a clue off the top of my head - but how hard can it be? Debuggers do it and so does jmap / visualvm. I see this indicated as one way to do it - but I assume that's not going to work for a non-Oracle JVM?
http://docs.oracle.com/javase/6/docs...ticMXBean.html
InfiinteSound: do you just want to debug your code? ArrayIndexOutOfBoundsException is always caused by failing to check array limits. You pass in the variable 'limit' and create an array of size 'size' and then iterate until 'limit' again. You should be checking i against your array's .length member.
Re: Drawing activation stack for program with Exception
Quote:
how hard can it be?
Probably not hard with the right API. I've never looked for it so I don't know what is available.