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