src/share/vm/services/diagnosticFramework.hpp

Mon, 09 Jan 2012 10:27:24 +0100

author
fparain
date
Mon, 09 Jan 2012 10:27:24 +0100
changeset 3402
4f25538b54c9
parent 3329
3b688d6ff3d0
child 3478
a42c07c38c47
permissions
-rw-r--r--

7120511: Add diagnostic commands
Reviewed-by: acorn, phh, dcubed, sspitsyn

fparain@3329 1 /*
fparain@3329 2 * Copyright (c) 2011, 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@3329 37 // CmdLine is the class used to handle a command line containing a single
fparain@3329 38 // diagnostic command and its arguments. It provides methods to access the
fparain@3329 39 // command name and the beginning of the arguments. The class is also
fparain@3329 40 // able to identify commented command lines and the "stop" keyword
fparain@3329 41 class CmdLine : public StackObj {
fparain@3329 42 private:
fparain@3329 43 const char* _cmd;
fparain@3329 44 size_t _cmd_len;
fparain@3329 45 const char* _args;
fparain@3329 46 size_t _args_len;
fparain@3329 47 public:
fparain@3329 48 CmdLine(const char* line, size_t len, bool no_command_name);
fparain@3329 49 const char* args_addr() const { return _args; }
fparain@3329 50 size_t args_len() const { return _args_len; }
fparain@3329 51 const char* cmd_addr() const { return _cmd; }
fparain@3329 52 size_t cmd_len() const { return _cmd_len; }
fparain@3329 53 bool is_empty() { return _cmd_len == 0; }
fparain@3329 54 bool is_executable() { return is_empty() || _cmd[0] != '#'; }
fparain@3329 55 bool is_stop() { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; }
fparain@3329 56 };
fparain@3329 57
fparain@3329 58 // Iterator class taking a character string in input and returning a CmdLine
fparain@3329 59 // instance for each command line. The argument delimiter has to be specified.
fparain@3329 60 class DCmdIter : public StackObj {
fparain@3329 61 friend class DCmd;
fparain@3329 62 private:
fparain@3329 63 const char* _str;
fparain@3329 64 char _delim;
fparain@3329 65 size_t _len;
fparain@3329 66 size_t _cursor;
fparain@3329 67 public:
fparain@3329 68
fparain@3329 69 DCmdIter(const char* str, char delim) {
fparain@3329 70 _str = str;
fparain@3329 71 _delim = delim;
fparain@3329 72 _len = strlen(str);
fparain@3329 73 _cursor = 0;
fparain@3329 74 }
fparain@3329 75 bool has_next() { return _cursor < _len; }
fparain@3329 76 CmdLine next() {
fparain@3329 77 assert(_cursor <= _len, "Cannot iterate more");
fparain@3329 78 size_t n = _cursor;
fparain@3329 79 while (n < _len && _str[n] != _delim) n++;
fparain@3329 80 CmdLine line(&(_str[_cursor]), n - _cursor, false);
fparain@3329 81 _cursor = n + 1;
fparain@3329 82 // The default copy constructor of CmdLine is used to return a CmdLine
fparain@3329 83 // instance to the caller.
fparain@3329 84 return line;
fparain@3329 85 }
fparain@3329 86 };
fparain@3329 87
fparain@3329 88 // Iterator class to iterate over diagnostic command arguments
fparain@3329 89 class DCmdArgIter : public ResourceObj {
fparain@3329 90 const char* _buffer;
fparain@3329 91 size_t _len;
fparain@3329 92 size_t _cursor;
fparain@3329 93 const char* _key_addr;
fparain@3329 94 size_t _key_len;
fparain@3329 95 const char* _value_addr;
fparain@3329 96 size_t _value_len;
fparain@3329 97 char _delim;
fparain@3329 98 public:
fparain@3329 99 DCmdArgIter(const char* buf, size_t len, char delim) {
fparain@3329 100 _buffer = buf;
fparain@3329 101 _len = len;
fparain@3329 102 _delim = delim;
fparain@3329 103 _cursor = 0;
fparain@3329 104 }
fparain@3329 105 bool next(TRAPS);
fparain@3329 106 const char* key_addr() { return _key_addr; }
fparain@3329 107 size_t key_length() { return _key_len; }
fparain@3329 108 const char* value_addr() { return _value_addr; }
fparain@3329 109 size_t value_length() { return _value_len; }
fparain@3329 110 };
fparain@3329 111
fparain@3329 112 // A DCmdInfo instance provides a description of a diagnostic command. It is
fparain@3329 113 // used to export the description to the JMX interface of the framework.
fparain@3329 114 class DCmdInfo : public ResourceObj {
fparain@3329 115 protected:
fparain@3329 116 const char* _name;
fparain@3329 117 const char* _description;
fparain@3329 118 const char* _impact;
fparain@3329 119 int _num_arguments;
fparain@3329 120 bool _is_enabled;
fparain@3329 121 public:
fparain@3329 122 DCmdInfo(const char* name,
fparain@3329 123 const char* description,
fparain@3329 124 const char* impact,
fparain@3329 125 int num_arguments,
fparain@3329 126 bool enabled) {
fparain@3329 127 this->_name = name;
fparain@3329 128 this->_description = description;
fparain@3329 129 this->_impact = impact;
fparain@3329 130 this->_num_arguments = num_arguments;
fparain@3329 131 this->_is_enabled = enabled;
fparain@3329 132 }
fparain@3329 133 const char* name() const { return _name; }
fparain@3329 134 const char* description() const { return _description; }
fparain@3329 135 const char* impact() const { return _impact; }
fparain@3329 136 int num_arguments() const { return _num_arguments; }
fparain@3329 137 bool is_enabled() const { return _is_enabled; }
fparain@3329 138
fparain@3329 139 static bool by_name(void* name, DCmdInfo* info);
fparain@3329 140 };
fparain@3329 141
fparain@3329 142 // A DCmdArgumentInfo instance provides a description of a diagnostic command
fparain@3329 143 // argument. It is used to export the description to the JMX interface of the
fparain@3329 144 // framework.
fparain@3329 145 class DCmdArgumentInfo : public ResourceObj {
fparain@3329 146 protected:
fparain@3329 147 const char* _name;
fparain@3329 148 const char* _description;
fparain@3329 149 const char* _type;
fparain@3329 150 const char* _default_string;
fparain@3329 151 bool _mandatory;
fparain@3329 152 bool _option;
fparain@3329 153 int _position;
fparain@3329 154 public:
fparain@3329 155 DCmdArgumentInfo(const char* name, const char* description, const char* type,
fparain@3329 156 const char* default_string, bool mandatory, bool option) {
fparain@3329 157 this->_name = name;
fparain@3329 158 this->_description = description;
fparain@3329 159 this->_type = type;
fparain@3329 160 this->_default_string = default_string;
fparain@3329 161 this->_option = option;
fparain@3329 162 this->_mandatory = mandatory;
fparain@3329 163 this->_option = option;
fparain@3329 164 this->_position = -1;
fparain@3329 165 }
fparain@3329 166 DCmdArgumentInfo(const char* name, const char* description, const char* type,
fparain@3329 167 const char* default_string, bool mandatory, bool option,
fparain@3329 168 int position) {
fparain@3329 169 this->_name = name;
fparain@3329 170 this->_description = description;
fparain@3329 171 this->_type = type;
fparain@3329 172 this->_default_string = default_string;
fparain@3329 173 this->_option = option;
fparain@3329 174 this->_mandatory = mandatory;
fparain@3329 175 this->_option = option;
fparain@3329 176 this->_position = position;
fparain@3329 177 }
fparain@3329 178 const char* name() const { return _name; }
fparain@3329 179 const char* description() const { return _description; }
fparain@3329 180 const char* type() const { return _type; }
fparain@3329 181 const char* default_string() const { return _default_string; }
fparain@3329 182 bool is_mandatory() const { return _mandatory; }
fparain@3329 183 bool is_option() const { return _option; }
fparain@3329 184 int position() const { return _position; }
fparain@3329 185 };
fparain@3329 186
fparain@3329 187 // The DCmdParser class can be used to create an argument parser for a
fparain@3329 188 // diagnostic command. It is not mandatory to use it to parse arguments.
fparain@3329 189 class DCmdParser {
fparain@3329 190 private:
fparain@3329 191 GenDCmdArgument* _options;
fparain@3329 192 GenDCmdArgument* _arguments_list;
fparain@3329 193 char _delim;
fparain@3329 194 public:
fparain@3329 195 DCmdParser() {
fparain@3329 196 _options = NULL;
fparain@3329 197 _arguments_list = NULL;
fparain@3329 198 }
fparain@3329 199 void add_dcmd_option(GenDCmdArgument* arg);
fparain@3329 200 void add_dcmd_argument(GenDCmdArgument* arg);
fparain@3329 201 GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);
fparain@3329 202 GenDCmdArgument* arguments_list() { return _arguments_list; };
fparain@3329 203 void check(TRAPS);
fparain@3329 204 void parse(CmdLine* line, char delim, TRAPS);
fparain@3329 205 void print_help(outputStream* out, const char* cmd_name);
fparain@3329 206 void reset(TRAPS);
fparain@3329 207 void cleanup();
fparain@3329 208 int num_arguments();
fparain@3329 209 GrowableArray<const char*>* argument_name_array();
fparain@3329 210 GrowableArray<DCmdArgumentInfo*>* argument_info_array();
fparain@3329 211 };
fparain@3329 212
fparain@3329 213 // The DCmd class is the parent class of all diagnostic commands
fparain@3329 214 // Diagnostic command instances should not be instantiated directly but
fparain@3329 215 // created using the associated factory. The factory can be retrieved with
fparain@3329 216 // the DCmdFactory::getFactory() method.
fparain@3329 217 // A diagnostic command instance can either be allocated in the resource Area
fparain@3329 218 // or in the C-heap. Allocation in the resource area is recommended when the
fparain@3329 219 // current thread is the only one which will access the diagnostic command
fparain@3329 220 // instance. Allocation in the C-heap is required when the diagnostic command
fparain@3329 221 // is accessed by several threads (for instance to perform asynchronous
fparain@3329 222 // execution).
fparain@3329 223 // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for
fparain@3329 224 // each diagnostic command instance. In case of a C-heap allocated diagnostic
fparain@3329 225 // command instance, the DCmdMark must be created in the context of the last
fparain@3329 226 // thread that will access the instance.
fparain@3329 227 class DCmd : public ResourceObj {
fparain@3329 228 protected:
fparain@3329 229 outputStream* _output;
fparain@3329 230 bool _is_heap_allocated;
fparain@3329 231 public:
fparain@3329 232 DCmd(outputStream* output, bool heap_allocated) {
fparain@3329 233 _output = output;
fparain@3329 234 _is_heap_allocated = heap_allocated;
fparain@3329 235 }
fparain@3329 236
fparain@3329 237 static const char* name() { return "No Name";}
fparain@3329 238 static const char* description() { return "No Help";}
fparain@3329 239 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
fparain@3329 240 static const char* impact() { return "Low: No impact"; }
fparain@3329 241 static int num_arguments() { return 0; }
fparain@3329 242 outputStream* output() { return _output; }
fparain@3329 243 bool is_heap_allocated() { return _is_heap_allocated; }
fparain@3402 244 virtual void print_help(const char* name) {
fparain@3402 245 output()->print_cr("Syntax: %s", name);
fparain@3402 246 }
fparain@3402 247 virtual void parse(CmdLine* line, char delim, TRAPS) {
fparain@3402 248 DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
fparain@3402 249 bool has_arg = iter.next(CHECK);
fparain@3402 250 if (has_arg) {
fparain@3402 251 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
fparain@3402 252 "Unknown argument in diagnostic command");
fparain@3402 253 }
fparain@3402 254 }
fparain@3329 255 virtual void execute(TRAPS) { }
fparain@3329 256 virtual void reset(TRAPS) { }
fparain@3329 257 virtual void cleanup() { }
fparain@3329 258
fparain@3329 259 // support for the JMX interface
fparain@3329 260 virtual GrowableArray<const char*>* argument_name_array() {
fparain@3329 261 GrowableArray<const char*>* array = new GrowableArray<const char*>(0);
fparain@3329 262 return array;
fparain@3329 263 }
fparain@3329 264 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() {
fparain@3329 265 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);
fparain@3329 266 return array;
fparain@3329 267 }
fparain@3329 268
fparain@3329 269 // main method to invoke the framework
fparain@3329 270 static void parse_and_execute(outputStream* out, const char* cmdline,
fparain@3329 271 char delim, TRAPS);
fparain@3329 272 };
fparain@3329 273
fparain@3402 274 class DCmdWithParser : public DCmd {
fparain@3402 275 protected:
fparain@3402 276 DCmdParser _dcmdparser;
fparain@3402 277 public:
fparain@3402 278 DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }
fparain@3402 279 static const char* name() { return "No Name";}
fparain@3402 280 static const char* description() { return "No Help";}
fparain@3402 281 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
fparain@3402 282 static const char* impact() { return "Low: No impact"; }
fparain@3402 283 static int num_arguments() { return 0; }
fparain@3402 284 virtual void parse(CmdLine *line, char delim, TRAPS);
fparain@3402 285 virtual void execute(TRAPS) { }
fparain@3402 286 virtual void reset(TRAPS);
fparain@3402 287 virtual void cleanup();
fparain@3402 288 virtual void print_help(const char* name);
fparain@3402 289 virtual GrowableArray<const char*>* argument_name_array();
fparain@3402 290 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array();
fparain@3402 291 };
fparain@3402 292
fparain@3329 293 class DCmdMark : public StackObj {
fparain@3329 294 DCmd* _ref;
fparain@3329 295 public:
fparain@3329 296 DCmdMark(DCmd* cmd) { _ref = cmd; }
fparain@3329 297 ~DCmdMark() {
fparain@3329 298 if (_ref != NULL) {
fparain@3329 299 _ref->cleanup();
fparain@3329 300 if (_ref->is_heap_allocated()) {
fparain@3329 301 delete _ref;
fparain@3329 302 }
fparain@3329 303 }
fparain@3329 304 }
fparain@3329 305 };
fparain@3329 306
fparain@3329 307 // Diagnostic commands are not directly instantiated but created with a factory.
fparain@3329 308 // Each diagnostic command class has its own factory. The DCmdFactory class also
fparain@3329 309 // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory
fparain@3329 310 // has to be registered to make the diagnostic command available (see
fparain@3329 311 // management.cpp)
fparain@3329 312 class DCmdFactory: public CHeapObj {
fparain@3329 313 private:
fparain@3329 314 static Mutex* _dcmdFactory_lock;
fparain@3329 315 // Pointer to the next factory in the singly-linked list of registered
fparain@3329 316 // diagnostic commands
fparain@3329 317 DCmdFactory* _next;
fparain@3329 318 // When disabled, a diagnostic command cannot be executed. Any attempt to
fparain@3329 319 // execute it will result in the printing of the disabled message without
fparain@3329 320 // instantiating the command.
fparain@3329 321 bool _enabled;
fparain@3329 322 // When hidden, a diagnostic command doesn't appear in the list of commands
fparain@3329 323 // provided by the 'help' command.
fparain@3329 324 bool _hidden;
fparain@3329 325 int _num_arguments;
fparain@3329 326 static DCmdFactory* _DCmdFactoryList;
fparain@3329 327 public:
fparain@3329 328 DCmdFactory(int num_arguments, bool enabled, bool hidden) {
fparain@3329 329 _next = NULL;
fparain@3329 330 _enabled = enabled;
fparain@3329 331 _hidden = hidden;
fparain@3329 332 _num_arguments = num_arguments;
fparain@3329 333 }
fparain@3329 334 bool is_enabled() const { return _enabled; }
fparain@3329 335 void set_enabled(bool b) { _enabled = b; }
fparain@3329 336 bool is_hidden() const { return _hidden; }
fparain@3329 337 void set_hidden(bool b) { _hidden = b; }
fparain@3329 338 int num_arguments() { return _num_arguments; }
fparain@3329 339 DCmdFactory* next() { return _next; }
fparain@3329 340 virtual DCmd* create_Cheap_instance(outputStream* output) = 0;
fparain@3329 341 virtual DCmd* create_resource_instance(outputStream* output) = 0;
fparain@3329 342 virtual const char* name() const = 0;
fparain@3329 343 virtual const char* description() const = 0;
fparain@3329 344 virtual const char* impact() const = 0;
fparain@3329 345 virtual const char* disabled_message() const = 0;
fparain@3329 346 // Register a DCmdFactory to make a diagnostic command available.
fparain@3329 347 // Once registered, a diagnostic command must not be unregistered.
fparain@3329 348 // To prevent a diagnostic command from being executed, just set the
fparain@3329 349 // enabled flag to false.
fparain@3329 350 static int register_DCmdFactory(DCmdFactory* factory);
fparain@3329 351 static DCmdFactory* factory(const char* cmd, size_t len);
fparain@3329 352 // Returns a C-heap allocated diagnostic command for the given command line
fparain@3329 353 static DCmd* create_global_DCmd(CmdLine &line, outputStream* out, TRAPS);
fparain@3329 354 // Returns a resourceArea allocated diagnostic command for the given command line
fparain@3329 355 static DCmd* create_local_DCmd(CmdLine &line, outputStream* out, TRAPS);
fparain@3329 356 static GrowableArray<const char*>* DCmd_list();
fparain@3329 357 static GrowableArray<DCmdInfo*>* DCmdInfo_list();
fparain@3329 358
fparain@3329 359 friend class HelpDCmd;
fparain@3329 360 };
fparain@3329 361
fparain@3329 362 // Template to easily create DCmdFactory instances. See management.cpp
fparain@3329 363 // where this template is used to create and register factories.
fparain@3329 364 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {
fparain@3329 365 public:
fparain@3329 366 DCmdFactoryImpl(bool enabled, bool hidden) :
fparain@3329 367 DCmdFactory(DCmdClass::num_arguments(), enabled, hidden) { }
fparain@3329 368 // Returns a C-heap allocated instance
fparain@3329 369 virtual DCmd* create_Cheap_instance(outputStream* output) {
fparain@3329 370 return new (ResourceObj::C_HEAP) DCmdClass(output, true);
fparain@3329 371 }
fparain@3329 372 // Returns a resourceArea allocated instance
fparain@3329 373 virtual DCmd* create_resource_instance(outputStream* output) {
fparain@3329 374 return new DCmdClass(output, false);
fparain@3329 375 }
fparain@3329 376 virtual const char* name() const {
fparain@3329 377 return DCmdClass::name();
fparain@3329 378 }
fparain@3329 379 virtual const char* description() const {
fparain@3329 380 return DCmdClass::description();
fparain@3329 381 }
fparain@3329 382 virtual const char* impact() const {
fparain@3329 383 return DCmdClass::impact();
fparain@3329 384 }
fparain@3329 385 virtual const char* disabled_message() const {
fparain@3329 386 return DCmdClass::disabled_message();
fparain@3329 387 }
fparain@3329 388 };
fparain@3329 389
fparain@3329 390 #endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP

mercurial