private void handleAppStop() { if (mConfig.isExpExpired()) { return; } if (mInfo != null) { mInfo.save(mRuntimeInfo.stop()); mInfo = null; sendSavedHistory(); } }
void checkThreadInfo(ThreadInfo info) { if (!getName().equals(info.getThreadName())) { throw new RuntimeException( "Name: " + info.getThreadName() + " not matched. Expected: " + getName()); } MonitorInfo[] monitors = info.getLockedMonitors(); if (monitors.length != OWNED_MONITORS) { throw new RuntimeException( "Number of locked monitors = " + monitors.length + " not matched. Expected: " + OWNED_MONITORS); } MonitorInfo m = monitors[0]; StackTraceElement ste = m.getLockedStackFrame(); int depth = m.getLockedStackDepth(); StackTraceElement[] stacktrace = info.getStackTrace(); if (!ste.equals(stacktrace[depth])) { System.out.println("LockedStackFrame:- " + ste); System.out.println("StackTrace at " + depth + " :-" + stacktrace[depth]); throw new RuntimeException( "LockedStackFrame does not match " + "stack frame in ThreadInfo.getStackTrace"); } String className = lock.getClass().getName(); int hcode = System.identityHashCode(lock); if (!className.equals(m.getClassName()) || hcode != m.getIdentityHashCode() || !m.getLockedStackFrame().getMethodName().equals("run")) { System.out.println(info); throw new RuntimeException("MonitorInfo " + m + " doesn't match."); } LockInfo[] syncs = info.getLockedSynchronizers(); if (syncs.length != OWNED_SYNCS) { throw new RuntimeException( "Number of locked syncs = " + syncs.length + " not matched. Expected: " + OWNED_SYNCS); } AbstractOwnableSynchronizer s = mutex.getSync(); String lockName = s.getClass().getName(); hcode = System.identityHashCode(s); if (!lockName.equals(syncs[0].getClassName())) { throw new RuntimeException( "LockInfo : " + syncs[0] + " class name not matched. Expected: " + lockName); } if (hcode != syncs[0].getIdentityHashCode()) { throw new RuntimeException( "LockInfo: " + syncs[0] + " IdentityHashCode not matched. Expected: " + hcode); } LockInfo li = info.getLockInfo(); if (li == null) { throw new RuntimeException("Expected non-null LockInfo"); } }
private static List<ThreadState> buildThreadStates(VirtualMachineProxyImpl vmProxy) { final List<ThreadReference> threads = vmProxy.getVirtualMachine().allThreads(); final List<ThreadState> result = new ArrayList<ThreadState>(); final Map<String, ThreadState> nameToThreadMap = new HashMap<String, ThreadState>(); final Map<String, String> waitingMap = new HashMap<String, String>(); // key 'waits_for' value for (ThreadReference threadReference : threads) { final StringBuilder buffer = new StringBuilder(); boolean hasEmptyStack = true; final int threadStatus = threadReference.status(); if (threadStatus == ThreadReference.THREAD_STATUS_ZOMBIE) { continue; } final String threadName = threadName(threadReference); final ThreadState threadState = new ThreadState(threadName, threadStatusToState(threadStatus)); nameToThreadMap.put(threadName, threadState); result.add(threadState); threadState.setJavaThreadState(threadStatusToJavaThreadState(threadStatus)); buffer.append("\"").append(threadName).append("\""); ReferenceType referenceType = threadReference.referenceType(); if (referenceType != null) { //noinspection HardCodedStringLiteral Field daemon = referenceType.fieldByName("daemon"); if (daemon != null) { Value value = threadReference.getValue(daemon); if (value instanceof BooleanValue && ((BooleanValue) value).booleanValue()) { buffer .append(" ") .append(DebuggerBundle.message("threads.export.attribute.label.daemon")); threadState.setDaemon(true); } } //noinspection HardCodedStringLiteral Field priority = referenceType.fieldByName("priority"); if (priority != null) { Value value = threadReference.getValue(priority); if (value instanceof IntegerValue) { buffer .append(" ") .append( DebuggerBundle.message( "threads.export.attribute.label.priority", ((IntegerValue) value).intValue())); } } Field tid = referenceType.fieldByName("tid"); if (tid != null) { Value value = threadReference.getValue(tid); if (value instanceof LongValue) { buffer .append(" ") .append( DebuggerBundle.message( "threads.export.attribute.label.tid", Long.toHexString(((LongValue) value).longValue()))); buffer.append(" nid=NA"); } } } // ThreadGroupReference groupReference = threadReference.threadGroup(); // if (groupReference != null) { // buffer.append(", ").append(DebuggerBundle.message("threads.export.attribute.label.group", // groupReference.name())); // } final String state = threadState.getState(); if (state != null) { buffer.append(" ").append(state); } buffer.append("\n java.lang.Thread.State: ").append(threadState.getJavaThreadState()); try { if (vmProxy.canGetOwnedMonitorInfo() && vmProxy.canGetMonitorInfo()) { List<ObjectReference> list = threadReference.ownedMonitors(); for (ObjectReference reference : list) { if (!vmProxy.canGetMonitorFrameInfo()) { // java 5 and earlier buffer.append("\n\t ").append(renderLockedObject(reference)); } final List<ThreadReference> waiting = reference.waitingThreads(); for (ThreadReference thread : waiting) { final String waitingThreadName = threadName(thread); waitingMap.put(waitingThreadName, threadName); buffer .append("\n\t ") .append( DebuggerBundle.message( "threads.export.attribute.label.blocks.thread", waitingThreadName)); } } } ObjectReference waitedMonitor = vmProxy.canGetCurrentContendedMonitor() ? threadReference.currentContendedMonitor() : null; if (waitedMonitor != null) { if (vmProxy.canGetMonitorInfo()) { ThreadReference waitedMonitorOwner = waitedMonitor.owningThread(); if (waitedMonitorOwner != null) { final String monitorOwningThreadName = threadName(waitedMonitorOwner); waitingMap.put(threadName, monitorOwningThreadName); buffer .append("\n\t ") .append( DebuggerBundle.message( "threads.export.attribute.label.waiting.for.thread", monitorOwningThreadName, renderObject(waitedMonitor))); } } } final List<StackFrame> frames = threadReference.frames(); hasEmptyStack = frames.size() == 0; final TIntObjectHashMap<List<ObjectReference>> lockedAt = new TIntObjectHashMap<List<ObjectReference>>(); if (vmProxy.canGetMonitorFrameInfo()) { for (MonitorInfo info : threadReference.ownedMonitorsAndFrames()) { final int stackDepth = info.stackDepth(); List<ObjectReference> monitors; if ((monitors = lockedAt.get(stackDepth)) == null) { lockedAt.put(stackDepth, monitors = new SmartList<ObjectReference>()); } monitors.add(info.monitor()); } } for (int i = 0, framesSize = frames.size(); i < framesSize; i++) { final StackFrame stackFrame = frames.get(i); try { final Location location = stackFrame.location(); buffer.append("\n\t ").append(renderLocation(location)); final List<ObjectReference> monitors = lockedAt.get(i); if (monitors != null) { for (ObjectReference monitor : monitors) { buffer.append("\n\t - ").append(renderLockedObject(monitor)); } } } catch (InvalidStackFrameException e) { buffer.append("\n\t Invalid stack frame: ").append(e.getMessage()); } } } catch (IncompatibleThreadStateException e) { buffer .append("\n\t ") .append(DebuggerBundle.message("threads.export.attribute.error.incompatible.state")); } threadState.setStackTrace(buffer.toString(), hasEmptyStack); ThreadDumpParser.inferThreadStateDetail(threadState); } for (String waiting : waitingMap.keySet()) { final ThreadState waitingThread = nameToThreadMap.get(waiting); final ThreadState awaitedThread = nameToThreadMap.get(waitingMap.get(waiting)); awaitedThread.addWaitingThread(waitingThread); } // detect simple deadlocks for (ThreadState thread : result) { for (ThreadState awaitingThread : thread.getAwaitingThreads()) { if (awaitingThread.isAwaitedBy(thread)) { thread.addDeadlockedThread(awaitingThread); awaitingThread.addDeadlockedThread(thread); } } } ThreadDumpParser.sortThreads(result); return result; }
private void startMonitoring() { if (mCurrentAppInfo != null && mCurrentAppInfo.isCheckable()) { mInfo = new MonitorInfo(mCurrentAppInfo, mdb); Log.d("trans", "### start : " + mInfo.toString()); } }