58 _args_len = line_end - _args;
59 }
60
61 bool DCmdArgIter::next(TRAPS) {
62 if (_len == 0) return false;
63 // skipping spaces
64 while (_cursor < _len - 1 && _buffer[_cursor] == _delim) {
65 _cursor++;
66 }
67 // handling end of command line
68 if (_cursor >= _len - 1) {
69 _cursor = _len - 1;
70 _key_addr = &_buffer[_len - 1];
71 _key_len = 0;
72 _value_addr = &_buffer[_len - 1];
73 _value_len = 0;
74 return false;
75 }
76 // extracting first item, argument or option name
77 _key_addr = &_buffer[_cursor];
78 while (_cursor <= _len - 1 && _buffer[_cursor] != '=' && _buffer[_cursor] != _delim) {
79 // argument can be surrounded by single or double quotes
80 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
81 _key_addr++;
82 char quote = _buffer[_cursor];
83 while (_cursor < _len - 1) {
84 _cursor++;
85 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
86 break;
87 }
88 }
89 if (_buffer[_cursor] != quote) {
90 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
91 "Format error in diagnostic command arguments", false);
92 }
93 break;
94 }
95 _cursor++;
96 }
97 _key_len = &_buffer[_cursor] - _key_addr;
98 // check if the argument has the <key>=<value> format
99 if (_cursor <= _len -1 && _buffer[_cursor] == '=') {
100 _cursor++;
101 _value_addr = &_buffer[_cursor];
102 // extract the value
103 while (_cursor <= _len - 1 && _buffer[_cursor] != _delim) {
104 // value can be surrounded by simple or double quotes
105 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
106 _value_addr++;
107 char quote = _buffer[_cursor];
108 while (_cursor < _len - 1) {
109 _cursor++;
110 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
111 break;
112 }
113 }
114 if (_buffer[_cursor] != quote) {
115 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
116 "Format error in diagnostic command arguments", false);
117 }
118 break;
119 }
120 _cursor++;
121 }
122 _value_len = &_buffer[_cursor] - _value_addr;
123 } else {
124 _value_addr = NULL;
125 _value_len = 0;
126 }
127 return _key_len != 0;
128 }
129
130 bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) {
131 if (info == NULL) return false;
132 return strcmp((const char*)cmd_name, info->name()) == 0;
133 }
134
135 void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) {
136 assert(arg != NULL, "Sanity");
137 if (_options == NULL) {
138 _options = arg;
139 } else {
140 GenDCmdArgument* o = _options;
141 while (o->next() != NULL) {
142 o = o->next();
168 if (HAS_PENDING_EXCEPTION) {
169 fatal("Initialization must be successful");
170 }
171 }
172
173 void DCmdParser::parse(CmdLine* line, char delim, TRAPS) {
174 GenDCmdArgument* next_argument = _arguments_list;
175 DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
176 bool cont = iter.next(CHECK);
177 while (cont) {
178 GenDCmdArgument* arg = lookup_dcmd_option(iter.key_addr(),
179 iter.key_length());
180 if (arg != NULL) {
181 arg->read_value(iter.value_addr(), iter.value_length(), CHECK);
182 } else {
183 if (next_argument != NULL) {
184 arg = next_argument;
185 arg->read_value(iter.key_addr(), iter.key_length(), CHECK);
186 next_argument = next_argument->next();
187 } else {
188 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
189 "Unknown argument in diagnostic command");
190 }
191 }
192 cont = iter.next(CHECK);
193 }
194 check(CHECK);
195 }
196
197 GenDCmdArgument* DCmdParser::lookup_dcmd_option(const char* name, size_t len) {
198 GenDCmdArgument* arg = _options;
199 while (arg != NULL) {
200 if (strlen(arg->name()) == len &&
201 strncmp(name, arg->name(), len) == 0) {
202 return arg;
203 }
204 arg = arg->next();
205 }
206 return NULL;
207 }
208
209 void DCmdParser::check(TRAPS) {
210 GenDCmdArgument* arg = _arguments_list;
211 while (arg != NULL) {
212 if (arg->is_mandatory() && !arg->has_value()) {
213 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
214 "Missing argument for diagnostic command");
215 }
216 arg = arg->next();
217 }
218 arg = _options;
219 while (arg != NULL) {
220 if (arg->is_mandatory() && !arg->has_value()) {
221 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
222 "Missing option for diagnostic command");
223 }
224 arg = arg->next();
225 }
226 }
227
228 void DCmdParser::print_help(outputStream* out, const char* cmd_name) {
229 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
230 GenDCmdArgument* arg = _arguments_list;
231 while (arg != NULL) {
232 if (arg->is_mandatory()) {
233 out->print(" <%s>", arg->name());
234 } else {
235 out->print(" [<%s>]", arg->name());
236 }
237 arg = arg->next();
238 }
239 out->print_cr("");
240 if (_arguments_list != NULL) {
241 out->print_cr("\nArguments:");
242 arg = _arguments_list;
|
58 _args_len = line_end - _args;
59 }
60
61 bool DCmdArgIter::next(TRAPS) {
62 if (_len == 0) return false;
63 // skipping spaces
64 while (_cursor < _len - 1 && _buffer[_cursor] == _delim) {
65 _cursor++;
66 }
67 // handling end of command line
68 if (_cursor >= _len - 1) {
69 _cursor = _len - 1;
70 _key_addr = &_buffer[_len - 1];
71 _key_len = 0;
72 _value_addr = &_buffer[_len - 1];
73 _value_len = 0;
74 return false;
75 }
76 // extracting first item, argument or option name
77 _key_addr = &_buffer[_cursor];
78 bool arg_had_quotes = false;
79 while (_cursor <= _len - 1 && _buffer[_cursor] != '=' && _buffer[_cursor] != _delim) {
80 // argument can be surrounded by single or double quotes
81 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
82 _key_addr++;
83 char quote = _buffer[_cursor];
84 arg_had_quotes = true;
85 while (_cursor < _len - 1) {
86 _cursor++;
87 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
88 break;
89 }
90 }
91 if (_buffer[_cursor] != quote) {
92 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
93 "Format error in diagnostic command arguments", false);
94 }
95 break;
96 }
97 _cursor++;
98 }
99 _key_len = &_buffer[_cursor] - _key_addr;
100 if (arg_had_quotes) {
101 // if the argument was quoted, we need to step past the last quote here
102 _cursor++;
103 }
104 // check if the argument has the <key>=<value> format
105 if (_cursor <= _len -1 && _buffer[_cursor] == '=') {
106 _cursor++;
107 _value_addr = &_buffer[_cursor];
108 bool value_had_quotes = false;
109 // extract the value
110 while (_cursor <= _len - 1 && _buffer[_cursor] != _delim) {
111 // value can be surrounded by simple or double quotes
112 if (_buffer[_cursor] == '\"' || _buffer[_cursor] == '\'') {
113 _value_addr++;
114 char quote = _buffer[_cursor];
115 value_had_quotes = true;
116 while (_cursor < _len - 1) {
117 _cursor++;
118 if (_buffer[_cursor] == quote && _buffer[_cursor - 1] != '\\') {
119 break;
120 }
121 }
122 if (_buffer[_cursor] != quote) {
123 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
124 "Format error in diagnostic command arguments", false);
125 }
126 break;
127 }
128 _cursor++;
129 }
130 _value_len = &_buffer[_cursor] - _value_addr;
131 if (value_had_quotes) {
132 // if the value was quoted, we need to step past the last quote here
133 _cursor++;
134 }
135 } else {
136 _value_addr = NULL;
137 _value_len = 0;
138 }
139 return _key_len != 0;
140 }
141
142 bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) {
143 if (info == NULL) return false;
144 return strcmp((const char*)cmd_name, info->name()) == 0;
145 }
146
147 void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) {
148 assert(arg != NULL, "Sanity");
149 if (_options == NULL) {
150 _options = arg;
151 } else {
152 GenDCmdArgument* o = _options;
153 while (o->next() != NULL) {
154 o = o->next();
180 if (HAS_PENDING_EXCEPTION) {
181 fatal("Initialization must be successful");
182 }
183 }
184
185 void DCmdParser::parse(CmdLine* line, char delim, TRAPS) {
186 GenDCmdArgument* next_argument = _arguments_list;
187 DCmdArgIter iter(line->args_addr(), line->args_len(), delim);
188 bool cont = iter.next(CHECK);
189 while (cont) {
190 GenDCmdArgument* arg = lookup_dcmd_option(iter.key_addr(),
191 iter.key_length());
192 if (arg != NULL) {
193 arg->read_value(iter.value_addr(), iter.value_length(), CHECK);
194 } else {
195 if (next_argument != NULL) {
196 arg = next_argument;
197 arg->read_value(iter.key_addr(), iter.key_length(), CHECK);
198 next_argument = next_argument->next();
199 } else {
200 size_t buflen = 120;
201 char buf[buflen];
202 size_t len = iter.key_length();
203 char argname[len + 1];
204
205 strncpy(argname, iter.key_addr(), len);
206 argname[len] = '\0';
207 jio_snprintf(buf, buflen - 1, "Unknown argument '%s' in diagnostic command.", argname);
208
209 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
210 }
211 }
212 cont = iter.next(CHECK);
213 }
214 check(CHECK);
215 }
216
217 GenDCmdArgument* DCmdParser::lookup_dcmd_option(const char* name, size_t len) {
218 GenDCmdArgument* arg = _options;
219 while (arg != NULL) {
220 if (strlen(arg->name()) == len &&
221 strncmp(name, arg->name(), len) == 0) {
222 return arg;
223 }
224 arg = arg->next();
225 }
226 return NULL;
227 }
228
229 void DCmdParser::check(TRAPS) {
230 size_t buflen = 256;
231 char buf[buflen];
232 GenDCmdArgument* arg = _arguments_list;
233 while (arg != NULL) {
234 if (arg->is_mandatory() && !arg->has_value()) {
235 snprintf(buf, buflen - 1, "The argument '%s' is mandatory.", arg->name());
236 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
237 }
238 arg = arg->next();
239 }
240 arg = _options;
241 while (arg != NULL) {
242 if (arg->is_mandatory() && !arg->has_value()) {
243 snprintf(buf, buflen - 1, "The option '%s' is mandatory.", arg->name());
244 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), buf);
245 }
246 arg = arg->next();
247 }
248 }
249
250 void DCmdParser::print_help(outputStream* out, const char* cmd_name) {
251 out->print("Syntax : %s %s", cmd_name, _options == NULL ? "" : "[options]");
252 GenDCmdArgument* arg = _arguments_list;
253 while (arg != NULL) {
254 if (arg->is_mandatory()) {
255 out->print(" <%s>", arg->name());
256 } else {
257 out->print(" [<%s>]", arg->name());
258 }
259 arg = arg->next();
260 }
261 out->print_cr("");
262 if (_arguments_list != NULL) {
263 out->print_cr("\nArguments:");
264 arg = _arguments_list;
|