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