1 /*
   2  * Copyright (c) 2011, 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 "services/diagnosticArgument.hpp"
  27 #include "services/diagnosticFramework.hpp"
  28 #include "services/diagnosticCommand.hpp"
  29 
  30 HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmd(output, heap),
  31 _all("-all", "Show help for all commands", "BOOLEAN", false, "false"),
  32 _cmd("command name", "The name of the command for which we want help", "STRING", false) {
  33   _dcmdparser.add_dcmd_option(&_all);
  34   _dcmdparser.add_dcmd_argument(&_cmd);
  35 };
  36 
  37 void HelpDCmd::parse(CmdLine* line, char delim, TRAPS) {
  38   _dcmdparser.parse(line, delim, CHECK);
  39 }
  40 
  41 void HelpDCmd::print_help(outputStream* out) {
  42   _dcmdparser.print_help(out, get_name());
  43 }
  44 
  45 void HelpDCmd::execute(TRAPS) {
  46   if (_all.get_value()) {
  47     GrowableArray<const char*>* cmd_list = DCmdFactory::get_DCmd_list();
  48     for (int i = 0; i < cmd_list->length(); i++) {
  49       DCmdFactory* factory = DCmdFactory::get_factory(cmd_list->at(i), strlen(cmd_list->at(i)));
  50       if (!factory->is_hidden()) {
  51         get_output()->print_cr("%s%s", factory->get_name(),
  52                 factory->is_enabled() ? "" : " [disabled]");
  53         get_output()->print_cr("\t%s", factory->get_description());
  54         get_output()->cr();
  55       }
  56       factory = factory->_next;
  57     }
  58   } else if (_cmd.has_value()) {
  59     DCmd* cmd = NULL;
  60     DCmdFactory* factory = DCmdFactory::get_factory(_cmd.get_value(), strlen(_cmd.get_value()));
  61     if (factory != NULL) {
  62       get_output()->print_cr("%s%s", factory->get_name(),
  63               factory->is_enabled() ? "" : " [disabled]");
  64       get_output()->print_cr(factory->get_description());
  65       get_output()->print_cr("\nImpact: %s", factory->get_impact());
  66       cmd = factory->get_cheap_instance(get_output());
  67       if (cmd != NULL) {
  68         DCmdMark mark(cmd);
  69         cmd->print_help(get_output());
  70       }
  71     } else {
  72       get_output()->print_cr("Help unavailable : No such command");
  73     }
  74   } else {
  75     get_output()->print_cr("The following commands are available:");
  76     GrowableArray<const char *>* cmd_list = DCmdFactory::get_DCmd_list();
  77     for (int i = 0; i < cmd_list->length(); i++) {
  78       DCmdFactory* factory = DCmdFactory::get_factory(cmd_list->at(i), strlen(cmd_list->at(i)));
  79       if (!factory->is_hidden()) {
  80         get_output()->print_cr("%s%s", factory->get_name(),
  81                 factory->is_enabled() ? "" : " [disabled]");
  82       }
  83       factory = factory->_next;
  84     }
  85     get_output()->print_cr("\nFor more information about a specific command use 'help <command>'.");
  86   }
  87 }
  88 
  89 void HelpDCmd::reset(TRAPS) {
  90   _dcmdparser.reset(CHECK);
  91 }
  92 
  93 void HelpDCmd::cleanup() {
  94   _dcmdparser.cleanup();
  95 }
  96 
  97 int HelpDCmd::get_num_arguments() {
  98     ResourceMark rm;
  99     HelpDCmd* dcmd = new HelpDCmd(NULL);
 100     return dcmd->_dcmdparser.get_num_arguments();
 101 }
 102 
 103 GrowableArray<const char*>* HelpDCmd::get_argument_name_array() {
 104   return _dcmdparser.get_argument_name_array();
 105 }
 106 
 107 GrowableArray<DCmdArgumentInfo*>* HelpDCmd::get_argument_info_array() {
 108   return _dcmdparser.get_argument_info_array();
 109 }
 110 
 111 void VersionDCmd::execute(TRAPS) {
 112   get_output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(),
 113           Abstract_VM_Version::vm_release());
 114   JDK_Version jdk_version = JDK_Version::current();
 115   if (jdk_version.update_version() > 0) {
 116     get_output()->print_cr("JDK %d.%d_%02d", jdk_version.major_version(),
 117             jdk_version.minor_version(), jdk_version.update_version());
 118   } else {
 119     get_output()->print_cr("JDK %d.%d", jdk_version.major_version(),
 120             jdk_version.minor_version());
 121   }
 122 }