Path: blob/master/src/hotspot/share/services/diagnosticFramework.hpp
41144 views
/*1* Copyright (c) 2011, 2021, 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_SERVICES_DIAGNOSTICFRAMEWORK_HPP25#define SHARE_SERVICES_DIAGNOSTICFRAMEWORK_HPP2627#include "classfile/vmSymbols.hpp"28#include "memory/allocation.hpp"29#include "memory/resourceArea.hpp"30#include "runtime/os.hpp"31#include "runtime/vmThread.hpp"32#include "utilities/ostream.hpp"33#include <type_traits>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() const { return _cmd_len == 0; }69bool is_executable() const { return is_empty() || _cmd[0] != '#'; }70bool is_stop() const { 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* const _str;79const char _delim;80const size_t _len;81size_t _cursor;82public:8384DCmdIter(const char* str, char delim)85: _str(str), _delim(delim), _len(::strlen(str)),86_cursor(0) {}87bool has_next() const { return _cursor < _len; }88CmdLine next() {89assert(_cursor <= _len, "Cannot iterate more");90size_t n = _cursor;91while (n < _len && _str[n] != _delim) n++;92CmdLine line(&(_str[_cursor]), n - _cursor, false);93_cursor = n + 1;94// The default copy constructor of CmdLine is used to return a CmdLine95// instance to the caller.96return line;97}98};99100// Iterator class to iterate over diagnostic command arguments101class DCmdArgIter : public ResourceObj {102const char* const _buffer;103const size_t _len;104size_t _cursor;105const char* _key_addr;106size_t _key_len;107const char* _value_addr;108size_t _value_len;109const char _delim;110public:111DCmdArgIter(const char* buf, size_t len, char delim)112: _buffer(buf), _len(len), _cursor(0), _key_addr(NULL),113_key_len(0), _value_addr(NULL), _value_len(0), _delim(delim) {}114115bool next(TRAPS);116const char* key_addr() const { return _key_addr; }117size_t key_length() const { return _key_len; }118const char* value_addr() const { return _value_addr; }119size_t value_length() const { return _value_len; }120};121122// A DCmdInfo instance provides a description of a diagnostic command. It is123// used to export the description to the JMX interface of the framework.124class DCmdInfo : public ResourceObj {125protected:126const char* const _name; /* Name of the diagnostic command */127const char* const _description; /* Short description */128const char* const _impact; /* Impact on the JVM */129const JavaPermission _permission; /* Java Permission required to execute this command if any */130const int _num_arguments; /* Number of supported options or arguments */131const bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */132public:133DCmdInfo(const char* name,134const char* description,135const char* impact,136JavaPermission permission,137int num_arguments,138bool enabled)139: _name(name), _description(description), _impact(impact), _permission(permission),140_num_arguments(num_arguments), _is_enabled(enabled) {}141const char* name() const { return _name; }142const char* description() const { return _description; }143const char* impact() const { return _impact; }144const JavaPermission& permission() const { return _permission; }145int num_arguments() const { return _num_arguments; }146bool is_enabled() const { return _is_enabled; }147148static bool by_name(void* name, DCmdInfo* info);149};150151// A DCmdArgumentInfo instance provides a description of a diagnostic command152// argument. It is used to export the description to the JMX interface of the153// framework.154class DCmdArgumentInfo : public ResourceObj {155protected:156const char* const _name; /* Option/Argument name*/157const char* const _description; /* Short description */158const char* const _type; /* Type: STRING, BOOLEAN, etc. */159const char* const _default_string; /* Default value in a parsable string */160const bool _mandatory; /* True if the option/argument is mandatory */161const bool _option; /* True if it is an option, false if it is an argument */162/* (see diagnosticFramework.hpp for option/argument definitions) */163const bool _multiple; /* True is the option can be specified several time */164const int _position; /* Expected position for this argument (this field is */165/* meaningless for options) */166public:167DCmdArgumentInfo(const char* name, const char* description, const char* type,168const char* default_string, bool mandatory, bool option,169bool multiple, int position = -1)170: _name(name), _description(description), _type(type),171_default_string(default_string), _mandatory(mandatory), _option(option),172_multiple(multiple), _position(position) {}173174const char* name() const { return _name; }175const char* description() const { return _description; }176const char* type() const { return _type; }177const char* default_string() const { return _default_string; }178bool is_mandatory() const { return _mandatory; }179bool is_option() const { return _option; }180bool is_multiple() const { return _multiple; }181int position() const { return _position; }182};183184// The DCmdParser class can be used to create an argument parser for a185// diagnostic command. It is not mandatory to use it to parse arguments.186// The DCmdParser parses a CmdLine instance according to the parameters that187// have been declared by its associated diagnostic command. A parameter can188// either be an option or an argument. Options are identified by the option name189// while arguments are identified by their position in the command line. The190// position of an argument is defined relative to all arguments passed on the191// command line, options are not considered when defining an argument position.192// The generic syntax of a diagnostic command is:193//194// <command name> [<option>=<value>] [<argument_value>]195//196// Example:197//198// command_name option1=value1 option2=value argumentA argumentB argumentC199//200// In this command line, the diagnostic command receives five parameters, two201// options named option1 and option2, and three arguments. argumentA's position202// is 0, argumentB's position is 1 and argumentC's position is 2.203class DCmdParser {204private:205GenDCmdArgument* _options;206GenDCmdArgument* _arguments_list;207public:208DCmdParser()209: _options(NULL), _arguments_list(NULL) {}210void add_dcmd_option(GenDCmdArgument* arg);211void add_dcmd_argument(GenDCmdArgument* arg);212GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);213GenDCmdArgument* arguments_list() const { return _arguments_list; };214void check(TRAPS);215void parse(CmdLine* line, char delim, TRAPS);216void print_help(outputStream* out, const char* cmd_name) const;217void reset(TRAPS);218void cleanup();219int num_arguments() const;220GrowableArray<const char*>* argument_name_array() const;221GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;222};223224// The DCmd class is the parent class of all diagnostic commands225// Diagnostic command instances should not be instantiated directly but226// created using the associated factory. The factory can be retrieved with227// the DCmdFactory::getFactory() method.228// A diagnostic command instance can either be allocated in the resource Area229// or in the C-heap. Allocation in the resource area is recommended when the230// current thread is the only one which will access the diagnostic command231// instance. Allocation in the C-heap is required when the diagnostic command232// is accessed by several threads (for instance to perform asynchronous233// execution).234// To ensure a proper cleanup, it's highly recommended to use a DCmdMark for235// each diagnostic command instance. In case of a C-heap allocated diagnostic236// command instance, the DCmdMark must be created in the context of the last237// thread that will access the instance.238class DCmd : public ResourceObj {239protected:240outputStream* const _output;241const bool _is_heap_allocated;242public:243DCmd(outputStream* output, bool heap_allocated)244: _output(output), _is_heap_allocated(heap_allocated) {}245246// Child classes: please always provide these methods:247// static const char* name() { return "<command name>";}248// static const char* description() { return "<command help>";}249250static const char* disabled_message() { return "Diagnostic command currently disabled"; }251252// The impact() method returns a description of the intrusiveness of the diagnostic253// command on the Java Virtual Machine behavior. The rational for this method is that some254// diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine255// (for instance a Thread Dump for an application with several tens of thousands of threads,256// or a Head Dump with a 40GB+ heap size) and other diagnostic commands have no serious257// impact on the JVM (for instance, getting the command line arguments or the JVM version).258// The recommended format for the description is <impact level>: [longer description],259// where the impact level is selected among this list: {Low, Medium, High}. The optional260// longer description can provide more specific details like the fact that Thread Dump261// impact depends on the heap size.262static const char* impact() { return "Low: No impact"; }263264// The permission() method returns the description of Java Permission. This265// permission is required when the diagnostic command is invoked via the266// DiagnosticCommandMBean. The rationale for this permission check is that267// the DiagnosticCommandMBean can be used to perform remote invocations of268// diagnostic commands through the PlatformMBeanServer. The (optional) Java269// Permission associated with each diagnostic command should ease the work270// of system administrators to write policy files granting permissions to271// execute diagnostic commands to remote users. Any diagnostic command with272// a potential impact on security should overwrite this method.273static const JavaPermission permission() {274JavaPermission p = {NULL, NULL, NULL};275return p;276}277// num_arguments() is used by the DCmdFactoryImpl::get_num_arguments() template functions.278// - For subclasses of DCmdWithParser, it's calculated by DCmdParser::num_arguments().279// - Other subclasses of DCmd have zero arguments by default. You can change this280// by defining your own version of MyDCmd::num_arguments().281static int num_arguments() { return 0; }282outputStream* output() const { return _output; }283bool is_heap_allocated() const { return _is_heap_allocated; }284virtual void print_help(const char* name) const {285output()->print_cr("Syntax: %s", name);286}287virtual void parse(CmdLine* line, char delim, TRAPS) {288DCmdArgIter iter(line->args_addr(), line->args_len(), delim);289bool has_arg = iter.next(CHECK);290if (has_arg) {291THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),292"The argument list of this diagnostic command should be empty.");293}294}295virtual void execute(DCmdSource source, TRAPS) { }296virtual void reset(TRAPS) { }297virtual void cleanup() { }298299// support for the JMX interface300virtual GrowableArray<const char*>* argument_name_array() const {301GrowableArray<const char*>* array = new GrowableArray<const char*>(0);302return array;303}304virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const {305GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);306return array;307}308309// main method to invoke the framework310static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline,311char delim, TRAPS);312};313314class DCmdWithParser : public DCmd {315protected:316DCmdParser _dcmdparser;317public:318DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }319static const char* disabled_message() { return "Diagnostic command currently disabled"; }320static const char* impact() { return "Low: No impact"; }321virtual void parse(CmdLine *line, char delim, TRAPS);322virtual void execute(DCmdSource source, TRAPS) { }323virtual void reset(TRAPS);324virtual void cleanup();325virtual void print_help(const char* name) const;326virtual GrowableArray<const char*>* argument_name_array() const;327virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;328DCmdParser* dcmdparser() {329return &_dcmdparser;330}331};332333class DCmdMark : public StackObj {334DCmd* const _ref;335public:336DCmdMark(DCmd* cmd) : _ref(cmd) {}337~DCmdMark() {338if (_ref != NULL) {339_ref->cleanup();340if (_ref->is_heap_allocated()) {341delete _ref;342}343}344}345};346347// Diagnostic commands are not directly instantiated but created with a factory.348// Each diagnostic command class has its own factory. The DCmdFactory class also349// manages the status of the diagnostic command (hidden, enabled). A DCmdFactory350// has to be registered to make the diagnostic command available (see351// management.cpp)352class DCmdFactory: public CHeapObj<mtInternal> {353private:354static bool _send_jmx_notification;355static bool _has_pending_jmx_notification;356static DCmdFactory* _DCmdFactoryList;357358// Pointer to the next factory in the singly-linked list of registered359// diagnostic commands360DCmdFactory* _next;361// When disabled, a diagnostic command cannot be executed. Any attempt to362// execute it will result in the printing of the disabled message without363// instantiating the command.364const bool _enabled;365// When hidden, a diagnostic command doesn't appear in the list of commands366// provided by the 'help' command.367const bool _hidden;368const uint32_t _export_flags;369const int _num_arguments;370371public:372DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden)373: _next(NULL), _enabled(enabled), _hidden(hidden),374_export_flags(flags), _num_arguments(num_arguments) {}375bool is_enabled() const { return _enabled; }376bool is_hidden() const { return _hidden; }377uint32_t export_flags() const { return _export_flags; }378int num_arguments() const { return _num_arguments; }379DCmdFactory* next() const { return _next; }380virtual DCmd* create_resource_instance(outputStream* output) const = 0;381virtual const char* name() const = 0;382virtual const char* description() const = 0;383virtual const char* impact() const = 0;384virtual const JavaPermission permission() const = 0;385virtual const char* disabled_message() const = 0;386// Register a DCmdFactory to make a diagnostic command available.387// Once registered, a diagnostic command must not be unregistered.388// To prevent a diagnostic command from being executed, just set the389// enabled flag to false.390static int register_DCmdFactory(DCmdFactory* factory);391static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len);392// Returns a resourceArea allocated diagnostic command for the given command line393static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS);394static GrowableArray<const char*>* DCmd_list(DCmdSource source);395static GrowableArray<DCmdInfo*>* DCmdInfo_list(DCmdSource source);396397static void set_jmx_notification_enabled(bool enabled) {398_send_jmx_notification = enabled;399}400static void push_jmx_notification_request();401static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; }402static void send_notification(TRAPS);403private:404static void send_notification_internal(TRAPS);405406friend class HelpDCmd;407};408409// Template to easily create DCmdFactory instances. See management.cpp410// where this template is used to create and register factories.411template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {412public:413DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) :414DCmdFactory(get_num_arguments<DCmdClass>(), flags, enabled, hidden) { }415// Returns a resourceArea allocated instance416DCmd* create_resource_instance(outputStream* output) const {417return new DCmdClass(output, false);418}419const char* name() const {420return DCmdClass::name();421}422const char* description() const {423return DCmdClass::description();424}425const char* impact() const {426return DCmdClass::impact();427}428const JavaPermission permission() const {429return DCmdClass::permission();430}431const char* disabled_message() const {432return DCmdClass::disabled_message();433}434435private:436template <typename T, ENABLE_IF(!std::is_base_of<DCmdWithParser, T>::value)>437static int get_num_arguments() {438return T::num_arguments();439}440441template <typename T, ENABLE_IF(std::is_base_of<DCmdWithParser, T>::value)>442static int get_num_arguments() {443ResourceMark rm;444DCmdClass* dcmd = new DCmdClass(NULL, false);445if (dcmd != NULL) {446DCmdMark mark(dcmd);447return dcmd->dcmdparser()->num_arguments();448} else {449return 0;450}451}452};453454// This class provides a convenient way to register Dcmds, without a need to change455// management.cpp every time. Body of these two methods resides in456// diagnosticCommand.cpp457458class DCmdRegistrant : public AllStatic {459460private:461static void register_dcmds();462static void register_dcmds_ext();463464friend class Management;465};466467#endif // SHARE_SERVICES_DIAGNOSTICFRAMEWORK_HPP468469470