@Override public List<IWatchable> getVisibleWatchables() { try { StackFrame stackFrame = getStackFrame(); List<IWatchable> result = new ArrayList<IWatchable>(); if (stackFrame != null) { for (LocalVariable variable : stackFrame.visibleVariables()) { result.add(new JavaLocalVariable(variable, this, myClassFqName, myThreadReference)); } ObjectReference thisObject = stackFrame.thisObject(); if (thisObject != null) { result.add(new JavaThisObject(thisObject, this, myClassFqName, myThreadReference)); } else { result.add( new JavaStaticContext( getStackFrame().location().declaringType(), myClassFqName, myThreadReference)); } } return result; } catch (InvalidStackFrameException ex) { LOG.warning( "InvalidStackFrameException", ex); // TODO something should be done here. See, for instance, how idea deals with those // exceptions return Collections.emptyList(); } catch (AbsentInformationException ex) { // doing nothing, variables are just not available for us return Collections.emptyList(); } }
@Override public Map<IWatchable, IValue> getWatchableValues() { try { StackFrame stackFrame = getStackFrame(); Map<IWatchable, IValue> result = new HashMap<IWatchable, IValue>(); if (stackFrame != null) { Map<LocalVariable, Value> map = stackFrame.getValues(stackFrame.visibleVariables()); for (LocalVariable variable : map.keySet()) { result.put( new JavaLocalVariable(variable, this, myClassFqName, stackFrame.thread()), JavaValue.fromJDIValue(map.get(variable), myClassFqName, stackFrame.thread())); } ObjectReference thisObject = stackFrame.thisObject(); if (thisObject != null) { JavaThisObject object = new JavaThisObject(thisObject, this, myClassFqName, stackFrame.thread()); result.put(object, object.getValue()); } } return result; } catch (AbsentInformationException ex) { // doing nothing return Collections.emptyMap(); } }
public List<LocalVariable> getLocals(ThreadReference tr, int idx) throws IllegalArgumentException, IncompatibleThreadStateException, AbsentInformationException { StackFrame frame = tr.frames().get(idx); if (frame != null) { return frame.visibleVariables(); } 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"); } }
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; } }
public int[] localVariableModifiers() { try { StackFrame frame = getStackFrame(); List variables = frame.visibleVariables(); int[] modifiers = new int[variables.size()]; /* Iterator iterator = variables.iterator(); int i = 0; while (iterator.hasNext()) { LocalVariable var = (LocalVariable)iterator.next(); // TBD modifiers[i++] = var. ??? ; } */ return modifiers; } catch (AbsentInformationException e) { return null; } }
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; }