Java Code Examples for java.lang.management.ThreadInfo#getLockedMonitors()

The following examples show how to use java.lang.management.ThreadInfo#getLockedMonitors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimedOutTestsListener.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
    // print thread information
    printThread(ti, out);

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        out.println(indent + "at " + ste.toString());
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                out.println(indent + "  - locked " + mi);
            }
        }
    }
    out.println();
}
 
Example 2
Source File: NbTestCase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static StringBuilder printThreadInfo(ThreadInfo ti, StringBuilder sb) {
    // print thread information
    printThread(ti, sb);

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        sb.append("\t at " + ste.toString()).append("\n");
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t  - locked " + mi).append("\n");
            }
        }
    }
    sb.append("\n");
    return sb;
}
 
Example 3
Source File: ThreadMonitor.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 4
Source File: ThreadMonitor.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 5
Source File: ThreadMonitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 6
Source File: ThreadMonitor.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 7
Source File: ThreadMonitor.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 8
Source File: ThreadMonitor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void printThreadInfo(ThreadInfo ti) {
   // print thread information
   printThread(ti);

   // print stack trace with locks
   StackTraceElement[] stacktrace = ti.getStackTrace();
   MonitorInfo[] monitors = ti.getLockedMonitors();
   for (int i = 0; i < stacktrace.length; i++) {
       StackTraceElement ste = stacktrace[i];
       System.out.println(INDENT + "at " + ste.toString());
       for (MonitorInfo mi : monitors) {
           if (mi.getLockedStackDepth() == i) {
               System.out.println(INDENT + "  - locked " + mi);
           }
       }
   }
   System.out.println();
}
 
Example 9
Source File: Watchdog.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private static void dumpThread(ThreadInfo thread, Logger logger) {
    logger.emergency("Current Thread: " + thread.getThreadName());
    logger.emergency("\tPID: " + thread.getThreadId() + " | Suspended: " + thread.isSuspended() + " | Native: " + thread.isInNative() + " | State: " + thread.getThreadState());
    // Monitors
    if (thread.getLockedMonitors().length != 0) {
        logger.emergency("\tThread is waiting on monitor(s):");
        for (MonitorInfo monitor : thread.getLockedMonitors()) {
            logger.emergency("\t\tLocked on:" + monitor.getLockedStackFrame());
        }
    }

    logger.emergency("\tStack:");
    for (StackTraceElement stack : thread.getStackTrace()) {
        logger.emergency("\t\t" + stack);
    }
}
 
Example 10
Source File: ThreadMonitor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void printMonitorInfo(ThreadInfo ti) {
   MonitorInfo[] monitors = ti.getLockedMonitors();
   System.out.println(INDENT + "Locked monitors: count = " + monitors.length);
   for (MonitorInfo mi : monitors) {
       System.out.println(INDENT + "  - " + mi + " locked at ");
       System.out.println(INDENT + "      " + mi.getLockedStackDepth() +
                          " " + mi.getLockedStackFrame());
   }
}
 
Example 11
Source File: Detector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void printMonitorInfo(ThreadInfo ti, PrintStream out) {
   MonitorInfo[] monitors = ti.getLockedMonitors();
   out.println(INDENT + "Locked monitors: count = " + monitors.length); // NOI18N
   for (MonitorInfo mi : monitors) {
       out.println(INDENT + "  - " + mi + " locked at "); // NOI18N
       out.println(INDENT + "      " + mi.getLockedStackDepth() + // NOI18N
                          " " + mi.getLockedStackFrame());       // NOI18N
   }
}
 
Example 12
Source File: Diagnostics.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Formats the thread dump for one thread.
 *
 * @param ti the ThreadInfo describing the thread
 * @return the formatted thread dump
 */
