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