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