1 /*
   2  * Copyright (c) 2011, 2015, 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 "classfile/classLoaderStats.hpp"
  27 #include "classfile/compactHashtable.hpp"
  28 #include "gc_implementation/shared/vmGCOperations.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/javaCalls.hpp"
  31 #include "runtime/os.hpp"
  32 #include "services/diagnosticArgument.hpp"
  33 #include "services/diagnosticCommand.hpp"
  34 #include "services/diagnosticFramework.hpp"
  35 #include "services/writeableFlags.hpp"
  36 #include "services/heapDumper.hpp"
  37 #include "services/management.hpp"
  38 #include "utilities/macros.hpp"
  39 #include "oops/objArrayOop.inline.hpp"
  40 
  41 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  42 
  43 void DCmdRegistrant::register_dcmds(){
  44   // Registration of the diagnostic commands
  45   // First argument specifies which interfaces will export the command
  46   // Second argument specifies if the command is enabled
  47   // Third  argument specifies if the command is hidden
  48   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  49                          | DCmd_Source_MBean;
  50   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
  57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  59   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapInfoDCmd>(full_export, true, false));
  61   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<FinalizerInfoDCmd>(full_export, true, false));
  62 #if INCLUDE_SERVICES // Heap dumping/inspection supported
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  64   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  65   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  66   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
  67   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
  68   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
  69 #endif // INCLUDE_SERVICES
  70 #if INCLUDE_JVMTI
  71   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
  72 #endif // INCLUDE_JVMTI
  73   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  74   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
  75   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
  76   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
  77   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
  78   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
  79 
  80   // Enhanced JMX Agent Support
  81   // These commands won't be exported via the DiagnosticCommandMBean until an
  82   // appropriate permission is created for them
  83   uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI;
  84   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartRemoteDCmd>(jmx_agent_export_flags, true,false));
  85   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStartLocalDCmd>(jmx_agent_export_flags, true,false));
  86   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
  87   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStatusDCmd>(jmx_agent_export_flags, true,false));
  88 
  89 }
  90 
  91 #ifndef HAVE_EXTRA_DCMD
  92 void DCmdRegistrant::register_dcmds_ext(){
  93    // Do nothing here
  94 }
  95 #endif
  96 
  97 
  98 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap),
  99   _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
 100   _cmd("command name", "The name of the command for which we want help",
 101         "STRING", false) {
 102   _dcmdparser.add_dcmd_option(&_all);
 103   _dcmdparser.add_dcmd_argument(&_cmd);
 104 };
 105 
 106 void HelpDCmd::execute(DCmdSource source, TRAPS) {
 107   if (_all.value()) {
 108     GrowableArray<const char*>* cmd_list = DCmdFactory::DCmd_list(source);
 109     for (int i = 0; i < cmd_list->length(); i++) {
 110       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 111                                                   strlen(cmd_list->at(i)));
 112       output()->print_cr("%s%s", factory->name(),
 113                          factory->is_enabled() ? "" : " [disabled]");
 114       output()->print_cr("\t%s", factory->description());
 115       output()->cr();
 116       factory = factory->next();
 117     }
 118   } else if (_cmd.has_value()) {
 119     DCmd* cmd = NULL;
 120     DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(),
 121                                                 strlen(_cmd.value()));
 122     if (factory != NULL) {
 123       output()->print_cr("%s%s", factory->name(),
 124                          factory->is_enabled() ? "" : " [disabled]");
 125       output()->print_cr("%s", factory->description());
 126       output()->print_cr("\nImpact: %s", factory->impact());
 127       JavaPermission p = factory->permission();
 128       if(p._class != NULL) {
 129         if(p._action != NULL) {
 130           output()->print_cr("\nPermission: %s(%s, %s)",
 131                   p._class, p._name == NULL ? "null" : p._name, p._action);
 132         } else {
 133           output()->print_cr("\nPermission: %s(%s)",
 134                   p._class, p._name == NULL ? "null" : p._name);
 135         }
 136       }
 137       output()->cr();
 138       cmd = factory->create_resource_instance(output());
 139       if (cmd != NULL) {
 140         DCmdMark mark(cmd);
 141         cmd->print_help(factory->name());
 142       }
 143     } else {
 144       output()->print_cr("Help unavailable : '%s' : No such command", _cmd.value());
 145     }
 146   } else {
 147     output()->print_cr("The following commands are available:");
 148     GrowableArray<const char *>* cmd_list = DCmdFactory::DCmd_list(source);
 149     for (int i = 0; i < cmd_list->length(); i++) {
 150       DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i),
 151                                                   strlen(cmd_list->at(i)));
 152       output()->print_cr("%s%s", factory->name(),
 153                          factory->is_enabled() ? "" : " [disabled]");
 154       factory = factory->_next;
 155     }
 156     output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
 157   }
 158 }
 159 
 160 int HelpDCmd::num_arguments() {
 161   ResourceMark rm;
 162   HelpDCmd* dcmd = new HelpDCmd(NULL, false);
 163   if (dcmd != NULL) {
 164     DCmdMark mark(dcmd);
 165     return dcmd->_dcmdparser.num_arguments();
 166   } else {
 167     return 0;
 168   }
 169 }
 170 
 171 void VersionDCmd::execute(DCmdSource source, TRAPS) {
 172   output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 173           Abstract_VM_Version::vm_release());
 174   JDK_Version jdk_version = JDK_Version::current();
 175   if (jdk_version.update_version() > 0) {
 176     output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 177             jdk_version.minor_version(), jdk_version.update_version());
 178   } else {
 179     output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 180             jdk_version.minor_version());
 181   }
 182 }
 183 
 184 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 185                                    DCmdWithParser(output, heap),
 186   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 187   _dcmdparser.add_dcmd_option(&_all);
 188 }
 189 
 190 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 191   if (_all.value()) {
 192     CommandLineFlags::printFlags(output(), true);
 193   } else {
 194     CommandLineFlags::printSetFlags(output());
 195   }
 196 }
 197 
 198 int PrintVMFlagsDCmd::num_arguments() {
 199     ResourceMark rm;
 200     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 201     if (dcmd != NULL) {
 202       DCmdMark mark(dcmd);
 203       return dcmd->_dcmdparser.num_arguments();
 204     } else {
 205       return 0;
 206     }
 207 }
 208 
 209 SetVMFlagDCmd::SetVMFlagDCmd(outputStream* output, bool heap) :
 210                                    DCmdWithParser(output, heap),
 211   _flag("flag name", "The name of the flag we want to set",
 212         "STRING", true),
 213   _value("string value", "The value we want to set", "STRING", false) {
 214   _dcmdparser.add_dcmd_argument(&_flag);
 215   _dcmdparser.add_dcmd_argument(&_value);
 216 }
 217 
 218 void SetVMFlagDCmd::execute(DCmdSource source, TRAPS) {
 219   const char* val = NULL;
 220   if (_value.value() != NULL) {
 221     val = _value.value();
 222   }
 223 
 224   FormatBuffer<80> err_msg("%s", "");
 225   int ret = WriteableFlags::set_flag(_flag.value(), val, Flag::MANAGEMENT, err_msg);
 226 
 227   if (ret != WriteableFlags::SUCCESS) {
 228     output()->print_cr("%s", err_msg.buffer());
 229   }
 230 }
 231 
 232 int SetVMFlagDCmd::num_arguments() {
 233   ResourceMark rm;
 234   SetVMFlagDCmd* dcmd = new SetVMFlagDCmd(NULL, false);
 235   if (dcmd != NULL) {
 236     DCmdMark mark(dcmd);
 237     return dcmd->_dcmdparser.num_arguments();
 238   } else {
 239     return 0;
 240   }
 241 }
 242 
 243 void JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
 244   if (JvmtiExport::should_post_data_dump()) {
 245     JvmtiExport::post_data_dump();
 246   }
 247 }
 248 
 249 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 250   // load sun.misc.VMSupport
 251   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 252   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 253   instanceKlassHandle ik (THREAD, k);
 254   if (ik->should_be_initialized()) {
 255     ik->initialize(THREAD);
 256   }
 257   if (HAS_PENDING_EXCEPTION) {
 258     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 259     output()->cr();
 260     CLEAR_PENDING_EXCEPTION;
 261     return;
 262   }
 263 
 264   // invoke the serializePropertiesToByteArray method
 265   JavaValue result(T_OBJECT);
 266   JavaCallArguments args;
 267 
 268   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 269   JavaCalls::call_static(&result,
 270                          ik,
 271                          vmSymbols::serializePropertiesToByteArray_name(),
 272                          signature,
 273                          &args,
 274                          THREAD);
 275   if (HAS_PENDING_EXCEPTION) {
 276     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 277     output()->cr();
 278     CLEAR_PENDING_EXCEPTION;
 279     return;
 280   }
 281 
 282   // The result should be a [B
 283   oop res = (oop)result.get_jobject();
 284   assert(res->is_typeArray(), "just checking");
 285   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 286 
 287   // copy the bytes to the output stream
 288   typeArrayOop ba = typeArrayOop(res);
 289   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 290   output()->print_raw((const char*)addr, ba->length());
 291 }
 292 
 293 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 294                            DCmdWithParser(output, heap),
 295   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 296   _dcmdparser.add_dcmd_option(&_date);
 297 }
 298 
 299 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 300   if (_date.value()) {
 301     output()->date_stamp(true, "", ": ");
 302   }
 303   output()->time_stamp().update_to(tty->time_stamp().ticks());
 304   output()->stamp();
 305   output()->print_cr(" s");
 306 }
 307 
 308 int VMUptimeDCmd::num_arguments() {
 309   ResourceMark rm;
 310   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 311   if (dcmd != NULL) {
 312     DCmdMark mark(dcmd);
 313     return dcmd->_dcmdparser.num_arguments();
 314   } else {
 315     return 0;
 316   }
 317 }
 318 
 319 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 320   if (!DisableExplicitGC) {
 321     Universe::heap()->collect(GCCause::_java_lang_system_gc);
 322   } else {
 323     output()->print_cr("Explicit GC is disabled, no GC has been performed.");
 324   }
 325 }
 326 
 327 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 328   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 329                                                  true, CHECK);
 330   instanceKlassHandle klass(THREAD, k);
 331   JavaValue result(T_VOID);
 332   JavaCalls::call_static(&result, klass,
 333                          vmSymbols::run_finalization_name(),
 334                          vmSymbols::void_method_signature(), CHECK);
 335 }
 336 
 337 void HeapInfoDCmd::execute(DCmdSource source, TRAPS) {
 338   Universe::heap()->print_on(output());
 339 }
 340 
 341 void FinalizerInfoDCmd::execute(DCmdSource source, TRAPS) {
 342   ResourceMark rm;
 343 
 344   output()->print_cr("Unreachable instances awaiting finalization:");
 345 
 346   Klass *k = SystemDictionary::Finalizer_klass();
 347   instanceKlassHandle klass(THREAD, k);
 348   JavaValue result(T_ARRAY);
 349 
 350   // We are calling lang.ref.Finalizer::getFinalizerHistogram() method
 351   // and expect it to return array of FinalizerHistogramEntry as Object[]
 352   // actual class layout should be:
 353   // java.lang.ref.Finalizer$FinalizerHistogramEntry
 354   // - klass: 'java/lang/ref/Finalizer$FinalizerHistogramEntry'
 355   // - private 'count' 'I'
 356   // - private 'name' 'Ljava/lang/String;'
 357 
 358 #ifdef _LP64
 359   int count_field_offset = 12;
 360   int name_field_offset = 16;
 361   if (!UseCompressedOops) {
 362     count_field_offset = 16;
 363     name_field_offset = 24;
 364   }
 365 #else
 366   int count_field_offset = 8;
 367   int name_field_offset = 12;
 368 #endif
 369 
 370   JavaCalls::call_static(&result, klass,
 371                          vmSymbols::get_finalizer_histogram_name(),
 372                          vmSymbols::void_object_array_signature(), CHECK);
 373 
 374   objArrayOop result_oop =  (objArrayOop) result.get_jobject();
 375   for (int i = 0; i < result_oop->length(); ++i) {
 376     oop element_oop = result_oop->obj_at(i);
 377     oop str_oop = element_oop->obj_field(name_field_offset);
 378     char *name = java_lang_String::as_utf8_string(str_oop);
 379     int count = element_oop->int_field(count_field_offset);
 380     output()->print_cr("Class %s - %d", name, count);
 381   }
 382 }
 383 
 384 
 385 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 386 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 387                            DCmdWithParser(output, heap),
 388   _filename("filename","Name of the dump file", "STRING",true),
 389   _all("-all", "Dump all objects, including unreachable objects",
 390        "BOOLEAN", false, "false") {
 391   _dcmdparser.add_dcmd_option(&_all);
 392   _dcmdparser.add_dcmd_argument(&_filename);
 393 }
 394 
 395 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 396   // Request a full GC before heap dump if _all is false
 397   // This helps reduces the amount of unreachable objects in the dump
 398   // and makes it easier to browse.
 399   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 400   int res = dumper.dump(_filename.value());
 401   if (res == 0) {
 402     output()->print_cr("Heap dump file created");
 403   } else {
 404     // heap dump failed
 405     ResourceMark rm;
 406     char* error = dumper.error_as_C_string();
 407     if (error == NULL) {
 408       output()->print_cr("Dump failed - reason unknown");
 409     } else {
 410       output()->print_cr("%s", error);
 411     }
 412   }
 413 }
 414 
 415 int HeapDumpDCmd::num_arguments() {
 416   ResourceMark rm;
 417   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 418   if (dcmd != NULL) {
 419     DCmdMark mark(dcmd);
 420     return dcmd->_dcmdparser.num_arguments();
 421   } else {
 422     return 0;
 423   }
 424 }
 425 
 426 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 427                                        DCmdWithParser(output, heap),
 428   _all("-all", "Inspect all objects, including unreachable objects",
 429        "BOOLEAN", false, "false") {
 430   _dcmdparser.add_dcmd_option(&_all);
 431 }
 432 
 433 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 434   VM_GC_HeapInspection heapop(output(),
 435                               !_all.value() /* request full gc if false */);
 436   VMThread::execute(&heapop);
 437 }
 438 
 439 int ClassHistogramDCmd::num_arguments() {
 440   ResourceMark rm;
 441   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 442   if (dcmd != NULL) {
 443     DCmdMark mark(dcmd);
 444     return dcmd->_dcmdparser.num_arguments();
 445   } else {
 446     return 0;
 447   }
 448 }
 449 
 450 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 451 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 452                                        DCmdWithParser(output, heap),
 453   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 454        "BOOLEAN", false, "false"),
 455   _all("-all", "Show all columns",
 456        "BOOLEAN", false, "false"),
 457   _help("-help", "Show meaning of all the columns",
 458        "BOOLEAN", false, "false"),
 459   _columns("columns", "Comma-separated list of all the columns to show. "
 460            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 461            "STRING", false) {
 462   _dcmdparser.add_dcmd_option(&_all);
 463   _dcmdparser.add_dcmd_option(&_csv);
 464   _dcmdparser.add_dcmd_option(&_help);
 465   _dcmdparser.add_dcmd_argument(&_columns);
 466 }
 467 
 468 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 469   if (!UnlockDiagnosticVMOptions) {
 470     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 471     return;
 472   }
 473 
 474   VM_GC_HeapInspection heapop(output(),
 475                               true /* request_full_gc */);
 476   heapop.set_csv_format(_csv.value());
 477   heapop.set_print_help(_help.value());
 478   heapop.set_print_class_stats(true);
 479   if (_all.value()) {
 480     if (_columns.has_value()) {
 481       output()->print_cr("Cannot specify -all and individual columns at the same time");
 482       return;
 483     } else {
 484       heapop.set_columns(NULL);
 485     }
 486   } else {
 487     if (_columns.has_value()) {
 488       heapop.set_columns(_columns.value());
 489     } else {
 490       heapop.set_columns(DEFAULT_COLUMNS);
 491     }
 492   }
 493   VMThread::execute(&heapop);
 494 }
 495 
 496 int ClassStatsDCmd::num_arguments() {
 497   ResourceMark rm;
 498   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 499   if (dcmd != NULL) {
 500     DCmdMark mark(dcmd);
 501     return dcmd->_dcmdparser.num_arguments();
 502   } else {
 503     return 0;
 504   }
 505 }
 506 #endif // INCLUDE_SERVICES
 507 
 508 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 509                                DCmdWithParser(output, heap),
 510   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 511   _dcmdparser.add_dcmd_option(&_locks);
 512 }
 513 
 514 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 515   // thread stacks
 516   VM_PrintThreads op1(output(), _locks.value());
 517   VMThread::execute(&op1);
 518 
 519   // JNI global handles
 520   VM_PrintJNI op2(output());
 521   VMThread::execute(&op2);
 522 
 523   // Deadlock detection
 524   VM_FindDeadlocks op3(output());
 525   VMThread::execute(&op3);
 526 }
 527 
 528 int ThreadDumpDCmd::num_arguments() {
 529   ResourceMark rm;
 530   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 531   if (dcmd != NULL) {
 532     DCmdMark mark(dcmd);
 533     return dcmd->_dcmdparser.num_arguments();
 534   } else {
 535     return 0;
 536   }
 537 }
 538 
 539 // Enhanced JMX Agent support
 540 
 541 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 542 
 543   DCmdWithParser(output, heap_allocated),
 544 
 545   _config_file
 546   ("config.file",
 547    "set com.sun.management.config.file", "STRING", false),
 548 
 549   _jmxremote_port
 550   ("jmxremote.port",
 551    "set com.sun.management.jmxremote.port", "STRING", false),
 552 
 553   _jmxremote_rmi_port
 554   ("jmxremote.rmi.port",
 555    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 556 
 557   _jmxremote_ssl
 558   ("jmxremote.ssl",
 559    "set com.sun.management.jmxremote.ssl", "STRING", false),
 560 
 561   _jmxremote_registry_ssl
 562   ("jmxremote.registry.ssl",
 563    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 564 
 565   _jmxremote_authenticate
 566   ("jmxremote.authenticate",
 567    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 568 
 569   _jmxremote_password_file
 570   ("jmxremote.password.file",
 571    "set com.sun.management.jmxremote.password.file", "STRING", false),
 572 
 573   _jmxremote_access_file
 574   ("jmxremote.access.file",
 575    "set com.sun.management.jmxremote.access.file", "STRING", false),
 576 
 577   _jmxremote_login_config
 578   ("jmxremote.login.config",
 579    "set com.sun.management.jmxremote.login.config", "STRING", false),
 580 
 581   _jmxremote_ssl_enabled_cipher_suites
 582   ("jmxremote.ssl.enabled.cipher.suites",
 583    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 584 
 585   _jmxremote_ssl_enabled_protocols
 586   ("jmxremote.ssl.enabled.protocols",
 587    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 588 
 589   _jmxremote_ssl_need_client_auth
 590   ("jmxremote.ssl.need.client.auth",
 591    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 592 
 593   _jmxremote_ssl_config_file
 594   ("jmxremote.ssl.config.file",
 595    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 596 
 597 // JDP Protocol support
 598   _jmxremote_autodiscovery
 599   ("jmxremote.autodiscovery",
 600    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 601 
 602    _jdp_port
 603   ("jdp.port",
 604    "set com.sun.management.jdp.port", "INT", false),
 605 
 606    _jdp_address
 607   ("jdp.address",
 608    "set com.sun.management.jdp.address", "STRING", false),
 609 
 610    _jdp_source_addr
 611   ("jdp.source_addr",
 612    "set com.sun.management.jdp.source_addr", "STRING", false),
 613 
 614    _jdp_ttl
 615   ("jdp.ttl",
 616    "set com.sun.management.jdp.ttl", "INT", false),
 617 
 618    _jdp_pause
 619   ("jdp.pause",
 620    "set com.sun.management.jdp.pause", "INT", false),
 621 
 622    _jdp_name
 623   ("jdp.name",
 624    "set com.sun.management.jdp.name", "STRING", false)
 625 
 626   {
 627     _dcmdparser.add_dcmd_option(&_config_file);
 628     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 629     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 630     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 631     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 632     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 633     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 634     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 635     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 636     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 637     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 638     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 639     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 640     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 641     _dcmdparser.add_dcmd_option(&_jdp_port);
 642     _dcmdparser.add_dcmd_option(&_jdp_address);
 643     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 644     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 645     _dcmdparser.add_dcmd_option(&_jdp_pause);
 646     _dcmdparser.add_dcmd_option(&_jdp_name);
 647 }
 648 
 649 
 650 int JMXStartRemoteDCmd::num_arguments() {
 651   ResourceMark rm;
 652   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 653   if (dcmd != NULL) {
 654     DCmdMark mark(dcmd);
 655     return dcmd->_dcmdparser.num_arguments();
 656   } else {
 657     return 0;
 658   }
 659 }
 660 
 661 
 662 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 663     ResourceMark rm(THREAD);
 664     HandleMark hm(THREAD);
 665 
 666     // Load and initialize the sun.management.Agent class
 667     // invoke startRemoteManagementAgent(string) method to start
 668     // the remote management server.
 669     // throw java.lang.NoSuchMethodError if the method doesn't exist
 670 
 671     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 672     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 673     instanceKlassHandle ik (THREAD, k);
 674 
 675     JavaValue result(T_VOID);
 676 
 677     // Pass all command line arguments to java as key=value,...
 678     // All checks are done on java side
 679 
 680     int len = 0;
 681     stringStream options;
 682     char comma[2] = {0,0};
 683 
 684     // Leave default values on Agent.class side and pass only
 685     // agruments explicitly set by user. All arguments passed
 686     // to jcmd override properties with the same name set by
 687     // command line with -D or by managmenent.properties
 688     // file.
 689 #define PUT_OPTION(a) \
 690     if ( (a).is_set() ){ \
 691         options.print(\
 692                ( *((a).type()) == 'I' ) ? "%scom.sun.management.%s=%d" : "%scom.sun.management.%s=%s",\
 693                 comma, (a).name(), (a).value()); \
 694         comma[0] = ','; \
 695     }
 696 
 697     PUT_OPTION(_config_file);
 698     PUT_OPTION(_jmxremote_port);
 699     PUT_OPTION(_jmxremote_rmi_port);
 700     PUT_OPTION(_jmxremote_ssl);
 701     PUT_OPTION(_jmxremote_registry_ssl);
 702     PUT_OPTION(_jmxremote_authenticate);
 703     PUT_OPTION(_jmxremote_password_file);
 704     PUT_OPTION(_jmxremote_access_file);
 705     PUT_OPTION(_jmxremote_login_config);
 706     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 707     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 708     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 709     PUT_OPTION(_jmxremote_ssl_config_file);
 710     PUT_OPTION(_jmxremote_autodiscovery);
 711     PUT_OPTION(_jdp_port);
 712     PUT_OPTION(_jdp_address);
 713     PUT_OPTION(_jdp_source_addr);
 714     PUT_OPTION(_jdp_ttl);
 715     PUT_OPTION(_jdp_pause);
 716     PUT_OPTION(_jdp_name);
 717 
 718 #undef PUT_OPTION
 719 
 720     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 721     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 722 }
 723 
 724 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 725   DCmd(output, heap_allocated) {
 726   // do nothing
 727 }
 728 
 729 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 730     ResourceMark rm(THREAD);
 731     HandleMark hm(THREAD);
 732 
 733     // Load and initialize the sun.management.Agent class
 734     // invoke startLocalManagementAgent(void) method to start
 735     // the local management server
 736     // throw java.lang.NoSuchMethodError if method doesn't exist
 737 
 738     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 739     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 740     instanceKlassHandle ik (THREAD, k);
 741 
 742     JavaValue result(T_VOID);
 743     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 744 }
 745 
 746 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 747     ResourceMark rm(THREAD);
 748     HandleMark hm(THREAD);
 749 
 750     // Load and initialize the sun.management.Agent class
 751     // invoke stopRemoteManagementAgent method to stop the
 752     // management server
 753     // throw java.lang.NoSuchMethodError if method doesn't exist
 754 
 755     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 756     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 757     instanceKlassHandle ik (THREAD, k);
 758 
 759     JavaValue result(T_VOID);
 760     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 761 }
 762 
 763 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 764   DCmd(output, heap_allocated) {
 765   // do nothing
 766 }
 767 
 768 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 769   ResourceMark rm(THREAD);
 770   HandleMark hm(THREAD);
 771 
 772   // Load and initialize the sun.management.Agent class
 773   // invoke getManagementAgentStatus() method to generate the status info
 774   // throw java.lang.NoSuchMethodError if method doesn't exist
 775 
 776   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 777   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 778   instanceKlassHandle ik (THREAD, k);
 779 
 780   JavaValue result(T_OBJECT);
 781   JavaCalls::call_static(&result, ik, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 782 
 783   jvalue* jv = (jvalue*) result.get_value_addr();
 784   oop str = (oop) jv->l;
 785   if (str != NULL) {
 786       char* out = java_lang_String::as_utf8_string(str);
 787       if (out) {
 788           output()->print_cr("%s", out);
 789           return;
 790       }
 791   }
 792   output()->print_cr("Error obtaining management agent status");
 793 }
 794 
 795 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 796   DCmd(output, heap_allocated) {
 797   // do nothing
 798 }
 799 
 800 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 801   os::print_dll_info(output());
 802   output()->cr();
 803 }
 804 
 805 void RotateGCLogDCmd::execute(DCmdSource source, TRAPS) {
 806   if (UseGCLogFileRotation) {
 807     VM_RotateGCLog rotateop(output());
 808     VMThread::execute(&rotateop);
 809   } else {
 810     output()->print_cr("Target VM does not support GC log file rotation.");
 811   }
 812 }
 813 
 814 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 815   VM_PrintCompileQueue printCompileQueueOp(output());
 816   VMThread::execute(&printCompileQueueOp);
 817 }
 818 
 819 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 820   VM_PrintCodeList printCodeListOp(output());
 821   VMThread::execute(&printCodeListOp);
 822 }
 823 
 824 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 825   VM_PrintCodeCache printCodeCacheOp(output());
 826   VMThread::execute(&printCodeCacheOp);
 827 }
 828 
 829 #if INCLUDE_SERVICES
 830 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 831                                        DCmdWithParser(output, heap),
 832   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 833   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 834                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 835   _classname("classname", "Name of class whose hierarchy should be printed. "
 836              "If not specified, all class hierarchies are printed.",
 837              "STRING", false) {
 838   _dcmdparser.add_dcmd_option(&_print_interfaces);
 839   _dcmdparser.add_dcmd_option(&_print_subclasses);
 840   _dcmdparser.add_dcmd_argument(&_classname);
 841 }
 842 
 843 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 844   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 845                                                _print_subclasses.value(), _classname.value());
 846   VMThread::execute(&printClassHierarchyOp);
 847 }
 848 
 849 int ClassHierarchyDCmd::num_arguments() {
 850   ResourceMark rm;
 851   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 852   if (dcmd != NULL) {
 853     DCmdMark mark(dcmd);
 854     return dcmd->_dcmdparser.num_arguments();
 855   } else {
 856     return 0;
 857   }
 858 }
 859 
 860 #endif