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.update_version() > 0) {
 183     output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 184             jdk_version.minor_version(), jdk_version.update_version());
 185   } else {
 186     output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 187             jdk_version.minor_version());
 188   }
 189 }
 190 
 191 PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) :
 192                                    DCmdWithParser(output, heap),
 193   _all("-all", "Print all flags supported by the VM", "BOOLEAN", false, "false") {
 194   _dcmdparser.add_dcmd_option(&_all);
 195 }
 196 
 197 void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) {
 198   if (_all.value()) {
 199     CommandLineFlags::printFlags(output(), true);
 200   } else {
 201     CommandLineFlags::printSetFlags(output());
 202   }
 203 }
 204 
 205 int PrintVMFlagsDCmd::num_arguments() {
 206     ResourceMark rm;
 207     PrintVMFlagsDCmd* dcmd = new PrintVMFlagsDCmd(NULL, false);
 208     if (dcmd != NULL) {
 209       DCmdMark mark(dcmd);
 210       return dcmd->_dcmdparser.num_arguments();
 211     } else {
 212       return 0;
 213     }
 214 }
 215 
 216 SetVMFlagDCmd::SetVMFlagDCmd(outputStream* output, bool heap) :
 217                                    DCmdWithParser(output, heap),
 218   _flag("flag name", "The name of the flag we want to set",
 219         "STRING", true),
 220   _value("string value", "The value we want to set", "STRING", false) {
 221   _dcmdparser.add_dcmd_argument(&_flag);
 222   _dcmdparser.add_dcmd_argument(&_value);
 223 }
 224 
 225 void SetVMFlagDCmd::execute(DCmdSource source, TRAPS) {
 226   const char* val = NULL;
 227   if (_value.value() != NULL) {
 228     val = _value.value();
 229   }
 230 
 231   FormatBuffer<80> err_msg("%s", "");
 232   int ret = WriteableFlags::set_flag(_flag.value(), val, Flag::MANAGEMENT, err_msg);
 233 
 234   if (ret != Flag::SUCCESS) {
 235     output()->print_cr("%s", err_msg.buffer());
 236   }
 237 }
 238 
 239 int SetVMFlagDCmd::num_arguments() {
 240   ResourceMark rm;
 241   SetVMFlagDCmd* dcmd = new SetVMFlagDCmd(NULL, false);
 242   if (dcmd != NULL) {
 243     DCmdMark mark(dcmd);
 244     return dcmd->_dcmdparser.num_arguments();
 245   } else {
 246     return 0;
 247   }
 248 }
 249 
 250 void JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
 251   if (JvmtiExport::should_post_data_dump()) {
 252     JvmtiExport::post_data_dump();
 253   }
 254 }
 255 
 256 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
 257   // load sun.misc.VMSupport
 258   Symbol* klass = vmSymbols::sun_misc_VMSupport();
 259   Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK);
 260   instanceKlassHandle ik (THREAD, k);
 261   if (ik->should_be_initialized()) {
 262     ik->initialize(THREAD);
 263   }
 264   if (HAS_PENDING_EXCEPTION) {
 265     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 266     output()->cr();
 267     CLEAR_PENDING_EXCEPTION;
 268     return;
 269   }
 270 
 271   // invoke the serializePropertiesToByteArray method
 272   JavaValue result(T_OBJECT);
 273   JavaCallArguments args;
 274 
 275   Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
 276   JavaCalls::call_static(&result,
 277                          ik,
 278                          vmSymbols::serializePropertiesToByteArray_name(),
 279                          signature,
 280                          &args,
 281                          THREAD);
 282   if (HAS_PENDING_EXCEPTION) {
 283     java_lang_Throwable::print(PENDING_EXCEPTION, output());
 284     output()->cr();
 285     CLEAR_PENDING_EXCEPTION;
 286     return;
 287   }
 288 
 289   // The result should be a [B
 290   oop res = (oop)result.get_jobject();
 291   assert(res->is_typeArray(), "just checking");
 292   assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
 293 
 294   // copy the bytes to the output stream
 295   typeArrayOop ba = typeArrayOop(res);
 296   jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
 297   output()->print_raw((const char*)addr, ba->length());
 298 }
 299 
 300 VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) :
 301                            DCmdWithParser(output, heap),
 302   _date("-date", "Add a prefix with current date", "BOOLEAN", false, "false") {
 303   _dcmdparser.add_dcmd_option(&_date);
 304 }
 305 
 306 void VMUptimeDCmd::execute(DCmdSource source, TRAPS) {
 307   if (_date.value()) {
 308     output()->date_stamp(true, "", ": ");
 309   }
 310   output()->time_stamp().update_to(tty->time_stamp().ticks());
 311   output()->stamp();
 312   output()->print_cr(" s");
 313 }
 314 
 315 int VMUptimeDCmd::num_arguments() {
 316   ResourceMark rm;
 317   VMUptimeDCmd* dcmd = new VMUptimeDCmd(NULL, false);
 318   if (dcmd != NULL) {
 319     DCmdMark mark(dcmd);
 320     return dcmd->_dcmdparser.num_arguments();
 321   } else {
 322     return 0;
 323   }
 324 }
 325 
 326 void VMInfoDCmd::execute(DCmdSource source, TRAPS) {
 327   VMError::print_vm_info(_output);
 328 }
 329 
 330 void SystemGCDCmd::execute(DCmdSource source, TRAPS) {
 331   if (!DisableExplicitGC) {
 332     Universe::heap()->collect(GCCause::_dcmd_gc_run);
 333   } else {
 334     output()->print_cr("Explicit GC is disabled, no GC has been performed.");
 335   }
 336 }
 337 
 338 void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) {
 339   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(),
 340                                                  true, CHECK);
 341   instanceKlassHandle klass(THREAD, k);
 342   JavaValue result(T_VOID);
 343   JavaCalls::call_static(&result, klass,
 344                          vmSymbols::run_finalization_name(),
 345                          vmSymbols::void_method_signature(), CHECK);
 346 }
 347 
 348 void HeapInfoDCmd::execute(DCmdSource source, TRAPS) {
 349   Universe::heap()->print_on(output());
 350 }
 351 
 352 void FinalizerInfoDCmd::execute(DCmdSource source, TRAPS) {
 353   ResourceMark rm;
 354 
 355 
 356   Klass* k = SystemDictionary::resolve_or_null(
 357     vmSymbols::finalizer_histogram_klass(), THREAD);
 358   assert(k != NULL, "FinalizerHistogram class is not accessible");
 359 
 360   instanceKlassHandle klass(THREAD, k);
 361   JavaValue result(T_ARRAY);
 362 
 363   // We are calling lang.ref.FinalizerHistogram.getFinalizerHistogram() method
 364   // and expect it to return array of FinalizerHistogramEntry as Object[]
 365 
 366   JavaCalls::call_static(&result, klass,
 367                          vmSymbols::get_finalizer_histogram_name(),
 368                          vmSymbols::void_finalizer_histogram_entry_array_signature(), CHECK);
 369 
 370   objArrayOop result_oop = (objArrayOop) result.get_jobject();
 371   if (result_oop->length() == 0) {
 372     output()->print_cr("No instances waiting for finalization found");
 373     return;
 374   }
 375 
 376   oop foop = result_oop->obj_at(0);
 377   InstanceKlass* ik = InstanceKlass::cast(foop->klass());
 378 
 379   fieldDescriptor count_fd, name_fd;
 380 
 381   Klass* count_res = ik->find_field(
 382     vmSymbols::finalizer_histogram_entry_count_field(), vmSymbols::int_signature(), &count_fd);
 383 
 384   Klass* name_res = ik->find_field(
 385     vmSymbols::finalizer_histogram_entry_name_field(), vmSymbols::string_signature(), &name_fd);
 386 
 387   assert(count_res != NULL && name_res != NULL, "Unexpected layout of FinalizerHistogramEntry");
 388 
 389   output()->print_cr("Unreachable instances waiting for finalization");
 390   output()->print_cr("#instances  class name");
 391   output()->print_cr("-----------------------");
 392 
 393   for (int i = 0; i < result_oop->length(); ++i) {
 394     oop element_oop = result_oop->obj_at(i);
 395     oop str_oop = element_oop->obj_field(name_fd.offset());
 396     char *name = java_lang_String::as_utf8_string(str_oop);
 397     int count = element_oop->int_field(count_fd.offset());
 398     output()->print_cr("%10d  %s", count, name);
 399   }
 400 }
 401 
 402 #if INCLUDE_SERVICES // Heap dumping/inspection supported
 403 HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
 404                            DCmdWithParser(output, heap),
 405   _filename("filename","Name of the dump file", "STRING",true),
 406   _all("-all", "Dump all objects, including unreachable objects",
 407        "BOOLEAN", false, "false") {
 408   _dcmdparser.add_dcmd_option(&_all);
 409   _dcmdparser.add_dcmd_argument(&_filename);
 410 }
 411 
 412 void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
 413   // Request a full GC before heap dump if _all is false
 414   // This helps reduces the amount of unreachable objects in the dump
 415   // and makes it easier to browse.
 416   HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
 417   int res = dumper.dump(_filename.value());
 418   if (res == 0) {
 419     output()->print_cr("Heap dump file created");
 420   } else {
 421     // heap dump failed
 422     ResourceMark rm;
 423     char* error = dumper.error_as_C_string();
 424     if (error == NULL) {
 425       output()->print_cr("Dump failed - reason unknown");
 426     } else {
 427       output()->print_cr("%s", error);
 428     }
 429   }
 430 }
 431 
 432 int HeapDumpDCmd::num_arguments() {
 433   ResourceMark rm;
 434   HeapDumpDCmd* dcmd = new HeapDumpDCmd(NULL, false);
 435   if (dcmd != NULL) {
 436     DCmdMark mark(dcmd);
 437     return dcmd->_dcmdparser.num_arguments();
 438   } else {
 439     return 0;
 440   }
 441 }
 442 
 443 ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
 444                                        DCmdWithParser(output, heap),
 445   _all("-all", "Inspect all objects, including unreachable objects",
 446        "BOOLEAN", false, "false") {
 447   _dcmdparser.add_dcmd_option(&_all);
 448 }
 449 
 450 void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
 451   VM_GC_HeapInspection heapop(output(),
 452                               !_all.value() /* request full gc if false */);
 453   VMThread::execute(&heapop);
 454 }
 455 
 456 int ClassHistogramDCmd::num_arguments() {
 457   ResourceMark rm;
 458   ClassHistogramDCmd* dcmd = new ClassHistogramDCmd(NULL, false);
 459   if (dcmd != NULL) {
 460     DCmdMark mark(dcmd);
 461     return dcmd->_dcmdparser.num_arguments();
 462   } else {
 463     return 0;
 464   }
 465 }
 466 
 467 #define DEFAULT_COLUMNS "InstBytes,KlassBytes,CpAll,annotations,MethodCount,Bytecodes,MethodAll,ROAll,RWAll,Total"
 468 ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) :
 469                                        DCmdWithParser(output, heap),
 470   _csv("-csv", "Print in CSV (comma-separated values) format for spreadsheets",
 471        "BOOLEAN", false, "false"),
 472   _all("-all", "Show all columns",
 473        "BOOLEAN", false, "false"),
 474   _help("-help", "Show meaning of all the columns",
 475        "BOOLEAN", false, "false"),
 476   _columns("columns", "Comma-separated list of all the columns to show. "
 477            "If not specified, the following columns are shown: " DEFAULT_COLUMNS,
 478            "STRING", false) {
 479   _dcmdparser.add_dcmd_option(&_all);
 480   _dcmdparser.add_dcmd_option(&_csv);
 481   _dcmdparser.add_dcmd_option(&_help);
 482   _dcmdparser.add_dcmd_argument(&_columns);
 483 }
 484 
 485 void ClassStatsDCmd::execute(DCmdSource source, TRAPS) {
 486   if (!UnlockDiagnosticVMOptions) {
 487     output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions");
 488     return;
 489   }
 490 
 491   VM_GC_HeapInspection heapop(output(),
 492                               true /* request_full_gc */);
 493   heapop.set_csv_format(_csv.value());
 494   heapop.set_print_help(_help.value());
 495   heapop.set_print_class_stats(true);
 496   if (_all.value()) {
 497     if (_columns.has_value()) {
 498       output()->print_cr("Cannot specify -all and individual columns at the same time");
 499       return;
 500     } else {
 501       heapop.set_columns(NULL);
 502     }
 503   } else {
 504     if (_columns.has_value()) {
 505       heapop.set_columns(_columns.value());
 506     } else {
 507       heapop.set_columns(DEFAULT_COLUMNS);
 508     }
 509   }
 510   VMThread::execute(&heapop);
 511 }
 512 
 513 int ClassStatsDCmd::num_arguments() {
 514   ResourceMark rm;
 515   ClassStatsDCmd* dcmd = new ClassStatsDCmd(NULL, false);
 516   if (dcmd != NULL) {
 517     DCmdMark mark(dcmd);
 518     return dcmd->_dcmdparser.num_arguments();
 519   } else {
 520     return 0;
 521   }
 522 }
 523 #endif // INCLUDE_SERVICES
 524 
 525 ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) :
 526                                DCmdWithParser(output, heap),
 527   _locks("-l", "print java.util.concurrent locks", "BOOLEAN", false, "false") {
 528   _dcmdparser.add_dcmd_option(&_locks);
 529 }
 530 
 531 void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) {
 532   // thread stacks
 533   VM_PrintThreads op1(output(), _locks.value());
 534   VMThread::execute(&op1);
 535 
 536   // JNI global handles
 537   VM_PrintJNI op2(output());
 538   VMThread::execute(&op2);
 539 
 540   // Deadlock detection
 541   VM_FindDeadlocks op3(output());
 542   VMThread::execute(&op3);
 543 }
 544 
 545 int ThreadDumpDCmd::num_arguments() {
 546   ResourceMark rm;
 547   ThreadDumpDCmd* dcmd = new ThreadDumpDCmd(NULL, false);
 548   if (dcmd != NULL) {
 549     DCmdMark mark(dcmd);
 550     return dcmd->_dcmdparser.num_arguments();
 551   } else {
 552     return 0;
 553   }
 554 }
 555 
 556 // Enhanced JMX Agent support
 557 
 558 JMXStartRemoteDCmd::JMXStartRemoteDCmd(outputStream *output, bool heap_allocated) :
 559 
 560   DCmdWithParser(output, heap_allocated),
 561 
 562   _config_file
 563   ("config.file",
 564    "set com.sun.management.config.file", "STRING", false),
 565 
 566   _jmxremote_port
 567   ("jmxremote.port",
 568    "set com.sun.management.jmxremote.port", "STRING", false),
 569 
 570   _jmxremote_rmi_port
 571   ("jmxremote.rmi.port",
 572    "set com.sun.management.jmxremote.rmi.port", "STRING", false),
 573 
 574   _jmxremote_ssl
 575   ("jmxremote.ssl",
 576    "set com.sun.management.jmxremote.ssl", "STRING", false),
 577 
 578   _jmxremote_registry_ssl
 579   ("jmxremote.registry.ssl",
 580    "set com.sun.management.jmxremote.registry.ssl", "STRING", false),
 581 
 582   _jmxremote_authenticate
 583   ("jmxremote.authenticate",
 584    "set com.sun.management.jmxremote.authenticate", "STRING", false),
 585 
 586   _jmxremote_password_file
 587   ("jmxremote.password.file",
 588    "set com.sun.management.jmxremote.password.file", "STRING", false),
 589 
 590   _jmxremote_access_file
 591   ("jmxremote.access.file",
 592    "set com.sun.management.jmxremote.access.file", "STRING", false),
 593 
 594   _jmxremote_login_config
 595   ("jmxremote.login.config",
 596    "set com.sun.management.jmxremote.login.config", "STRING", false),
 597 
 598   _jmxremote_ssl_enabled_cipher_suites
 599   ("jmxremote.ssl.enabled.cipher.suites",
 600    "set com.sun.management.jmxremote.ssl.enabled.cipher.suite", "STRING", false),
 601 
 602   _jmxremote_ssl_enabled_protocols
 603   ("jmxremote.ssl.enabled.protocols",
 604    "set com.sun.management.jmxremote.ssl.enabled.protocols", "STRING", false),
 605 
 606   _jmxremote_ssl_need_client_auth
 607   ("jmxremote.ssl.need.client.auth",
 608    "set com.sun.management.jmxremote.need.client.auth", "STRING", false),
 609 
 610   _jmxremote_ssl_config_file
 611   ("jmxremote.ssl.config.file",
 612    "set com.sun.management.jmxremote.ssl_config_file", "STRING", false),
 613 
 614 // JDP Protocol support
 615   _jmxremote_autodiscovery
 616   ("jmxremote.autodiscovery",
 617    "set com.sun.management.jmxremote.autodiscovery", "STRING", false),
 618 
 619    _jdp_port
 620   ("jdp.port",
 621    "set com.sun.management.jdp.port", "INT", false),
 622 
 623    _jdp_address
 624   ("jdp.address",
 625    "set com.sun.management.jdp.address", "STRING", false),
 626 
 627    _jdp_source_addr
 628   ("jdp.source_addr",
 629    "set com.sun.management.jdp.source_addr", "STRING", false),
 630 
 631    _jdp_ttl
 632   ("jdp.ttl",
 633    "set com.sun.management.jdp.ttl", "INT", false),
 634 
 635    _jdp_pause
 636   ("jdp.pause",
 637    "set com.sun.management.jdp.pause", "INT", false),
 638 
 639    _jdp_name
 640   ("jdp.name",
 641    "set com.sun.management.jdp.name", "STRING", false)
 642 
 643   {
 644     _dcmdparser.add_dcmd_option(&_config_file);
 645     _dcmdparser.add_dcmd_option(&_jmxremote_port);
 646     _dcmdparser.add_dcmd_option(&_jmxremote_rmi_port);
 647     _dcmdparser.add_dcmd_option(&_jmxremote_ssl);
 648     _dcmdparser.add_dcmd_option(&_jmxremote_registry_ssl);
 649     _dcmdparser.add_dcmd_option(&_jmxremote_authenticate);
 650     _dcmdparser.add_dcmd_option(&_jmxremote_password_file);
 651     _dcmdparser.add_dcmd_option(&_jmxremote_access_file);
 652     _dcmdparser.add_dcmd_option(&_jmxremote_login_config);
 653     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_cipher_suites);
 654     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_enabled_protocols);
 655     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_need_client_auth);
 656     _dcmdparser.add_dcmd_option(&_jmxremote_ssl_config_file);
 657     _dcmdparser.add_dcmd_option(&_jmxremote_autodiscovery);
 658     _dcmdparser.add_dcmd_option(&_jdp_port);
 659     _dcmdparser.add_dcmd_option(&_jdp_address);
 660     _dcmdparser.add_dcmd_option(&_jdp_source_addr);
 661     _dcmdparser.add_dcmd_option(&_jdp_ttl);
 662     _dcmdparser.add_dcmd_option(&_jdp_pause);
 663     _dcmdparser.add_dcmd_option(&_jdp_name);
 664 }
 665 
 666 
 667 int JMXStartRemoteDCmd::num_arguments() {
 668   ResourceMark rm;
 669   JMXStartRemoteDCmd* dcmd = new JMXStartRemoteDCmd(NULL, false);
 670   if (dcmd != NULL) {
 671     DCmdMark mark(dcmd);
 672     return dcmd->_dcmdparser.num_arguments();
 673   } else {
 674     return 0;
 675   }
 676 }
 677 
 678 
 679 void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) {
 680     ResourceMark rm(THREAD);
 681     HandleMark hm(THREAD);
 682 
 683     // Load and initialize the sun.management.Agent class
 684     // invoke startRemoteManagementAgent(string) method to start
 685     // the remote management server.
 686     // throw java.lang.NoSuchMethodError if the method doesn't exist
 687 
 688     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 689     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 690     instanceKlassHandle ik (THREAD, k);
 691 
 692     JavaValue result(T_VOID);
 693 
 694     // Pass all command line arguments to java as key=value,...
 695     // All checks are done on java side
 696 
 697     int len = 0;
 698     stringStream options;
 699     char comma[2] = {0,0};
 700 
 701     // Leave default values on Agent.class side and pass only
 702     // agruments explicitly set by user. All arguments passed
 703     // to jcmd override properties with the same name set by
 704     // command line with -D or by managmenent.properties
 705     // file.
 706 #define PUT_OPTION(a) \
 707     do { \
 708         if ( (a).is_set() ){ \
 709             if ( *((a).type()) == 'I' ) { \
 710                 options.print("%scom.sun.management.%s=" JLONG_FORMAT, comma, (a).name(), (jlong)((a).value())); \
 711             } else { \
 712                 options.print("%scom.sun.management.%s=%s", comma, (a).name(), (char*)((a).value())); \
 713             } \
 714             comma[0] = ','; \
 715         }\
 716     } while(0);
 717 
 718 
 719     PUT_OPTION(_config_file);
 720     PUT_OPTION(_jmxremote_port);
 721     PUT_OPTION(_jmxremote_rmi_port);
 722     PUT_OPTION(_jmxremote_ssl);
 723     PUT_OPTION(_jmxremote_registry_ssl);
 724     PUT_OPTION(_jmxremote_authenticate);
 725     PUT_OPTION(_jmxremote_password_file);
 726     PUT_OPTION(_jmxremote_access_file);
 727     PUT_OPTION(_jmxremote_login_config);
 728     PUT_OPTION(_jmxremote_ssl_enabled_cipher_suites);
 729     PUT_OPTION(_jmxremote_ssl_enabled_protocols);
 730     PUT_OPTION(_jmxremote_ssl_need_client_auth);
 731     PUT_OPTION(_jmxremote_ssl_config_file);
 732     PUT_OPTION(_jmxremote_autodiscovery);
 733     PUT_OPTION(_jdp_port);
 734     PUT_OPTION(_jdp_address);
 735     PUT_OPTION(_jdp_source_addr);
 736     PUT_OPTION(_jdp_ttl);
 737     PUT_OPTION(_jdp_pause);
 738     PUT_OPTION(_jdp_name);
 739 
 740 #undef PUT_OPTION
 741 
 742     Handle str = java_lang_String::create_from_str(options.as_string(), CHECK);
 743     JavaCalls::call_static(&result, ik, vmSymbols::startRemoteAgent_name(), vmSymbols::string_void_signature(), str, CHECK);
 744 }
 745 
 746 JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) :
 747   DCmd(output, heap_allocated) {
 748   // do nothing
 749 }
 750 
 751 void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) {
 752     ResourceMark rm(THREAD);
 753     HandleMark hm(THREAD);
 754 
 755     // Load and initialize the sun.management.Agent class
 756     // invoke startLocalManagementAgent(void) method to start
 757     // the local management server
 758     // throw java.lang.NoSuchMethodError if method doesn't exist
 759 
 760     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 761     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 762     instanceKlassHandle ik (THREAD, k);
 763 
 764     JavaValue result(T_VOID);
 765     JavaCalls::call_static(&result, ik, vmSymbols::startLocalAgent_name(), vmSymbols::void_method_signature(), CHECK);
 766 }
 767 
 768 void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) {
 769     ResourceMark rm(THREAD);
 770     HandleMark hm(THREAD);
 771 
 772     // Load and initialize the sun.management.Agent class
 773     // invoke stopRemoteManagementAgent method to stop the
 774     // management server
 775     // throw java.lang.NoSuchMethodError if method doesn't exist
 776 
 777     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 778     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 779     instanceKlassHandle ik (THREAD, k);
 780 
 781     JavaValue result(T_VOID);
 782     JavaCalls::call_static(&result, ik, vmSymbols::stopRemoteAgent_name(), vmSymbols::void_method_signature(), CHECK);
 783 }
 784 
 785 JMXStatusDCmd::JMXStatusDCmd(outputStream *output, bool heap_allocated) :
 786   DCmd(output, heap_allocated) {
 787   // do nothing
 788 }
 789 
 790 void JMXStatusDCmd::execute(DCmdSource source, TRAPS) {
 791   ResourceMark rm(THREAD);
 792   HandleMark hm(THREAD);
 793 
 794   // Load and initialize the sun.management.Agent class
 795   // invoke getManagementAgentStatus() method to generate the status info
 796   // throw java.lang.NoSuchMethodError if method doesn't exist
 797 
 798   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 799   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(), loader, Handle(), true, CHECK);
 800   instanceKlassHandle ik (THREAD, k);
 801 
 802   JavaValue result(T_OBJECT);
 803   JavaCalls::call_static(&result, ik, vmSymbols::getAgentStatus_name(), vmSymbols::void_string_signature(), CHECK);
 804 
 805   jvalue* jv = (jvalue*) result.get_value_addr();
 806   oop str = (oop) jv->l;
 807   if (str != NULL) {
 808       char* out = java_lang_String::as_utf8_string(str);
 809       if (out) {
 810           output()->print_cr("%s", out);
 811           return;
 812       }
 813   }
 814   output()->print_cr("Error obtaining management agent status");
 815 }
 816 
 817 VMDynamicLibrariesDCmd::VMDynamicLibrariesDCmd(outputStream *output, bool heap_allocated) :
 818   DCmd(output, heap_allocated) {
 819   // do nothing
 820 }
 821 
 822 void VMDynamicLibrariesDCmd::execute(DCmdSource source, TRAPS) {
 823   os::print_dll_info(output());
 824   output()->cr();









 825 }
 826 
 827 void CompileQueueDCmd::execute(DCmdSource source, TRAPS) {
 828   VM_PrintCompileQueue printCompileQueueOp(output());
 829   VMThread::execute(&printCompileQueueOp);
 830 }
 831 
 832 void CodeListDCmd::execute(DCmdSource source, TRAPS) {
 833   VM_PrintCodeList printCodeListOp(output());
 834   VMThread::execute(&printCodeListOp);
 835 }
 836 
 837 void CodeCacheDCmd::execute(DCmdSource source, TRAPS) {
 838   VM_PrintCodeCache printCodeCacheOp(output());
 839   VMThread::execute(&printCodeCacheOp);
 840 }
 841 
 842 void CompilerDirectivesPrintDCmd::execute(DCmdSource source, TRAPS) {
 843   DirectivesStack::print(output());
 844 }
 845 
 846 CompilerDirectivesAddDCmd::CompilerDirectivesAddDCmd(outputStream* output, bool heap) :
 847                            DCmdWithParser(output, heap),
 848   _filename("filename","Name of the directives file", "STRING",true) {
 849   _dcmdparser.add_dcmd_argument(&_filename);
 850 }
 851 
 852 void CompilerDirectivesAddDCmd::execute(DCmdSource source, TRAPS) {
 853   DirectivesParser::parse_from_file(_filename.value(), output());
 854 }
 855 
 856 int CompilerDirectivesAddDCmd::num_arguments() {
 857   ResourceMark rm;
 858   CompilerDirectivesAddDCmd* dcmd = new CompilerDirectivesAddDCmd(NULL, false);
 859   if (dcmd != NULL) {
 860     DCmdMark mark(dcmd);
 861     return dcmd->_dcmdparser.num_arguments();
 862   } else {
 863     return 0;
 864   }
 865 }
 866 
 867 void CompilerDirectivesRemoveDCmd::execute(DCmdSource source, TRAPS) {
 868   DirectivesStack::pop();
 869 }
 870 
 871 void CompilerDirectivesClearDCmd::execute(DCmdSource source, TRAPS) {
 872   DirectivesStack::clear();
 873 }
 874 #if INCLUDE_SERVICES
 875 ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) :
 876                                        DCmdWithParser(output, heap),
 877   _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"),
 878   _print_subclasses("-s", "If a classname is specified, print its subclasses. "
 879                     "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"),
 880   _classname("classname", "Name of class whose hierarchy should be printed. "
 881              "If not specified, all class hierarchies are printed.",
 882              "STRING", false) {
 883   _dcmdparser.add_dcmd_option(&_print_interfaces);
 884   _dcmdparser.add_dcmd_option(&_print_subclasses);
 885   _dcmdparser.add_dcmd_argument(&_classname);
 886 }
 887 
 888 void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) {
 889   VM_PrintClassHierarchy printClassHierarchyOp(output(), _print_interfaces.value(),
 890                                                _print_subclasses.value(), _classname.value());
 891   VMThread::execute(&printClassHierarchyOp);
 892 }
 893 
 894 int ClassHierarchyDCmd::num_arguments() {
 895   ResourceMark rm;
 896   ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false);
 897   if (dcmd != NULL) {
 898     DCmdMark mark(dcmd);
 899     return dcmd->_dcmdparser.num_arguments();
 900   } else {
 901     return 0;
 902   }
 903 }
 904 
 905 #endif
 906 
 907 class VM_DumpTouchedMethods : public VM_Operation {
 908 private:
 909   outputStream* _out;
 910 public:
 911   VM_DumpTouchedMethods(outputStream* out) {
 912     _out = out;
 913   }
 914 
 915   virtual VMOp_Type type() const { return VMOp_DumpTouchedMethods; }
 916 
 917   virtual void doit() {
 918     Method::print_touched_methods(_out);
 919   }
 920 };
 921 
 922 TouchedMethodsDCmd::TouchedMethodsDCmd(outputStream* output, bool heap) :
 923                                        DCmdWithParser(output, heap)
 924 {}
 925 
 926 void TouchedMethodsDCmd::execute(DCmdSource source, TRAPS) {
 927   if (!UnlockDiagnosticVMOptions) {
 928     output()->print_cr("VM.touched_methods command requires -XX:+UnlockDiagnosticVMOptions");
 929     return;
 930   }
 931   VM_DumpTouchedMethods dumper(output());
 932   VMThread::execute(&dumper);
 933 }
 934 
 935 int TouchedMethodsDCmd::num_arguments() {
 936   return 0;
 937 }
--- EOF ---