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