jdk

Форк
0
/
diagnosticArgument.cpp 
352 строки · 11.7 Кб
1
/*
2
 * Copyright (c) 2011, 2023, 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 "jvm.h"
27
#include "memory/allocation.inline.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "runtime/javaThread.hpp"
30
#include "services/diagnosticArgument.hpp"
31
#include "utilities/globalDefinitions.hpp"
32

33
StringArrayArgument::StringArrayArgument() {
34
  _array = new (mtServiceability) GrowableArray<char *>(32, mtServiceability);
35
  assert(_array != nullptr, "Sanity check");
36
}
37

38
StringArrayArgument::~StringArrayArgument() {
39
  for (int i=0; i<_array->length(); i++) {
40
    FREE_C_HEAP_ARRAY(char, _array->at(i));
41
  }
42
  delete _array;
43
}
44

45
void StringArrayArgument::add(const char* str, size_t len) {
46
  if (str != nullptr) {
47
    char* ptr = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
48
    strncpy(ptr, str, len);
49
    ptr[len] = 0;
50
    _array->append(ptr);
51
  }
52
}
53

54
void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {
55
  /* NOTE:Some argument types doesn't require a value,
56
   * for instance boolean arguments: "enableFeatureX". is
57
   * equivalent to "enableFeatureX=true". In these cases,
58
   * str will be null. This is perfectly valid.
59
   * All argument types must perform null checks on str.
60
   */
61

62
  if (is_set() && !allow_multiple()) {
63
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
64
            "Duplicates in diagnostic command arguments\n");
65
  }
66
  parse_value(str, len, CHECK);
67
  set_is_set(true);
68
}
69

70
void GenDCmdArgument::to_string(jlong l, char* buf, size_t len) const {
71
  jio_snprintf(buf, len, INT64_FORMAT, l);
72
}
73

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

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

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

86
void GenDCmdArgument::to_string(char* c, char* buf, size_t len) const {
87
  jio_snprintf(buf, len, "%s", (c != nullptr) ? c : "");
88
}
89

90
void GenDCmdArgument::to_string(StringArrayArgument* f, char* buf, size_t len) const {
91
  int length = f->array()->length();
92
  size_t written = 0;
93
  buf[0] = 0;
94
  for (int i = 0; i < length; i++) {
95
    char* next_str = f->array()->at(i);
96
    size_t next_size = strlen(next_str);
97
    //Check if there's room left to write next element
98
    if (written + next_size > len) {
99
      return;
100
    }
101
    //Actually write element
102
    strcat(buf, next_str);
103
    written += next_size;
104
    //Check if there's room left for the comma
105
    if (i < length-1 && len - written > 0) {
106
      strcat(buf, ",");
107
    }
108
  }
109
}
110

111
template <> void DCmdArgument<jlong>::parse_value(const char* str,
112
                                                  size_t len, TRAPS) {
113
  int scanned = -1;
114
  if (str == nullptr
115
      || sscanf(str, JLONG_FORMAT "%n", &_value, &scanned) != 1
116
      || (size_t)scanned != len)
117
  {
118
    const int maxprint = 64;
119
    Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),
120
      "Integer parsing error in command argument '%s'. Could not parse: %.*s%s.\n", _name,
121
      MIN2((int)len, maxprint),
122
      (str == nullptr ? "<null>" : str),
123
      (len > maxprint ? "..." : ""));
124
  }
125
}
126

127
template <> void DCmdArgument<jlong>::init_value(TRAPS) {
128
  if (has_default()) {
129
    this->parse_value(_default_string, strlen(_default_string), THREAD);
130
    if (HAS_PENDING_EXCEPTION) {
131
      fatal("Default string must be parseable");
132
    }
133
  } else {
134
    set_value(0);
135
  }
136
}
137

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

140
template <> void DCmdArgument<bool>::parse_value(const char* str,
141
                                                 size_t len, TRAPS) {
142
  // len is the length of the current token starting at str
143
  if (len == 0) {
144
    set_value(true);
145
  } else {
146
    if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {
147
       set_value(true);
148
    } else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {
149
       set_value(false);
150
    } else {
151
      ResourceMark rm;
152

153
      char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
154

155
PRAGMA_DIAG_PUSH
156
PRAGMA_STRINGOP_TRUNCATION_IGNORED
157
      // This code can incorrectly cause a "stringop-truncation" warning with gcc
158
      strncpy(buf, str, len);
159
PRAGMA_DIAG_POP
160

161
      buf[len] = '\0';
162
      Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),
163
        "Boolean parsing error in command argument '%s'. Could not parse: %s.\n", _name, buf);
164
    }
165
  }
166
}
167

168
template <> void DCmdArgument<bool>::init_value(TRAPS) {
169
  if (has_default()) {
170
    this->parse_value(_default_string, strlen(_default_string), THREAD);
171
    if (HAS_PENDING_EXCEPTION) {
172
      fatal("Default string must be parsable");
173
    }
174
  } else {
175
    set_value(false);
176
  }
177
}
178

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

181
template <> void DCmdArgument<char*>::destroy_value() {
182
  FREE_C_HEAP_ARRAY(char, _value);
183
  set_value(nullptr);
184
}
185

186
template <> void DCmdArgument<char*>::parse_value(const char* str,
187
                                                  size_t len, TRAPS) {
188
  if (str == nullptr) {
189
    destroy_value();
190
  } else {
191
    // Use realloc as we may have a default set.
192
    _value = REALLOC_C_HEAP_ARRAY(char, _value, len + 1, mtInternal);
193
    int n = os::snprintf(_value, len + 1, "%.*s", (int)len, str);
194
    assert((size_t)n <= len, "Unexpected number of characters in string");
195
  }
196
}
197

