Sun, 03 Feb 2013 22:28:08 +0400
8002048: Protocol to discovery of manageable Java processes on a network
Summary: Introduce a protocol to discover manageble Java instances across a network subnet, JDP
Reviewed-by: sla, dfuchs
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 */
25 #include "precompiled.hpp"
26 #include "memory/oopFactory.hpp"
27 #include "runtime/javaCalls.hpp"
28 #include "runtime/mutexLocker.hpp"
29 #include "services/diagnosticArgument.hpp"
30 #include "services/diagnosticFramework.hpp"
31 #include "services/management.hpp"
33 CmdLine::CmdLine(const char* line, size_t len, bool no_command_name) {
34 assert(line != NULL, "Command line string should not be NULL");
35 const char* line_end;
36 const char* cmd_end;
38 _cmd = line;
39 line_end = &line[len];
41 // Skip whitespace in the beginning of the line.
42 while (_cmd < line_end && isspace((int) _cmd[0])) {
43 _cmd++;
44 }
45 cmd_end = _cmd;
47 if (no_command_name) {
48 _cmd = NULL;
49 _cmd_len = 0;
50 } else {
51 // Look for end of the command name
52 while (cmd_end < line_end && !isspace((int) cmd_end[0])) {
53 cmd_end++;
54 }
55 _cmd_len = cmd_end - _cmd;
56 }
57 _args = cmd_end;
58 _args_len = line_end - _args;
59 }
61 bool DCmdArgIter::next(TRAPS) {
62 if (_len == 0) return false;
63 // skipping spaces
64 while (_cursor < _len - 1 && _buffer[_cursor] == _delim) {
65 _cursor++;
66 }
67 // handling end of command line
68 if (_cursor >= _len - 1) {
69 _cursor = _len - 1;
70 _key_addr = &_buffer[_len - 1];
71 _key_len = 0;
72 _value_addr = &_buffer[_len - 1];
73 _value_len = 0;
74 return false;
75 }
76 // extracting first item, argument or option name
77 _key_addr = &_buffer[_cursor];
78 bool arg_had_quotes = false;
79 while (_cursor <= _len - 1 && _buffer[_cursor] != '=' && _buffer[_cursor] != _delim) {
80 // argument can be surrounded by single or double quotes
81 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
82 _key_addr++;
83 char quote = _buffer[_cursor];
84 arg_had_quotes = true;
85 while (_cursor < _len - 1) {
86 _cursor++;
87 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
88 break;
89 }
90 }
91 if (_buffer[_cursor] != quote) {
92 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
93 "Format error in diagnostic command arguments", false);
94 }
95 break;
96 }
97 _cursor++;
98 }
99 _key_len = &_buffer[_cursor] - _key_addr;
100 if (arg_had_quotes) {
101 // if the argument was quoted, we need to step past the last quote here
102 _cursor++;
103 }
104 // check if the argument has the <key>=<value> format
105 if (_cursor <= _len -1 && _buffer[_cursor] == '=') {
106 _cursor++;
107 _value_addr = &_buffer[_cursor];
108 bool value_had_quotes = false;
109 // extract the value
110 while (_cursor <= _len - 1 && _buffer[_cursor] != _delim) {
111 // value can be surrounded by simple or double quotes
112 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
113 _value_addr++;
114 char quote = _buffer[_cursor];
115 value_had_quotes = true;
116 while (_cursor < _len - 1) {
117 _cursor++;
118 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
119 break;
120 }
121 }
122 if (_buffer[_cursor] != quote) {
123 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
124 "Format error in diagnostic command arguments", false);
125 }
126 break;
127 }
128 _cursor++;
129 }
130 _value_len = &_buffer[_cursor] - _value_addr;
131 if (value_had_quotes) {
132 // if the value was quoted, we need to step past the last quote here
133 _cursor++;
134 }
135 } else {
136 _value_addr = NULL;
137 _value_len = 0;
138 }
139 return _key_len != 0;
140 }
142 bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) {
143 if (info == NULL) return false;
144 return strcmp((const char*)cmd_name, info->name()) == 0;
145 }
147 void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) {
148 assert(arg != NULL, "Sanity");
149 if (_options == NULL) {
150 _options = arg;
151 } else {
152 GenDCmdArgument* o = _options;
153 while (o->next() != NULL) {
154 o = o->next();
155 }
156 o->set_next(arg);
157 }
158 arg->set_next(NULL);
159 Thread* THREAD = Thread::current();
160 arg->init_value(THREAD);
161 if (HAS_PENDING_EXCEPTION) {
162 fatal("Initialization must be successful");
163 }
164 }
166 void DCmdParser::add_dcmd_argument(GenDCmdArgument* arg) {
167 assert(arg != NULL, "Sanity");
168 if (_arguments_list == NULL) {
169 _arguments_list = arg;
170 } else {
171 GenDCmdArgument* a = _arguments_list;
172 while (a->next() != NULL) {
173 a = a->next();
174 }
175 a->set_next(arg);
176 }
177 arg->set_next(NULL);
178 Thread* THREAD = Thread::current();
179 arg->init_value(THREAD);
180 if (HAS_PENDING_EXCEPTION) {
181 fatal("Initialization must be successful");
182 }
183 }
185 void DCmdParser::parse(CmdLine* line, char delim, TRAPS) {
186 GenDCmdArgument* next_argument = _arguments_list;
187 DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
188 bool cont = iter.next(CHECK);
189 while (cont) {
190 GenDCmdArgument* arg = lookup_dcmd_option(iter.key_addr(),
191 iter.key_length());
192 if (arg != NULL) {
193 arg->read_value(iter.value_addr(), iter.value_length(), CHECK);
194 } else {
195 if (next_argument != NULL) {
196 arg = next_argument;
197 arg->read_value(iter.key_addr(), iter.key_length(), CHECK);
198 next_argument = next_argument->next();
199 } else {
200 const size_t buflen = 120;
201 const size_t argbuflen = 30;
202 char buf[buflen];
203 char argbuf[argbuflen];
204 size_t len = MIN2<size_t>(iter.key_length(), argbuflen - 1);
206 strncpy(argbuf, iter.key_addr(), len);
207 argbuf[len] = '\0';
208 jio_snprintf(buf, buflen - 1, "Unknown argument '%s' in diagnostic command.", argbuf);
210 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
211 }
212 }
213 cont = iter.next(CHECK);
214 }
215 check(CHECK);
216 }
218 GenDCmdArgument* DCmdParser::lookup_dcmd_option(const char* name, size_t len) {
219 GenDCmdArgument* arg = _options;
220 while (arg != NULL) {
221 if (strlen(arg->name()) == len &&
222 strncmp(name, arg->name(), len) == 0) {
223 return arg;
224 }
225 arg = arg->next();
226 }
227 return NULL;
228 }
230 void DCmdParser::check(TRAPS) {
231 const size_t buflen = 256;
232 char buf[buflen];
233 GenDCmdArgument* arg = _arguments_list;
234 while (arg != NULL) {
235 if (arg->is_mandatory() && !arg->has_value()) {
236 jio_snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name());
237 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
238 }
239 arg = arg->next();
240 }
241 arg = _options;
242 while (arg != NULL) {
243 if (arg->is_mandatory() && !arg->has_value()) {
244 jio_snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name());
245 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
246 }
247 arg = arg->next();
248 }
249 }
251 void DCmdParser::print_help(outputStream* out, const char* cmd_name) {
252 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
253 GenDCmdArgument* arg = _arguments_list;
254 while (arg != NULL) {
255 if (arg->is_mandatory()) {
256 out->print(" <%s>", arg->name());
257 } else {
258 out->print(" [<%s>]", arg->name());
259 }
260 arg = arg->next();
261 }
262 out->print_cr("");
263 if (_arguments_list != NULL) {
264 out->print_cr("\nArguments:");
265 arg = _arguments_list;
266 while (arg != NULL) {
267 out->print("\t%s : %s %s (%s, ", arg->name(),
268 arg->is_mandatory() ? "" : "[optional]",
269 arg->description(), arg->type());
270 if (arg->has_default()) {
271 out->print(arg->default_string());
272 } else {
273 out->print("no default value");
274 }
275 out->print_cr(")");
276 arg = arg->next();
277 }
278 }
279 if (_options != NULL) {
280 out->print_cr("\nOptions: (options must be specified using the <key> or <key>=<value> syntax)");
281 arg = _options;
282 while (arg != NULL) {
283 out->print("\t%s : %s %s (%s, ", arg->name(),
284 arg->is_mandatory() ? "" : "[optional]",
285 arg->description(), arg->type());
286 if (arg->has_default()) {
287 out->print(arg->default_string());
288 } else {
289 out->print("no default value");
290 }
291 out->print_cr(")");
292 arg = arg->next();
293 }
294 }
295 }
297 void DCmdParser::reset(TRAPS) {
298 GenDCmdArgument* arg = _arguments_list;
299 while (arg != NULL) {
300 arg->reset(CHECK);
301 arg = arg->next();
302 }
303 arg = _options;
304 while (arg != NULL) {
305 arg->reset(CHECK);
306 arg = arg->next();
307 }
308 }
310 void DCmdParser::cleanup() {
311 GenDCmdArgument* arg = _arguments_list;
312 while (arg != NULL) {
313 arg->cleanup();
314 arg = arg->next();
315 }
316 arg = _options;
317 while (arg != NULL) {
318 arg->cleanup();
319 arg = arg->next();
320 }
321 }
323 int DCmdParser::num_arguments() {
324 GenDCmdArgument* arg = _arguments_list;
325 int count = 0;
326 while (arg != NULL) {
327 count++;
328 arg = arg->next();
329 }
330 arg = _options;
331 while (arg != NULL) {
332 count++;
333 arg = arg->next();
334 }
335 return count;
336 }
338 GrowableArray<const char *>* DCmdParser::argument_name_array() {
339 int count = num_arguments();
340 GrowableArray<const char *>* array = new GrowableArray<const char *>(count);
341 GenDCmdArgument* arg = _arguments_list;
342 while (arg != NULL) {
343 array->append(arg->name());
344 arg = arg->next();
345 }
346 arg = _options;
347 while (arg != NULL) {
348 array->append(arg->name());
349 arg = arg->next();
350 }
351 return array;
352 }
354 GrowableArray<DCmdArgumentInfo*>* DCmdParser::argument_info_array() {
355 int count = num_arguments();
356 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo *>(count);
357 int idx = 0;
358 GenDCmdArgument* arg = _arguments_list;
359 while (arg != NULL) {
360 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
361 arg->type(), arg->default_string(), arg->is_mandatory(),
362 false, idx));
363 idx++;
364 arg = arg->next();
365 }
366 arg = _options;
367 while (arg != NULL) {
368 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
369 arg->type(), arg->default_string(), arg->is_mandatory(),
370 true));
371 arg = arg->next();
372 }
373 return array;
374 }
376 DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL;
378 void DCmd::parse_and_execute(outputStream* out, const char* cmdline,
379 char delim, TRAPS) {
381 if (cmdline == NULL) return; // Nothing to do!
382 DCmdIter iter(cmdline, '\n');
384 while (iter.has_next()) {
385 CmdLine line = iter.next();
386 if (line.is_stop()) {
387 break;
388 }
389 if (line.is_executable()) {
390 DCmd* command = DCmdFactory::create_local_DCmd(line, out, CHECK);
391 assert(command != NULL, "command error must be handled before this line");
392 DCmdMark mark(command);
393 command->parse(&line, delim, CHECK);
394 command->execute(CHECK);
395 }
396 }
397 }
399 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) {
400 _dcmdparser.parse(line, delim, CHECK);
401 }
403 void DCmdWithParser::print_help(const char* name) {
404 _dcmdparser.print_help(output(), name);
405 }
407 void DCmdWithParser::reset(TRAPS) {
408 _dcmdparser.reset(CHECK);
409 }
411 void DCmdWithParser::cleanup() {
412 _dcmdparser.cleanup();
413 }
415 GrowableArray<const char*>* DCmdWithParser::argument_name_array() {
416 return _dcmdparser.argument_name_array();
417 }
419 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() {
420 return _dcmdparser.argument_info_array();
421 }
423 Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf, "DCmdFactory", true);
425 DCmdFactory* DCmdFactory::factory(const char* name, size_t len) {
426 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
427 DCmdFactory* factory = _DCmdFactoryList;
428 while (factory != NULL) {
429 if (strlen(factory->name()) == len &&
430 strncmp(name, factory->name(), len) == 0) {
431 return factory;
432 }
433 factory = factory->_next;
434 }
435 return NULL;
436 }
438 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) {
439 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
440 factory->_next = _DCmdFactoryList;
441 _DCmdFactoryList = factory;
442 return 0; // Actually, there's no checks for duplicates
443 }
445 DCmd* DCmdFactory::create_global_DCmd(CmdLine &line, outputStream* out, TRAPS) {
446 DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len());
447 if (f != NULL) {
448 if (f->is_enabled()) {
449 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
450 f->disabled_message());
451 }
452 return f->create_Cheap_instance(out);
453 }
454 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
455 "Unknown diagnostic command");
456 }
458 DCmd* DCmdFactory::create_local_DCmd(CmdLine &line, outputStream* out, TRAPS) {
459 DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len());
460 if (f != NULL) {
461 if (!f->is_enabled()) {
462 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
463 f->disabled_message());
464 }
465 return f->create_resource_instance(out);
466 }
467 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
468 "Unknown diagnostic command");
469 }
471 GrowableArray<const char*>* DCmdFactory::DCmd_list() {
472 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
473 GrowableArray<const char*>* array = new GrowableArray<const char*>();
474 DCmdFactory* factory = _DCmdFactoryList;
475 while (factory != NULL) {
476 if (!factory->is_hidden()) {
477 array->append(factory->name());
478 }
479 factory = factory->next();
480 }
481 return array;
482 }
484 GrowableArray<DCmdInfo*>* DCmdFactory::DCmdInfo_list() {
485 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
486 GrowableArray<DCmdInfo*>* array = new GrowableArray<DCmdInfo*>();
487 DCmdFactory* factory = _DCmdFactoryList;
488 while (factory != NULL) {
489 if (!factory->is_hidden()) {
490 array->append(new DCmdInfo(factory->name(),
491 factory->description(), factory->impact(),
492 factory->num_arguments(), factory->is_enabled()));
493 }
494 factory = factory->next();
495 }
496 return array;
497 }