private void printVar(OutputSink out, LocalVariable var, StackFrame frame) { out.print(" " + var.name()); if (var.isVisible(frame)) { Value val = frame.getValue(var); out.println(" = " + val.toString()); } else { out.println(" is not in scope"); } }
private void commandLocals() throws NoSessionException { ThreadReference current = context.getCurrentThread(); if (current == null) { env.failure("No default thread specified: " + "use the \"thread\" command first."); return; } StackFrame frame; try { frame = context.getCurrentFrame(current); if (frame == null) { env.failure("Thread has not yet created any stack frames."); return; } } catch (VMNotInterruptedException e) { env.failure("Target VM must be in interrupted state."); return; } List<LocalVariable> vars; try { vars = frame.visibleVariables(); if (vars == null || vars.size() == 0) { env.failure("No local variables"); return; } } catch (AbsentInformationException e) { env.failure( "Local variable information not available." + " Compile with -g to generate variable information"); return; } OutputSink out = env.getOutputSink(); out.println("Method arguments:"); for (LocalVariable var : vars) { if (var.isArgument()) { printVar(out, var, frame); } } out.println("Local variables:"); for (LocalVariable var : vars) { if (!var.isArgument()) { printVar(out, var, frame); } } out.show(); return; }
public char[][] localVariableTypeNames() { try { StackFrame frame = getStackFrame(); Iterator variables = frame.visibleVariables().iterator(); Vector names = new Vector(); while (variables.hasNext()) { LocalVariable var = (LocalVariable) variables.next(); names.addElement(var.typeName().toCharArray()); } char[][] result = new char[names.size()][]; names.copyInto(result); return result; } catch (AbsentInformationException e) { return null; } }
void compareTwoDifferentVars(LocalVariable lv1, LocalVariable lv2) { if (!lv1.equals(lv2)) { println(" Success: difference of local vars detected"); } else { failure(" Failure: difference of local vars is NOT detected"); } if (lv1.hashCode() != lv2.hashCode()) { println(" Success: hashCode's of different local vars differ"); } else { failure(" Failure: hashCode's of different local vars are equal"); } if (lv1.compareTo(lv2) != 0) { println(" Success: compareTo() is correct for different local vars"); } else { failure(" Failure: compareTo() is NOT correct for different local vars"); } }
void compareTwoEqualVars(LocalVariable lv1, LocalVariable lv2) { if (lv1.equals(lv2)) { println(" Success: equality of local vars detected"); } else { failure(" Failure: equality of local vars is NOT detected"); } if (lv1.hashCode() == lv2.hashCode()) { println(" Success: hashCode's of equal local vars are equal"); } else { failure(" Failure: hashCode's of equal local vars differ"); } if (lv1.compareTo(lv2) == 0) { println(" Success: compareTo() is correct for equal local vars"); } else { failure(" Failure: compareTo() is NOT correct for equal local vars"); } }
void printVariable(LocalVariable lv, int index) throws Exception { if (lv == null) { println(" Var name: null"); return; } String tyname = lv.typeName(); println( " Var: " + lv.name() + ", index: " + index + ", type: " + tyname + ", Signature: " + lv.type().signature()); // Sorry, there is no way to take local variable slot numbers using JDI! // It is because method LocalVariableImpl.slot() is private. }
public StringReference getLocalVariableValueAsString(StackFrame fr, String localName) throws AbsentInformationException, IncompatibleThreadStateException { Value val = null; StringReference ret = null; LocalVariable var = fr.visibleVariableByName(localName); if (var != null) { val = fr.getValue(var); if (val instanceof StringReference) { ret = (StringReference) fr.getValue(var); } else { LOGGER.warn( "getLocalVariableValueAsString called with non-String Object: " + var.getClass().getName()); } } else { LOGGER.warn("LocalVariable with name: " + localName + " was not found"); } return ret; }
/* * (non-Javadoc) * * @see org.eclipse.jdt.debug.core.IJavaVariable#getGenericSignature() */ @Override public String getGenericSignature() throws DebugException { try { String genericSignature = fLocal.genericSignature(); if (genericSignature != null) { return genericSignature; } return fLocal.signature(); } catch (RuntimeException e) { targetRequestFailed( MessageFormat.format( JDIDebugModelMessages .JDILocalVariable_exception_retrieving_local_variable_type_signature, e.toString()), e); // execution will not reach this line, as // #targetRequestFailed will thrown an exception return null; } }
protected void runTests() throws Exception { /* * Get to the top of main() * to determine targetClass and mainThread */ BreakpointEvent bpe = startToMain("GetLocalVariables2Targ"); targetClass = bpe.location().declaringType(); mainThread = bpe.thread(); EventRequestManager erm = vm().eventRequestManager(); bpe = resumeTo("GetLocalVariables2Targ", "bar", "(I)Z"); /* * Inspect the stack frame for main(), not bar()... */ StackFrame frame = bpe.thread().frame(1); List localVars = frame.visibleVariables(); System.out.println(" Visible variables at this point are: "); for (Iterator it = localVars.iterator(); it.hasNext(); ) { LocalVariable lv = (LocalVariable) it.next(); System.out.print(lv.name()); System.out.print(" typeName: "); System.out.print(lv.typeName()); System.out.print(" signature: "); System.out.print(lv.type().signature()); System.out.print(" primitive type: "); System.out.println(lv.type().name()); if ("command".equals(lv.name())) { failure("Failure: LocalVariable \"command\" should not be visible at this point."); if (lv.isVisible(frame)) { System.out.println("Failure: \"command.isvisible(frame)\" returned true."); } } } /* * resume the target listening for events */ listenUntilVMDisconnect(); /* * deal with results of test * if anything has called failure("foo") testFailed will be true */ if (!testFailed) { println("GetLocalVariables2Test: passed"); } else { throw new Exception("GetLocalVariables2Test: failed"); } }
/* * This function appears to take an exorbitant amount of time why why why */ public boolean setLocalVariableValue(int i, String name, Value sf) throws IncompatibleThreadStateException, InvalidTypeException, ClassNotLoadedException, AbsentInformationException { StackFrame frame = this.currentThread.frames().get(i); LocalVariable var = frame.visibleVariableByName(name); LOGGER.info("got var: " + var.typeName()); try { frame.setValue(var, sf); LOGGER.info("success setting new variable value"); return true; } catch (java.lang.ClassCastException e) { /* * KNOWN ISSUE: when checking type compatibility the debugger * requests the ClassLoader of the type of the variable. When an * object is loaded via reflection (using the current method) this * will return an ObjectReference. The debugger is expecting a * ClassLoaderReference and apparently the ObjectReference cannot be * cast to a ClassLoaderReference. */ LOGGER.info( "ClassCastException due to type assignments with classes loaded via reflection, work around is to load class again"); } return false; }
public boolean run(String codeSnippetClassName) { ClassType codeSnippetClass; ObjectReference codeSnippet; Method method; List arguments; ObjectReference codeSnippetRunner; try { // Get the code snippet class List classes = jdiVM.classesByName(codeSnippetClassName); while (classes.size() == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { } classes = jdiVM.classesByName(codeSnippetClassName); if (classes.size() == 0) { // workaround bug in Standard VM Iterator iterator = this.jdiVM.allClasses().iterator(); while (iterator.hasNext()) { ReferenceType type = (ReferenceType) iterator.next(); if (type.name().equals(codeSnippetClassName)) { classes = new ArrayList(1); classes.add(type); break; } } } } codeSnippetClass = (ClassType) classes.get(0); // Create a new code snippet Method constructor = (Method) codeSnippetClass.methodsByName("<init>").get(0); codeSnippet = codeSnippetClass.newInstance( jdiThread, constructor, new ArrayList(), ClassType.INVOKE_SINGLE_THREADED); // Install local variables and "this" into generated fields StackFrame stackFrame = getStackFrame(); try { Iterator variables = stackFrame.visibleVariables().iterator(); while (variables.hasNext()) { LocalVariable jdiVariable = (LocalVariable) variables.next(); Value value = stackFrame.getValue(jdiVariable); Field field = codeSnippetClass.fieldByName(new String(LOCAL_VAR_PREFIX) + jdiVariable.name()); codeSnippet.setValue(field, value); } } catch (AbsentInformationException e) { // No variables } Field delegateThis = codeSnippetClass.fieldByName(new String(DELEGATE_THIS)); ObjectReference thisObject; if (delegateThis != null && ((thisObject = stackFrame.thisObject()) != null)) { codeSnippet.setValue(delegateThis, thisObject); } // Get the code snippet runner ClassType codeSnippetRunnerClass = (ClassType) jdiVM.classesByName(CODE_SNIPPET_RUNNER_CLASS_NAME).get(0); Field theRunner = codeSnippetRunnerClass.fieldByName(THE_RUNNER_FIELD); codeSnippetRunner = (ObjectReference) codeSnippetRunnerClass.getValue(theRunner); // Get the method 'runCodeSnippet' and its arguments method = (Method) codeSnippetRunnerClass.methodsByName(RUN_CODE_SNIPPET_METHOD).get(0); arguments = new ArrayList(); arguments.add(codeSnippet); } catch (ClassNotLoadedException e) { e.printStackTrace(); return false; } catch (IncompatibleThreadStateException e) { e.printStackTrace(); return false; } catch (InvalidTypeException e) { e.printStackTrace(); return false; } catch (InvocationException e) { e.printStackTrace(); return false; } try { // Invoke runCodeSnippet(CodeSnippet) codeSnippetRunner.invokeMethod( jdiThread, method, arguments, ClassType.INVOKE_SINGLE_THREADED); // Retrieve values of local variables and put them back in the stack frame StackFrame stackFrame = getStackFrame(); try { Iterator variables = stackFrame.visibleVariables().iterator(); while (variables.hasNext()) { LocalVariable jdiVariable = (LocalVariable) variables.next(); Field field = codeSnippetClass.fieldByName(new String(LOCAL_VAR_PREFIX) + jdiVariable.name()); Value value = codeSnippet.getValue(field); stackFrame.setValue(jdiVariable, value); } } catch (AbsentInformationException e) { // No variables } } catch (ClassNotLoadedException e) { e.printStackTrace(); } catch (IncompatibleThreadStateException e) { e.printStackTrace(); } catch (InvalidTypeException e) { e.printStackTrace(); } catch (InvocationException e) { e.printStackTrace(); } return true; }