198
template <> void DCmdArgument<char*>::init_value(TRAPS) {
199
  set_value(nullptr); // Must be initialized before calling parse_value
200
  if (has_default()) {
201
    this->parse_value(_default_string, strlen(_default_string), THREAD);
202
  }
203
}
204

205
template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,
206
                                                 size_t len, TRAPS) {
207
  if (str == nullptr) {
208
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
209
              "Integer parsing error nanotime value: syntax error, value is null\n");
210
  }
211

212
  int argc = sscanf(str, JLONG_FORMAT, &_value._time);
213
  if (argc != 1) {
214
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
215
              "Integer parsing error nanotime value: syntax error\n");
216
  }
217
  size_t idx = 0;
218
  while(idx < len && isdigit(str[idx])) {
219
    idx++;
220
  }
221
  if (idx == len) {
222
    // only accept missing unit if the value is 0
223
    if (_value._time != 0) {
224
      THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
225
                "Integer parsing error nanotime value: unit required\n");
226
    } else {
227
      _value._nanotime = 0;
228
      strcpy(_value._unit, "ns");
229
      return;
230
    }
231
  } else if(len - idx > 2) {
232
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
233
              "Integer parsing error nanotime value: illegal unit\n");
234
  } else {
235
    strncpy(_value._unit, &str[idx], len - idx);
236
    /*Write an extra null termination. This is safe because _value._unit
237
     * is declared as char[3], and length is checked to be not larger than
238
     * two above. Also, this is necessary, since length might be 1, and the
239
     * default value already in the string is ns, which is two chars.
240
     */
241
    _value._unit[len-idx] = '\0';
242
  }
243

244
  if (strcmp(_value._unit, "ns") == 0) {
245
    _value._nanotime = _value._time;
246
  } else if (strcmp(_value._unit, "us") == 0) {
247
    _value._nanotime = _value._time * 1000;
248
  } else if (strcmp(_value._unit, "ms") == 0) {
249
    _value._nanotime = _value._time * 1000 * 1000;
250
  } else if (strcmp(_value._unit, "s") == 0) {
251
    _value._nanotime = _value._time * 1000 * 1000 * 1000;
252
  } else if (strcmp(_value._unit, "m") == 0) {
253
    _value._nanotime = _value._time * 60 * 1000 * 1000 * 1000;
254
  } else if (strcmp(_value._unit, "h") == 0) {
255
    _value._nanotime = _value._time * 60 * 60 * 1000 * 1000 * 1000;
256
  } else if (strcmp(_value._unit, "d") == 0) {
257
    _value._nanotime = _value._time * 24 * 60 * 60 * 1000 * 1000 * 1000;
258
  } else {
259
     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
260
               "Integer parsing error nanotime value: illegal unit\n");
261
  }
262
}
263

264
template <> void DCmdArgument<NanoTimeArgument>::init_value(TRAPS) {
265
  if (has_default()) {
266
    this->parse_value(_default_string, strlen(_default_string), THREAD);
267
    if (HAS_PENDING_EXCEPTION) {
268
      fatal("Default string must be parsable");
269
    }
270
  } else {
271
    _value._time = 0;
272
    _value._nanotime = 0;
273
    strcpy(_value._unit, "ns");
274
  }
275
}
276

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

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

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

287
template <> void DCmdArgument<StringArrayArgument*>::init_value(TRAPS) {
288
  _value = new StringArrayArgument();
289
  _allow_multiple = true;
290
  if (has_default()) {
291
    fatal("StringArrayArgument cannot have default value");
292
  }
293
}
294

295
template <> void DCmdArgument<StringArrayArgument*>::destroy_value() {
296
  if (_value != nullptr) {
297
    delete _value;
298
    set_value(nullptr);
299
  }
300
}
301

302
template <> void DCmdArgument<MemorySizeArgument>::parse_value(const char* str,
303
                                                  size_t len, TRAPS) {
304
  if (str == nullptr) {
305
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
306
               "Parsing error memory size value: syntax error, value is null\n");
307
  }
308
  if (*str == '-') {
309
    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
310
               "Parsing error memory size value: negative values not allowed\n");
311
  }
312
  int res = sscanf(str, UINT64_FORMAT "%c", &_value._val, &_value._multiplier);
313
  if (res == 2) {
314
     switch (_value._multiplier) {
315
      case 'k': case 'K':
316
         _value._size = _value._val * 1024;
317
         break;
318
      case 'm': case 'M':
319
         _value._size = _value._val * 1024 * 1024;
320
         break;
321
      case 'g': case 'G':
322
         _value._size = _value._val * 1024 * 1024 * 1024;
323
         break;
324
       default:
325
         _value._size = _value._val;
326
         _value._multiplier = ' ';
327
         //default case should be to break with no error, since user
328
         //can write size in bytes, or might have a delimiter and next arg
329
         break;
330
     }
331
   } else if (res == 1) {
332
     _value._size = _value._val;
333
   } else {
334
     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
335
               "Parsing error memory size value: invalid value\n");
336
   }
337
}
338

339
template <> void DCmdArgument<MemorySizeArgument>::init_value(TRAPS) {
340
  if (has_default()) {
341
    this->parse_value(_default_string, strlen(_default_string), THREAD);
342
    if (HAS_PENDING_EXCEPTION) {
343
      fatal("Default string must be parsable");
344
    }
345
  } else {
346
    _value._size = 0;
347
    _value._val = 0;
348
    _value._multiplier = ' ';
349
  }
350
}
351

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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.