48 const char* _name;
49 const char* _action;
50 };
51
52 // CmdLine is the class used to handle a command line containing a single
53 // diagnostic command and its arguments. It provides methods to access the
54 // command name and the beginning of the arguments. The class is also
55 // able to identify commented command lines and the "stop" keyword
56 class CmdLine : public StackObj {
57 private:
58 const char* _cmd;
59 size_t _cmd_len;
60 const char* _args;
61 size_t _args_len;
62 public:
63 CmdLine(const char* line, size_t len, bool no_command_name);
64 const char* args_addr() const { return _args; }
65 size_t args_len() const { return _args_len; }
66 const char* cmd_addr() const { return _cmd; }
67 size_t cmd_len() const { return _cmd_len; }
68 bool is_empty() { return _cmd_len == 0; }
69 bool is_executable() { return is_empty() || _cmd[0] != '#'; }
70 bool is_stop() { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; }
71 };
72
73 // Iterator class taking a character string in input and returning a CmdLine
74 // instance for each command line. The argument delimiter has to be specified.
75 class DCmdIter : public StackObj {
76 friend class DCmd;
77 private:
78 const char* const _str;
79 const char _delim;
80 const size_t _len;
81 size_t _cursor;
82 public:
83
84 DCmdIter(const char* str, char delim)
85 : _str(str), _delim(delim), _len(::strlen(str)), _cursor(0) {}
86 bool has_next() const { return _cursor < _len; }
87 CmdLine next() {
88 assert(_cursor <= _len, "Cannot iterate more");
89 size_t n = _cursor;
90 while (n < _len && _str[n] != _delim) n++;
91 CmdLine line(&(_str[_cursor]), n - _cursor, false);
92 _cursor = n + 1;
93 // The default copy constructor of CmdLine is used to return a CmdLine
94 // instance to the caller.
95 return line;
96 }
97 };
98
99 // Iterator class to iterate over diagnostic command arguments
100 class DCmdArgIter : public ResourceObj {
101 const char* const _buffer;
102 const size_t _len;
103 size_t _cursor;
104 const char* _key_addr;
105 size_t _key_len;
106 const char* _value_addr;
107 size_t _value_len;
108 const char _delim;
109 public:
110 DCmdArgIter(const char* buf, size_t len, char delim)
111 : _buffer(buf), _len(len), _delim(delim), _cursor(0) {}
112
113 bool next(TRAPS);
114 const char* key_addr() const { return _key_addr; }
115 size_t key_length() const { return _key_len; }
116 const char* value_addr() const { return _value_addr; }
117 size_t value_length() const { return _value_len; }
118 };
119
120 // A DCmdInfo instance provides a description of a diagnostic command. It is
121 // used to export the description to the JMX interface of the framework.
122 class DCmdInfo : public ResourceObj {
123 protected:
124 const char* _name; /* Name of the diagnostic command */
125 const char* _description; /* Short description */
126 const char* _impact; /* Impact on the JVM */
127 JavaPermission _permission; /* Java Permission required to execute this command if any */
128 int _num_arguments; /* Number of supported options or arguments */
129 bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */
130 public:
131 DCmdInfo(const char* name,
132 const char* description,
133 const char* impact,
134 JavaPermission permission,
135 int num_arguments,
136 bool enabled) {
137 this->_name = name;
138 this->_description = description;
139 this->_impact = impact;
140 this->_permission = permission;
141 this->_num_arguments = num_arguments;
142 this->_is_enabled = enabled;
143 }
144 const char* name() const { return _name; }
145 const char* description() const { return _description; }
146 const char* impact() const { return _impact; }
147 JavaPermission permission() const { return _permission; }
148 int num_arguments() const { return _num_arguments; }
149 bool is_enabled() const { return _is_enabled; }
150
151 static bool by_name(void* name, DCmdInfo* info);
152 };
153
154 // A DCmdArgumentInfo instance provides a description of a diagnostic command
155 // argument. It is used to export the description to the JMX interface of the
156 // framework.
157 class DCmdArgumentInfo : public ResourceObj {
158 protected:
159 const char* const _name; /* Option/Argument name*/
160 const char* const _description; /* Short description */
161 const char* const _type; /* Type: STRING, BOOLEAN, etc. */
162 const char* const _default_string; /* Default value in a parsable string */
163 const bool _mandatory; /* True if the option/argument is mandatory */
164 const bool _option; /* True if it is an option, false if it is an argument */
165 /* (see diagnosticFramework.hpp for option/argument definitions) */
166 const bool _multiple; /* True is the option can be specified several time */
167 const int _position; /* Expected position for this argument (this field is */
168 /* meaningless for options) */
169 public:
170 DCmdArgumentInfo(const char* name, const char* description, const char* type,
171 const char* default_string, bool mandatory, bool option,
172 bool multiple, int position = -1)
173 : _name(name), _description(description), _type(type)
174 , _default_string(default_string), _mandatory(mandatory)
175 , _option(option), _multiple(multiple), _position(-1) {}
176
177 const char* name() const { return _name; }
178 const char* description() const { return _description; }
179 const char* type() const { return _type; }
180 const char* default_string() const { return _default_string; }
181 bool is_mandatory() const { return _mandatory; }
182 bool is_option() const { return _option; }
183 bool is_multiple() const { return _multiple; }
184 int position() const { return _position; }
185 };
186
187 // 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.
189 // The DCmdParser parses a CmdLine instance according to the parameters that
190 // have been declared by its associated diagnostic command. A parameter can
191 // either be an option or an argument. Options are identified by the option name
192 // while arguments are identified by their position in the command line. The
193 // position of an argument is defined relative to all arguments passed on the
194 // command line, options are not considered when defining an argument position.
195 // The generic syntax of a diagnostic command is:
196 //
197 // <command name> [<option>=<value>] [<argument_value>]
198 //
199 // Example:
200 //
201 // command_name option1=value1 option2=value argumentA argumentB argumentC
202 //
203 // In this command line, the diagnostic command receives five parameters, two
204 // options named option1 and option2, and three arguments. argumentA's position
205 // is 0, argumentB's position is 1 and argumentC's position is 2.
206 class DCmdParser {
207 private:
208 GenDCmdArgument* _options;
209 GenDCmdArgument* _arguments_list;
210 public:
211 DCmdParser()
212 : _options(NULL), _arguments_list(NULL) {}
213 void add_dcmd_option(GenDCmdArgument* arg);
214 void add_dcmd_argument(GenDCmdArgument* arg);
215 GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);
216 GenDCmdArgument* arguments_list() const { return _arguments_list; };
217 void check(TRAPS);
218 void parse(CmdLine* line, char delim, TRAPS);
219 void print_help(outputStream* out, const char* cmd_name) const;
220 void reset(TRAPS);
221 void cleanup();
222 int num_arguments() const;
223 GrowableArray<const char*>* argument_name_array() const;
224 GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;
225 };
226
227 // The DCmd class is the parent class of all diagnostic commands
228 // Diagnostic command instances should not be instantiated directly but
229 // created using the associated factory. The factory can be retrieved with
230 // the DCmdFactory::getFactory() method.
231 // A diagnostic command instance can either be allocated in the resource Area
232 // or in the C-heap. Allocation in the resource area is recommended when the
233 // current thread is the only one which will access the diagnostic command
234 // instance. Allocation in the C-heap is required when the diagnostic command
235 // is accessed by several threads (for instance to perform asynchronous
236 // execution).
237 // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for
238 // each diagnostic command instance. In case of a C-heap allocated diagnostic
239 // command instance, the DCmdMark must be created in the context of the last
240 // thread that will access the instance.
241 class DCmd : public ResourceObj {
242 protected:
243 outputStream* const _output;
244 const bool _is_heap_allocated;
245 public:
246 DCmd(outputStream* output, bool heap_allocated)
247 : _output(output), _is_heap_allocated(heap_allocated) {}
248
249 static const char* name() { return "No Name";}
250 static const char* description() { return "No Help";}
251 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
252 // The impact() method returns a description of the intrusiveness of the diagnostic
253 // command on the Java Virtual Machine behavior. The rational for this method is that some
254 // diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine
255 // (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 serious
257 // 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 optional
260 // longer description can provide more specific details like the fact that Thread Dump
261 // impact depends on the heap size.
262 static const char* impact() { return "Low: No impact"; }
263 // The permission() method returns the description of Java Permission. This
264 // permission is required when the diagnostic command is invoked via the
265 // DiagnosticCommandMBean. The rationale for this permission check is that
266 // the DiagnosticCommandMBean can be used to perform remote invocations of
267 // diagnostic commands through the PlatformMBeanServer. The (optional) Java
268 // Permission associated with each diagnostic command should ease the work
269 // of system administrators to write policy files granting permissions to
270 // execute diagnostic commands to remote users. Any diagnostic command with
271 // a potential impact on security should overwrite this method.
272 static const JavaPermission permission() {
273 JavaPermission p = {NULL, NULL, NULL};
274 return p;
275 }
276 static int num_arguments() { return 0; }
277 outputStream* output() const { return _output; }
278 bool is_heap_allocated() const { return _is_heap_allocated; }
279 virtual void print_help(const char* name) const {
280 output()->print_cr("Syntax: %s", name);
281 }
282 virtual void parse(CmdLine* line, char delim, TRAPS) {
294 // support for the JMX interface
295 virtual GrowableArray<const char*>* argument_name_array() const {
296 GrowableArray<const char*>* array = new GrowableArray<const char*>(0);
297 return array;
298 }
299 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const {
300 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);
301 return array;
302 }
303
304 // main method to invoke the framework
305 static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline,
306 char delim, TRAPS);
307 };
308
309 class DCmdWithParser : public DCmd {
310 protected:
311 DCmdParser _dcmdparser;
312 public:
313 DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }
314 static const char* name() { return "No Name";}
315 static const char* description() { return "No Help";}
316 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
317 static const char* impact() { return "Low: No impact"; }
318 static const JavaPermission permission() {JavaPermission p = {NULL, NULL, NULL}; return p; }
319 static int num_arguments() { return 0; }
320 virtual void parse(CmdLine *line, char delim, TRAPS);
321 virtual void execute(DCmdSource source, TRAPS) { }
322 virtual void reset(TRAPS);
323 virtual void cleanup();
324 virtual void print_help(const char* name) const;
325 virtual GrowableArray<const char*>* argument_name_array() const;
326 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;
327 };
328
329 class DCmdMark : public StackObj {
330 DCmd* const _ref;
331 public:
332 DCmdMark(DCmd* cmd) : _ref(cmd) {}
333 ~DCmdMark() {
334 if (_ref != NULL) {
335 _ref->cleanup();
336 if (_ref->is_heap_allocated()) {
337 delete _ref;
338 }
339 }
340 }
341 };
342
343 // Diagnostic commands are not directly instantiated but created with a factory.
344 // Each diagnostic command class has its own factory. The DCmdFactory class also
345 // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory
346 // has to be registered to make the diagnostic command available (see
347 // management.cpp)
348 class DCmdFactory: public CHeapObj<mtInternal> {
349 private:
350 static Mutex* _dcmdFactory_lock;
351 static bool _send_jmx_notification;
352 static bool _has_pending_jmx_notification;
353 // Pointer to the next factory in the singly-linked list of registered
354 // diagnostic commands
355 DCmdFactory* _next;
356 // When disabled, a diagnostic command cannot be executed. Any attempt to
357 // execute it will result in the printing of the disabled message without
358 // instantiating the command.
359 const bool _enabled;
360 // When hidden, a diagnostic command doesn't appear in the list of commands
361 // provided by the 'help' command.
362 const bool _hidden;
363 const uint32_t _export_flags;
364 const int _num_arguments;
365 static DCmdFactory* _DCmdFactoryList;
366 public:
367 DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden)
368 : _num_arguments(num_arguments), _enabled(enabled), _hidden(hidden)
369 , _export_flags(flags), _next(NULL) {}
370 bool is_enabled() const { return _enabled; }
371 bool is_hidden() const { return _hidden; }
372 uint32_t export_flags() const { return _export_flags; }
373 int num_arguments() const { return _num_arguments; }
374 DCmdFactory* next() const { return _next; }
375 virtual DCmd* create_resource_instance(outputStream* output) const = 0;
376 virtual const char* name() const = 0;
377 virtual const char* description() const = 0;
378 virtual const char* impact() const = 0;
379 virtual const JavaPermission permission() const = 0;
380 virtual const char* disabled_message() const = 0;
381 // Register a DCmdFactory to make a diagnostic command available.
382 // Once registered, a diagnostic command must not be unregistered.
383 // To prevent a diagnostic command from being executed, just set the
384 // enabled flag to false.
385 static int register_DCmdFactory(DCmdFactory* factory);
386 static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len);
387 // Returns a resourceArea allocated diagnostic command for the given command line
388 static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS);
389 static GrowableArray<const char*>* DCmd_list(DCmdSource source);
391
392 static void set_jmx_notification_enabled(bool enabled) {
393 _send_jmx_notification = enabled;
394 }
395 static void push_jmx_notification_request();
396 static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; }
397 static void send_notification(TRAPS);
398 private:
399 static void send_notification_internal(TRAPS);
400
401 friend class HelpDCmd;
402 };
403
404 // Template to easily create DCmdFactory instances. See management.cpp
405 // where this template is used to create and register factories.
406 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {
407 public:
408 DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) :
409 DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { }
410 // Returns a resourceArea allocated instance
411 virtual DCmd* create_resource_instance(outputStream* output) const {
412 return new DCmdClass(output, false);
413 }
414 virtual const char* name() const {
415 return DCmdClass::name();
416 }
417 virtual const char* description() const {
418 return DCmdClass::description();
419 }
420 virtual const char* impact() const {
421 return DCmdClass::impact();
422 }
423 virtual const JavaPermission permission() const {
424 return DCmdClass::permission();
425 }
426 virtual const char* disabled_message() const {
427 return DCmdClass::disabled_message();
428 }
429 };
430
431 // This class provides a convenient way to register Dcmds, without a need to change
432 // management.cpp every time. Body of these two methods resides in
433 // diagnosticCommand.cpp
434
435 class DCmdRegistrant : public AllStatic {
436
437 private:
438 static void register_dcmds();
439 static void register_dcmds_ext();
440
441 friend class Management;
442 };
443
444 #endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
|
48 const char* _name;
49 const char* _action;
50 };
51
52 // CmdLine is the class used to handle a command line containing a single
53 // diagnostic command and its arguments. It provides methods to access the
54 // command name and the beginning of the arguments. The class is also
55 // able to identify commented command lines and the "stop" keyword
56 class CmdLine : public StackObj {
57 private:
58 const char* _cmd;
59 size_t _cmd_len;
60 const char* _args;
61 size_t _args_len;
62 public:
63 CmdLine(const char* line, size_t len, bool no_command_name);
64 const char* args_addr() const { return _args; }
65 size_t args_len() const { return _args_len; }
66 const char* cmd_addr() const { return _cmd; }
67 size_t cmd_len() const { return _cmd_len; }
68 bool is_empty() const { return _cmd_len == 0; }
69 bool is_executable() const { return is_empty() || _cmd[0] != '#'; }
70 bool is_stop() const { return !is_empty() && strncmp("stop", _cmd, _cmd_len) == 0; }
71 };
72
73 // Iterator class taking a character string in input and returning a CmdLine
74 // instance for each command line. The argument delimiter has to be specified.
75 class DCmdIter : public StackObj {
76 friend class DCmd;
77 private:
78 const char* const _str;
79 const char _delim;
80 const size_t _len;
81 size_t _cursor;
82 public:
83
84 DCmdIter(const char* str, char delim)
85 : _str(str)
86 , _delim(delim)
87 , _len(::strlen(str))
88 , _cursor(0) {}
89 bool has_next() const { return _cursor < _len; }
90 CmdLine next() {
91 assert(_cursor <= _len, "Cannot iterate more");
92 size_t n = _cursor;
93 while (n < _len && _str[n] != _delim) n++;
94 CmdLine line(&(_str[_cursor]), n - _cursor, false);
95 _cursor = n + 1;
96 // The default copy constructor of CmdLine is used to return a CmdLine
97 // instance to the caller.
98 return line;
99 }
100 };
101
102 // Iterator class to iterate over diagnostic command arguments
103 class DCmdArgIter : public ResourceObj {
104 const char* const _buffer;
105 const size_t _len;
106 size_t _cursor;
107 const char* _key_addr;
108 size_t _key_len;
109 const char* _value_addr;
110 size_t _value_len;
111 const char _delim;
112 public:
113 DCmdArgIter(const char* buf, size_t len, char delim)
114 : _buffer(buf)
115 , _len(len)
116 , _cursor(0)
117 , _key_addr(NULL)
118 , _key_len(0)
119 , _value_addr(NULL)
120 , _value_len(0)
121 , _delim(delim)
122 {}
123
124 bool next(TRAPS);
125 const char* key_addr() const { return _key_addr; }
126 size_t key_length() const { return _key_len; }
127 const char* value_addr() const { return _value_addr; }
128 size_t value_length() const { return _value_len; }
129 };
130
131 // A DCmdInfo instance provides a description of a diagnostic command. It is
132 // used to export the description to the JMX interface of the framework.
133 class DCmdInfo : public ResourceObj {
134 protected:
135 const char* const _name; /* Name of the diagnostic command */
136 const char* const _description; /* Short description */
137 const char* const _impact; /* Impact on the JVM */
138 const JavaPermission _permission; /* Java Permission required to execute this command if any */
139 const int _num_arguments; /* Number of supported options or arguments */
140 const bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */
141 public:
142 DCmdInfo(const char* name,
143 const char* description,
144 const char* impact,
145 JavaPermission permission,
146 int num_arguments,
147 bool enabled)
148 : _name(name)
149 , _description(description)
150 , _impact(impact)
151 , _permission(permission)
152 , _num_arguments(num_arguments)
153 , _is_enabled(enabled) {}
154 const char* name() const { return _name; }
155 const char* description() const { return _description; }
156 const char* impact() const { return _impact; }
157 const JavaPermission& permission() const { return _permission; }
158 int num_arguments() const { return _num_arguments; }
159 bool is_enabled() const { return _is_enabled; }
160
161 static bool by_name(void* name, DCmdInfo* info);
162 };
163
164 // A DCmdArgumentInfo instance provides a description of a diagnostic command
165 // argument. It is used to export the description to the JMX interface of the
166 // framework.
167 class DCmdArgumentInfo : public ResourceObj {
168 protected:
169 const char* const _name; /* Option/Argument name*/
170 const char* const _description; /* Short description */
171 const char* const _type; /* Type: STRING, BOOLEAN, etc. */
172 const char* const _default_string; /* Default value in a parsable string */
173 const bool _mandatory; /* True if the option/argument is mandatory */
174 const bool _option; /* True if it is an option, false if it is an argument */
175 /* (see diagnosticFramework.hpp for option/argument definitions) */
176 const bool _multiple; /* True is the option can be specified several time */
177 const int _position; /* Expected position for this argument (this field is */
178 /* meaningless for options) */
179 public:
180 DCmdArgumentInfo(const char* name, const char* description, const char* type,
181 const char* default_string, bool mandatory, bool option,
182 bool multiple, int position = -1)
183 : _name(name)
184 , _description(description)
185 , _type(type)
186 , _default_string(default_string)
187 , _mandatory(mandatory)
188 , _option(option)
189 , _multiple(multiple)
190 , _position(position) {}
191
192 const char* name() const { return _name; }
193 const char* description() const { return _description; }
194 const char* type() const { return _type; }
195 const char* default_string() const { return _default_string; }
196 bool is_mandatory() const { return _mandatory; }
197 bool is_option() const { return _option; }
198 bool is_multiple() const { return _multiple; }
199 int position() const { return _position; }
200 };
201
202 // The DCmdParser class can be used to create an argument parser for a
203 // diagnostic command. It is not mandatory to use it to parse arguments.
204 // The DCmdParser parses a CmdLine instance according to the parameters that
205 // have been declared by its associated diagnostic command. A parameter can
206 // either be an option or an argument. Options are identified by the option name
207 // while arguments are identified by their position in the command line. The
208 // position of an argument is defined relative to all arguments passed on the
209 // command line, options are not considered when defining an argument position.
210 // The generic syntax of a diagnostic command is:
211 //
212 // <command name> [<option>=<value>] [<argument_value>]
213 //
214 // Example:
215 //
216 // command_name option1=value1 option2=value argumentA argumentB argumentC
217 //
218 // In this command line, the diagnostic command receives five parameters, two
219 // options named option1 and option2, and three arguments. argumentA's position
220 // is 0, argumentB's position is 1 and argumentC's position is 2.
221 class DCmdParser {
222 private:
223 GenDCmdArgument* _options;
224 GenDCmdArgument* _arguments_list;
225 public:
226 DCmdParser()
227 : _options(NULL)
228 , _arguments_list(NULL) {}
229 void add_dcmd_option(GenDCmdArgument* arg);
230 void add_dcmd_argument(GenDCmdArgument* arg);
231 GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);
232 GenDCmdArgument* arguments_list() const { return _arguments_list; };
233 void check(TRAPS);
234 void parse(CmdLine* line, char delim, TRAPS);
235 void print_help(outputStream* out, const char* cmd_name) const;
236 void reset(TRAPS);
237 void cleanup();
238 int num_arguments() const;
239 GrowableArray<const char*>* argument_name_array() const;
240 GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;
241 };
242
243 // The DCmd class is the parent class of all diagnostic commands
244 // Diagnostic command instances should not be instantiated directly but
245 // created using the associated factory. The factory can be retrieved with
246 // the DCmdFactory::getFactory() method.
247 // A diagnostic command instance can either be allocated in the resource Area
248 // or in the C-heap. Allocation in the resource area is recommended when the
249 // current thread is the only one which will access the diagnostic command
250 // instance. Allocation in the C-heap is required when the diagnostic command
251 // is accessed by several threads (for instance to perform asynchronous
252 // execution).
253 // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for
254 // each diagnostic command instance. In case of a C-heap allocated diagnostic
255 // command instance, the DCmdMark must be created in the context of the last
256 // thread that will access the instance.
257 class DCmd : public ResourceObj {
258 protected:
259 outputStream* const _output;
260 const bool _is_heap_allocated;
261 public:
262 DCmd(outputStream* output, bool heap_allocated)
263 : _output(output)
264 , _is_heap_allocated(heap_allocated) {}
265
266 // Child classes: please always provide these methods:
267 // static const char* name() { return "<command name>";}
268 // static const char* description() { return "<command help>";}
269
270 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
271
272 // The impact() method returns a description of the intrusiveness of the diagnostic
273 // command on the Java Virtual Machine behavior. The rational for this method is that some
274 // diagnostic commands can seriously disrupt the behavior of the Java Virtual Machine
275 // (for instance a Thread Dump for an application with several tens of thousands of threads,
276 // or a Head Dump with a 40GB+ heap size) and other diagnostic commands have no serious
277 // impact on the JVM (for instance, getting the command line arguments or the JVM version).
278 // The recommended format for the description is <impact level>: [longer description],
279 // where the impact level is selected among this list: {Low, Medium, High}. The optional
280 // longer description can provide more specific details like the fact that Thread Dump
281 // impact depends on the heap size.
282 static const char* impact() { return "Low: No impact"; }
283
284 // The permission() method returns the description of Java Permission. This
285 // permission is required when the diagnostic command is invoked via the
286 // DiagnosticCommandMBean. The rationale for this permission check is that
287 // the DiagnosticCommandMBean can be used to perform remote invocations of
288 // diagnostic commands through the PlatformMBeanServer. The (optional) Java
289 // Permission associated with each diagnostic command should ease the work
290 // of system administrators to write policy files granting permissions to
291 // execute diagnostic commands to remote users. Any diagnostic command with
292 // a potential impact on security should overwrite this method.
293 static const JavaPermission permission() {
294 JavaPermission p = {NULL, NULL, NULL};
295 return p;
296 }
297 static int num_arguments() { return 0; }
298 outputStream* output() const { return _output; }
299 bool is_heap_allocated() const { return _is_heap_allocated; }
300 virtual void print_help(const char* name) const {
301 output()->print_cr("Syntax: %s", name);
302 }
303 virtual void parse(CmdLine* line, char delim, TRAPS) {
315 // support for the JMX interface
316 virtual GrowableArray<const char*>* argument_name_array() const {
317 GrowableArray<const char*>* array = new GrowableArray<const char*>(0);
318 return array;
319 }
320 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const {
321 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);
322 return array;
323 }
324
325 // main method to invoke the framework
326 static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline,
327 char delim, TRAPS);
328 };
329
330 class DCmdWithParser : public DCmd {
331 protected:
332 DCmdParser _dcmdparser;
333 public:
334 DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { }
335 static const char* disabled_message() { return "Diagnostic command currently disabled"; }
336 static const char* impact() { return "Low: No impact"; }
337 virtual void parse(CmdLine *line, char delim, TRAPS);
338 virtual void execute(DCmdSource source, TRAPS) { }
339 virtual void reset(TRAPS);
340 virtual void cleanup();
341 virtual void print_help(const char* name) const;
342 virtual GrowableArray<const char*>* argument_name_array() const;
343 virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const;
344 };
345
346 class DCmdMark : public StackObj {
347 DCmd* const _ref;
348 public:
349 DCmdMark(DCmd* cmd) : _ref(cmd) {}
350 ~DCmdMark() {
351 if (_ref != NULL) {
352 _ref->cleanup();
353 if (_ref->is_heap_allocated()) {
354 delete _ref;
355 }
356 }
357 }
358 };
359
360 // Diagnostic commands are not directly instantiated but created with a factory.
361 // Each diagnostic command class has its own factory. The DCmdFactory class also
362 // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory
363 // has to be registered to make the diagnostic command available (see
364 // management.cpp)
365 class DCmdFactory: public CHeapObj<mtInternal> {
366 private:
367 static Mutex* _dcmdFactory_lock;
368 static bool _send_jmx_notification;
369 static bool _has_pending_jmx_notification;
370 static DCmdFactory* _DCmdFactoryList;
371
372 // Pointer to the next factory in the singly-linked list of registered
373 // diagnostic commands
374 DCmdFactory* _next;
375 // When disabled, a diagnostic command cannot be executed. Any attempt to
376 // execute it will result in the printing of the disabled message without
377 // instantiating the command.
378 const bool _enabled;
379 // When hidden, a diagnostic command doesn't appear in the list of commands
380 // provided by the 'help' command.
381 const bool _hidden;
382 const uint32_t _export_flags;
383 const int _num_arguments;
384
385 public:
386 DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden)
387 : _next(NULL)
388 , _enabled(enabled)
389 , _hidden(hidden)
390 , _export_flags(flags)
391 , _num_arguments(num_arguments) {}
392 bool is_enabled() const { return _enabled; }
393 bool is_hidden() const { return _hidden; }
394 uint32_t export_flags() const { return _export_flags; }
395 int num_arguments() const { return _num_arguments; }
396 DCmdFactory* next() const { return _next; }
397 virtual DCmd* create_resource_instance(outputStream* output) const = 0;
398 virtual const char* name() const = 0;
399 virtual const char* description() const = 0;
400 virtual const char* impact() const = 0;
401 virtual const JavaPermission permission() const = 0;
402 virtual const char* disabled_message() const = 0;
403 // Register a DCmdFactory to make a diagnostic command available.
404 // Once registered, a diagnostic command must not be unregistered.
405 // To prevent a diagnostic command from being executed, just set the
406 // enabled flag to false.
407 static int register_DCmdFactory(DCmdFactory* factory);
408 static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len);
409 // Returns a resourceArea allocated diagnostic command for the given command line
410 static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS);
411 static GrowableArray<const char*>* DCmd_list(DCmdSource source);
413
414 static void set_jmx_notification_enabled(bool enabled) {
415 _send_jmx_notification = enabled;
416 }
417 static void push_jmx_notification_request();
418 static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; }
419 static void send_notification(TRAPS);
420 private:
421 static void send_notification_internal(TRAPS);
422
423 friend class HelpDCmd;
424 };
425
426 // Template to easily create DCmdFactory instances. See management.cpp
427 // where this template is used to create and register factories.
428 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {
429 public:
430 DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) :
431 DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { }
432 // Returns a resourceArea allocated instance
433 DCmd* create_resource_instance(outputStream* output) const {
434 return new DCmdClass(output, false);
435 }
436 const char* name() const {
437 return DCmdClass::name();
438 }
439 const char* description() const {
440 return DCmdClass::description();
441 }
442 const char* impact() const {
443 return DCmdClass::impact();
444 }
445 const JavaPermission permission() const {
446 return DCmdClass::permission();
447 }
448 const char* disabled_message() const {
449 return DCmdClass::disabled_message();
450 }
451 };
452
453 // This class provides a convenient way to register Dcmds, without a need to change
454 // management.cpp every time. Body of these two methods resides in
455 // diagnosticCommand.cpp
456
457 class DCmdRegistrant : public AllStatic {
458
459 private:
460 static void register_dcmds();
461 static void register_dcmds_ext();
462
463 friend class Management;
464 };
465
466 #endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
|