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