private static String getThreadDump(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(getThreadDumpHeader(ti));
    for (LockInfo li : ti.getLockedSynchronizers()) {
        sb.append(INDENT2 + "locks " +
                  li.toString() + CRLF);
    }
    boolean start = true;
    StackTraceElement[] stes = ti.getStackTrace();
    Object[] monitorDepths = new Object[stes.length];
    MonitorInfo[] mis = ti.getLockedMonitors();
    for (int i = 0; i < mis.length; i++) {
        monitorDepths[mis[i].getLockedStackDepth()] = mis[i];
    }
    for (int i = 0; i < stes.length; i++) {
        StackTraceElement ste = stes[i];
        sb.append(INDENT2 +
                  "at " + ste.toString() + CRLF);
        if (start) {
            if (ti.getLockName() != null) {
                sb.append(INDENT2 + "- waiting on (a " +
                          ti.getLockName() + ")");
                if (ti.getLockOwnerName() != null) {
                    sb.append(" owned by " + ti.getLockOwnerName() +
                              " Id=" + ti.getLockOwnerId());
                }
                sb.append(CRLF);
            }
            start = false;
        }
        if (monitorDepths[i] != null) {
            MonitorInfo mi = (MonitorInfo)monitorDepths[i];
            sb.append(INDENT2 +
                      "- locked (a " + mi.toString() + ")"+
                      " index " + mi.getLockedStackDepth() +
                      " frame " + mi.getLockedStackFrame().toString());
            sb.append(CRLF);

        }
    }
    return sb.toString();
}
 
Example 13
Source File: ThreadDump.java    From metrics with Apache License 2.0 4 votes vote down vote up
/**
 * Dumps all of the threads' current information to an output stream.
 *
 * @param out an output stream
 */
public void dump(OutputStream out) {
    final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
    final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));

    for (int ti = threads.length - 1; ti >= 0; ti--) {
        final ThreadInfo t = threads[ti];
        writer.printf("%s id=%d state=%s",
                      t.getThreadName(),
                      t.getThreadId(),
                      t.getThreadState());
        final LockInfo lock = t.getLockInfo();
        if (lock != null && t.getThreadState() != Thread.State.BLOCKED) {
            writer.printf("%n    - waiting on <0x%08x> (a %s)",
                          lock.getIdentityHashCode(),
                          lock.getClassName());
            writer.printf("%n    - locked <0x%08x> (a %s)",
                          lock.getIdentityHashCode(),
                          lock.getClassName());
        } else if (lock != null && t.getThreadState() == Thread.State.BLOCKED) {
            writer.printf("%n    - waiting to lock <0x%08x> (a %s)",
                          lock.getIdentityHashCode(),
                          lock.getClassName());
        }

        if (t.isSuspended()) {
            writer.print(" (suspended)");
        }

        if (t.isInNative()) {
            writer.print(" (running in native)");
        }

        writer.println();
        if (t.getLockOwnerName() != null) {
            writer.printf("     owned by %s id=%d%n", t.getLockOwnerName(), t.getLockOwnerId());
        }

        final StackTraceElement[] elements = t.getStackTrace();
        final MonitorInfo[] monitors = t.getLockedMonitors();

        for (int i = 0; i < elements.length; i++) {
            final StackTraceElement element = elements[i];
            writer.printf("    at %s%n", element);
            for (int j = 1; j < monitors.length; j++) {
                final MonitorInfo monitor = monitors[j];
                if (monitor.getLockedStackDepth() == i) {
                    writer.printf("      - locked %s%n", monitor);
                }
            }
        }
        writer.println();

        final LockInfo[] locks = t.getLockedSynchronizers();
        if (locks.length > 0) {
            writer.printf("    Locked synchronizers: count = %d%n", locks.length);
            for (LockInfo l : locks) {
                writer.printf("      - %s%n", l);
            }
            writer.println();
        }
    }

    writer.println();
    writer.flush();
}
 
Example 14
Source File: JmxSupport.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private void print16Thread(final StringBuilder sb, final ThreadMXBean threadMXBean, final ThreadInfo thread) {
    MonitorInfo[] monitors = null;
    if (threadMXBean.isObjectMonitorUsageSupported()) {
        monitors = thread.getLockedMonitors();
    }
    sb.append("\n\"" + thread.getThreadName() + // NOI18N
            "\" - Thread t@" + thread.getThreadId() + "\n");    // NOI18N
    sb.append("   java.lang.Thread.State: " + thread.getThreadState()); // NOI18N
    sb.append("\n");   // NOI18N
    int index = 0;
    for (StackTraceElement st : thread.getStackTrace()) {
        LockInfo lock = thread.getLockInfo();
        String lockOwner = thread.getLockOwnerName();
        
        sb.append("\tat " + st.toString() + "\n");    // NOI18N
        if (index == 0) {
            if ("java.lang.Object".equals(st.getClassName()) &&     // NOI18N
                    "wait".equals(st.getMethodName())) {                // NOI18N
                if (lock != null) {
                    sb.append("\t- waiting on ");    // NOI18N
                    printLock(sb,lock);
                    sb.append("\n");    // NOI18N
                }
            } else if (lock != null) {
                if (lockOwner == null) {
                    sb.append("\t- parking to wait for ");      // NOI18N
                    printLock(sb,lock);
                    sb.append("\n");            // NOI18N
                } else {
                    sb.append("\t- waiting to lock ");      // NOI18N
                    printLock(sb,lock);
                    sb.append(" owned by \""+lockOwner+"\" t@"+thread.getLockOwnerId()+"\n");   // NOI18N
                }
            }
        }
        printMonitors(sb, monitors, index);
        index++;
    }
    StringBuilder jnisb = new StringBuilder();
    printMonitors(jnisb, monitors, -1);
    if (jnisb.length() > 0) {
        sb.append("   JNI locked monitors:\n");
        sb.append(jnisb);
    }
    if (threadMXBean.isSynchronizerUsageSupported()) {
        sb.append("\n   Locked ownable synchronizers:");    // NOI18N
        LockInfo[] synchronizers = thread.getLockedSynchronizers();
        if (synchronizers == null || synchronizers.length == 0) {
            sb.append("\n\t- None\n");  // NOI18N
        } else {
            for (LockInfo li : synchronizers) {
                sb.append("\n\t- locked ");         // NOI18N
                printLock(sb,li);
                sb.append("\n");  // NOI18N
            }
        }
    }
}
 
