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/shared/vmGCOperations.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "runtime/javaCalls.hpp"
  32 #include "runtime/os.hpp"
  33 #include "services/diagnosticArgument.hpp"
  34 #include "services/diagnosticCommand.hpp"
  35 #include "services/diagnosticFramework.hpp"
  36 #include "services/heapDumper.hpp"
  37 #include "services/management.hpp"
  38 #include "services/writeableFlags.hpp"
  39 #include "utilities/macros.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 
  42 void DCmdRegistrant::register_dcmds(){
  43   // Registration of the diagnostic commands
  44   // First argument specifies which interfaces will export the command
  45   // Second argument specifies if the command is enabled
  46   // Third  argument specifies if the command is hidden
  47   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI
  48                          | DCmd_Source_MBean;
  49   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HelpDCmd>(full_export, true, false));
  50   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VersionDCmd>(full_export, true, false));
  51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
  52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
  53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
  54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
  55   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
  56   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
  57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
  58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RunFinalizationDCmd>(full_export, true, false));
  59   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapInfoDCmd>(full_export, true, false));
  60   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<FinalizerInfoDCmd>(full_export, true, false));
  61 #if INCLUDE_SERVICES // Heap dumping/inspection supported
  62   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<HeapDumpDCmd>(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false));
  63   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHistogramDCmd>(full_export, true, false));
  64   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassStatsDCmd>(full_export, true, false));
  65   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassHierarchyDCmd>(full_export, true, false));
  66   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
  67   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
  68 #endif // INCLUDE_SERVICES
  69 #if INCLUDE_JVMTI
  70   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
  71 #endif // INCLUDE_JVMTI
  72   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
  73   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
  74   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
  75   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CompileQueueDCmd>(full_export, true, false));
  76   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeListDCmd>(full_export, true, false));
  77   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeCacheDCmd>(full_export, true, false));
  78   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<TouchedMethodsDCmd>(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 != Flag::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::_dcmd_gc_run);
 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 
 345   Klass* k = SystemDictionary::resolve_or_null(
 346     vmSymbols::finalizer_histogram_klass(), THREAD);
 347   assert(k != NULL, "FinalizerHistogram class is not accessible");
 348 
 349   instanceKlassHandle klass(THREAD, k);
 350   JavaValue result(T_ARRAY);
 351 
 352   // We are calling lang.ref.FinalizerHistogram.getFinalizerHistogram() method
 353   // and expect it to return array of FinalizerHistogramEntry as Object[]
 354 
 355   JavaCalls::call_static(&result, klass,
 356                          vmSymbols::get_finalizer_histogram_name(),
 357                          vmSymbols::void_finalizer_histogram_entry_array_signature(), CHECK);
 358 
 359   objArrayOop result_oop = (objArrayOop) result.get_jobject();
 360   if (result_oop->length() == 0) {
 361     output()->print_cr("No instances waiting for finalization found");
 362     return;
 363   }
 364 
 365   oop foop = result_oop->obj_at(0);
 366   InstanceKlass* ik = InstanceKlass::cast(foop->klass());
 367 
 368   fieldDescriptor count_fd, name_fd;
 369 
 370   Klass* count_res = ik->find_field(
 371     vmSymbols::finalizer_histogram_entry_count_field(), vmSymbols::int_signature(), &count_fd);
 372 
 373   Klass* name_res = ik->find_field(
 374     vmSymbols::finalizer_histogram_entry_name_field(), vmSymbols::string_signature(), &name_fd);
 375 
 376   assert(count_res != NULL && name_res != NULL, "Unexpected layout of FinalizerHistogramEntry");
 377 
 378   output()->print_cr("Unreachable instances waiting for finalization");
 379   output()->print_cr("#instances  class name");
 380   output()->print_cr("-----------------------");
 381 
 382   for (int i = 0; i < result_oop->length(); ++i) {
 383     oop element_oop = result_oop->obj_at(i);
 384     oop str_oop = element_oop->obj_field(name_fd.offset());
 385     char *name = java_lang_String::as_utf8_string(str_oop);
 386     int count = element_oop->int_field(count_fd.offset());
 387     output()->print_cr("%10d  %s", count, name);
 388   }
 389 }
 390 
 391 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 392 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 393                            DCmdWithParser(output, heap),
 394   _filename("filename","Name of the dump file", "STRING",true),
 395   _all("-all", "Dump all objects, including unreachable objects",
 396        "BOOLEAN", false, "false") {
 397   _dcmdparser.add_dcmd_option(&_all);
 398   _dcmdparser.add_dcmd_argument(&_filename);
 399 }
 400 
 401 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 402   // Request a full GC before heap dump if _all is false
 403   // This helps reduces the amount of unreachable objects in the dump
 404   // and makes it easier to browse.
 405   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 406   int res = dumper.dump(_filename.value());
 407   if (res == 0) {
 408     output()->print_cr("Heap dump file created");
 409   } else {
 410     // heap dump failed
 411     ResourceMark rm;
 412     char* error = dumper.error_as_C_string();
 413     if (error == NULL) {
 414       output()->print_cr("Dump failed - reason unknown");
 415     } else {
 416       output()->print_cr("%s", error);
 417     }
 418   }
 419 }
 420 
 421 int HeapDumpDCmd::num_arguments() {
 422   ResourceMark rm;
 423   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 424   if (dcmd != NULL) {
 425     DCmdMark mark(dcmd);
 426     return dcmd->_dcmdparser.num_arguments();
 427   } else {
 428     return 0;
 429   }
 430 }
 431 
 432 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 433                                        DCmdWithParser(output, heap),
 434   _all("-all", "Inspect all objects, including unreachable objects",
 435        "BOOLEAN", false, "false") {
 436   _dcmdparser.add_dcmd_option(&_all);
 437 }
 438 
 439 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 440   VM_GC_HeapInspection heapop(output(),
 441                               !_all.value() /* request full gc if false */);
 442   VMThread::execute(&heapop);
 443 }
 444 
 445 int ClassHistogramDCmd::num_arguments() {
 446   ResourceMark rm;
 447   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 448   if (dcmd != NULL) {
 449     DCmdMark mark(dcmd);
 450     return dcmd->_dcmdparser.num_arguments();
 451   } else {
 452     return 0;
 453   }
 454 }
 455 
 456 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 457 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 458                                        DCmdWithParser(output, heap),
 459   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 460        "BOOLEAN", false, "false"),
 461   _all("-all", "Show all columns",
 462        "BOOLEAN", false, "false"),
 463   _help("-help", "Show meaning of all the columns",
 464        "BOOLEAN", false, "false"),
 465   _columns("columns", "Comma-separated list of all the columns to show. "
 466            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 467            "STRING", false) {
 468   _dcmdparser.add_dcmd_option(&_all);
 469   _dcmdparser.add_dcmd_option(&_csv);
 470   _dcmdparser.add_dcmd_option(&_help);
 471   _dcmdparser.add_dcmd_argument(&_columns);
 472 }
 473 
 474 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 475   if (!UnlockDiagnosticVMOptions) {
 476     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 477     return;
 478   }
 479 
 480   VM_GC_HeapInspection heapop(output(),
 481                               true /* request_full_gc */);
 482   heapop.set_csv_format(_csv.value());
 483   heapop.set_print_help(_help.value());
 484   heapop.set_print_class_stats(true);
 485   if (_all.value()) {
 486     if (_columns.has_value()) {
 487       output()->print_cr("Cannot specify -all and individual columns at the same time");
 488       return;
 489     } else {
 490       heapop.set_columns(NULL);
 491     }
 492   } else {
 493     if (_columns.has_value()) {
 494       heapop.set_columns(_columns.value());
 495     } else {
 496       heapop.set_columns(DEFAULT_COLUMNS);
 497     }
 498   }
 499   VMThread::execute(&heapop);
 500 }
 501 
 502 int ClassStatsDCmd::num_arguments() {
 503   ResourceMark rm;
 504   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 505   if (dcmd != NULL) {
 506     DCmdMark mark(dcmd);
 507     return dcmd->_dcmdparser.num_arguments();
 508   } else {
 509     return 0;
 510   }
 511 }
 512 #endif // INCLUDE_SERVICES
 513 
 514 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 515                                DCmdWithParser(output, heap),
 516   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 517   _dcmdparser.add_dcmd_option(&_locks);
 518 }
 519 
 520 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 521   // thread stacks
 522   VM_PrintThreads op1(output(), _locks.value());
 523   VMThread::execute(&op1);
 524 
 525   // JNI global handles
 526   VM_PrintJNI op2(output());
 527   VMThread::execute(&op2);
 528 
 529   // Deadlock detection
 530   VM_FindDeadlocks op3(output());
 531   VMThread::execute(&op3);
 532 }
 533 
 534 int ThreadDumpDCmd::num_arguments() {
 535   ResourceMark rm;
 536   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 537   if (dcmd != NULL) {
 538     DCmdMark mark(dcmd);
 539     return dcmd->_dcmdparser.num_arguments();
 540   } else {
 541     return 0;
 542   }
 543 }
 544 
 545 // Enhanced JMX Agent support
 546 
 547 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 548 
 549   DCmdWithParser(output, heap_allocated),
 550 
 551   _config_file
 552   ("config.file",
 553    "set com.sun.management.config.file", "STRING", false),
 554 
 555   _jmxremote_host
 556   ("jmxremote.host",
 557    "set com.sun.management.jmxremote.host", "STRING", false),
 558 
 559   _jmxremote_port
 560   ("jmxremote.port",
 561    "set com.sun.management.jmxremote.port", "STRING", false),
 562 
 563   _jmxremote_rmi_port
 564   ("jmxremote.rmi.port",
 565    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 566 
 567   _jmxremote_ssl
 568   ("jmxremote.ssl",
 569    "set com.sun.management.jmxremote.ssl", "STRING", false),
 570 
 571   _jmxremote_registry_ssl
 572   ("jmxremote.registry.ssl",
 573    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 574 
 575   _jmxremote_authenticate
 576   ("jmxremote.authenticate",
 577    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 578 
 579   _jmxremote_password_file
 580   ("jmxremote.password.file",
 581    "set com.sun.management.jmxremote.password.file", "STRING", false),
 582 
 583   _jmxremote_access_file
 584   ("jmxremote.access.file",
 585    "set com.sun.management.jmxremote.access.file", "STRING", false),
 586 
 587   _jmxremote_login_config
 588   ("jmxremote.login.config",
 589    "set com.sun.management.jmxremote.login.config", "STRING", false),
 590 
 591   _jmxremote_ssl_enabled_cipher_suites
 592   ("jmxremote.ssl.enabled.cipher.suites",
 593    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 594 
 595   _jmxremote_ssl_enabled_protocols
 596   ("jmxremote.ssl.enabled.protocols",
 597    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 598 
 599   _jmxremote_ssl_need_client_auth
 600   ("jmxremote.ssl.need.client.auth",
 601    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 602 
 603   _jmxremote_ssl_config_file
 604   ("jmxremote.ssl.config.file",
 605    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 606 
 607 // JDP Protocol support
 608   _jmxremote_autodiscovery
 609   ("jmxremote.autodiscovery",
 610    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 611 
 612    _jdp_port
 613   ("jdp.port",
 614    "set com.sun.management.jdp.port", "INT", false),
 615 
 616    _jdp_address
 617   ("jdp.address",
 618    "set com.sun.management.jdp.address", "STRING", false),
 619 
 620    _jdp_source_addr
 621   ("jdp.source_addr",
 622    "set com.sun.management.jdp.source_addr", "STRING", false),
 623 
 624    _jdp_ttl
 625   ("jdp.ttl",
 626    "set com.sun.management.jdp.ttl", "INT", false),
 627 
 628    _jdp_pause
 629   ("jdp.pause",
 630    "set com.sun.management.jdp.pause", "INT", false),
 631 
 632    _jdp_name
 633   ("jdp.name",
 634    "set com.sun.management.jdp.name", "STRING", false)
 635 
 636   {
 637     _dcmdparser.add_dcmd_option(&_config_file);
 638     _dcmdparser.add_dcmd_option(&_jmxremote_host);
 639     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 640     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 641     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 642     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 643     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 644     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 645     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 646     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 647     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 648     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 649     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 650     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 651     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 652     _dcmdparser.add_dcmd_option(&_jdp_port);
 653     _dcmdparser.add_dcmd_option(&_jdp_address);
 654     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 655     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 656     _dcmdparser.add_dcmd_option(&_jdp_pause);
 657     _dcmdparser.add_dcmd_option(&_jdp_name);
 658 }
 659 
 660 
 661 int JMXStartRemoteDCmd::num_arguments() {
 662   ResourceMark rm;
 663   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 664   if (dcmd != NULL) {
 665     DCmdMark mark(dcmd);
 666     return dcmd->_dcmdparser.num_arguments();
 667   } else {
 668     return 0;
 669   }
 670 }
 671 
 672 
 673 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 674     ResourceMark rm(THREAD);
 675     HandleMark hm(THREAD);
 676 
 677     // Load and initialize the sun.management.Agent class
 678     // invoke startRemoteManagementAgent(string) method to start
 679     // the remote management server.
 680     // throw java.lang.NoSuchMethodError if the method doesn't exist
 681 
 682     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 683     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 684     instanceKlassHandle ik (THREAD, k);
 685 
 686     JavaValue result(T_VOID);
 687 
 688     // Pass all command line arguments to java as key=value,...
 689     // All checks are done on java side
 690 
 691     int len = 0;
 692     stringStream options;
 693     char comma[2] = {0,0};
 694 
 695     // Leave default values on Agent.class side and pass only
 696     // agruments explicitly set by user. All arguments passed
 697     // to jcmd override properties with the same name set by
 698     // command line with -D or by managmenent.properties
 699     // file.
 700 #define PUT_OPTION(a) \
 701     do { \
 702         if ( (a).is_set() ){ \
 703             if ( *((a).type()) == 'I' ) { \
 704                 options.print("%scom.sun.management.%s=" JLONG_FORMAT, comma, (a).name(), (jlong)((a).value())); \
 705             } else { \
 706                 options.print("%scom.sun.management.%s=%s", comma, (a).name(), (char*)((a).value())); \
 707             } \
 708             comma[0] = ','; \
 709         }\
 710     } while(0);
 711 
 712 
 713     PUT_OPTION(_config_file);
 714     PUT_OPTION(_jmxremote_host);
 715     PUT_OPTION(_jmxremote_port);
 716     PUT_OPTION(_jmxremote_rmi_port);
 717     PUT_OPTION(_jmxremote_ssl);
 718     PUT_OPTION(_jmxremote_registry_ssl);
 719     PUT_OPTION(_jmxremote_authenticate);
 720     PUT_OPTION(_jmxremote_password_file);
 721     PUT_OPTION(_jmxremote_access_file);
 722     PUT_OPTION(_jmxremote_login_config);
 723     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 724     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 725     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 726     PUT_OPTION(_jmxremote_ssl_config_file);
 727     PUT_OPTION(_jmxremote_autodiscovery);
 728     PUT_OPTION(_jdp_port);
 729     PUT_OPTION(_jdp_address);
 730     PUT_OPTION(_jdp_source_addr);
 731     PUT_OPTION(_jdp_ttl);
 732     PUT_OPTION(_jdp_pause);
 733     PUT_OPTION(_jdp_name);
 734 
 735 #undef PUT_OPTION
 736 
 737     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 738     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 739 }
 740 
 741 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 742   DCmd(output, heap_allocated) {
 743   // do nothing
 744 }
 745 
 746 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 747     ResourceMark rm(THREAD);
 748     HandleMark hm(THREAD);
 749 
 750     // Load and initialize the sun.management.Agent class
 751     // invoke startLocalManagementAgent(void) method to start
 752     // the local 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::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 761 }
 762 
 763 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 764     ResourceMark rm(THREAD);
 765     HandleMark hm(THREAD);
 766 
 767     // Load and initialize the sun.management.Agent class
 768     // invoke stopRemoteManagementAgent method to stop the
 769     // management server
 770     // throw java.lang.NoSuchMethodError if method doesn't exist
 771 
 772     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 773     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 774     instanceKlassHandle ik (THREAD, k);
 775 
 776     JavaValue result(T_VOID);
 777     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 778 }
 779 
 780 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 781   DCmd(output, heap_allocated) {
 782   // do nothing
 783 }
 784 
 785 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 786   ResourceMark rm(THREAD);
 787   HandleMark hm(THREAD);
 788 
 789   // Load and initialize the sun.management.Agent class
 790   // invoke getManagementAgentStatus() method to generate the status info
 791   // throw java.lang.NoSuchMethodError if method doesn't exist
 792 
 793   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 794   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 795   instanceKlassHandle ik (THREAD, k);
 796 
 797   JavaValue result(T_OBJECT);
 798   JavaCalls::call_static(&result, ik, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 799 
 800   jvalue* jv = (jvalue*) result.get_value_addr();
 801   oop str = (oop) jv->l;
 802   if (str != NULL) {
 803       char* out = java_lang_String::as_utf8_string(str);
 804       if (out) {
 805           output()->print_cr("%s", out);
 806           return;
 807       }
 808   }
 809   output()->print_cr("Error obtaining management agent status");
 810 }
 811 
 812 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 813   DCmd(output, heap_allocated) {
 814   // do nothing
 815 }
 816 
 817 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 818   os::print_dll_info(output());
 819   output()->cr();
 820 }
 821 
 822 void RotateGCLogDCmd::execute(DCmdSource source, TRAPS) {
 823   if (UseGCLogFileRotation) {
 824     VM_RotateGCLog rotateop(output());
 825     VMThread::execute(&rotateop);
 826   } else {
 827     output()->print_cr("Target VM does not support GC log file rotation.");
 828   }
 829 }
 830 
 831 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 832   VM_PrintCompileQueue printCompileQueueOp(output());
 833   VMThread::execute(&printCompileQueueOp);
 834 }
 835 
 836 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 837   VM_PrintCodeList printCodeListOp(output());
 838   VMThread::execute(&printCodeListOp);
 839 }
 840 
 841 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 842   VM_PrintCodeCache printCodeCacheOp(output());
 843   VMThread::execute(&printCodeCacheOp);
 844 }
 845 
 846 #if INCLUDE_SERVICES
 847 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 848                                        DCmdWithParser(output, heap),
 849   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 850   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 851                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 852   _classname("classname", "Name of class whose hierarchy should be printed. "
 853              "If not specified, all class hierarchies are printed.",
 854              "STRING", false) {
 855   _dcmdparser.add_dcmd_option(&_print_interfaces);
 856   _dcmdparser.add_dcmd_option(&_print_subclasses);
 857   _dcmdparser.add_dcmd_argument(&_classname);
 858 }
 859 
 860 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 861   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 862                                                _print_subclasses.value(), _classname.value());
 863   VMThread::execute(&printClassHierarchyOp);
 864 }
 865 
 866 int ClassHierarchyDCmd::num_arguments() {
 867   ResourceMark rm;
 868   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 869   if (dcmd != NULL) {
 870     DCmdMark mark(dcmd);
 871     return dcmd->_dcmdparser.num_arguments();
 872   } else {
 873     return 0;
 874   }
 875 }
 876 
 877 #endif
 878 
 879 class VM_DumpTouchedMethods : public VM_Operation {
 880 private:
 881   outputStream* _out;
 882 public:
 883   VM_DumpTouchedMethods(outputStream* out) {
 884     _out = out;
 885   }
 886 
 887   virtual VMOp_Type type() const { return VMOp_DumpTouchedMethods; }
 888 
 889   virtual void doit() {
 890     Method::print_touched_methods(_out);
 891   }
 892 };
 893 
 894 TouchedMethodsDCmd::TouchedMethodsDCmd(outputStream* output, bool heap) :
 895                                        DCmdWithParser(output, heap)
 896 {}
 897 
 898 void TouchedMethodsDCmd::execute(DCmdSource source, TRAPS) {
 899   if (!UnlockDiagnosticVMOptions) {
 900     output()->print_cr("VM.touched_methods command requires -XX:+UnlockDiagnosticVMOptions");
 901     return;
 902   }
 903   VM_DumpTouchedMethods dumper(output());
 904   VMThread::execute(&dumper);
 905 }
 906 
 907 int TouchedMethodsDCmd::num_arguments() {
 908   return 0;
 909 }