1 /*
   2  * Copyright (c) 2011, 2016, 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 "compiler/compileBroker.hpp"
  29 #include "compiler/directivesParser.hpp"
  30 #include "gc/shared/vmGCOperations.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/globals.hpp"
  34 #include "runtime/javaCalls.hpp"
  35 #include "runtime/os.hpp"
  36 #include "services/diagnosticArgument.hpp"
  37 #include "services/diagnosticCommand.hpp"
  38 #include "services/diagnosticFramework.hpp"
  39 #include "services/heapDumper.hpp"
  40 #include "services/management.hpp"
  41 #include "services/writeableFlags.hpp"
  42 #include "utilities/macros.hpp"
  43 #include "oops/objArrayOop.inline.hpp"
  44 
  45 void DCmdRegistrant::register_dcmds(){
  46   // Registration of the diagnostic commands
  47   // First argument specifies which interfaces will export the command
  48   // Second argument specifies if the command is enabled
  49   // Third  argument specifies if the command is hidden
  50   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  51                          | DCmd_Source_MBean;
  52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
  58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
  59   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMInfoDCmd>(full_export, true, false));
  61   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapInfoDCmd>(full_export, true, false));
  64   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<FinalizerInfoDCmd>(full_export, true, false));
  65 #if INCLUDE_SERVICES
  66   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  67   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  68   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  69   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
  70   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
  71   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
  72 #if INCLUDE_JVMTI // Both JVMTI and SERVICES have to be enabled to have this dcmd
  73   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIAgentLoadDCmd>(full_export, true, false));
  74 #endif // INCLUDE_JVMTI
  75 #endif // INCLUDE_SERVICES
  76 #if INCLUDE_JVMTI
  77   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
  78 #endif // INCLUDE_JVMTI
  79   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  80   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
  81   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
  82   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
  83   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
  84   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<TouchedMethodsDCmd>(full_export, true, false));
  85 
  86   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesPrintDCmd>(full_export, true, false));
  87   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesAddDCmd>(full_export, true, false));
  88   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesRemoveDCmd>(full_export, true, false));
  89   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesClearDCmd>(full_export, true, false));
  90 
  91   // Enhanced JMX Agent Support
  92   // These commands won't be exported via the DiagnosticCommandMBean until an
  93   // appropriate permission is created for them
  94   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
  95   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
  96   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
  97   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
  98   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStatusDCmd>(jmx_agent_export_flags, true,false));
  99 
 100 }
 101 
 102 #ifndef HAVE_EXTRA_DCMD
 103 void DCmdRegistrant::register_dcmds_ext(){
 104    // Do nothing here
 105 }
 106 #endif
 107 
 108 
 109 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
 110   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
 111   _cmd("command name", "The name of the command for which we want help",
 112         "STRING", false) {
 113   _dcmdparser.add_dcmd_option(&_all);
 114   _dcmdparser.add_dcmd_argument(&_cmd);
 115 };
 116 
 117 void HelpDCmd::execute(DCmdSource source, TRAPS) {
 118   if (_all.value()) {
 119     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
 120     for (int i = 0; i < cmd_list->length(); i++) {
 121       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 122                                                   strlen(cmd_list->at(i)));
 123       output()->print_cr("%s%s", factory->name(),
 124                          factory->is_enabled() ? "" : " [disabled]");
 125       output()->print_cr("\t%s", factory->description());
 126       output()->cr();
 127       factory = factory->next();
 128     }
 129   } else if (_cmd.has_value()) {
 130     DCmd* cmd = NULL;
 131     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
 132                                                 strlen(_cmd.value()));
 133     if (factory != NULL) {
 134       output()->print_cr("%s%s", factory->name(),
 135                          factory->is_enabled() ? "" : " [disabled]");
 136       output()->print_cr("%s", factory->description());
 137       output()->print_cr("\nImpact: %s", factory->impact());
 138       JavaPermission p = factory->permission();
 139       if(p._class != NULL) {
 140         if(p._action != NULL) {
 141           output()->print_cr("\nPermission: %s(%s, %s)",
 142                   p._class, p._name == NULL ? "null" : p._name, p._action);
 143         } else {
 144           output()->print_cr("\nPermission: %s(%s)",
 145                   p._class, p._name == NULL ? "null" : p._name);
 146         }
 147       }
 148       output()->cr();
 149       cmd = factory->create_resource_instance(output());
 150       if (cmd != NULL) {
 151         DCmdMark mark(cmd);
 152         cmd->print_help(factory->name());
 153       }
 154     } else {
 155       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 156     }
 157   } else {
 158     output()->print_cr("The following commands are available:");
 159     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 160     for (int i = 0; i < cmd_list->length(); i++) {
 161       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 162                                                   strlen(cmd_list->at(i)));
 163       output()->print_cr("%s%s", factory->name(),
 164                          factory->is_enabled() ? "" : " [disabled]");
 165       factory = factory->_next;
 166     }
 167     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 168   }
 169 }
 170 
 171 int HelpDCmd::num_arguments() {
 172   ResourceMark rm;
 173   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 174   if (dcmd != NULL) {
 175     DCmdMark mark(dcmd);
 176     return dcmd->_dcmdparser.num_arguments();
 177   } else {
 178     return 0;
 179   }
 180 }
 181 
 182 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 183   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 184           Abstract_VM_Version::vm_release());
 185   JDK_Version jdk_version = JDK_Version::current();
 186   if (jdk_version.patch_version() > 0) {
 187     output()->print_cr("JDK %d.%d.%d.%d", jdk_version.major_version(),
 188             jdk_version.minor_version(), jdk_version.security_version(),
 189             jdk_version.patch_version());
 190   } else {
 191     output()->print_cr("JDK %d.%d.%d", jdk_version.major_version(),
 192             jdk_version.minor_version(), jdk_version.security_version());
 193   }
 194 }
 195 
 196 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 197                                    DCmdWithParser(output, heap),
 198   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 199   _dcmdparser.add_dcmd_option(&_all);
 200 }
 201 
 202 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 203   if (_all.value()) {
 204     CommandLineFlags::printFlags(output(), true);
 205   } else {
 206     CommandLineFlags::printSetFlags(output());
 207   }
 208 }
 209 
 210 int PrintVMFlagsDCmd::num_arguments() {
 211     ResourceMark rm;
 212     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 213     if (dcmd != NULL) {
 214       DCmdMark mark(dcmd);
 215       return dcmd->_dcmdparser.num_arguments();
 216     } else {
 217       return 0;
 218     }
 219 }
 220 
 221 SetVMFlagDCmd::SetVMFlagDCmd(outputStream* output, bool heap) :
 222                                    DCmdWithParser(output, heap),
 223   _flag("flag name", "The name of the flag we want to set",
 224         "STRING", true),
 225   _value("string value", "The value we want to set", "STRING", false) {
 226   _dcmdparser.add_dcmd_argument(&_flag);
 227   _dcmdparser.add_dcmd_argument(&_value);
 228 }
 229 
 230 void SetVMFlagDCmd::execute(DCmdSource source, TRAPS) {
 231   const char* val = NULL;
 232   if (_value.value() != NULL) {
 233     val = _value.value();
 234   }
 235 
 236   FormatBuffer<80> err_msg("%s", "");
 237   int ret = WriteableFlags::set_flag(_flag.value(), val, Flag::MANAGEMENT, err_msg);
 238 
 239   if (ret != Flag::SUCCESS) {
 240     output()->print_cr("%s", err_msg.buffer());
 241   }
 242 }
 243 
 244 int SetVMFlagDCmd::num_arguments() {
 245   ResourceMark rm;
 246   SetVMFlagDCmd* dcmd = new SetVMFlagDCmd(NULL, false);
 247   if (dcmd != NULL) {
 248     DCmdMark mark(dcmd);
 249     return dcmd->_dcmdparser.num_arguments();
 250   } else {
 251     return 0;
 252   }
 253 }
 254 
 255 void JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
 256   if (JvmtiExport::should_post_data_dump()) {
 257     JvmtiExport::post_data_dump();
 258   }
 259 }
 260 
 261 #if INCLUDE_SERVICES
 262 JVMTIAgentLoadDCmd::JVMTIAgentLoadDCmd(outputStream* output, bool heap) :
 263                                        DCmdWithParser(output, heap),
 264   _libpath("library path", "Absolute path of the JVMTI agent to load.",
 265            "STRING", true),
 266   _option("agent option", "Option string to pass the agent.", "STRING", false) {
 267   _dcmdparser.add_dcmd_argument(&_libpath);
 268   _dcmdparser.add_dcmd_argument(&_option);
 269 }
 270 
 271 void JVMTIAgentLoadDCmd::execute(DCmdSource source, TRAPS) {
 272 
 273   if (_libpath.value() == NULL) {
 274     output()->print_cr("JVMTI.agent_load dcmd needs library path.");
 275     return;
 276   }
 277 
 278   char *suffix = strrchr(_libpath.value(), '.');
 279   bool is_java_agent = (suffix != NULL) && (strncmp(".jar", suffix, 4) == 0);
 280 
 281   if (is_java_agent) {
 282     if (_option.value() == NULL) {
 283       JvmtiExport::load_agent_library("instrument", "false",
 284                                       _libpath.value(), output());
 285     } else {
 286       size_t opt_len = strlen(_libpath.value()) + strlen(_option.value()) + 2;
 287       if (opt_len > 4096) {
 288         output()->print_cr("JVMTI agent attach failed: Options is too long.");
 289         return;
 290       }
 291 
 292       char *opt = (char *)os::malloc(opt_len, mtInternal);
 293       if (opt == NULL) {
 294         output()->print_cr("JVMTI agent attach failed: "
 295                            "Could not allocate %zu bytes for argument.",
 296                            opt_len);
 297         return;
 298       }
 299 
 300       jio_snprintf(opt, opt_len, "%s=%s", _libpath.value(), _option.value());
 301       JvmtiExport::load_agent_library("instrument", "false", opt, output());
 302 
 303       os::free(opt);
 304     }
 305   } else {
 306     JvmtiExport::load_agent_library(_libpath.value(), "true",
 307                                     _option.value(), output());
 308   }
 309 }
 310 
 311 int JVMTIAgentLoadDCmd::num_arguments() {
 312   ResourceMark rm;
 313   JVMTIAgentLoadDCmd* dcmd = new JVMTIAgentLoadDCmd(NULL, false);
 314   if (dcmd != NULL) {
 315     DCmdMark mark(dcmd);
 316     return dcmd->_dcmdparser.num_arguments();
 317   } else {
 318     return 0;
 319   }
 320 }
 321 #endif // INCLUDE_SERVICES
 322 
 323 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 324   // load VMSupport
 325   Symbol* klass = vmSymbols::jdk_internal_vm_VMSupport();
 326   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 327   instanceKlassHandle ik (THREAD, k);
 328   if (ik->should_be_initialized()) {
 329     ik->initialize(THREAD);
 330   }
 331   if (HAS_PENDING_EXCEPTION) {
 332     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 333     output()->cr();
 334     CLEAR_PENDING_EXCEPTION;
 335     return;
 336   }
 337 
 338   // invoke the serializePropertiesToByteArray method
 339   JavaValue result(T_OBJECT);
 340   JavaCallArguments args;
 341 
 342   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 343   JavaCalls::call_static(&result,
 344                          ik,
 345                          vmSymbols::serializePropertiesToByteArray_name(),
 346                          signature,
 347                          &args,
 348                          THREAD);
 349   if (HAS_PENDING_EXCEPTION) {
 350     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 351     output()->cr();
 352     CLEAR_PENDING_EXCEPTION;
 353     return;
 354   }
 355 
 356   // The result should be a [B
 357   oop res = (oop)result.get_jobject();
 358   assert(res->is_typeArray(), "just checking");
 359   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 360 
 361   // copy the bytes to the output stream
 362   typeArrayOop ba = typeArrayOop(res);
 363   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 364   output()->print_raw((const char*)addr, ba->length());
 365 }
 366 
 367 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 368                            DCmdWithParser(output, heap),
 369   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 370   _dcmdparser.add_dcmd_option(&_date);
 371 }
 372 
 373 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 374   if (_date.value()) {
 375     output()->date_stamp(true, "", ": ");
 376   }
 377   output()->time_stamp().update_to(tty->time_stamp().ticks());
 378   output()->stamp();
 379   output()->print_cr(" s");
 380 }
 381 
 382 int VMUptimeDCmd::num_arguments() {
 383   ResourceMark rm;
 384   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 385   if (dcmd != NULL) {
 386     DCmdMark mark(dcmd);
 387     return dcmd->_dcmdparser.num_arguments();
 388   } else {
 389     return 0;
 390   }
 391 }
 392 
 393 void VMInfoDCmd::execute(DCmdSource source, TRAPS) {
 394   VMError::print_vm_info(_output);
 395 }
 396 
 397 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 398   if (!DisableExplicitGC) {
 399     Universe::heap()->collect(GCCause::_dcmd_gc_run);
 400   } else {
 401     output()->print_cr("Explicit GC is disabled, no GC has been performed.");
 402   }
 403 }
 404 
 405 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 406   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 407                                                  true, CHECK);
 408   instanceKlassHandle klass(THREAD, k);
 409   JavaValue result(T_VOID);
 410   JavaCalls::call_static(&result, klass,
 411                          vmSymbols::run_finalization_name(),
 412                          vmSymbols::void_method_signature(), CHECK);
 413 }
 414 
 415 void HeapInfoDCmd::execute(DCmdSource source, TRAPS) {
 416   MutexLocker hl(Heap_lock);
 417   Universe::heap()->print_on(output());
 418 }
 419 
 420 void FinalizerInfoDCmd::execute(DCmdSource source, TRAPS) {
 421   ResourceMark rm;
 422 
 423 
 424   Klass* k = SystemDictionary::resolve_or_null(
 425     vmSymbols::finalizer_histogram_klass(), THREAD);
 426   assert(k != NULL, "FinalizerHistogram class is not accessible");
 427 
 428   instanceKlassHandle klass(THREAD, k);
 429   JavaValue result(T_ARRAY);
 430 
 431   // We are calling lang.ref.FinalizerHistogram.getFinalizerHistogram() method
 432   // and expect it to return array of FinalizerHistogramEntry as Object[]
 433 
 434   JavaCalls::call_static(&result, klass,
 435                          vmSymbols::get_finalizer_histogram_name(),
 436                          vmSymbols::void_finalizer_histogram_entry_array_signature(), CHECK);
 437 
 438   objArrayOop result_oop = (objArrayOop) result.get_jobject();
 439   if (result_oop->length() == 0) {
 440     output()->print_cr("No instances waiting for finalization found");
 441     return;
 442   }
 443 
 444   oop foop = result_oop->obj_at(0);
 445   InstanceKlass* ik = InstanceKlass::cast(foop->klass());
 446 
 447   fieldDescriptor count_fd, name_fd;
 448 
 449   Klass* count_res = ik->find_field(
 450     vmSymbols::finalizer_histogram_entry_count_field(), vmSymbols::int_signature(), &count_fd);
 451 
 452   Klass* name_res = ik->find_field(
 453     vmSymbols::finalizer_histogram_entry_name_field(), vmSymbols::string_signature(), &name_fd);
 454 
 455   assert(count_res != NULL && name_res != NULL, "Unexpected layout of FinalizerHistogramEntry");
 456 
 457   output()->print_cr("Unreachable instances waiting for finalization");
 458   output()->print_cr("#instances  class name");
 459   output()->print_cr("-----------------------");
 460 
 461   for (int i = 0; i < result_oop->length(); ++i) {
 462     oop element_oop = result_oop->obj_at(i);
 463     oop str_oop = element_oop->obj_field(name_fd.offset());
 464     char *name = java_lang_String::as_utf8_string(str_oop);
 465     int count = element_oop->int_field(count_fd.offset());
 466     output()->print_cr("%10d  %s", count, name);
 467   }
 468 }
 469 
 470 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 471 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 472                            DCmdWithParser(output, heap),
 473   _filename("filename","Name of the dump file", "STRING",true),
 474   _all("-all", "Dump all objects, including unreachable objects",
 475        "BOOLEAN", false, "false") {
 476   _dcmdparser.add_dcmd_option(&_all);
 477   _dcmdparser.add_dcmd_argument(&_filename);
 478 }
 479 
 480 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 481   // Request a full GC before heap dump if _all is false
 482   // This helps reduces the amount of unreachable objects in the dump
 483   // and makes it easier to browse.
 484   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 485   int res = dumper.dump(_filename.value());
 486   if (res == 0) {
 487     output()->print_cr("Heap dump file created");
 488   } else {
 489     // heap dump failed
 490     ResourceMark rm;
 491     char* error = dumper.error_as_C_string();
 492     if (error == NULL) {
 493       output()->print_cr("Dump failed - reason unknown");
 494     } else {
 495       output()->print_cr("%s", error);
 496     }
 497   }
 498 }
 499 
 500 int HeapDumpDCmd::num_arguments() {
 501   ResourceMark rm;
 502   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 503   if (dcmd != NULL) {
 504     DCmdMark mark(dcmd);
 505     return dcmd->_dcmdparser.num_arguments();
 506   } else {
 507     return 0;
 508   }
 509 }
 510 
 511 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 512                                        DCmdWithParser(output, heap),
 513   _all("-all", "Inspect all objects, including unreachable objects",
 514        "BOOLEAN", false, "false") {
 515   _dcmdparser.add_dcmd_option(&_all);
 516 }
 517 
 518 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 519   VM_GC_HeapInspection heapop(output(),
 520                               !_all.value() /* request full gc if false */);
 521   VMThread::execute(&heapop);
 522 }
 523 
 524 int ClassHistogramDCmd::num_arguments() {
 525   ResourceMark rm;
 526   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 527   if (dcmd != NULL) {
 528     DCmdMark mark(dcmd);
 529     return dcmd->_dcmdparser.num_arguments();
 530   } else {
 531     return 0;
 532   }
 533 }
 534 
 535 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 536 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 537                                        DCmdWithParser(output, heap),
 538   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 539        "BOOLEAN", false, "false"),
 540   _all("-all", "Show all columns",
 541        "BOOLEAN", false, "false"),
 542   _help("-help", "Show meaning of all the columns",
 543        "BOOLEAN", false, "false"),
 544   _columns("columns", "Comma-separated list of all the columns to show. "
 545            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 546            "STRING", false) {
 547   _dcmdparser.add_dcmd_option(&_all);
 548   _dcmdparser.add_dcmd_option(&_csv);
 549   _dcmdparser.add_dcmd_option(&_help);
 550   _dcmdparser.add_dcmd_argument(&_columns);
 551 }
 552 
 553 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 554   if (!UnlockDiagnosticVMOptions) {
 555     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 556     return;
 557   }
 558 
 559   VM_GC_HeapInspection heapop(output(),
 560                               true /* request_full_gc */);
 561   heapop.set_csv_format(_csv.value());
 562   heapop.set_print_help(_help.value());
 563   heapop.set_print_class_stats(true);
 564   if (_all.value()) {
 565     if (_columns.has_value()) {
 566       output()->print_cr("Cannot specify -all and individual columns at the same time");
 567       return;
 568     } else {
 569       heapop.set_columns(NULL);
 570     }
 571   } else {
 572     if (_columns.has_value()) {
 573       heapop.set_columns(_columns.value());
 574     } else {
 575       heapop.set_columns(DEFAULT_COLUMNS);
 576     }
 577   }
 578   VMThread::execute(&heapop);
 579 }
 580 
 581 int ClassStatsDCmd::num_arguments() {
 582   ResourceMark rm;
 583   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 584   if (dcmd != NULL) {
 585     DCmdMark mark(dcmd);
 586     return dcmd->_dcmdparser.num_arguments();
 587   } else {
 588     return 0;
 589   }
 590 }
 591 #endif // INCLUDE_SERVICES
 592 
 593 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 594                                DCmdWithParser(output, heap),
 595   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 596   _dcmdparser.add_dcmd_option(&_locks);
 597 }
 598 
 599 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 600   // thread stacks
 601   VM_PrintThreads op1(output(), _locks.value());
 602   VMThread::execute(&op1);
 603 
 604   // JNI global handles
 605   VM_PrintJNI op2(output());
 606   VMThread::execute(&op2);
 607 
 608   // Deadlock detection
 609   VM_FindDeadlocks op3(output());
 610   VMThread::execute(&op3);
 611 }
 612 
 613 int ThreadDumpDCmd::num_arguments() {
 614   ResourceMark rm;
 615   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 616   if (dcmd != NULL) {
 617     DCmdMark mark(dcmd);
 618     return dcmd->_dcmdparser.num_arguments();
 619   } else {
 620     return 0;
 621   }
 622 }
 623 
 624 // Enhanced JMX Agent support
 625 
 626 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 627 
 628   DCmdWithParser(output, heap_allocated),
 629 
 630   _config_file
 631   ("config.file",
 632    "set com.sun.management.config.file", "STRING", false),
 633 
 634   _jmxremote_host
 635   ("jmxremote.host",
 636    "set com.sun.management.jmxremote.host", "STRING", false),
 637 
 638   _jmxremote_port
 639   ("jmxremote.port",
 640    "set com.sun.management.jmxremote.port", "STRING", false),
 641 
 642   _jmxremote_rmi_port
 643   ("jmxremote.rmi.port",
 644    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 645 
 646   _jmxremote_ssl
 647   ("jmxremote.ssl",
 648    "set com.sun.management.jmxremote.ssl", "STRING", false),
 649 
 650   _jmxremote_registry_ssl
 651   ("jmxremote.registry.ssl",
 652    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 653 
 654   _jmxremote_authenticate
 655   ("jmxremote.authenticate",
 656    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 657 
 658   _jmxremote_password_file
 659   ("jmxremote.password.file",
 660    "set com.sun.management.jmxremote.password.file", "STRING", false),
 661 
 662   _jmxremote_access_file
 663   ("jmxremote.access.file",
 664    "set com.sun.management.jmxremote.access.file", "STRING", false),
 665 
 666   _jmxremote_login_config
 667   ("jmxremote.login.config",
 668    "set com.sun.management.jmxremote.login.config", "STRING", false),
 669 
 670   _jmxremote_ssl_enabled_cipher_suites
 671   ("jmxremote.ssl.enabled.cipher.suites",
 672    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 673 
 674   _jmxremote_ssl_enabled_protocols
 675   ("jmxremote.ssl.enabled.protocols",
 676    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 677 
 678   _jmxremote_ssl_need_client_auth
 679   ("jmxremote.ssl.need.client.auth",
 680    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 681 
 682   _jmxremote_ssl_config_file
 683   ("jmxremote.ssl.config.file",
 684    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 685 
 686 // JDP Protocol support
 687   _jmxremote_autodiscovery
 688   ("jmxremote.autodiscovery",
 689    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 690 
 691    _jdp_port
 692   ("jdp.port",
 693    "set com.sun.management.jdp.port", "INT", false),
 694 
 695    _jdp_address
 696   ("jdp.address",
 697    "set com.sun.management.jdp.address", "STRING", false),
 698 
 699    _jdp_source_addr
 700   ("jdp.source_addr",
 701    "set com.sun.management.jdp.source_addr", "STRING", false),
 702 
 703    _jdp_ttl
 704   ("jdp.ttl",
 705    "set com.sun.management.jdp.ttl", "INT", false),
 706 
 707    _jdp_pause
 708   ("jdp.pause",
 709    "set com.sun.management.jdp.pause", "INT", false),
 710 
 711    _jdp_name
 712   ("jdp.name",
 713    "set com.sun.management.jdp.name", "STRING", false)
 714 
 715   {
 716     _dcmdparser.add_dcmd_option(&_config_file);
 717     _dcmdparser.add_dcmd_option(&_jmxremote_host);
 718     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 719     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 720     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 721     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 722     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 723     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 724     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 725     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 726     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 727     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 728     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 729     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 730     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 731     _dcmdparser.add_dcmd_option(&_jdp_port);
 732     _dcmdparser.add_dcmd_option(&_jdp_address);
 733     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 734     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 735     _dcmdparser.add_dcmd_option(&_jdp_pause);
 736     _dcmdparser.add_dcmd_option(&_jdp_name);
 737 }
 738 
 739 
 740 int JMXStartRemoteDCmd::num_arguments() {
 741   ResourceMark rm;
 742   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 743   if (dcmd != NULL) {
 744     DCmdMark mark(dcmd);
 745     return dcmd->_dcmdparser.num_arguments();
 746   } else {
 747     return 0;
 748   }
 749 }
 750 
 751 
 752 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 753     ResourceMark rm(THREAD);
 754     HandleMark hm(THREAD);
 755 
 756     // Load and initialize the sun.management.Agent class
 757     // invoke startRemoteManagementAgent(string) method to start
 758     // the remote management server.
 759     // throw java.lang.NoSuchMethodError if the method doesn't exist
 760 
 761     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 762     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 763     instanceKlassHandle ik (THREAD, k);
 764 
 765     JavaValue result(T_VOID);
 766 
 767     // Pass all command line arguments to java as key=value,...
 768     // All checks are done on java side
 769 
 770     int len = 0;
 771     stringStream options;
 772     char comma[2] = {0,0};
 773 
 774     // Leave default values on Agent.class side and pass only
 775     // agruments explicitly set by user. All arguments passed
 776     // to jcmd override properties with the same name set by
 777     // command line with -D or by managmenent.properties
 778     // file.
 779 #define PUT_OPTION(a) \
 780     do { \
 781         if ( (a).is_set() ){ \
 782             if ( *((a).type()) == 'I' ) { \
 783                 options.print("%scom.sun.management.%s=" JLONG_FORMAT, comma, (a).name(), (jlong)((a).value())); \
 784             } else { \
 785                 options.print("%scom.sun.management.%s=%s", comma, (a).name(), (char*)((a).value())); \
 786             } \
 787             comma[0] = ','; \
 788         }\
 789     } while(0);
 790 
 791 
 792     PUT_OPTION(_config_file);
 793     PUT_OPTION(_jmxremote_host);
 794     PUT_OPTION(_jmxremote_port);
 795     PUT_OPTION(_jmxremote_rmi_port);
 796     PUT_OPTION(_jmxremote_ssl);
 797     PUT_OPTION(_jmxremote_registry_ssl);
 798     PUT_OPTION(_jmxremote_authenticate);
 799     PUT_OPTION(_jmxremote_password_file);
 800     PUT_OPTION(_jmxremote_access_file);
 801     PUT_OPTION(_jmxremote_login_config);
 802     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 803     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 804     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 805     PUT_OPTION(_jmxremote_ssl_config_file);
 806     PUT_OPTION(_jmxremote_autodiscovery);
 807     PUT_OPTION(_jdp_port);
 808     PUT_OPTION(_jdp_address);
 809     PUT_OPTION(_jdp_source_addr);
 810     PUT_OPTION(_jdp_ttl);
 811     PUT_OPTION(_jdp_pause);
 812     PUT_OPTION(_jdp_name);
 813 
 814 #undef PUT_OPTION
 815 
 816     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 817     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 818 }
 819 
 820 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 821   DCmd(output, heap_allocated) {
 822   // do nothing
 823 }
 824 
 825 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 826     ResourceMark rm(THREAD);
 827     HandleMark hm(THREAD);
 828 
 829     // Load and initialize the sun.management.Agent class
 830     // invoke startLocalManagementAgent(void) method to start
 831     // the local management server
 832     // throw java.lang.NoSuchMethodError if method doesn't exist
 833 
 834     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 835     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 836     instanceKlassHandle ik (THREAD, k);
 837 
 838     JavaValue result(T_VOID);
 839     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 840 }
 841 
 842 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 843     ResourceMark rm(THREAD);
 844     HandleMark hm(THREAD);
 845 
 846     // Load and initialize the sun.management.Agent class
 847     // invoke stopRemoteManagementAgent method to stop the
 848     // management server
 849     // throw java.lang.NoSuchMethodError if method doesn't exist
 850 
 851     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 852     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 853     instanceKlassHandle ik (THREAD, k);
 854 
 855     JavaValue result(T_VOID);
 856     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 857 }
 858 
 859 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 860   DCmd(output, heap_allocated) {
 861   // do nothing
 862 }
 863 
 864 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 865   ResourceMark rm(THREAD);
 866   HandleMark hm(THREAD);
 867 
 868   // Load and initialize the sun.management.Agent class
 869   // invoke getManagementAgentStatus() method to generate the status info
 870   // throw java.lang.NoSuchMethodError if method doesn't exist
 871 
 872   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 873   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 874   instanceKlassHandle ik (THREAD, k);
 875 
 876   JavaValue result(T_OBJECT);
 877   JavaCalls::call_static(&result, ik, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 878 
 879   jvalue* jv = (jvalue*) result.get_value_addr();
 880   oop str = (oop) jv->l;
 881   if (str != NULL) {
 882       char* out = java_lang_String::as_utf8_string(str);
 883       if (out) {
 884           output()->print_cr("%s", out);
 885           return;
 886       }
 887   }
 888   output()->print_cr("Error obtaining management agent status");
 889 }
 890 
 891 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 892   DCmd(output, heap_allocated) {
 893   // do nothing
 894 }
 895 
 896 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 897   os::print_dll_info(output());
 898   output()->cr();
 899 }
 900 
 901 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 902   VM_PrintCompileQueue printCompileQueueOp(output());
 903   VMThread::execute(&printCompileQueueOp);
 904 }
 905 
 906 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 907   CodeCache::print_codelist(output());
 908 }
 909 
 910 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 911   CodeCache::print_layout(output());
 912 }
 913 
 914 void CompilerDirectivesPrintDCmd::execute(DCmdSource source, TRAPS) {
 915   DirectivesStack::print(output());
 916 }
 917 
 918 CompilerDirectivesAddDCmd::CompilerDirectivesAddDCmd(outputStream* output, bool heap) :
 919                            DCmdWithParser(output, heap),
 920   _filename("filename","Name of the directives file", "STRING",true) {
 921   _dcmdparser.add_dcmd_argument(&_filename);
 922 }
 923 
 924 void CompilerDirectivesAddDCmd::execute(DCmdSource source, TRAPS) {
 925   DirectivesParser::parse_from_file(_filename.value(), output());
 926 }
 927 
 928 int CompilerDirectivesAddDCmd::num_arguments() {
 929   ResourceMark rm;
 930   CompilerDirectivesAddDCmd* dcmd = new CompilerDirectivesAddDCmd(NULL, false);
 931   if (dcmd != NULL) {
 932     DCmdMark mark(dcmd);
 933     return dcmd->_dcmdparser.num_arguments();
 934   } else {
 935     return 0;
 936   }
 937 }
 938 
 939 void CompilerDirectivesRemoveDCmd::execute(DCmdSource source, TRAPS) {
 940   DirectivesStack::pop(1);
 941 }
 942 
 943 void CompilerDirectivesClearDCmd::execute(DCmdSource source, TRAPS) {
 944   DirectivesStack::clear();
 945 }
 946 #if INCLUDE_SERVICES
 947 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 948                                        DCmdWithParser(output, heap),
 949   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 950   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 951                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 952   _classname("classname", "Name of class whose hierarchy should be printed. "
 953              "If not specified, all class hierarchies are printed.",
 954              "STRING", false) {
 955   _dcmdparser.add_dcmd_option(&_print_interfaces);
 956   _dcmdparser.add_dcmd_option(&_print_subclasses);
 957   _dcmdparser.add_dcmd_argument(&_classname);
 958 }
 959 
 960 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 961   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 962                                                _print_subclasses.value(), _classname.value());
 963   VMThread::execute(&printClassHierarchyOp);
 964 }
 965 
 966 int ClassHierarchyDCmd::num_arguments() {
 967   ResourceMark rm;
 968   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 969   if (dcmd != NULL) {
 970     DCmdMark mark(dcmd);
 971     return dcmd->_dcmdparser.num_arguments();
 972   } else {
 973     return 0;
 974   }
 975 }
 976 
 977 #endif
 978 
 979 class VM_DumpTouchedMethods : public VM_Operation {
 980 private:
 981   outputStream* _out;
 982 public:
 983   VM_DumpTouchedMethods(outputStream* out) {
 984     _out = out;
 985   }
 986 
 987   virtual VMOp_Type type() const { return VMOp_DumpTouchedMethods; }
 988 
 989   virtual void doit() {
 990     Method::print_touched_methods(_out);
 991   }
 992 };
 993 
 994 TouchedMethodsDCmd::TouchedMethodsDCmd(outputStream* output, bool heap) :
 995                                        DCmdWithParser(output, heap)
 996 {}
 997 
 998 void TouchedMethodsDCmd::execute(DCmdSource source, TRAPS) {
 999   if (!UnlockDiagnosticVMOptions) {
1000     output()->print_cr("VM.touched_methods command requires -XX:+UnlockDiagnosticVMOptions");
1001     return;
1002   }
1003   VM_DumpTouchedMethods dumper(output());
1004   VMThread::execute(&dumper);
1005 }
1006 
1007 int TouchedMethodsDCmd::num_arguments() {
1008   return 0;
1009 }