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