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