Mercurial > hg > graal-jvmci-8
comparison src/share/vm/services/diagnosticFramework.hpp @ 10215:31a4e55f8c9d
8004095: Add support for JMX interface to Diagnostic Framework and Commands
Reviewed-by: acorn, sla
author | fparain |
---|---|
date | Fri, 03 May 2013 05:05:31 -0700 |
parents | 5a1f452f8f90 |
children |
comparison
equal
deleted
inserted
replaced
10167:8fe2542bdc8d | 10215:31a4e55f8c9d |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
32 #include "runtime/vm_version.hpp" | 32 #include "runtime/vm_version.hpp" |
33 #include "runtime/vmThread.hpp" | 33 #include "runtime/vmThread.hpp" |
34 #include "utilities/ostream.hpp" | 34 #include "utilities/ostream.hpp" |
35 | 35 |
36 | 36 |
37 enum DCmdSource { | |
38 DCmd_Source_Internal = 0x01U, // invocation from the JVM | |
39 DCmd_Source_AttachAPI = 0x02U, // invocation via the attachAPI | |
40 DCmd_Source_MBean = 0x04U // invocation via a MBean | |
41 }; | |
42 | |
43 // Warning: strings referenced by the JavaPermission struct are passed to | |
44 // the native part of the JDK. Avoid use of dynamically allocated strings | |
45 // that could be de-allocated before the JDK native code had time to | |
46 // convert them into Java Strings. | |
47 struct JavaPermission { | |
48 const char* _class; | |
49 const char* _name; | |
50 const char* _action; | |
51 }; | |
52 | |
37 // CmdLine is the class used to handle a command line containing a single | 53 // CmdLine is the class used to handle a command line containing a single |
38 // diagnostic command and its arguments. It provides methods to access the | 54 // diagnostic command and its arguments. It provides methods to access the |
39 // command name and the beginning of the arguments. The class is also | 55 // command name and the beginning of the arguments. The class is also |
40 // able to identify commented command lines and the "stop" keyword | 56 // able to identify commented command lines and the "stop" keyword |
41 class CmdLine : public StackObj { | 57 class CmdLine : public StackObj { |
111 | 127 |
112 // A DCmdInfo instance provides a description of a diagnostic command. It is | 128 // A DCmdInfo instance provides a description of a diagnostic command. It is |
113 // used to export the description to the JMX interface of the framework. | 129 // used to export the description to the JMX interface of the framework. |
114 class DCmdInfo : public ResourceObj { | 130 class DCmdInfo : public ResourceObj { |
115 protected: | 131 protected: |
116 const char* _name; | 132 const char* _name; /* Name of the diagnostic command */ |
117 const char* _description; | 133 const char* _description; /* Short description */ |
118 const char* _impact; | 134 const char* _impact; /* Impact on the JVM */ |
119 int _num_arguments; | 135 JavaPermission _permission; /* Java Permission required to execute this command if any */ |
120 bool _is_enabled; | 136 int _num_arguments; /* Number of supported options or arguments */ |
137 bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */ | |
121 public: | 138 public: |
122 DCmdInfo(const char* name, | 139 DCmdInfo(const char* name, |
123 const char* description, | 140 const char* description, |
124 const char* impact, | 141 const char* impact, |
142 JavaPermission permission, | |
125 int num_arguments, | 143 int num_arguments, |
126 bool enabled) { | 144 bool enabled) { |
127 this->_name = name; | 145 this->_name = name; |
128 this->_description = description; | 146 this->_description = description; |
129 this->_impact = impact; | 147 this->_impact = impact; |
148 this->_permission = permission; | |
130 this->_num_arguments = num_arguments; | 149 this->_num_arguments = num_arguments; |
131 this->_is_enabled = enabled; | 150 this->_is_enabled = enabled; |
132 } | 151 } |
133 const char* name() const { return _name; } | 152 const char* name() const { return _name; } |
134 const char* description() const { return _description; } | 153 const char* description() const { return _description; } |
135 const char* impact() const { return _impact; } | 154 const char* impact() const { return _impact; } |
155 JavaPermission permission() const { return _permission; } | |
136 int num_arguments() const { return _num_arguments; } | 156 int num_arguments() const { return _num_arguments; } |
137 bool is_enabled() const { return _is_enabled; } | 157 bool is_enabled() const { return _is_enabled; } |
138 | 158 |
139 static bool by_name(void* name, DCmdInfo* info); | 159 static bool by_name(void* name, DCmdInfo* info); |
140 }; | 160 }; |
142 // A DCmdArgumentInfo instance provides a description of a diagnostic command | 162 // A DCmdArgumentInfo instance provides a description of a diagnostic command |
143 // argument. It is used to export the description to the JMX interface of the | 163 // argument. It is used to export the description to the JMX interface of the |
144 // framework. | 164 // framework. |
145 class DCmdArgumentInfo : public ResourceObj { | 165 class DCmdArgumentInfo : public ResourceObj { |
146 protected: | 166 protected: |
147 const char* _name; | 167 const char* _name; /* Option/Argument name*/ |
148 const char* _description; | 168 const char* _description; /* Short description */ |
149 const char* _type; | 169 const char* _type; /* Type: STRING, BOOLEAN, etc. */ |
150 const char* _default_string; | 170 const char* _default_string; /* Default value in a parsable string */ |
151 bool _mandatory; | 171 bool _mandatory; /* True if the option/argument is mandatory */ |
152 bool _option; | 172 bool _option; /* True if it is an option, false if it is an argument */ |
153 int _position; | 173 /* (see diagnosticFramework.hpp for option/argument definitions) */ |
174 bool _multiple; /* True is the option can be specified several time */ | |
175 int _position; /* Expected position for this argument (this field is */ | |
176 /* meaningless for options) */ | |
154 public: | 177 public: |
155 DCmdArgumentInfo(const char* name, const char* description, const char* type, | 178 DCmdArgumentInfo(const char* name, const char* description, const char* type, |
156 const char* default_string, bool mandatory, bool option) { | 179 const char* default_string, bool mandatory, bool option, |
180 bool multiple) { | |
157 this->_name = name; | 181 this->_name = name; |
158 this->_description = description; | 182 this->_description = description; |
159 this->_type = type; | 183 this->_type = type; |
160 this->_default_string = default_string; | 184 this->_default_string = default_string; |
161 this->_option = option; | 185 this->_option = option; |
162 this->_mandatory = mandatory; | 186 this->_mandatory = mandatory; |
163 this->_option = option; | 187 this->_option = option; |
188 this->_multiple = multiple; | |
164 this->_position = -1; | 189 this->_position = -1; |
165 } | 190 } |
166 DCmdArgumentInfo(const char* name, const char* description, const char* type, | 191 DCmdArgumentInfo(const char* name, const char* description, const char* type, |
167 const char* default_string, bool mandatory, bool option, | 192 const char* default_string, bool mandatory, bool option, |
168 int position) { | 193 bool multiple, int position) { |
169 this->_name = name; | 194 this->_name = name; |
170 this->_description = description; | 195 this->_description = description; |
171 this->_type = type; | 196 this->_type = type; |
172 this->_default_string = default_string; | 197 this->_default_string = default_string; |
173 this->_option = option; | 198 this->_option = option; |
174 this->_mandatory = mandatory; | 199 this->_mandatory = mandatory; |
175 this->_option = option; | 200 this->_option = option; |
201 this->_multiple = multiple; | |
176 this->_position = position; | 202 this->_position = position; |
177 } | 203 } |
178 const char* name() const { return _name; } | 204 const char* name() const { return _name; } |
179 const char* description() const { return _description; } | 205 const char* description() const { return _description; } |
180 const char* type() const { return _type; } | 206 const char* type() const { return _type; } |
181 const char* default_string() const { return _default_string; } | 207 const char* default_string() const { return _default_string; } |
182 bool is_mandatory() const { return _mandatory; } | 208 bool is_mandatory() const { return _mandatory; } |
183 bool is_option() const { return _option; } | 209 bool is_option() const { return _option; } |
210 bool is_multiple() const { return _multiple; } | |
184 int position() const { return _position; } | 211 int position() const { return _position; } |
185 }; | 212 }; |
186 | 213 |
187 // The DCmdParser class can be used to create an argument parser for a | 214 // The DCmdParser class can be used to create an argument parser for a |
188 // diagnostic command. It is not mandatory to use it to parse arguments. | 215 // diagnostic command. It is not mandatory to use it to parse arguments. |
216 // The DCmdParser parses a CmdLine instance according to the parameters that | |
217 // have been declared by its associated diagnostic command. A parameter can | |
218 // either be an option or an argument. Options are identified by the option name | |
219 // while arguments are identified by their position in the command line. The | |
220 // position of an argument is defined relative to all arguments passed on the | |
221 // command line, options are not considered when defining an argument position. | |
222 // The generic syntax of a diagnostic command is: | |
223 // | |
224 // <command name> [<option>=<value>] [<argument_value>] | |
225 // | |
226 // Example: | |
227 // | |
228 // command_name option1=value1 option2=value argumentA argumentB argumentC | |
229 // | |
230 // In this command line, the diagnostic command receives five parameters, two | |
231 // options named option1 and option2, and three arguments. argumentA's position | |
232 // is 0, argumentB's position is 1 and argumentC's position is 2. | |
189 class DCmdParser { | 233 class DCmdParser { |
190 private: | 234 private: |
191 GenDCmdArgument* _options; | 235 GenDCmdArgument* _options; |
192 GenDCmdArgument* _arguments_list; | 236 GenDCmdArgument* _arguments_list; |
193 char _delim; | 237 char _delim; |
247 // The recommended format for the description is <impact level>: [longer description], | 291 // The recommended format for the description is <impact level>: [longer description], |
248 // where the impact level is selected among this list: {Low, Medium, High}. The optional | 292 // where the impact level is selected among this list: {Low, Medium, High}. The optional |
249 // longer description can provide more specific details like the fact that Thread Dump | 293 // longer description can provide more specific details like the fact that Thread Dump |
250 // impact depends on the heap size. | 294 // impact depends on the heap size. |
251 static const char* impact() { return "Low: No impact"; } | 295 static const char* impact() { return "Low: No impact"; } |
296 // The permission() method returns the description of Java Permission. This | |
297 // permission is required when the diagnostic command is invoked via the | |
298 // DiagnosticCommandMBean. The rationale for this permission check is that | |
299 // the DiagnosticCommandMBean can be used to perform remote invocations of | |
300 // diagnostic commands through the PlatformMBeanServer. The (optional) Java | |
301 // Permission associated with each diagnostic command should ease the work | |
302 // of system administrators to write policy files granting permissions to | |
303 // execute diagnostic commands to remote users. Any diagnostic command with | |
304 // a potential impact on security should overwrite this method. | |
305 static const JavaPermission permission() { | |
306 JavaPermission p = {NULL, NULL, NULL}; | |
307 return p; | |
308 } | |
252 static int num_arguments() { return 0; } | 309 static int num_arguments() { return 0; } |
253 outputStream* output() { return _output; } | 310 outputStream* output() { return _output; } |
254 bool is_heap_allocated() { return _is_heap_allocated; } | 311 bool is_heap_allocated() { return _is_heap_allocated; } |
255 virtual void print_help(const char* name) { | 312 virtual void print_help(const char* name) { |
256 output()->print_cr("Syntax: %s", name); | 313 output()->print_cr("Syntax: %s", name); |
261 if (has_arg) { | 318 if (has_arg) { |
262 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | 319 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
263 "The argument list of this diagnostic command should be empty."); | 320 "The argument list of this diagnostic command should be empty."); |
264 } | 321 } |
265 } | 322 } |
266 virtual void execute(TRAPS) { } | 323 virtual void execute(DCmdSource source, TRAPS) { } |
267 virtual void reset(TRAPS) { } | 324 virtual void reset(TRAPS) { } |
268 virtual void cleanup() { } | 325 virtual void cleanup() { } |
269 | 326 |
270 // support for the JMX interface | 327 // support for the JMX interface |
271 virtual GrowableArray<const char*>* argument_name_array() { | 328 virtual GrowableArray<const char*>* argument_name_array() { |
276 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0); | 333 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0); |
277 return array; | 334 return array; |
278 } | 335 } |
279 | 336 |
280 // main method to invoke the framework | 337 // main method to invoke the framework |
281 static void parse_and_execute(outputStream* out, const char* cmdline, | 338 static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline, |
282 char delim, TRAPS); | 339 char delim, TRAPS); |
283 }; | 340 }; |
284 | 341 |
285 class DCmdWithParser : public DCmd { | 342 class DCmdWithParser : public DCmd { |
286 protected: | 343 protected: |
289 DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { } | 346 DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { } |
290 static const char* name() { return "No Name";} | 347 static const char* name() { return "No Name";} |
291 static const char* description() { return "No Help";} | 348 static const char* description() { return "No Help";} |
292 static const char* disabled_message() { return "Diagnostic command currently disabled"; } | 349 static const char* disabled_message() { return "Diagnostic command currently disabled"; } |
293 static const char* impact() { return "Low: No impact"; } | 350 static const char* impact() { return "Low: No impact"; } |
351 static const JavaPermission permission() {JavaPermission p = {NULL, NULL, NULL}; return p; } | |
294 static int num_arguments() { return 0; } | 352 static int num_arguments() { return 0; } |
295 virtual void parse(CmdLine *line, char delim, TRAPS); | 353 virtual void parse(CmdLine *line, char delim, TRAPS); |
296 virtual void execute(TRAPS) { } | 354 virtual void execute(DCmdSource source, TRAPS) { } |
297 virtual void reset(TRAPS); | 355 virtual void reset(TRAPS); |
298 virtual void cleanup(); | 356 virtual void cleanup(); |
299 virtual void print_help(const char* name); | 357 virtual void print_help(const char* name); |
300 virtual GrowableArray<const char*>* argument_name_array(); | 358 virtual GrowableArray<const char*>* argument_name_array(); |
301 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array(); | 359 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array(); |
321 // has to be registered to make the diagnostic command available (see | 379 // has to be registered to make the diagnostic command available (see |
322 // management.cpp) | 380 // management.cpp) |
323 class DCmdFactory: public CHeapObj<mtInternal> { | 381 class DCmdFactory: public CHeapObj<mtInternal> { |
324 private: | 382 private: |
325 static Mutex* _dcmdFactory_lock; | 383 static Mutex* _dcmdFactory_lock; |
384 static bool _send_jmx_notification; | |
385 static bool _has_pending_jmx_notification; | |
326 // Pointer to the next factory in the singly-linked list of registered | 386 // Pointer to the next factory in the singly-linked list of registered |
327 // diagnostic commands | 387 // diagnostic commands |
328 DCmdFactory* _next; | 388 DCmdFactory* _next; |
329 // When disabled, a diagnostic command cannot be executed. Any attempt to | 389 // When disabled, a diagnostic command cannot be executed. Any attempt to |
330 // execute it will result in the printing of the disabled message without | 390 // execute it will result in the printing of the disabled message without |
331 // instantiating the command. | 391 // instantiating the command. |
332 bool _enabled; | 392 bool _enabled; |
333 // When hidden, a diagnostic command doesn't appear in the list of commands | 393 // When hidden, a diagnostic command doesn't appear in the list of commands |
334 // provided by the 'help' command. | 394 // provided by the 'help' command. |
335 bool _hidden; | 395 bool _hidden; |
396 uint32_t _export_flags; | |
336 int _num_arguments; | 397 int _num_arguments; |
337 static DCmdFactory* _DCmdFactoryList; | 398 static DCmdFactory* _DCmdFactoryList; |
338 public: | 399 public: |
339 DCmdFactory(int num_arguments, bool enabled, bool hidden) { | 400 DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden) { |
340 _next = NULL; | 401 _next = NULL; |
341 _enabled = enabled; | 402 _enabled = enabled; |
342 _hidden = hidden; | 403 _hidden = hidden; |
404 _export_flags = flags; | |
343 _num_arguments = num_arguments; | 405 _num_arguments = num_arguments; |
344 } | 406 } |
345 bool is_enabled() const { return _enabled; } | 407 bool is_enabled() const { return _enabled; } |
346 void set_enabled(bool b) { _enabled = b; } | 408 void set_enabled(bool b) { _enabled = b; } |
347 bool is_hidden() const { return _hidden; } | 409 bool is_hidden() const { return _hidden; } |
348 void set_hidden(bool b) { _hidden = b; } | 410 void set_hidden(bool b) { _hidden = b; } |
411 uint32_t export_flags() { return _export_flags; } | |
412 void set_export_flags(uint32_t f) { _export_flags = f; } | |
349 int num_arguments() { return _num_arguments; } | 413 int num_arguments() { return _num_arguments; } |
350 DCmdFactory* next() { return _next; } | 414 DCmdFactory* next() { return _next; } |
351 virtual DCmd* create_Cheap_instance(outputStream* output) = 0; | 415 virtual DCmd* create_Cheap_instance(outputStream* output) = 0; |
352 virtual DCmd* create_resource_instance(outputStream* output) = 0; | 416 virtual DCmd* create_resource_instance(outputStream* output) = 0; |
353 virtual const char* name() const = 0; | 417 virtual const char* name() const = 0; |
354 virtual const char* description() const = 0; | 418 virtual const char* description() const = 0; |
355 virtual const char* impact() const = 0; | 419 virtual const char* impact() const = 0; |
420 virtual const JavaPermission permission() const = 0; | |
356 virtual const char* disabled_message() const = 0; | 421 virtual const char* disabled_message() const = 0; |
357 // Register a DCmdFactory to make a diagnostic command available. | 422 // Register a DCmdFactory to make a diagnostic command available. |
358 // Once registered, a diagnostic command must not be unregistered. | 423 // Once registered, a diagnostic command must not be unregistered. |
359 // To prevent a diagnostic command from being executed, just set the | 424 // To prevent a diagnostic command from being executed, just set the |
360 // enabled flag to false. | 425 // enabled flag to false. |
361 static int register_DCmdFactory(DCmdFactory* factory); | 426 static int register_DCmdFactory(DCmdFactory* factory); |
362 static DCmdFactory* factory(const char* cmd, size_t len); | 427 static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len); |
363 // Returns a C-heap allocated diagnostic command for the given command line | 428 // Returns a C-heap allocated diagnostic command for the given command line |
364 static DCmd* create_global_DCmd(CmdLine &line, outputStream* out, TRAPS); | 429 static DCmd* create_global_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS); |
365 // Returns a resourceArea allocated diagnostic command for the given command line | 430 // Returns a resourceArea allocated diagnostic command for the given command line |
366 static DCmd* create_local_DCmd(CmdLine &line, outputStream* out, TRAPS); | 431 static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS); |
367 static GrowableArray<const char*>* DCmd_list(); | 432 static GrowableArray<const char*>* DCmd_list(DCmdSource source); |
368 static GrowableArray<DCmdInfo*>* DCmdInfo_list(); | 433 static GrowableArray<DCmdInfo*>* DCmdInfo_list(DCmdSource source); |
434 | |
435 static void set_jmx_notification_enabled(bool enabled) { | |
436 _send_jmx_notification = enabled; | |
437 } | |
438 static void push_jmx_notification_request(); | |
439 static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; } | |
440 static void send_notification(TRAPS); | |
441 private: | |
442 static void send_notification_internal(TRAPS); | |
369 | 443 |
370 friend class HelpDCmd; | 444 friend class HelpDCmd; |
371 }; | 445 }; |
372 | 446 |
373 // Template to easily create DCmdFactory instances. See management.cpp | 447 // Template to easily create DCmdFactory instances. See management.cpp |
374 // where this template is used to create and register factories. | 448 // where this template is used to create and register factories. |
375 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory { | 449 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory { |
376 public: | 450 public: |
377 DCmdFactoryImpl(bool enabled, bool hidden) : | 451 DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) : |
378 DCmdFactory(DCmdClass::num_arguments(), enabled, hidden) { } | 452 DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { } |
379 // Returns a C-heap allocated instance | 453 // Returns a C-heap allocated instance |
380 virtual DCmd* create_Cheap_instance(outputStream* output) { | 454 virtual DCmd* create_Cheap_instance(outputStream* output) { |
381 return new (ResourceObj::C_HEAP, mtInternal) DCmdClass(output, true); | 455 return new (ResourceObj::C_HEAP, mtInternal) DCmdClass(output, true); |
382 } | 456 } |
383 // Returns a resourceArea allocated instance | 457 // Returns a resourceArea allocated instance |
391 return DCmdClass::description(); | 465 return DCmdClass::description(); |
392 } | 466 } |
393 virtual const char* impact() const { | 467 virtual const char* impact() const { |
394 return DCmdClass::impact(); | 468 return DCmdClass::impact(); |
395 } | 469 } |
470 virtual const JavaPermission permission() const { | |
471 return DCmdClass::permission(); | |
472 } | |
396 virtual const char* disabled_message() const { | 473 virtual const char* disabled_message() const { |
397 return DCmdClass::disabled_message(); | 474 return DCmdClass::disabled_message(); |
398 } | 475 } |
399 }; | 476 }; |
400 | 477 |