1 /* 2 * Copyright (c) 2011, 2012, 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 - _cmd; 56 } 57 _args = cmd_end; 58 _args_len = line_end - _args; 59 } 60 61 bool DCmdArgIter::next(TRAPS) { 62 if (_len == 0) return false; 63 // skipping spaces 64 while (_cursor < _len - 1 && _buffer[_cursor] == _delim) { 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 bool arg_had_quotes = false; 79 while (_cursor <= _len - 1 && _buffer[_cursor] != '=' && _buffer[_cursor] != _delim) { 80 // argument can be surrounded by single or double quotes 81 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') { 82 _key_addr++; 83 char quote = _buffer[_cursor]; 84 arg_had_quotes = true; 85 while (_cursor < _len - 1) { 86 _cursor++; 87 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') { 88 break; 89 } 90 } 91 if (_buffer[_cursor] != quote) { 92 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 93 "Format error in diagnostic command arguments", false); 94 } 95 break; 96 } 97 _cursor++; 98 } 99 _key_len = &_buffer[_cursor] - _key_addr; 100 if (arg_had_quotes) { 101 // if the argument was quoted, we need to step past the last quote here 102 _cursor++; 103 } 104 // check if the argument has the <key>=<value> format 105 if (_cursor <= _len -1 && _buffer[_cursor] == '=') { 106 _cursor++; 107 _value_addr = &_buffer[_cursor]; 108 bool value_had_quotes = false; 109 // extract the value 110 while (_cursor <= _len - 1 && _buffer[_cursor] != _delim) { 111 // value can be surrounded by simple or double quotes 112 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') { 113 _value_addr++; 114 char quote = _buffer[_cursor]; 115 value_had_quotes = true; 116 while (_cursor < _len - 1) { 117 _cursor++; 118 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') { 119 break; 120 } 121 } 122 if (_buffer[_cursor] != quote) { 123 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 124 "Format error in diagnostic command arguments", false); 125 } 126 break; 127 } 128 _cursor++; 129 } 130 _value_len = &_buffer[_cursor] - _value_addr; 131 if (value_had_quotes) { 132 // if the value was quoted, we need to step past the last quote here 133 _cursor++; 134 } 135 } else { 136 _value_addr = NULL; 137 _value_len = 0; 138 } 139 return _key_len != 0; 140 } 141 142 bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) { 143 if (info == NULL) return false; 144 return strcmp((const char*)cmd_name, info->name()) == 0; 145 } 146 147 void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) { 148 assert(arg != NULL, "Sanity"); 149 if (_options == NULL) { 150 _options = arg; 151 } else { 152 GenDCmdArgument* o = _options; 153 while (o->next() != NULL) { 154 o = o->next(); 155 } 156 o->set_next(arg); 157 } 158 arg->set_next(NULL); 159 Thread* THREAD = Thread::current(); 160 arg->init_value(THREAD); 161 if (HAS_PENDING_EXCEPTION) { 162 fatal("Initialization must be successful"); 163 } 164 } 165 166 void DCmdParser::add_dcmd_argument(GenDCmdArgument* arg) { 167 assert(arg != NULL, "Sanity"); 168 if (_arguments_list == NULL) { 169 _arguments_list = arg; 170 } else { 171 GenDCmdArgument* a = _arguments_list; 172 while (a->next() != NULL) { 173 a = a->next(); 174 } 175 a->set_next(arg); 176 } 177 arg->set_next(NULL); 178 Thread* THREAD = Thread::current(); 179 arg->init_value(THREAD); 180 if (HAS_PENDING_EXCEPTION) { 181 fatal("Initialization must be successful"); 182 } 183 } 184 185 void DCmdParser::parse(CmdLine* line, char delim, TRAPS) { 186 GenDCmdArgument* next_argument = _arguments_list; 187 DCmdArgIter iter(line->args_addr(), line->args_len(), delim); 188 bool cont = iter.next(CHECK); 189 while (cont) { 190 GenDCmdArgument* arg = lookup_dcmd_option(iter.key_addr(), 191 iter.key_length()); 192 if (arg != NULL) { 193 arg->read_value(iter.value_addr(), iter.value_length(), CHECK); 194 } else { 195 if (next_argument != NULL) { 196 arg = next_argument; 197 arg->read_value(iter.key_addr(), iter.key_length(), CHECK); 198 next_argument = next_argument->next(); 199 } else { 200 size_t buflen = 120; 201 char buf[buflen]; 202 size_t len = iter.key_length(); 203 char argname[len + 1]; 204 205 strncpy(argname, iter.key_addr(), len); 206 argname[len] = '\0'; 207 jio_snprintf(buf, buflen - 1, "Unknown argument '%s' in diagnostic command.", argname); 208 209 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); 210 } 211 } 212 cont = iter.next(CHECK); 213 } 214 check(CHECK); 215 } 216 217 GenDCmdArgument* DCmdParser::lookup_dcmd_option(const char* name, size_t len) { 218 GenDCmdArgument* arg = _options; 219 while (arg != NULL) { 220 if (strlen(arg->name()) == len && 221 strncmp(name, arg->name(), len) == 0) { 222 return arg; 223 } 224 arg = arg->next(); 225 } 226 return NULL; 227 } 228 229 void DCmdParser::check(TRAPS) { 230 size_t buflen = 256; 231 char buf[buflen]; 232 GenDCmdArgument* arg = _arguments_list; 233 while (arg != NULL) { 234 if (arg->is_mandatory() && !arg->has_value()) { 235 snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name()); 236 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); 237 } 238 arg = arg->next(); 239 } 240 arg = _options; 241 while (arg != NULL) { 242 if (arg->is_mandatory() && !arg->has_value()) { 243 snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name()); 244 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf); 245 } 246 arg = arg->next(); 247 } 248 } 249 250 void DCmdParser::print_help(outputStream* out, const char* cmd_name) { 251 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]"); 252 GenDCmdArgument* arg = _arguments_list; 253 while (arg != NULL) { 254 if (arg->is_mandatory()) { 255 out->print(" <%s>", arg->name()); 256 } else { 257 out->print(" [<%s>]", arg->name()); 258 } 259 arg = arg->next(); 260 } 261 out->print_cr(""); 262 if (_arguments_list != NULL) { 263 out->print_cr("\nArguments:"); 264 arg = _arguments_list; 265 while (arg != NULL) { 266 out->print("\t%s : %s %s (%s, ", arg->name(), 267 arg->is_mandatory() ? "" : "[optional]", 268 arg->description(), arg->type()); 269 if (arg->has_default()) { 270 out->print(arg->default_string()); 271 } else { 272 out->print("no default value"); 273 } 274 out->print_cr(")"); 275 arg = arg->next(); 276 } 277 } 278 if (_options != NULL) { 279 out->print_cr("\nOptions: (options must be specified using the <key> or <key>=<value> syntax)"); 280 arg = _options; 281 while (arg != NULL) { 282 out->print("\t%s : %s %s (%s, ", arg->name(), 283 arg->is_mandatory() ? "" : "[optional]", 284 arg->description(), arg->type()); 285 if (arg->has_default()) { 286 out->print(arg->default_string()); 287 } else { 288 out->print("no default value"); 289 } 290 out->print_cr(")"); 291 arg = arg->next(); 292 } 293 } 294 } 295 296 void DCmdParser::reset(TRAPS) { 297 GenDCmdArgument* arg = _arguments_list; 298 while (arg != NULL) { 299 arg->reset(CHECK); 300 arg = arg->next(); 301 } 302 arg = _options; 303 while (arg != NULL) { 304 arg->reset(CHECK); 305 arg = arg->next(); 306 } 307 } 308 309 void DCmdParser::cleanup() { 310 GenDCmdArgument* arg = _arguments_list; 311 while (arg != NULL) { 312 arg->cleanup(); 313 arg = arg->next(); 314 } 315 arg = _options; 316 while (arg != NULL) { 317 arg->cleanup(); 318 arg = arg->next(); 319 } 320 } 321 322 int DCmdParser::num_arguments() { 323 GenDCmdArgument* arg = _arguments_list; 324 int count = 0; 325 while (arg != NULL) { 326 count++; 327 arg = arg->next(); 328 } 329 arg = _options; 330 while (arg != NULL) { 331 count++; 332 arg = arg->next(); 333 } 334 return count; 335 } 336 337 GrowableArray<const char *>* DCmdParser::argument_name_array() { 338 int count = num_arguments(); 339 GrowableArray<const char *>* array = new GrowableArray<const char *>(count); 340 GenDCmdArgument* arg = _arguments_list; 341 while (arg != NULL) { 342 array->append(arg->name()); 343 arg = arg->next(); 344 } 345 arg = _options; 346 while (arg != NULL) { 347 array->append(arg->name()); 348 arg = arg->next(); 349 } 350 return array; 351 } 352 353 GrowableArray<DCmdArgumentInfo*>* DCmdParser::argument_info_array() { 354 int count = num_arguments(); 355 GrowableArray<DCmdArgumentInfo*>* array = new GrowableArray<DCmdArgumentInfo *>(count); 356 int idx = 0; 357 GenDCmdArgument* arg = _arguments_list; 358 while (arg != NULL) { 359 array->append(new DCmdArgumentInfo(arg->name(), arg->description(), 360 arg->type(), arg->default_string(), arg->is_mandatory(), 361 false, idx)); 362 idx++; 363 arg = arg->next(); 364 } 365 arg = _options; 366 while (arg != NULL) { 367 array->append(new DCmdArgumentInfo(arg->name(), arg->description(), 368 arg->type(), arg->default_string(), arg->is_mandatory(), 369 true)); 370 arg = arg->next(); 371 } 372 return array; 373 } 374 375 DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL; 376 377 void DCmd::parse_and_execute(outputStream* out, const char* cmdline, 378 char delim, TRAPS) { 379 380 if (cmdline == NULL) return; // Nothing to do! 381 DCmdIter iter(cmdline, '\n'); 382 383 while (iter.has_next()) { 384 CmdLine line = iter.next(); 385 if (line.is_stop()) { 386 break; 387 } 388 if (line.is_executable()) { 389 DCmd* command = DCmdFactory::create_local_DCmd(line, out, CHECK); 390 assert(command != NULL, "command error must be handled before this line"); 391 DCmdMark mark(command); 392 command->parse(&line, delim, CHECK); 393 command->execute(CHECK); 394 } 395 } 396 } 397 398 void DCmdWithParser::parse(CmdLine* line, char delim, TRAPS) { 399 _dcmdparser.parse(line, delim, CHECK); 400 } 401 402 void DCmdWithParser::print_help(const char* name) { 403 _dcmdparser.print_help(output(), name); 404 } 405 406 void DCmdWithParser::reset(TRAPS) { 407 _dcmdparser.reset(CHECK); 408 } 409 410 void DCmdWithParser::cleanup() { 411 _dcmdparser.cleanup(); 412 } 413 414 GrowableArray<const char*>* DCmdWithParser::argument_name_array() { 415 return _dcmdparser.argument_name_array(); 416 } 417 418 GrowableArray<DCmdArgumentInfo*>* DCmdWithParser::argument_info_array() { 419 return _dcmdparser.argument_info_array(); 420 } 421 422 Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf, "DCmdFactory", true); 423 424 DCmdFactory* DCmdFactory::factory(const char* name, size_t len) { 425 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); 426 DCmdFactory* factory = _DCmdFactoryList; 427 while (factory != NULL) { 428 if (strlen(factory->name()) == len && 429 strncmp(name, factory->name(), len) == 0) { 430 return factory; 431 } 432 factory = factory->_next; 433 } 434 return NULL; 435 } 436 437 int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) { 438 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); 439 factory->_next = _DCmdFactoryList; 440 _DCmdFactoryList = factory; 441 return 0; // Actually, there's no checks for duplicates 442 } 443 444 DCmd* DCmdFactory::create_global_DCmd(CmdLine &line, outputStream* out, TRAPS) { 445 DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len()); 446 if (f != NULL) { 447 if (f->is_enabled()) { 448 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 449 f->disabled_message()); 450 } 451 return f->create_Cheap_instance(out); 452 } 453 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 454 "Unknown diagnostic command"); 455 } 456 457 DCmd* DCmdFactory::create_local_DCmd(CmdLine &line, outputStream* out, TRAPS) { 458 DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len()); 459 if (f != NULL) { 460 if (!f->is_enabled()) { 461 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 462 f->disabled_message()); 463 } 464 return f->create_resource_instance(out); 465 } 466 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 467 "Unknown diagnostic command"); 468 } 469 470 GrowableArray<const char*>* DCmdFactory::DCmd_list() { 471 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); 472 GrowableArray<const char*>* array = new GrowableArray<const char*>(); 473 DCmdFactory* factory = _DCmdFactoryList; 474 while (factory != NULL) { 475 if (!factory->is_hidden()) { 476 array->append(factory->name()); 477 } 478 factory = factory->next(); 479 } 480 return array; 481 } 482 483 GrowableArray<DCmdInfo*>* DCmdFactory::DCmdInfo_list() { 484 MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); 485 GrowableArray<DCmdInfo*>* array = new GrowableArray<DCmdInfo*>(); 486 DCmdFactory* factory = _DCmdFactoryList; 487 while (factory != NULL) { 488 if (!factory->is_hidden()) { 489 array->append(new DCmdInfo(factory->name(), 490 factory->description(), factory->impact(), 491 factory->num_arguments(), factory->is_enabled())); 492 } 493 factory = factory->next(); 494 } 495 return array; 496 }