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