diagnosticArgument.cpp 10.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2012 Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "memory/allocation.inline.hpp"
#include "runtime/thread.hpp"
#include "services/diagnosticArgument.hpp"

void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {
31 32 33 34 35 36 37 38
  /* NOTE:Some argument types doesn't require a value,
   * for instance boolean arguments: "enableFeatureX". is
   * equivalent to "enableFeatureX=true". In these cases,
   * str will be null. This is perfectly valid.
   * All argument types must perform null checks on str.
   */

  if (is_set() && !allow_multiple()) {
39
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
40
            "Duplicates in diagnostic command arguments\n");
41 42 43 44 45
  }
  parse_value(str, len, CHECK);
  set_is_set(true);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
void GenDCmdArgument::to_string(jlong l, char* buf, size_t len) {
  jio_snprintf(buf, len, INT64_FORMAT, l);
}

void GenDCmdArgument::to_string(bool b, char* buf, size_t len) {
  jio_snprintf(buf, len, b ? "true" : "false");
}

void GenDCmdArgument::to_string(NanoTimeArgument n, char* buf, size_t len) {
  jio_snprintf(buf, len, INT64_FORMAT, n._nanotime);
}

void GenDCmdArgument::to_string(MemorySizeArgument m, char* buf, size_t len) {
  jio_snprintf(buf, len, INT64_FORMAT, m._size);
}

void GenDCmdArgument::to_string(char* c, char* buf, size_t len) {
  jio_snprintf(buf, len, "%s", c);
}

void GenDCmdArgument::to_string(StringArrayArgument* f, char* buf, size_t len) {
  int length = f->array()->length();
  size_t written = 0;
  buf[0] = 0;
  for (int i = 0; i < length; i++) {
    char* next_str = f->array()->at(i);
    size_t next_size = strlen(next_str);
    //Check if there's room left to write next element
    if (written + next_size > len) {
      return;
    }
    //Actually write element
    strcat(buf, next_str);
    written += next_size;
    //Check if there's room left for the comma
    if (i < length-1 && len - written > 0) {
      strcat(buf, ",");
    }
  }
}

87 88
template <> void DCmdArgument<jlong>::parse_value(const char* str,
                                                  size_t len, TRAPS) {
89
    if (str == NULL || sscanf(str, INT64_FORMAT, &_value) != 1) {
90
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
91
      "Integer parsing error in diagnostic command arguments\n");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  }
}

template <> void DCmdArgument<jlong>::init_value(TRAPS) {
  if (has_default()) {
    this->parse_value(_default_string, strlen(_default_string), THREAD);
    if (HAS_PENDING_EXCEPTION) {
      fatal("Default string must be parsable");
    }
  } else {
    set_value(0);
  }
}

template <> void DCmdArgument<jlong>::destroy_value() { }

template <> void DCmdArgument<bool>::parse_value(const char* str,
                                                 size_t len, TRAPS) {
110
  // len is the length of the current token starting at str
111 112 113
  if (len == 0) {
    set_value(true);
  } else {
114
    if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {
115
       set_value(true);
116
    } else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
       set_value(false);
    } else {
      THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
        "Boolean parsing error in diagnostic command arguments");
    }
  }
}

template <> void DCmdArgument<bool>::init_value(TRAPS) {
  if (has_default()) {
    this->parse_value(_default_string, strlen(_default_string), THREAD);
    if (HAS_PENDING_EXCEPTION) {
      fatal("Default string must be parsable");
    }
  } else {
    set_value(false);
  }
}

template <> void DCmdArgument<bool>::destroy_value() { }

template <> void DCmdArgument<char*>::parse_value(const char* str,
                                                  size_t len, TRAPS) {
140 141 142
  if (str == NULL) {
    _value = NULL;
  } else {
Z
zgu 已提交
143
    _value = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
144 145 146
    strncpy(_value, str, len);
    _value[len] = 0;
  }
147 148 149
}

template <> void DCmdArgument<char*>::init_value(TRAPS) {
150
  if (has_default() && _default_string != NULL) {
151 152
    this->parse_value(_default_string, strlen(_default_string), THREAD);
    if (HAS_PENDING_EXCEPTION) {
153
     fatal("Default string must be parsable");
154 155 156 157 158 159 160 161
    }
  } else {
    set_value(NULL);
  }
}

