234 const size_t buflen = 256;
235 char buf[buflen];
236 GenDCmdArgument* arg = _arguments_list;
237 while (arg != NULL) {
238 if (arg->is_mandatory() && !arg->has_value()) {
239 jio_snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name());
240 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
241 }
242 arg = arg->next();
243 }
244 arg = _options;
245 while (arg != NULL) {
246 if (arg->is_mandatory() && !arg->has_value()) {
247 jio_snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name());
248 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
249 }
250 arg = arg->next();
251 }
252 }
253
254 void DCmdParser::print_help(outputStream* out, const char* cmd_name) {
255 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
256 GenDCmdArgument* arg = _arguments_list;
257 while (arg != NULL) {
258 if (arg->is_mandatory()) {
259 out->print(" <%s>", arg->name());
260 } else {
261 out->print(" [<%s>]", arg->name());
262 }
263 arg = arg->next();
264 }
265 out->cr();
266 if (_arguments_list != NULL) {
267 out->print_cr("\nArguments:");
268 arg = _arguments_list;
269 while (arg != NULL) {
270 out->print("\t%s : %s %s (%s, ", arg->name(),
271 arg->is_mandatory() ? "" : "[optional]",
272 arg->description(), arg->type());
273 if (arg->has_default()) {
274 out->print("%s", arg->default_string());
306 arg = _options;
307 while (arg != NULL) {
308 arg->reset(CHECK);
309 arg = arg->next();
310 }
311 }
312
313 void DCmdParser::cleanup() {
314 GenDCmdArgument* arg = _arguments_list;
315 while (arg != NULL) {
316 arg->cleanup();
317 arg = arg->next();
318 }
319 arg = _options;
320 while (arg != NULL) {
321 arg->cleanup();
322 arg = arg->next();
323 }
324 }
325
326 int DCmdParser::num_arguments() {
327 GenDCmdArgument* arg = _arguments_list;
328 int count = 0;
329 while (arg != NULL) {
330 count++;
331 arg = arg->next();
332 }
333 arg = _options;
334 while (arg != NULL) {
335 count++;
336 arg = arg->next();
337 }
338 return count;
339 }
340
341 GrowableArray<const char *>* DCmdParser::argument_name_array() {
342 int count = num_arguments();
343 GrowableArray<const char *>* array = new GrowableArray<const char *>(count);
344 GenDCmdArgument* arg = _arguments_list;
345 while (arg != NULL) {
346 array->append(arg->name());
347 arg = arg->next();
348 }
349 arg = _options;
350 while (arg != NULL) {
351 array->append(arg->name());
352 arg = arg->next();
353 }
354 return array;
355 }
356
357 GrowableArray<DCmdArgumentInfo*>* DCmdParser::argument_info_array() {
358 int count = num_arguments();
359 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo *>(count);
360 int idx = 0;
361 GenDCmdArgument* arg = _arguments_list;
362 while (arg != NULL) {
363 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
364 arg->type(), arg->default_string(), arg->is_mandatory(),
365 false, arg->allow_multiple(), idx));
366 idx++;
367 arg = arg->next();
368 }
369 arg = _options;
370 while (arg != NULL) {
371 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
372 arg->type(), arg->default_string(), arg->is_mandatory(),
373 true, arg->allow_multiple()));
374 arg = arg->next();
375 }
376 return array;
377 }
382 void DCmd::parse_and_execute(DCmdSource source, outputStream* out,
383 const char* cmdline, char delim, TRAPS) {
384
385 if (cmdline == NULL) return; // Nothing to do!
386 DCmdIter iter(cmdline, '\n');
387
388 int count = 0;
389 while (iter.has_next()) {
390 if(source == DCmd_Source_MBean && count > 0) {
391 // When diagnostic commands are invoked via JMX, each command line
392 // must contains one and only one command because of the Permission
393 // checks performed by the DiagnosticCommandMBean
394 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
395 "Invalid syntax");
396 }
397 CmdLine line = iter.next();
398 if (line.is_stop()) {
399 break;
400 }
401 if (line.is_executable()) {
402 DCmd* command = DCmdFactory::create_local_DCmd(source, line, out, CHECK);
403 assert(command != NULL, "command error must be handled before this line");
404 DCmdMark mark(command);
405 command->parse(&line, delim, CHECK);
406 command->execute(source, CHECK);
407 }
408 count++;
409 }
410 }
411
412 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) {
413 _dcmdparser.parse(line, delim, CHECK);
414 }
415
416 void DCmdWithParser::print_help(const char* name) {
417 _dcmdparser.print_help(output(), name);
418 }
419
420 void DCmdWithParser::reset(TRAPS) {
421 _dcmdparser.reset(CHECK);
422 }
423
424 void DCmdWithParser::cleanup() {
425 _dcmdparser.cleanup();
426 }
427
428 GrowableArray<const char*>* DCmdWithParser::argument_name_array() {
429 return _dcmdparser.argument_name_array();
430 }
431
432 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() {
433 return _dcmdparser.argument_info_array();
434 }
435
436 void DCmdFactory::push_jmx_notification_request() {
437 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
438 _has_pending_jmx_notification = true;
439 Service_lock->notify_all();
440 }
441
442 void DCmdFactory::send_notification(TRAPS) {
443 DCmdFactory::send_notification_internal(THREAD);
444 // Clearing pending exception to avoid premature termination of
445 // the service thread
446 if (HAS_PENDING_EXCEPTION) {
447 CLEAR_PENDING_EXCEPTION;
448 }
449 }
450 void DCmdFactory::send_notification_internal(TRAPS) {
451 ResourceMark rm(THREAD);
452 HandleMark hm(THREAD);
500 if(factory->export_flags() & source) {
501 return factory;
502 } else {
503 return NULL;
504 }
505 }
506 factory = factory->_next;
507 }
508 return NULL;
509 }
510
511 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) {
512 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
513 factory->_next = _DCmdFactoryList;
514 _DCmdFactoryList = factory;
515 if (_send_jmx_notification && !factory->_hidden
516 && (factory->_export_flags & DCmd_Source_MBean)) {
517 DCmdFactory::push_jmx_notification_request();
518 }
519 return 0; // Actually, there's no checks for duplicates
520 }
521
522 DCmd* DCmdFactory::create_global_DCmd(DCmdSource source, CmdLine &line,
523 outputStream* out, TRAPS) {
524 DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len());
525 if (f != NULL) {
526 if (f->is_enabled()) {
527 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
528 f->disabled_message());
529 }
530 return f->create_Cheap_instance(out);
531 }
532 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
533 "Unknown diagnostic command");
534 }
535
536 DCmd* DCmdFactory::create_local_DCmd(DCmdSource source, CmdLine &line,
537 outputStream* out, TRAPS) {
538 DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len());
539 if (f != NULL) {
540 if (!f->is_enabled()) {
541 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
542 f->disabled_message());
543 }
544 return f->create_resource_instance(out);
545 }
546 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
547 "Unknown diagnostic command");
548 }
549
550 GrowableArray<const char*>* DCmdFactory::DCmd_list(DCmdSource source) {
551 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
552 GrowableArray<const char*>* array = new GrowableArray<const char*>();
553 DCmdFactory* factory = _DCmdFactoryList;
|
234 const size_t buflen = 256;
235 char buf[buflen];
236 GenDCmdArgument* arg = _arguments_list;
237 while (arg != NULL) {
238 if (arg->is_mandatory() && !arg->has_value()) {
239 jio_snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name());
240 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
241 }
242 arg = arg->next();
243 }
244 arg = _options;
245 while (arg != NULL) {
246 if (arg->is_mandatory() && !arg->has_value()) {
247 jio_snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name());
248 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
249 }
250 arg = arg->next();
251 }
252 }
253
254 void DCmdParser::print_help(outputStream* out, const char* cmd_name) const {
255 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
256 GenDCmdArgument* arg = _arguments_list;
257 while (arg != NULL) {
258 if (arg->is_mandatory()) {
259 out->print(" <%s>", arg->name());
260 } else {
261 out->print(" [<%s>]", arg->name());
262 }
263 arg = arg->next();
264 }
265 out->cr();
266 if (_arguments_list != NULL) {
267 out->print_cr("\nArguments:");
268 arg = _arguments_list;
269 while (arg != NULL) {
270 out->print("\t%s : %s %s (%s, ", arg->name(),
271 arg->is_mandatory() ? "" : "[optional]",
272 arg->description(), arg->type());
273 if (arg->has_default()) {
274 out->print("%s", arg->default_string());
306 arg = _options;
307 while (arg != NULL) {
308 arg->reset(CHECK);
309 arg = arg->next();
310 }
311 }
312
313 void DCmdParser::cleanup() {
314 GenDCmdArgument* arg = _arguments_list;
315 while (arg != NULL) {
316 arg->cleanup();
317 arg = arg->next();
318 }
319 arg = _options;
320 while (arg != NULL) {
321 arg->cleanup();
322 arg = arg->next();
323 }
324 }
325
326 int DCmdParser::num_arguments() const {
327 GenDCmdArgument* arg = _arguments_list;
328 int count = 0;
329 while (arg != NULL) {
330 count++;
331 arg = arg->next();
332 }
333 arg = _options;
334 while (arg != NULL) {
335 count++;
336 arg = arg->next();
337 }
338 return count;
339 }
340
341 GrowableArray<const char *>* DCmdParser::argument_name_array() const {
342 int count = num_arguments();
343 GrowableArray<const char *>* array = new GrowableArray<const char *>(count);
344 GenDCmdArgument* arg = _arguments_list;
345 while (arg != NULL) {
346 array->append(arg->name());
347 arg = arg->next();
348 }
349 arg = _options;
350 while (arg != NULL) {
351 array->append(arg->name());
352 arg = arg->next();
353 }
354 return array;
355 }
356
357 GrowableArray<DCmdArgumentInfo*>* DCmdParser::argument_info_array() const {
358 int count = num_arguments();
359 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo *>(count);
360 int idx = 0;
361 GenDCmdArgument* arg = _arguments_list;
362 while (arg != NULL) {
363 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
364 arg->type(), arg->default_string(), arg->is_mandatory(),
365 false, arg->allow_multiple(), idx));
366 idx++;
367 arg = arg->next();
368 }
369 arg = _options;
370 while (arg != NULL) {
371 array->append(new DCmdArgumentInfo(arg->name(), arg->description(),
372 arg->type(), arg->default_string(), arg->is_mandatory(),
373 true, arg->allow_multiple()));
374 arg = arg->next();
375 }
376 return array;
377 }
382 void DCmd::parse_and_execute(DCmdSource source, outputStream* out,
383 const char* cmdline, char delim, TRAPS) {
384
385 if (cmdline == NULL) return; // Nothing to do!
386 DCmdIter iter(cmdline, '\n');
387
388 int count = 0;
389 while (iter.has_next()) {
390 if(source == DCmd_Source_MBean && count > 0) {
391 // When diagnostic commands are invoked via JMX, each command line
392 // must contains one and only one command because of the Permission
393 // checks performed by the DiagnosticCommandMBean
394 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
395 "Invalid syntax");
396 }
397 CmdLine line = iter.next();
398 if (line.is_stop()) {
399 break;
400 }
401 if (line.is_executable()) {
402 ResourceMark rm;
403 DCmd* command = DCmdFactory::create_local_DCmd(source, line, out, CHECK);
404 assert(command != NULL, "command error must be handled before this line");
405 DCmdMark mark(command);
406 command->parse(&line, delim, CHECK);
407 command->execute(source, CHECK);
408 }
409 count++;
410 }
411 }
412
413 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) {
414 _dcmdparser.parse(line, delim, CHECK);
415 }
416
417 void DCmdWithParser::print_help(const char* name) const {
418 _dcmdparser.print_help(output(), name);
419 }
420
421 void DCmdWithParser::reset(TRAPS) {
422 _dcmdparser.reset(CHECK);
423 }
424
425 void DCmdWithParser::cleanup() {
426 _dcmdparser.cleanup();
427 }
428
429 GrowableArray<const char*>* DCmdWithParser::argument_name_array() const {
430 return _dcmdparser.argument_name_array();
431 }
432
433 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() const {
434 return _dcmdparser.argument_info_array();
435 }
436
437 void DCmdFactory::push_jmx_notification_request() {
438 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
439 _has_pending_jmx_notification = true;
440 Service_lock->notify_all();
441 }
442
443 void DCmdFactory::send_notification(TRAPS) {
444 DCmdFactory::send_notification_internal(THREAD);
445 // Clearing pending exception to avoid premature termination of
446 // the service thread
447 if (HAS_PENDING_EXCEPTION) {
448 CLEAR_PENDING_EXCEPTION;
449 }
450 }
451 void DCmdFactory::send_notification_internal(TRAPS) {
452 ResourceMark rm(THREAD);
453 HandleMark hm(THREAD);
501 if(factory->export_flags() & source) {
502 return factory;
503 } else {
504 return NULL;
505 }
506 }
507 factory = factory->_next;
508 }
509 return NULL;
510 }
511
512 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) {
513 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
514 factory->_next = _DCmdFactoryList;
515 _DCmdFactoryList = factory;
516 if (_send_jmx_notification && !factory->_hidden
517 && (factory->_export_flags & DCmd_Source_MBean)) {
518 DCmdFactory::push_jmx_notification_request();
519 }
520 return 0; // Actually, there's no checks for duplicates
521 }
522
523 DCmd* DCmdFactory::create_local_DCmd(DCmdSource source, CmdLine &line,
524 outputStream* out, TRAPS) {
525 DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len());
526 if (f != NULL) {
527 if (!f->is_enabled()) {
528 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
529 f->disabled_message());
530 }
531 return f->create_resource_instance(out);
532 }
533 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
534 "Unknown diagnostic command");
535 }
536
537 GrowableArray<const char*>* DCmdFactory::DCmd_list(DCmdSource source) {
538 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
539 GrowableArray<const char*>* array = new GrowableArray<const char*>();
540 DCmdFactory* factory = _DCmdFactoryList;
|