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 #ifndef SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
  26 #define SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP
  27 
  28 #include "classfile/vmSymbols.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/vm_version.hpp"
  33 #include "runtime/vmThread.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 
  37 // CmdLine is the class used to handle a command line containing a single
  38 // diagnostic command and its arguments. It provides method to access the
  39 // command name and the beginning of the arguments. The class is also
  40 // able to identify commented command lines and the "stop" keywor
  41 class CmdLine : public StackObj {
  42 private:
  43   const char* _cmd;
  44   size_t _cmd_len;
  45   const char* _args;
  46   size_t _args_len;
  47 public:
  48   CmdLine(const char* line,size_t len, bool no_command_name=false);
  49   const char* args_addr() const { return _args; }
  50   size_t args_len() const { return _args_len; }
  51   const char* cmd_addr() const { return _cmd; }
  52   size_t cmd_len() const { return _cmd_len; }
  53   bool is_empty() { return _cmd_len == 0; }
  54   bool is_executable() { return is_empty() || _cmd[0] != '#'; }
  55   bool is_stop() { return !is_empty() && !strncmp("stop",_cmd,_cmd_len); }
  56 };
  57 
  58 // Iterator class taking a character string in input and returning a CmdLine
  59 // instance for each command line. The argument delimiter has to be specified.
  60 class DCmdIter : public StackObj {
  61   friend class DCmd;
  62 private:
  63     const char* _str;
  64     char _delim;
  65     size_t _len;
  66     size_t _cursor;
  67 public:
  68 
  69   DCmdIter(const char* str, char delim) {
  70     _str = str;
  71     _delim = delim;
  72     _len = strlen(str);
  73     _cursor = 0;
  74   }
  75   bool has_next() { return _cursor < _len; }
  76   CmdLine get_next() {
  77     assert(_cursor <= _len, "Cannot iterate more");
  78     size_t n = _cursor;
  79     while (n < _len && _str[n] != _delim) n++;
  80     CmdLine line(&(_str[_cursor]), n - _cursor);
  81     _cursor = n + 1;
  82     return line;
  83   }
  84 };
  85 
  86 // Iterator class to iterate over diagnostic command arguments
  87 class DCmdArgIter : public ResourceObj {
  88   const char* _buffer;
  89   size_t _len;
  90   size_t _cursor;
  91   const char* _key_addr;
  92   size_t _key_len;
  93   const char* _value_addr;
  94   size_t _value_len;
  95   char _delim;
  96 public:
  97   DCmdArgIter(const char* buf, size_t len, char delim) {
  98     _buffer = buf;
  99     _len = len;
 100     _delim = delim;
 101     _cursor = 0;
 102   }
 103   bool get_next(TRAPS);
 104   const char* get_key_addr() { return _key_addr; }
 105   size_t get_key_length() { return _key_len; }
 106   const char* get_value_addr() { return _value_addr; }
 107   size_t get_value_length() { return _value_len; }
 108 };
 109 
 110 // A DCmdInfo instance provides a description of a diagnostic command. It is
 111 // used to export the description to the JMX interface of the framework.
 112 class DCmdInfo : public ResourceObj {
 113 protected:
 114   const char* _name;
 115   const char* _description;
 116   const char* _impact;
 117   int _num_arguments;
 118   bool _enabled;
 119 public:
 120   DCmdInfo(const char* name,
 121           const char* description,
 122           const char* impact,
 123           int num_arguments,
 124           bool enabled) {
 125     this->_name = name;
 126     this->_description = description;
 127     this->_impact = impact;
 128     this->_num_arguments = num_arguments;
 129     this->_enabled = enabled;
 130   }
 131   const char* get_name() const { return _name; }
 132   const char* get_description() const { return _description; }
 133   const char* get_impact() const { return _impact; }
 134   int get_num_arguments() const { return _num_arguments; }
 135   bool is_enabled() const { return _enabled; }
 136 
 137   static bool by_name(void* name, DCmdInfo* info);
 138 };
 139 
 140 // A DCmdArgumentInfo instance provides a description of a diagnostic command
 141 // argument. It is used to export the description to the JMX interface of the
 142 // framework.
 143 class DCmdArgumentInfo : public ResourceObj {
 144 protected:
 145   const char* _name;
 146   const char* _description;
 147   const char* _type;
 148   const char* _default;
 149   bool _mandatory;
 150   bool _option;
 151   int _position;
 152 public:
 153   DCmdArgumentInfo(const char* name, const char* description, const char* type,
 154     const char* default_string, bool mandatory, bool option, int position = -1) {
 155     this->_name =name;
 156     this->_description = description;
 157     this->_type = type;
 158     this->_default = default_string;
 159     this->_option = option;
 160     this->_mandatory = mandatory;
 161     this->_option = option;
 162     this->_position = position;
 163   }
 164   const char* get_name() const { return _name; }
 165   const char* get_description() const { return _description; }
 166   const char* get_type() const { return _type; }
 167   const char* get_default() const { return _default; }
 168   bool is_mandatory() const { return _mandatory; }
 169   bool is_option() const { return _option; }
 170   int get_position() const { return _position; }
 171 };
 172 
 173 // The DCmdParser class can be used to create an argument parser for a
 174 // diagnostic command. It is not mandatory to use it to parse arguments.
 175 class DCmdParser {
 176 private:
 177   GenDCmdArgument* _options;
 178   GenDCmdArgument* _arguments;
 179   char _delim;
 180 public:
 181   DCmdParser() {
 182     _options = NULL;
 183     _arguments = NULL;
 184   }
 185   void add_dcmd_option(GenDCmdArgument* arg);
 186   void add_dcmd_argument(GenDCmdArgument* arg);
 187   GenDCmdArgument* lookup_dcmd_option(const char* name, size_t len);
 188   GenDCmdArgument* get_dcmd_arguments_list() { return _arguments; };
 189   void check(TRAPS);
 190   void parse(CmdLine* line, char delim, TRAPS);
 191   void print_help(outputStream* out, const char* cmd_name);
 192   void reset(TRAPS);
 193   void cleanup();
 194   int get_num_arguments();
 195   GrowableArray<const char*>* get_argument_name_array();
 196   GrowableArray<DCmdArgumentInfo*>* get_argument_info_array();
 197 };
 198 
 199 // The DCmd class is the parent class of all diagnostic commands
 200 // Diagnostic command instances should not be instanciated directly but
 201 // created using the associated factory. The factory can be retrieved with
 202 // the DCmdFactory::getFactory() method.
 203 // A diagnostic command instance can either be allocated in the resource Area
 204 // or in the C-heap. Allocation in the resource area is recommended when the
 205 // current thread is the only one which will access the diagnostic command
 206 // instance. Allocation in the C-heap is required when the diagnostic command
 207 // is accessed by several threads (for instance to perform asynchronous
 208 // execution).
 209 // To ensure a proper cleanup, it's highly recommended to use a DCmdMark for
 210 // each diagnostic command instance. In case of a C-heap allocated diagnostic
 211 // command instance, the DCmdMark must be created in the context of the last
 212 // thread that will access the instance.
 213 class DCmd : public ResourceObj {
 214 protected:
 215   outputStream* _output;
 216   bool _heap_allocated;
 217 public:
 218   DCmd(outputStream* output, bool heap=false) {
 219     _output = output;
 220     _heap_allocated = heap;
 221   }
 222 
 223   static const char* get_name() { return "No Name";}
 224   static const char* get_description() { return "No Help";}
 225   static const char* get_disabled_message() { return "Diagnostic command currently disabled"; }
 226   static const char* get_impact() { return "Low: No impact"; }
 227   static int get_num_arguments() { return 0; }
 228   outputStream* get_output() { return _output; }
 229   bool is_heap_allocated()  { return _heap_allocated; }
 230   virtual void print_help(outputStream* out) { };
 231   virtual void parse(CmdLine* line, char delim, TRAPS) { }
 232   virtual void execute(TRAPS) { }
 233   virtual void reset(TRAPS) { }
 234   virtual void cleanup() { }
 235 
 236   // support for the JMX interface
 237   virtual GrowableArray<const char*>* get_argument_name_array() {
 238     GrowableArray<const char*>* array = new GrowableArray<const char*>(0);
 239     return array;
 240   }
 241   virtual GrowableArray<DCmdArgumentInfo*>* get_argument_info_array() {
 242     GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo*>(0);
 243     return array;
 244   }
 245 
 246   // main method to invoke the framework
 247   static void parse_and_execute(outputStream* out, const char* cmdline,
 248             char delim, TRAPS);
 249 };
 250 
 251 class DCmdMark : public StackObj {
 252   DCmd* _ref;
 253 public:
 254   DCmdMark(DCmd* cmd) { _ref = cmd; }
 255   ~DCmdMark() {
 256     if(_ref != NULL) {
 257       _ref->cleanup();
 258       if(_ref->is_heap_allocated()) {
 259         delete _ref;
 260       }
 261     }
 262   }
 263 };
 264 
 265 // Diagnostic commands are not directly instanciated but created with a factory.
 266 // Each diagnostic command class has its own factory. The DCmdFactory class also
 267 // manages the status of the diagnostic command (hidden, enabled). A DCmdFactory
 268 // has to be registered to make the diagnostic command available (see
 269 // management.cpp)
 270 class DCmdFactory: public CHeapObj {
 271 private:
 272   static Mutex*   _dcmdFactory_lock;
 273   DCmdFactory* _next;
 274   // When disabled, a diagnostic command cannot be executed. Any attempt to
 275   // execute it will result in the printing of the disabled message without
 276   // instantiating the command.
 277   bool _enabled;
 278   // When hidden, a diagnostic command doesn't appear in the list of commands
 279   // provided by the 'help' command.
 280   bool _hidden;
 281   int _num_arguments;
 282   static DCmdFactory* _DCmdFactoryList;
 283 public:
 284   DCmdFactory(int num_arguments, bool enabled = true, bool hidden = false) {
 285     _next = NULL;
 286     _enabled = enabled;
 287     _hidden = hidden;
 288     _num_arguments = num_arguments;
 289   }
 290   bool is_enabled() const { return _enabled; }
 291   void set_enabled(bool b) { _enabled = b; }
 292   bool is_hidden() const { return _hidden; }
 293   void set_hidden(bool b) { _hidden = b; }
 294   int get_num_arguments() { return _num_arguments; }
 295   DCmdFactory* get_next() { return _next; }
 296   virtual DCmd* get_cheap_instance(outputStream* output) = 0;
 297   virtual DCmd* get_resource_instance(outputStream* output) = 0;
 298   virtual const char* get_name() const = 0;
 299   virtual const char* get_description() const = 0;
 300   virtual const char* get_impact() const = 0;
 301   virtual const char* get_disabled_message() const = 0;
 302   static int register_DCmdFactory(DCmdFactory* factory);
 303   static DCmdFactory* get_factory(const char* cmd, size_t len);
 304   // Returns a C-heap allocated diagnostic command for the given command line
 305   static DCmd* create_global_DCmd(CmdLine &line, outputStream* out, TRAPS);
 306   // Returns a resourceArea allocated diagnostic command for the given command line
 307   static DCmd* create_local_DCmd(CmdLine &line, outputStream* out, TRAPS);
 308   static GrowableArray<const char*>* get_DCmd_list();
 309   static GrowableArray<DCmdInfo*>* get_DCmdInfo_list();
 310 
 311   friend class HelpDCmd;
 312 };
 313 
 314 // Template to easily create DCmdFactory instances. See management.cpp
 315 // where this template is used to create and register factories.
 316 template <class DCmdClass> class DCmdFactoryImpl : public DCmdFactory {
 317 public:
 318   DCmdFactoryImpl(bool enabled=true, bool hidden=false) :
 319     DCmdFactory(DCmdClass::get_num_arguments(), enabled, hidden) { }
 320   // Returns a C-heap allocated instance
 321   virtual DCmd* get_cheap_instance(outputStream* output) {
 322     return new (ResourceObj::C_HEAP) DCmdClass(output,true);
 323   }
 324   // Returns a resourceArea allocated instance
 325   virtual DCmd* get_resource_instance(outputStream* output) {
 326     return new DCmdClass(output);
 327   }
 328   virtual const char* get_name() const {
 329     return DCmdClass::get_name();
 330   }
 331   virtual const char* get_description() const {
 332     return DCmdClass::get_description();
 333   }
 334   virtual const char* get_impact() const {
 335     return DCmdClass::get_impact();
 336   }
 337   virtual const char* get_disabled_message() const {
 338      return DCmdClass::get_disabled_message();
 339   }
 340 };
 341 
 342 #endif // SHARE_VM_SERVICES_DIAGNOSTICFRAMEWORK_HPP