static synchronized String sourceLine(Location location, int lineNumber) throws IOException { if (lineNumber == -1) { throw new IllegalArgumentException(); } try { String fileName = location.sourceName(); Iterator<SourceCode> iter = sourceCache.iterator(); SourceCode code = null; while (iter.hasNext()) { SourceCode candidate = iter.next(); if (candidate.fileName().equals(fileName)) { code = candidate; iter.remove(); break; } } if (code == null) { BufferedReader reader = sourceReader(location); if (reader == null) { throw new FileNotFoundException(fileName); } code = new SourceCode(fileName, reader); if (sourceCache.size() == SOURCE_CACHE_SIZE) { sourceCache.remove(sourceCache.size() - 1); } } sourceCache.add(0, code); return code.sourceLine(lineNumber); } catch (AbsentInformationException e) { throw new IllegalArgumentException(); } }
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(); }
public static ClassFilter[] readFilters(List<Element> children) throws InvalidDataException { if (ContainerUtil.isEmpty(children)) { return ClassFilter.EMPTY_ARRAY; } ClassFilter[] filters = new ClassFilter[children.size()]; for (int i = 0, size = children.size(); i < size; i++) { filters[i] = create(children.get(i)); } return filters; }
public static ClassFilter[] readFilters(List children) throws InvalidDataException { if (children == null || children.size() == 0) { return ClassFilter.EMPTY_ARRAY; } List<ClassFilter> classFiltersList = new ArrayList<ClassFilter>(children.size()); for (Iterator i = children.iterator(); i.hasNext(); ) { final ClassFilter classFilter = new ClassFilter(); classFilter.readExternal((Element) i.next()); classFiltersList.add(classFilter); } return classFiltersList.toArray(new ClassFilter[classFiltersList.size()]); }
/** ******** test assist ********* */ Method getMethod(String className, String methodName) { List refs = vm().classesByName(className); if (refs.size() != 1) { failure("Test failure: " + refs.size() + " ReferenceTypes named: " + className); return null; } ReferenceType refType = (ReferenceType) refs.get(0); List meths = refType.methodsByName(methodName); if (meths.size() != 1) { failure("Test failure: " + meths.size() + " methods named: " + methodName); return null; } return (Method) meths.get(0); }
private static boolean elementListsEqual(List<Element> l1, List<Element> l2) { if (l1 == null) return l2 == null; if (l2 == null) return false; if (l1.size() != l2.size()) return false; Iterator<Element> i1 = l1.iterator(); for (Element aL2 : l2) { Element elem1 = i1.next(); if (!elementsEqual(elem1, aL2)) return false; } return true; }
private void commandMethods(StringTokenizer t) throws NoSessionException { if (!t.hasMoreTokens()) { env.error("No class specified."); return; } String idClass = t.nextToken(); ReferenceType cls = findClass(idClass); if (cls != null) { List<Method> methods = cls.allMethods(); OutputSink out = env.getOutputSink(); for (int i = 0; i < methods.size(); i++) { Method method = methods.get(i); out.print(method.declaringType().name() + " " + method.name() + "("); Iterator<String> it = method.argumentTypeNames().iterator(); if (it.hasNext()) { while (true) { out.print(it.next()); if (!it.hasNext()) { break; } out.print(", "); } } out.println(")"); } out.show(); } else { // ### Should validate class name syntax. env.failure("\"" + idClass + "\" is not a valid id or class name."); } }
static ReferenceType getReferenceTypeFromToken(String idToken) { ReferenceType cls = null; if (Character.isDigit(idToken.charAt(0))) { cls = null; } else if (idToken.startsWith("*.")) { // This notation saves typing by letting the user omit leading // package names. The first // loaded class whose name matches this limited regular // expression is selected. idToken = idToken.substring(1); for (ReferenceType type : Env.vm().allClasses()) { if (type.name().endsWith(idToken)) { cls = type; break; } } } else { // It's a class name List<ReferenceType> classes = Env.vm().classesByName(idToken); if (classes.size() > 0) { // TO DO: handle multiples cls = classes.get(0); } } return cls; }
private void commandClear(StringTokenizer t) throws NoSessionException { if (!t.hasMoreTokens()) { // Print set breakpoints listEventRequests(); return; } // ### need 'clear all' BreakpointSpec bpSpec = parseBreakpointSpec(t.nextToken()); if (bpSpec != null) { List<EventRequestSpec> specs = runtime.eventRequestSpecs(); if (specs.isEmpty()) { env.notice("No breakpoints set."); } else { List<EventRequestSpec> toDelete = new ArrayList<EventRequestSpec>(); for (EventRequestSpec spec : specs) { if (spec.equals(bpSpec)) { toDelete.add(spec); } } // The request used for matching should be found if (toDelete.size() <= 1) { env.notice("No matching breakpoint set."); } for (EventRequestSpec spec : toDelete) { runtime.delete(spec); } } } else { env.error("Ill-formed breakpoint specification."); } }
private static AttachingConnector getAttachingConnectorFor(String transport) { List acs = Bootstrap.virtualMachineManager().attachingConnectors(); AttachingConnector ac; int i, k = acs.size(); for (i = 0; i < k; i++) if ((ac = (AttachingConnector) acs.get(i)).transport().name().equals(transport)) return ac; return null; }
private static boolean elementListsEqual(List<Element> l1, List<Element> l2) { if (l1 == null) return l2 == null; if (l2 == null) return false; if (l1.size() != l2.size()) return false; Iterator<Element> i1 = l1.iterator(); Iterator<Element> i2 = l2.iterator(); while (i2.hasNext()) { Element elem1 = i1.next(); Element elem2 = i2.next(); if (!elementsEqual(elem1, elem2)) return false; } return true; }
private ReferenceType findClass(String pattern) throws NoSessionException { List<ReferenceType> results = runtime.findClassesMatchingPattern(pattern); if (results.size() > 0) { // ### Should handle multiple results sensibly. return results.get(0); } return null; }
String sourceLine(int number) { int index = number - 1; // list is 0-indexed if (index >= sourceLines.size()) { return null; } else { return sourceLines.get(index); } }
private static boolean attributeListsEqual(List<Attribute> l1, List<Attribute> l2) { if (l1 == null) return l2 == null; if (l2 == null) return false; if (l1.size() != l2.size()) return false; Iterator<Attribute> i1 = l1.iterator(); for (Attribute aL2 : l2) { Attribute attr1 = i1.next(); if (!Comparing.equal(attr1.getName(), aL2.getName()) || !Comparing.equal(attr1.getValue(), aL2.getValue())) { return false; } } return true; }
EventRequest request(int eventCmd, int requestId) { List rl = requestList(eventCmd); for (int i = rl.size() - 1; i >= 0; i--) { EventRequestImpl er = (EventRequestImpl) rl.get(i); if (er.id == requestId) { return er; } } return null; }
private ThreadReference[] threads() throws NoSessionException { if (threads == null) { ThreadIterator ti = new ThreadIterator(getDefaultThreadGroup()); List<ThreadReference> tlist = new ArrayList<ThreadReference>(); while (ti.hasNext()) { tlist.add(ti.nextThread()); } threads = tlist.toArray(new ThreadReference[tlist.size()]); } return threads; }
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(); } }
/** @return a list of threadGroupProxies */ public List<ThreadGroupReferenceProxyImpl> topLevelThreadGroups() { List<ThreadGroupReference> list = getVirtualMachine().topLevelThreadGroups(); List<ThreadGroupReferenceProxyImpl> result = new ArrayList<>(list.size()); for (ThreadGroupReference threadGroup : list) { result.add(getThreadGroupReferenceProxy(threadGroup)); } return result; }
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 static void addThreadDump( Project project, List<ThreadState> threads, final RunnerLayoutUi ui, DebuggerSession session) { final TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project); consoleBuilder.filters(ExceptionFilters.getFilters(session.getSearchScope())); final ConsoleView consoleView = consoleBuilder.getConsole(); final DefaultActionGroup toolbarActions = new DefaultActionGroup(); consoleView.allowHeavyFilters(); final ThreadDumpPanel panel = new ThreadDumpPanel(project, consoleView, toolbarActions, threads); final String id = THREAD_DUMP_CONTENT_PREFIX + " #" + myCurrentThreadDumpId; final Content content = ui.createContent(id, panel, id, null, null); content.putUserData(RunnerContentUi.LIGHTWEIGHT_CONTENT_MARKER, Boolean.TRUE); content.setCloseable(true); content.setDescription("Thread Dump"); ui.addContent(content); ui.selectAndFocus(content, true, true); myThreadDumpsCount++; myCurrentThreadDumpId++; Disposer.register( content, new Disposable() { @Override public void dispose() { myThreadDumpsCount--; if (myThreadDumpsCount == 0) { myCurrentThreadDumpId = 1; } } }); Disposer.register(content, consoleView); ui.selectAndFocus(content, true, false); if (threads.size() > 0) { panel.selectStackFrame(0); } }
private void commandList(StringTokenizer t) throws NoSessionException { ThreadReference current = context.getCurrentThread(); if (current == null) { env.error("No thread specified."); return; } Location loc; try { StackFrame frame = context.getCurrentFrame(current); if (frame == null) { env.failure("Thread has not yet begun execution."); return; } loc = frame.location(); } catch (VMNotInterruptedException e) { env.failure("Target VM must be in interrupted state."); return; } SourceModel source = sourceManager.sourceForLocation(loc); if (source == null) { if (loc.method().isNative()) { env.failure("Current method is native."); return; } env.failure("No source available for " + Utils.locationString(loc) + "."); return; } ReferenceType refType = loc.declaringType(); int lineno = loc.lineNumber(); if (t.hasMoreTokens()) { String id = t.nextToken(); // See if token is a line number. try { lineno = Integer.valueOf(id).intValue(); } catch (NumberFormatException nfe) { // It isn't -- see if it's a method name. List<Method> meths = refType.methodsByName(id); if (meths == null || meths.size() == 0) { env.failure( id + " is not a valid line number or " + "method name for class " + refType.name()); return; } else if (meths.size() > 1) { env.failure(id + " is an ambiguous method name in" + refType.name()); return; } loc = meths.get(0).location(); lineno = loc.lineNumber(); } } int startLine = (lineno > 4) ? lineno - 4 : 1; int endLine = startLine + 9; String sourceLine = source.sourceLine(lineno); if (sourceLine == null) { env.failure("" + lineno + " is an invalid line number for " + refType.name()); } else { OutputSink out = env.getOutputSink(); for (int i = startLine; i <= endLine; i++) { sourceLine = source.sourceLine(i); if (sourceLine == null) { break; } out.print(i); out.print("\t"); if (i == lineno) { out.print("=> "); } else { out.print(" "); } out.println(sourceLine); } out.show(); } }