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