Example 15
Source File: StackTraces.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void dumpThreadInfo(ThreadInfo t, Thread thread, PrintStream out) {
  out.print("\"" + t.getThreadName() + "\"" +
            " Id=" + t.getThreadId() + " " +
            t.getThreadState());
  if (t.getLockName() != null) {
    out.print(" on " + t.getLockName());
  }
  if (t.getLockOwnerName() != null) {
    out.print(" owned by \"" + t.getLockOwnerName() +
              "\" Id=" + t.getLockOwnerId());
  }
  if (t.isSuspended()) {
    out.print(" (suspended)");
  }
  if (t.isInNative()) {
    out.print(" (in native)");
  }
  if (thread.isDaemon()) {
    out.print(" (daemon)");
  }
  out.print('\n');
  StackTraceElement[] stackTrace = t.getStackTrace();
  MonitorInfo[] lockedMonitors = t.getLockedMonitors();
  for (int i = 0; i < stackTrace.length; i++) {
    StackTraceElement ste = stackTrace[i];
    out.print("\tat " + ste.toString());
    out.print('\n');
    if (i == 0 && t.getLockInfo() != null) {
      Thread.State ts = t.getThreadState();
      switch (ts) {
        case BLOCKED:
          out.print("\t-  blocked on " + t.getLockInfo());
          out.print('\n');
          break;
        case WAITING:
          out.print("\t-  waiting on " + t.getLockInfo());
          out.print('\n');
          break;
        case TIMED_WAITING:
          out.print("\t-  waiting on " + t.getLockInfo());
          out.print('\n');
          break;
        default:
      }
    }

    for (MonitorInfo mi : lockedMonitors) {
      if (mi.getLockedStackDepth() == i) {
        out.print("\t-  locked " + mi);
        out.print('\n');
      }
    }
  }

  LockInfo[] locks = t.getLockedSynchronizers();
  if (locks.length > 0) {
    out.print("\n\tNumber of locked synchronizers = " + locks.length);
    out.print('\n');
    for (LockInfo li : locks) {
      out.print("\t- " + li);
      out.print('\n');
    }
  }
  out.print('\n');
}
 
Example 16
Source File: OWLHandler.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static CharSequence toString(ThreadInfo threadInfo) {
	StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
	if (threadInfo.getLockName() != null) {
		sb.append(" on " + threadInfo.getLockName());
	}
	if (threadInfo.getLockOwnerName() != null) {
		sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId());
	}
	if (threadInfo.isSuspended()) {
		sb.append(" (suspended)");
	}
	if (threadInfo.isInNative()) {
		sb.append(" (in native)");
	}
	sb.append('\n');
	StackTraceElement[] stackTrace = threadInfo.getStackTrace();
	int i = 0;
	for (; i < stackTrace.length; i++) {
		StackTraceElement ste = stackTrace[i];
		sb.append("\tat " + ste.toString());
		sb.append('\n');
		if (i == 0 && threadInfo.getLockInfo() != null) {
			Thread.State ts = threadInfo.getThreadState();
			switch (ts) {
			case BLOCKED:
				sb.append("\t-  blocked on " + threadInfo.getLockInfo());
				sb.append('\n');
				break;
			case WAITING:
				sb.append("\t-  waiting on " + threadInfo.getLockInfo());
				sb.append('\n');
				break;
			case TIMED_WAITING:
				sb.append("\t-  waiting on " + threadInfo.getLockInfo());
				sb.append('\n');
				break;
			default:
			}
		}

		for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
			if (mi.getLockedStackDepth() == i) {
				sb.append("\t-  locked " + mi);
				sb.append('\n');
			}
		}
	}
	LockInfo[] locks = threadInfo.getLockedSynchronizers();
	if (locks.length > 0) {
		sb.append("\n\tNumber of locked synchronizers = " + locks.length);
		sb.append('\n');
		for (LockInfo li : locks) {
			sb.append("\t- " + li);
			sb.append('\n');
		}
	}
	sb.append('\n');
	return sb;
}
 
