|
Message
From: cvs at opencores.org<cvs@o...>
Date: Thu Nov 29 15:51:55 CET 2007
Subject: [cvs-checkins] MODIFIED: jop ...
Date: 00/07/11 29:15:51 Modified: jop/java/target/src/test/debug JopDebugKernel.java Log: Major change to the JOP debug kernel. Refactoring, improving, adding methods, changing comments and removing bugs. - Refactored the breakpoint() method to isolate event handling for each JDWP command (specific methods created) - Refactored other methods - Created handlers for new commands - Created several new methods - Fixed one or two minor bugs - Changed class to be final - Changed access of many methods to be private - Changed some public methods to be final Class flag changed: 44: public final class JopDebugKernel Constant created: MAX_LOCAL_VARIABLES Methods changed: 103: public static final void breakpoint() - Refactored. Added code to dump the entire call stack, dump specific stack frames and provide other informations. 447: private static void initialize() 463: private static void setDebugStreams(InputStream in, OutputStream out) 547: private static int getCPLocalsArgsFromMP(int mp) 552: private static int getArgCountFromVal(int val) 557: private static int getLocalsCountFromVal(int val) 562: private static int getCPFromMP(int mp) 579: private static int getVPFromFrame(int frame) 521: public static final int getNextFramePointer(int framePointer) 635: private static int getNumLocalsAndParametersFromFrame(int frame) 668: private static int getLocalsPointerFromFrame(int frame) 594: public static final int getLocalVariable(int frame, int fieldIndex) 630: public static final void setLocalVariable(int frame, int fieldIndex, int value) 766: public static final int getFramePointerOfCallerMethod() 785: public static final boolean isFirstFrame(int framePointer) 901: private static int getStackDepth() 907: private static int getInstanceSize(int object) 913: private static int getClassReference(int object) 928: private static int getConstantPoolFromClassReference(int classReference) 944: private static boolean handleSetBreakPointCommand() throws IOException 983: private static boolean handleClearBreakPointCommand() throws IOException 1162: private static int setBreakPoint(int methodStructPointer, int instructionOffset) 1182: private static int clearBreakPoint(int methodStructPointer,
1208: private static int overwriteInstruction(int methodStructPointer,
1432: private static int getMethodStartAddress(int methodPointer)
1451: private static int getMethodSize(int methodPointer)
1464: private static int getMethodConstantPool(int methodPointer)
1478: public static final int getMethodArgCount(int methodPointer)
1491: public static final int getMethodLocalsCount(int methodPointer)
1506: public static final void dumpMethodStruct(int methodPointer)
1344: public static final int getCurrentMethodPointer()
1555: public static final void dumpMethodBody(int methodPointer)
Methods created:
308: private static void handleGetStackDepthCommand() throws IOException
323: private static void handlePrintCallStackCommand() throws IOException
336: private static void handlePrintStackFrameCommand() throws IOException
372: private static void handleGetStackFramesCommand() throws IOException
435: private static void handleResumeExecutionCommand() throws IOException
454: private static void handleInvokeStaticCommand() throws IOException
505: private static void handleExitCommmand() throws IOException
569: private static int getMPFromFrame(int frame)
584: private static int getPCFromFrame(int frame)
1028: private static void handleGetStackFrameMPCommand() throws IOException
1054: private static void handleGetLocalVariableCommand() throws IOException
1088: private static int getFramePointerAtIndex(int frameIndex)
1106: private static void handleSetLocalVariableCommand() throws IOException
1135: private static void handleGetNumberOfLocalVariablesCommand() throws IOException
1590: private static void prettyPrintStack()
1623: private static void prettyPrintStackFrame(int frame, int previousFrame)
1641: private static void printVariablesFromFrame(int frame)
1682: private static void printRegistersFromFrame(int frame)
1718: private static void printLocalStackFromFrame(int frame, int previousFrame)
1767: private static void printStackArea(int initialPosition, int length)
Methods removed:
471: public static final int getMPFromPreviousMethodCall(int frame)
- Replaced by getMPFromFrame.
685: public static int getPCFromPreviousMethodCall(int framePointer)
- Replaced by getPCFromFrame.
692: // public static int getSPFromFrameStart(int frame)
697: // public static int getMethodPointrFromStackPointer(int sp)
- Removed old, commented methods.
Revision Changes Path
1.3 jop/java/target/src/test/debug/JopDebugKernel.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/test/debug/JopDebugKernel.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: JopDebugKernel.java
===================================================================
RCS file: /cvsroot/paulo/jop/java/target/src/test/debug/JopDebugKernel.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- JopDebugKernel.java 22 Nov 2007 18:40:34 -0000 1.2
+++ JopDebugKernel.java 29 Nov 2007 14:51:55 -0000 1.3
@@ -41,7 +41,7 @@
* 03/06/2007 - 12:00:48
*
*/
-public class JopDebugKernel
+public final class JopDebugKernel
{
private static DataInputStream inputStream;
private static DataOutputStream outputStream;
@@ -62,6 +62,9 @@
private static final int NOP_INSTRUCTION = 0x00;
private static final int BREAKPOINT_INSTRUCTION = 0x00CA;
+ // currently the maximum number of local variables is 32.
+ private static final int MAX_LOCAL_VARIABLES = 32;
+
private static boolean initialized = false;
private static int breakpointMethodPointer = 0;
@@ -97,7 +100,7 @@
* @throws IOException
*
*/
- public static void breakpoint()
+ public static final void breakpoint()
{
if(initialized == false)
{
@@ -125,13 +128,8 @@
// exit from this method.
if((commandset == 1) && (command == 10))
{
- // acknowledge the command and exit.
- outputStream.writeInt(0);
-
- System.out.println(" Received \"Exit (10)\" command.");
- System.out.println(" Shutting down... ");
-
- System.exit(0);
+ System.out.println("Exit: stop execution.");
+ handleExitCommmand();
break;
}
@@ -181,71 +179,197 @@
// invoke a static method with one integer parameter on the stack
if((commandset == 3) && (command == 3))
{
- int methodId = inputStream.readInt();
-// int numArguments = inputStream.readInt();
- int argument = inputStream.readInt();
+ System.out.println("Invoke static");
+ handleInvokeStaticCommand();
+ continue;
+ }
- System.out.print(" Method ID: " );
- System.out.print(methodId);
- System.out.print(" Argument: " );
- System.out.println(argument);
+ // resume execution. Finish this method and continue.
+ if((commandset == 11) && (command == 3))
+ {
+ System.out.println("Resume execution");
+ handleResumeExecutionCommand();
- if(breakpointMethodPointer == methodId)
+ // stop the loop
+ break;
+ }
+
+ // return a list of all stack frame locations
+ if((commandset == 11) && (command == 6))
{
- System.out.println("Invalid method pointer!");
- System.out.println("Cannot call breakpoint from itself.");
+ System.out.println("Get stack frames");
+ handleGetStackFramesCommand();
+
+ continue;
}
- else
+
+ // calculate the stack depth of the caller method.
+ if((commandset == 11) && (command == 7))
{
- int sp = Native.getSP();
- System.out.print("SP = ");
- System.out.println(sp);
-// sp++;
-// Native.wrIntMem(argument, sp);
-// Native.setSP(sp);
-// test just to see if it breaks. It doesn't if SP is restored later.
-// Native.setSP(sp + 4);
+ System.out.println("Get stack depth");
+ handleGetStackDepthCommand();
+ continue;
+ }
- System.out.println("Calling method now:");
- Native.invoke(argument, methodId);
+ // dump the call stack. For development ONLY.
+ // NOT a standard JDWP command set/command pair.
+ if((commandset == 11) && (command == 13))
+ {
+ System.out.println("Print the call stack.");
+ handlePrintCallStackCommand();
+ continue;
+ }
- // now restore SP to its previous value. Doing this without
- // getting the top value just ignores anything left on the stack.
-// sp = sp - 1;
- Native.setSP(sp);
+ // dump one stack frame. For development ONLY.
+ // NOT a standard JDWP command set/command pair.
+ if((commandset == 11) && (command == 14))
+ {
+ System.out.println("Print a stack frame.");
+ handlePrintStackFrameCommand();
+ continue;
+ }
-// System.out.println("Right after return.");
-// sp = Native.getSP();
-// System.out.print("SP = ");
-// System.out.println(sp);
+ // ----------------------
+ // breakpoint commands
+ // ----------------------
+
+ // set breakpoint
+ if((commandset == 15) && (command == 1))
+ {
+ System.out.println("set breakpoint");
+ handleSetBreakPointCommand();
+ continue;
}
- // return the argument just to sync with the caller and inform that
- // execution has finished.
- outputStream.writeInt(argument);
+ // clear breakpoint
+ if((commandset == 15) && (command == 2))
+ {
+ System.out.println("clear breakpoint");
+ handleClearBreakPointCommand();
continue;
}
- // resume execution. Finish this method and continue.
- if((commandset == 11) && (command == 3))
+ // get the method pointer from a stack frame
+ if((commandset == 16) && (command == 0))
{
- // acknowledge the command and resume execution.
- outputStream.writeInt(0);
+ System.out.println("Get method pointer");
+ handleGetStackFrameMPCommand();
- System.out.println(" Received \"Resume (11, 3)\" command.");
+ continue;
+ }
- //TODO: now how can I run the bytecode that was standing where this
- // breakpoint is, now?
+ // get a local variable value
+ if((commandset == 16) && (command == 1))
+ {
+ System.out.println("Get local variable");
+ handleGetLocalVariableCommand();
-// // just for development,dump the call stack.
-// TestJopDebugKernel.dumpCallStack();
+ continue;
+ }
- // stop the loop
+ // set a local variable value
+ if((commandset == 16) && (command == 2))
+ {
+ System.out.println("Set local variable");
+ handleSetLocalVariableCommand();
+
+ continue;
+ }
+
+ // return the nuber of local variables
+ if((commandset == 16) && (command == 5))
+ {
+ System.out.println("Get number of local variables");
+ handleGetNumberOfLocalVariablesCommand();
+
+ continue;
+ }
+ System.out.println("Received invalid command or command set! ");
+ System.out.print("Command: ");
+ System.out.print(command);
+ System.out.print(" Command set: ");
+ System.out.print(commandset);
+ System.out.println();
+ }
+ catch(IOException exception)
+ {
+ System.out.println("Failure: " + exception.getMessage());
+ exception.printStackTrace();
break;
}
+ }
- // return a list of all stack frame locations
- if((commandset == 11) && (command == 6))
+ System.out.println("Returning from \"breakpoint\".");
+ }
+
+ /**
+ * @throws IOException
+ */
+ private static void handleGetStackDepthCommand() throws IOException
+ {
+ // get current stack depth (this frame)
+ int count = getStackDepth();
+ // remove one to return the caller's depth
+ count --;
+ System.out.print(" Stack depth of caller method: " );
+ System.out.println(count);
+
+ outputStream.writeInt(count);
+ }
+
+ /**
+ * @throws IOException
+ */
+ private static void handlePrintCallStackCommand() throws IOException
+ {
+ // get current stack depth (this frame)
+ int count = getStackDepth();
+
+ prettyPrintStack();
+
+ // just for development
+ //TestJopDebugKernel.dumpCallStack();
+
+ outputStream.writeInt(count);
+ }
+
+ private static void handlePrintStackFrameCommand() throws IOException
+ {
+ // get stack frame index to be printed (this frame)
+ int frameIndex;
+ int previousFrameIndex;
+ int framePointer,previousFramePointer;
+
+// System.out.println("Starting handlePrintStackFrameCommand");
+
+ frameIndex = inputStream.readInt();
+ previousFrameIndex = frameIndex + 1;
+
+// System.out.print("Frame index to print: ");
+// System.out.println(frameIndex);
+
+ if(previousFrameIndex < getStackDepth())
+ {
+ previousFramePointer = getFramePointerAtIndex(previousFrameIndex);
+ framePointer = getNextFramePointer(previousFramePointer);
+
+ prettyPrintStackFrame(framePointer, previousFramePointer);
+ }
+ else
+ {
+ System.out.println("Failure: invalid index -> " + frameIndex);
+ }
+
+ // Will return the frame index just to sync
+ outputStream.writeInt(frameIndex);
+ }
+
+ /**
+ * Handle the "Frames" command.
+ *
+ * @throws IOException
+ */
+ private static void handleGetStackFramesCommand() throws IOException
{
System.out.println(" Will read startFrame");
@@ -274,12 +398,12 @@
boolean shouldContinue = true;
while(shouldContinue)
{
- int programCounter = getPCFromPreviousMethodCall(framePointer);
+ int programCounter = getPCFromFrame(framePointer);
// System.out.print(" Program counter: ");
// System.out.println(programCounter);
outputStream.writeInt(programCounter);
- int methodPointer = getMPFromPreviousMethodCall(framePointer);
+ int methodPointer = getMPFromFrame(framePointer);
// System.out.print(" Method pointer: ");
// System.out.println(methodPointer);
@@ -301,141 +425,92 @@
}
System.out.println("Done! ");
-
- continue;
}
- // calculate the stack depth of the caller method.
- if((commandset == 11) && (command == 7))
- {
- // get current stack depth (this frame)
- int count = getStackDepth();
- // remove one to return the caller's depth
- count --;
- System.out.print(" Stack depth of caller method: " );
- System.out.println(count);
-
- outputStream.writeInt(count);
- continue;
- }
-
- // ----------------------
- // breakpoint commands
- // ----------------------
-
- // set breakpoint
- if((commandset == 15) && (command == 1))
- {
- System.out.println("set breakpoint");
- handleSetBreakPointCommand();
- continue;
- }
-
- // clear breakpoint
- if((commandset == 15) && (command == 2))
- {
- System.out.println("clear breakpoint");
- handleClearBreakPointCommand();
- continue;
- }
-
- // get the method pointer from a stack frame
- if((commandset == 16) && (command == 0))
+ /**
+ * Handle the "Resume execution" command.
+ *
+ * @throws IOException
+ */
+ private static void handleResumeExecutionCommand() throws IOException
{
- int frameIndex = inputStream.readInt();
-
- System.out.print(" Frame index: " );
- System.out.print(frameIndex);
+ // acknowledge the command and resume execution.
+ outputStream.writeInt(0);
- int count = getStackDepth();
- int pointer = getCurrentFramePointer();
- for(int i = 0; i < (count - (frameIndex + 1)); i++)
- {
- pointer = getNextFramePointer(pointer);
- }
- pointer = getMPFromPreviousMethodCall(pointer);
+ System.out.println(" Received \"Resume (11, 3)\" command.");
- System.out.print(" Method pointer: ");
- System.out.println(pointer);
+ //TODO: now how can I run the bytecode that was standing where this
+ // breakpoint is, now?
- outputStream.writeInt(pointer);
- continue;
+ // just for development,dump the call stack.
+ TestJopDebugKernel.dumpCallStack();
}
- // get a local variable value
- if((commandset == 16) && (command == 1))
+ /**
+ * Handle the "Invoke static" command.
+ *
+ * @throws IOException
+ */
+ private static void handleInvokeStaticCommand() throws IOException
{
- int frameIndex = inputStream.readInt();
- int fieldIndex = inputStream.readInt();
-
- System.out.print(" Frame index: " );
- System.out.print(frameIndex);
- System.out.print(" Variable index: " );
- System.out.println(fieldIndex);
+ int methodId = inputStream.readInt();
+// int numArguments = inputStream.readInt();
+ int argument = inputStream.readInt();
+ System.out.print(" Method ID: " );
+ System.out.print(methodId);
+ System.out.print(" Argument: " );
+ System.out.println(argument);
- int count = getStackDepth();
- int pointer = getCurrentFramePointer();
- for(int i = 0; i < (count - frameIndex); i++)
+ if(breakpointMethodPointer == methodId)
{
- pointer = getNextFramePointer(pointer);
- }
- count = getLocalVariable(pointer, fieldIndex);
-
- System.out.print(" Value: ");
- System.out.println(count);
- System.out.print(" Pointer: ");
- System.out.println(pointer);
-
- outputStream.writeInt(count);
- continue;
+ System.out.println("Invalid method pointer!");
+ System.out.println("Cannot call breakpoint from itself.");
}
- // set a local variable value
- if((commandset == 16) && (command == 2))
+ else
{
- int frameIndex = inputStream.readInt();
- int fieldIndex = inputStream.readInt();
- int value = inputStream.readInt();
-
- System.out.print(" Frame index: " );
- System.out.print(frameIndex);
- System.out.print(" Variable index: " );
- System.out.println(fieldIndex);
-
+ int sp = Native.getSP();
+ System.out.print("SP = ");
+ System.out.println(sp);
+// sp++;
+// Native.wrIntMem(argument, sp);
+// Native.setSP(sp);
+// test just to see if it breaks. It doesn't if SP is restored later.
+// Native.setSP(sp + 4);
- int count = getStackDepth();
- int pointer = getCurrentFramePointer();
- for(int i = 0; i < (count - frameIndex); i++)
- {
- pointer = getNextFramePointer(pointer);
- }
- setLocalVariable(pointer, fieldIndex, value);
+ System.out.println("Calling method now:");
+ Native.invoke(argument, methodId);
- System.out.print(" Value: ");
- System.out.println(value);
- System.out.print(" Pointer: ");
- System.out.println(pointer);
+ // now restore SP to its previous value. Doing this without
+ // getting the top value just ignores anything left on the stack.
+// sp = sp - 1;
+ Native.setSP(sp);
-// writeInt(4);
- outputStream.writeInt(value);
- continue;
+// System.out.println("Right after return.");
+// sp = Native.getSP();
+// System.out.print("SP = ");
+// System.out.println(sp);
}
- System.out.println("Received invalid command or command set! ");
- System.out.print("Command: ");
- System.out.print(command);
- System.out.print(" Command set: ");
- System.out.print(commandset);
- System.out.println();
+
+ // return the argument just to sync with the caller and inform that
+ // execution has finished.
+ outputStream.writeInt(argument);
}
- catch(IOException exception)
+
+ /**
+ * Handle the "Exit" command.
+ *
+ * @throws IOException
+ */
+ private static void handleExitCommmand() throws IOException
{
- System.out.println("Failure: " + exception.getMessage());
- exception.printStackTrace();
- break;
- }
- }
+ // acknowledge the command and exit.
+ outputStream.writeInt(0);
- System.out.println("Returning from \"breakpoint\".");
+ System.out.println(" Received \"Exit (10)\" command.");
+ System.out.println(" Shutting down... ");
+
+ System.exit(0);
}
/**
@@ -447,10 +522,12 @@
private static void initialize()
{
breakpointMethodPointer =
- getMPFromPreviousMethodCall(getCurrentFramePointer());
+ getMPFromFrame(getCurrentFramePointer());
EmbeddedOutputStream embeddedStream = new EmbeddedOutputStream(System.out);
setDebugStreams(System.in, embeddedStream);
+
+ initialized = true;
}
/**
@@ -465,48 +542,51 @@
inputStream = new DataInputStream(in);
outputStream = new DataOutputStream(out);
- initialized = true;
- }
-
- public static final int getMPFromPreviousMethodCall(int frame)
- {
-// System.out.println("getMPFromPreviousMethodCall(int frame)");
- return Native.rdIntMem(frame + 4);
}
- public static final int getCPLocalsArgsFromMP(int mp)
+ private static int getCPLocalsArgsFromMP(int mp)
{
return Native.rdMem(mp + 1); // cp, locals, args
}
- public static final int getArgCountFromVal(int val)
+ private static int getArgCountFromVal(int val)
{
return val & 0x1f;
}
- public static final int getLocalsCountFromVal(int val)
+ private static int getLocalsCountFromVal(int val)
{
return ((val >>> 5) & 0x1f);
}
- public static final int getCPFromMP(int mp)
+ private static int getCPFromMP(int mp)
{
int value = getCPLocalsArgsFromMP(mp);
value = value >>> 10;
return value; // cp
}
- public static int getCPFromFrame(int frame)
+ private static int getMPFromFrame(int frame)
+ {
+ return Native.rdIntMem(frame + 4);
+ }
+
+ private static int getCPFromFrame(int frame)
{
return Native.rdIntMem(frame + 3);
}
- public static int getVPFromFrame(int frame)
+ private static int getVPFromFrame(int frame)
{
return Native.rdIntMem(frame + 2);
}
- public static int getSPFromFrame(int frame)
+ private static int getPCFromFrame(int frame)
+ {
+ return Native.rdIntMem(frame + 1);
+ }
+
+ private static int getSPFromFrame(int frame)
{
return Native.rdIntMem(frame);
}
@@ -532,7 +612,7 @@
}
else
{
- mp = getMPFromPreviousMethodCall(framePointer);
+ mp = getMPFromFrame(framePointer);
val= getCPLocalsArgsFromMP(mp);
args = getArgCountFromVal(val);
@@ -547,28 +627,29 @@
/**
* Calculate the number of local variables based on the
* frame pointer to a call stack frame.
- * Does not consider the "this" reference or parameters:
- * it just count the number of locals as declared on the source.
+ * It consider the "this" reference, parameters and locals.
*
* @param frame
* @return
*/
- public static final int getNumLocalsFromFrame(int frame)
+ private static int getNumLocalsAndParametersFromFrame(int frame)
{
int loc;
int localsPointer = getLocalsPointerFromFrame(frame);
loc = frame - localsPointer;
-// int mp, val;
-// mp = getMPFromFrame(frame);
-// System.out.println("mp = getMPFromFrame(frame); : " + mp);
-//
-// val= getCPLocalsArgsFromMP(mp);
-// System.out.println("val= getCPLocalsArgsFromMP(mp); : " + val);
+// System.out.print("Frame: ");
+// System.out.print(frame);
+// System.out.print(" localsPointer: ");
+// System.out.print(localsPointer);
//
-// loc = getLocalsCountFromVal(val);
-// System.out.println("loc = getLocalsCountFromVal(val); : " + loc);
+// System.out.print(" loc: ");
+// System.out.print(loc);
+// if(isFirstFrame(frame))
+// {
+// System.out.println("First frame!");
+// }
return loc;
}
@@ -584,7 +665,7 @@
* @param frame
* @return
*/
- public static int getLocalsPointerFromFrame(int frame)
+ private static int getLocalsPointerFromFrame(int frame)
{
int previous_sp = getSPFromFrame(frame);
int localsPointer = previous_sp + 1;
@@ -597,7 +678,7 @@
int vp;
int value = 0;
- numLoc = getNumLocalsFromFrame(frame);
+ numLoc = getNumLocalsAndParametersFromFrame(frame);
// System.out.println(" getField(int frame, int fieldIndex) frame = " + frame);
// System.out.println("Num. Locals: " + numLoc);
if(fieldIndex < numLoc && fieldIndex >= 0)
@@ -643,7 +724,7 @@
// print
// increment, set
- numLoc = getNumLocalsFromFrame(frame);
+ numLoc = getNumLocalsAndParametersFromFrame(frame);
// System.out.println(" setField(int frame, int fieldIndex, value) frame = " + frame);
// System.out.println("Num. Locals: " + numLoc);
if(fieldIndex < numLoc && fieldIndex >= 0)
@@ -682,25 +763,7 @@
return getVPFromFrame(frame);
}
- public static int getPCFromPreviousMethodCall(int framePointer)
- {
-// System.out.println("getPCFromPreviousMethodCall(int framePointer)");
-// return framePointer + 1;
- return Native.rdIntMem(framePointer + 1);
- }
-
-// public static int getSPFromFrameStart(int frame)
-// {
-// return frame;
-// }
-//
-// public static int getMethodPointrFromStackPointer(int sp)
-// {
-// int mp = Native.rdIntMem(sp);
-// return mp;
-// }
-
- public static int getFramePointerOfCallerMethod()
+ public static final int getFramePointerOfCallerMethod()
{
int frame;
@@ -719,7 +782,7 @@
* method. Consider the call stack frame for "main"
* as the first one in the stack.
*/
- public static boolean isFirstFrame(int framePointer)
+ public static final boolean isFirstFrame(int framePointer)
{
// assume it's not the first and try to show otherwise
boolean result = false;
@@ -835,19 +898,19 @@
*
* @return
*/
- public static final int getStackDepth()
+ private static int getStackDepth()
{
int framePointer = getFramePointerOfCallerMethod();
return getStackDepth(framePointer);
}
- public static int getInstanceSize(int object)
+ private static int getInstanceSize(int object)
{
int classReference = getClassReference(object);
return Native.rdMem(classReference);
}
- public static int getClassReference(int object)
+ private static int getClassReference(int object)
{
int classreference = 0;
@@ -862,7 +925,7 @@
return classreference;
}
- public static int getConstantPoolFromClassReference(int classReference)
+ private static int getConstantPoolFromClassReference(int classReference)
{
// get reference to the first method. There will always be some,
// due to generated/synthetic methods such as the default constructur
@@ -878,7 +941,7 @@
* @return
* @throws IOException
*/
- public static final boolean handleSetBreakPointCommand() throws IOException
+ private static boolean handleSetBreakPointCommand() throws IOException
{
int methodStructPointer;
int instructionOffset;
@@ -917,7 +980,7 @@
* @return
* @throws IOException
*/
- public static final boolean handleClearBreakPointCommand() throws IOException
+ private static boolean handleClearBreakPointCommand() throws IOException
{
int methodStructPointer;
int instructionOffset;
@@ -955,6 +1018,139 @@
}
/**
+ * Handle a "get method pointer" command.
+ *
+ * This is not a standard JDWP command but is necessary to implement
+ * debugging support in JOP.
+ *
+ * @throws IOException
+ */
+ private static void handleGetStackFrameMPCommand() throws IOException
+ {
+ int frameIndex = inputStream.readInt();
+
+ System.out.print(" Frame index: " );
+ System.out.print(frameIndex);
+
+ int count = getStackDepth();
+ int pointer = getCurrentFramePointer();
+ for(int i = 0; i < (count - (frameIndex + 1)); i++)
+ {
+ pointer = getNextFramePointer(pointer);
+ }
+ pointer = getMPFromFrame(pointer);
+
+ System.out.print(" Method pointer: ");
+ System.out.println(pointer);
+
+ outputStream.writeInt(pointer);
+ }
+
+ /**
+ * Handle a "Get local variable" command.
+ *
+ * @throws IOException
+ */
+ private static void handleGetLocalVariableCommand() throws IOException
+ {
+ int frameIndex = inputStream.readInt();
+ int fieldIndex = inputStream.readInt();
+ int value;
+ int pointer;
+
+ System.out.print(" Frame index: " );
+ System.out.print(frameIndex);
+ System.out.print(" Variable index: " );
+ System.out.println(fieldIndex);
+
+ pointer = getFramePointerAtIndex(frameIndex);
+ value = getLocalVariable(pointer, fieldIndex);
+
+ System.out.print(" Value: ");
+ System.out.println(value);
+ System.out.print(" Pointer: ");
+ System.out.println(pointer);
+
+ outputStream.writeInt(value);
+ }
+
+ /**
+ * Get the frame pointer at the given index.
+ *
+ * This method allows accesing the call stack as an array,
+ * with the stack frame for the "main" method call
+ * at index zero, the next method (called inside main) at
+ * index one and so on.
+ *
+ * @param frameIndex
+ * @return
+ */
+ private static int getFramePointerAtIndex(int frameIndex)
+ {
+ int count = getStackDepth();
+ int pointer = getCurrentFramePointer();
+
+ // traverse the stack to find the frame pointer
+ for(int i = 0; i < (count - frameIndex); i++)
+ {
+ pointer = getNextFramePointer(pointer);
+ }
+ return pointer;
+ }
+
+ /**
+ * Handle a "Set local variable" command.
+ *
+ * @throws IOException
+ */
+ private static void handleSetLocalVariableCommand() throws IOException
+ {
+ int frameIndex = inputStream.readInt();
+ int fieldIndex = inputStream.readInt();
+ int value = inputStream.readInt();
+ int pointer;
+
+ System.out.print(" Frame index: " );
+ System.out.print(frameIndex);
+ System.out.print(" Variable index: " );
+ System.out.println(fieldIndex);
+
+ pointer = getFramePointerAtIndex(frameIndex);
+ setLocalVariable(pointer, fieldIndex, value);
+
+ System.out.print(" Value: ");
+ System.out.println(value);
+ System.out.print(" Pointer: ");
+ System.out.println(pointer);
+
+// writeInt(4);
+ outputStream.writeInt(value);
+ }
+
+ /**
+ * Handle a "Get number of local variables" command.
+ *
+ * @throws IOException
+ */
+ private static void handleGetNumberOfLocalVariablesCommand() throws IOException
+ {
+ int frameIndex = inputStream.readInt();
+ int framePointer;
+ int numLocals;
+
+ System.out.print(" Frame index: " );
+ System.out.print(frameIndex);
+
+ framePointer = getFramePointerAtIndex(frameIndex);
+ numLocals = getNumLocalsAndParametersFromFrame(framePointer);
+
+ System.out.print(" Number of local variables: " );
+ System.out.println(numLocals);
+
+ outputStream.writeInt(numLocals);
+ }
+
+ /**
* Set a breakpoint instruction.
*
* Note: this method DOES NOT check instruction boundaries.
@@ -963,7 +1159,7 @@
* @param instruction
* @return
*/
- public static final int setBreakPoint(int methodStructPointer, int instructionOffset)
+ private static int setBreakPoint(int methodStructPointer, int instructionOffset)
{
int instruction;
@@ -983,7 +1179,7 @@
* @param newInstruction
* @return
*/
- public static final int clearBreakPoint(int methodStructPointer,
+ private static int clearBreakPoint(int methodStructPointer,
int instructionOffset, int newInstruction)
{
int instruction;
@@ -1009,7 +1205,7 @@
* @param instruction
* @return
*/
- private static final int overwriteInstruction(int methodStructPointer,
+ private static int overwriteInstruction(int methodStructPointer,
int instructionOffset, int newInstruction)
{
int methodSize;
@@ -1233,7 +1429,7 @@
* @param methodPointer
* @return
*/
- public static int getMethodStartAddress(int methodPointer)
+ private static int getMethodStartAddress(int methodPointer)
{
int startAddress;
@@ -1252,7 +1448,7 @@
* @param methodPointer
* @return
*/
- public static int getMethodSize(int methodPointer)
+ private static int getMethodSize(int methodPointer)
{
int startAddress;
int methodSize;
@@ -1265,7 +1461,7 @@
return methodSize;
}
- public static int getMethodConstantPool(int methodPointer)
+ private static int getMethodConstantPool(int methodPointer)
{
int data;
@@ -1279,7 +1475,7 @@
return data;
}
- public static int getMethodArgCount(int methodPointer)
+ public static final int getMethodArgCount(int methodPointer)
{
int data;
@@ -1292,7 +1488,7 @@
return data;
}
- public static int getMethodLocalsCount(int methodPointer)
+ public static final int getMethodLocalsCount(int methodPointer)
{
int data;
@@ -1307,7 +1503,7 @@
return data;
}
- public static void dumpMethodStruct(int methodPointer)
+ public static final void dumpMethodStruct(int methodPointer)
{
int data;
@@ -1349,14 +1545,14 @@
data = getCurrentFramePointer();
// get the method pointer from the previous method directly from the stack
- data = getMPFromPreviousMethodCall(data);
+ data = getMPFromFrame(data);
// System.out.println("getCurrentMethodPointer()");
return data;
}
- public static void dumpMethodBody(int methodPointer)
+ public static final void dumpMethodBody(int methodPointer)
{
int index, start, size, data;
@@ -1383,4 +1579,215 @@
System.out.println();
System.out.println();
}
+
+ /**
+ * Methods to print the stack content in a way that is
+ * easy to inspect.
+ *
+ * @param frame
+ * @param previousFrame
+ */
+ private static void prettyPrintStack()
+ {
+ int frame;
+ int previousFrame;
+
+// previousFrame = getCurrentFramePointer();
+// frame = getNextFramePointer(previousFrame);
+
+ frame = getCurrentFramePointer();
+
+ while(isFirstFrame(frame) == false)
+ {
+ previousFrame = frame;
+ frame = getNextFramePointer(previousFrame);
+
+ if(isFirstFrame(frame))
+ {
+ System.out.println("Stack frame for main method (next):");
+ }
+
+ prettyPrintStackFrame(frame, previousFrame);
+ }
+ // almost done now, but still need to do the last step:
+ // print information for the frame of the main method.
+// prettyPrintStackFrame(frame, previousFrame);
+ }
+
+ /**
+ * Pretty print method to show a stack frame internal structure.
+ * Useful for debugging.
+ *
+ * @param frame
+ */
+ private static void prettyPrintStackFrame(int frame, int previousFrame)
+ {
+ TestJopDebugKernel.printLine();
+
+ printVariablesFromFrame(frame);
+ System.out.println();
+
+ printRegistersFromFrame(frame);
+ System.out.println();
+
+ printLocalStackFromFrame(frame, previousFrame);
+
+ TestJopDebugKernel.printLine();
+ }
+
+ /**
+ * @param frame
+ */
+ private static void printVariablesFromFrame(int frame)
+ {
+ int localPointer;
+ int length;
+
+ localPointer = getLocalsPointerFromFrame(frame);
+ length = frame - localPointer;
+
+// System.out.print(" Local pointer: ");
+// System.out.print(localPointer);
+// System.out.print(" Frame pointer: ");
+// System.out.print(frame);
+// System.out.print(" Length: ");
+// System.out.print(length);
+// System.out.println();
+
+ if(length < 0 || length > MAX_LOCAL_VARIABLES)
+ {
+ System.out.println("FAILURE!!! wrong local pointer!");
+ System.out.print(" Local pointer: ");
+ System.out.print(localPointer);
+
+ System.out.print(" Frame: ");
+ System.out.print(frame);
+
+ System.out.print(" Max. variables: ");
+ System.out.print(MAX_LOCAL_VARIABLES);
+ return;
+ }
+
+ System.out.println("Local variables:");
+
+ // print all variables before the frame pointer
+ printStackArea(localPointer, length);
+ }
+
+ /**
+ * Print the five register fields based on the frame pointer.
+ *
+ * @param frame
+ */
+ private static void printRegistersFromFrame(int frame)
+ {
+ int value;
+
+ System.out.print("Previous registers - SP: ");
+ value = getSPFromFrame(frame);
+ System.out.print(value);
+
+ System.out.print(" PC: ");
+ value = getPCFromFrame(frame);
+ System.out.print(value);
+
+ System.out.print(" VP: ");
+ value = getVPFromFrame(frame);
+ System.out.print(value);
+
+ System.out.print(" CP: ");
+ value = getCPFromFrame(frame);
+ System.out.print(value);
+
+ System.out.print(" MP: ");
+ value = getMPFromFrame(frame);
+ System.out.print(value);
+
+ System.out.println();
+ }
+
+ /**
+ * Print the local execution stack based on the current frame.
+ *
+ * The previousFrame parameter points to the frame on top of
+ * the one pointed by frame. It is used to delimit the local stack.
+ * It should be greater than frame.
+ *
+ * @param frame
+ */
+ private static void printLocalStackFromFrame(int frame, int previousFrame)
+ {
+ int localStackPointer, length;
+
+ // the pointer to the local stack, right after the five frame fields.
+ // Be careful here: this is *NOT* the SP field
+ // (which points to the previous stack top).
+ localStackPointer = frame + 5;
+
+ // the local execution stack (after a method call has started)
+ // goes from the 5th byte (right after "previous MP" location)
+ // until the position pointed by the "previous SP" field of the
+ // next stack frame (the one of the called method).
+ // Since the "previous SP" in the next frame points to the
+ // stack top, it will point to the "previous MP" in the
+ // current frame when the local stack is empty.
+ length = getSPFromFrame(previousFrame) - localStackPointer + 1;
+
+// System.out.print("Frame: ");
+// System.out.print(frame);
+// System.out.print(" localStackPointer: ");
+// System.out.print(localStackPointer);
+// System.out.print(" previousFrame: ");
+// System.out.print(previousFrame);
+//
+// System.out.print(" length: ");
+// System.out.print(length);
+
+ if(length > 0)
+ {
+ System.out.print("Local execution stack size: ");
+ System.out.println(length);
+ printStackArea(localStackPointer, length);
+ }
+ else
+ {
+ System.out.println("Local execution stack: empty");
+ // System.out.println("Length: " + length);
+ }
+ }
+
+ /**
+ * Print a set of stack positions in hex format.
+ * It formats the output by inserting a new line for every
+ * 8 words printed.
+ *
+ * @param initialPosition the initial position to be printed.
+ * @param length the number of stack slots to be printed.
+ */
+ private static void printStackArea(int initialPosition, int length)
+ {
+ int index, data;
+
+ if(length <= 0)
+ {
+ return;
+ }
+
+ // print a set of stack words. Do nothing and return, in case of a negative
+ // value for length.
+ for(index = 0; index < length; index++)
+ {
+ data = Native.rdIntMem(initialPosition + index);
+ printIntHex(data);
+ System.out.print(" ");
+
+ if(((index + 1)% 0x08) == 0)
+ {
+// System.out.print("Index:");
+// System.out.println(index);
+ System.out.println();
+ }
+ }
+ System.out.println();
+ }
}
|
 |