src/share/vm/services/diagnosticFramework.cpp

Print this page




 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 }
 353 
 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 }
 375 
 376 DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL;

 377 
 378 void DCmd::parse_and_execute(outputStream* out, const char* cmdline,
 379                              char delim, TRAPS) {
 380 
 381   if (cmdline == NULL) return; // Nothing to do!
 382   DCmdIter iter(cmdline, '\n');
 383 
 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 }
 398 
 399 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) {
 400   _dcmdparser.parse(line, delim, CHECK);
 401 }
 402 
 403 void DCmdWithParser::print_help(const char* name) {
 404   _dcmdparser.print_help(output(), name);
 405 }
 406 
 407 void DCmdWithParser::reset(TRAPS) {
 408   _dcmdparser.reset(CHECK);
 409 }
 410 
 411 void DCmdWithParser::cleanup() {
 412   _dcmdparser.cleanup();
 413 }
 414 
 415 GrowableArray<const char*>* DCmdWithParser::argument_name_array() {
 416   return _dcmdparser.argument_name_array();
 417 }
 418 
 419 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() {
 420   return _dcmdparser.argument_info_array();
 421 }
 422 


























































 423 Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf, "DCmdFactory", true);

 424 
 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 }
 437 
 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 }
 444 
 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 }
 457 
 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 }
 470 
 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 }
 483 
 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 }


 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 }
 353 
 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, arg->allow_multiple(), 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, arg->allow_multiple()));
 371     arg = arg->next();
 372   }
 373   return array;
 374 }
 375 
 376 DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL;
 377 bool DCmdFactory::_has_pending_jmx_notification = false;
 378 
 379 void DCmd::parse_and_execute(DCmdSource source, outputStream* out,
 380                              const char* cmdline, char delim, TRAPS) {
 381 
 382   if (cmdline == NULL) return; // Nothing to do!
 383   DCmdIter iter(cmdline, '\n');
 384 
 385   while (iter.has_next()) {
 386     CmdLine line = iter.next();
 387     if (line.is_stop()) {
 388       break;
 389     }
 390     if (line.is_executable()) {
 391       DCmd* command = DCmdFactory::create_local_DCmd(source, line, out, CHECK);
 392       assert(command != NULL, "command error must be handled before this line");
 393       DCmdMark mark(command);
 394       command->parse(&line, delim, CHECK);
 395       command->execute(source, CHECK);
 396     }
 397   }
 398 }
 399 
 400 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) {
 401   _dcmdparser.parse(line, delim, CHECK);
 402 }
 403 
 404 void DCmdWithParser::print_help(const char* name) {
 405   _dcmdparser.print_help(output(), name);
 406 }
 407 
 408 void DCmdWithParser::reset(TRAPS) {
 409   _dcmdparser.reset(CHECK);
 410 }
 411 
 412 void DCmdWithParser::cleanup() {
 413   _dcmdparser.cleanup();
 414 }
 415 
 416 GrowableArray<const char*>* DCmdWithParser::argument_name_array() {
 417   return _dcmdparser.argument_name_array();
 418 }
 419 
 420 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() {
 421   return _dcmdparser.argument_info_array();
 422 }
 423 
 424 void DCmdFactory::push_jmx_notification_request() {
 425   MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
 426   _has_pending_jmx_notification = true;
 427   Service_lock->notify_all();
 428 }
 429 
 430 void DCmdFactory::send_notification(TRAPS) {
 431   DCmdFactory::send_notification_internal(THREAD);
 432   // Clearing pending exception to avoid premature termination of
 433   // the service thread
 434   if (HAS_PENDING_EXCEPTION) {
 435     CLEAR_PENDING_EXCEPTION;
 436   }
 437 }
 438 void DCmdFactory::send_notification_internal(TRAPS) {
 439   ResourceMark rm(THREAD);
 440   HandleMark hm(THREAD);
 441   bool notif = false;
 442   {
 443     MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
 444     notif = _has_pending_jmx_notification;
 445     _has_pending_jmx_notification = false;
 446   }
 447   if (notif) {
 448 
 449     Klass* k = Management::sun_management_ManagementFactoryHelper_klass(CHECK);
 450     instanceKlassHandle mgmt_factory_helper_klass(THREAD, k);
 451 
 452     JavaValue result(T_OBJECT);
 453     JavaCalls::call_static(&result,
 454             mgmt_factory_helper_klass,
 455             vmSymbols::getDiagnosticCommandMBean_name(),
 456             vmSymbols::getDiagnosticCommandMBean_signature(),
 457             CHECK);
 458 
 459     instanceOop m = (instanceOop) result.get_jobject();
 460     instanceHandle dcmd_mbean_h(THREAD, m);
 461 
 462     Klass* k2 = Management::sun_management_DiagnosticCommandImpl_klass(CHECK);
 463     instanceKlassHandle dcmd_mbean_klass(THREAD, k2);
 464 
 465     if (!dcmd_mbean_h->is_a(k2)) {
 466       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 467               "ManagementFactory.getDiagnosticCommandMBean didn't return a DiagnosticCommandMBean instance");
 468     }
 469 
 470     JavaValue result2(T_VOID);
 471     JavaCallArguments args2(dcmd_mbean_h);
 472 
 473     JavaCalls::call_virtual(&result2,
 474             dcmd_mbean_klass,
 475             vmSymbols::createDiagnosticFrameworkNotification_name(),
 476             vmSymbols::void_method_signature(),
 477             &args2,
 478             CHECK);
 479   }
 480 }
 481 
 482 Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf, "DCmdFactory", true);
 483 bool DCmdFactory::_send_jmx_notification = false;
 484 
 485 DCmdFactory* DCmdFactory::factory(DCmdSource source, const char* name, size_t len) {
 486   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 487   DCmdFactory* factory = _DCmdFactoryList;
 488   while (factory != NULL) {
 489     if (strlen(factory->name()) == len &&
 490         strncmp(name, factory->name(), len) == 0) {
 491       if(factory->export_flags() & source) {
 492         return factory;
 493       } else {
 494         return NULL;
 495       }
 496     }
 497     factory = factory->_next;
 498   }
 499   return NULL;
 500 }
 501 
 502 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) {
 503   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 504   factory->_next = _DCmdFactoryList;
 505   _DCmdFactoryList = factory;
 506   if (_send_jmx_notification && !factory->_hidden
 507       && (factory->_export_flags & DCmd_Source_MBean)) {
 508     DCmdFactory::push_jmx_notification_request();
 509   }
 510   return 0; // Actually, there's no checks for duplicates
 511 }
 512 
 513 DCmd* DCmdFactory::create_global_DCmd(DCmdSource source, CmdLine &line,
 514                                       outputStream* out, TRAPS) {
 515   DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len());
 516   if (f != NULL) {
 517     if (f->is_enabled()) {
 518       THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 519                      f->disabled_message());
 520     }
 521     return f->create_Cheap_instance(out);
 522   }
 523   THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 524              "Unknown diagnostic command");
 525 }
 526 
 527 DCmd* DCmdFactory::create_local_DCmd(DCmdSource source, CmdLine &line,
 528                                      outputStream* out, TRAPS) {
 529   DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len());
 530   if (f != NULL) {
 531     if (!f->is_enabled()) {
 532       THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 533                      f->disabled_message());
 534     }
 535     return f->create_resource_instance(out);
 536   }
 537   THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 538              "Unknown diagnostic command");
 539 }
 540 
 541 GrowableArray<const char*>* DCmdFactory::DCmd_list(DCmdSource source) {
 542   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 543   GrowableArray<const char*>* array = new GrowableArray<const char*>();
 544   DCmdFactory* factory = _DCmdFactoryList;
 545   while (factory != NULL) {
 546     if (!factory->is_hidden() && (factory->export_flags() & source)) {
 547       array->append(factory->name());
 548     }
 549     factory = factory->next();
 550   }
 551   return array;
 552 }
 553 
 554 GrowableArray<DCmdInfo*>* DCmdFactory::DCmdInfo_list(DCmdSource source ) {
 555   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 556   GrowableArray<DCmdInfo*>* array = new GrowableArray<DCmdInfo*>();
 557   DCmdFactory* factory = _DCmdFactoryList;
 558   while (factory != NULL) {
 559     if (!factory->is_hidden() && (factory->export_flags() & source)) {
 560       array->append(new DCmdInfo(factory->name(),
 561                     factory->description(), factory->impact(),
 562                     factory->permission(), factory->num_arguments(),
 563                     factory->is_enabled()));
 564     }
 565     factory = factory->next();
 566   }
 567   return array;
 568 }