Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/diagnosticFramework.hpp
32285 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP25#define SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP2627#include "classfile/vmSymbols.hpp"28#include "memory/allocation.hpp"29#include "runtime/arguments.hpp"30#include "runtime/os.hpp"31#include "runtime/vm_version.hpp"32#include "runtime/vmThread.hpp"33#include "utilities/ostream.hpp"343536enum DCmdSource {37DCmd_Source_Internal = 0x01U, // invocation from the JVM38DCmd_Source_AttachAPI = 0x02U, // invocation via the attachAPI39DCmd_Source_MBean = 0x04U // invocation via a MBean40};4142// Warning: strings referenced by the JavaPermission struct are passed to43// the native part of the JDK. Avoid use of dynamically allocated strings44// that could be de-allocated before the JDK native code had time to45// convert them into Java Strings.46struct JavaPermission {47const char* _class;48const char* _name;49const char* _action;50};5152// CmdLine is the class used to handle a command line containing a single53// diagnostic command and its arguments. It provides methods to access the54// command name and the beginning of the arguments. The class is also55// able to identify commented command lines and the "stop" keyword56class CmdLine : public StackObj {57private:58const char* _cmd;59size_t _cmd_len;60const char* _args;61size_t _args_len;62public:63CmdLine(const char* line, size_t len, bool no_command_name);64const char* args_addr() const { return _args; }65size_t args_len() const { return _args_len; }66const char* cmd_addr() const { return _cmd; }67size_t cmd_len() const { return _cmd_len; }68bool is_empty() { return _cmd_len == 0; }69bool is_executable() { return is_empty() || _cmd[0] != '#'; }70bool is_stop() { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; }71};7273// Iterator class taking a character string in input and returning a CmdLine74// instance for each command line. The argument delimiter has to be specified.75class DCmdIter : public StackObj {76friend class DCmd;77private:78const char* _str;79char _delim;80size_t _len;81size_t _cursor;82public:8384DCmdIter(const char* str, char delim) {85_str = str;86_delim = delim;87_len = strlen(str);88_cursor = 0;89}90bool has_next() { return _cursor < _len; }91CmdLine next() {92assert(_cursor <= _len, "Cannot iterate more");93size_t n = _cursor;94while (n < _len && _str[n] != _delim) n++;95CmdLine line(&(_str[_cursor]), n - _cursor, false);96_cursor = n + 1;97// The default copy constructor of CmdLine is used to return a CmdLine98// instance to the caller.99return line;100}101};102103// Iterator class to iterate over diagnostic command arguments104class DCmdArgIter : public ResourceObj {105const char* _buffer;106size_t _len;107size_t _cursor;108const char* _key_addr;109size_t _key_len;110const char* _value_addr;111size_t _value_len;112char _delim;113public:114DCmdArgIter(const char* buf, size_t len, char delim) {115_buffer = buf;116_len = len;117_delim = delim;118_cursor = 0;119}120bool next(TRAPS);121const char* key_addr() { return _key_addr; }122size_t key_length() { return _key_len; }123const char* value_addr() { return _value_addr; }124size_t value_length() { return _value_len; }125};126127// A DCmdInfo instance provides a description of a diagnostic command. It is128// used to export the description to the JMX interface of the framework.129class DCmdInfo : public ResourceObj {130protected:131const char* _name; /* Name of the diagnostic command */132const char* _description; /* Short description */133const char* _impact; /* Impact on the JVM */134JavaPermission _permission; /* Java Permission required to execute this command if any */135int _num_arguments; /* Number of supported options or arguments */136bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */137public:138DCmdInfo(const char* name,139const char* description,140const char* impact,141JavaPermission permission,142int num_arguments,143bool enabled) {144this->_name = name;145this->_description = description;146this->_impact = impact;147this->_permission = permission;148this->_num_arguments = num_arguments;149this->_is_enabled = enabled;150}151const char* name() const { return _name; }152const char* description() const { return _description; }153const char* impact() const { return _impact; }154JavaPermission permission() const { return _permission; }155int num_arguments() const { return _num_arguments; }156bool is_enabled() const { return _is_enabled; }157158static bool by_name(void* name, DCmdInfo* info);159};160161// A DCmdArgumentInfo instance provides a description of a diagnostic command162// argument. It is used to export the description to the JMX interface of the163// framework.164class DCmdArgumentInfo : public ResourceObj {165protected:166const char* _name; /* Option/Argument name*/167const char* _description; /* Short description */168const char* _type; /* Type: STRING, BOOLEAN, etc. */169const char* _default_string; /* Default value in a parsable string */170bool _mandatory; /* True if the option/argument is mandatory */171bool _option; /* True if it is an option, false if it is an argument */172/* (see diagnosticFramework.hpp for option/argument definitions) */173bool _multiple; /* True is the option can be specified several time */174int _position; /* Expected position for this argument (this field is */175/* meaningless for options) */176public:177DCmdArgumentInfo(const char* name, const char* description, const char* type,178const char* default_string, bool mandatory, bool option,179bool multiple) {180this->_name = name;181this->_description = description;182this->_type = type;183this->_default_string = default_string;184this->_option = option;185this->_mandatory = mandatory;186this->_option = option;187this->_multiple = multiple;188this->_position = -1;189}190DCmdArgumentInfo(const char* name, const char* description, const char* type,191const char* default_string, bool mandatory, bool option,192bool multiple, int position) {193this->_name = name;194this->_description = description;195this->_type = type;196this->_default_string = default_string;197this->_option = option;198this->_mandatory = mandatory;199this->_option = option;200this->_multiple = multiple;201this->_position = position;202}203const char* name() const { return _name; }204const char* description() const { return _description; }205const char* type() const { return _type; }206const char* default_string() const { return _default_string; }207bool is_mandatory() const { return _mandatory; }208bool is_option() const { return _option; }209bool is_multiple() const { return _multiple; }210int position() const { return _position; }211};212213// The DCmdParser class can be used to create an argument parser for a214// diagnostic command. It is not mandatory to use it to parse arguments.215// The DCmdParser parses a CmdLine instance according to the parameters that216// have been declared by its associated diagnostic command. A parameter can217// either be an option or an argument. Options are identified by the option name218// while arguments are identified by their position in the command line. The219// position of an argument is defined relative to all arguments passed on the220// command line, options are not considered when defining an argument position.221// The generic syntax of a diagnostic command is:222//223// <command name> [<option>=<value>] [<argument_value>]224//225// Example:226//227// command_name option1=value1 option2=value argumentA argumentB argumentC228//229// In this command line, the diagnostic command receives five parameters, two230// options named option1 and option2, and three arguments. argumentA's position231// is 0, argumentB's position is 1 and argumentC's position is 2.232class DCmdParser {233private:234GenDCmdArgument* _options;235GenDCmdArgument* _arguments_list;236char _delim;237public:238DCmdParser() {239_options = NULL;240_arguments_list = NULL;241_delim = ' ';242}243void add_dcmd_option(GenDCmdArgument* arg);244void add_dcmd_argument(GenDCmdArgument* arg);245GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);246GenDCmdArgument* arguments_list() { return _arguments_list; };247void check(TRAPS);248void parse(CmdLine* line, char delim, TRAPS);249void print_help(outputStream* out, const char* cmd_name);250void reset(TRAPS);251void cleanup();252int num_arguments();253GrowableArray<const char*>* argument_name_array();254GrowableArray<DCmdArgumentInfo*>* argument_info_array();255};256257// The DCmd class is the parent class of all diagnostic commands258// Diagnostic command instances should not be instantiated directly but259// created using the associated factory. The factory can be retrieved with260// the DCmdFactory::getFactory() method.261// A diagnostic command instance can either be allocated in the resource Area262// or in the C-heap. Allocation in the resource area is recommended when the263// current thread is the only one which will access the diagnostic command264// instance. Allocation in the C-heap is required when the diagnostic command265// is accessed by several threads (for instance to perform asynchronous266// execution).267// To ensure a proper cleanup, it's highly recommended to use a DCmdMark for268// each diagnostic command instance. In case of a C-heap allocated diagnostic269// command instance, the DCmdMark must be created in the context of the last270// thread that will access the instance.271class DCmd : public ResourceObj {272protected:273outputStream* _output;274bool _is_heap_allocated;275public:276DCmd(outputStream* output, bool heap_allocated) {277_output = output;278_is_heap_allocated = heap_allocated;279}280281static const char* name() { return "No Name";}282static const char* description() { return "No Help";}283static const char* disabled_message() { return "Diagnostic command currently disabled"; }284// The impact() method returns a description of the intrusiveness of the diagnostic285// command on the Java Virtual Machine behavior. The rational for this method is that some286// diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine287// (for instance a Thread Dump for an application with several tens of thousands of threads,288// or a Head Dump with a 40GB+ heap size) and other diagnostic commands have no serious289// impact on the JVM (for instance, getting the command line arguments or the JVM version).290// The recommended format for the description is <impact level>: [longer description],291// where the impact level is selected among this list: {Low, Medium, High}. The optional292// longer description can provide more specific details like the fact that Thread Dump293// impact depends on the heap size.294static const char* impact() { return "Low: No impact"; }295// The permission() method returns the description of Java Permission. This296// permission is required when the diagnostic command is invoked via the297// DiagnosticCommandMBean. The rationale for this permission check is that298// the DiagnosticCommandMBean can be used to perform remote invocations of299// diagnostic commands through the PlatformMBeanServer. The (optional) Java300// Permission associated with each diagnostic command should ease the work301// of system administrators to write policy files granting permissions to302// execute diagnostic commands to remote users. Any diagnostic command with303// a potential impact on security should overwrite this method.304static const JavaPermission permission() {305JavaPermission p = {NULL, NULL, NULL};306return p;307}308static int num_arguments() { return 0; }309outputStream* output() { return _output; }310bool is_heap_allocated() { return _is_heap_allocated; }311virtual void print_help(const char* name) {312output()->print_cr("Syntax: %s", name);313}314virtual void parse(CmdLine* line, char delim, TRAPS) {315DCmdArgIter iter(line->args_addr(), line->args_len(), delim);316bool has_arg = iter.next(CHECK);317if (has_arg) {318THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),319"The argument list of this diagnostic command should be empty.");320}321}322virtual void execute(DCmdSource source, TRAPS) { }323virtual void reset(TRAPS) { }324virtual void cleanup() { }325326// support for the JMX interface327virtual GrowableArray<const char*>* argument_name_array() {328GrowableArray<const char*>* array = new GrowableArray<const char*>(0);329return array;330}331virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() {332GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);333return array;334}335336// main method to invoke the framework337static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline,338char delim, TRAPS);339};340341class DCmdWithParser : public DCmd {342protected:343DCmdParser _dcmdparser;344public:345DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }346static const char* name() { return "No Name";}347static const char* description() { return "No Help";}348static const char* disabled_message() { return "Diagnostic command currently disabled"; }349static const char* impact() { return "Low: No impact"; }350static const JavaPermission permission() {JavaPermission p = {NULL, NULL, NULL}; return p; }351static int num_arguments() { return 0; }352virtual void parse(CmdLine *line, char delim, TRAPS);353virtual void execute(DCmdSource source, TRAPS) { }354virtual void reset(TRAPS);355virtual void cleanup();356virtual void print_help(const char* name);357virtual GrowableArray<const char*>* argument_name_array();358virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array();359};360361class DCmdMark : public StackObj {362DCmd* _ref;363public:364DCmdMark(DCmd* cmd) { _ref = cmd; }365~DCmdMark() {366if (_ref != NULL) {367_ref->cleanup();368if (_ref->is_heap_allocated()) {369delete _ref;370}371}372}373};374375// Diagnostic commands are not directly instantiated but created with a factory.376// Each diagnostic command class has its own factory. The DCmdFactory class also377// manages the status of the diagnostic command (hidden, enabled). A DCmdFactory378// has to be registered to make the diagnostic command available (see379// management.cpp)380class DCmdFactory: public CHeapObj<mtInternal> {381private:382static Mutex* _dcmdFactory_lock;383static bool _send_jmx_notification;384static bool _has_pending_jmx_notification;385// Pointer to the next factory in the singly-linked list of registered386// diagnostic commands387DCmdFactory* _next;388// When disabled, a diagnostic command cannot be executed. Any attempt to389// execute it will result in the printing of the disabled message without390// instantiating the command.391bool _enabled;392// When hidden, a diagnostic command doesn't appear in the list of commands393// provided by the 'help' command.394bool _hidden;395uint32_t _export_flags;396int _num_arguments;397static DCmdFactory* _DCmdFactoryList;398public:399DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden) {400_next = NULL;401_enabled = enabled;402_hidden = hidden;403_export_flags = flags;404_num_arguments = num_arguments;405}406bool is_enabled() const { return _enabled; }407void set_enabled(bool b) { _enabled = b; }408bool is_hidden() const { return _hidden; }409void set_hidden(bool b) { _hidden = b; }410uint32_t export_flags() { return _export_flags; }411void set_export_flags(uint32_t f) { _export_flags = f; }412int num_arguments() { return _num_arguments; }413DCmdFactory* next() { return _next; }414virtual DCmd* create_Cheap_instance(outputStream* output) = 0;415virtual DCmd* create_resource_instance(outputStream* output) = 0;416virtual const char* name() const = 0;417virtual const char* description() const = 0;418virtual const char* impact() const = 0;419virtual const JavaPermission permission() const = 0;420virtual const char* disabled_message() const = 0;421// Register a DCmdFactory to make a diagnostic command available.422// Once registered, a diagnostic command must not be unregistered.423// To prevent a diagnostic command from being executed, just set the424// enabled flag to false.425static int register_DCmdFactory(DCmdFactory* factory);426static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len);427// Returns a C-heap allocated diagnostic command for the given command line428static DCmd* create_global_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS);429// Returns a resourceArea allocated diagnostic command for the given command line430static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS);431static GrowableArray<const char*>* DCmd_list(DCmdSource source);432static GrowableArray<DCmdInfo*>* DCmdInfo_list(DCmdSource source);433434static void set_jmx_notification_enabled(bool enabled) {435_send_jmx_notification = enabled;436}437static void push_jmx_notification_request();438static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; }439static void send_notification(TRAPS);440private:441static void send_notification_internal(TRAPS);442443friend class HelpDCmd;444};445446// Template to easily create DCmdFactory instances. See management.cpp447// where this template is used to create and register factories.448template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {449public:450DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) :451DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { }452// Returns a C-heap allocated instance453virtual DCmd* create_Cheap_instance(outputStream* output) {454return new (ResourceObj::C_HEAP, mtInternal) DCmdClass(output, true);455}456// Returns a resourceArea allocated instance457virtual DCmd* create_resource_instance(outputStream* output) {458return new DCmdClass(output, false);459}460virtual const char* name() const {461return DCmdClass::name();462}463virtual const char* description() const {464return DCmdClass::description();465}466virtual const char* impact() const {467return DCmdClass::impact();468}469virtual const JavaPermission permission() const {470return DCmdClass::permission();471}472virtual const char* disabled_message() const {473return DCmdClass::disabled_message();474}475};476477// This class provides a convenient way to register Dcmds, without a need to change478// management.cpp every time. Body of these two methods resides in479// diagnosticCommand.cpp480481class DCmdRegistrant : public AllStatic {482483private:484static void register_dcmds();485static void register_dcmds_ext();486487friend class Management;488};489490#endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP491492493