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 "memory/oopFactory.hpp"
  27 #include "runtime/javaCalls.hpp"
  28 #include "runtime/mutexLocker.hpp"
  29 #include "services/diagnosticArgument.hpp"
  30 #include "services/diagnosticFramework.hpp"
  31 #include "services/management.hpp"
  32 
  33 CmdLine::CmdLine(const char* line, size_t len, bool no_command_name) {
  34     assert(line != NULL, "Command line string should not be NULL");
  35     const char* line_end;
  36     const char* cmd_end;
  37 
  38     _cmd = line;
  39     line_end = &line[len];
  40 
  41     // Skip whitespace in the beginning of the line.
  42     while (_cmd < line_end && isspace((int) _cmd[0])) {
  43       _cmd++;
  44     }
  45     cmd_end = _cmd;
  46 
  47     if(no_command_name) {
  48       _cmd = NULL;
  49       _cmd_len = 0;
  50     } else {
  51       // Look for end of the command name
  52       while (cmd_end < line_end && !isspace((int) cmd_end[0])) {
  53         cmd_end++;
  54       }
  55       _cmd_len = cmd_end - line;
  56     }
  57     _args = cmd_end;
  58     _args_len = len - _cmd_len;
  59 }
  60 
  61 bool DCmdArgIter::get_next(TRAPS) {
  62   if(_len == 0) return false;
  63   // skipping spaces
  64   while (_cursor < _len - 1 && isspace(_buffer[_cursor])) {
  65     _cursor++;
  66   }
  67   // handling end of command line
  68   if (_cursor >= _len - 1) {
  69     _cursor = _len - 1;
  70     _key_addr = &_buffer[_len - 1];
  71     _key_len = 0;
  72     _value_addr = &_buffer[_len - 1];
  73     _value_len = 0;
  74     return false;
  75   }
  76   // extracting first item, argument or option name
  77   _key_addr = &_buffer[_cursor];
  78   while (_cursor <= _len - 1 && _buffer[_cursor] != '=' && _buffer[_cursor] != _delim) {
  79     // argument can be surrounded by simple or double quotes
  80     if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
  81       _key_addr++;
  82       char quote = _buffer[_cursor];
  83       while (_cursor < _len - 1) {
  84         _cursor++;
  85         if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
  86           break;
  87         }
  88       }
  89       if (_buffer[_cursor] != quote) {
  90         THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
  91                 "Format error in diagnostic command arguments", false);
  92       }
  93       break;
  94     }
  95     _cursor++;
  96   }
  97   _key_len = &_buffer[_cursor] - _key_addr;
  98   // check if the argument has the <key>=<value> format
  99   if (_cursor <= _len -1 && _buffer[_cursor] == '=') {
 100     _cursor++;
 101     _value_addr = &_buffer[_cursor];
 102     // extract the value
 103     while (_cursor <= _len - 1 && _buffer[_cursor] != _delim) {
 104       // value can be surrounded by simple or double quotes
 105       if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
 106         _value_addr++;
 107         char quote = _buffer[_cursor];
 108         while (_cursor < _len - 1) {
 109           _cursor++;
 110           if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
 111             break;
 112           }
 113         }
 114         if (_buffer[_cursor] != quote) {
 115           THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 116                   "Format error in diagnostic command arguments", false);
 117         }
 118         break;
 119       }
 120       _cursor++;
 121     }
 122     _value_len = &_buffer[_cursor] - _value_addr;
 123   } else {
 124     _value_addr = NULL;
 125     _value_len = 0;
 126   }
 127   return _key_len != 0;
 128 }
 129 
 130 bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) {
 131     if(info == NULL)
 132         return false;
 133 
 134     return strcmp((const char*)cmd_name, info->get_name()) == 0;
 135 }
 136 
 137 void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) {
 138   assert(arg != NULL, "Sanity");
 139   if (_options == NULL) {
 140     _options = arg;
 141   } else {
 142     GenDCmdArgument* o = _options;
 143     while (o->get_next() != NULL) {
 144       o = o->get_next();
 145     }
 146     o->set_next(arg);
 147   }
 148   arg->set_next(NULL);
 149   Thread* THREAD = Thread::current();
 150   arg->init_value(THREAD);
 151   if (HAS_PENDING_EXCEPTION) {
 152     fatal("Initialization must be successful");
 153   }
 154 }
 155 
 156 void DCmdParser::add_dcmd_argument(GenDCmdArgument* arg) {
 157   assert(arg != NULL, "Sanity");
 158   if (_arguments == NULL) {
 159     _arguments = arg;
 160   } else {
 161     GenDCmdArgument* a = _arguments;
 162     while (a->get_next() != NULL) {
 163       a = a->get_next();
 164     }
 165     a->set_next(arg);
 166   }
 167   arg->set_next(NULL);
 168   Thread* THREAD = Thread::current();
 169   arg->init_value(THREAD);
 170   if (HAS_PENDING_EXCEPTION) {
 171     fatal("Initialization must be successful");
 172   }
 173 }
 174 
 175 void DCmdParser::parse(CmdLine* line, char delim, TRAPS) {
 176   GenDCmdArgument* next_argument = _arguments;
 177   DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
 178   bool cont = iter.get_next(CHECK);
 179   while(cont) {
 180     GenDCmdArgument* arg = lookup_dcmd_option(iter.get_key_addr(),
 181             iter.get_key_length());
 182     if(arg != NULL) {
 183       arg->read_value(iter.get_value_addr(), iter.get_value_length(), CHECK);
 184     } else {
 185       if(next_argument != NULL) {
 186         arg = next_argument;
 187         arg->read_value(iter.get_key_addr(), iter.get_key_length(), CHECK);
 188         next_argument = next_argument->get_next();
 189       } else {
 190         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 191           "Unknown argument in diagnostic command");
 192       }
 193     }
 194     cont = iter.get_next(CHECK);
 195   }
 196   check(CHECK);
 197 }
 198 
 199 GenDCmdArgument* DCmdParser::lookup_dcmd_option(const char* name, size_t len) {
 200   GenDCmdArgument* arg = _options;
 201   while (arg != NULL) {
 202     if (strlen(arg->get_name()) == len &&
 203       strncmp(name, arg->get_name(), len) == 0) {
 204       return arg;
 205     }
 206     arg = arg->get_next();
 207   }
 208   return NULL;
 209 }
 210 
 211 void DCmdParser::check(TRAPS) {
 212   GenDCmdArgument* arg = _arguments;
 213   while (arg != NULL) {
 214     if (arg->is_mandatory() && !arg->has_value()) {
 215       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 216               "Missing argument for diagnostic command");
 217     }
 218     arg = arg->get_next();
 219   }
 220   arg = _options;
 221   while (arg != NULL) {
 222     if (arg->is_mandatory() && !arg->has_value()) {
 223       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 224               "Missing option for diagnostic command");
 225     }
 226     arg = arg->get_next();
 227   }
 228 }
 229 
 230 void DCmdParser::print_help(outputStream* out, const char* cmd_name) {
 231   out->print("\nSyntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
 232   GenDCmdArgument* arg = _arguments;
 233   while(arg != NULL) {
 234     if(arg->is_mandatory()) {
 235       out->print(" <%s>",arg->get_name());
 236     } else {
 237       out->print(" [<%s>]",arg->get_name());
 238     }
 239     arg = arg->get_next();
 240   }
 241   out->print_cr("");
 242   if(_arguments != NULL) {
 243     out->print_cr("\nArguments:");
 244     arg = _arguments;
 245     while (arg != NULL) {
 246       out->print("\t%s : %s %s (%s, ", arg->get_name(),
 247               arg->is_mandatory() ? "" : "[optional]",
 248               arg->get_description(), arg->get_type());
 249       if (arg->has_default()) {
 250         out->print(arg->get_default_as_string());
 251       } else {
 252         out->print("no default value");
 253       }
 254       out->print_cr(")");
 255       arg = arg->get_next();
 256     }
 257   }
 258   if (_options != NULL) {
 259     out->print_cr("\nOptions: (options must be specified using the <key> or <key>=<value> syntax)");
 260     arg = _options;
 261     while (arg != NULL) {
 262       out->print("\t%s : %s %s (%s, ", arg->get_name(),
 263               arg->is_mandatory() ? "" : "[optional]",
 264               arg->get_description(), arg->get_type());
 265       if (arg->has_default()) {
 266         out->print(arg->get_default_as_string());
 267       } else {
 268         out->print("no default value");
 269       }
 270       out->print_cr(")");
 271       arg = arg->get_next();
 272     }
 273   }
 274 }
 275 
 276 void DCmdParser::reset(TRAPS) {
 277   GenDCmdArgument* arg = _arguments;
 278   while(arg != NULL) {
 279     arg->reset(CHECK);
 280     arg = arg->get_next();
 281   }
 282   arg = _options;
 283   while(arg != NULL) {
 284     arg->reset(CHECK);
 285     arg = arg->get_next();
 286   }
 287 }
 288 
 289 void DCmdParser::cleanup() {
 290   GenDCmdArgument* arg = _arguments;
 291   while(arg != NULL) {
 292     arg->cleanup();
 293     arg = arg->get_next();
 294   }
 295   arg = _options;
 296   while(arg != NULL) {
 297     arg->cleanup();
 298     arg = arg->get_next();
 299   }
 300 }
 301 
 302 int DCmdParser::get_num_arguments() {
 303   GenDCmdArgument* arg = _arguments;
 304   int count = 0;
 305   while(arg != NULL) {
 306     count++;
 307     arg = arg->get_next();
 308   }
 309   arg = _options;
 310   while(arg != NULL) {
 311     count++;
 312     arg = arg->get_next();
 313   }
 314   return count;
 315 }
 316 
 317 GrowableArray<const char *>* DCmdParser::get_argument_name_array() {
 318   int count = get_num_arguments();
 319   GrowableArray<const char *>* array = new GrowableArray<const char *>(count);
 320   int idx = 0;
 321   GenDCmdArgument* arg = _arguments;
 322   while(arg != NULL) {
 323     array->append(arg->get_name());
 324     idx++;
 325     arg = arg->get_next();
 326   }
 327   arg = _options;
 328   while(arg != NULL) {
 329     array->append(arg->get_name());
 330     idx++;
 331     arg = arg->get_next();
 332   }
 333   return array;
 334 }
 335 
 336 GrowableArray<DCmdArgumentInfo*>* DCmdParser::get_argument_info_array() {
 337   int count = get_num_arguments();
 338   GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo *>(count);
 339   int idx = 0;
 340   GenDCmdArgument* arg = _arguments;
 341   while(arg != NULL) {
 342     array->append(new DCmdArgumentInfo(arg->get_name(), arg->get_description(),
 343             arg->get_type(), arg->get_default_as_string(), arg->is_mandatory(),
 344             false, idx));
 345     idx++;
 346     arg = arg->get_next();
 347   }
 348   arg = _options;
 349   while(arg != NULL) {
 350     array->append(new DCmdArgumentInfo(arg->get_name(), arg->get_description(),
 351             arg->get_type(), arg->get_default_as_string(), arg->is_mandatory(),
 352             true));
 353     idx++;
 354     arg = arg->get_next();
 355   }
 356   return array;
 357 }
 358 
 359 DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL;
 360 
 361 void DCmd::parse_and_execute(outputStream* out, const char* cmdline,
 362         char delim, TRAPS) {
 363 
 364   if (cmdline == NULL) return; // Nothing to do!
 365   DCmdIter iter(cmdline, '\n');
 366 
 367   while (iter.has_next()) {
 368     CmdLine line = iter.get_next();
 369     if(line.is_stop()) {
 370       break;
 371     }
 372     if (line.is_executable()) {
 373       DCmd* command = DCmdFactory::create_local_DCmd(line,out, CHECK);
 374       assert(command != NULL,"command error must be handled before this line");
 375       DCmdMark mark(command);
 376       command->parse(&line, delim, CHECK);
 377       command->execute(CHECK);
 378     }
 379   }
 380 }
 381 
 382 Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf,"DCmdFactory",true);
 383 
 384 DCmdFactory* DCmdFactory::get_factory(const char* name, size_t len) {
 385   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 386   DCmdFactory* factory = _DCmdFactoryList;
 387   while (factory != NULL) {
 388     if (strlen(factory->get_name()) == len &&
 389             strncmp(name,factory->get_name(), len) == 0) {
 390       return factory;
 391     }
 392     factory = factory->_next;
 393   }
 394   return NULL;
 395 }
 396 
 397 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) {
 398   MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 399   factory->_next = _DCmdFactoryList;
 400   _DCmdFactoryList = factory;
 401   return 0; // Actually, there's no checks for duplicates
 402 }
 403 
 404 DCmd* DCmdFactory::create_global_DCmd(CmdLine &line, outputStream* out, TRAPS) {
 405   {
 406     DCmdFactory* factory = get_factory(line.cmd_addr(),line.cmd_len());
 407     if(factory != NULL) {
 408       if(!factory->is_enabled()) {
 409         THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 410              factory->get_disabled_message());
 411       }
 412       return factory->get_cheap_instance(out);
 413     }
 414   }
 415   THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 416              "Unknown diagnostic command");
 417 }
 418 
 419 DCmd* DCmdFactory::create_local_DCmd(CmdLine &line, outputStream* out, TRAPS) {
 420   {
 421     DCmdFactory* factory = get_factory(line.cmd_addr(),line.cmd_len());
 422     if(factory != NULL) {
 423       if(!factory->is_enabled()) {
 424         THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 425              factory->get_disabled_message());
 426       }
 427       return factory->get_resource_instance(out);
 428     }
 429   }
 430   THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 431              "Unknown diagnostic command");
 432 }
 433 
 434 GrowableArray<const char*>* DCmdFactory::get_DCmd_list() {
 435     MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 436     GrowableArray<const char*>* array = new GrowableArray<const char*>();
 437     DCmdFactory* factory = _DCmdFactoryList;
 438     while (factory != NULL) {
 439         if (!factory->is_hidden()) {
 440             array->append(factory->get_name());
 441         }
 442         factory = factory->get_next();
 443     }
 444     return array;
 445 }
 446 
 447 GrowableArray<DCmdInfo*>* DCmdFactory::get_DCmdInfo_list() {
 448     MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag);
 449     GrowableArray<DCmdInfo*>* array = new GrowableArray<DCmdInfo*>();
 450     DCmdFactory* factory = _DCmdFactoryList;
 451     while (factory != NULL) {
 452         if (!factory->is_hidden()) {
 453             array->append(new DCmdInfo(factory->get_name(),
 454                     factory->get_description(), factory->get_impact(),
 455                     factory->get_num_arguments(), factory->is_enabled()));
 456         }
 457         factory = factory->get_next();
 458     }
 459     return array;
 460 }