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/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   // len is the length of the current token starting at str
  63   if (len == 0) {
  64     set_value(true);
  65   } else {
  66     if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {
  67        set_value(true);
  68     } else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {
  69        set_value(false);
  70     } else {
  71       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
  72         "Boolean parsing error in diagnostic command arguments");
  73     }
  74   }
  75 }
  76 
  77 template <> void DCmdArgument<bool>::init_value(TRAPS) {
  78   if (has_default()) {
  79     this->parse_value(_default_string, strlen(_default_string), THREAD);
  80     if (HAS_PENDING_EXCEPTION) {
  81       fatal("Default string must be parsable");
  82     }
  83   } else {
  84     set_value(false);
  85   }
  86 }
  87 
  88 template <> void DCmdArgument<bool>::destroy_value() { }
  89 
  90 template <> void DCmdArgument<char*>::parse_value(const char* str,
  91                                                   size_t len, TRAPS) {
  92   _value = NEW_C_HEAP_ARRAY(char, len+1);
  93   strncpy(_value, str, len);
  94   _value[len] = 0;
  95 }
  96 
  97 template <> void DCmdArgument<char*>::init_value(TRAPS) {
  98   if (has_default()) {
  99     this->parse_value(_default_string, strlen(_default_string), THREAD);
 100     if (HAS_PENDING_EXCEPTION) {
 101       fatal("Default string must be parsable");
 102     }
 103   } else {
 104     set_value(NULL);
 105   }
 106 }
 107 
 108 template <> void DCmdArgument<char*>::destroy_value() {
 109   if (_value != NULL) {
 110     FREE_C_HEAP_ARRAY(char, _value);
 111     set_value(NULL);
 112   }
 113 }