GDB (xrefs)
Loading...
Searching...
No Matches
mi-parse.c
Go to the documentation of this file.
1/* MI Command Set - MI parser.
2
3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions (a Red Hat company).
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "mi-cmds.h"
24#include "mi-parse.h"
25#include "charset.h"
26
27#include <ctype.h>
28#include "cli/cli-utils.h"
29#include "language.h"
30
31static const char mi_no_values[] = "--no-values";
32static const char mi_simple_values[] = "--simple-values";
33static const char mi_all_values[] = "--all-values";
34
35/* Like parse_escape, but leave the results as a host char, not a
36 target char. */
37
38static int
39mi_parse_escape (const char **string_ptr)
40{
41 int c = *(*string_ptr)++;
42
43 switch (c)
44 {
45 case '\n':
46 return -2;
47 case 0:
48 (*string_ptr)--;
49 return 0;
50
51 case '0':
52 case '1':
53 case '2':
54 case '3':
55 case '4':
56 case '5':
57 case '6':
58 case '7':
59 {
60 int i = fromhex (c);
61 int count = 0;
62
63 while (++count < 3)
64 {
65 c = (**string_ptr);
66 if (isdigit (c) && c != '8' && c != '9')
67 {
68 (*string_ptr)++;
69 i *= 8;
70 i += fromhex (c);
71 }
72 else
73 {
74 break;
75 }
76 }
77 return i;
78 }
79
80 case 'a':
81 c = '\a';
82 break;
83 case 'b':
84 c = '\b';
85 break;
86 case 'f':
87 c = '\f';
88 break;
89 case 'n':
90 c = '\n';
91 break;
92 case 'r':
93 c = '\r';
94 break;
95 case 't':
96 c = '\t';
97 break;
98 case 'v':
99 c = '\v';
100 break;
101
102 default:
103 break;
104 }
105
106 return c;
107}
108
109void
111{
112 /* If arguments were already computed (or were supplied at
113 construction), then there's no need to re-compute them. */
114 if (argv != nullptr)
115 return;
116
117 const char *chp = m_args.c_str ();
118 int argc = 0;
119 char **argv = XNEWVEC (char *, argc + 1);
120
121 argv[argc] = NULL;
122 while (1)
123 {
124 char *arg;
125
126 /* Skip leading white space. */
127 chp = skip_spaces (chp);
128 /* Three possibilities: EOF, quoted string, or other text. */
129 switch (*chp)
130 {
131 case '\0':
132 this->argv = argv;
133 this->argc = argc;
134 return;
135 case '"':
136 {
137 /* A quoted string. */
138 int len;
139 const char *start = chp + 1;
140
141 /* Determine the buffer size. */
142 chp = start;
143 len = 0;
144 while (*chp != '\0' && *chp != '"')
145 {
146 if (*chp == '\\')
147 {
148 chp++;
149 if (mi_parse_escape (&chp) <= 0)
150 {
151 /* Do not allow split lines or "\000". */
152 freeargv (argv);
153 return;
154 }
155 }
156 else
157 chp++;
158 len++;
159 }
160 /* Insist on a closing quote. */
161 if (*chp != '"')
162 {
163 freeargv (argv);
164 return;
165 }
166 /* Insist on trailing white space. */
167 if (chp[1] != '\0' && !isspace (chp[1]))
168 {
169 freeargv (argv);
170 return;
171 }
172 /* Create the buffer and copy characters in. */
173 arg = XNEWVEC (char, len + 1);
174 chp = start;
175 len = 0;
176 while (*chp != '\0' && *chp != '"')
177 {
178 if (*chp == '\\')
179 {
180 chp++;
181 arg[len] = mi_parse_escape (&chp);
182 }
183 else
184 arg[len] = *chp++;
185 len++;
186 }
187 arg[len] = '\0';
188 chp++; /* That closing quote. */
189 break;
190 }
191 default:
192 {
193 /* An unquoted string. Accumulate all non-blank
194 characters into a buffer. */
195 int len;
196 const char *start = chp;
197
198 while (*chp != '\0' && !isspace (*chp))
199 {
200 chp++;
201 }
202 len = chp - start;
203 arg = XNEWVEC (char, len + 1);
204 strncpy (arg, start, len);
205 arg[len] = '\0';
206 break;
207 }
208 }
209 /* Append arg to argv. */
210 argv = XRESIZEVEC (char *, argv, argc + 2);
211 argv[argc++] = arg;
212 argv[argc] = NULL;
213 }
214}
215
217{
218 freeargv (argv);
219}
220
221/* See mi-parse.h. */
222
223const char *
225{
226 /* If args were already computed, or if there is no pre-computed
227 argv, just return the args. */
228 if (!m_args.empty () || argv == nullptr)
229 return m_args.c_str ();
230
231 /* Compute args from argv. */
232 for (int i = 0; i < argc; ++i)
233 {
234 if (!m_args.empty ())
235 m_args += " ";
236 m_args += argv[i];
237 }
238
239 return m_args.c_str ();
240}
241
242/* See mi-parse.h. */
243
244void
245mi_parse::set_thread_group (const char *arg, char **endp)
246{
247 if (thread_group != -1)
248 error (_("Duplicate '--thread-group' option"));
249 if (*arg != 'i')
250 error (_("Invalid thread group id"));
251 arg += 1;
252 thread_group = strtol (arg, endp, 10);
253}
254
255/* See mi-parse.h. */
256
257void
258mi_parse::set_thread (const char *arg, char **endp)
259{
260 if (thread != -1)
261 error (_("Duplicate '--thread' option"));
262 thread = strtol (arg, endp, 10);
263}
264
265/* See mi-parse.h. */
266
267void
268mi_parse::set_frame (const char *arg, char **endp)
269{
270 if (frame != -1)
271 error (_("Duplicate '--frame' option"));
272 frame = strtol (arg, endp, 10);
273}
274
275/* See mi-parse.h. */
276
277void
278mi_parse::set_language (const char *arg, const char **endp)
279{
280 std::string lang_name = extract_arg (&arg);
281
282 language = language_enum (lang_name.c_str ());
284 error (_("Invalid --language argument: %s"), lang_name.c_str ());
285
286 if (endp != nullptr)
287 *endp = arg;
288}
289
290/* See mi-parse.h. */
291
292mi_parse::mi_parse (const char *cmd, std::string *token)
293{
294 const char *chp;
295
296 /* Before starting, skip leading white space. */
297 cmd = skip_spaces (cmd);
298
299 /* Find/skip any token and then extract it. */
300 for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++)
301 ;
302 *token = std::string (cmd, chp - cmd);
303
304 /* This wasn't a real MI command. Return it as a CLI_COMMAND. */
305 if (*chp != '-')
306 {
307 chp = skip_spaces (chp);
308 this->command = make_unique_xstrdup (chp);
309 this->op = CLI_COMMAND;
310
311 return;
312 }
313
314 /* Extract the command. */
315 {
316 const char *tmp = chp + 1; /* discard ``-'' */
317
318 for (; *chp && !isspace (*chp); chp++)
319 ;
320 this->command = make_unique_xstrndup (tmp, chp - tmp);
321 }
322
323 /* Find the command in the MI table. */
324 this->cmd = mi_cmd_lookup (this->command.get ());
325 if (this->cmd == NULL)
326 throw_error (UNDEFINED_COMMAND_ERROR,
327 _("Undefined MI command: %s"), this->command.get ());
328
329 /* Skip white space following the command. */
330 chp = skip_spaces (chp);
331
332 /* Parse the --thread and --frame options, if present. At present,
333 some important commands, like '-break-*' are implemented by
334 forwarding to the CLI layer directly. We want to parse --thread
335 and --frame here, so as not to leave those option in the string
336 that will be passed to CLI.
337
338 Same for the --language option. */
339
340 for (;;)
341 {
342 const char *option;
343 size_t as = sizeof ("--all ") - 1;
344 size_t tgs = sizeof ("--thread-group ") - 1;
345 size_t ts = sizeof ("--thread ") - 1;
346 size_t fs = sizeof ("--frame ") - 1;
347 size_t ls = sizeof ("--language ") - 1;
348
349 if (strncmp (chp, "--all ", as) == 0)
350 {
351 this->all = 1;
352 chp += as;
353 }
354 /* See if --all is the last token in the input. */
355 if (strcmp (chp, "--all") == 0)
356 {
357 this->all = 1;
358 chp += strlen (chp);
359 }
360 if (strncmp (chp, "--thread-group ", tgs) == 0)
361 {
362 char *endp;
363
364 option = "--thread-group";
365 chp += tgs;
366 this->set_thread_group (chp, &endp);
367 chp = endp;
368 }
369 else if (strncmp (chp, "--thread ", ts) == 0)
370 {
371 char *endp;
372
373 option = "--thread";
374 chp += ts;
375 this->set_thread (chp, &endp);
376 chp = endp;
377 }
378 else if (strncmp (chp, "--frame ", fs) == 0)
379 {
380 char *endp;
381
382 option = "--frame";
383 chp += fs;
384 this->set_frame (chp, &endp);
385 chp = endp;
386 }
387 else if (strncmp (chp, "--language ", ls) == 0)
388 {
389 option = "--language";
390 chp += ls;
391 this->set_language (chp, &chp);
392 }
393 else
394 break;
395
396 if (*chp != '\0' && !isspace (*chp))
397 error (_("Invalid value for the '%s' option"), option);
398 chp = skip_spaces (chp);
399 }
400
401 /* Save the rest of the arguments for the command. */
402 this->m_args = chp;
403
404 /* Fully parsed, flag as an MI command. */
405 this->op = MI_COMMAND;
406}
407
408/* See mi-parse.h. */
409
410mi_parse::mi_parse (gdb::unique_xmalloc_ptr<char> command,
411 std::vector<gdb::unique_xmalloc_ptr<char>> args)
412{
413 this->command = std::move (command);
414 this->token = "";
415
416 if (this->command.get ()[0] != '-')
417 throw_error (UNDEFINED_COMMAND_ERROR,
418 _("MI command '%s' does not start with '-'"),
419 this->command.get ());
420
421 /* Find the command in the MI table. */
422 this->cmd = mi_cmd_lookup (this->command.get () + 1);
423 if (this->cmd == NULL)
424 throw_error (UNDEFINED_COMMAND_ERROR,
425 _("Undefined MI command: %s"), this->command.get ());
426
427 /* This over-allocates slightly, but it seems unimportant. */
428 this->argv = XCNEWVEC (char *, args.size () + 1);
429
430 for (size_t i = 0; i < args.size (); ++i)
431 {
432 const char *chp = args[i].get ();
433
434 /* See if --all is the last token in the input. */
435 if (strcmp (chp, "--all") == 0)
436 {
437 this->all = 1;
438 }
439 else if (strcmp (chp, "--thread-group") == 0)
440 {
441 ++i;
442 if (i == args.size ())
443 error ("No argument to '--thread-group'");
444 this->set_thread_group (args[i].get (), nullptr);
445 }
446 else if (strcmp (chp, "--thread") == 0)
447 {
448 ++i;
449 if (i == args.size ())
450 error ("No argument to '--thread'");
451 this->set_thread (args[i].get (), nullptr);
452 }
453 else if (strcmp (chp, "--frame") == 0)
454 {
455 ++i;
456 if (i == args.size ())
457 error ("No argument to '--frame'");
458 this->set_frame (args[i].get (), nullptr);
459 }
460 else if (strcmp (chp, "--language") == 0)
461 {
462 ++i;
463 if (i == args.size ())
464 error ("No argument to '--language'");
465 this->set_language (args[i].get (), nullptr);
466 }
467 else
468 this->argv[this->argc++] = args[i].release ();
469 }
470
471 /* Fully parsed, flag as an MI command. */
472 this->op = MI_COMMAND;
473}
474
475enum print_values
477{
478 if (strcmp (name, "0") == 0
479 || strcmp (name, mi_no_values) == 0)
480 return PRINT_NO_VALUES;
481 else if (strcmp (name, "1") == 0
482 || strcmp (name, mi_all_values) == 0)
483 return PRINT_ALL_VALUES;
484 else if (strcmp (name, "2") == 0
485 || strcmp (name, mi_simple_values) == 0)
486 return PRINT_SIMPLE_VALUES;
487 else
488 error (_("Unknown value for PRINT_VALUES: must be: \
4890 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
491}
constexpr string_view get()
Definition 70483.cc:49
const char *const name
std::string extract_arg(const char **arg)
Definition cli-utils.c:383
language
Definition defs.h:211
@ language_unknown
Definition defs.h:212
enum language language_enum(const char *str)
Definition language.c:427
int token
Definition m2-exp.c:2422
mi_command * mi_cmd_lookup(const char *command)
Definition mi-cmds.c:364
print_values
Definition mi-cmds.h:29
@ PRINT_SIMPLE_VALUES
Definition mi-cmds.h:32
@ PRINT_ALL_VALUES
Definition mi-cmds.h:31
@ PRINT_NO_VALUES
Definition mi-cmds.h:30
static const char mi_no_values[]
Definition mi-parse.c:31
static const char mi_simple_values[]
Definition mi-parse.c:32
static const char mi_all_values[]
Definition mi-parse.c:33
static int mi_parse_escape(const char **string_ptr)
Definition mi-parse.c:39
enum print_values mi_parse_print_values(const char *name)
Definition mi-parse.c:476
@ MI_COMMAND
Definition mi-parse.h:39
@ CLI_COMMAND
Definition mi-parse.h:39
void set_thread_group(const char *arg, char **endp)
Definition mi-parse.c:245
char ** argv
Definition mi-parse.h:75
int all
Definition mi-parse.h:77
std::string m_args
Definition mi-parse.h:97
mi_parse(const char *cmd, std::string *token)
Definition mi-parse.c:292
int argc
Definition mi-parse.h:76
void parse_argv()
Definition mi-parse.c:110
int thread_group
Definition mi-parse.h:78
void set_thread(const char *arg, char **endp)
Definition mi-parse.c:258
void set_frame(const char *arg, char **endp)
Definition mi-parse.c:268
gdb::unique_xmalloc_ptr< char > command
Definition mi-parse.h:71
std::string token
Definition mi-parse.h:72
int frame
Definition mi-parse.h:80
const char * args()
Definition mi-parse.c:224
enum mi_command_type op
Definition mi-parse.h:68
void set_language(const char *arg, const char **endp)
Definition mi-parse.c:278
~mi_parse()
Definition mi-parse.c:216
int thread
Definition mi-parse.h:79
const struct mi_command * cmd
Definition mi-parse.h:73