1 | /* |
2 | * Copyright (c) 2011, 2019, 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_SERVICES_DIAGNOSTICFRAMEWORK_HPP |
26 | #define SHARE_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/vmThread.hpp" |
33 | #include "utilities/ostream.hpp" |
34 | |
35 | |
36 | enum DCmdSource { |
37 | DCmd_Source_Internal = 0x01U, // invocation from the JVM |
38 | DCmd_Source_AttachAPI = 0x02U, // invocation via the attachAPI |
39 | DCmd_Source_MBean = 0x04U // invocation via a MBean |
40 | }; |
41 | |
42 | // Warning: strings referenced by the JavaPermission struct are passed to |
43 | // the native part of the JDK. Avoid use of dynamically allocated strings |
44 | // that could be de-allocated before the JDK native code had time to |
45 | // convert them into Java Strings. |
46 | struct JavaPermission { |
47 | const char* _class; |
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), _delim(delim), _len(::strlen(str)), |
86 | _cursor(0) {} |
87 | bool has_next() const { return _cursor < _len; } |
88 | CmdLine next() { |
89 | assert(_cursor <= _len, "Cannot iterate more" ); |
90 | size_t n = _cursor; |
91 | while (n < _len && _str[n] != _delim) n++; |
92 | CmdLine line(&(_str[_cursor]), n - _cursor, false); |
93 | _cursor = n + 1; |
94 | // The default copy constructor of CmdLine is used to return a CmdLine |
95 | // instance to the caller. |
96 | return line; |
97 | } |
98 | }; |
99 | |
100 | // Iterator class to iterate over diagnostic command arguments |
101 | class DCmdArgIter : public ResourceObj { |
102 | const char* const _buffer; |
103 | const size_t _len; |
104 | size_t _cursor; |
105 | const char* _key_addr; |
106 | size_t _key_len; |
107 | const char* _value_addr; |
108 | size_t _value_len; |
109 | const char _delim; |
110 | public: |
111 | DCmdArgIter(const char* buf, size_t len, char delim) |
112 | : _buffer(buf), _len(len), _cursor(0), _key_addr(NULL), |
113 | _key_len(0), _value_addr(NULL), _value_len(0), _delim(delim) {} |
114 | |
115 | bool next(TRAPS); |
116 | const char* key_addr() const { return _key_addr; } |
117 | size_t key_length() const { return _key_len; } |
118 | const char* value_addr() const { return _value_addr; } |
119 | size_t value_length() const { return _value_len; } |
120 | }; |
121 | |
122 | // A DCmdInfo instance provides a description of a diagnostic command. It is |
123 | // used to export the description to the JMX interface of the framework. |
124 | class DCmdInfo : public ResourceObj { |
125 | protected: |
126 | const char* const _name; /* Name of the diagnostic command */ |
127 | const char* const _description; /* Short description */ |
128 | const char* const _impact; /* Impact on the JVM */ |
129 | const JavaPermission _permission; /* Java Permission required to execute this command if any */ |
130 | const int _num_arguments; /* Number of supported options or arguments */ |
131 | const bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */ |
132 | public: |
133 | DCmdInfo(const char* name, |
134 | const char* description, |
135 | const char* impact, |
136 | JavaPermission permission, |
137 | int num_arguments, |
138 | bool enabled) |
139 | : _name(name), _description(description), _impact(impact), _permission(permission), |
140 | _num_arguments(num_arguments), _is_enabled(enabled) {} |
141 | const char* name() const { return _name; } |
142 | const char* description() const { return _description; } |
143 | const char* impact() const { return _impact; } |
144 | const JavaPermission& permission() const { return _permission; } |
145 | int num_arguments() const { return _num_arguments; } |
146 | bool is_enabled() const { return _is_enabled; } |
147 | |
148 | static bool by_name(void* name, DCmdInfo* info); |
149 | }; |
150 | |
151 | // A DCmdArgumentInfo instance provides a description of a diagnostic command |
152 | // argument. It is used to export the description to the JMX interface of the |
153 | // framework. |
154 | class DCmdArgumentInfo : public ResourceObj { |
155 | protected: |
156 | const char* const _name; /* Option/Argument name*/ |
157 | const char* const _description; /* Short description */ |
158 | const char* const _type; /* Type: STRING, BOOLEAN, etc. */ |
159 | const char* const _default_string; /* Default value in a parsable string */ |
160 | const bool _mandatory; /* True if the option/argument is mandatory */ |
161 | const bool _option; /* True if it is an option, false if it is an argument */ |
162 | /* (see diagnosticFramework.hpp for option/argument definitions) */ |
163 | const bool _multiple; /* True is the option can be specified several time */ |
164 | const int _position; /* Expected position for this argument (this field is */ |
165 | /* meaningless for options) */ |
166 | public: |
167 | DCmdArgumentInfo(const char* name, const char* description, const char* type, |
168 | const char* default_string, bool mandatory, bool option, |
169 | bool multiple, int position = -1) |
170 | : _name(name), _description(description), _type(type), |
171 | _default_string(default_string), _mandatory(mandatory), _option(option), |
172 | _multiple(multiple), _position(position) {} |
173 | |
174 | const char* name() const { return _name; } |
175 | const char* description() const { return _description; } |
176 | const char* type() const { return _type; } |
177 | const char* default_string() const { return _default_string; } |
178 | bool is_mandatory() const { return _mandatory; } |
179 | bool is_option() const { return _option; } |
180 | bool is_multiple() const { return _multiple; } |
181 | int position() const { return _position; } |
182 | }; |
183 | |
184 | // The DCmdParser class can be used to create an argument parser for a |
185 | // diagnostic command. It is not mandatory to use it to parse arguments. |
186 | // The DCmdParser parses a CmdLine instance according to the parameters that |
187 | // have been declared by its associated diagnostic command. A parameter can |
188 | // either be an option or an argument. Options are identified by the option name |
189 | // while arguments are identified by their position in the command line. The |
190 | // position of an argument is defined relative to all arguments passed on the |
191 | // command line, options are not considered when defining an argument position. |
192 | // The generic syntax of a diagnostic command is: |
193 | // |
194 | // <command name> [<option>=<value>] [<argument_value>] |
195 | // |
196 | // Example: |
197 | // |
198 | // command_name option1=value1 option2=value argumentA argumentB argumentC |
199 | // |
200 | // In this command line, the diagnostic command receives five parameters, two |
201 | // options named option1 and option2, and three arguments. argumentA's position |
202 | // is 0, argumentB's position is 1 and argumentC's position is 2. |
203 | class DCmdParser { |
204 | private: |
205 | GenDCmdArgument* _options; |
206 | GenDCmdArgument* _arguments_list; |
207 | public: |
208 | DCmdParser() |
209 | : _options(NULL), _arguments_list(NULL) {} |
210 | void add_dcmd_option(GenDCmdArgument* arg); |
211 | void add_dcmd_argument(GenDCmdArgument* arg); |
212 | GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len); |
213 | GenDCmdArgument* arguments_list() const { return _arguments_list; }; |
214 | void check(TRAPS); |
215 | void parse(CmdLine* line, char delim, TRAPS); |
216 | void print_help(outputStream* out, const char* cmd_name) const; |
217 | void reset(TRAPS); |
218 | void cleanup(); |
219 | int num_arguments() const; |
220 | GrowableArray<const char*>* argument_name_array() const; |
221 | GrowableArray<DCmdArgumentInfo*>* argument_info_array() const; |
222 | }; |
223 | |
224 | // The DCmd class is the parent class of all diagnostic commands |
225 | // Diagnostic command instances should not be instantiated directly but |
226 | // created using the associated factory. The factory can be retrieved with |
227 | // the DCmdFactory::getFactory() method. |
228 | // A diagnostic command instance can either be allocated in the resource Area |
229 | // or in the C-heap. Allocation in the resource area is recommended when the |
230 | // current thread is the only one which will access the diagnostic command |
231 | // instance. Allocation in the C-heap is required when the diagnostic command |
232 | // is accessed by several threads (for instance to perform asynchronous |
233 | // execution). |
234 | // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for |
235 | // each diagnostic command instance. In case of a C-heap allocated diagnostic |
236 | // command instance, the DCmdMark must be created in the context of the last |
237 | // thread that will access the instance. |
238 | class DCmd : public ResourceObj { |
239 | protected: |
240 | outputStream* const _output; |
241 | const bool _is_heap_allocated; |
242 | public: |
243 | DCmd(outputStream* output, bool heap_allocated) |
244 | : _output(output), _is_heap_allocated(heap_allocated) {} |
245 | |
246 | // Child classes: please always provide these methods: |
247 | // static const char* name() { return "<command name>";} |
248 | // static const char* description() { return "<command help>";} |
249 | |
250 | static const char* disabled_message() { return "Diagnostic command currently disabled" ; } |
251 | |
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 | |
264 | // The permission() method returns the description of Java Permission. This |
265 | // permission is required when the diagnostic command is invoked via the |
266 | // DiagnosticCommandMBean. The rationale for this permission check is that |
267 | // the DiagnosticCommandMBean can be used to perform remote invocations of |
268 | // diagnostic commands through the PlatformMBeanServer. The (optional) Java |
269 | // Permission associated with each diagnostic command should ease the work |
270 | // of system administrators to write policy files granting permissions to |
271 | // execute diagnostic commands to remote users. Any diagnostic command with |
272 | // a potential impact on security should overwrite this method. |
273 | static const JavaPermission permission() { |
274 | JavaPermission p = {NULL, NULL, NULL}; |
275 | return p; |
276 | } |
277 | static int num_arguments() { return 0; } |
278 | outputStream* output() const { return _output; } |
279 | bool is_heap_allocated() const { return _is_heap_allocated; } |
280 | virtual void print_help(const char* name) const { |
281 | output()->print_cr("Syntax: %s" , name); |
282 | } |
283 | virtual void parse(CmdLine* line, char delim, TRAPS) { |
284 | DCmdArgIter iter(line->args_addr(), line->args_len(), delim); |
285 | bool has_arg = iter.next(CHECK); |
286 | if (has_arg) { |
287 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
288 | "The argument list of this diagnostic command should be empty." ); |
289 | } |
290 | } |
291 | virtual void execute(DCmdSource source, TRAPS) { } |
292 | virtual void reset(TRAPS) { } |
293 | virtual void cleanup() { } |
294 | |
295 | // support for the JMX interface |
296 | virtual GrowableArray<const char*>* argument_name_array() const { |
297 | GrowableArray<const char*>* array = new GrowableArray<const char*>(0); |
298 | return array; |
299 | } |
300 | virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const { |
301 | GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0); |
302 | return array; |
303 | } |
304 | |
305 | // main method to invoke the framework |
306 | static void parse_and_execute(DCmdSource source, outputStream* out, const char* cmdline, |
307 | char delim, TRAPS); |
308 | }; |
309 | |
310 | class DCmdWithParser : public DCmd { |
311 | protected: |
312 | DCmdParser _dcmdparser; |
313 | public: |
314 | DCmdWithParser (outputStream *output, bool heap=false) : DCmd(output, heap) { } |
315 | static const char* disabled_message() { return "Diagnostic command currently disabled" ; } |
316 | static const char* impact() { return "Low: No impact" ; } |
317 | virtual void parse(CmdLine *line, char delim, TRAPS); |
318 | virtual void execute(DCmdSource source, TRAPS) { } |
319 | virtual void reset(TRAPS); |
320 | virtual void cleanup(); |
321 | virtual void print_help(const char* name) const; |
322 | virtual GrowableArray<const char*>* argument_name_array() const; |
323 | virtual GrowableArray<DCmdArgumentInfo*>* argument_info_array() const; |
324 | }; |
325 | |
326 | class DCmdMark : public StackObj { |
327 | DCmd* const _ref; |
328 | public: |
329 | DCmdMark(DCmd* cmd) : _ref(cmd) {} |
330 | ~DCmdMark() { |
331 | if (_ref != NULL) { |
332 | _ref->cleanup(); |
333 | if (_ref->is_heap_allocated()) { |
334 | delete _ref; |
335 | } |
336 | } |
337 | } |
338 | }; |
339 | |
340 | // Diagnostic commands are not directly instantiated but created with a factory. |
341 | // Each diagnostic command class has its own factory. The DCmdFactory class also |
342 | // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory |
343 | // has to be registered to make the diagnostic command available (see |
344 | // management.cpp) |
345 | class DCmdFactory: public CHeapObj<mtInternal> { |
346 | private: |
347 | static bool _send_jmx_notification; |
348 | static bool _has_pending_jmx_notification; |
349 | static DCmdFactory* _DCmdFactoryList; |
350 | |
351 | // Pointer to the next factory in the singly-linked list of registered |
352 | // diagnostic commands |
353 | DCmdFactory* _next; |
354 | // When disabled, a diagnostic command cannot be executed. Any attempt to |
355 | // execute it will result in the printing of the disabled message without |
356 | // instantiating the command. |
357 | const bool _enabled; |
358 | // When hidden, a diagnostic command doesn't appear in the list of commands |
359 | // provided by the 'help' command. |
360 | const bool _hidden; |
361 | const uint32_t _export_flags; |
362 | const int _num_arguments; |
363 | |
364 | public: |
365 | DCmdFactory(int num_arguments, uint32_t flags, bool enabled, bool hidden) |
366 | : _next(NULL), _enabled(enabled), _hidden(hidden), |
367 | _export_flags(flags), _num_arguments(num_arguments) {} |
368 | bool is_enabled() const { return _enabled; } |
369 | bool is_hidden() const { return _hidden; } |
370 | uint32_t export_flags() const { return _export_flags; } |
371 | int num_arguments() const { return _num_arguments; } |
372 | DCmdFactory* next() const { return _next; } |
373 | virtual DCmd* create_resource_instance(outputStream* output) const = 0; |
374 | virtual const char* name() const = 0; |
375 | virtual const char* description() const = 0; |
376 | virtual const char* impact() const = 0; |
377 | virtual const JavaPermission permission() const = 0; |
378 | virtual const char* disabled_message() const = 0; |
379 | // Register a DCmdFactory to make a diagnostic command available. |
380 | // Once registered, a diagnostic command must not be unregistered. |
381 | // To prevent a diagnostic command from being executed, just set the |
382 | // enabled flag to false. |
383 | static int register_DCmdFactory(DCmdFactory* factory); |
384 | static DCmdFactory* factory(DCmdSource source, const char* cmd, size_t len); |
385 | // Returns a resourceArea allocated diagnostic command for the given command line |
386 | static DCmd* create_local_DCmd(DCmdSource source, CmdLine &line, outputStream* out, TRAPS); |
387 | static GrowableArray<const char*>* DCmd_list(DCmdSource source); |
388 | static GrowableArray<DCmdInfo*>* DCmdInfo_list(DCmdSource source); |
389 | |
390 | static void set_jmx_notification_enabled(bool enabled) { |
391 | _send_jmx_notification = enabled; |
392 | } |
393 | static void push_jmx_notification_request(); |
394 | static bool has_pending_jmx_notification() { return _has_pending_jmx_notification; } |
395 | static void send_notification(TRAPS); |
396 | private: |
397 | static void send_notification_internal(TRAPS); |
398 | |
399 | friend class HelpDCmd; |
400 | }; |
401 | |
402 | // Template to easily create DCmdFactory instances. See management.cpp |
403 | // where this template is used to create and register factories. |
404 | template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory { |
405 | public: |
406 | DCmdFactoryImpl(uint32_t flags, bool enabled, bool hidden) : |
407 | DCmdFactory(DCmdClass::num_arguments(), flags, enabled, hidden) { } |
408 | // Returns a resourceArea allocated instance |
409 | DCmd* create_resource_instance(outputStream* output) const { |
410 | return new DCmdClass(output, false); |
411 | } |
412 | const char* name() const { |
413 | return DCmdClass::name(); |
414 | } |
415 | const char* description() const { |
416 | return DCmdClass::description(); |
417 | } |
418 | const char* impact() const { |
419 | return DCmdClass::impact(); |
420 | } |
421 | const JavaPermission permission() const { |
422 | return DCmdClass::permission(); |
423 | } |
424 | const char* disabled_message() const { |
425 | return DCmdClass::disabled_message(); |
426 | } |
427 | }; |
428 | |
429 | // This class provides a convenient way to register Dcmds, without a need to change |
430 | // management.cpp every time. Body of these two methods resides in |
431 | // diagnosticCommand.cpp |
432 | |
433 | class DCmdRegistrant : public AllStatic { |
434 | |
435 | private: |
436 | static void register_dcmds(); |
437 | static void register_dcmds_ext(); |
438 | |
439 | friend class Management; |
440 | }; |
441 | |
442 | #endif // SHARE_SERVICES_DIAGNOSTICFRAMEWORK_HPP |
443 | |