src/share/vm/services/diagnosticCommand.cpp

Print this page




  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 #include "precompiled.hpp"
  26 #include "gc_implementation/shared/vmGCOperations.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "services/diagnosticArgument.hpp"
  29 #include "services/diagnosticCommand.hpp"
  30 #include "services/diagnosticFramework.hpp"
  31 #include "services/heapDumper.hpp"
  32 #include "services/management.hpp"

  33 
  34 void DCmdRegistrant::register_dcmds(){
  35   // Registration of the diagnostic commands
  36   // First boolean argument specifies if the command is enabled
  37   // Second boolean argument specifies if the command is hidden
  38   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(true, false));
  39   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(true, false));
  40   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(true, false));
  41   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(true, false));
  42   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(true, false));
  43   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(true, false));
  44   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(true, false));
  45   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(true, false));



  46 #if INCLUDE_SERVICES // Heap dumping supported
  47   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(true, false));
  48 #endif // INCLUDE_SERVICES
  49   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(true, false));
  50   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(true, false));

  51 
  52   //Enhanced JMX Agent Support
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(true,false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(true,false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(true,false));



  56 
  57 }
  58 
  59 #ifndef HAVE_EXTRA_DCMD
  60 void DCmdRegistrant::register_dcmds_ext(){
  61    // Do nothing here
  62 }
  63 #endif
  64 
  65 
  66 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
  67   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
  68   _cmd("command name", "The name of the command for which we want help",
  69         "STRING", false) {
  70   _dcmdparser.add_dcmd_option(&_all);
  71   _dcmdparser.add_dcmd_argument(&_cmd);
  72 };
  73 
  74 void HelpDCmd::execute(TRAPS) {
  75   if (_all.value()) {
  76     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list();
  77     for (int i = 0; i < cmd_list->length(); i++) {
  78       DCmdFactory* factory = DCmdFactory::factory(cmd_list->at(i),
  79                                                   strlen(cmd_list->at(i)));
  80       if (!factory->is_hidden()) {
  81         output()->print_cr("%s%s", factory->name(),
  82                            factory->is_enabled() ? "" : " [disabled]");
  83         output()->print_cr("\t%s", factory->description());
  84         output()->cr();
  85       }
  86       factory = factory->next();
  87     }
  88   } else if (_cmd.has_value()) {
  89     DCmd* cmd = NULL;
  90     DCmdFactory* factory = DCmdFactory::factory(_cmd.value(),
  91                                                 strlen(_cmd.value()));
  92     if (factory != NULL) {
  93       output()->print_cr("%s%s", factory->name(),
  94                          factory->is_enabled() ? "" : " [disabled]");
  95       output()->print_cr(factory->description());
  96       output()->print_cr("\nImpact: %s", factory->impact());










  97       output()->cr();
  98       cmd = factory->create_resource_instance(output());
  99       if (cmd != NULL) {
 100         DCmdMark mark(cmd);
 101         cmd->print_help(factory->name());
 102       }
 103     } else {
 104       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 105     }
 106   } else {
 107     output()->print_cr("The following commands are available:");
 108     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list();
 109     for (int i = 0; i < cmd_list->length(); i++) {
 110       DCmdFactory* factory = DCmdFactory::factory(cmd_list->at(i),
 111                                                   strlen(cmd_list->at(i)));
 112       if (!factory->is_hidden()) {
 113         output()->print_cr("%s%s", factory->name(),
 114                            factory->is_enabled() ? "" : " [disabled]");
 115       }
 116       factory = factory->_next;
 117     }
 118     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 119   }
 120 }
 121 
 122 int HelpDCmd::num_arguments() {
 123   ResourceMark rm;
 124   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 125   if (dcmd != NULL) {
 126     DCmdMark mark(dcmd);
 127     return dcmd->_dcmdparser.num_arguments();
 128   } else {
 129     return 0;
 130   }
 131 }
 132 
 133 void VersionDCmd::execute(TRAPS) {
 134   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 135           Abstract_VM_Version::vm_release());
 136   JDK_Version jdk_version = JDK_Version::current();
 137   if (jdk_version.update_version() > 0) {
 138     output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 139             jdk_version.minor_version(), jdk_version.update_version());
 140   } else {
 141     output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 142             jdk_version.minor_version());
 143   }
 144 }
 145 
 146 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 147                                    DCmdWithParser(output, heap),
 148   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 149   _dcmdparser.add_dcmd_option(&_all);
 150 }
 151 
 152 void PrintVMFlagsDCmd::execute(TRAPS) {
 153   if (_all.value()) {
 154     CommandLineFlags::printFlags(output(), true);
 155   } else {
 156     CommandLineFlags::printSetFlags(output());
 157   }
 158 }
 159 
 160 int PrintVMFlagsDCmd::num_arguments() {
 161     ResourceMark rm;
 162     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 163     if (dcmd != NULL) {
 164       DCmdMark mark(dcmd);
 165       return dcmd->_dcmdparser.num_arguments();
 166     } else {
 167       return 0;
 168     }
 169 }
 170 
 171 void PrintSystemPropertiesDCmd::execute(TRAPS) {
 172   // load sun.misc.VMSupport
 173   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 174   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 175   instanceKlassHandle ik (THREAD, k);
 176   if (ik->should_be_initialized()) {
 177     ik->initialize(THREAD);
 178   }
 179   if (HAS_PENDING_EXCEPTION) {
 180     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 181     output()->cr();
 182     CLEAR_PENDING_EXCEPTION;
 183     return;
 184   }
 185 
 186   // invoke the serializePropertiesToByteArray method
 187   JavaValue result(T_OBJECT);
 188   JavaCallArguments args;
 189 
 190   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 191   JavaCalls::call_static(&result,


 201     return;
 202   }
 203 
 204   // The result should be a [B
 205   oop res = (oop)result.get_jobject();
 206   assert(res->is_typeArray(), "just checking");
 207   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 208 
 209   // copy the bytes to the output stream
 210   typeArrayOop ba = typeArrayOop(res);
 211   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 212   output()->print_raw((const char*)addr, ba->length());
 213 }
 214 
 215 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 216                            DCmdWithParser(output, heap),
 217   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 218   _dcmdparser.add_dcmd_option(&_date);
 219 }
 220 
 221 void VMUptimeDCmd::execute(TRAPS) {
 222   if (_date.value()) {
 223     output()->date_stamp(true, "", ": ");
 224   }
 225   output()->time_stamp().update_to(tty->time_stamp().ticks());
 226   output()->stamp();
 227   output()->print_cr(" s");
 228 }
 229 
 230 int VMUptimeDCmd::num_arguments() {
 231   ResourceMark rm;
 232   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 233   if (dcmd != NULL) {
 234     DCmdMark mark(dcmd);
 235     return dcmd->_dcmdparser.num_arguments();
 236   } else {
 237     return 0;
 238   }
 239 }
 240 
 241 void SystemGCDCmd::execute(TRAPS) {

 242   Universe::heap()->collect(GCCause::_java_lang_system_gc);

 243 }
 244 
 245 void RunFinalizationDCmd::execute(TRAPS) {
 246   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 247                                                  true, CHECK);
 248   instanceKlassHandle klass(THREAD, k);
 249   JavaValue result(T_VOID);
 250   JavaCalls::call_static(&result, klass,
 251                          vmSymbols::run_finalization_name(),
 252                          vmSymbols::void_method_signature(), CHECK);
 253 }
 254 
 255 #if INCLUDE_SERVICES // Heap dumping supported
 256 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 257                            DCmdWithParser(output, heap),
 258   _filename("filename","Name of the dump file", "STRING",true),
 259   _all("-all", "Dump all objects, including unreachable objects",
 260        "BOOLEAN", false, "false") {
 261   _dcmdparser.add_dcmd_option(&_all);
 262   _dcmdparser.add_dcmd_argument(&_filename);
 263 }
 264 
 265 void HeapDumpDCmd::execute(TRAPS) {
 266   // Request a full GC before heap dump if _all is false
 267   // This helps reduces the amount of unreachable objects in the dump
 268   // and makes it easier to browse.
 269   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 270   int res = dumper.dump(_filename.value());
 271   if (res == 0) {
 272     output()->print_cr("Heap dump file created");
 273   } else {
 274     // heap dump failed
 275     ResourceMark rm;
 276     char* error = dumper.error_as_C_string();
 277     if (error == NULL) {
 278       output()->print_cr("Dump failed - reason unknown");
 279     } else {
 280       output()->print_cr("%s", error);
 281     }
 282   }
 283 }
 284 
 285 int HeapDumpDCmd::num_arguments() {
 286   ResourceMark rm;
 287   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 288   if (dcmd != NULL) {
 289     DCmdMark mark(dcmd);
 290     return dcmd->_dcmdparser.num_arguments();
 291   } else {
 292     return 0;
 293   }
 294 }
 295 #endif // INCLUDE_SERVICES
 296 
 297 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 298                                        DCmdWithParser(output, heap),
 299   _all("-all", "Inspect all objects, including unreachable objects",
 300        "BOOLEAN", false, "false") {
 301   _dcmdparser.add_dcmd_option(&_all);
 302 }
 303 
 304 void ClassHistogramDCmd::execute(TRAPS) {
 305   VM_GC_HeapInspection heapop(output(),
 306                               !_all.value() /* request full gc if false */,
 307                               true /* need_prologue */);
 308   VMThread::execute(&heapop);
 309 }
 310 
 311 int ClassHistogramDCmd::num_arguments() {
 312   ResourceMark rm;
 313   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 314   if (dcmd != NULL) {
 315     DCmdMark mark(dcmd);
 316     return dcmd->_dcmdparser.num_arguments();
 317   } else {
 318     return 0;
 319   }
 320 }
 321 
 322 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 323                                DCmdWithParser(output, heap),
 324   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 325   _dcmdparser.add_dcmd_option(&_locks);
 326 }
 327 
 328 void ThreadDumpDCmd::execute(TRAPS) {
 329   // thread stacks
 330   VM_PrintThreads op1(output(), _locks.value());
 331   VMThread::execute(&op1);
 332 
 333   // JNI global handles
 334   VM_PrintJNI op2(output());
 335   VMThread::execute(&op2);
 336 
 337   // Deadlock detection
 338   VM_FindDeadlocks op3(output());
 339   VMThread::execute(&op3);
 340 }
 341 
 342 int ThreadDumpDCmd::num_arguments() {
 343   ResourceMark rm;
 344   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 345   if (dcmd != NULL) {
 346     DCmdMark mark(dcmd);
 347     return dcmd->_dcmdparser.num_arguments();
 348   } else {


 420     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 421     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 422     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 423     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 424     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 425 }
 426 
 427 
 428 int JMXStartRemoteDCmd::num_arguments() {
 429   ResourceMark rm;
 430   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 431   if (dcmd != NULL) {
 432     DCmdMark mark(dcmd);
 433     return dcmd->_dcmdparser.num_arguments();
 434   } else {
 435     return 0;
 436   }
 437 }
 438 
 439 
 440 void JMXStartRemoteDCmd::execute(TRAPS) {
 441     ResourceMark rm(THREAD);
 442     HandleMark hm(THREAD);
 443 
 444     // Load and initialize the sun.management.Agent class
 445     // invoke startRemoteManagementAgent(string) method to start
 446     // the remote management server.
 447     // throw java.lang.NoSuchMethodError if the method doesn't exist
 448 
 449     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 450     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 451     instanceKlassHandle ik (THREAD, k);
 452 
 453     JavaValue result(T_VOID);
 454 
 455     // Pass all command line arguments to java as key=value,...
 456     // All checks are done on java side
 457 
 458     int len = 0;
 459     stringStream options;
 460     char comma[2] = {0,0};


 479     PUT_OPTION(_jmxremote_password_file);
 480     PUT_OPTION(_jmxremote_access_file);
 481     PUT_OPTION(_jmxremote_login_config);
 482     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 483     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 484     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 485     PUT_OPTION(_jmxremote_ssl_config_file);
 486 
 487 #undef PUT_OPTION
 488 
 489     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 490     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 491 }
 492 
 493 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 494   DCmd(output, heap_allocated)
 495 {
 496   // do nothing
 497 }
 498 
 499 void JMXStartLocalDCmd::execute(TRAPS) {
 500     ResourceMark rm(THREAD);
 501     HandleMark hm(THREAD);
 502 
 503     // Load and initialize the sun.management.Agent class
 504     // invoke startLocalManagementAgent(void) method to start
 505     // the local management server
 506     // throw java.lang.NoSuchMethodError if method doesn't exist
 507 
 508     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 509     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 510     instanceKlassHandle ik (THREAD, k);
 511 
 512     JavaValue result(T_VOID);
 513     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 514 }
 515 
 516 
 517 void JMXStopRemoteDCmd::execute(TRAPS) {
 518     ResourceMark rm(THREAD);
 519     HandleMark hm(THREAD);
 520 
 521     // Load and initialize the sun.management.Agent class
 522     // invoke stopRemoteManagementAgent method to stop the
 523     // management server
 524     // throw java.lang.NoSuchMethodError if method doesn't exist
 525 
 526     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 527     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 528     instanceKlassHandle ik (THREAD, k);
 529 
 530     JavaValue result(T_VOID);
 531     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 532 }
 533 


  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 #include "precompiled.hpp"
  26 #include "gc_implementation/shared/vmGCOperations.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "services/diagnosticArgument.hpp"
  29 #include "services/diagnosticCommand.hpp"
  30 #include "services/diagnosticFramework.hpp"
  31 #include "services/heapDumper.hpp"
  32 #include "services/management.hpp"
  33 #include "services/nmtDCmd.hpp"
  34 
  35 void DCmdRegistrant::register_dcmds(){
  36   // Registration of the diagnostic commands
  37   // First argument specifies which interfaces will export the command
  38   // Second argument specifies if the command is enabled
  39   // Third  argument specifies if the command is hidden
  40   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  41                          | DCmd_Source_MBean;
  42   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  43   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  44   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  45   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  46   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  47   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  48   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  49   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  50 #if INCLUDE_SERVICES // Heap dumping supported
  51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(full_export, true, false));
  52 #endif // INCLUDE_SERVICES
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<NMTDCmd>(full_export, true, false));
  56 
  57   // Enhanced JMX Agent Support
  58   // These commands won't be exported via the DiagnosticCommandMBean until an
  59   // appropriate permission is created for them
  60   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
  61   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
  64 
  65 }
  66 
  67 #ifndef HAVE_EXTRA_DCMD
  68 void DCmdRegistrant::register_dcmds_ext(){
  69    // Do nothing here
  70 }
  71 #endif
  72 
  73 
  74 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
  75   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
  76   _cmd("command name", "The name of the command for which we want help",
  77         "STRING", false) {
  78   _dcmdparser.add_dcmd_option(&_all);
  79   _dcmdparser.add_dcmd_argument(&_cmd);
  80 };
  81 
  82 void HelpDCmd::execute(DCmdSource source, TRAPS) {
  83   if (_all.value()) {
  84     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
  85     for (int i = 0; i < cmd_list->length(); i++) {
  86       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
  87                                                   strlen(cmd_list->at(i)));

  88       output()->print_cr("%s%s", factory->name(),
  89                          factory->is_enabled() ? "" : " [disabled]");
  90       output()->print_cr("\t%s", factory->description());
  91       output()->cr(); 

  92       factory = factory->next();
  93     }
  94   } else if (_cmd.has_value()) {
  95     DCmd* cmd = NULL;
  96     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
  97                                                 strlen(_cmd.value()));
  98     if (factory != NULL) {
  99       output()->print_cr("%s%s", factory->name(),
 100                          factory->is_enabled() ? "" : " [disabled]");
 101       output()->print_cr(factory->description());
 102       output()->print_cr("\nImpact: %s", factory->impact());
 103       JavaPermission p = factory->permission();
 104       if(p._class != NULL) {
 105         if(p._action != NULL) {
 106           output()->print_cr("\nPermission: %s(%s, %s)",
 107                   p._class, p._name, p._action);
 108         } else {
 109           output()->print_cr("\nPermission: %s(%s)", 
 110                   p._class, p._name);
 111         }
 112       }
 113       output()->cr();
 114       cmd = factory->create_resource_instance(output());
 115       if (cmd != NULL) {
 116         DCmdMark mark(cmd);
 117         cmd->print_help(factory->name());
 118       }
 119     } else {
 120       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 121     }
 122   } else {
 123     output()->print_cr("The following commands are available:");
 124     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 125     for (int i = 0; i < cmd_list->length(); i++) {
 126       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 127                                                   strlen(cmd_list->at(i)));

 128       output()->print_cr("%s%s", factory->name(),
 129                          factory->is_enabled() ? "" : " [disabled]");

 130       factory = factory->_next;
 131     }
 132     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 133   }
 134 }
 135 
 136 int HelpDCmd::num_arguments() {
 137   ResourceMark rm;
 138   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 139   if (dcmd != NULL) {
 140     DCmdMark mark(dcmd);
 141     return dcmd->_dcmdparser.num_arguments();
 142   } else {
 143     return 0;
 144   }
 145 }
 146 
 147 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 148   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 149           Abstract_VM_Version::vm_release());
 150   JDK_Version jdk_version = JDK_Version::current();
 151   if (jdk_version.update_version() > 0) {
 152     output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 153             jdk_version.minor_version(), jdk_version.update_version());
 154   } else {
 155     output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 156             jdk_version.minor_version());
 157   }
 158 }
 159 
 160 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 161                                    DCmdWithParser(output, heap),
 162   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 163   _dcmdparser.add_dcmd_option(&_all);
 164 }
 165 
 166 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 167   if (_all.value()) {
 168     CommandLineFlags::printFlags(output(), true);
 169   } else {
 170     CommandLineFlags::printSetFlags(output());
 171   }
 172 }
 173 
 174 int PrintVMFlagsDCmd::num_arguments() {
 175     ResourceMark rm;
 176     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 177     if (dcmd != NULL) {
 178       DCmdMark mark(dcmd);
 179       return dcmd->_dcmdparser.num_arguments();
 180     } else {
 181       return 0;
 182     }
 183 }
 184 
 185 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 186   // load sun.misc.VMSupport
 187   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 188   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 189   instanceKlassHandle ik (THREAD, k);
 190   if (ik->should_be_initialized()) {
 191     ik->initialize(THREAD);
 192   }
 193   if (HAS_PENDING_EXCEPTION) {
 194     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 195     output()->cr();
 196     CLEAR_PENDING_EXCEPTION;
 197     return;
 198   }
 199 
 200   // invoke the serializePropertiesToByteArray method
 201   JavaValue result(T_OBJECT);
 202   JavaCallArguments args;
 203 
 204   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 205   JavaCalls::call_static(&result,


 215     return;
 216   }
 217 
 218   // The result should be a [B
 219   oop res = (oop)result.get_jobject();
 220   assert(res->is_typeArray(), "just checking");
 221   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 222 
 223   // copy the bytes to the output stream
 224   typeArrayOop ba = typeArrayOop(res);
 225   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 226   output()->print_raw((const char*)addr, ba->length());
 227 }
 228 
 229 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 230                            DCmdWithParser(output, heap),
 231   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 232   _dcmdparser.add_dcmd_option(&_date);
 233 }
 234 
 235 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 236   if (_date.value()) {
 237     output()->date_stamp(true, "", ": ");
 238   }
 239   output()->time_stamp().update_to(tty->time_stamp().ticks());
 240   output()->stamp();
 241   output()->print_cr(" s");
 242 }
 243 
 244 int VMUptimeDCmd::num_arguments() {
 245   ResourceMark rm;
 246   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 247   if (dcmd != NULL) {
 248     DCmdMark mark(dcmd);
 249     return dcmd->_dcmdparser.num_arguments();
 250   } else {
 251     return 0;
 252   }
 253 }
 254 
 255 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 256   if (!DisableExplicitGC) {
 257     Universe::heap()->collect(GCCause::_java_lang_system_gc);
 258   }
 259 }
 260 
 261 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 262   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 263                                                  true, CHECK);
 264   instanceKlassHandle klass(THREAD, k);
 265   JavaValue result(T_VOID);
 266   JavaCalls::call_static(&result, klass,
 267                          vmSymbols::run_finalization_name(),
 268                          vmSymbols::void_method_signature(), CHECK);
 269 }
 270 
 271 #if INCLUDE_SERVICES // Heap dumping supported
 272 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 273                            DCmdWithParser(output, heap),
 274   _filename("filename","Name of the dump file", "STRING",true),
 275   _all("-all", "Dump all objects, including unreachable objects",
 276        "BOOLEAN", false, "false") {
 277   _dcmdparser.add_dcmd_option(&_all);
 278   _dcmdparser.add_dcmd_argument(&_filename);
 279 }
 280 
 281 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 282   // Request a full GC before heap dump if _all is false
 283   // This helps reduces the amount of unreachable objects in the dump
 284   // and makes it easier to browse.
 285   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 286   int res = dumper.dump(_filename.value());
 287   if (res == 0) {
 288     output()->print_cr("Heap dump file created");
 289   } else {
 290     // heap dump failed
 291     ResourceMark rm;
 292     char* error = dumper.error_as_C_string();
 293     if (error == NULL) {
 294       output()->print_cr("Dump failed - reason unknown");
 295     } else {
 296       output()->print_cr("%s", error);
 297     }
 298   }
 299 }
 300 
 301 int HeapDumpDCmd::num_arguments() {
 302   ResourceMark rm;
 303   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 304   if (dcmd != NULL) {
 305     DCmdMark mark(dcmd);
 306     return dcmd->_dcmdparser.num_arguments();
 307   } else {
 308     return 0;
 309   }
 310 }
 311 #endif // INCLUDE_SERVICES
 312 
 313 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 314                                        DCmdWithParser(output, heap),
 315   _all("-all", "Inspect all objects, including unreachable objects",
 316        "BOOLEAN", false, "false") {
 317   _dcmdparser.add_dcmd_option(&_all);
 318 }
 319 
 320 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 321   VM_GC_HeapInspection heapop(output(),
 322                               !_all.value() /* request full gc if false */,
 323                               true /* need_prologue */);
 324   VMThread::execute(&heapop);
 325 }
 326 
 327 int ClassHistogramDCmd::num_arguments() {
 328   ResourceMark rm;
 329   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 330   if (dcmd != NULL) {
 331     DCmdMark mark(dcmd);
 332     return dcmd->_dcmdparser.num_arguments();
 333   } else {
 334     return 0;
 335   }
 336 }
 337 
 338 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 339                                DCmdWithParser(output, heap),
 340   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 341   _dcmdparser.add_dcmd_option(&_locks);
 342 }
 343 
 344 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 345   // thread stacks
 346   VM_PrintThreads op1(output(), _locks.value());
 347   VMThread::execute(&op1);
 348 
 349   // JNI global handles
 350   VM_PrintJNI op2(output());
 351   VMThread::execute(&op2);
 352 
 353   // Deadlock detection
 354   VM_FindDeadlocks op3(output());
 355   VMThread::execute(&op3);
 356 }
 357 
 358 int ThreadDumpDCmd::num_arguments() {
 359   ResourceMark rm;
 360   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 361   if (dcmd != NULL) {
 362     DCmdMark mark(dcmd);
 363     return dcmd->_dcmdparser.num_arguments();
 364   } else {


 436     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 437     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 438     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 439     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 440     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 441 }
 442 
 443 
 444 int JMXStartRemoteDCmd::num_arguments() {
 445   ResourceMark rm;
 446   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 447   if (dcmd != NULL) {
 448     DCmdMark mark(dcmd);
 449     return dcmd->_dcmdparser.num_arguments();
 450   } else {
 451     return 0;
 452   }
 453 }
 454 
 455 
 456 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 457     ResourceMark rm(THREAD);
 458     HandleMark hm(THREAD);
 459 
 460     // Load and initialize the sun.management.Agent class
 461     // invoke startRemoteManagementAgent(string) method to start
 462     // the remote management server.
 463     // throw java.lang.NoSuchMethodError if the method doesn't exist
 464 
 465     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 466     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 467     instanceKlassHandle ik (THREAD, k);
 468 
 469     JavaValue result(T_VOID);
 470 
 471     // Pass all command line arguments to java as key=value,...
 472     // All checks are done on java side
 473 
 474     int len = 0;
 475     stringStream options;
 476     char comma[2] = {0,0};


 495     PUT_OPTION(_jmxremote_password_file);
 496     PUT_OPTION(_jmxremote_access_file);
 497     PUT_OPTION(_jmxremote_login_config);
 498     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 499     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 500     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 501     PUT_OPTION(_jmxremote_ssl_config_file);
 502 
 503 #undef PUT_OPTION
 504 
 505     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 506     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 507 }
 508 
 509 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 510   DCmd(output, heap_allocated)
 511 {
 512   // do nothing
 513 }
 514 
 515 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 516     ResourceMark rm(THREAD);
 517     HandleMark hm(THREAD);
 518 
 519     // Load and initialize the sun.management.Agent class
 520     // invoke startLocalManagementAgent(void) method to start
 521     // the local management server
 522     // throw java.lang.NoSuchMethodError if method doesn't exist
 523 
 524     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 525     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 526     instanceKlassHandle ik (THREAD, k);
 527 
 528     JavaValue result(T_VOID);
 529     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 530 }
 531 
 532 
 533 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 534     ResourceMark rm(THREAD);
 535     HandleMark hm(THREAD);
 536 
 537     // Load and initialize the sun.management.Agent class
 538     // invoke stopRemoteManagementAgent method to stop the
 539     // management server
 540     // throw java.lang.NoSuchMethodError if method doesn't exist
 541 
 542     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 543     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 544     instanceKlassHandle ik (THREAD, k);
 545 
 546     JavaValue result(T_VOID);
 547     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 548 }
 549