1 /*
   2  * Copyright (c) 2011, 2017, 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 "gc_implementation/shared/vmGCOperations.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "runtime/os.hpp"
  29 #include "services/diagnosticArgument.hpp"
  30 #include "services/diagnosticCommand.hpp"
  31 #include "services/diagnosticFramework.hpp"
  32 #include "services/heapDumper.hpp"
  33 #include "services/management.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "oops/objArrayOop.hpp"
  36 
  37 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  38 
  39 void DCmdRegistrant::register_dcmds(){
  40   // Registration of the diagnostic commands
  41   // First argument specifies which interfaces will export the command
  42   // Second argument specifies if the command is enabled
  43   // Third  argument specifies if the command is hidden
  44   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  45                          | DCmd_Source_MBean;
  46   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  47   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  48   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  49   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  50   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
  52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapInfoDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<FinalizerInfoDCmd>(full_export, true, false));
  57 #if INCLUDE_SERVICES // Heap dumping/inspection supported
  58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  59   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  61 #endif // INCLUDE_SERVICES
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
  64 
  65   // Enhanced JMX Agent Support
  66   // These commands won't be exported via the DiagnosticCommandMBean until an
  67   // appropriate permission is created for them
  68   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
  69   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
  70   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
  71   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
  72 
  73 }
  74 
  75 #ifndef HAVE_EXTRA_DCMD
  76 void DCmdRegistrant::register_dcmds_ext(){
  77    // Do nothing here
  78 }
  79 #endif
  80 
  81 
  82 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
  83   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
  84   _cmd("command name", "The name of the command for which we want help",
  85         "STRING", false) {
  86   _dcmdparser.add_dcmd_option(&_all);
  87   _dcmdparser.add_dcmd_argument(&_cmd);
  88 };
  89 
  90 void HelpDCmd::execute(DCmdSource source, TRAPS) {
  91   if (_all.value()) {
  92     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
  93     for (int i = 0; i < cmd_list->length(); i++) {
  94       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
  95                                                   strlen(cmd_list->at(i)));
  96       output()->print_cr("%s%s", factory->name(),
  97                          factory->is_enabled() ? "" : " [disabled]");
  98       output()->print_cr("\t%s", factory->description());
  99       output()->cr();
 100       factory = factory->next();
 101     }
 102   } else if (_cmd.has_value()) {
 103     DCmd* cmd = NULL;
 104     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
 105                                                 strlen(_cmd.value()));
 106     if (factory != NULL) {
 107       output()->print_cr("%s%s", factory->name(),
 108                          factory->is_enabled() ? "" : " [disabled]");
 109       output()->print_cr("%s", factory->description());
 110       output()->print_cr("\nImpact: %s", factory->impact());
 111       JavaPermission p = factory->permission();
 112       if(p._class != NULL) {
 113         if(p._action != NULL) {
 114           output()->print_cr("\nPermission: %s(%s, %s)",
 115                   p._class, p._name == NULL ? "null" : p._name, p._action);
 116         } else {
 117           output()->print_cr("\nPermission: %s(%s)",
 118                   p._class, p._name == NULL ? "null" : p._name);
 119         }
 120       }
 121       output()->cr();
 122       cmd = factory->create_resource_instance(output());
 123       if (cmd != NULL) {
 124         DCmdMark mark(cmd);
 125         cmd->print_help(factory->name());
 126       }
 127     } else {
 128       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 129     }
 130   } else {
 131     output()->print_cr("The following commands are available:");
 132     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 133     for (int i = 0; i < cmd_list->length(); i++) {
 134       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 135                                                   strlen(cmd_list->at(i)));
 136       output()->print_cr("%s%s", factory->name(),
 137                          factory->is_enabled() ? "" : " [disabled]");
 138       factory = factory->_next;
 139     }
 140     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 141   }
 142 }
 143 
 144 int HelpDCmd::num_arguments() {
 145   ResourceMark rm;
 146   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 147   if (dcmd != NULL) {
 148     DCmdMark mark(dcmd);
 149     return dcmd->_dcmdparser.num_arguments();
 150   } else {
 151     return 0;
 152   }
 153 }
 154 
 155 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 156   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 157           Abstract_VM_Version::vm_release());
 158   JDK_Version jdk_version = JDK_Version::current();
 159   if (jdk_version.update_version() > 0) {
 160     output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 161             jdk_version.minor_version(), jdk_version.update_version());
 162   } else {
 163     output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 164             jdk_version.minor_version());
 165   }
 166 }
 167 
 168 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 169                                    DCmdWithParser(output, heap),
 170   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 171   _dcmdparser.add_dcmd_option(&_all);
 172 }
 173 
 174 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 175   if (_all.value()) {
 176     CommandLineFlags::printFlags(output(), true);
 177   } else {
 178     CommandLineFlags::printSetFlags(output());
 179   }
 180 }
 181 
 182 int PrintVMFlagsDCmd::num_arguments() {
 183     ResourceMark rm;
 184     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 185     if (dcmd != NULL) {
 186       DCmdMark mark(dcmd);
 187       return dcmd->_dcmdparser.num_arguments();
 188     } else {
 189       return 0;
 190     }
 191 }
 192 
 193 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 194   // load sun.misc.VMSupport
 195   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 196   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 197   instanceKlassHandle ik (THREAD, k);
 198   if (ik->should_be_initialized()) {
 199     ik->initialize(THREAD);
 200   }
 201   if (HAS_PENDING_EXCEPTION) {
 202     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 203     output()->cr();
 204     CLEAR_PENDING_EXCEPTION;
 205     return;
 206   }
 207 
 208   // invoke the serializePropertiesToByteArray method
 209   JavaValue result(T_OBJECT);
 210   JavaCallArguments args;
 211 
 212   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 213   JavaCalls::call_static(&result,
 214                          ik,
 215                          vmSymbols::serializePropertiesToByteArray_name(),
 216                          signature,
 217                          &args,
 218                          THREAD);
 219   if (HAS_PENDING_EXCEPTION) {
 220     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 221     output()->cr();
 222     CLEAR_PENDING_EXCEPTION;
 223     return;
 224   }
 225 
 226   // The result should be a [B
 227   oop res = (oop)result.get_jobject();
 228   assert(res->is_typeArray(), "just checking");
 229   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 230 
 231   // copy the bytes to the output stream
 232   typeArrayOop ba = typeArrayOop(res);
 233   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 234   output()->print_raw((const char*)addr, ba->length());
 235 }
 236 
 237 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 238                            DCmdWithParser(output, heap),
 239   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 240   _dcmdparser.add_dcmd_option(&_date);
 241 }
 242 
 243 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 244   if (_date.value()) {
 245     output()->date_stamp(true, "", ": ");
 246   }
 247   output()->time_stamp().update_to(tty->time_stamp().ticks());
 248   output()->stamp();
 249   output()->print_cr(" s");
 250 }
 251 
 252 int VMUptimeDCmd::num_arguments() {
 253   ResourceMark rm;
 254   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 255   if (dcmd != NULL) {
 256     DCmdMark mark(dcmd);
 257     return dcmd->_dcmdparser.num_arguments();
 258   } else {
 259     return 0;
 260   }
 261 }
 262 
 263 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 264   if (!DisableExplicitGC) {
 265     Universe::heap()->collect(GCCause::_java_lang_system_gc);
 266   } else {
 267     output()->print_cr("Explicit GC is disabled, no GC has been performed.");
 268   }
 269 }
 270 
 271 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 272   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 273                                                  true, CHECK);
 274   instanceKlassHandle klass(THREAD, k);
 275   JavaValue result(T_VOID);
 276   JavaCalls::call_static(&result, klass,
 277                          vmSymbols::run_finalization_name(),
 278                          vmSymbols::void_method_signature(), CHECK);
 279 }
 280 
 281 void HeapInfoDCmd::execute(DCmdSource source, TRAPS) {
 282   Universe::heap()->print_on(output());
 283 }
 284 
 285 void FinalizerInfoDCmd::execute(DCmdSource source, TRAPS) {
 286   ResourceMark rm;
 287 
 288 
 289   Klass* k = SystemDictionary::resolve_or_null(
 290     vmSymbols::finalizer_histogram_klass(), THREAD);
 291   assert(k != NULL, "FinalizerHistogram class is not accessible");
 292 
 293   instanceKlassHandle klass(THREAD, k);
 294   JavaValue result(T_ARRAY);
 295 
 296   // We are calling lang.ref.FinalizerHistogram.getFinalizerHistogram() method
 297   // and expect it to return array of FinalizerHistogramEntry as Object[]
 298 
 299   JavaCalls::call_static(&result, klass,
 300                          vmSymbols::get_finalizer_histogram_name(),
 301                          vmSymbols::void_finalizer_histogram_entry_array_signature(), CHECK);
 302 
 303   objArrayOop result_oop = (objArrayOop) result.get_jobject();
 304   if (result_oop->length() == 0) {
 305     output()->print_cr("No instances waiting for finalization found");
 306     return;
 307   }
 308 
 309   oop foop = result_oop->obj_at(0);
 310   InstanceKlass* ik = InstanceKlass::cast(foop->klass());
 311 
 312   fieldDescriptor count_fd, name_fd;
 313 
 314   Klass* count_res = ik->find_field(
 315     vmSymbols::finalizer_histogram_entry_count_field(), vmSymbols::int_signature(), &count_fd);
 316 
 317   Klass* name_res = ik->find_field(
 318     vmSymbols::finalizer_histogram_entry_name_field(), vmSymbols::string_signature(), &name_fd);
 319 
 320   assert(count_res != NULL && name_res != NULL, "Unexpected layout of FinalizerHistogramEntry");
 321 
 322   output()->print_cr("Unreachable instances waiting for finalization");
 323   output()->print_cr("#instances  class name");
 324   output()->print_cr("-----------------------");
 325 
 326   for (int i = 0; i < result_oop->length(); ++i) {
 327     oop element_oop = result_oop->obj_at(i);
 328     oop str_oop = element_oop->obj_field(name_fd.offset());
 329     char *name = java_lang_String::as_utf8_string(str_oop);
 330     int count = element_oop->int_field(count_fd.offset());
 331     output()->print_cr("%10d  %s", count, name);
 332   }
 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_host
 500   ("jmxremote.host",
 501    "set com.sun.management.jmxremote.host", "STRING", false),
 502 
 503   _jmxremote_port
 504   ("jmxremote.port",
 505    "set com.sun.management.jmxremote.port", "STRING", false),
 506 
 507   _jmxremote_rmi_port
 508   ("jmxremote.rmi.port",
 509    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 510 
 511   _jmxremote_ssl
 512   ("jmxremote.ssl",
 513    "set com.sun.management.jmxremote.ssl", "STRING", false),
 514 
 515   _jmxremote_registry_ssl
 516   ("jmxremote.registry.ssl",
 517    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 518 
 519   _jmxremote_authenticate
 520   ("jmxremote.authenticate",
 521    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 522 
 523   _jmxremote_password_file
 524   ("jmxremote.password.file",
 525    "set com.sun.management.jmxremote.password.file", "STRING", false),
 526 
 527   _jmxremote_access_file
 528   ("jmxremote.access.file",
 529    "set com.sun.management.jmxremote.access.file", "STRING", false),
 530 
 531   _jmxremote_login_config
 532   ("jmxremote.login.config",
 533    "set com.sun.management.jmxremote.login.config", "STRING", false),
 534 
 535   _jmxremote_ssl_enabled_cipher_suites
 536   ("jmxremote.ssl.enabled.cipher.suites",
 537    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 538 
 539   _jmxremote_ssl_enabled_protocols
 540   ("jmxremote.ssl.enabled.protocols",
 541    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 542 
 543   _jmxremote_ssl_need_client_auth
 544   ("jmxremote.ssl.need.client.auth",
 545    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 546 
 547   _jmxremote_ssl_config_file
 548   ("jmxremote.ssl.config.file",
 549    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 550 
 551 // JDP Protocol support
 552   _jmxremote_autodiscovery
 553   ("jmxremote.autodiscovery",
 554    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 555 
 556    _jdp_port
 557   ("jdp.port",
 558    "set com.sun.management.jdp.port", "INT", false),
 559 
 560    _jdp_address
 561   ("jdp.address",
 562    "set com.sun.management.jdp.address", "STRING", false),
 563 
 564    _jdp_source_addr
 565   ("jdp.source_addr",
 566    "set com.sun.management.jdp.source_addr", "STRING", false),
 567 
 568    _jdp_ttl
 569   ("jdp.ttl",
 570    "set com.sun.management.jdp.ttl", "INT", false),
 571 
 572    _jdp_pause
 573   ("jdp.pause",
 574    "set com.sun.management.jdp.pause", "INT", false),
 575 
 576    _jdp_name
 577   ("jdp.name",
 578    "set com.sun.management.jdp.name", "STRING", false)
 579 
 580   {
 581     _dcmdparser.add_dcmd_option(&_config_file);
 582     _dcmdparser.add_dcmd_option(&_jmxremote_host);
 583     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 584     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 585     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 586     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 587     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 588     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 589     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 590     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 591     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 592     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 593     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 594     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 595     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 596     _dcmdparser.add_dcmd_option(&_jdp_port);
 597     _dcmdparser.add_dcmd_option(&_jdp_address);
 598     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 599     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 600     _dcmdparser.add_dcmd_option(&_jdp_pause);
 601     _dcmdparser.add_dcmd_option(&_jdp_name);
 602 }
 603 
 604 
 605 int JMXStartRemoteDCmd::num_arguments() {
 606   ResourceMark rm;
 607   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 608   if (dcmd != NULL) {
 609     DCmdMark mark(dcmd);
 610     return dcmd->_dcmdparser.num_arguments();
 611   } else {
 612     return 0;
 613   }
 614 }
 615 
 616 
 617 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 618     ResourceMark rm(THREAD);
 619     HandleMark hm(THREAD);
 620 
 621     // Load and initialize the sun.management.Agent class
 622     // invoke startRemoteManagementAgent(string) method to start
 623     // the remote management server.
 624     // throw java.lang.NoSuchMethodError if the method doesn't exist
 625 
 626     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 627     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 628     instanceKlassHandle ik (THREAD, k);
 629 
 630     JavaValue result(T_VOID);
 631 
 632     // Pass all command line arguments to java as key=value,...
 633     // All checks are done on java side
 634 
 635     int len = 0;
 636     stringStream options;
 637     char comma[2] = {0,0};
 638 
 639     // Leave default values on Agent.class side and pass only
 640     // agruments explicitly set by user. All arguments passed
 641     // to jcmd override properties with the same name set by
 642     // command line with -D or by managmenent.properties
 643     // file.
 644 #define PUT_OPTION(a) \
 645     if ( (a).is_set() ){ \
 646         options.print(\
 647                ( *((a).type()) == 'I' ) ? "%scom.sun.management.%s=%d" : "%scom.sun.management.%s=%s",\
 648                 comma, (a).name(), (a).value()); \
 649         comma[0] = ','; \
 650     }
 651 
 652     PUT_OPTION(_config_file);
 653     PUT_OPTION(_jmxremote_host);
 654     PUT_OPTION(_jmxremote_port);
 655     PUT_OPTION(_jmxremote_rmi_port);
 656     PUT_OPTION(_jmxremote_ssl);
 657     PUT_OPTION(_jmxremote_registry_ssl);
 658     PUT_OPTION(_jmxremote_authenticate);
 659     PUT_OPTION(_jmxremote_password_file);
 660     PUT_OPTION(_jmxremote_access_file);
 661     PUT_OPTION(_jmxremote_login_config);
 662     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 663     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 664     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 665     PUT_OPTION(_jmxremote_ssl_config_file);
 666     PUT_OPTION(_jmxremote_autodiscovery);
 667     PUT_OPTION(_jdp_port);
 668     PUT_OPTION(_jdp_address);
 669     PUT_OPTION(_jdp_source_addr);
 670     PUT_OPTION(_jdp_ttl);
 671     PUT_OPTION(_jdp_pause);
 672     PUT_OPTION(_jdp_name);
 673 
 674 #undef PUT_OPTION
 675 
 676     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 677     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 678 }
 679 
 680 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 681   DCmd(output, heap_allocated) {
 682   // do nothing
 683 }
 684 
 685 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 686     ResourceMark rm(THREAD);
 687     HandleMark hm(THREAD);
 688 
 689     // Load and initialize the sun.management.Agent class
 690     // invoke startLocalManagementAgent(void) method to start
 691     // the local management server
 692     // throw java.lang.NoSuchMethodError if method doesn't exist
 693 
 694     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 695     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 696     instanceKlassHandle ik (THREAD, k);
 697 
 698     JavaValue result(T_VOID);
 699     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 700 }
 701 
 702 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 703     ResourceMark rm(THREAD);
 704     HandleMark hm(THREAD);
 705 
 706     // Load and initialize the sun.management.Agent class
 707     // invoke stopRemoteManagementAgent method to stop the
 708     // management server
 709     // throw java.lang.NoSuchMethodError if method doesn't exist
 710 
 711     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 712     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 713     instanceKlassHandle ik (THREAD, k);
 714 
 715     JavaValue result(T_VOID);
 716     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 717 }
 718 
 719 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 720   DCmd(output, heap_allocated) {
 721   // do nothing
 722 }
 723 
 724 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 725   os::print_dll_info(output());
 726   output()->cr();
 727 }
 728 
 729 void RotateGCLogDCmd::execute(DCmdSource source, TRAPS) {
 730   if (UseGCLogFileRotation) {
 731     VM_RotateGCLog rotateop(output());
 732     VMThread::execute(&rotateop);
 733   } else {
 734     output()->print_cr("Target VM does not support GC log file rotation.");
 735   }
 736 }