Path: blob/master/src/hotspot/share/services/diagnosticArgument.cpp
41144 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm.h"26#include "memory/allocation.inline.hpp"27#include "memory/resourceArea.hpp"28#include "runtime/thread.hpp"29#include "services/diagnosticArgument.hpp"3031StringArrayArgument::StringArrayArgument() {32_array = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<char *>(32, mtServiceability);33assert(_array != NULL, "Sanity check");34}3536StringArrayArgument::~StringArrayArgument() {37for (int i=0; i<_array->length(); i++) {38FREE_C_HEAP_ARRAY(char, _array->at(i));39}40delete _array;41}4243void StringArrayArgument::add(const char* str, size_t len) {44if (str != NULL) {45char* ptr = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);46strncpy(ptr, str, len);47ptr[len] = 0;48_array->append(ptr);49}50}5152void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {53/* NOTE:Some argument types doesn't require a value,54* for instance boolean arguments: "enableFeatureX". is55* equivalent to "enableFeatureX=true". In these cases,56* str will be null. This is perfectly valid.57* All argument types must perform null checks on str.58*/5960if (is_set() && !allow_multiple()) {61THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),62"Duplicates in diagnostic command arguments\n");63}64parse_value(str, len, CHECK);65set_is_set(true);66}6768void GenDCmdArgument::to_string(jlong l, char* buf, size_t len) const {69jio_snprintf(buf, len, INT64_FORMAT, l);70}7172void GenDCmdArgument::to_string(bool b, char* buf, size_t len) const {73jio_snprintf(buf, len, b ? "true" : "false");74}7576void GenDCmdArgument::to_string(NanoTimeArgument n, char* buf, size_t len) const {77jio_snprintf(buf, len, INT64_FORMAT, n._nanotime);78}7980void GenDCmdArgument::to_string(MemorySizeArgument m, char* buf, size_t len) const {81jio_snprintf(buf, len, INT64_FORMAT, m._size);82}8384void GenDCmdArgument::to_string(char* c, char* buf, size_t len) const {85jio_snprintf(buf, len, "%s", (c != NULL) ? c : "");86}8788void GenDCmdArgument::to_string(StringArrayArgument* f, char* buf, size_t len) const {89int length = f->array()->length();90size_t written = 0;91buf[0] = 0;92for (int i = 0; i < length; i++) {93char* next_str = f->array()->at(i);94size_t next_size = strlen(next_str);95//Check if there's room left to write next element96if (written + next_size > len) {97return;98}99//Actually write element100strcat(buf, next_str);101written += next_size;102//Check if there's room left for the comma103if (i < length-1 && len - written > 0) {104strcat(buf, ",");105}106}107}108109template <> void DCmdArgument<jlong>::parse_value(const char* str,110size_t len, TRAPS) {111int scanned = -1;112if (str == NULL113|| sscanf(str, JLONG_FORMAT "%n", &_value, &scanned) != 1114|| (size_t)scanned != len)115{116ResourceMark rm;117118char* buf = NEW_RESOURCE_ARRAY(char, len + 1);119strncpy(buf, str, len);120buf[len] = '\0';121Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),122"Integer parsing error in command argument '%s'. Could not parse: %s.\n", _name, buf);123}124}125126template <> void DCmdArgument<jlong>::init_value(TRAPS) {127if (has_default()) {128this->parse_value(_default_string, strlen(_default_string), THREAD);129if (HAS_PENDING_EXCEPTION) {130fatal("Default string must be parseable");131}132} else {133set_value(0);134}135}136137template <> void DCmdArgument<jlong>::destroy_value() { }138139template <> void DCmdArgument<bool>::parse_value(const char* str,140size_t len, TRAPS) {141// len is the length of the current token starting at str142if (len == 0) {143set_value(true);144} else {145if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {146set_value(true);147} else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {148set_value(false);149} else {150ResourceMark rm;151152char* buf = NEW_RESOURCE_ARRAY(char, len + 1);153154PRAGMA_DIAG_PUSH155PRAGMA_STRINGOP_TRUNCATION_IGNORED156// This code can incorrectly cause a "stringop-truncation" warning with gcc157strncpy(buf, str, len);158PRAGMA_DIAG_POP159160buf[len] = '\0';161Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),162"Boolean parsing error in command argument '%s'. Could not parse: %s.\n", _name, buf);163}164}165}166167template <> void DCmdArgument<bool>::init_value(TRAPS) {168if (has_default()) {169this->parse_value(_default_string, strlen(_default_string), THREAD);170if (HAS_PENDING_EXCEPTION) {171fatal("Default string must be parsable");172}173} else {174set_value(false);175}176}177178template <> void DCmdArgument<bool>::destroy_value() { }179180template <> void DCmdArgument<char*>::parse_value(const char* str,181size_t len, TRAPS) {182if (str == NULL) {183_value = NULL;184} else {185_value = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);186int n = os::snprintf(_value, len + 1, "%.*s", (int)len, str);187assert((size_t)n <= len, "Unexpected number of characters in string");188}189}190191template <> void DCmdArgument<char*>::init_value(TRAPS) {192if (has_default() && _default_string != NULL) {193this->parse_value(_default_string, strlen(_default_string), THREAD);194if (HAS_PENDING_EXCEPTION) {195fatal("Default string must be parsable");196}197} else {198set_value(NULL);199}200}201202template <> void DCmdArgument<char*>::destroy_value() {203FREE_C_HEAP_ARRAY(char, _value);204set_value(NULL);205}206207template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,208size_t len, TRAPS) {209if (str == NULL) {210THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),211"Integer parsing error nanotime value: syntax error, value is null\n");212}213214int argc = sscanf(str, JLONG_FORMAT, &_value._time);215if (argc != 1) {216THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),217"Integer parsing error nanotime value: syntax error\n");218}219size_t idx = 0;220while(idx < len && isdigit(str[idx])) {221idx++;222}223if (idx == len) {224// only accept missing unit if the value is 0225if (_value._time != 0) {226THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),227"Integer parsing error nanotime value: unit required\n");228} else {229_value._nanotime = 0;230strcpy(_value._unit, "ns");231return;232}233} else if(len - idx > 2) {234THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),235"Integer parsing error nanotime value: illegal unit\n");236} else {237strncpy(_value._unit, &str[idx], len - idx);238/*Write an extra null termination. This is safe because _value._unit239* is declared as char[3], and length is checked to be not larger than240* two above. Also, this is necessary, since length might be 1, and the241* default value already in the string is ns, which is two chars.242*/243_value._unit[len-idx] = '\0';244}245246if (strcmp(_value._unit, "ns") == 0) {247_value._nanotime = _value._time;248} else if (strcmp(_value._unit, "us") == 0) {249_value._nanotime = _value._time * 1000;250} else if (strcmp(_value._unit, "ms") == 0) {251_value._nanotime = _value._time * 1000 * 1000;252} else if (strcmp(_value._unit, "s") == 0) {253_value._nanotime = _value._time * 1000 * 1000 * 1000;254} else if (strcmp(_value._unit, "m") == 0) {255_value._nanotime = _value._time * 60 * 1000 * 1000 * 1000;256} else if (strcmp(_value._unit, "h") == 0) {257_value._nanotime = _value._time * 60 * 60 * 1000 * 1000 * 1000;258} else if (strcmp(_value._unit, "d") == 0) {259_value._nanotime = _value._time * 24 * 60 * 60 * 1000 * 1000 * 1000;260} else {261THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),262"Integer parsing error nanotime value: illegal unit\n");263}264}265266template <> void DCmdArgument<NanoTimeArgument>::init_value(TRAPS) {267if (has_default()) {268this->parse_value(_default_string, strlen(_default_string), THREAD);269if (HAS_PENDING_EXCEPTION) {270fatal("Default string must be parsable");271}272} else {273_value._time = 0;274_value._nanotime = 0;275strcpy(_value._unit, "ns");276}277}278279template <> void DCmdArgument<NanoTimeArgument>::destroy_value() { }280281// WARNING StringArrayArgument can only be used as an option, it cannot be282// used as an argument with the DCmdParser283284template <> void DCmdArgument<StringArrayArgument*>::parse_value(const char* str,285size_t len, TRAPS) {286_value->add(str,len);287}288289template <> void DCmdArgument<StringArrayArgument*>::init_value(TRAPS) {290_value = new StringArrayArgument();291_allow_multiple = true;292if (has_default()) {293fatal("StringArrayArgument cannot have default value");294}295}296297template <> void DCmdArgument<StringArrayArgument*>::destroy_value() {298if (_value != NULL) {299delete _value;300set_value(NULL);301}302}303304template <> void DCmdArgument<MemorySizeArgument>::parse_value(const char* str,305size_t len, TRAPS) {306if (str == NULL) {307THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),308"Parsing error memory size value: syntax error, value is null\n");309}310if (*str == '-') {311THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),312"Parsing error memory size value: negative values not allowed\n");313}314int res = sscanf(str, UINT64_FORMAT "%c", &_value._val, &_value._multiplier);315if (res == 2) {316switch (_value._multiplier) {317case 'k': case 'K':318_value._size = _value._val * 1024;319break;320case 'm': case 'M':321_value._size = _value._val * 1024 * 1024;322break;323case 'g': case 'G':324_value._size = _value._val * 1024 * 1024 * 1024;325break;326default:327_value._size = _value._val;328_value._multiplier = ' ';329//default case should be to break with no error, since user330//can write size in bytes, or might have a delimiter and next arg331break;332}333} else if (res == 1) {334_value._size = _value._val;335} else {336THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),337"Parsing error memory size value: invalid value\n");338}339}340341template <> void DCmdArgument<MemorySizeArgument>::init_value(TRAPS) {342if (has_default()) {343this->parse_value(_default_string, strlen(_default_string), THREAD);344if (HAS_PENDING_EXCEPTION) {345fatal("Default string must be parsable");346}347} else {348_value._size = 0;349_value._val = 0;350_value._multiplier = ' ';351}352}353354template <> void DCmdArgument<MemorySizeArgument>::destroy_value() { }355356357