0N/A/*
3261N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#ifndef SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
0N/A#define SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
0N/A
0N/A#include "classfile/vmSymbols.hpp"
0N/A#include "memory/allocation.hpp"
0N/A#include "runtime/arguments.hpp"
2153N/A#include "runtime/os.hpp"
2153N/A#include "runtime/vm_version.hpp"
0N/A#include "runtime/vmThread.hpp"
0N/A#include "utilities/ostream.hpp"
0N/A
0N/A
0N/A// CmdLine is the class used to handle a command line containing a single
0N/A// diagnostic command and its arguments. It provides methods to access the
0N/A// command name and the beginning of the arguments. The class is also
0N/A// able to identify commented command lines and the "stop" keyword
0N/Aclass CmdLine : public StackObj {
0N/Aprivate:
0N/A const char* _cmd;
0N/A size_t _cmd_len;
0N/A const char* _args;
0N/A size_t _args_len;
0N/Apublic:
0N/A CmdLine(const char* line, size_t len, bool no_command_name);
0N/A const char* args_addr() const { return _args; }
0N/A size_t args_len() const { return _args_len; }
0N/A const char* cmd_addr() const { return _cmd; }
0N/A size_t cmd_len() const { return _cmd_len; }
0N/A bool is_empty() { return _cmd_len == 0; }
0N/A bool is_executable() { return is_empty() || _cmd[0] != '#'; }
0N/A bool is_stop() { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; }
0N/A};
0N/A
0N/A// Iterator class taking a character string in input and returning a CmdLine
0N/A// instance for each command line. The argument delimiter has to be specified.
0N/Aclass DCmdIter : public StackObj {
0N/A friend class DCmd;
0N/Aprivate:
0N/A const char* _str;
0N/A char _delim;
0N/A size_t _len;
0N/A size_t _cursor;
0N/Apublic:
0N/A
0N/A DCmdIter(const char* str, char delim) {
0N/A _str = str;
0N/A _delim = delim;
0N/A _len = strlen(str);
0N/A _cursor = 0;
0N/A }
0N/A bool has_next() { return _cursor < _len; }
0N/A CmdLine next() {
0N/A assert(_cursor <= _len, "Cannot iterate more");
0N/A size_t n = _cursor;
0N/A while (n < _len && _str[n] != _delim) n++;
0N/A CmdLine line(&(_str[_cursor]), n - _cursor, false);
0N/A _cursor = n + 1;
0N/A // The default copy constructor of CmdLine is used to return a CmdLine
0N/A // instance to the caller.
0N/A return line;
0N/A }
0N/A};
0N/A
0N/A// Iterator class to iterate over diagnostic command arguments
0N/Aclass DCmdArgIter : public ResourceObj {
0N/A const char* _buffer;
0N/A size_t _len;
0N/A size_t _cursor;
0N/A const char* _key_addr;
0N/A size_t _key_len;
0N/A const char* _value_addr;
0N/A size_t _value_len;
0N/A char _delim;
2153N/Apublic:
2153N/A DCmdArgIter(const char* buf, size_t len, char delim) {
2153N/A _buffer = buf;
2153N/A _len = len;
3076N/A _delim = delim;
2153N/A _cursor = 0;
2153N/A }
2153N/A bool next(TRAPS);
2153N/A const char* key_addr() { return _key_addr; }
2153N/A size_t key_length() { return _key_len; }
2153N/A const char* value_addr() { return _value_addr; }
2153N/A size_t value_length() { return _value_len; }
2153N/A};
2153N/A
2153N/A// A DCmdInfo instance provides a description of a diagnostic command. It is
2153N/A// used to export the description to the JMX interface of the framework.
2153N/Aclass DCmdInfo : public ResourceObj {
2153N/Aprotected:
2153N/A const char* _name;
0N/A const char* _description;
0N/A const char* _impact;
0N/A int _num_arguments;
0N/A bool _is_enabled;
0N/Apublic:
0N/A DCmdInfo(const char* name,
0N/A const char* description,
0N/A const char* impact,
0N/A int num_arguments,
0N/A bool enabled) {
0N/A this->_name = name;
0N/A this->_description = description;
0N/A this->_impact = impact;
0N/A this->_num_arguments = num_arguments;
0N/A this->_is_enabled = enabled;
0N/A }
0N/A const char* name() const { return _name; }
0N/A const char* description() const { return _description; }
0N/A const char* impact() const { return _impact; }
0N/A int num_arguments() const { return _num_arguments; }
0N/A bool is_enabled() const { return _is_enabled; }
0N/A
0N/A static bool by_name(void* name, DCmdInfo* info);
0N/A};
0N/A
0N/A// A DCmdArgumentInfo instance provides a description of a diagnostic command
0N/A// argument. It is used to export the description to the JMX interface of the
0N/A// framework.
0N/Aclass DCmdArgumentInfo : public ResourceObj {
0N/Aprotected:
2153N/A const char* _name;
2153N/A const char* _description;
2153N/A const char* _type;
4787N/A const char* _default_string;
4787N/A bool _mandatory;
2153N/A bool _option;
2153N/A int _position;
2153N/Apublic:
2153N/A DCmdArgumentInfo(const char* name, const char* description, const char* type,
2153N/A const char* default_string, bool mandatory, bool option) {
2153N/A this->_name = name;
2153N/A this->_description = description;
2153N/A this->_type = type;
2153N/A this->_default_string = default_string;
2153N/A this->_option = option;
2153N/A this->_mandatory = mandatory;
2153N/A this->_option = option;
2153N/A this->_position = -1;
2153N/A }
2153N/A DCmdArgumentInfo(const char* name, const char* description, const char* type,
0N/A const char* default_string, bool mandatory, bool option,
0N/A int position) {
0N/A this->_name = name;
0N/A this->_description = description;
0N/A this->_type = type;
0N/A this->_default_string = default_string;
0N/A this->_option = option;
0N/A this->_mandatory = mandatory;
0N/A this->_option = option;
0N/A this->_position = position;
0N/A }
0N/A const char* name() const { return _name; }
0N/A const char* description() const { return _description; }
0N/A const char* type() const { return _type; }
0N/A const char* default_string() const { return _default_string; }
0N/A bool is_mandatory() const { return _mandatory; }
0N/A bool is_option() const { return _option; }
0N/A int position() const { return _position; }
0N/A};
0N/A
0N/A// The DCmdParser class can be used to create an argument parser for a
0N/A// diagnostic command. It is not mandatory to use it to parse arguments.
0N/Aclass DCmdParser {
0N/Aprivate:
0N/A GenDCmdArgument* _options;
0N/A GenDCmdArgument* _arguments_list;
0N/A char _delim;
0N/Apublic:
0N/A DCmdParser() {
0N/A _options = NULL;
0N/A _arguments_list = NULL;
0N/A _delim = ' ';
0N/A }
0N/A void add_dcmd_option(GenDCmdArgument* arg);
0N/A void add_dcmd_argument(GenDCmdArgument* arg);
0N/A GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);
0N/A GenDCmdArgument* arguments_list() { return _arguments_list; };
0N/A void check(TRAPS);
0N/A void parse(CmdLine* line, char delim, TRAPS);
0N/A void print_help(outputStream* out, const char* cmd_name);
0N/A void reset(TRAPS);
0N/A void cleanup();
0N/A int num_arguments();
0N/A GrowableArray<const char*>* argument_name_array();
0N/A GrowableArray<DCmdArgumentInfo*>* argument_info_array();
0N/A};
0N/A
0N/A// The DCmd class is the parent class of all diagnostic commands
0N/A// Diagnostic command instances should not be instantiated directly but
0N/A// created using the associated factory. The factory can be retrieved with
0N/A// the DCmdFactory::getFactory() method.
0N/A// A diagnostic command instance can either be allocated in the resource Area
0N/A// or in the C-heap. Allocation in the resource area is recommended when the
0N/A// current thread is the only one which will access the diagnostic command
0N/A// instance. Allocation in the C-heap is required when the diagnostic command
0N/A// is accessed by several threads (for instance to perform asynchronous
0N/A// execution).
0N/A// To ensure a proper cleanup, it's highly recommended to use a DCmdMark for
0N/A// each diagnostic command instance. In case of a C-heap allocated diagnostic
0N/A// command instance, the DCmdMark must be created in the context of the last
0N/A// thread that will access the instance.
0N/Aclass DCmd : public ResourceObj {
0N/Aprotected:
0N/A outputStream* _output;
0N/A bool _is_heap_allocated;
0N/Apublic:
0N/A DCmd(outputStream* output, bool heap_allocated) {
0N/A _output = output;
0N/A _is_heap_allocated = heap_allocated;
0N/A }
0N/A
0N/A static const char* name() { return "No Name";}
0N/A static const char* description() { return "No Help";}
0N/A static const char* disabled_message() { return "Diagnostic command currently disabled"; }
0N/A // The impact() method returns a description of the intrusiveness of the diagnostic
0N/A // command on the Java Virtual Machine behavior. The rational for this method is that some
0N/A // diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine
0N/A // (for instance a Thread Dump for an application with several tens of thousands of threads,
0N/A // or a Head Dump with a 40GB+ heap size) and other diagnostic commands have no serious
0N/A // impact on the JVM (for instance, getting the command line arguments or the JVM version).
0N/A // The recommended format for the description is <impact level>: [longer description],
0N/A // where the impact level is selected among this list: {Low, Medium, High}. The optional
0N/A // longer description can provide more specific details like the fact that Thread Dump
0N/A // impact depends on the heap size.
0N/A static const char* impact() { return "Low: No impact"; }
0N/A static int num_arguments() { return 0; }
0N/A outputStream* output() { return _output; }
0N/A bool is_heap_allocated() { return _is_heap_allocated; }
0N/A virtual void print_help(const char* name) {
0N/A output()->print_cr("Syntax: %s", name);
0N/A }
0N/A virtual void parse(CmdLine* line, char delim, TRAPS) {
0N/A DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
0N/A bool has_arg = iter.next(CHECK);
0N/A if (has_arg) {
0N/A THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
0N/A "The argument list of this diagnostic command should be empty.");
0N/A }
0N/A }
0N/A virtual void execute(TRAPS) { }
0N/A virtual void reset(TRAPS) { }
0N/A virtual void cleanup() { }
0N/A
0N/A // support for the JMX interface
0N/A virtual GrowableArray<const char*>* argument_name_array() {
0N/A GrowableArray<const char*>* array = new GrowableArray<const char*>(0);
0N/A return array;
0N/A }
0N/A virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() {
0N/A GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);
0N/A return array;
0N/A }
0N/A
0N/A // main method to invoke the framework
0N/A static void parse_and_execute(outputStream* out, const char* cmdline,
0N/A char delim, TRAPS);
0N/A};
0N/A
0N/Aclass DCmdWithParser : public DCmd {
0N/Aprotected:
0N/A DCmdParser _dcmdparser;
0N/Apublic:
0N/A DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }
0N/A static const char* name() { return "No Name";}
0N/A static const char* description() { return "No Help";}
0N/A static const char* disabled_message() { return "Diagnostic command currently disabled"; }
0N/A static const char* impact() { return "Low: No impact"; }
0N/A static int num_arguments() { return 0; }
0N/A virtual void parse(CmdLine *line, char delim, TRAPS);
0N/A virtual void execute(TRAPS) { }
0N/A virtual void reset(TRAPS);
0N/A virtual void cleanup();
0N/A virtual void print_help(const char* name);
0N/A virtual GrowableArray<const char*>* argument_name_array();
0N/A virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array();
0N/A};
0N/A
0N/Aclass DCmdMark : public StackObj {
0N/A DCmd* _ref;
0N/Apublic:
0N/A DCmdMark(DCmd* cmd) { _ref = cmd; }
0N/A ~DCmdMark() {
0N/A if (_ref != NULL) {
0N/A _ref->cleanup();
0N/A if (_ref->is_heap_allocated()) {
0N/A delete _ref;
0N/A }
0N/A }
0N/A }
0N/A};
0N/A
0N/A// Diagnostic commands are not directly instantiated but created with a factory.
0N/A// Each diagnostic command class has its own factory. The DCmdFactory class also
0N/A// manages the status of the diagnostic command (hidden, enabled). A DCmdFactory
0N/A// has to be registered to make the diagnostic command available (see
0N/A// management.cpp)
0N/Aclass DCmdFactory: public CHeapObj<mtInternal> {
0N/Aprivate:
0N/A static Mutex* _dcmdFactory_lock;
0N/A // Pointer to the next factory in the singly-linked list of registered
0N/A // diagnostic commands
0N/A DCmdFactory* _next;
0N/A // When disabled, a diagnostic command cannot be executed. Any attempt to
0N/A // execute it will result in the printing of the disabled message without
0N/A // instantiating the command.
0N/A bool _enabled;
0N/A // When hidden, a diagnostic command doesn't appear in the list of commands
0N/A // provided by the 'help' command.
0N/A bool _hidden;
0N/A int _num_arguments;
0N/A static DCmdFactory* _DCmdFactoryList;
0N/Apublic:
0N/A DCmdFactory(int num_arguments, bool enabled, bool hidden) {
0N/A _next = NULL;
0N/A _enabled = enabled;
0N/A _hidden = hidden;
0N/A _num_arguments = num_arguments;
0N/A }
0N/A bool is_enabled() const { return _enabled; }
0N/A void set_enabled(bool b) { _enabled = b; }
0N/A bool is_hidden() const { return _hidden; }
0N/A void set_hidden(bool b) { _hidden = b; }
0N/A int num_arguments() { return _num_arguments; }
0N/A DCmdFactory* next() { return _next; }
0N/A virtual DCmd* create_Cheap_instance(outputStream* output) = 0;
0N/A virtual DCmd* create_resource_instance(outputStream* output) = 0;
0N/A virtual const char* name() const = 0;
0N/A virtual const char* description() const = 0;
0N/A virtual const char* impact() const = 0;
0N/A virtual const char* disabled_message() const = 0;
0N/A // Register a DCmdFactory to make a diagnostic command available.
0N/A // Once registered, a diagnostic command must not be unregistered.
0N/A // To prevent a diagnostic command from being executed, just set the
0N/A // enabled flag to false.
0N/A static int register_DCmdFactory(DCmdFactory* factory);
0N/A static DCmdFactory* factory(const char* cmd, size_t len);
0N/A // Returns a C-heap allocated diagnostic command for the given command line
0N/A static DCmd* create_global_DCmd(CmdLine &line, outputStream* out, TRAPS);
0N/A // Returns a resourceArea allocated diagnostic command for the given command line
0N/A static DCmd* create_local_DCmd(CmdLine &line, outputStream* out, TRAPS);
0N/A static GrowableArray<const char*>* DCmd_list();
0N/A static GrowableArray<DCmdInfo*>* DCmdInfo_list();
0N/A
0N/A friend class HelpDCmd;
0N/A};
0N/A
0N/A// Template to easily create DCmdFactory instances. See management.cpp
0N/A// where this template is used to create and register factories.
0N/Atemplate <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {
0N/Apublic:
0N/A DCmdFactoryImpl(bool enabled, bool hidden) :
0N/A DCmdFactory(DCmdClass::num_arguments(), enabled, hidden) { }
0N/A // Returns a C-heap allocated instance
0N/A virtual DCmd* create_Cheap_instance(outputStream* output) {
0N/A return new (ResourceObj::C_HEAP, mtInternal) DCmdClass(output, true);
0N/A }
0N/A // Returns a resourceArea allocated instance
0N/A virtual DCmd* create_resource_instance(outputStream* output) {
0N/A return new DCmdClass(output, false);
0N/A }
0N/A virtual const char* name() const {
0N/A return DCmdClass::name();
0N/A }
0N/A virtual const char* description() const {
0N/A return DCmdClass::description();
0N/A }
0N/A virtual const char* impact() const {
0N/A return DCmdClass::impact();
0N/A }
0N/A virtual const char* disabled_message() const {
0N/A return DCmdClass::disabled_message();
0N/A }
0N/A};
0N/A
0N/A// This class provides a convenient way to register Dcmds, without a need to change
0N/A// management.cpp every time. Body of these two methods resides in
0N/A// diagnosticCommand.cpp
0N/A
0N/Aclass DCmdRegistrant : public AllStatic {
0N/A
0N/Aprivate:
0N/A static void register_dcmds();
0N/A static void register_dcmds_ext();
0N/A
0N/A friend class Management;
0N/A};
0N/A
0N/A#endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
2153N/A