Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/diagnosticArgument.cpp
32285 views
/*1* Copyright (c) 2011, 2013, 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 "memory/allocation.inline.hpp"26#include "memory/resourceArea.hpp"27#include "runtime/thread.hpp"28#include "services/diagnosticArgument.hpp"2930void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {31/* NOTE:Some argument types doesn't require a value,32* for instance boolean arguments: "enableFeatureX". is33* equivalent to "enableFeatureX=true". In these cases,34* str will be null. This is perfectly valid.35* All argument types must perform null checks on str.36*/3738if (is_set() && !allow_multiple()) {39THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),40"Duplicates in diagnostic command arguments\n");41}42parse_value(str, len, CHECK);43set_is_set(true);44}4546void GenDCmdArgument::to_string(jlong l, char* buf, size_t len) {47jio_snprintf(buf, len, INT64_FORMAT, l);48}4950void GenDCmdArgument::to_string(bool b, char* buf, size_t len) {51jio_snprintf(buf, len, b ? "true" : "false");52}5354void GenDCmdArgument::to_string(NanoTimeArgument n, char* buf, size_t len) {55jio_snprintf(buf, len, INT64_FORMAT, n._nanotime);56}5758void GenDCmdArgument::to_string(MemorySizeArgument m, char* buf, size_t len) {59jio_snprintf(buf, len, INT64_FORMAT, m._size);60}6162void GenDCmdArgument::to_string(char* c, char* buf, size_t len) {63jio_snprintf(buf, len, "%s", (c != NULL) ? c : "");64}6566void GenDCmdArgument::to_string(StringArrayArgument* f, char* buf, size_t len) {67int length = f->array()->length();68size_t written = 0;69buf[0] = 0;70for (int i = 0; i < length; i++) {71char* next_str = f->array()->at(i);72size_t next_size = strlen(next_str);73//Check if there's room left to write next element74if (written + next_size > len) {75return;76}77//Actually write element78strcat(buf, next_str);79written += next_size;80//Check if there's room left for the comma81if (i < length-1 && len - written > 0) {82strcat(buf, ",");83}84}85}8687template <> void DCmdArgument<jlong>::parse_value(const char* str,88size_t len, TRAPS) {89int scanned = -1;90if (str == NULL91|| sscanf(str, JLONG_FORMAT "%n", &_value, &scanned) != 192|| (size_t)scanned != len)93{94ResourceMark rm;9596char* buf = NEW_RESOURCE_ARRAY(char, len + 1);97strncpy(buf, str, len);98buf[len] = '\0';99Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),100"Integer parsing error in command argument '%s'. Could not parse: %s.", _name, buf);101}102}103104template <> void DCmdArgument<jlong>::init_value(TRAPS) {105if (has_default()) {106this->parse_value(_default_string, strlen(_default_string), THREAD);107if (HAS_PENDING_EXCEPTION) {108fatal("Default string must be parseable");109}110} else {111set_value(0);112}113}114115template <> void DCmdArgument<jlong>::destroy_value() { }116117template <> void DCmdArgument<bool>::parse_value(const char* str,118size_t len, TRAPS) {119// len is the length of the current token starting at str120if (len == 0) {121set_value(true);122} else {123if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {124set_value(true);125} else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {126set_value(false);127} else {128ResourceMark rm;129130char* buf = NEW_RESOURCE_ARRAY(char, len + 1);131strncpy(buf, str, len);132buf[len] = '\0';133Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),134"Boolean parsing error in command argument '%s'. Could not parse: %s.", _name, buf);135}136}137}138139template <> void DCmdArgument<bool>::init_value(TRAPS) {140if (has_default()) {141this->parse_value(_default_string, strlen(_default_string), THREAD);142if (HAS_PENDING_EXCEPTION) {143fatal("Default string must be parsable");144}145} else {146set_value(false);147}148}149150template <> void DCmdArgument<bool>::destroy_value() { }151152template <> void DCmdArgument<char*>::parse_value(const char* str,153size_t len, TRAPS) {154if (str == NULL) {155_value = NULL;156} else {157_value = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);158strncpy(_value, str, len);159_value[len] = 0;160}161}162163template <> void DCmdArgument<char*>::init_value(TRAPS) {164if (has_default() && _default_string != NULL) {165this->parse_value(_default_string, strlen(_default_string), THREAD);166if (HAS_PENDING_EXCEPTION) {167fatal("Default string must be parsable");168}169} else {170set_value(NULL);171}172}173174template <> void DCmdArgument<char*>::destroy_value() {175if (_value != NULL) {176FREE_C_HEAP_ARRAY(char, _value, mtInternal);177set_value(NULL);178}179}180181template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,182size_t len, TRAPS) {183if (str == NULL) {184THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),185"Integer parsing error nanotime value: syntax error, value is null");186}187188int argc = sscanf(str, JLONG_FORMAT, &_value._time);189if (argc != 1) {190THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),191"Integer parsing error nanotime value: syntax error");192}193size_t idx = 0;194while(idx < len && isdigit(str[idx])) {195idx++;196}197if (idx == len) {198// only accept missing unit if the value is 0199if (_value._time != 0) {200THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),201"Integer parsing error nanotime value: unit required");202} else {203_value._nanotime = 0;204strcpy(_value._unit, "ns");205return;206}207} else if(len - idx > 2) {208THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),209"Integer parsing error nanotime value: illegal unit");210} else {211strncpy(_value._unit, &str[idx], len - idx);212/*Write an extra null termination. This is safe because _value._unit213* is declared as char[3], and length is checked to be not larger than214* two above. Also, this is necessary, since length might be 1, and the215* default value already in the string is ns, which is two chars.216*/217_value._unit[len-idx] = '\0';218}219220if (strcmp(_value._unit, "ns") == 0) {221_value._nanotime = _value._time;222} else if (strcmp(_value._unit, "us") == 0) {223_value._nanotime = _value._time * 1000;224} else if (strcmp(_value._unit, "ms") == 0) {225_value._nanotime = _value._time * 1000 * 1000;226} else if (strcmp(_value._unit, "s") == 0) {227_value._nanotime = _value._time * 1000 * 1000 * 1000;228} else if (strcmp(_value._unit, "m") == 0) {229_value._nanotime = _value._time * 60 * 1000 * 1000 * 1000;230} else if (strcmp(_value._unit, "h") == 0) {231_value._nanotime = _value._time * 60 * 60 * 1000 * 1000 * 1000;232} else if (strcmp(_value._unit, "d") == 0) {233_value._nanotime = _value._time * 24 * 60 * 60 * 1000 * 1000 * 1000;234} else {235THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),236"Integer parsing error nanotime value: illegal unit");237}238}239240template <> void DCmdArgument<NanoTimeArgument>::init_value(TRAPS) {241if (has_default()) {242this->parse_value(_default_string, strlen(_default_string), THREAD);243if (HAS_PENDING_EXCEPTION) {244fatal("Default string must be parsable");245}246} else {247_value._time = 0;248_value._nanotime = 0;249strcpy(_value._unit, "ns");250}251}252253template <> void DCmdArgument<NanoTimeArgument>::destroy_value() { }254255// WARNING StringArrayArgument can only be used as an option, it cannot be256// used as an argument with the DCmdParser257258template <> void DCmdArgument<StringArrayArgument*>::parse_value(const char* str,259size_t len, TRAPS) {260_value->add(str,len);261}262263template <> void DCmdArgument<StringArrayArgument*>::init_value(TRAPS) {264_value = new StringArrayArgument();265_allow_multiple = true;266if (has_default()) {267fatal("StringArrayArgument cannot have default value");268}269}270271template <> void DCmdArgument<StringArrayArgument*>::destroy_value() {272if (_value != NULL) {273delete _value;274set_value(NULL);275}276}277278template <> void DCmdArgument<MemorySizeArgument>::parse_value(const char* str,279size_t len, TRAPS) {280if (str == NULL) {281THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),282"Parsing error memory size value: syntax error, value is null\n");283}284285if (*str == '-') {286THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),287"Parsing error memory size value: negative values not allowed");288}289int res = sscanf(str, UINT64_FORMAT "%c", &_value._val, &_value._multiplier);290if (res == 2) {291switch (_value._multiplier) {292case 'k': case 'K':293_value._size = _value._val * 1024;294break;295case 'm': case 'M':296_value._size = _value._val * 1024 * 1024;297break;298case 'g': case 'G':299_value._size = _value._val * 1024 * 1024 * 1024;300break;301default:302_value._size = _value._val;303_value._multiplier = ' ';304//default case should be to break with no error, since user305//can write size in bytes, or might have a delimiter and next arg306break;307}308} else if (res == 1) {309_value._size = _value._val;310} else {311THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),312"Parsing error memory size value: invalid value");313}314}315316template <> void DCmdArgument<MemorySizeArgument>::init_value(TRAPS) {317if (has_default()) {318this->parse_value(_default_string, strlen(_default_string), THREAD);319if (HAS_PENDING_EXCEPTION) {320fatal("Default string must be parsable");321}322} else {323_value._size = 0;324_value._val = 0;325_value._multiplier = ' ';326}327}328329template <> void DCmdArgument<MemorySizeArgument>::destroy_value() { }330331332