1 /*
   2  * Copyright (c) 2011, 2024, 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 "cds/cds_globals.hpp"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/classLoaderHierarchyDCmd.hpp"
  29 #include "classfile/classLoaderStats.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/systemDictionary.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "code/codeCache.hpp"
  34 #include "compiler/compilationMemoryStatistic.hpp"
  35 #include "compiler/compiler_globals.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/directivesParser.hpp"
  38 #include "gc/shared/gcVMOperations.hpp"
  39 #include "jvm.h"
  40 #include "memory/metaspace/metaspaceDCmd.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "nmt/memMapPrinter.hpp"
  44 #include "nmt/memTracker.hpp"
  45 #include "nmt/nmtDCmd.hpp"
  46 #include "oops/instanceKlass.hpp"
  47 #include "oops/objArrayOop.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "oops/typeArrayOop.inline.hpp"
  50 #include "prims/jvmtiAgentList.hpp"
  51 #include "runtime/fieldDescriptor.inline.hpp"
  52 #include "runtime/flags/jvmFlag.hpp"
  53 #include "runtime/handles.inline.hpp"
  54 #include "runtime/interfaceSupport.inline.hpp"
  55 #include "runtime/javaCalls.hpp"
  56 #include "runtime/jniHandles.hpp"
  57 #include "runtime/os.hpp"
  58 #include "runtime/vmOperations.hpp"
  59 #include "runtime/vm_version.hpp"
  60 #include "services/diagnosticArgument.hpp"
  61 #include "services/diagnosticCommand.hpp"
  62 #include "services/diagnosticFramework.hpp"
  63 #include "services/heapDumper.hpp"
  64 #include "services/management.hpp"
  65 #include "services/writeableFlags.hpp"
  66 #include "utilities/debug.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/formatBuffer.hpp"
  69 #include "utilities/macros.hpp"
  70 #include "utilities/parseInteger.hpp"
  71 #ifdef LINUX
  72 #include "os_posix.hpp"
  73 #include "mallocInfoDcmd.hpp"
  74 #include "trimCHeapDCmd.hpp"
  75 #include <errno.h>
  76 #endif
  77 
  78 static void loadAgentModule(TRAPS) {
  79   ResourceMark rm(THREAD);
  80   HandleMark hm(THREAD);
  81 
  82   JavaValue result(T_OBJECT);
  83   Handle h_module_name = java_lang_String::create_from_str("jdk.management.agent", CHECK);
  84   JavaCalls::call_static(&result,
  85                          vmClasses::module_Modules_klass(),
  86                          vmSymbols::loadModule_name(),
  87                          vmSymbols::loadModule_signature(),
  88                          h_module_name,
  89                          THREAD);
  90 }
  91 
  92 void DCmd::register_dcmds(){
  93   // Registration of the diagnostic commands
  94   // First argument specifies which interfaces will export the command
  95   // Second argument specifies if the command is enabled
  96   // Third  argument specifies if the command is hidden
  97   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  98                          | DCmd_Source_MBean;
  99   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
 100   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
 101   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
 102   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
 103   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
 104   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
 105   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
 106   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
 107   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMInfoDCmd>(full_export, true, false));
 108   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
 109   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
 110   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapInfoDCmd>(full_export, true, false));
 111   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<FinalizerInfoDCmd>(full_export, true, false));
 112 #if INCLUDE_SERVICES
 113   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
 114   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
 115   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemDictionaryDCmd>(full_export, true, false));
 116   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
 117   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassesDCmd>(full_export, true, false));
 118   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
 119   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
 120   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<metaspace::MetaspaceDCmd>(full_export, true, false));
 121   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintClassLayoutDCmd>(full_export, true, false));
 122   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<EventLogDCmd>(full_export, true, false));
 123 #if INCLUDE_JVMTI // Both JVMTI and SERVICES have to be enabled to have this dcmd
 124   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIAgentLoadDCmd>(full_export, true, false));
 125 #endif // INCLUDE_JVMTI
 126 #endif // INCLUDE_SERVICES
 127 #if INCLUDE_JVMTI
 128   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
 129 #endif // INCLUDE_JVMTI
 130   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
 131   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpToFileDCmd>(full_export, true, false));
 132   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
 133   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderHierarchyDCmd>(full_export, true, false));
 134   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
 135   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
 136   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
 137 #ifdef LINUX
 138   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PerfMapDCmd>(full_export, true, false));
 139   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<TrimCLibcHeapDCmd>(full_export, true, false));
 140   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<MallocInfoDcmd>(full_export, true, false));
 141   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemMapDCmd>(full_export, true,false));
 142   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemDumpMapDCmd>(full_export, true,false));
 143 #endif // LINUX
 144   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeHeapAnalyticsDCmd>(full_export, true, false));
 145 
 146   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesPrintDCmd>(full_export, true, false));
 147   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesAddDCmd>(full_export, true, false));
 148   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesRemoveDCmd>(full_export, true, false));
 149   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilerDirectivesClearDCmd>(full_export, true, false));
 150   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompilationMemoryStatisticDCmd>(full_export, true, false));
 151 
 152   // Enhanced JMX Agent Support
 153   // These commands won't be exported via the DiagnosticCommandMBean until an
 154   // appropriate permission is created for them
 155   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
 156   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
 157   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
 158   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
 159   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStatusDCmd>(jmx_agent_export_flags, true,false));
 160   // Debug on cmd (only makes sense with JVMTI since the agentlib needs it).
 161 #if INCLUDE_JVMTI
 162   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<DebugOnCmdStartDCmd>(full_export, true, true));
 163 #endif // INCLUDE_JVMTI
 164 
 165 #if INCLUDE_CDS
 166   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<DumpSharedArchiveDCmd>(full_export, true, false));
 167 #endif // INCLUDE_CDS
 168 
 169   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<NMTDCmd>(full_export, true, false));
 170 }
 171 
 172 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
 173   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
 174   _cmd("command name", "The name of the command for which we want help",
 175         "STRING", false) {
 176   _dcmdparser.add_dcmd_option(&_all);
 177   _dcmdparser.add_dcmd_argument(&_cmd);
 178 };
 179 
 180 
 181 static int compare_strings(const char** s1, const char** s2) {
 182   return ::strcmp(*s1, *s2);
 183 }
 184 
 185 void HelpDCmd::execute(DCmdSource source, TRAPS) {
 186   if (_all.value()) {
 187     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
 188     cmd_list->sort(compare_strings);
 189     for (int i = 0; i < cmd_list->length(); i++) {
 190       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 191                                                   strlen(cmd_list->at(i)));
 192       output()->print_cr("%s%s", factory->name(),
 193                          factory->is_enabled() ? "" : " [disabled]");
 194       output()->print_cr("\t%s", factory->description());
 195       output()->cr();
 196       factory = factory->next();
 197     }
 198   } else if (_cmd.has_value()) {
 199     DCmd* cmd = nullptr;
 200     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
 201                                                 strlen(_cmd.value()));
 202     if (factory != nullptr) {
 203       output()->print_cr("%s%s", factory->name(),
 204                          factory->is_enabled() ? "" : " [disabled]");
 205       output()->print_cr("%s", factory->description());
 206       output()->print_cr("\nImpact: %s", factory->impact());
 207       output()->cr();
 208       cmd = factory->create_resource_instance(output());
 209       if (cmd != nullptr) {
 210         DCmdMark mark(cmd);
 211         cmd->print_help(factory->name());
 212       }
 213     } else {
 214       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 215     }
 216   } else {
 217     output()->print_cr("The following commands are available:");
 218     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 219     cmd_list->sort(compare_strings);
 220     for (int i = 0; i < cmd_list->length(); i++) {
 221       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 222                                                   strlen(cmd_list->at(i)));
 223       output()->print_cr("%s%s", factory->name(),
 224                          factory->is_enabled() ? "" : " [disabled]");
 225       factory = factory->_next;
 226     }
 227     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 228   }
 229 }
 230 
 231 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 232   output()->print_cr("%s version %s", VM_Version::vm_name(),
 233           VM_Version::vm_release());
 234   JDK_Version jdk_version = JDK_Version::current();
 235   if (jdk_version.patch_version() > 0) {
 236     output()->print_cr("JDK %d.%d.%d.%d", jdk_version.major_version(),
 237             jdk_version.minor_version(), jdk_version.security_version(),
 238             jdk_version.patch_version());
 239   } else {
 240     output()->print_cr("JDK %d.%d.%d", jdk_version.major_version(),
 241             jdk_version.minor_version(), jdk_version.security_version());
 242   }
 243 }
 244 
 245 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 246                                    DCmdWithParser(output, heap),
 247   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 248   _dcmdparser.add_dcmd_option(&_all);
 249 }
 250 
 251 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 252   if (_all.value()) {
 253     JVMFlag::printFlags(output(), true);
 254   } else {
 255     JVMFlag::printSetFlags(output());
 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 = nullptr;
 270   if (_value.value() != nullptr) {
 271     val = _value.value();
 272   }
 273 
 274   FormatBuffer<80> err_msg("%s", "");
 275   int ret = WriteableFlags::set_flag(_flag.value(), val, JVMFlagOrigin::MANAGEMENT, err_msg);
 276 
 277   if (ret != JVMFlag::SUCCESS) {
 278     output()->print_cr("%s", err_msg.buffer());
 279   }
 280 }
 281 
 282 void JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
 283   if (JvmtiExport::should_post_data_dump()) {
 284     JvmtiExport::post_data_dump();
 285   }
 286 }
 287 
 288 #if INCLUDE_SERVICES
 289 #if INCLUDE_JVMTI
 290 JVMTIAgentLoadDCmd::JVMTIAgentLoadDCmd(outputStream* output, bool heap) :
 291                                        DCmdWithParser(output, heap),
 292   _libpath("library path", "Absolute path of the JVMTI agent to load.",
 293            "STRING", true),
 294   _option("agent option", "Option string to pass the agent.", "STRING", false) {
 295   _dcmdparser.add_dcmd_argument(&_libpath);
 296   _dcmdparser.add_dcmd_argument(&_option);
 297 }
 298 
 299 void JVMTIAgentLoadDCmd::execute(DCmdSource source, TRAPS) {
 300 
 301   if (_libpath.value() == nullptr) {
 302     output()->print_cr("JVMTI.agent_load dcmd needs library path.");
 303     return;
 304   }
 305 
 306   char *suffix = strrchr(_libpath.value(), '.');
 307   bool is_java_agent = (suffix != nullptr) && (strncmp(".jar", suffix, 4) == 0);
 308 
 309   if (is_java_agent) {
 310     if (_option.value() == nullptr) {
 311       JvmtiAgentList::load_agent("instrument", "false", _libpath.value(), output());
 312     } else {
 313       size_t opt_len = strlen(_libpath.value()) + strlen(_option.value()) + 2;
 314       if (opt_len > 4096) {
 315         output()->print_cr("JVMTI agent attach failed: Options is too long.");
 316         return;
 317       }
 318 
 319       char *opt = (char *)os::malloc(opt_len, mtInternal);
 320       if (opt == nullptr) {
 321         output()->print_cr("JVMTI agent attach failed: "
 322                            "Could not allocate " SIZE_FORMAT " bytes for argument.",
 323                            opt_len);
 324         return;
 325       }
 326 
 327       jio_snprintf(opt, opt_len, "%s=%s", _libpath.value(), _option.value());
 328       JvmtiAgentList::load_agent("instrument", "false", opt, output());
 329 
 330       os::free(opt);
 331     }
 332   } else {
 333     JvmtiAgentList::load_agent(_libpath.value(), "true", _option.value(), output());
 334   }
 335 }
 336 
 337 #endif // INCLUDE_JVMTI
 338 #endif // INCLUDE_SERVICES
 339 
 340 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 341   // load VMSupport
 342   Symbol* klass = vmSymbols::jdk_internal_vm_VMSupport();
 343   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 344   InstanceKlass* ik = InstanceKlass::cast(k);
 345   if (ik->should_be_initialized()) {
 346     ik->initialize(THREAD);
 347   }
 348   if (HAS_PENDING_EXCEPTION) {
 349     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 350     output()->cr();
 351     CLEAR_PENDING_EXCEPTION;
 352     return;
 353   }
 354 
 355   // invoke the serializePropertiesToByteArray method
 356   JavaValue result(T_OBJECT);
 357   JavaCallArguments args;
 358 
 359   Symbol* signature = vmSymbols::void_byte_array_signature();
 360   JavaCalls::call_static(&result,
 361                          ik,
 362                          vmSymbols::serializePropertiesToByteArray_name(),
 363                          signature,
 364                          &args,
 365                          THREAD);
 366   if (HAS_PENDING_EXCEPTION) {
 367     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 368     output()->cr();
 369     CLEAR_PENDING_EXCEPTION;
 370     return;
 371   }
 372 
 373   // The result should be a [B
 374   oop res = result.get_oop();
 375   assert(res->is_typeArray(), "just checking");
 376   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 377 
 378   // copy the bytes to the output stream
 379   typeArrayOop ba = typeArrayOop(res);
 380   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 381   output()->print_raw((const char*)addr, ba->length());
 382 }
 383 
 384 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 385                            DCmdWithParser(output, heap),
 386   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 387   _dcmdparser.add_dcmd_option(&_date);
 388 }
 389 
 390 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 391   if (_date.value()) {
 392     output()->date_stamp(true, "", ": ");
 393   }
 394   output()->time_stamp().update_to(tty->time_stamp().ticks());
 395   output()->stamp();
 396   output()->print_cr(" s");
 397 }
 398 
 399 void VMInfoDCmd::execute(DCmdSource source, TRAPS) {
 400   VMError::print_vm_info(_output);
 401 }
 402 
 403 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 404   Universe::heap()->collect(GCCause::_dcmd_gc_run);
 405 }
 406 
 407 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 408   Klass* k = vmClasses::System_klass();
 409   JavaValue result(T_VOID);
 410   JavaCalls::call_static(&result, k,
 411                          vmSymbols::run_finalization_name(),
 412                          vmSymbols::void_method_signature(), CHECK);
 413 }
 414 
 415 void HeapInfoDCmd::execute(DCmdSource source, TRAPS) {
 416   MutexLocker hl(THREAD, Heap_lock);
 417   Universe::heap()->print_on(output());
 418 }
 419 
 420 void FinalizerInfoDCmd::execute(DCmdSource source, TRAPS) {
 421   ResourceMark rm(THREAD);
 422 
 423   if (!InstanceKlass::is_finalization_enabled()) {
 424     output()->print_cr("Finalization is disabled");
 425     return;
 426   }
 427 
 428   Klass* k = SystemDictionary::resolve_or_fail(
 429     vmSymbols::finalizer_histogram_klass(), true, CHECK);
 430 
 431   JavaValue result(T_ARRAY);
 432 
 433   // We are calling lang.ref.FinalizerHistogram.getFinalizerHistogram() method
 434   // and expect it to return array of FinalizerHistogramEntry as Object[]
 435 
 436   JavaCalls::call_static(&result, k,
 437                          vmSymbols::get_finalizer_histogram_name(),
 438                          vmSymbols::void_finalizer_histogram_entry_array_signature(), CHECK);
 439 
 440   objArrayOop result_oop = (objArrayOop) result.get_oop();
 441   if (result_oop->length() == 0) {
 442     output()->print_cr("No instances waiting for finalization found");
 443     return;
 444   }
 445 
 446   oop foop = result_oop->obj_at(0);
 447   InstanceKlass* ik = InstanceKlass::cast(foop->klass());
 448 
 449   fieldDescriptor count_fd, name_fd;
 450 
 451   Klass* count_res = ik->find_field(
 452     vmSymbols::finalizer_histogram_entry_count_field(), vmSymbols::int_signature(), &count_fd);
 453 
 454   Klass* name_res = ik->find_field(
 455     vmSymbols::finalizer_histogram_entry_name_field(), vmSymbols::string_signature(), &name_fd);
 456 
 457   assert(count_res != nullptr && name_res != nullptr, "Unexpected layout of FinalizerHistogramEntry");
 458 
 459   output()->print_cr("Unreachable instances waiting for finalization");
 460   output()->print_cr("#instances  class name");
 461   output()->print_cr("-----------------------");
 462 
 463   for (int i = 0; i < result_oop->length(); ++i) {
 464     oop element_oop = result_oop->obj_at(i);
 465     oop str_oop = element_oop->obj_field(name_fd.offset());
 466     char *name = java_lang_String::as_utf8_string(str_oop);
 467     int count = element_oop->int_field(count_fd.offset());
 468     output()->print_cr("%10d  %s", count, name);
 469   }
 470 }
 471 
 472 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 473 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 474                            DCmdWithParser(output, heap),
 475   _filename("filename","Name of the dump file", "STRING",true),
 476   _all("-all", "Dump all objects, including unreachable objects",
 477        "BOOLEAN", false, "false"),
 478   _gzip("-gz", "If specified, the heap dump is written in gzipped format "
 479                "using the given compression level. 1 (recommended) is the fastest, "
 480                "9 the strongest compression.", "INT", false, "1"),
 481   _overwrite("-overwrite", "If specified, the dump file will be overwritten if it exists",
 482            "BOOLEAN", false, "false"),
 483   _parallel("-parallel", "Number of parallel threads to use for heap dump. The VM "
 484                           "will try to use the specified number of threads, but might use fewer.",
 485             "INT", false, "1") {
 486   _dcmdparser.add_dcmd_option(&_all);
 487   _dcmdparser.add_dcmd_argument(&_filename);
 488   _dcmdparser.add_dcmd_option(&_gzip);
 489   _dcmdparser.add_dcmd_option(&_overwrite);
 490   _dcmdparser.add_dcmd_option(&_parallel);
 491 }
 492 
 493 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 494   jlong level = -1; // -1 means no compression.
 495   jlong parallel = HeapDumper::default_num_of_dump_threads();
 496 
 497   if (_gzip.is_set()) {
 498     level = _gzip.value();
 499 
 500     if (level < 1 || level > 9) {
 501       output()->print_cr("Compression level out of range (1-9): " JLONG_FORMAT, level);
 502       return;
 503     }
 504   }
 505 
 506   if (_parallel.is_set()) {
 507     parallel = _parallel.value();
 508 
 509     if (parallel < 0) {
 510       output()->print_cr("Invalid number of parallel dump threads.");
 511       return;
 512     } else if (parallel == 0) {
 513       // 0 implies to disable parallel heap dump, in such case, we use serial dump instead
 514       parallel = 1;
 515     }
 516   }
 517 
 518   // Request a full GC before heap dump if _all is false
 519   // This helps reduces the amount of unreachable objects in the dump
 520   // and makes it easier to browse.
 521   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 522   dumper.dump(_filename.value(), output(), (int) level, _overwrite.value(), (uint)parallel);
 523 }
 524 
 525 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 526                                        DCmdWithParser(output, heap),
 527   _all("-all", "Inspect all objects, including unreachable objects",
 528        "BOOLEAN", false, "false"),
 529   _parallel_thread_num("-parallel",
 530        "Number of parallel threads to use for heap inspection. "
 531        "0 (the default) means let the VM determine the number of threads to use. "
 532        "1 means use one thread (disable parallelism). "
 533        "For any other value the VM will try to use the specified number of "
 534        "threads, but might use fewer.",
 535        "INT", false, "0") {
 536   _dcmdparser.add_dcmd_option(&_all);
 537   _dcmdparser.add_dcmd_option(&_parallel_thread_num);
 538 }
 539 
 540 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 541   jlong num = _parallel_thread_num.value();
 542   if (num < 0) {
 543     output()->print_cr("Parallel thread number out of range (>=0): " JLONG_FORMAT, num);
 544     return;
 545   }
 546   uint parallel_thread_num = num == 0
 547       ? MAX2<uint>(1, (uint)os::initial_active_processor_count() * 3 / 8)
 548       : num;
 549   VM_GC_HeapInspection heapop(output(),
 550                               !_all.value(), /* request full gc if false */
 551                               parallel_thread_num);
 552   VMThread::execute(&heapop);
 553 }
 554 
 555 #endif // INCLUDE_SERVICES
 556 
 557 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 558                                DCmdWithParser(output, heap),
 559   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false"),
 560   _extended("-e", "print extended thread information", "BOOLEAN", false, "false") {
 561   _dcmdparser.add_dcmd_option(&_locks);
 562   _dcmdparser.add_dcmd_option(&_extended);
 563 }
 564 
 565 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 566   // thread stacks and JNI global handles
 567   VM_PrintThreads op1(output(), _locks.value(), _extended.value(), true /* print JNI handle info */);
 568   VMThread::execute(&op1);
 569 
 570   // Deadlock detection
 571   VM_FindDeadlocks op2(output());
 572   VMThread::execute(&op2);
 573 }
 574 
 575 // Enhanced JMX Agent support
 576 
 577 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 578 
 579   DCmdWithParser(output, heap_allocated),
 580 
 581   _config_file
 582   ("config.file",
 583    "set com.sun.management.config.file", "STRING", false),
 584 
 585   _jmxremote_host
 586   ("jmxremote.host",
 587    "set com.sun.management.jmxremote.host", "STRING", false),
 588 
 589   _jmxremote_port
 590   ("jmxremote.port",
 591    "set com.sun.management.jmxremote.port", "STRING", false),
 592 
 593   _jmxremote_rmi_port
 594   ("jmxremote.rmi.port",
 595    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 596 
 597   _jmxremote_ssl
 598   ("jmxremote.ssl",
 599    "set com.sun.management.jmxremote.ssl", "STRING", false),
 600 
 601   _jmxremote_registry_ssl
 602   ("jmxremote.registry.ssl",
 603    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 604 
 605   _jmxremote_authenticate
 606   ("jmxremote.authenticate",
 607    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 608 
 609   _jmxremote_password_file
 610   ("jmxremote.password.file",
 611    "set com.sun.management.jmxremote.password.file", "STRING", false),
 612 
 613   _jmxremote_access_file
 614   ("jmxremote.access.file",
 615    "set com.sun.management.jmxremote.access.file", "STRING", false),
 616 
 617   _jmxremote_login_config
 618   ("jmxremote.login.config",
 619    "set com.sun.management.jmxremote.login.config", "STRING", false),
 620 
 621   _jmxremote_ssl_enabled_cipher_suites
 622   ("jmxremote.ssl.enabled.cipher.suites",
 623    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 624 
 625   _jmxremote_ssl_enabled_protocols
 626   ("jmxremote.ssl.enabled.protocols",
 627    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 628 
 629   _jmxremote_ssl_need_client_auth
 630   ("jmxremote.ssl.need.client.auth",
 631    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 632 
 633   _jmxremote_ssl_config_file
 634   ("jmxremote.ssl.config.file",
 635    "set com.sun.management.jmxremote.ssl.config.file", "STRING", false),
 636 
 637 // JDP Protocol support
 638   _jmxremote_autodiscovery
 639   ("jmxremote.autodiscovery",
 640    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 641 
 642    _jdp_port
 643   ("jdp.port",
 644    "set com.sun.management.jdp.port", "INT", false),
 645 
 646    _jdp_address
 647   ("jdp.address",
 648    "set com.sun.management.jdp.address", "STRING", false),
 649 
 650    _jdp_source_addr
 651   ("jdp.source_addr",
 652    "set com.sun.management.jdp.source_addr", "STRING", false),
 653 
 654    _jdp_ttl
 655   ("jdp.ttl",
 656    "set com.sun.management.jdp.ttl", "INT", false),
 657 
 658    _jdp_pause
 659   ("jdp.pause",
 660    "set com.sun.management.jdp.pause", "INT", false),
 661 
 662    _jdp_name
 663   ("jdp.name",
 664    "set com.sun.management.jdp.name", "STRING", false)
 665 
 666   {
 667     _dcmdparser.add_dcmd_option(&_config_file);
 668     _dcmdparser.add_dcmd_option(&_jmxremote_host);
 669     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 670     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 671     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 672     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 673     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 674     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 675     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 676     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 677     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 678     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 679     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 680     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 681     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 682     _dcmdparser.add_dcmd_option(&_jdp_port);
 683     _dcmdparser.add_dcmd_option(&_jdp_address);
 684     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 685     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 686     _dcmdparser.add_dcmd_option(&_jdp_pause);
 687     _dcmdparser.add_dcmd_option(&_jdp_name);
 688 }
 689 
 690 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 691     ResourceMark rm(THREAD);
 692     HandleMark hm(THREAD);
 693 
 694     // Load and initialize the jdk.internal.agent.Agent class
 695     // invoke startRemoteManagementAgent(string) method to start
 696     // the remote management server.
 697     // throw java.lang.NoSuchMethodError if the method doesn't exist
 698 
 699     loadAgentModule(CHECK);
 700     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 701     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
 702 
 703     JavaValue result(T_VOID);
 704 
 705     // Pass all command line arguments to java as key=value,...
 706     // All checks are done on java side
 707 
 708     int len = 0;
 709     stringStream options;
 710     char comma[2] = {0,0};
 711 
 712     // Leave default values on Agent.class side and pass only
 713     // arguments explicitly set by user. All arguments passed
 714     // to jcmd override properties with the same name set by
 715     // command line with -D or by managmenent.properties
 716     // file.
 717 #define PUT_OPTION(a) \
 718     do { \
 719         if ( (a).is_set() ){ \
 720             if ( *((a).type()) == 'I' ) { \
 721                 options.print("%scom.sun.management.%s=" JLONG_FORMAT, comma, (a).name(), (jlong)((a).value())); \
 722             } else { \
 723                 options.print("%scom.sun.management.%s=%s", comma, (a).name(), (char*)((a).value())); \
 724             } \
 725             comma[0] = ','; \
 726         }\
 727     } while(0);
 728 
 729 
 730     PUT_OPTION(_config_file);
 731     PUT_OPTION(_jmxremote_host);
 732     PUT_OPTION(_jmxremote_port);
 733     PUT_OPTION(_jmxremote_rmi_port);
 734     PUT_OPTION(_jmxremote_ssl);
 735     PUT_OPTION(_jmxremote_registry_ssl);
 736     PUT_OPTION(_jmxremote_authenticate);
 737     PUT_OPTION(_jmxremote_password_file);
 738     PUT_OPTION(_jmxremote_access_file);
 739     PUT_OPTION(_jmxremote_login_config);
 740     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 741     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 742     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 743     PUT_OPTION(_jmxremote_ssl_config_file);
 744     PUT_OPTION(_jmxremote_autodiscovery);
 745     PUT_OPTION(_jdp_port);
 746     PUT_OPTION(_jdp_address);
 747     PUT_OPTION(_jdp_source_addr);
 748     PUT_OPTION(_jdp_ttl);
 749     PUT_OPTION(_jdp_pause);
 750     PUT_OPTION(_jdp_name);
 751 
 752 #undef PUT_OPTION
 753 
 754     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 755     JavaCalls::call_static(&result, k, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 756 }
 757 
 758 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 759   DCmd(output, heap_allocated) {
 760   // do nothing
 761 }
 762 
 763 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 764     ResourceMark rm(THREAD);
 765     HandleMark hm(THREAD);
 766 
 767     // Load and initialize the jdk.internal.agent.Agent class
 768     // invoke startLocalManagementAgent(void) method to start
 769     // the local management server
 770     // throw java.lang.NoSuchMethodError if method doesn't exist
 771 
 772     loadAgentModule(CHECK);
 773     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 774     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
 775 
 776     JavaValue result(T_VOID);
 777     JavaCalls::call_static(&result, k, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 778 }
 779 
 780 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 781     ResourceMark rm(THREAD);
 782     HandleMark hm(THREAD);
 783 
 784     // Load and initialize the jdk.internal.agent.Agent class
 785     // invoke stopRemoteManagementAgent method to stop the
 786     // management server
 787     // throw java.lang.NoSuchMethodError if method doesn't exist
 788 
 789     loadAgentModule(CHECK);
 790     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 791     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
 792 
 793     JavaValue result(T_VOID);
 794     JavaCalls::call_static(&result, k, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 795 }
 796 
 797 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 798   DCmd(output, heap_allocated) {
 799   // do nothing
 800 }
 801 
 802 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 803   ResourceMark rm(THREAD);
 804   HandleMark hm(THREAD);
 805 
 806   // Load and initialize the jdk.internal.agent.Agent class
 807   // invoke getManagementAgentStatus() method to generate the status info
 808   // throw java.lang.NoSuchMethodError if method doesn't exist
 809 
 810   loadAgentModule(CHECK);
 811   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 812   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
 813 
 814   JavaValue result(T_OBJECT);
 815   JavaCalls::call_static(&result, k, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 816 
 817   jvalue* jv = (jvalue*) result.get_value_addr();
 818   oop str = cast_to_oop(jv->l);
 819   if (str != nullptr) {
 820       char* out = java_lang_String::as_utf8_string(str);
 821       if (out) {
 822           // Avoid using print_cr() because length maybe longer than O_BUFLEN
 823           output()->print_raw_cr(out);
 824           return;
 825       }
 826   }
 827   output()->print_cr("Error obtaining management agent status");
 828 }
 829 
 830 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 831   DCmd(output, heap_allocated) {
 832   // do nothing
 833 }
 834 
 835 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 836   os::print_dll_info(output());
 837   output()->cr();
 838 }
 839 
 840 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 841   VM_PrintCompileQueue printCompileQueueOp(output());
 842   VMThread::execute(&printCompileQueueOp);
 843 }
 844 
 845 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 846   CodeCache::print_codelist(output());
 847 }
 848 
 849 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 850   CodeCache::print_layout(output());
 851 }
 852 
 853 #ifdef LINUX
 854 #define DEFAULT_PERFMAP_FILENAME "/tmp/perf-<pid>.map"
 855 
 856 PerfMapDCmd::PerfMapDCmd(outputStream* output, bool heap) :
 857              DCmdWithParser(output, heap),
 858   _filename("filename", "Name of the map file", "STRING", false, DEFAULT_PERFMAP_FILENAME)
 859 {
 860   _dcmdparser.add_dcmd_argument(&_filename);
 861 }
 862 
 863 void PerfMapDCmd::execute(DCmdSource source, TRAPS) {
 864   // The check for _filename.is_set() is because we don't want to use
 865   // DEFAULT_PERFMAP_FILENAME, since it is meant as a description
 866   // of the default, not the actual default.
 867   CodeCache::write_perf_map(_filename.is_set() ? _filename.value() : nullptr);
 868 }
 869 #endif // LINUX
 870 
 871 //---<  BEGIN  >--- CodeHeap State Analytics.
 872 CodeHeapAnalyticsDCmd::CodeHeapAnalyticsDCmd(outputStream* output, bool heap) :
 873                                              DCmdWithParser(output, heap),
 874   _function("function", "Function to be performed (aggregate, UsedSpace, FreeSpace, MethodCount, MethodSpace, MethodAge, MethodNames, discard", "STRING", false, "all"),
 875   _granularity("granularity", "Detail level - smaller value -> more detail", "INT", false, "4096") {
 876   _dcmdparser.add_dcmd_argument(&_function);
 877   _dcmdparser.add_dcmd_argument(&_granularity);
 878 }
 879 
 880 void CodeHeapAnalyticsDCmd::execute(DCmdSource source, TRAPS) {
 881   jlong granularity = _granularity.value();
 882   if (granularity < 1) {
 883     Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),
 884                        "Invalid granularity value " JLONG_FORMAT  ". Should be positive.\n", granularity);
 885     return;
 886   }
 887 
 888   CompileBroker::print_heapinfo(output(), _function.value(), granularity);
 889 }
 890 //---<  END  >--- CodeHeap State Analytics.
 891 
 892 EventLogDCmd::EventLogDCmd(outputStream* output, bool heap) :
 893   DCmdWithParser(output, heap),
 894   _log("log", "Name of log to be printed. If omitted, all logs are printed.", "STRING", false, nullptr),
 895   _max("max", "Maximum number of events to be printed (newest first). If omitted, all events are printed.", "STRING", false, nullptr)
 896 {
 897   _dcmdparser.add_dcmd_option(&_log);
 898   _dcmdparser.add_dcmd_option(&_max);
 899 }
 900 
 901 void EventLogDCmd::execute(DCmdSource source, TRAPS) {
 902   const char* max_value = _max.value();
 903   int max = -1;
 904   if (max_value != nullptr) {
 905     char* endptr = nullptr;
 906     int max;
 907     if (!parse_integer(max_value, &max)) {
 908       output()->print_cr("Invalid max option: \"%s\".", max_value);
 909       return;
 910     }
 911   }
 912   const char* log_name = _log.value();
 913   if (log_name != nullptr) {
 914     Events::print_one(output(), log_name, max);
 915   } else {
 916     Events::print_all(output(), max);
 917   }
 918 }
 919 
 920 void CompilerDirectivesPrintDCmd::execute(DCmdSource source, TRAPS) {
 921   DirectivesStack::print(output());
 922 }
 923 
 924 CompilerDirectivesAddDCmd::CompilerDirectivesAddDCmd(outputStream* output, bool heap) :
 925                            DCmdWithParser(output, heap),
 926   _filename("filename","Name of the directives file", "STRING",true) {
 927   _dcmdparser.add_dcmd_argument(&_filename);
 928 }
 929 
 930 void CompilerDirectivesAddDCmd::execute(DCmdSource source, TRAPS) {
 931   DirectivesParser::parse_from_file(_filename.value(), output(), true);
 932 }
 933 
 934 void CompilerDirectivesRemoveDCmd::execute(DCmdSource source, TRAPS) {
 935   DirectivesStack::pop(1);
 936 }
 937 
 938 void CompilerDirectivesClearDCmd::execute(DCmdSource source, TRAPS) {
 939   DirectivesStack::clear();
 940 }
 941 #if INCLUDE_SERVICES
 942 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 943                                        DCmdWithParser(output, heap),
 944   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 945   _print_subclasses("-s", "If a classname is specified, print its subclasses "
 946                     "in addition to its superclasses. Without this option only the "
 947                     "superclasses will be printed.", "BOOLEAN", false, "false"),
 948   _classname("classname", "Name of class whose hierarchy should be printed. "
 949              "If not specified, all class hierarchies are printed.",
 950              "STRING", false) {
 951   _dcmdparser.add_dcmd_option(&_print_interfaces);
 952   _dcmdparser.add_dcmd_option(&_print_subclasses);
 953   _dcmdparser.add_dcmd_argument(&_classname);
 954 }
 955 
 956 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 957   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 958                                                _print_subclasses.value(), _classname.value());
 959   VMThread::execute(&printClassHierarchyOp);
 960 }
 961 
 962 PrintClassLayoutDCmd::PrintClassLayoutDCmd(outputStream* output, bool heap) :
 963                                        DCmdWithParser(output, heap),
 964   _classname("classname", "Name of class whose layout should be printed. ",
 965              "STRING", true) {
 966   _dcmdparser.add_dcmd_argument(&_classname);
 967 }
 968 
 969 void PrintClassLayoutDCmd::execute(DCmdSource source, TRAPS) {
 970   VM_PrintClassLayout printClassLayoutOp(output(), _classname.value());
 971   VMThread::execute(&printClassLayoutOp);
 972 }
 973 
 974 int PrintClassLayoutDCmd::num_arguments() {
 975   ResourceMark rm;
 976   PrintClassLayoutDCmd* dcmd = new PrintClassLayoutDCmd(nullptr, false);
 977   if (dcmd != nullptr) {
 978     DCmdMark mark(dcmd);
 979     return dcmd->_dcmdparser.num_arguments();
 980   } else {
 981     return 0;
 982   }
 983 }
 984 
 985 #endif // INCLUDE_SERVICES
 986 
 987 ClassesDCmd::ClassesDCmd(outputStream* output, bool heap) :
 988                                      DCmdWithParser(output, heap),
 989   _verbose("-verbose",
 990            "Dump the detailed content of a Java class. "
 991            "Some classes are annotated with flags: "
 992            "F = has, or inherits, a non-empty finalize method, "
 993            "f = has final method, "
 994            "W = methods rewritten, "
 995            "C = marked with @Contended annotation, "
 996            "R = has been redefined, "
 997            "S = is shared class",
 998            "BOOLEAN", false, "false") {
 999   _dcmdparser.add_dcmd_option(&_verbose);
1000 }
1001 
1002 class VM_PrintClasses : public VM_Operation {
1003 private:
1004   outputStream* _out;
1005   bool _verbose;
1006 public:
1007   VM_PrintClasses(outputStream* out, bool verbose) : _out(out), _verbose(verbose) {}
1008 
1009   virtual VMOp_Type type() const { return VMOp_PrintClasses; }
1010 
1011   virtual void doit() {
1012     PrintClassClosure closure(_out, _verbose);
1013     ClassLoaderDataGraph::classes_do(&closure);
1014   }
1015 };
1016 
1017 void ClassesDCmd::execute(DCmdSource source, TRAPS) {
1018   VM_PrintClasses vmop(output(), _verbose.is_set());
1019   VMThread::execute(&vmop);
1020 }
1021 
1022 #if INCLUDE_CDS
1023 #define DEFAULT_CDS_ARCHIVE_FILENAME "java_pid<pid>_<subcmd>.jsa"
1024 
1025 DumpSharedArchiveDCmd::DumpSharedArchiveDCmd(outputStream* output, bool heap) :
1026                                      DCmdWithParser(output, heap),
1027   _suboption("subcmd", "static_dump | dynamic_dump", "STRING", true),
1028   _filename("filename", "Name of shared archive to be dumped", "STRING", false,
1029             DEFAULT_CDS_ARCHIVE_FILENAME)
1030 {
1031   _dcmdparser.add_dcmd_argument(&_suboption);
1032   _dcmdparser.add_dcmd_argument(&_filename);
1033 }
1034 
1035 void DumpSharedArchiveDCmd::execute(DCmdSource source, TRAPS) {
1036   jboolean is_static;
1037   const char* scmd = _suboption.value();
1038 
1039   // The check for _filename.is_set() is because we don't want to use
1040   // DEFAULT_CDS_ARCHIVE_FILENAME, since it is meant as a description
1041   // of the default, not the actual default.
1042   const char* file = _filename.is_set() ? _filename.value() : nullptr;
1043 
1044   if (strcmp(scmd, "static_dump") == 0) {
1045     is_static = JNI_TRUE;
1046     output()->print("Static dump: ");
1047   } else if (strcmp(scmd, "dynamic_dump") == 0) {
1048     is_static = JNI_FALSE;
1049     output()->print("Dynamic dump: ");
1050     if (!UseSharedSpaces) {
1051       output()->print_cr("Dynamic dump is unsupported when base CDS archive is not loaded");
1052       return;
1053     }
1054     if (!RecordDynamicDumpInfo) {
1055       output()->print_cr("Dump dynamic should run with -XX:+RecordDynamicDumpInfo");
1056       return;
1057     }
1058   } else {
1059     output()->print_cr("Invalid command for VM.cds, valid input is static_dump or dynamic_dump");
1060     return;
1061   }
1062 
1063   // call CDS.dumpSharedArchive
1064   Handle fileh;
1065   if (file != nullptr) {
1066     fileh =  java_lang_String::create_from_str(_filename.value(), CHECK);
1067   }
1068   Symbol* cds_name  = vmSymbols::jdk_internal_misc_CDS();
1069   Klass*  cds_klass = SystemDictionary::resolve_or_fail(cds_name, true /*throw error*/,  CHECK);
1070   JavaValue result(T_OBJECT);
1071   JavaCallArguments args;
1072   args.push_int(is_static);
1073   args.push_oop(fileh);
1074   JavaCalls::call_static(&result,
1075                          cds_klass,
1076                          vmSymbols::dumpSharedArchive(),
1077                          vmSymbols::dumpSharedArchive_signature(),
1078                          &args, CHECK);
1079   if (!HAS_PENDING_EXCEPTION) {
1080     assert(result.get_type() == T_OBJECT, "Sanity check");
1081     // result contains the archive name
1082     char* archive_name = java_lang_String::as_utf8_string(result.get_oop());
1083     output()->print_cr("%s", archive_name);
1084   }
1085 }
1086 #endif // INCLUDE_CDS
1087 
1088 #if INCLUDE_JVMTI
1089 extern "C" typedef char const* (JNICALL *debugInit_startDebuggingViaCommandPtr)(JNIEnv* env, jthread thread, char const** transport_name,
1090                                                                                 char const** address, jboolean* first_start);
1091 static debugInit_startDebuggingViaCommandPtr dvc_start_ptr = nullptr;
1092 
1093 void DebugOnCmdStartDCmd::execute(DCmdSource source, TRAPS) {
1094   char const* transport = nullptr;
1095   char const* addr = nullptr;
1096   jboolean is_first_start = JNI_FALSE;
1097   JavaThread* thread = THREAD;
1098   jthread jt = JNIHandles::make_local(thread->threadObj());
1099   ThreadToNativeFromVM ttn(thread);
1100   const char *error = "Could not find jdwp agent.";
1101 
1102   if (!dvc_start_ptr) {
1103     JvmtiAgentList::Iterator it = JvmtiAgentList::agents();
1104     while (it.has_next()) {
1105       JvmtiAgent* agent = it.next();
1106       if ((strcmp("jdwp", agent->name()) == 0) && (dvc_start_ptr == nullptr)) {
1107         char const* func = "debugInit_startDebuggingViaCommand";
1108         dvc_start_ptr = (debugInit_startDebuggingViaCommandPtr) os::find_agent_function(agent, false, &func, 1);
1109       }
1110     }
1111   }
1112 
1113   if (dvc_start_ptr) {
1114     error = dvc_start_ptr(thread->jni_environment(), jt, &transport, &addr, &is_first_start);
1115   }
1116 
1117   if (error != nullptr) {
1118     output()->print_cr("Debugging has not been started: %s", error);
1119   } else {
1120     output()->print_cr(is_first_start ? "Debugging has been started." : "Debugging is already active.");
1121     output()->print_cr("Transport : %s", transport ? transport : "#unknown");
1122     output()->print_cr("Address : %s", addr ? addr : "#unknown");
1123   }
1124 }
1125 #endif // INCLUDE_JVMTI
1126 
1127 ThreadDumpToFileDCmd::ThreadDumpToFileDCmd(outputStream* output, bool heap) :
1128                                            DCmdWithParser(output, heap),
1129   _overwrite("-overwrite", "May overwrite existing file", "BOOLEAN", false, "false"),
1130   _format("-format", "Output format (\"plain\" or \"json\")", "STRING", false, "plain"),
1131   _filepath("filepath", "The file path to the output file", "STRING", true) {
1132   _dcmdparser.add_dcmd_option(&_overwrite);
1133   _dcmdparser.add_dcmd_option(&_format);
1134   _dcmdparser.add_dcmd_argument(&_filepath);
1135 }
1136 
1137 void ThreadDumpToFileDCmd::execute(DCmdSource source, TRAPS) {
1138   bool json = (_format.value() != nullptr) && (strcmp(_format.value(), "json") == 0);
1139   char* path = _filepath.value();
1140   bool overwrite = _overwrite.value();
1141   Symbol* name = (json) ? vmSymbols::dumpThreadsToJson_name() : vmSymbols::dumpThreads_name();
1142   dumpToFile(name, vmSymbols::string_bool_byte_array_signature(), path, overwrite, CHECK);
1143 }
1144 
1145 void ThreadDumpToFileDCmd::dumpToFile(Symbol* name, Symbol* signature, const char* path, bool overwrite, TRAPS) {
1146   ResourceMark rm(THREAD);
1147   HandleMark hm(THREAD);
1148 
1149   Handle h_path = java_lang_String::create_from_str(path, CHECK);
1150 
1151   Symbol* sym = vmSymbols::jdk_internal_vm_ThreadDumper();
1152   Klass* k = SystemDictionary::resolve_or_fail(sym, true, CHECK);
1153   InstanceKlass* ik = InstanceKlass::cast(k);
1154   if (HAS_PENDING_EXCEPTION) {
1155     java_lang_Throwable::print(PENDING_EXCEPTION, output());
1156     output()->cr();
1157     CLEAR_PENDING_EXCEPTION;
1158     return;
1159   }
1160 
1161   // invoke the ThreadDump method to dump to file
1162   JavaValue result(T_OBJECT);
1163   JavaCallArguments args;
1164   args.push_oop(h_path);
1165   args.push_int(overwrite ? JNI_TRUE : JNI_FALSE);
1166   JavaCalls::call_static(&result,
1167                          k,
1168                          name,
1169                          signature,
1170                          &args,
1171                          THREAD);
1172   if (HAS_PENDING_EXCEPTION) {
1173     java_lang_Throwable::print(PENDING_EXCEPTION, output());
1174     output()->cr();
1175     CLEAR_PENDING_EXCEPTION;
1176     return;
1177   }
1178 
1179   // check that result is byte array
1180   oop res = cast_to_oop(result.get_jobject());
1181   assert(res->is_typeArray(), "just checking");
1182   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
1183 
1184   // copy the bytes to the output stream
1185   typeArrayOop ba = typeArrayOop(res);
1186   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
1187   output()->print_raw((const char*)addr, ba->length());
1188 }
1189 
1190 CompilationMemoryStatisticDCmd::CompilationMemoryStatisticDCmd(outputStream* output, bool heap) :
1191     DCmdWithParser(output, heap),
1192   _human_readable("-H", "Human readable format", "BOOLEAN", false, "false"),
1193   _minsize("-s", "Minimum memory size", "MEMORY SIZE", false, "0") {
1194   _dcmdparser.add_dcmd_option(&_human_readable);
1195   _dcmdparser.add_dcmd_option(&_minsize);
1196 }
1197 
1198 void CompilationMemoryStatisticDCmd::execute(DCmdSource source, TRAPS) {
1199   const bool human_readable = _human_readable.value();
1200   const size_t minsize = _minsize.has_value() ? _minsize.value()._size : 0;
1201   CompilationMemoryStatistic::print_all_by_size(output(), human_readable, minsize);
1202 }
1203 
1204 #ifdef LINUX
1205 
1206 SystemMapDCmd::SystemMapDCmd(outputStream* output, bool heap) :
1207     DCmdWithParser(output, heap),
1208   _human_readable("-H", "Human readable format", "BOOLEAN", false, "false") {
1209   _dcmdparser.add_dcmd_option(&_human_readable);
1210 }
1211 
1212 void SystemMapDCmd::execute(DCmdSource source, TRAPS) {
1213   MemMapPrinter::print_all_mappings(output(), _human_readable.value());
1214 }
1215 
1216 SystemDumpMapDCmd::SystemDumpMapDCmd(outputStream* output, bool heap) :
1217     DCmdWithParser(output, heap),
1218   _human_readable("-H", "Human readable format", "BOOLEAN", false, "false"),
1219   _filename("-F", "file path (defaults: \"vm_memory_map_<pid>.txt\")", "STRING", false) {
1220   _dcmdparser.add_dcmd_option(&_human_readable);
1221   _dcmdparser.add_dcmd_option(&_filename);
1222 }
1223 
1224 void SystemDumpMapDCmd::execute(DCmdSource source, TRAPS) {
1225   stringStream default_name;
1226   default_name.print("vm_memory_map_%d.txt", os::current_process_id());
1227   const char* name = _filename.is_set() ? _filename.value() : default_name.base();
1228   fileStream fs(name);
1229   if (fs.is_open()) {
1230     if (!MemTracker::enabled()) {
1231       output()->print_cr("(NMT is disabled, will not annotate mappings).");
1232     }
1233     MemMapPrinter::print_all_mappings(&fs, _human_readable.value());
1234     // For the readers convenience, resolve path name.
1235     char tmp[JVM_MAXPATHLEN];
1236     const char* absname = os::Posix::realpath(name, tmp, sizeof(tmp));
1237     name = absname != nullptr ? absname : name;
1238     output()->print_cr("Memory map dumped to \"%s\".", name);
1239   } else {
1240     output()->print_cr("Failed to open \"%s\" for writing (%s).", name, os::strerror(errno));
1241   }
1242 }
1243 
1244 #endif // LINUX