1 /*
   2  * Copyright (c) 2011, 2015, 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 #include "precompiled.hpp"
  26 #include "classfile/classLoaderStats.hpp"
  27 #include "classfile/compactHashtable.hpp"
  28 #include "gc/shared/vmGCOperations.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/javaCalls.hpp"
  31 #include "runtime/os.hpp"
  32 #include "services/diagnosticArgument.hpp"
  33 #include "services/diagnosticCommand.hpp"
  34 #include "services/diagnosticFramework.hpp"
  35 #include "services/heapDumper.hpp"
  36 #include "services/management.hpp"
  37 #include "services/writeableFlags.hpp"
  38 #include "utilities/macros.hpp"
  39 
  40 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  41 
  42 void DCmdRegistrant::register_dcmds(){
  43   // Registration of the diagnostic commands
  44   // First argument specifies which interfaces will export the command
  45   // Second argument specifies if the command is enabled
  46   // Third  argument specifies if the command is hidden
  47   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  48                          | DCmd_Source_MBean;
  49   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  50   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  59 #if INCLUDE_SERVICES // Heap dumping/inspection supported
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  61   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
  64   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
  65   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
  66 #endif // INCLUDE_SERVICES
  67 #if INCLUDE_JVMTI
  68   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
  69 #endif // INCLUDE_JVMTI
  70   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  71   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
  72   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
  73   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
  74   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
  75   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
  76 
  77   // Enhanced JMX Agent Support
  78   // These commands won't be exported via the DiagnosticCommandMBean until an
  79   // appropriate permission is created for them
  80   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
  81   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
  82   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
  83   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
  84   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStatusDCmd>(jmx_agent_export_flags, true,false));
  85 
  86 }
  87 
  88 #ifndef HAVE_EXTRA_DCMD
  89 void DCmdRegistrant::register_dcmds_ext(){
  90    // Do nothing here
  91 }
  92 #endif
  93 
  94 
  95 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
  96   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
  97   _cmd("command name", "The name of the command for which we want help",
  98         "STRING", false) {
  99   _dcmdparser.add_dcmd_option(&_all);
 100   _dcmdparser.add_dcmd_argument(&_cmd);
 101 };
 102 
 103 void HelpDCmd::execute(DCmdSource source, TRAPS) {
 104   if (_all.value()) {
 105     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
 106     for (int i = 0; i < cmd_list->length(); i++) {
 107       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 108                                                   strlen(cmd_list->at(i)));
 109       output()->print_cr("%s%s", factory->name(),
 110                          factory->is_enabled() ? "" : " [disabled]");
 111       output()->print_cr("\t%s", factory->description());
 112       output()->cr();
 113       factory = factory->next();
 114     }
 115   } else if (_cmd.has_value()) {
 116     DCmd* cmd = NULL;
 117     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
 118                                                 strlen(_cmd.value()));
 119     if (factory != NULL) {
 120       output()->print_cr("%s%s", factory->name(),
 121                          factory->is_enabled() ? "" : " [disabled]");
 122       output()->print_cr("%s", factory->description());
 123       output()->print_cr("\nImpact: %s", factory->impact());
 124       JavaPermission p = factory->permission();
 125       if(p._class != NULL) {
 126         if(p._action != NULL) {
 127           output()->print_cr("\nPermission: %s(%s, %s)",
 128                   p._class, p._name == NULL ? "null" : p._name, p._action);
 129         } else {
 130           output()->print_cr("\nPermission: %s(%s)",
 131                   p._class, p._name == NULL ? "null" : p._name);
 132         }
 133       }
 134       output()->cr();
 135       cmd = factory->create_resource_instance(output());
 136       if (cmd != NULL) {
 137         DCmdMark mark(cmd);
 138         cmd->print_help(factory->name());
 139       }
 140     } else {
 141       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 142     }
 143   } else {
 144     output()->print_cr("The following commands are available:");
 145     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 146     for (int i = 0; i < cmd_list->length(); i++) {
 147       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 148                                                   strlen(cmd_list->at(i)));
 149       output()->print_cr("%s%s", factory->name(),
 150                          factory->is_enabled() ? "" : " [disabled]");
 151       factory = factory->_next;
 152     }
 153     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 154   }
 155 }
 156 
 157 int HelpDCmd::num_arguments() {
 158   ResourceMark rm;
 159   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 160   if (dcmd != NULL) {
 161     DCmdMark mark(dcmd);
 162     return dcmd->_dcmdparser.num_arguments();
 163   } else {
 164     return 0;
 165   }
 166 }
 167 
 168 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 169   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 170           Abstract_VM_Version::vm_release());
 171   JDK_Version jdk_version = JDK_Version::current();
 172   if (jdk_version.patch_version() > 0) {
 173     output()->print_cr("JDK %d.%d.%d.%d", jdk_version.major_version(),
 174             jdk_version.minor_version(), jdk_version.security_version(),
 175             jdk_version.patch_version());
 176   } else {
 177     output()->print_cr("JDK %d.%d.%d", jdk_version.major_version(),
 178             jdk_version.minor_version(), jdk_version.security_version());
 179   }
 180 }
 181 
 182 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 183                                    DCmdWithParser(output, heap),
 184   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 185   _dcmdparser.add_dcmd_option(&_all);
 186 }
 187 
 188 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 189   if (_all.value()) {
 190     CommandLineFlags::printFlags(output(), true);
 191   } else {
 192     CommandLineFlags::printSetFlags(output());
 193   }
 194 }
 195 
 196 int PrintVMFlagsDCmd::num_arguments() {
 197     ResourceMark rm;
 198     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 199     if (dcmd != NULL) {
 200       DCmdMark mark(dcmd);
 201       return dcmd->_dcmdparser.num_arguments();
 202     } else {
 203       return 0;
 204     }
 205 }
 206 
 207 SetVMFlagDCmd::SetVMFlagDCmd(outputStream* output, bool heap) :
 208                                    DCmdWithParser(output, heap),
 209   _flag("flag name", "The name of the flag we want to set",
 210         "STRING", true),
 211   _value("string value", "The value we want to set", "STRING", false) {
 212   _dcmdparser.add_dcmd_argument(&_flag);
 213   _dcmdparser.add_dcmd_argument(&_value);
 214 }
 215 
 216 void SetVMFlagDCmd::execute(DCmdSource source, TRAPS) {
 217   const char* val = NULL;
 218   if (_value.value() != NULL) {
 219     val = _value.value();
 220   }
 221 
 222   FormatBuffer<80> err_msg("%s", "");
 223   int ret = WriteableFlags::set_flag(_flag.value(), val, Flag::MANAGEMENT, err_msg);
 224 
 225   if (ret != WriteableFlags::SUCCESS) {
 226     output()->print_cr("%s", err_msg.buffer());
 227   }
 228 }
 229 
 230 int SetVMFlagDCmd::num_arguments() {
 231   ResourceMark rm;
 232   SetVMFlagDCmd* dcmd = new SetVMFlagDCmd(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 JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
 242   if (JvmtiExport::should_post_data_dump()) {
 243     JvmtiExport::post_data_dump();
 244   }
 245 }
 246 
 247 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 248   // load sun.misc.VMSupport
 249   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 250   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 251   instanceKlassHandle ik (THREAD, k);
 252   if (ik->should_be_initialized()) {
 253     ik->initialize(THREAD);
 254   }
 255   if (HAS_PENDING_EXCEPTION) {
 256     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 257     output()->cr();
 258     CLEAR_PENDING_EXCEPTION;
 259     return;
 260   }
 261 
 262   // invoke the serializePropertiesToByteArray method
 263   JavaValue result(T_OBJECT);
 264   JavaCallArguments args;
 265 
 266   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 267   JavaCalls::call_static(&result,
 268                          ik,
 269                          vmSymbols::serializePropertiesToByteArray_name(),
 270                          signature,
 271                          &args,
 272                          THREAD);
 273   if (HAS_PENDING_EXCEPTION) {
 274     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 275     output()->cr();
 276     CLEAR_PENDING_EXCEPTION;
 277     return;
 278   }
 279 
 280   // The result should be a [B
 281   oop res = (oop)result.get_jobject();
 282   assert(res->is_typeArray(), "just checking");
 283   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 284 
 285   // copy the bytes to the output stream
 286   typeArrayOop ba = typeArrayOop(res);
 287   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 288   output()->print_raw((const char*)addr, ba->length());
 289 }
 290 
 291 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 292                            DCmdWithParser(output, heap),
 293   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 294   _dcmdparser.add_dcmd_option(&_date);
 295 }
 296 
 297 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 298   if (_date.value()) {
 299     output()->date_stamp(true, "", ": ");
 300   }
 301   output()->time_stamp().update_to(tty->time_stamp().ticks());
 302   output()->stamp();
 303   output()->print_cr(" s");
 304 }
 305 
 306 int VMUptimeDCmd::num_arguments() {
 307   ResourceMark rm;
 308   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 309   if (dcmd != NULL) {
 310     DCmdMark mark(dcmd);
 311     return dcmd->_dcmdparser.num_arguments();
 312   } else {
 313     return 0;
 314   }
 315 }
 316 
 317 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 318   if (!DisableExplicitGC) {
 319     Universe::heap()->collect(GCCause::_java_lang_system_gc);
 320   } else {
 321     output()->print_cr("Explicit GC is disabled, no GC has been performed.");
 322   }
 323 }
 324 
 325 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 326   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 327                                                  true, CHECK);
 328   instanceKlassHandle klass(THREAD, k);
 329   JavaValue result(T_VOID);
 330   JavaCalls::call_static(&result, klass,
 331                          vmSymbols::run_finalization_name(),
 332                          vmSymbols::void_method_signature(), CHECK);
 333 }
 334 
 335 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 336 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 337                            DCmdWithParser(output, heap),
 338   _filename("filename","Name of the dump file", "STRING",true),
 339   _all("-all", "Dump all objects, including unreachable objects",
 340        "BOOLEAN", false, "false") {
 341   _dcmdparser.add_dcmd_option(&_all);
 342   _dcmdparser.add_dcmd_argument(&_filename);
 343 }
 344 
 345 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 346   // Request a full GC before heap dump if _all is false
 347   // This helps reduces the amount of unreachable objects in the dump
 348   // and makes it easier to browse.
 349   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 350   int res = dumper.dump(_filename.value());
 351   if (res == 0) {
 352     output()->print_cr("Heap dump file created");
 353   } else {
 354     // heap dump failed
 355     ResourceMark rm;
 356     char* error = dumper.error_as_C_string();
 357     if (error == NULL) {
 358       output()->print_cr("Dump failed - reason unknown");
 359     } else {
 360       output()->print_cr("%s", error);
 361     }
 362   }
 363 }
 364 
 365 int HeapDumpDCmd::num_arguments() {
 366   ResourceMark rm;
 367   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 368   if (dcmd != NULL) {
 369     DCmdMark mark(dcmd);
 370     return dcmd->_dcmdparser.num_arguments();
 371   } else {
 372     return 0;
 373   }
 374 }
 375 
 376 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 377                                        DCmdWithParser(output, heap),
 378   _all("-all", "Inspect all objects, including unreachable objects",
 379        "BOOLEAN", false, "false") {
 380   _dcmdparser.add_dcmd_option(&_all);
 381 }
 382 
 383 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 384   VM_GC_HeapInspection heapop(output(),
 385                               !_all.value() /* request full gc if false */);
 386   VMThread::execute(&heapop);
 387 }
 388 
 389 int ClassHistogramDCmd::num_arguments() {
 390   ResourceMark rm;
 391   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 392   if (dcmd != NULL) {
 393     DCmdMark mark(dcmd);
 394     return dcmd->_dcmdparser.num_arguments();
 395   } else {
 396     return 0;
 397   }
 398 }
 399 
 400 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 401 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 402                                        DCmdWithParser(output, heap),
 403   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 404        "BOOLEAN", false, "false"),
 405   _all("-all", "Show all columns",
 406        "BOOLEAN", false, "false"),
 407   _help("-help", "Show meaning of all the columns",
 408        "BOOLEAN", false, "false"),
 409   _columns("columns", "Comma-separated list of all the columns to show. "
 410            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 411            "STRING", false) {
 412   _dcmdparser.add_dcmd_option(&_all);
 413   _dcmdparser.add_dcmd_option(&_csv);
 414   _dcmdparser.add_dcmd_option(&_help);
 415   _dcmdparser.add_dcmd_argument(&_columns);
 416 }
 417 
 418 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 419   if (!UnlockDiagnosticVMOptions) {
 420     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 421     return;
 422   }
 423 
 424   VM_GC_HeapInspection heapop(output(),
 425                               true /* request_full_gc */);
 426   heapop.set_csv_format(_csv.value());
 427   heapop.set_print_help(_help.value());
 428   heapop.set_print_class_stats(true);
 429   if (_all.value()) {
 430     if (_columns.has_value()) {
 431       output()->print_cr("Cannot specify -all and individual columns at the same time");
 432       return;
 433     } else {
 434       heapop.set_columns(NULL);
 435     }
 436   } else {
 437     if (_columns.has_value()) {
 438       heapop.set_columns(_columns.value());
 439     } else {
 440       heapop.set_columns(DEFAULT_COLUMNS);
 441     }
 442   }
 443   VMThread::execute(&heapop);
 444 }
 445 
 446 int ClassStatsDCmd::num_arguments() {
 447   ResourceMark rm;
 448   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 449   if (dcmd != NULL) {
 450     DCmdMark mark(dcmd);
 451     return dcmd->_dcmdparser.num_arguments();
 452   } else {
 453     return 0;
 454   }
 455 }
 456 #endif // INCLUDE_SERVICES
 457 
 458 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 459                                DCmdWithParser(output, heap),
 460   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 461   _dcmdparser.add_dcmd_option(&_locks);
 462 }
 463 
 464 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 465   // thread stacks
 466   VM_PrintThreads op1(output(), _locks.value());
 467   VMThread::execute(&op1);
 468 
 469   // JNI global handles
 470   VM_PrintJNI op2(output());
 471   VMThread::execute(&op2);
 472 
 473   // Deadlock detection
 474   VM_FindDeadlocks op3(output());
 475   VMThread::execute(&op3);
 476 }
 477 
 478 int ThreadDumpDCmd::num_arguments() {
 479   ResourceMark rm;
 480   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 481   if (dcmd != NULL) {
 482     DCmdMark mark(dcmd);
 483     return dcmd->_dcmdparser.num_arguments();
 484   } else {
 485     return 0;
 486   }
 487 }
 488 
 489 // Enhanced JMX Agent support
 490 
 491 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 492 
 493   DCmdWithParser(output, heap_allocated),
 494 
 495   _config_file
 496   ("config.file",
 497    "set com.sun.management.config.file", "STRING", false),
 498 
 499   _jmxremote_port
 500   ("jmxremote.port",
 501    "set com.sun.management.jmxremote.port", "STRING", false),
 502 
 503   _jmxremote_rmi_port
 504   ("jmxremote.rmi.port",
 505    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 506 
 507   _jmxremote_ssl
 508   ("jmxremote.ssl",
 509    "set com.sun.management.jmxremote.ssl", "STRING", false),
 510 
 511   _jmxremote_registry_ssl
 512   ("jmxremote.registry.ssl",
 513    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 514 
 515   _jmxremote_authenticate
 516   ("jmxremote.authenticate",
 517    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 518 
 519   _jmxremote_password_file
 520   ("jmxremote.password.file",
 521    "set com.sun.management.jmxremote.password.file", "STRING", false),
 522 
 523   _jmxremote_access_file
 524   ("jmxremote.access.file",
 525    "set com.sun.management.jmxremote.access.file", "STRING", false),
 526 
 527   _jmxremote_login_config
 528   ("jmxremote.login.config",
 529    "set com.sun.management.jmxremote.login.config", "STRING", false),
 530 
 531   _jmxremote_ssl_enabled_cipher_suites
 532   ("jmxremote.ssl.enabled.cipher.suites",
 533    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 534 
 535   _jmxremote_ssl_enabled_protocols
 536   ("jmxremote.ssl.enabled.protocols",
 537    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 538 
 539   _jmxremote_ssl_need_client_auth
 540   ("jmxremote.ssl.need.client.auth",
 541    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 542 
 543   _jmxremote_ssl_config_file
 544   ("jmxremote.ssl.config.file",
 545    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 546 
 547 // JDP Protocol support
 548   _jmxremote_autodiscovery
 549   ("jmxremote.autodiscovery",
 550    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 551 
 552    _jdp_port
 553   ("jdp.port",
 554    "set com.sun.management.jdp.port", "INT", false),
 555 
 556    _jdp_address
 557   ("jdp.address",
 558    "set com.sun.management.jdp.address", "STRING", false),
 559 
 560    _jdp_source_addr
 561   ("jdp.source_addr",
 562    "set com.sun.management.jdp.source_addr", "STRING", false),
 563 
 564    _jdp_ttl
 565   ("jdp.ttl",
 566    "set com.sun.management.jdp.ttl", "INT", false),
 567 
 568    _jdp_pause
 569   ("jdp.pause",
 570    "set com.sun.management.jdp.pause", "INT", false),
 571 
 572    _jdp_name
 573   ("jdp.name",
 574    "set com.sun.management.jdp.name", "STRING", false)
 575 
 576   {
 577     _dcmdparser.add_dcmd_option(&_config_file);
 578     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 579     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 580     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 581     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 582     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 583     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 584     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 585     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 586     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 587     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 588     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 589     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 590     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 591     _dcmdparser.add_dcmd_option(&_jdp_port);
 592     _dcmdparser.add_dcmd_option(&_jdp_address);
 593     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 594     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 595     _dcmdparser.add_dcmd_option(&_jdp_pause);
 596     _dcmdparser.add_dcmd_option(&_jdp_name);
 597 }
 598 
 599 
 600 int JMXStartRemoteDCmd::num_arguments() {
 601   ResourceMark rm;
 602   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 603   if (dcmd != NULL) {
 604     DCmdMark mark(dcmd);
 605     return dcmd->_dcmdparser.num_arguments();
 606   } else {
 607     return 0;
 608   }
 609 }
 610 
 611 
 612 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 613     ResourceMark rm(THREAD);
 614     HandleMark hm(THREAD);
 615 
 616     // Load and initialize the sun.management.Agent class
 617     // invoke startRemoteManagementAgent(string) method to start
 618     // the remote management server.
 619     // throw java.lang.NoSuchMethodError if the method doesn't exist
 620 
 621     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 622     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 623     instanceKlassHandle ik (THREAD, k);
 624 
 625     JavaValue result(T_VOID);
 626 
 627     // Pass all command line arguments to java as key=value,...
 628     // All checks are done on java side
 629 
 630     int len = 0;
 631     stringStream options;
 632     char comma[2] = {0,0};
 633 
 634     // Leave default values on Agent.class side and pass only
 635     // agruments explicitly set by user. All arguments passed
 636     // to jcmd override properties with the same name set by
 637     // command line with -D or by managmenent.properties
 638     // file.
 639 #define PUT_OPTION(a) \
 640     if ( (a).is_set() ){ \
 641         options.print(\
 642                ( *((a).type()) == 'I' ) ? "%scom.sun.management.%s=%d" : "%scom.sun.management.%s=%s",\
 643                 comma, (a).name(), (a).value()); \
 644         comma[0] = ','; \
 645     }
 646 
 647     PUT_OPTION(_config_file);
 648     PUT_OPTION(_jmxremote_port);
 649     PUT_OPTION(_jmxremote_rmi_port);
 650     PUT_OPTION(_jmxremote_ssl);
 651     PUT_OPTION(_jmxremote_registry_ssl);
 652     PUT_OPTION(_jmxremote_authenticate);
 653     PUT_OPTION(_jmxremote_password_file);
 654     PUT_OPTION(_jmxremote_access_file);
 655     PUT_OPTION(_jmxremote_login_config);
 656     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 657     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 658     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 659     PUT_OPTION(_jmxremote_ssl_config_file);
 660     PUT_OPTION(_jmxremote_autodiscovery);
 661     PUT_OPTION(_jdp_port);
 662     PUT_OPTION(_jdp_address);
 663     PUT_OPTION(_jdp_source_addr);
 664     PUT_OPTION(_jdp_ttl);
 665     PUT_OPTION(_jdp_pause);
 666     PUT_OPTION(_jdp_name);
 667 
 668 #undef PUT_OPTION
 669 
 670     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 671     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 672 }
 673 
 674 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 675   DCmd(output, heap_allocated) {
 676   // do nothing
 677 }
 678 
 679 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 680     ResourceMark rm(THREAD);
 681     HandleMark hm(THREAD);
 682 
 683     // Load and initialize the sun.management.Agent class
 684     // invoke startLocalManagementAgent(void) method to start
 685     // the local management server
 686     // throw java.lang.NoSuchMethodError if method doesn't exist
 687 
 688     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 689     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 690     instanceKlassHandle ik (THREAD, k);
 691 
 692     JavaValue result(T_VOID);
 693     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 694 }
 695 
 696 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 697     ResourceMark rm(THREAD);
 698     HandleMark hm(THREAD);
 699 
 700     // Load and initialize the sun.management.Agent class
 701     // invoke stopRemoteManagementAgent method to stop the
 702     // management server
 703     // throw java.lang.NoSuchMethodError if method doesn't exist
 704 
 705     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 706     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 707     instanceKlassHandle ik (THREAD, k);
 708 
 709     JavaValue result(T_VOID);
 710     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 711 }
 712 
 713 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 714   DCmd(output, heap_allocated) {
 715   // do nothing
 716 }
 717 
 718 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 719   ResourceMark rm(THREAD);
 720   HandleMark hm(THREAD);
 721 
 722   // Load and initialize the sun.management.Agent class
 723   // invoke getManagementAgentStatus() method to generate the status info
 724   // throw java.lang.NoSuchMethodError if method doesn't exist
 725 
 726   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 727   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 728   instanceKlassHandle ik (THREAD, k);
 729 
 730   JavaValue result(T_OBJECT);
 731   JavaCalls::call_static(&result, ik, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 732 
 733   jvalue* jv = (jvalue*) result.get_value_addr();
 734   oop str = (oop) jv->l;
 735   if (str != NULL) {
 736       char* out = java_lang_String::as_utf8_string(str);
 737       if (out) {
 738           output()->print_cr("%s", out);
 739           return;
 740       }
 741   }
 742   output()->print_cr("Error obtaining management agent status");
 743 }
 744 
 745 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 746   DCmd(output, heap_allocated) {
 747   // do nothing
 748 }
 749 
 750 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 751   os::print_dll_info(output());
 752   output()->cr();
 753 }
 754 
 755 void RotateGCLogDCmd::execute(DCmdSource source, TRAPS) {
 756   if (UseGCLogFileRotation) {
 757     VM_RotateGCLog rotateop(output());
 758     VMThread::execute(&rotateop);
 759   } else {
 760     output()->print_cr("Target VM does not support GC log file rotation.");
 761   }
 762 }
 763 
 764 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 765   VM_PrintCompileQueue printCompileQueueOp(output());
 766   VMThread::execute(&printCompileQueueOp);
 767 }
 768 
 769 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 770   VM_PrintCodeList printCodeListOp(output());
 771   VMThread::execute(&printCodeListOp);
 772 }
 773 
 774 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 775   VM_PrintCodeCache printCodeCacheOp(output());
 776   VMThread::execute(&printCodeCacheOp);
 777 }
 778 
 779 #if INCLUDE_SERVICES
 780 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 781                                        DCmdWithParser(output, heap),
 782   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 783   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 784                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 785   _classname("classname", "Name of class whose hierarchy should be printed. "
 786              "If not specified, all class hierarchies are printed.",
 787              "STRING", false) {
 788   _dcmdparser.add_dcmd_option(&_print_interfaces);
 789   _dcmdparser.add_dcmd_option(&_print_subclasses);
 790   _dcmdparser.add_dcmd_argument(&_classname);
 791 }
 792 
 793 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 794   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 795                                                _print_subclasses.value(), _classname.value());
 796   VMThread::execute(&printClassHierarchyOp);
 797 }
 798 
 799 int ClassHierarchyDCmd::num_arguments() {
 800   ResourceMark rm;
 801   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 802   if (dcmd != NULL) {
 803     DCmdMark mark(dcmd);
 804     return dcmd->_dcmdparser.num_arguments();
 805   } else {
 806     return 0;
 807   }
 808 }
 809 
 810 #endif