Wed, 27 Aug 2014 09:36:55 +0200
Merge
fparain@3329 | 1 | /* |
fparain@5047 | 2 | * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
fparain@3329 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
fparain@3329 | 4 | * |
fparain@3329 | 5 | * This code is free software; you can redistribute it and/or modify it |
fparain@3329 | 6 | * under the terms of the GNU General Public License version 2 only, as |
fparain@3329 | 7 | * published by the Free Software Foundation. |
fparain@3329 | 8 | * |
fparain@3329 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
fparain@3329 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
fparain@3329 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
fparain@3329 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
fparain@3329 | 13 | * accompanied this code). |
fparain@3329 | 14 | * |
fparain@3329 | 15 | * You should have received a copy of the GNU General Public License version |
fparain@3329 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
fparain@3329 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
fparain@3329 | 18 | * |
fparain@3329 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
fparain@3329 | 20 | * or visit www.oracle.com if you need additional information or have any |
fparain@3329 | 21 | * questions. |
fparain@3329 | 22 | * |
fparain@3329 | 23 | */ |
fparain@3329 | 24 | |
fparain@3329 | 25 | #ifndef SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP |
fparain@3329 | 26 | #define SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP |
fparain@3329 | 27 | |
fparain@3329 | 28 | #include "classfile/vmSymbols.hpp" |
fparain@3329 | 29 | #include "memory/allocation.hpp" |
fparain@3329 | 30 | #include "runtime/arguments.hpp" |
fparain@3329 | 31 | #include "runtime/os.hpp" |
fparain@3329 | 32 | #include "runtime/vm_version.hpp" |
fparain@3329 | 33 | #include "runtime/vmThread.hpp" |
fparain@3329 | 34 | #include "utilities/ostream.hpp" |
fparain@3329 | 35 | |
fparain@3329 | 36 | |
fparain@5047 | 37 | enum DCmdSource { |
fparain@5047 | 38 | DCmd_Source_Internal = 0x01U, // invocation from the JVM |
fparain@5047 | 39 | DCmd_Source_AttachAPI = 0x02U, // invocation via the attachAPI |
fparain@5047 | 40 | DCmd_Source_MBean = 0x04U // invocation via a MBean |
fparain@5047 | 41 | }; |
fparain@5047 | 42 | |
fparain@5047 | 43 | // Warning: strings referenced by the JavaPermission struct are passed to |
fparain@5047 | 44 | // the native part of the JDK. Avoid use of dynamically allocated strings |
fparain@5047 | 45 | // that could be de-allocated before the JDK native code had time to |
fparain@5047 | 46 | // convert them into Java Strings. |
fparain@5047 | 47 | struct JavaPermission { |
fparain@5047 | 48 | const char* _class; |
fparain@5047 | 49 | const char* _name; |
fparain@5047 | 50 | const char* _action; |
fparain@5047 | 51 | }; |
fparain@5047 | 52 | |
fparain@3329 | 53 | // CmdLine is the class used to handle a command line containing a single |
fparain@3329 | 54 | // diagnostic command and its arguments. It provides methods to access the |
fparain@3329 | 55 | // command name and the beginning of the arguments. The class is also |
fparain@3329 | 56 | // able to identify commented command lines and the "stop" keyword |
fparain@3329 | 57 | class CmdLine : public StackObj { |
fparain@3329 | 58 | private: |
fparain@3329 | 59 | const char* _cmd; |
fparain@3329 | 60 | size_t _cmd_len; |
fparain@3329 | 61 | const char* _args; |
fparain@3329 | 62 | size_t _args_len; |
fparain@3329 | 63 | public: |
fparain@3329 | 64 | CmdLine(const char* line, size_t len, bool no_command_name); |
fparain@3329 | 65 | const char* args_addr() const { return _args; } |
fparain@3329 | 66 | size_t args_len() const { return _args_len; } |
fparain@3329 | 67 | const char* cmd_addr() const { return _cmd; } |
fparain@3329 | 68 | size_t cmd_len() const { return _cmd_len; } |
fparain@3329 | 69 | bool is_empty() { return _cmd_len == 0; } |
fparain@3329 | 70 | bool is_executable() { return is_empty() || _cmd[0] != '#'; } |
fparain@3329 | 71 | bool is_stop() { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; } |
fparain@3329 | 72 | }; |
fparain@3329 | 73 | |
fparain@3329 | 74 | // Iterator class taking a character string in input and returning a CmdLine |
fparain@3329 | 75 | // instance for each command line. The argument delimiter has to be specified. |
fparain@3329 | 76 | class DCmdIter : public StackObj { |
fparain@3329 | 77 | friend class DCmd; |
fparain@3329 | 78 | private: |
fparain@3329 | 79 | const char* _str; |
fparain@3329 | 80 | char _delim; |
fparain@3329 | 81 | size_t _len; |
fparain@3329 | 82 | size_t _cursor; |
fparain@3329 | 83 | public: |
fparain@3329 | 84 | |
fparain@3329 | 85 | DCmdIter(const char* str, char delim) { |
fparain@3329 | 86 | _str = str; |
fparain@3329 | 87 | _delim = delim; |
fparain@3329 | 88 | _len = strlen(str); |
fparain@3329 | 89 | _cursor = 0; |
fparain@3329 | 90 | } |
fparain@3329 | 91 | bool has_next() { return _cursor < _len; } |
fparain@3329 | 92 | CmdLine next() { |
fparain@3329 | 93 | assert(_cursor <= _len, "Cannot iterate more"); |
fparain@3329 | 94 | size_t n = _cursor; |
fparain@3329 | 95 | while (n < _len && _str[n] != _delim) n++; |
fparain@3329 | 96 | CmdLine line(&(_str[_cursor]), n - _cursor, false); |
fparain@3329 | 97 | _cursor = n + 1; |
fparain@3329 | 98 | // The default copy constructor of CmdLine is used to return a CmdLine |
fparain@3329 | 99 | // instance to the caller. |
fparain@3329 | 100 | return line; |
fparain@3329 | 101 | } |
fparain@3329 | 102 | }; |
fparain@3329 | 103 | |
fparain@3329 | 104 | // Iterator class to iterate over diagnostic command arguments |
fparain@3329 | 105 | class DCmdArgIter : public ResourceObj { |
fparain@3329 | 106 | const char* _buffer; |
fparain@3329 | 107 | size_t _len; |
fparain@3329 | 108 | size_t _cursor; |
fparain@3329 | 109 | const char* _key_addr; |
fparain@3329 | 110 | size_t _key_len; |
fparain@3329 | 111 | const char* _value_addr; |
fparain@3329 | 112 | size_t _value_len; |
fparain@3329 | 113 | char _delim; |
fparain@3329 | 114 | public: |
fparain@3329 | 115 | DCmdArgIter(const char* buf, size_t len, char delim) { |
fparain@3329 | 116 | _buffer = buf; |
fparain@3329 | 117 | _len = len; |
fparain@3329 | 118 | _delim = delim; |
fparain@3329 | 119 | _cursor = 0; |
fparain@3329 | 120 | } |
fparain@3329 | 121 | bool next(TRAPS); |
fparain@3329 | 122 | const char* key_addr() { return _key_addr; } |
fparain@3329 | 123 | size_t key_length() { return _key_len; } |
fparain@3329 | 124 | const char* value_addr() { return _value_addr; } |
fparain@3329 | 125 | size_t value_length() { return _value_len; } |
fparain@3329 | 126 | }; |
fparain@3329 | 127 | |
fparain@3329 | 128 | // A DCmdInfo instance provides a description of a diagnostic command. It is |
fparain@3329 | 129 | // used to export the description to the JMX interface of the framework. |
fparain@3329 | 130 | class DCmdInfo : public ResourceObj { |
fparain@3329 | 131 | protected: |
fparain@5047 | 132 | const char* _name; /* Name of the diagnostic command */ |
fparain@5047 | 133 | const char* _description; /* Short description */ |
fparain@5047 | 134 | const char* _impact; /* Impact on the JVM */ |
fparain@5047 | 135 | JavaPermission _permission; /* Java Permission required to execute this command if any */ |
fparain@5047 | 136 | int _num_arguments; /* Number of supported options or arguments */ |
fparain@5047 | 137 | bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */ |
fparain@3329 | 138 | public: |
fparain@3329 | 139 | DCmdInfo(const char* name, |
fparain@3329 | 140 | const char* description, |
fparain@3329 | 141 | const char* impact, |
fparain@5047 | 142 | JavaPermission permission, |
fparain@3329 | 143 | int num_arguments, |
fparain@3329 | 144 | bool enabled) { |
fparain@3329 | 145 | this->_name = name; |
fparain@3329 | 146 | this->_description = description; |
fparain@3329 | 147 | this->_impact = impact; |
fparain@5047 | 148 | this->_permission = permission; |
fparain@3329 | 149 | this->_num_arguments = num_arguments; |
fparain@3329 | 150 | this->_is_enabled = enabled; |
fparain@3329 | 151 | } |
fparain@3329 | 152 | const char* name() const { return _name; } |
fparain@3329 | 153 | const char* description() const { return _description; } |
fparain@3329 | 154 | const char* impact() const { return _impact; } |
fparain@5047 | 155 | JavaPermission permission() const { return _permission; } |
fparain@3329 | 156 | int num_arguments() const { return _num_arguments; } |
fparain@3329 | 157 | bool is_enabled() const { return _is_enabled; } |
fparain@3329 | 158 | |
fparain@3329 | 159 | static bool by_name(void* name, DCmdInfo* info); |
fparain@3329 | 160 | }; |
fparain@3329 | 161 | |
fparain@3329 | 162 | // A DCmdArgumentInfo instance provides a description of a diagnostic command |
fparain@3329 | 163 | // argument. It is used to export the description to the JMX interface of the |
fparain@3329 | 164 | // framework. |
fparain@3329 | 165 | class DCmdArgumentInfo : public ResourceObj { |
fparain@3329 | 166 | protected: |
fparain@5047 | 167 | const char* _name; /* Option/Argument name*/ |
fparain@5047 | 168 | const char* _description; /* Short description */ |
fparain@5047 | 169 | const char* _type; /* Type: STRING, BOOLEAN, etc. */ |
fparain@5047 | 170 | const char* _default_string; /* Default value in a parsable string */ |
fparain@5047 | 171 | bool _mandatory; /* True if the option/argument is mandatory */ |
fparain@5047 | 172 | bool _option; /* True if it is an option, false if it is an argument */ |
fparain@5047 | 173 | /* (see diagnosticFramework.hpp for option/argument definitions) */ |
fparain@5047 | 174 | bool _multiple; /* True is the option can be specified several time */ |
fparain@5047 | 175 | int _position; /* Expected position for this argument (this field is */ |
fparain@5047 | 176 | /* meaningless for options) */ |
fparain@3329 | 177 | public: |
fparain@3329 | 178 | DCmdArgumentInfo(const char* name, const char* description, const char* type, |
fparain@5047 | 179 | const char* default_string, bool mandatory, bool option, |
fparain@5047 | 180 | bool multiple) { |
fparain@3329 | 181 | this->_name = name; |
fparain@3329 | 182 | this->_description = description; |
fparain@3329 | 183 | this->_type = type; |
fparain@3329 | 184 | this->_default_string = default_string; |
fparain@3329 | 185 | this->_option = option; |
fparain@3329 | 186 | this->_mandatory = mandatory; |
fparain@3329 | 187 | this->_option = option; |
fparain@5047 | 188 | this->_multiple = multiple; |
fparain@3329 | 189 | this->_position = -1; |
fparain@3329 | 190 | } |
fparain@3329 | 191 | DCmdArgumentInfo(const char* name, const char* description, const char* type, |
fparain@3329 | 192 | const char* default_string, bool mandatory, bool option, |
fparain@5047 | 193 | bool multiple, int position) { |
fparain@3329 | 194 | this->_name = name; |
fparain@3329 | 195 | this->_description = description; |
fparain@3329 | 196 | this->_type = type; |
fparain@3329 | 197 | this->_default_string = default_string; |
fparain@3329 | 198 | this->_option = option; |
fparain@3329 | 199 | this->_mandatory = mandatory; |
fparain@3329 | 200 | this->_option = option; |
fparain@5047 | 201 | this->_multiple = multiple; |
fparain@3329 | 202 | this->_position = position; |
fparain@3329 | 203 | } |
fparain@3329 | 204 | const char* name() const { return _name; } |
fparain@3329 | 205 | const char* description() const { return _description; } |
fparain@3329 | 206 | const char* type() const { return _type; } |
fparain@3329 | 207 | const char* default_string() const { return _default_string; } |
fparain@3329 | 208 | bool is_mandatory() const { return _mandatory; } |
fparain@3329 | 209 | bool is_option() const { return _option; } |
fparain@5047 | 210 | bool is_multiple() const { return _multiple; } |
fparain@3329 | 211 | int position() const { return _position; } |
fparain@3329 | 212 | }; |
fparain@3329 | 213 | |
fparain@3329 | 214 | // The DCmdParser class can be used to create an argument parser for a |
fparain@3329 | 215 | // diagnostic command. It is not mandatory to use it to parse arguments. |
fparain@5047 | 216 | // The DCmdParser parses a CmdLine instance according to the parameters that |
fparain@5047 | 217 | // have been declared by its associated diagnostic command. A parameter can |
fparain@5047 | 218 | // either be an option or an argument. Options are identified by the option name |
fparain@5047 | 219 | // while arguments are identified by their position in the command line. The |
fparain@5047 | 220 | // position of an argument is defined relative to all arguments passed on the |
fparain@5047 | 221 | // command line, options are not considered when defining an argument position. |
fparain@5047 | 222 | // The generic syntax of a diagnostic command is: |
fparain@5047 | 223 | // |
fparain@5047 | 224 | // <command name> [<option>=<value>] [<argument_value>] |
fparain@5047 | 225 | // |
fparain@5047 | 226 | // Example: |
fparain@5047 | 227 | // |
fparain@5047 | 228 | // command_name option1=value1 option2=value argumentA argumentB argumentC |
fparain@5047 | 229 | // |
fparain@5047 | 230 | // In this command line, the diagnostic command receives five parameters, two |
fparain@5047 | 231 | // options named option1 and option2, and three arguments. argumentA's position |
fparain@5047 | 232 | // is 0, argumentB's position is 1 and argumentC's position is 2. |
fparain@3329 | 233 | class DCmdParser { |
fparain@3329 | 234 | private: |
fparain@3329 | 235 | GenDCmdArgument* _options; |
fparain@3329 | 236 | GenDCmdArgument* _arguments_list; |
fparain@3329 | 237 | char _delim; |
fparain@3329 | 238 | public: |
fparain@3329 | 239 | DCmdParser() { |
fparain@3329 | 240 | _options = NULL; |
fparain@3329 | 241 | _arguments_list = NULL; |
fparain@3559 | 242 | _delim = ' '; |
fparain@3329 | 243 | } |
fparain@3329 | 244 | void add_dcmd_option(GenDCmdArgument* arg); |
fparain@3329 | 245 | void add_dcmd_argument(GenDCmdArgument* arg); |
fparain@3329 | 246 | GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len); |
fparain@3329 | 247 | GenDCmdArgument* arguments_list() { return _arguments_list; }; |
fparain@3329 | 248 | void check(TRAPS); |
fparain@3329 | 249 | void parse(CmdLine* line, char delim, TRAPS); |
fparain@3329 | 250 | void print_help(outputStream* out, const char* cmd_name); |
fparain@3329 | 251 | void reset(TRAPS); |
fparain@3329 | 252 | void cleanup(); |
fparain@3329 | 253 | int num_arguments(); |
fparain@3329 | 254 | GrowableArray<const char*>* argument_name_array(); |
fparain@3329 | 255 | GrowableArray<DCmdArgumentInfo*>* argument_info_array(); |
fparain@3329 | 256 | }; |
fparain@3329 | 257 | |
fparain@3329 | 258 | // The DCmd class is the parent class of all diagnostic commands |
fparain@3329 | 259 | // Diagnostic command instances should not be instantiated directly but |
fparain@3329 | 260 | // created using the associated factory. The factory can be retrieved with |
fparain@3329 | 261 | // the DCmdFactory::getFactory() method. |
fparain@3329 | 262 | // A diagnostic command instance can either be allocated in the resource Area |
fparain@3329 | 263 | // or in the C-heap. Allocation in the resource area is recommended when the |
fparain@3329 | 264 | // current thread is the only one which will access the diagnostic command |
fparain@3329 | 265 | // instance. Allocation in the C-heap is required when the diagnostic command |
fparain@3329 | 266 | // is accessed by several threads (for instance to perform asynchronous |
fparain@3329 | 267 | // execution). |
fparain@3329 | 268 | // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for |
fparain@3329 | 269 | // each diagnostic command instance. In case of a C-heap allocated diagnostic |
fparain@3329 | 270 | // command instance, the DCmdMark must be created in the context of the last |
fparain@3329 | 271 | // thread that will access the instance. |
fparain@3329 | 272 | class DCmd : public ResourceObj { |
fparain@3329 | 273 | protected: |
fparain@3329 | 274 | outputStream* _output; |
fparain@3329 | 275 | bool _is_heap_allocated; |
fparain@3329 | 276 | public: |
fparain@3329 | 277 | DCmd(outputStream* output, bool heap_allocated) { |
fparain@3329 | 278 | _output = output; |
fparain@3329 | 279 | _is_heap_allocated = heap_allocated; |
fparain@3329 | 280 | } |
fparain@3329 | 281 | |
fparain@3329 | 282 | static const char* name() { return "No Name";} |
fparain@3329 | 283 | static const char* description() { return "No Help";} |
fparain@3329 | 284 | static const char* disabled_message() { return "Diagnostic command currently disabled"; } |
sla@3905 | 285 | // The impact() method returns a description of the intrusiveness of the diagnostic |
sla@3905 | 286 | // command on the Java Virtual Machine behavior. The rational for this method is that some |
sla@3905 | 287 | // diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine |
sla@3905 | 288 | // (for instance a Thread Dump for an application with several tens of thousands of threads, |
sla@3905 | 289 | // or a Head Dump with a 40GB+ heap size) and other diagnostic commands have no serious |
sla@3905 | 290 | // impact on the JVM (for instance, getting the command line arguments or the JVM version). |
sla@3905 | 291 | // The recommended format for the description is <impact level>: [longer description], |
sla@3905 | 292 | // where the impact level is selected among this list: {Low, Medium, High}. The optional |
sla@3905 | 293 | // longer description can provide more specific details like the fact that Thread Dump |
sla@3905 | 294 | // impact depends on the heap size. |
fparain@3329 | 295 | static const char* impact() { return "Low: No impact"; } |
fparain@5047 | 296 | // The permission() method returns the description of Java Permission. This |
fparain@5047 | 297 | // permission is required when the diagnostic command is invoked via the |
fparain@5047 | 298 | // DiagnosticCommandMBean. The rationale for this permission check is that |
fparain@5047 | 299 | // the DiagnosticCommandMBean can be used to perform remote invocations of |
fparain@5047 | 300 | // diagnostic commands through the PlatformMBeanServer. The (optional) Java |
fparain@5047 | 301 | // Permission associated with each diagnostic command should ease the work |
fparain@5047 | 302 | // of system administrators to write policy files granting permissions to |
fparain@5047 | 303 | // execute diagnostic commands to remote users. Any diagnostic command with |
fparain@5047 | 304 | // a potential impact on security should overwrite this method. |
fparain@5047 | 305 | static const JavaPermission permission() { |
fparain@5047 | 306 | JavaPermission p = {NULL, NULL, NULL}; |
fparain@5047 | 307 | return p; |
fparain@5047 | 308 | } |
fparain@3329 | 309 | static int num_arguments() { return 0; } |
fparain@3329 | 310 | outputStream* output() { return _output; } |
fparain@3329 | 311 | bool is_heap_allocated() { return _is_heap_allocated; } |
fparain@3402 | 312 | virtual void print_help(const char* name) { |
fparain@3402 | 313 | output()->print_cr("Syntax: %s", name); |
fparain@3402 | 314 | } |
fparain@3402 | 315 | virtual void parse(CmdLine* line, char delim, TRAPS) { |
fparain@3402 | 316 | DCmdArgIter iter(line->args_addr(), line->args_len(), delim); |
fparain@3402 | 317 | bool has_arg = iter.next(CHECK); |
fparain@3402 | 318 | if (has_arg) { |
fparain@3402 | 319 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
sla@3905 | 320 | "The argument list of this diagnostic command should be empty."); |
fparain@3402 | 321 | } |
fparain@3402 | 322 | } |
fparain@5047 | 323 | virtual void execute(DCmdSource source, TRAPS) { } |
fparain@3329 | 324 | virtual void reset(TRAPS) { } |
fparain@3329 | 325 | virtual void cleanup() { } |
fparain@3329 | 326 | |
fparain@3329 | 327 | // support for the JMX interface |
fparain@3329 | 328 | virtual GrowableArray<const char*>* argument_name_array() { |
fparain@3329 | 329 | GrowableArray<const char*>* array = new GrowableArray<const char*>(0); |
fparain@3329 | 330 | return array; |
fparain@3329 | 331 | } |
fparain@3329 | 332 | virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() { |
fparain@3329 | 333 | GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0); |
fparain@3329 | 334 | return array; |
fparain@3329 | 335 | } |
fparain@3329 | 336 | |
fparain@3329 | 337 | // main method to invoke the framework |
fparain@5047 | 338 | static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline, |
fparain@3329 | 339 | char delim, TRAPS); |
fparain@3329 | 340 | }; |
fparain@3329 | 341 | |
fparain@3402 | 342 | class DCmdWithParser : public DCmd { |
fparain@3402 | 343 | protected: |
fparain@3402 | 344 | DCmdParser _dcmdparser; |
fparain@3402 | 345 | public: |
fparain@3402 | 346 | DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { } |
fparain@3402 | 347 | static const char* name() { return "No Name";} |
fparain@3402 | 348 | static const char* description() { return "No Help";} |
fparain@3402 | 349 | static const char* disabled_message() { return "Diagnostic command currently disabled"; } |
fparain@3402 | 350 | static const char* impact() { return "Low: No impact"; } |
fparain@5047 | 351 | static const JavaPermission permission() {JavaPermission p = {NULL, NULL, NULL}; return p; } |
fparain@3402 | 352 | static int num_arguments() { return 0; } |
fparain@3402 | 353 | virtual void parse(CmdLine *line, char delim, TRAPS); |
fparain@5047 | 354 | virtual void execute(DCmdSource source, TRAPS) { } |
fparain@3402 | 355 | virtual void reset(TRAPS); |
fparain@3402 | 356 | virtual void cleanup(); |
fparain@3402 | 357 | virtual void print_help(const char* name); |
fparain@3402 | 358 | virtual GrowableArray<const char*>* argument_name_array(); |
fparain@3402 | 359 | virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array(); |
fparain@3402 | 360 | }; |
fparain@3402 | 361 | |
fparain@3329 | 362 | class DCmdMark : public StackObj { |
fparain@3329 | 363 | DCmd* _ref; |
fparain@3329 | 364 | public: |
fparain@3329 | 365 | DCmdMark(DCmd* cmd) { _ref = cmd; } |
fparain@3329 | 366 | ~DCmdMark() { |
fparain@3329 | 367 | if (_ref != NULL) { |
fparain@3329 | 368 | _ref->cleanup(); |
fparain@3329 | 369 | if (_ref->is_heap_allocated()) { |
fparain@3329 | 370 | delete _ref; |
fparain@3329 | 371 | } |
fparain@3329 | 372 | } |
fparain@3329 | 373 | } |
fparain@3329 | 374 | }; |
fparain@3329 | 375 | |
fparain@3329 | 376 | // Diagnostic commands are not directly instantiated but created with a factory. |
fparain@3329 | 377 | // Each diagnostic command class has its own factory. The DCmdFactory class also |
fparain@3329 | 378 | // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory |
fparain@3329 | 379 | // has to be registered to make the diagnostic command available (see |
fparain@3329 | 380 | // management.cpp) |
zgu@3900 | 381 | class DCmdFactory: public CHeapObj<mtInternal> { |
fparain@3329 | 382 | private: |
fparain@3329 | 383 | static Mutex* _dcmdFactory_lock; |
fparain@5047 | 384 | static bool _send_jmx_notification; |
fparain@5047 | 385 | static bool _has_pending_jmx_notification; |
fparain@3329 | 386 | // Pointer to the next factory in the singly-linked list of registered |
fparain@3329 | 387 | // diagnostic commands |
fparain@3329 | 388 | DCmdFactory* _next; |
fparain@3329 | 389 | // When disabled, a diagnostic command cannot be executed. Any attempt to |
fparain@3329 | 390 | // execute it will result in the printing of the disabled message without |
fparain@3329 | 391 | // instantiating the command. |
fparain@3329 | 392 | bool _enabled; |
fparain@3329 | 393 | // When hidden, a diagnostic command doesn't appear in the list of commands |
fparain@3329 | 394 | // provided by the 'help' command. |
fparain@3329 | 395 | bool _hidden; |
fparain@5047 | 396 | uint32_t _export_flags; |
fparain@3329 | 397 | int _num_arguments; |
fparain@3329 | 398 | static DCmdFactory* _DCmdFactoryList; |
fparain@3329 | 399 | public: |
fparain@5047 | 400 | DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden) { |
fparain@3329 | 401 | _next = NULL; |
fparain@3329 | 402 | _enabled = enabled; |
fparain@3329 | 403 | _hidden = hidden; |
fparain@5047 | 404 | _export_flags = flags; |
fparain@3329 | 405 | _num_arguments = num_arguments; |
fparain@3329 | 406 | } |
fparain@3329 | 407 | bool is_enabled() const { return _enabled; } |
fparain@3329 | 408 | void set_enabled(bool b) { _enabled = b; } |
fparain@3329 | 409 | bool is_hidden() const { return _hidden; } |
fparain@3329 | 410 | void set_hidden(bool b) { _hidden = b; } |
fparain@5047 | 411 | uint32_t export_flags() { return _export_flags; } |
fparain@5047 | 412 | void set_export_flags(uint32_t f) { _export_flags = f; } |
fparain@3329 | 413 | int num_arguments() { return _num_arguments; } |
fparain@3329 | 414 | DCmdFactory* next() { return _next; } |
fparain@3329 | 415 | virtual DCmd* create_Cheap_instance(outputStream* output) = 0; |
fparain@3329 | 416 | virtual DCmd* create_resource_instance(outputStream* output) = 0; |
fparain@3329 | 417 | virtual const char* name() const = 0; |
fparain@3329 | 418 | virtual const char* description() const = 0; |
fparain@3329 | 419 | virtual const char* impact() const = 0; |
fparain@5047 | 420 | virtual const JavaPermission permission() const = 0; |
fparain@3329 | 421 | virtual const char* disabled_message() const = 0; |
fparain@3329 | 422 | // Register a DCmdFactory to make a diagnostic command available. |
fparain@3329 | 423 | // Once registered, a diagnostic command must not be unregistered. |
fparain@3329 | 424 | // To prevent a diagnostic command from being executed, just set the |
fparain@3329 | 425 | // enabled flag to false. |
fparain@3329 | 426 | static int register_DCmdFactory(DCmdFactory* factory); |
fparain@5047 | 427 | static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len); |
fparain@3329 | 428 | // Returns a C-heap allocated diagnostic command for the given command line |
fparain@5047 | 429 | static DCmd* create_global_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS); |
fparain@3329 | 430 | // Returns a resourceArea allocated diagnostic command for the given command line |
fparain@5047 | 431 | static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS); |
fparain@5047 | 432 | static GrowableArray<const char*>* DCmd_list(DCmdSource source); |
fparain@5047 | 433 | static GrowableArray<DCmdInfo*>* DCmdInfo_list(DCmdSource source); |
fparain@5047 | 434 | |
fparain@5047 | 435 | static void set_jmx_notification_enabled(bool enabled) { |
fparain@5047 | 436 | _send_jmx_notification = enabled; |
fparain@5047 | 437 | } |
fparain@5047 | 438 | static void push_jmx_notification_request(); |
fparain@5047 | 439 | static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; } |
fparain@5047 | 440 | static void send_notification(TRAPS); |
fparain@5047 | 441 | private: |
fparain@5047 | 442 | static void send_notification_internal(TRAPS); |
fparain@3329 | 443 | |
fparain@3329 | 444 | friend class HelpDCmd; |
fparain@3329 | 445 | }; |
fparain@3329 | 446 | |
fparain@3329 | 447 | // Template to easily create DCmdFactory instances. See management.cpp |
fparain@3329 | 448 | // where this template is used to create and register factories. |
fparain@3329 | 449 | template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory { |
fparain@3329 | 450 | public: |
fparain@5047 | 451 | DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) : |
fparain@5047 | 452 | DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { } |
fparain@3329 | 453 | // Returns a C-heap allocated instance |
fparain@3329 | 454 | virtual DCmd* create_Cheap_instance(outputStream* output) { |
zgu@3900 | 455 | return new (ResourceObj::C_HEAP, mtInternal) DCmdClass(output, true); |
fparain@3329 | 456 | } |
fparain@3329 | 457 | // Returns a resourceArea allocated instance |
fparain@3329 | 458 | virtual DCmd* create_resource_instance(outputStream* output) { |
fparain@3329 | 459 | return new DCmdClass(output, false); |
fparain@3329 | 460 | } |
fparain@3329 | 461 | virtual const char* name() const { |
fparain@3329 | 462 | return DCmdClass::name(); |
fparain@3329 | 463 | } |
fparain@3329 | 464 | virtual const char* description() const { |
fparain@3329 | 465 | return DCmdClass::description(); |
fparain@3329 | 466 | } |
fparain@3329 | 467 | virtual const char* impact() const { |
fparain@3329 | 468 | return DCmdClass::impact(); |
fparain@3329 | 469 | } |
fparain@5047 | 470 | virtual const JavaPermission permission() const { |
fparain@5047 | 471 | return DCmdClass::permission(); |
fparain@5047 | 472 | } |
fparain@3329 | 473 | virtual const char* disabled_message() const { |
fparain@3329 | 474 | return DCmdClass::disabled_message(); |
fparain@3329 | 475 | } |
fparain@3329 | 476 | }; |
fparain@3329 | 477 | |
dsamersoff@3478 | 478 | // This class provides a convenient way to register Dcmds, without a need to change |
dsamersoff@3478 | 479 | // management.cpp every time. Body of these two methods resides in |
dsamersoff@3478 | 480 | // diagnosticCommand.cpp |
dsamersoff@3478 | 481 | |
dsamersoff@3478 | 482 | class DCmdRegistrant : public AllStatic { |
dsamersoff@3478 | 483 | |
dsamersoff@3478 | 484 | private: |
dsamersoff@3478 | 485 | static void register_dcmds(); |
dsamersoff@3478 | 486 | static void register_dcmds_ext(); |
dsamersoff@3478 | 487 | |
dsamersoff@3478 | 488 | friend class Management; |
dsamersoff@3478 | 489 | }; |
dsamersoff@3478 | 490 | |
fparain@3329 | 491 | #endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP |