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