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/method.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/javaCalls.hpp"
  32 #include "runtime/os.hpp"
  33 #include "services/diagnosticArgument.hpp"
  34 #include "services/diagnosticCommand.hpp"
  35 #include "services/diagnosticFramework.hpp"
  36 #include "services/heapDumper.hpp"
  37 #include "services/management.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<VMDynamicLibrariesDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  58 #if INCLUDE_SERVICES // Heap dumping/inspection supported
  59   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  61   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
  64   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
  65 #endif // INCLUDE_SERVICES
  66   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  67   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
  68   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
  69   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
  70   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
  71   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
  72   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<MethodUsageDCmd>(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 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 291 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 292                            DCmdWithParser(output, heap),
 293   _filename("filename","Name of the dump file", "STRING",true),
 294   _all("-all", "Dump all objects, including unreachable objects",
 295        "BOOLEAN", false, "false") {
 296   _dcmdparser.add_dcmd_option(&_all);
 297   _dcmdparser.add_dcmd_argument(&_filename);
 298 }
 299 
 300 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 301   // Request a full GC before heap dump if _all is false
 302   // This helps reduces the amount of unreachable objects in the dump
 303   // and makes it easier to browse.
 304   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 305   int res = dumper.dump(_filename.value());
 306   if (res == 0) {
 307     output()->print_cr("Heap dump file created");
 308   } else {
 309     // heap dump failed
 310     ResourceMark rm;
 311     char* error = dumper.error_as_C_string();
 312     if (error == NULL) {
 313       output()->print_cr("Dump failed - reason unknown");
 314     } else {
 315       output()->print_cr("%s", error);
 316     }
 317   }
 318 }
 319 
 320 int HeapDumpDCmd::num_arguments() {
 321   ResourceMark rm;
 322   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 323   if (dcmd != NULL) {
 324     DCmdMark mark(dcmd);
 325     return dcmd->_dcmdparser.num_arguments();
 326   } else {
 327     return 0;
 328   }
 329 }
 330 
 331 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 332                                        DCmdWithParser(output, heap),
 333   _all("-all", "Inspect all objects, including unreachable objects",
 334        "BOOLEAN", false, "false") {
 335   _dcmdparser.add_dcmd_option(&_all);
 336 }
 337 
 338 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 339   VM_GC_HeapInspection heapop(output(),
 340                               !_all.value() /* request full gc if false */);
 341   VMThread::execute(&heapop);
 342 }
 343 
 344 int ClassHistogramDCmd::num_arguments() {
 345   ResourceMark rm;
 346   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 347   if (dcmd != NULL) {
 348     DCmdMark mark(dcmd);
 349     return dcmd->_dcmdparser.num_arguments();
 350   } else {
 351     return 0;
 352   }
 353 }
 354 
 355 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 356 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 357                                        DCmdWithParser(output, heap),
 358   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 359        "BOOLEAN", false, "false"),
 360   _all("-all", "Show all columns",
 361        "BOOLEAN", false, "false"),
 362   _help("-help", "Show meaning of all the columns",
 363        "BOOLEAN", false, "false"),
 364   _columns("columns", "Comma-separated list of all the columns to show. "
 365            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 366            "STRING", false) {
 367   _dcmdparser.add_dcmd_option(&_all);
 368   _dcmdparser.add_dcmd_option(&_csv);
 369   _dcmdparser.add_dcmd_option(&_help);
 370   _dcmdparser.add_dcmd_argument(&_columns);
 371 }
 372 
 373 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 374   if (!UnlockDiagnosticVMOptions) {
 375     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 376     return;
 377   }
 378 
 379   VM_GC_HeapInspection heapop(output(),
 380                               true /* request_full_gc */);
 381   heapop.set_csv_format(_csv.value());
 382   heapop.set_print_help(_help.value());
 383   heapop.set_print_class_stats(true);
 384   if (_all.value()) {
 385     if (_columns.has_value()) {
 386       output()->print_cr("Cannot specify -all and individual columns at the same time");
 387       return;
 388     } else {
 389       heapop.set_columns(NULL);
 390     }
 391   } else {
 392     if (_columns.has_value()) {
 393       heapop.set_columns(_columns.value());
 394     } else {
 395       heapop.set_columns(DEFAULT_COLUMNS);
 396     }
 397   }
 398   VMThread::execute(&heapop);
 399 }
 400 
 401 int ClassStatsDCmd::num_arguments() {
 402   ResourceMark rm;
 403   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 404   if (dcmd != NULL) {
 405     DCmdMark mark(dcmd);
 406     return dcmd->_dcmdparser.num_arguments();
 407   } else {
 408     return 0;
 409   }
 410 }
 411 #endif // INCLUDE_SERVICES
 412 
 413 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 414                                DCmdWithParser(output, heap),
 415   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 416   _dcmdparser.add_dcmd_option(&_locks);
 417 }
 418 
 419 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 420   // thread stacks
 421   VM_PrintThreads op1(output(), _locks.value());
 422   VMThread::execute(&op1);
 423 
 424   // JNI global handles
 425   VM_PrintJNI op2(output());
 426   VMThread::execute(&op2);
 427 
 428   // Deadlock detection
 429   VM_FindDeadlocks op3(output());
 430   VMThread::execute(&op3);
 431 }
 432 
 433 int ThreadDumpDCmd::num_arguments() {
 434   ResourceMark rm;
 435   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 436   if (dcmd != NULL) {
 437     DCmdMark mark(dcmd);
 438     return dcmd->_dcmdparser.num_arguments();
 439   } else {
 440     return 0;
 441   }
 442 }
 443 
 444 // Enhanced JMX Agent support
 445 
 446 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 447 
 448   DCmdWithParser(output, heap_allocated),
 449 
 450   _config_file
 451   ("config.file",
 452    "set com.sun.management.config.file", "STRING", false),
 453 
 454   _jmxremote_port
 455   ("jmxremote.port",
 456    "set com.sun.management.jmxremote.port", "STRING", false),
 457 
 458   _jmxremote_rmi_port
 459   ("jmxremote.rmi.port",
 460    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 461 
 462   _jmxremote_ssl
 463   ("jmxremote.ssl",
 464    "set com.sun.management.jmxremote.ssl", "STRING", false),
 465 
 466   _jmxremote_registry_ssl
 467   ("jmxremote.registry.ssl",
 468    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 469 
 470   _jmxremote_authenticate
 471   ("jmxremote.authenticate",
 472    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 473 
 474   _jmxremote_password_file
 475   ("jmxremote.password.file",
 476    "set com.sun.management.jmxremote.password.file", "STRING", false),
 477 
 478   _jmxremote_access_file
 479   ("jmxremote.access.file",
 480    "set com.sun.management.jmxremote.access.file", "STRING", false),
 481 
 482   _jmxremote_login_config
 483   ("jmxremote.login.config",
 484    "set com.sun.management.jmxremote.login.config", "STRING", false),
 485 
 486   _jmxremote_ssl_enabled_cipher_suites
 487   ("jmxremote.ssl.enabled.cipher.suites",
 488    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 489 
 490   _jmxremote_ssl_enabled_protocols
 491   ("jmxremote.ssl.enabled.protocols",
 492    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 493 
 494   _jmxremote_ssl_need_client_auth
 495   ("jmxremote.ssl.need.client.auth",
 496    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 497 
 498   _jmxremote_ssl_config_file
 499   ("jmxremote.ssl.config.file",
 500    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 501 
 502 // JDP Protocol support
 503   _jmxremote_autodiscovery
 504   ("jmxremote.autodiscovery",
 505    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 506 
 507    _jdp_port
 508   ("jdp.port",
 509    "set com.sun.management.jdp.port", "INT", false),
 510 
 511    _jdp_address
 512   ("jdp.address",
 513    "set com.sun.management.jdp.address", "STRING", false),
 514 
 515    _jdp_source_addr
 516   ("jdp.source_addr",
 517    "set com.sun.management.jdp.source_addr", "STRING", false),
 518 
 519    _jdp_ttl
 520   ("jdp.ttl",
 521    "set com.sun.management.jdp.ttl", "INT", false),
 522 
 523    _jdp_pause
 524   ("jdp.pause",
 525    "set com.sun.management.jdp.pause", "INT", false),
 526 
 527    _jdp_name
 528   ("jdp.name",
 529    "set com.sun.management.jdp.name", "STRING", false)
 530 
 531   {
 532     _dcmdparser.add_dcmd_option(&_config_file);
 533     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 534     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 535     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 536     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 537     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 538     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 539     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 540     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 541     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 542     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 543     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 544     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 545     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 546     _dcmdparser.add_dcmd_option(&_jdp_port);
 547     _dcmdparser.add_dcmd_option(&_jdp_address);
 548     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 549     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 550     _dcmdparser.add_dcmd_option(&_jdp_pause);
 551     _dcmdparser.add_dcmd_option(&_jdp_name);
 552 }
 553 
 554 
 555 int JMXStartRemoteDCmd::num_arguments() {
 556   ResourceMark rm;
 557   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 558   if (dcmd != NULL) {
 559     DCmdMark mark(dcmd);
 560     return dcmd->_dcmdparser.num_arguments();
 561   } else {
 562     return 0;
 563   }
 564 }
 565 
 566 
 567 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 568     ResourceMark rm(THREAD);
 569     HandleMark hm(THREAD);
 570 
 571     // Load and initialize the sun.management.Agent class
 572     // invoke startRemoteManagementAgent(string) method to start
 573     // the remote management server.
 574     // throw java.lang.NoSuchMethodError if the method doesn't exist
 575 
 576     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 577     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 578     instanceKlassHandle ik (THREAD, k);
 579 
 580     JavaValue result(T_VOID);
 581 
 582     // Pass all command line arguments to java as key=value,...
 583     // All checks are done on java side
 584 
 585     int len = 0;
 586     stringStream options;
 587     char comma[2] = {0,0};
 588 
 589     // Leave default values on Agent.class side and pass only
 590     // agruments explicitly set by user. All arguments passed
 591     // to jcmd override properties with the same name set by
 592     // command line with -D or by managmenent.properties
 593     // file.
 594 #define PUT_OPTION(a) \
 595     if ( (a).is_set() ){ \
 596         options.print(\
 597                ( *((a).type()) == 'I' ) ? "%scom.sun.management.%s=%d" : "%scom.sun.management.%s=%s",\
 598                 comma, (a).name(), (a).value()); \
 599         comma[0] = ','; \
 600     }
 601 
 602     PUT_OPTION(_config_file);
 603     PUT_OPTION(_jmxremote_port);
 604     PUT_OPTION(_jmxremote_rmi_port);
 605     PUT_OPTION(_jmxremote_ssl);
 606     PUT_OPTION(_jmxremote_registry_ssl);
 607     PUT_OPTION(_jmxremote_authenticate);
 608     PUT_OPTION(_jmxremote_password_file);
 609     PUT_OPTION(_jmxremote_access_file);
 610     PUT_OPTION(_jmxremote_login_config);
 611     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 612     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 613     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 614     PUT_OPTION(_jmxremote_ssl_config_file);
 615     PUT_OPTION(_jmxremote_autodiscovery);
 616     PUT_OPTION(_jdp_port);
 617     PUT_OPTION(_jdp_address);
 618     PUT_OPTION(_jdp_source_addr);
 619     PUT_OPTION(_jdp_ttl);
 620     PUT_OPTION(_jdp_pause);
 621     PUT_OPTION(_jdp_name);
 622 
 623 #undef PUT_OPTION
 624 
 625     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 626     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 627 }
 628 
 629 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 630   DCmd(output, heap_allocated) {
 631   // do nothing
 632 }
 633 
 634 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 635     ResourceMark rm(THREAD);
 636     HandleMark hm(THREAD);
 637 
 638     // Load and initialize the sun.management.Agent class
 639     // invoke startLocalManagementAgent(void) method to start
 640     // the local management server
 641     // throw java.lang.NoSuchMethodError if method doesn't exist
 642 
 643     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 644     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 645     instanceKlassHandle ik (THREAD, k);
 646 
 647     JavaValue result(T_VOID);
 648     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 649 }
 650 
 651 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 652     ResourceMark rm(THREAD);
 653     HandleMark hm(THREAD);
 654 
 655     // Load and initialize the sun.management.Agent class
 656     // invoke stopRemoteManagementAgent method to stop the
 657     // management server
 658     // throw java.lang.NoSuchMethodError if method doesn't exist
 659 
 660     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 661     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 662     instanceKlassHandle ik (THREAD, k);
 663 
 664     JavaValue result(T_VOID);
 665     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 666 }
 667 
 668 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 669   DCmd(output, heap_allocated) {
 670   // do nothing
 671 }
 672 
 673 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 674   os::print_dll_info(output());
 675   output()->cr();
 676 }
 677 
 678 void RotateGCLogDCmd::execute(DCmdSource source, TRAPS) {
 679   if (UseGCLogFileRotation) {
 680     VM_RotateGCLog rotateop(output());
 681     VMThread::execute(&rotateop);
 682   } else {
 683     output()->print_cr("Target VM does not support GC log file rotation.");
 684   }
 685 }
 686 
 687 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 688   VM_PrintCompileQueue printCompileQueueOp(output());
 689   VMThread::execute(&printCompileQueueOp);
 690 }
 691 
 692 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 693   VM_PrintCodeList printCodeListOp(output());
 694   VMThread::execute(&printCodeListOp);
 695 }
 696 
 697 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 698   VM_PrintCodeCache printCodeCacheOp(output());
 699   VMThread::execute(&printCodeCacheOp);
 700 }
 701 
 702 #if INCLUDE_SERVICES
 703 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 704                                        DCmdWithParser(output, heap),
 705   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 706   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 707                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 708   _classname("classname", "Name of class whose hierarchy should be printed. "
 709              "If not specified, all class hierarchies are printed.",
 710              "STRING", false) {
 711   _dcmdparser.add_dcmd_option(&_print_interfaces);
 712   _dcmdparser.add_dcmd_option(&_print_subclasses);
 713   _dcmdparser.add_dcmd_argument(&_classname);
 714 }
 715 
 716 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 717   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 718                                                _print_subclasses.value(), _classname.value());
 719   VMThread::execute(&printClassHierarchyOp);
 720 }
 721 
 722 int ClassHierarchyDCmd::num_arguments() {
 723   ResourceMark rm;
 724   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 725   if (dcmd != NULL) {
 726     DCmdMark mark(dcmd);
 727     return dcmd->_dcmdparser.num_arguments();
 728   } else {
 729     return 0;
 730   }
 731 }
 732 
 733 #endif
 734 
 735 class VM_DumpMethodUsage : public VM_Operation {
 736 private:
 737   outputStream* _out;
 738 public:
 739   VM_DumpMethodUsage(outputStream* out) {
 740     _out = out;
 741   }
 742 
 743   virtual VMOp_Type type() const { return VMOp_DumpMethodUsage; }
 744 
 745   virtual void doit() {
 746     Method::print_usage(_out);
 747   }
 748 };
 749 
 750 MethodUsageDCmd::MethodUsageDCmd(outputStream* output, bool heap) :
 751                                        DCmdWithParser(output, heap)
 752 {}
 753 
 754 void MethodUsageDCmd::execute(DCmdSource source, TRAPS) {
 755   if (!UnlockDiagnosticVMOptions) {
 756     output()->print_cr("VM.method_usage command requires -XX:+UnlockDiagnosticVMOptions");
 757     return;
 758   }
 759   VM_DumpMethodUsage dumper(output());
 760   VMThread::execute(&dumper);
 761 }
 762 
 763 int MethodUsageDCmd::num_arguments() {
 764   return 0;
 765 }