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