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/allocation.inline.hpp"
  27 #include "runtime/thread.hpp"
  28 #include "services/diagnosticArgument.hpp"
  29 
  30 void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {
  31   if (is_set()) {
  32     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
  33             "Duplicates in diagnostic command arguments");
  34   }
  35   parse_value(str, len, CHECK);
  36   set_is_set(true);
  37 }
  38 
  39 template <> void DCmdArgument<jlong>::parse_value(const char* str,
  40         size_t len, TRAPS) {
  41   if(sscanf(str, INT64_FORMAT, &_value) != 1) {
  42     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
  43       "Integer parsing error in diagnostic command arguments");
  44   }
  45 }
  46 
  47 template <> void DCmdArgument<jlong>::init_value(TRAPS) {
  48   if(has_default()) {
  49     this->parse_value(_default_string, strlen(_default_string), THREAD);
  50     if (HAS_PENDING_EXCEPTION) {
  51       fatal("Default string must be parsable");
  52     }
  53   } else {
  54     set_value(0);
  55   }
  56 }
  57 
  58 template <> void DCmdArgument<jlong>::destroy_value() { }
  59 
  60 template <> void DCmdArgument<bool>::parse_value(const char* str,
  61         size_t len, TRAPS) {
  62   if(len == 0) {
  63     set_value(true);
  64   } else {
  65     if(strcasecmp(str, "true") == 0) {
  66        set_value(true);
  67     } else if (strcasecmp(str, "false") == 0) {
  68        set_value(false);
  69     } else {
  70       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
  71         "Boolean parsing error in diagnostic command arguments");
  72     }
  73   }
  74 }
  75 
  76 template <> void DCmdArgument<bool>::init_value(TRAPS) {
  77   if(has_default()) {
  78     this->parse_value(_default_string, strlen(_default_string), THREAD);
  79     if (HAS_PENDING_EXCEPTION) {
  80       fatal("Default string must be parsable");
  81     }
  82   } else {
  83     set_value(false);
  84   }
  85 }
  86 
  87 template <> void DCmdArgument<bool>::destroy_value() { }
  88 
  89 template <> void DCmdArgument<char*>::parse_value(const char* str,
  90         size_t len, TRAPS) {
  91   _value = NEW_C_HEAP_ARRAY(char, len+1);
  92   strncpy(_value, str, len);
  93   _value[len] = 0;
  94 }
  95 
  96 template <> void DCmdArgument<char*>::init_value(TRAPS) {
  97   if(has_default()) {
  98     this->parse_value(_default_string, strlen(_default_string), THREAD);
  99     if (HAS_PENDING_EXCEPTION) {
 100       fatal("Default string must be parsable");
 101     }
 102   } else {
 103     set_value(NULL);
 104   }
 105 }
 106 
 107 template <> void DCmdArgument<char*>::destroy_value() {
 108   if(_value != NULL) {
 109     FREE_C_HEAP_ARRAY(char, _value);
 110     set_value(NULL);
 111   }
 112 }