diagnosticArgument.hpp revision 7462:a0dd995271c4
1101242Srwatson/*
2101242Srwatson * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3107603Sru * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4101242Srwatson *
5101242Srwatson * This code is free software; you can redistribute it and/or modify it
6101242Srwatson * under the terms of the GNU General Public License version 2 only, as
7101242Srwatson * published by the Free Software Foundation.
8101242Srwatson *
9107603Sru * This code is distributed in the hope that it will be useful, but WITHOUT
10101242Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11101242Srwatson * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12101242Srwatson * version 2 for more details (a copy is included in the LICENSE file that
13101242Srwatson * accompanied this code).
14101242Srwatson *
15101242Srwatson * You should have received a copy of the GNU General Public License version
16101242Srwatson * 2 along with this work; if not, write to the Free Software Foundation,
17101242Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18107603Sru *
19101242Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20101242Srwatson * or visit www.oracle.com if you need additional information or have any
21101242Srwatson * questions.
22101242Srwatson *
23101242Srwatson */
24101242Srwatson
25101242Srwatson#ifndef SHARE_VM_SERVICES_DIAGNOSTICARGUMENT_HPP
26101242Srwatson#define SHARE_VM_SERVICES_DIAGNOSTICARGUMENT_HPP
27101242Srwatson
28101242Srwatson#include "classfile/vmSymbols.hpp"
29101242Srwatson#include "memory/allocation.hpp"
30107603Sru#include "runtime/os.hpp"
31101242Srwatson#include "runtime/thread.hpp"
32107603Sru#include "utilities/exceptions.hpp"
33162841Sru
34162841Sruclass StringArrayArgument : public CHeapObj<mtInternal> {
35107744Sruprivate:
36101242Srwatson  GrowableArray<char*>* _array;
37160154Srwatsonpublic:
38101242Srwatson  StringArrayArgument() {
39107603Sru    _array = new(ResourceObj::C_HEAP, mtInternal)GrowableArray<char *>(32, true);
40107603Sru    assert(_array != NULL, "Sanity check");
41101242Srwatson  }
42101242Srwatson  void add(const char* str, size_t len) {
43101242Srwatson    if (str != NULL) {
44101242Srwatson      char* ptr = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
45101242Srwatson      strncpy(ptr, str, len);
46101242Srwatson      ptr[len] = 0;
47160154Srwatson      _array->append(ptr);
48101242Srwatson    }
49101242Srwatson  }
50101242Srwatson  GrowableArray<char*>* array() {
51101242Srwatson    return _array;
52107603Sru  }
53162841Sru  ~StringArrayArgument() {
54101242Srwatson    for (int i=0; i<_array->length(); i++) {
55107603Sru      if(_array->at(i) != NULL) { // Safety check
56107603Sru        FREE_C_HEAP_ARRAY(char, _array->at(i));
57107603Sru      }
58107603Sru    }
59101242Srwatson    delete _array;
60101242Srwatson  }
61101242Srwatson};
62101242Srwatson
63107603Sruclass NanoTimeArgument {
64101242Srwatsonpublic:
65101242Srwatson  jlong _nanotime;
66101242Srwatson  jlong _time;
67101242Srwatson  char _unit[3];
68101242Srwatson};
69101242Srwatson
70101242Srwatsonclass MemorySizeArgument {
71101242Srwatsonpublic:
72101242Srwatson  u8 _size;
73101242Srwatson  u8 _val;
74109263Schris  char _multiplier;
75109263Schris};
76109263Schris
77109263Schrisclass GenDCmdArgument : public ResourceObj {
78109263Schrisprotected:
79109263Schris  GenDCmdArgument* _next;
80109273Schris  const char*      _name;
81109263Schris  const char*      _description;
82119321Srwatson  const char*      _type;
83119321Srwatson  const char*      _default_string;
84119321Srwatson  bool             _is_set;
85119321Srwatson  bool             _is_mandatory;
86119321Srwatson  bool             _allow_multiple;
87119321Srwatson  GenDCmdArgument(const char* name, const char* description, const char* type,
88                  const char* default_string, bool mandatory) {
89    _name = name;
90    _description = description;
91    _type = type;
92    _default_string = default_string;
93    _is_mandatory = mandatory;
94    _is_set = false;
95    _allow_multiple = false;
96  };
97public:
98  const char* name() { return _name; }
99  const char* description() { return _description; }
100  const char* type() { return _type; }
101  const char* default_string() { return _default_string; }
102  bool is_set() { return _is_set; }
103  void set_is_set(bool b) { _is_set = b; }
104  bool allow_multiple() { return _allow_multiple; }
105  bool is_mandatory() { return _is_mandatory; }
106  bool has_value() { return _is_set || _default_string != NULL; }
107  bool has_default() { return _default_string != NULL; }
108  void read_value(const char* str, size_t len, TRAPS);
109  virtual void parse_value(const char* str, size_t len, TRAPS) = 0;
110  virtual void init_value(TRAPS) = 0;
111  virtual void reset(TRAPS) = 0;
112  virtual void cleanup() = 0;
113  virtual void value_as_str(char* buf, size_t len) = 0;
114  void set_next(GenDCmdArgument* arg) {
115    _next = arg;
116  }
117  GenDCmdArgument* next() {
118    return _next;
119  }
120
121  void to_string(jlong l, char* buf, size_t len);
122  void to_string(bool b, char* buf, size_t len);
123  void to_string(char* c, char* buf, size_t len);
124  void to_string(NanoTimeArgument n, char* buf, size_t len);
125  void to_string(MemorySizeArgument f, char* buf, size_t len);
126  void to_string(StringArrayArgument* s, char* buf, size_t len);
127};
128
129template <class ArgType> class DCmdArgument: public GenDCmdArgument {
130private:
131  ArgType _value;
132public:
133  DCmdArgument(const char* name, const char* description, const char* type,
134               bool mandatory) :
135               GenDCmdArgument(name, description, type, NULL, mandatory) { }
136  DCmdArgument(const char* name, const char* description, const char* type,
137               bool mandatory, const char* defaultvalue) :
138               GenDCmdArgument(name, description, type, defaultvalue, mandatory)
139               { }
140  ~DCmdArgument() { destroy_value(); }
141  ArgType value() { return _value;}
142  void set_value(ArgType v) { _value = v; }
143  void reset(TRAPS) {
144    destroy_value();
145    init_value(CHECK);
146    _is_set = false;
147  }
148  void cleanup() {
149    destroy_value();
150  }
151  void parse_value(const char* str, size_t len, TRAPS);
152  void init_value(TRAPS);
153  void destroy_value();
154  void value_as_str(char *buf, size_t len) { return to_string(_value, buf, len);}
155};
156
157#endif  /* SHARE_VM_SERVICES_DIAGNOSTICARGUMENT_HPP */
158