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.types.Attribute;
039import org.opends.server.types.AttributeBuilder;
040import org.opends.server.types.InitializationException;
041
042/**
043 * This class defines a Directory Server monitor provider that can be used to
044 * obtain a stack trace from all server threads that are currently defined in
045 * the JVM.
046 */
047public class StackTraceMonitorProvider
048       extends MonitorProvider<StackTraceMonitorProviderCfg>
049{
050  @Override
051  public void initializeMonitorProvider(
052                   StackTraceMonitorProviderCfg configuration)
053         throws ConfigException, InitializationException
054  {
055    // No initialization is required.
056  }
057
058  @Override
059  public String getMonitorInstanceName()
060  {
061    return "JVM Stack Trace";
062  }
063
064  @Override
065  public List<Attribute> getMonitorData()
066  {
067    Map<Thread,StackTraceElement[]> threadStacks = Thread.getAllStackTraces();
068
069    // Re-arrange all of the elements by thread ID so that there is some logical order.
070    TreeMap<Long,Map.Entry<Thread,StackTraceElement[]>> orderedStacks = new TreeMap<>();
071    for (Map.Entry<Thread,StackTraceElement[]> e : threadStacks.entrySet())
072    {
073      orderedStacks.put(e.getKey().getId(), e);
074    }
075
076    AttributeBuilder builder = new AttributeBuilder("jvmThread");
077    for (Map.Entry<Thread,StackTraceElement[]> e : orderedStacks.values())
078    {
079      Thread t                          = e.getKey();
080      StackTraceElement[] stackElements = e.getValue();
081
082      long id = t.getId();
083      builder.add("id=" + id + " ---------- " + t.getName() + " ----------");
084
085      // Create an attribute for the stack trace.
086      if (stackElements != null)
087      {
088        for (int j=0; j < stackElements.length; j++)
089        {
090          StringBuilder buffer = new StringBuilder();
091          buffer.append("id=");
092          buffer.append(id);
093          buffer.append(" frame[");
094          buffer.append(j);
095          buffer.append("]=");
096
097          buffer.append(stackElements[j].getClassName());
098          buffer.append(".");
099          buffer.append(stackElements[j].getMethodName());
100          buffer.append("(");
101          buffer.append(stackElements[j].getFileName());
102          buffer.append(":");
103          if (stackElements[j].isNativeMethod())
104          {
105            buffer.append("native");
106          }
107          else
108          {
109            buffer.append(stackElements[j].getLineNumber());
110          }
111          buffer.append(")");
112
113          builder.add(buffer.toString());
114        }
115      }
116    }
117
118    return newArrayList(builder.toAttribute());
119  }
120}