template <> void DCmdArgument<char*>::destroy_value() {
  if (_value != NULL) {
Z
zgu 已提交
162
    FREE_C_HEAP_ARRAY(char, _value, mtInternal);
163 164 165
    set_value(NULL);
  }
}
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,
                                                 size_t len, TRAPS) {
  if (str == NULL) {
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
              "Integer parsing error nanotime value: syntax error");
  }

  int argc = sscanf(str, INT64_FORMAT , &_value._time);
  if (argc != 1) {
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
              "Integer parsing error nanotime value: syntax error");
  }
  size_t idx = 0;
  while(idx < len && isdigit(str[idx])) {
    idx++;
  }
  if (idx == len) {
    // only accept missing unit if the value is 0
    if (_value._time != 0) {
      THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
                "Integer parsing error nanotime value: unit required");
    } else {
      _value._nanotime = 0;
      strcpy(_value._unit, "ns");
      return;
    }
  } else if(len - idx > 2) {
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
              "Integer parsing error nanotime value: illegal unit");
  } else {
    strncpy(_value._unit, &str[idx], len - idx);
    /*Write an extra null termination. This is safe because _value._unit
     * is declared as char[3], and length is checked to be not larger than
     * two above. Also, this is necessary, since length might be 1, and the
     * default value already in the string is ns, which is two chars.
     */
    _value._unit[len-idx] = '\0';
  }

  if (strcmp(_value._unit, "ns") == 0) {
    _value._nanotime = _value._time;
  } else if (strcmp(_value._unit, "us") == 0) {
    _value._nanotime = _value._time * 1000;
  } else if (strcmp(_value._unit, "ms") == 0) {
    _value._nanotime = _value._time * 1000 * 1000;
  } else if (strcmp(_value._unit, "s") == 0) {
    _value._nanotime = _value._time * 1000 * 1000 * 1000;
  } else if (strcmp(_value._unit, "m") == 0) {
    _value._nanotime = _value._time * 60 * 1000 * 1000 * 1000;
  } else if (strcmp(_value._unit, "h") == 0) {
    _value._nanotime = _value._time * 60 * 60 * 1000 * 1000 * 1000;
  } else if (strcmp(_value._unit, "d") == 0) {
    _value._nanotime = _value._time * 24 * 60 * 60 * 1000 * 1000 * 1000;
  } else {
     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
               "Integer parsing error nanotime value: illegal unit");
  }
}

template <> void DCmdArgument<NanoTimeArgument>::init_value(TRAPS) {
  if (has_default()) {
    this->parse_value(_default_string, strlen(_default_string), THREAD);
    if (HAS_PENDING_EXCEPTION) {
      fatal("Default string must be parsable");
    }
  } else {
    _value._time = 0;
    _value._nanotime = 0;
    strcmp(_value._unit, "ns");
  }
}

template <> void DCmdArgument<NanoTimeArgument>::destroy_value() { }

// WARNING StringArrayArgument can only be used as an option, it cannot be
// used as an argument with the DCmdParser

template <> void DCmdArgument<StringArrayArgument*>::parse_value(const char* str,
                                                  size_t len, TRAPS) {
  _value->add(str,len);
}

template <> void DCmdArgument<StringArrayArgument*>::init_value(TRAPS) {
  _value = new StringArrayArgument();
  _allow_multiple = true;
  if (has_default()) {
    fatal("StringArrayArgument cannot have default value");
  }
}

template <> void DCmdArgument<StringArrayArgument*>::destroy_value() {
  if (_value != NULL) {
    delete _value;
    set_value(NULL);
  }
}

template <> void DCmdArgument<MemorySizeArgument>::parse_value(const char* str,
                                                  size_t len, TRAPS) {
  if (str == NULL) {
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
              "Integer parsing error nanotime value: syntax error");
  }

  if (*str == '-') {
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
               "Parsing error memory size value: negative values not allowed");
  }
  int res = sscanf(str, UINT64_FORMAT "%c", &_value._val, &_value._multiplier);
  if (res == 2) {
     switch (_value._multiplier) {
      case 'k': case 'K':
         _value._size = _value._val * 1024;
         break;
      case 'm': case 'M':
         _value._size = _value._val * 1024 * 1024;
         break;
      case 'g': case 'G':
         _value._size = _value._val * 1024 * 1024 * 1024;
         break;
       default:
         _value._size = _value._val;
         _value._multiplier = ' ';
         //default case should be to break with no error, since user
         //can write size in bytes, or might have a delimiter and next arg
         break;
     }
   } else if (res == 1) {
     _value._size = _value._val;
   } else {
     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
               "Parsing error memory size value: invalid value");
   }
}

template <> void DCmdArgument<MemorySizeArgument>::init_value(TRAPS) {
  if (has_default()) {
    this->parse_value(_default_string, strlen(_default_string), THREAD);
    if (HAS_PENDING_EXCEPTION) {
      fatal("Default string must be parsable");
    }
  } else {
    _value._size = 0;
    _value._val = 0;
    _value._multiplier = ' ';
  }
}

template <> void DCmdArgument<MemorySizeArgument>::destroy_value() { }