private void commandKill(StringTokenizer t) throws NoSessionException { // ### Should change the way in which thread ids and threadgroup names // ### are distinguished. if (!t.hasMoreTokens()) { env.error("Usage: kill <threadgroup name> or <thread id>"); return; } while (t.hasMoreTokens()) { String idToken = t.nextToken(); ThreadReference thread = findThread(idToken); if (thread != null) { runtime.stopThread(thread); env.notice("Thread " + thread.name() + " killed."); return; } else { /* Check for threadgroup name, NOT skipping "system". */ // ### Should skip "system"? Classic 'jdb' does this. // ### Should deal with possible non-uniqueness of threadgroup names. ThreadGroupIterator itg = allThreadGroups(); while (itg.hasNext()) { ThreadGroupReference tg = itg.nextThreadGroup(); if (tg.name().equals(idToken)) { ThreadIterator it = new ThreadIterator(tg); while (it.hasNext()) { runtime.stopThread(it.nextThread()); } env.notice("Threadgroup " + tg.name() + "killed."); return; } } env.failure("\"" + idToken + "\" is not a valid threadgroup or id."); } } }
private ThreadReference findThread(String idToken) throws NoSessionException { String id; ThreadReference thread = null; if (idToken.startsWith("t@")) { id = idToken.substring(2); } else { id = idToken; } try { ThreadReference[] threads = threads(); long threadID = Long.parseLong(id, 16); for (ThreadReference thread2 : threads) { if (thread2.uniqueID() == threadID) { thread = thread2; break; } } if (thread == null) { // env.failure("No thread for id \"" + idToken + "\""); env.failure("\"" + idToken + "\" is not a valid thread id."); } } catch (NumberFormatException e) { env.error("Thread id \"" + idToken + "\" is ill-formed."); thread = null; } return thread; }
private void commandWhere(StringTokenizer t, boolean showPC) throws NoSessionException { ThreadReference current = context.getCurrentThread(); if (!t.hasMoreTokens()) { if (current == null) { env.error("No thread specified."); return; } dumpStack(current, showPC); } else { String token = t.nextToken(); if (token.toLowerCase().equals("all")) { ThreadIterator it = allThreads(); while (it.hasNext()) { ThreadReference thread = it.next(); out.println(thread.name() + ": "); dumpStack(thread, showPC); } } else { ThreadReference thread = findThread(t.nextToken()); // ### Do we want to set current thread here? // ### Should notify user of change. if (thread != null) { context.setCurrentThread(thread); } dumpStack(thread, showPC); } } }
private int printThreadGroup(OutputSink out, ThreadGroupReference tg, int iThread) { out.println("Group " + tg.name() + ":"); List<ThreadReference> tlist = tg.threads(); int maxId = 0; int maxName = 0; for (int i = 0; i < tlist.size(); i++) { ThreadReference thr = tlist.get(i); int len = Utils.description(thr).length(); if (len > maxId) { maxId = len; } String name = thr.name(); int iDot = name.lastIndexOf('.'); if (iDot >= 0 && name.length() > iDot) { name = name.substring(iDot + 1); } if (name.length() > maxName) { maxName = name.length(); } } String maxNumString = String.valueOf(iThread + tlist.size()); int maxNumDigits = maxNumString.length(); for (int i = 0; i < tlist.size(); i++) { ThreadReference thr = tlist.get(i); char buf[] = new char[80]; for (int j = 0; j < 79; j++) { buf[j] = ' '; } buf[79] = '\0'; StringBuffer sbOut = new StringBuffer(); sbOut.append(buf); // Right-justify the thread number at start of output string String numString = String.valueOf(iThread + i + 1); sbOut.insert(maxNumDigits - numString.length(), numString); sbOut.insert(maxNumDigits, "."); int iBuf = maxNumDigits + 2; sbOut.insert(iBuf, Utils.description(thr)); iBuf += maxId + 1; String name = thr.name(); int iDot = name.lastIndexOf('.'); if (iDot >= 0 && name.length() > iDot) { name = name.substring(iDot + 1); } sbOut.insert(iBuf, name); iBuf += maxName + 1; sbOut.insert(iBuf, Utils.getStatus(thr)); sbOut.setLength(79); out.println(sbOut.toString()); } for (ThreadGroupReference tg0 : tg.threadGroups()) { if (!tg.equals(tg0)) { // TODO ref mgt iThread += printThreadGroup(out, tg0, iThread + tlist.size()); } } return tlist.size(); }
@Nullable public StackFrame getStackFrame() { try { return myThreadReference.frame(myIndex); } catch (Throwable t) { LOG.error(t); return null; } }
/** if this line can not be current => stepOver & return true. return false on the other hand. */ boolean resolveCanBeCurrent(ThreadReference tr) { try { Location l = tr.frame(0).location(); if (l == null) return false; return resolveCanBeCurrent( tr, Utils.getLineForSource(l.declaringType().name(), l.sourceName(), l.lineNumber())); } catch (Exception e) { } return false; }
public void resume() { switch (suspendPolicy()) { case EventRequest.SUSPEND_ALL: vm.resume(); break; case EventRequest.SUSPEND_EVENT_THREAD: ThreadReference thread = eventThread(); if (thread == null) { throw new InternalException("Inconsistent suspend policy"); } thread.resume(); break; case EventRequest.SUSPEND_NONE: // Do nothing break; default: throw new InternalException("Invalid suspend policy"); } }
private void dumpStack(ThreadReference thread, boolean showPC) { // ### Check for these. // env.failure("Thread no longer exists."); // env.failure("Target VM must be in interrupted state."); // env.failure("Current thread isn't suspended."); // ### Should handle extremely long stack traces sensibly for user. List<StackFrame> stack = null; try { stack = thread.frames(); } catch (IncompatibleThreadStateException e) { env.failure("Thread is not suspended."); } // ### Fix this! // ### Previously mishandled cases where thread was not current. // ### Now, prints all of the stack regardless of current frame. int frameIndex = 0; // int frameIndex = context.getCurrentFrameIndex(); if (stack == null) { env.failure("Thread is not running (no stack)."); } else { OutputSink out = env.getOutputSink(); int nFrames = stack.size(); for (int i = frameIndex; i < nFrames; i++) { StackFrame frame = stack.get(i); Location loc = frame.location(); Method meth = loc.method(); out.print(" [" + (i + 1) + "] "); out.print(meth.declaringType().name()); out.print('.'); out.print(meth.name()); out.print(" ("); if (meth.isNative()) { out.print("native method"); } else if (loc.lineNumber() != -1) { try { out.print(loc.sourceName()); } catch (AbsentInformationException e) { out.print("<unknown>"); } out.print(':'); out.print(loc.lineNumber()); } out.print(')'); if (showPC) { long pc = loc.codeIndex(); if (pc != -1) { out.print(", pc = " + pc); } } out.println(); } out.show(); } }
/** Executes breakpoint hit event. */ public void exec(com.sun.jdi.event.Event ev) { // S ystem.out.println ("exec "); // NOI18N removeStepRequest(); StepEvent event = (StepEvent) ev; ThreadReference tr = event.thread(); Location loc = event.location(); int ln = -1; String methodName = "?"; // NOI18N String className = "?"; // NOI18N String lineNumber = "?"; // NOI18N String threadName = tr.name(); Line l = null; if (loc != null) { if (loc.method() != null) methodName = loc.method().name(); className = loc.declaringType().name(); ln = loc.lineNumber(); if (ln >= 0) lineNumber = "" + loc.lineNumber(); } if (ln != -1) try { l = Utils.getLineForSource(className, loc.sourceName(), ln); } catch (AbsentInformationException e) { l = Utils.getLine(className, ln); } if (resolveCanBeCurrent(tr, l)) // if this line can not be current resolveCanBeCurrent () calls stepOver return; // line can be current if ((l == null) && (getLastAction() == ACTION_TRACE_INTO)) // try to find another "intelligent" line of code traceToSourceCode(tr); // you know - intelligent means that one with source code else { makeCurrent(threadName, className, methodName, lineNumber, l != null, tr); operator.stopRequest(); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; JavaStackFrame that = (JavaStackFrame) o; if (myIndex != that.myIndex) return false; if (!myThreadReference.equals(that.myThreadReference)) return false; return true; }
/** * Move through a list of stack frames, searching for references to code found in the current * sketch. Return with a RunnerException that contains the location of the error, or if nothing is * found, just return with a RunnerException that wraps the error message itself. */ protected SketchException findException( String message, ObjectReference or, ThreadReference thread) { try { // use to dump the stack for debugging // for (StackFrame frame : thread.frames()) { // System.out.println("frame: " + frame); // } List<StackFrame> frames = thread.frames(); for (StackFrame frame : frames) { try { Location location = frame.location(); String filename = null; filename = location.sourceName(); int lineNumber = location.lineNumber() - 1; SketchException rex = build.placeException(message, filename, lineNumber); if (rex != null) { return rex; } } catch (AbsentInformationException e) { // Any of the thread.blah() methods can throw an AbsentInformationEx // if that bit of data is missing. If so, just write out the error // message to the console. // e.printStackTrace(); // not useful exception = new SketchException(message); exception.hideStackTrace(); listener.statusError(exception); } } } catch (IncompatibleThreadStateException e) { // This shouldn't happen, but if it does, print the exception in case // it's something that needs to be debugged separately. e.printStackTrace(); } // before giving up, try to extract from the throwable object itself // since sometimes exceptions are re-thrown from a different context try { // assume object reference is Throwable, get stack trace Method method = ((ClassType) or.referenceType()) .concreteMethodByName("getStackTrace", "()[Ljava/lang/StackTraceElement;"); ArrayReference result = (ArrayReference) or.invokeMethod( thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED); // iterate through stack frames and pull filename and line number for each for (Value val : result.getValues()) { ObjectReference ref = (ObjectReference) val; method = ((ClassType) ref.referenceType()) .concreteMethodByName("getFileName", "()Ljava/lang/String;"); StringReference strref = (StringReference) ref.invokeMethod( thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED); String filename = strref == null ? "Unknown Source" : strref.value(); method = ((ClassType) ref.referenceType()).concreteMethodByName("getLineNumber", "()I"); IntegerValue intval = (IntegerValue) ref.invokeMethod( thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED); int lineNumber = intval.intValue() - 1; SketchException rex = build.placeException(message, filename, lineNumber); if (rex != null) { return rex; } } // for (Method m : ((ClassType) or.referenceType()).allMethods()) { // System.out.println(m + " | " + m.signature() + " | " + m.genericSignature()); // } // Implemented for 2.0b9, writes a stack trace when there's an internal error inside core. method = ((ClassType) or.referenceType()).concreteMethodByName("printStackTrace", "()V"); // System.err.println("got method " + method); or.invokeMethod( thread, method, new ArrayList<Value>(), ObjectReference.INVOKE_SINGLE_THREADED); } catch (Exception e) { e.printStackTrace(); } // Give up, nothing found inside the pile of stack frames SketchException rex = new SketchException(message); // exception is being created /here/, so stack trace is not useful rex.hideStackTrace(); return rex; }
public void breakpointReached(BreakpointEvent event) { String origMethodName = event.location().method().name(); String methodName = origMethodName.substring(2); ThreadReference tr = event.thread(); if (vm().canForceEarlyReturn()) { try { if ("bytef".equals(methodName)) { Field theValueField = targetClass.fieldByName("ebyteValue"); ByteValue theValue = (ByteValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); /* * See what happens if we access the stack after the force * and before the resume. Disabling this since spec says * the stack is undefined. This type of code can be used to * pursue just what that means. * * StackFrame sf = tr.frame(0); * List<Value> ll = sf.getArgumentValues(); * for (Value vv: ll) { * System.out.println("vv = " + vv); * } */ } else if ("charf".equals(methodName)) { Field theValueField = targetClass.fieldByName("echarValue"); CharValue theValue = (CharValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("doublef".equals(methodName)) { Field theValueField = targetClass.fieldByName("edoubleValue"); DoubleValue theValue = (DoubleValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("floatf".equals(methodName)) { Field theValueField = targetClass.fieldByName("efloatValue"); FloatValue theValue = (FloatValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("intf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eintValue"); IntegerValue theValue = (IntegerValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("longf".equals(methodName)) { Field theValueField = targetClass.fieldByName("elongValue"); LongValue theValue = (LongValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("shortf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eshortValue"); ShortValue theValue = (ShortValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("booleanf".equals(methodName)) { Field theValueField = targetClass.fieldByName("ebooleanValue"); BooleanValue theValue = (BooleanValue) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("stringf".equals(methodName)) { Field theValueField = targetClass.fieldByName("estringValue"); StringReference theValue = (StringReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("classf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eclassValue"); ClassObjectReference theValue = (ClassObjectReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("classLoaderf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eclassLoaderValue"); ClassLoaderReference theValue = (ClassLoaderReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("threadf".equals(methodName)) { Field theValueField = targetClass.fieldByName("ethreadValue"); ThreadReference theValue = (ThreadReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("threadGroupf".equals(methodName)) { Field theValueField = targetClass.fieldByName("ethreadGroupValue"); ThreadGroupReference theValue = (ThreadGroupReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("intArrayf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eintArrayValue"); ArrayReference theValue = (ArrayReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("nullObjectf".equals(methodName)) { tr.forceEarlyReturn(null); } else if ("objectf".equals(methodName)) { Field theValueField = targetClass.fieldByName("eobjectValue"); ObjectReference theValue = (ObjectReference) targetClass.getValue(theValueField); tr.forceEarlyReturn(theValue); } else if ("voidf".equals(methodName)) { VoidValue theValue = vm().mirrorOfVoid(); tr.forceEarlyReturn(theValue); } else { failure("failure: Unknown methodName: " + origMethodName); } } catch (Exception ex) { failure("failure: " + ex.toString()); ex.printStackTrace(); } } else { System.out.println("Cannot force early return for method: " + origMethodName); } }
/* * Complete the construction of an EventSet. This is called from * an event handler thread. It upacks the JDWP events inside * the packet and creates EventImpls for them. The EventSet is already * on EventQueues when this is called, so it has to be synch. */ synchronized void build() { if (pkt == null) { return; } PacketStream ps = new PacketStream(vm, pkt); JDWP.Event.Composite compEvt = new JDWP.Event.Composite(vm, ps); suspendPolicy = compEvt.suspendPolicy; if ((vm.traceFlags & vm.TRACE_EVENTS) != 0) { switch (suspendPolicy) { case JDWP.SuspendPolicy.ALL: vm.printTrace("EventSet: SUSPEND_ALL"); break; case JDWP.SuspendPolicy.EVENT_THREAD: vm.printTrace("EventSet: SUSPEND_EVENT_THREAD"); break; case JDWP.SuspendPolicy.NONE: vm.printTrace("EventSet: SUSPEND_NONE"); break; } } ThreadReference fix6485605 = null; for (int i = 0; i < compEvt.events.length; i++) { EventImpl evt = createEvent(compEvt.events[i]); if ((vm.traceFlags & vm.TRACE_EVENTS) != 0) { try { vm.printTrace("Event: " + evt); } catch (VMDisconnectedException ee) { // ignore - see bug 6502716 } } switch (evt.destination()) { case UNKNOWN_EVENT: // Ignore disabled, deleted, unknown events, but // save the thread if there is one since we might // have to resume it. Note that events for different // threads can't be in the same event set. if (evt instanceof ThreadedEventImpl && suspendPolicy == JDWP.SuspendPolicy.EVENT_THREAD) { fix6485605 = ((ThreadedEventImpl) evt).thread(); } continue; case CLIENT_EVENT: addEvent(evt); break; case INTERNAL_EVENT: if (internalEventSet == null) { internalEventSet = new EventSetImpl(this.vm, null); } internalEventSet.addEvent(evt); break; default: throw new InternalException("Invalid event destination"); } } pkt = null; // No longer needed - free it up // Avoid hangs described in 6296125, 6293795 if (super.size() == 0) { // This set has no client events. If we don't do // needed resumes, no one else is going to. if (suspendPolicy == JDWP.SuspendPolicy.ALL) { vm.resume(); } else if (suspendPolicy == JDWP.SuspendPolicy.EVENT_THREAD) { // See bug 6485605. if (fix6485605 != null) { fix6485605.resume(); } else { // apparently, there is nothing to resume. } } suspendPolicy = JDWP.SuspendPolicy.NONE; } }
public String toString() { return eventName() + " in thread " + thread.name(); }
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + myThreadReference.hashCode() * 31 + myIndex; return result; }