001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.monitors; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Map; 022import java.util.TreeMap; 023 024import org.forgerock.opendj.config.server.ConfigException; 025import org.opends.server.api.MonitorData; 026import org.forgerock.opendj.server.config.server.StackTraceMonitorProviderCfg; 027import org.opends.server.api.MonitorProvider; 028import org.opends.server.types.InitializationException; 029 030/** 031 * This class defines a Directory Server monitor provider that can be used to 032 * obtain a stack trace from all server threads that are currently defined in 033 * the JVM. 034 */ 035public class StackTraceMonitorProvider 036 extends MonitorProvider<StackTraceMonitorProviderCfg> 037{ 038 @Override 039 public void initializeMonitorProvider( 040 StackTraceMonitorProviderCfg configuration) 041 throws ConfigException, InitializationException 042 { 043 // No initialization is required. 044 } 045 046 @Override 047 public String getMonitorInstanceName() 048 { 049 return "JVM Stack Trace"; 050 } 051 052 @Override 053 public MonitorData getMonitorData() 054 { 055 Map<Thread,StackTraceElement[]> threadStacks = Thread.getAllStackTraces(); 056 057 // Re-arrange all of the elements by thread ID so that there is some logical order. 058 TreeMap<Long,Map.Entry<Thread,StackTraceElement[]>> orderedStacks = new TreeMap<>(); 059 for (Map.Entry<Thread,StackTraceElement[]> e : threadStacks.entrySet()) 060 { 061 orderedStacks.put(e.getKey().getId(), e); 062 } 063 064 Collection<String> jvmThreads = new ArrayList<>(); 065 for (Map.Entry<Thread,StackTraceElement[]> e : orderedStacks.values()) 066 { 067 Thread t = e.getKey(); 068 StackTraceElement[] stackElements = e.getValue(); 069 070 long tid = t.getId(); 071 jvmThreads.add("id=" + tid + " ---------- " + t.getName() + " ----------"); 072 073 // Create an attribute for the stack trace. 074 if (stackElements != null) 075 { 076 for (int j = 0; j < stackElements.length; j++) 077 { 078 jvmThreads.add(toString(tid, j, stackElements[j])); 079 } 080 } 081 } 082 083 MonitorData result = new MonitorData(1); 084 result.add("jvmThread", jvmThreads); 085 return result; 086 } 087 088 private String toString(long tid, int frame, StackTraceElement stackElement) 089 { 090 StringBuilder buffer = new StringBuilder(); 091 buffer.append("id=").append(tid); 092 buffer.append(" frame[").append(frame).append("]="); 093 094 buffer.append(stackElement.getClassName()); 095 buffer.append("."); 096 buffer.append(stackElement.getMethodName()); 097 buffer.append("("); 098 buffer.append(stackElement.getFileName()); 099 buffer.append(":"); 100 if (stackElement.isNativeMethod()) 101 { 102 buffer.append("native"); 103 } 104 else 105 { 106 buffer.append(stackElement.getLineNumber()); 107 } 108 buffer.append(")"); 109 return buffer.toString(); 110 } 111}