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