private void showDefaultInfo(ThreadGroup grp) { TreeSet<Thread> threadSet = new TreeSet<Thread>( new Comparator<Thread>() { @Override public int compare(Thread t1, Thread t2) { return Long.valueOf(t1.getId()).compareTo(t2.getId()); } }); findThreads(grp, threadSet); PrintWriter out = getOutput().getPrintWriter(); for (final Thread thread : threadSet) { VmThread vmThread = AccessController.doPrivileged( new PrivilegedAction<VmThread>() { public VmThread run() { return ThreadHelper.getVmThread(thread); } }); out.println( " " + thread.getId() + SEPARATOR + thread.getName() + SEPARATOR + thread.getPriority() + SEPARATOR + vmThread.getThreadStateName()); } }
/** * Traverse the ThreadGroups threads and its child ThreadGroups printing information for each * thread found. If 'threadName' is non-null, only print information for the thread that matches * the name. * * @param grp the ThreadGroup to traverse * @param out the destination for output * @param threadName if non-null, only display this thread. */ private void showThreads(ThreadGroup grp, PrintWriter out, String threadName) { if (threadName == null) { out.println(GROUP + grp.getName()); } final int max = grp.activeCount() * 2; final Thread[] ts = new Thread[max]; grp.enumerate(ts); for (int i = 0; i < max; i++) { final Thread t = ts[i]; if (t != null) { if ((threadName == null) || threadName.equals(t.getName())) { VmThread vmThread = AccessController.doPrivileged( new PrivilegedAction<VmThread>() { public VmThread run() { return ThreadHelper.getVmThread(t); } }); out.println( SLASH_T + t.getId() + SEPARATOR + t.getName() + SEPARATOR + t.getPriority() + SEPARATOR + vmThread.getThreadStateName()); if (threadName != null) { final Object[] trace = VmThread.getStackTrace(vmThread); final int traceLen = trace.length; out.println(SLASH_T + SLASH_T + TRACE); for (int k = 0; k < traceLen; k++) { out.println(SLASH_T + SLASH_T + trace[k]); } return; } } } } final int gmax = grp.activeGroupCount() * 2; final ThreadGroup[] tgs = new ThreadGroup[gmax]; grp.enumerate(tgs); for (int i = 0; i < gmax; i++) { final ThreadGroup tg = tgs[i]; if (tg != null) { showThreads(tg, out, threadName); } } }
private static synchronized Throwable fillInStackTrace(Throwable instance) { ObjectReference.fromObject(instance) .toAddress() .add(BACKTRACE_OFFSET) .store( ObjectReference.fromObject( VmThread.getStackTrace(VmProcessor.current().getCurrentThread()))); return instance; }
/** * Show the current stacktrace using Screen.debug. TODO that method only exist to have line * numbers : find a way to add line numbers to debugStackTrace(max) */ @KernelSpace public final void debugStackTraceWithLineNumbers(int max) { final VmThread current = VmThread.currentThread(); final VmStackFrame[] frames = (VmStackFrame[]) VmThread.getStackTrace(current); if (frames == null) { Unsafe.debug("Debug stacktrace:<no stack trace>\n"); } else { Unsafe.debug("Debug stacktrace: "); for (VmStackFrame frame : frames) { final VmStackFrame s = (VmStackFrame) frame; Unsafe.debug(s.getMethod().getDeclaringClass().getName()); Unsafe.debug("::"); Unsafe.debug(s.getMethod().getName()); Unsafe.debug(":"); Unsafe.debug(s.getLocationInfo()); Unsafe.debug('\n'); } } }
private void getThreads(Map<String, Thread> map, ThreadGroup grp, int state) { final int max = grp.activeCount() * 2; final Thread[] ts = new Thread[max]; grp.enumerate(ts); for (int i = 0; i < max; i++) { final Thread t = ts[i]; if (t != null) { final VmThread vmThread = ThreadHelper.getVmThread(t); final boolean add; switch (state) { case ST_ALL: add = true; break; case ST_RUNNING: add = vmThread.isRunning(); break; case ST_WAITING: add = vmThread.isWaiting(); break; default: add = false; } if (add) { map.put(t.getName(), t); } } } final int gmax = grp.activeGroupCount() * 2; final ThreadGroup[] tgs = new ThreadGroup[gmax]; grp.enumerate(tgs); for (int i = 0; i < gmax; i++) { final ThreadGroup tg = tgs[i]; if (tg != null) { getThreads(map, tg, state); } } }
/** Show the stacktrace of the given thread using Screen.debug. */ @KernelSpace public final void debugStackTrace(VmThread thread) { Address f = thread.getStackFrame(); Unsafe.debug("Debug stacktrace: "); boolean first = true; int max = 20; while (isValid(f) && (max > 0)) { if (first) { first = false; } else { Unsafe.debug(", "); } final VmMethod method = getMethod(f); final VmType vmClass = method.getDeclaringClass(); Unsafe.debug(vmClass.getName()); Unsafe.debug("::"); Unsafe.debug(method.getName()); f = getPrevious(f); max--; } if (isValid(f)) { Unsafe.debug("..."); } }