Example 17
Source File: ThreadDumpUtil.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private static String threadInfoToString(ThreadInfo threadInfo) {
   StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" +
         " Id=" + threadInfo.getThreadId() + " " +
         threadInfo.getThreadState());
   if (threadInfo.getLockName() != null) {
      sb.append(" on " + threadInfo.getLockName());
   }
   if (threadInfo.getLockOwnerName() != null) {
      sb.append(" owned by \"" + threadInfo.getLockOwnerName() +
            "\" Id=" + threadInfo.getLockOwnerId());
   }
   if (threadInfo.isSuspended()) {
      sb.append(" (suspended)");
   }
   if (threadInfo.isInNative()) {
      sb.append(" (in native)");
   }
   sb.append('\n');
   int i = 0;
   for (; i < threadInfo.getStackTrace().length; i++) {
      StackTraceElement ste = threadInfo.getStackTrace()[i];
      sb.append("\tat " + ste.toString());
      sb.append('\n');
      if (i == 0 && threadInfo.getLockInfo() != null) {
         Thread.State ts = threadInfo.getThreadState();
         switch (ts) {
            case BLOCKED:
               sb.append("\t-  blocked on " + threadInfo.getLockInfo());
               sb.append('\n');
               break;
            case WAITING:
               sb.append("\t-  waiting on " + threadInfo.getLockInfo());
               sb.append('\n');
               break;
            case TIMED_WAITING:
               sb.append("\t-  waiting on " + threadInfo.getLockInfo());
               sb.append('\n');
               break;
            default:
         }
      }

      for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
         if (mi.getLockedStackDepth() == i) {
            sb.append("\t-  locked " + mi);
            sb.append('\n');
         }
      }
   }

   LockInfo[] locks = threadInfo.getLockedSynchronizers();
   if (locks.length > 0) {
      sb.append("\n\tNumber of locked synchronizers = " + locks.length);
      sb.append('\n');
      for (LockInfo li : locks) {
         sb.append("\t- " + li);
         sb.append('\n');
      }
   }
   sb.append('\n');
   return sb.toString();
}
 
