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