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