Example 18
Source File: AbstractContainer.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private String getThreadStackTraces(final ThreadInfo threadInfo)
{
    String lineSeparator = System.lineSeparator();
    StringBuilder dump = new StringBuilder();
    dump.append("\"").append(threadInfo.getThreadName()).append("\"").append(" Id=")
        .append(threadInfo.getThreadId()).append( " ").append(threadInfo.getThreadState());
    if (threadInfo.getLockName() != null)
    {
        dump.append(" on ").append(threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null)
    {
        dump.append(" owned by \"").append(threadInfo.getLockOwnerName())
            .append("\" Id=").append(threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended())
    {
        dump.append(" (suspended)");
    }
    if (threadInfo.isInNative())
    {
        dump.append(" (in native)");
    }
    dump.append(lineSeparator);
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++)
    {
        StackTraceElement stackTraceElement = stackTrace[i];
        dump.append("    at ").append(stackTraceElement.toString()).append(lineSeparator);

        LockInfo lockInfo = threadInfo.getLockInfo();
        if (i == 0 && lockInfo != null)
        {
            Thread.State threadState = threadInfo.getThreadState();
            switch (threadState)
            {
                case BLOCKED:
                    dump.append("    -  blocked on ").append(lockInfo).append(lineSeparator);
                    break;
                case WAITING:
                    dump.append("    -  waiting on ").append(lockInfo).append(lineSeparator);
                    break;
                case TIMED_WAITING:
                    dump.append("    -  waiting on ").append(lockInfo).append(lineSeparator);
                    break;
                default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors())
        {
            if (mi.getLockedStackDepth() == i)
            {
                dump.append("    -  locked ").append(mi).append(lineSeparator);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0)
    {
        dump.append(lineSeparator).append("    Number of locked synchronizers = ").append(locks.length);
        dump.append(lineSeparator);
        for (LockInfo li : locks)
        {
            dump.append("    - " + li);
            dump.append(lineSeparator);
        }
    }
    dump.append(lineSeparator);
    return dump.toString();
}
 
Example 19
Source File: GfxdLocalLockService.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void generateThreadDump(StringBuilder msg) {
  ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  for (ThreadInfo tInfo : mbean.dumpAllThreads(true, true)) {
    msg.append('"').append(tInfo.getThreadName()).append('"').append(" Id=")
        .append(tInfo.getThreadId()).append(' ')
        .append(tInfo.getThreadState());
    if (tInfo.getLockName() != null) {
      msg.append(" on ").append(tInfo.getLockName());
    }
    if (tInfo.getLockOwnerName() != null) {
      msg.append(" owned by \"").append(tInfo.getLockOwnerName())
          .append("\" Id=").append(tInfo.getLockOwnerId());
    }
    if (tInfo.isSuspended()) {
      msg.append(" (suspended)");
    }
    if (tInfo.isInNative()) {
      msg.append(" (in native)");
    }
    msg.append(SanityManager.lineSeparator);
    final StackTraceElement[] stackTrace = tInfo.getStackTrace();
    for (int index = 0; index < stackTrace.length; ++index) {
      msg.append("\tat ").append(stackTrace[index].toString())
          .append(SanityManager.lineSeparator);
      if (index == 0 && tInfo.getLockInfo() != null) {
        final Thread.State ts = tInfo.getThreadState();
        switch (ts) {
          case BLOCKED:
            msg.append("\t-  blocked on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          case TIMED_WAITING:
            msg.append("\t-  waiting on ").append(tInfo.getLockInfo())
                .append(SanityManager.lineSeparator);
            break;
          default:
        }
      }

      for (MonitorInfo mi : tInfo.getLockedMonitors()) {
        if (mi.getLockedStackDepth() == index) {
          msg.append("\t-  locked ").append(mi)
              .append(SanityManager.lineSeparator);
        }
      }
    }

    final LockInfo[] locks = tInfo.getLockedSynchronizers();
    if (locks.length > 0) {
      msg.append(SanityManager.lineSeparator)
          .append("\tNumber of locked synchronizers = ").append(locks.length)
          .append(SanityManager.lineSeparator);
      for (LockInfo li : locks) {
        msg.append("\t- ").append(li).append(SanityManager.lineSeparator);
      }
    }
    msg.append(SanityManager.lineSeparator);
  }
}
 
Example 20
Source File: Diagnostics.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Formats the thread dump for one thread.
 *
 * @param ti the ThreadInfo describing the thread
 * @return the formatted thread dump
 */
private static String getThreadDump(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(getThreadDumpHeader(ti));
    for (LockInfo li : ti.getLockedSynchronizers()) {
        sb.append(INDENT2 + "locks " +
                  li.toString() + CRLF);
    }
    boolean start = true;
    StackTraceElement[] stes = ti.getStackTrace();
    Object[] monitorDepths = new Object[stes.length];
    MonitorInfo[] mis = ti.getLockedMonitors();
    for (int i = 0; i < mis.length; i++) {
        monitorDepths[mis[i].getLockedStackDepth()] = mis[i];
    }
    for (int i = 0; i < stes.length; i++) {
        StackTraceElement ste = stes[i];
        sb.append(INDENT2 +
                  "at " + ste.toString() + CRLF);
        if (start) {
            if (ti.getLockName() != null) {
                sb.append(INDENT2 + "- waiting on (a " +
                          ti.getLockName() + ")");
                if (ti.getLockOwnerName() != null) {
                    sb.append(" owned by " + ti.getLockOwnerName() +
                              " Id=" + ti.getLockOwnerId());
                }
                sb.append(CRLF);
            }
            start = false;
        }
        if (monitorDepths[i] != null) {
            MonitorInfo mi = (MonitorInfo)monitorDepths[i];
            sb.append(INDENT2 +
                      "- locked (a " + mi.toString() + ")"+
                      " index " + mi.getLockedStackDepth() +
                      " frame " + mi.getLockedStackFrame().toString());
            sb.append(CRLF);

        }
    }
    return sb.toString();
}