]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/macrocmd.c
Fix leak of identifier in macro definition.
[thirdparty/binutils-gdb.git] / gdb / macrocmd.c
1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "defs.h"
22 #include "macrotab.h"
23 #include "macroexp.h"
24 #include "macroscope.h"
25 #include "cli/cli-utils.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "linespec.h"
29
30 \f
31 /* The `macro' prefix command. */
32
33 static struct cmd_list_element *macrolist;
34
35 static void
36 macro_command (const char *arg, int from_tty)
37 {
38 printf_unfiltered
39 ("\"macro\" must be followed by the name of a macro command.\n");
40 help_list (macrolist, "macro ", all_commands, gdb_stdout);
41 }
42
43
44 \f
45 /* Macro expansion commands. */
46
47
48 /* Prints an informational message regarding the lack of macro information. */
49 static void
50 macro_inform_no_debuginfo (void)
51 {
52 puts_filtered ("GDB has no preprocessor macro information for that code.\n");
53 }
54
55 static void
56 macro_expand_command (const char *exp, int from_tty)
57 {
58 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
59 gdb::unique_xmalloc_ptr<char> expanded;
60
61 /* You know, when the user doesn't specify any expression, it would be
62 really cool if this defaulted to the last expression evaluated.
63 Then it would be easy to ask, "Hey, what did I just evaluate?" But
64 at the moment, the `print' commands don't save the last expression
65 evaluated, just its value. */
66 if (! exp || ! *exp)
67 error (_("You must follow the `macro expand' command with the"
68 " expression you\n"
69 "want to expand."));
70
71 ms = default_macro_scope ();
72 if (ms)
73 {
74 expanded = macro_expand (exp, standard_macro_lookup, ms.get ());
75 fputs_filtered ("expands to: ", gdb_stdout);
76 fputs_filtered (expanded.get (), gdb_stdout);
77 fputs_filtered ("\n", gdb_stdout);
78 }
79 else
80 macro_inform_no_debuginfo ();
81 }
82
83
84 static void
85 macro_expand_once_command (const char *exp, int from_tty)
86 {
87 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
88 gdb::unique_xmalloc_ptr<char> expanded;
89
90 /* You know, when the user doesn't specify any expression, it would be
91 really cool if this defaulted to the last expression evaluated.
92 And it should set the once-expanded text as the new `last
93 expression'. That way, you could just hit return over and over and
94 see the expression expanded one level at a time. */
95 if (! exp || ! *exp)
96 error (_("You must follow the `macro expand-once' command with"
97 " the expression\n"
98 "you want to expand."));
99
100 ms = default_macro_scope ();
101 if (ms)
102 {
103 expanded = macro_expand_once (exp, standard_macro_lookup, ms.get ());
104 fputs_filtered ("expands to: ", gdb_stdout);
105 fputs_filtered (expanded.get (), gdb_stdout);
106 fputs_filtered ("\n", gdb_stdout);
107 }
108 else
109 macro_inform_no_debuginfo ();
110 }
111
112 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
113
114 Care should be taken that this function does not cause any lookups into
115 the splay tree so that it can be safely used while iterating. */
116 static void
117 show_pp_source_pos (struct ui_file *stream,
118 struct macro_source_file *file,
119 int line)
120 {
121 char *fullname;
122
123 fullname = macro_source_fullname (file);
124 fprintf_filtered (stream, "%s:%d\n", fullname, line);
125 xfree (fullname);
126
127 while (file->included_by)
128 {
129 fullname = macro_source_fullname (file->included_by);
130 fprintf_filtered (gdb_stdout, " included at %s:%d\n", fullname,
131 file->included_at_line);
132 xfree (fullname);
133 file = file->included_by;
134 }
135 }
136
137 /* Outputs a macro for human consumption, detailing the include path
138 and macro definition. NAME is the name of the macro.
139 D the definition. FILE the start of the include path, and LINE the
140 line number in FILE.
141
142 Care should be taken that this function does not cause any lookups into
143 the splay tree so that it can be safely used while iterating. */
144 static void
145 print_macro_definition (const char *name,
146 const struct macro_definition *d,
147 struct macro_source_file *file,
148 int line)
149 {
150 fprintf_filtered (gdb_stdout, "Defined at ");
151 show_pp_source_pos (gdb_stdout, file, line);
152
153 if (line != 0)
154 fprintf_filtered (gdb_stdout, "#define %s", name);
155 else
156 fprintf_filtered (gdb_stdout, "-D%s", name);
157
158 if (d->kind == macro_function_like)
159 {
160 int i;
161
162 fputs_filtered ("(", gdb_stdout);
163 for (i = 0; i < d->argc; i++)
164 {
165 fputs_filtered (d->argv[i], gdb_stdout);
166 if (i + 1 < d->argc)
167 fputs_filtered (", ", gdb_stdout);
168 }
169 fputs_filtered (")", gdb_stdout);
170 }
171
172 if (line != 0)
173 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
174 else
175 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
176 }
177
178 /* The implementation of the `info macro' command. */
179 static void
180 info_macro_command (const char *args, int from_tty)
181 {
182 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
183 const char *name;
184 int show_all_macros_named = 0;
185 const char *arg_start = args;
186 int processing_args = 1;
187
188 while (processing_args
189 && arg_start && *arg_start == '-' && *arg_start != '\0')
190 {
191 const char *p = skip_to_space (arg_start);
192
193 if (strncmp (arg_start, "-a", p - arg_start) == 0
194 || strncmp (arg_start, "-all", p - arg_start) == 0)
195 show_all_macros_named = 1;
196 else if (strncmp (arg_start, "--", p - arg_start) == 0)
197 /* Our macro support seems rather C specific but this would
198 seem necessary for languages allowing - in macro names.
199 e.g. Scheme's (defmacro ->foo () "bar\n") */
200 processing_args = 0;
201 else
202 report_unrecognized_option_error ("info macro", arg_start);
203
204 arg_start = skip_spaces (p);
205 }
206
207 name = arg_start;
208
209 if (! name || ! *name)
210 error (_("You must follow the `info macro' command with the name"
211 " of the macro\n"
212 "whose definition you want to see."));
213
214 ms = default_macro_scope ();
215
216 if (! ms)
217 macro_inform_no_debuginfo ();
218 else if (show_all_macros_named)
219 macro_for_each (ms->file->table, [&] (const char *macro_name,
220 const macro_definition *macro,
221 macro_source_file *source,
222 int line)
223 {
224 if (strcmp (name, macro_name) == 0)
225 print_macro_definition (name, macro, source, line);
226 });
227 else
228 {
229 struct macro_definition *d;
230
231 d = macro_lookup_definition (ms->file, ms->line, name);
232 if (d)
233 {
234 int line;
235 struct macro_source_file *file
236 = macro_definition_location (ms->file, ms->line, name, &line);
237
238 print_macro_definition (name, d, file, line);
239 }
240 else
241 {
242 fprintf_filtered (gdb_stdout,
243 "The symbol `%s' has no definition as a C/C++"
244 " preprocessor macro\n"
245 "at ", name);
246 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
247 }
248 }
249 }
250
251 /* Implementation of the "info macros" command. */
252 static void
253 info_macros_command (const char *args, int from_tty)
254 {
255 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
256
257 if (args == NULL)
258 ms = default_macro_scope ();
259 else
260 {
261 std::vector<symtab_and_line> sals
262 = decode_line_with_current_source (args, 0);
263
264 if (!sals.empty ())
265 ms = sal_macro_scope (sals[0]);
266 }
267
268 if (! ms || ! ms->file || ! ms->file->table)
269 macro_inform_no_debuginfo ();
270 else
271 macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
272 }
273
274 \f
275 /* User-defined macros. */
276
277 static void
278 skip_ws (const char **expp)
279 {
280 while (macro_is_whitespace (**expp))
281 ++*expp;
282 }
283
284 /* Try to find the bounds of an identifier. If an identifier is
285 found, returns a newly allocated string; otherwise returns NULL.
286 EXPP is a pointer to an input string; it is updated to point to the
287 text following the identifier. If IS_PARAMETER is true, this
288 function will also allow "..." forms as used in varargs macro
289 parameters. */
290
291 static gdb::unique_xmalloc_ptr<char>
292 extract_identifier (const char **expp, int is_parameter)
293 {
294 char *result;
295 const char *p = *expp;
296 unsigned int len;
297
298 if (is_parameter && startswith (p, "..."))
299 {
300 /* Ok. */
301 }
302 else
303 {
304 if (! *p || ! macro_is_identifier_nondigit (*p))
305 return NULL;
306 for (++p;
307 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
308 ++p)
309 ;
310 }
311
312 if (is_parameter && startswith (p, "..."))
313 p += 3;
314
315 len = p - *expp;
316 result = (char *) xmalloc (len + 1);
317 memcpy (result, *expp, len);
318 result[len] = '\0';
319 *expp += len;
320 return gdb::unique_xmalloc_ptr<char> (result);
321 }
322
323 struct temporary_macro_definition : public macro_definition
324 {
325 temporary_macro_definition ()
326 {
327 table = nullptr;
328 kind = macro_object_like;
329 argc = 0;
330 argv = nullptr;
331 replacement = nullptr;
332 }
333
334 ~temporary_macro_definition ()
335 {
336 int i;
337
338 for (i = 0; i < argc; ++i)
339 xfree ((char *) argv[i]);
340 xfree ((char *) argv);
341 /* Note that the 'replacement' field is not allocated. */
342 }
343 };
344
345 static void
346 macro_define_command (const char *exp, int from_tty)
347 {
348 temporary_macro_definition new_macro;
349
350 if (!exp)
351 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
352
353 skip_ws (&exp);
354 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
355 if (name == NULL)
356 error (_("Invalid macro name."));
357 if (*exp == '(')
358 {
359 /* Function-like macro. */
360 int alloced = 5;
361 char **argv = XNEWVEC (char *, alloced);
362
363 new_macro.kind = macro_function_like;
364 new_macro.argc = 0;
365 new_macro.argv = (const char * const *) argv;
366
367 /* Skip the '(' and whitespace. */
368 ++exp;
369 skip_ws (&exp);
370
371 while (*exp != ')')
372 {
373 int i;
374
375 if (new_macro.argc == alloced)
376 {
377 alloced *= 2;
378 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
379 /* Must update new_macro as well... */
380 new_macro.argv = (const char * const *) argv;
381 }
382 argv[new_macro.argc] = extract_identifier (&exp, 1).release ();
383 if (! argv[new_macro.argc])
384 error (_("Macro is missing an argument."));
385 ++new_macro.argc;
386
387 for (i = new_macro.argc - 2; i >= 0; --i)
388 {
389 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
390 error (_("Two macro arguments with identical names."));
391 }
392
393 skip_ws (&exp);
394 if (*exp == ',')
395 {
396 ++exp;
397 skip_ws (&exp);
398 }
399 else if (*exp != ')')
400 error (_("',' or ')' expected at end of macro arguments."));
401 }
402 /* Skip the closing paren. */
403 ++exp;
404 skip_ws (&exp);
405
406 macro_define_function (macro_main (macro_user_macros), -1, name.get (),
407 new_macro.argc, (const char **) new_macro.argv,
408 exp);
409 }
410 else
411 {
412 skip_ws (&exp);
413 macro_define_object (macro_main (macro_user_macros), -1, name.get (),
414 exp);
415 }
416 }
417
418
419 static void
420 macro_undef_command (const char *exp, int from_tty)
421 {
422 if (!exp)
423 error (_("usage: macro undef NAME"));
424
425 skip_ws (&exp);
426 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
427 if (name == nullptr)
428 error (_("Invalid macro name."));
429 macro_undef (macro_main (macro_user_macros), -1, name.get ());
430 }
431
432
433 static void
434 print_one_macro (const char *name, const struct macro_definition *macro,
435 struct macro_source_file *source, int line)
436 {
437 fprintf_filtered (gdb_stdout, "macro define %s", name);
438 if (macro->kind == macro_function_like)
439 {
440 int i;
441
442 fprintf_filtered (gdb_stdout, "(");
443 for (i = 0; i < macro->argc; ++i)
444 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
445 macro->argv[i]);
446 fprintf_filtered (gdb_stdout, ")");
447 }
448 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
449 }
450
451
452 static void
453 macro_list_command (const char *exp, int from_tty)
454 {
455 macro_for_each (macro_user_macros, print_one_macro);
456 }
457
458 /* Initializing the `macrocmd' module. */
459
460 void
461 _initialize_macrocmd (void)
462 {
463 /* We introduce a new command prefix, `macro', under which we'll put
464 the various commands for working with preprocessor macros. */
465 add_prefix_cmd ("macro", class_info, macro_command,
466 _("Prefix for commands dealing with C preprocessor macros."),
467 &macrolist, "macro ", 0, &cmdlist);
468
469 add_cmd ("expand", no_class, macro_expand_command, _("\
470 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
471 Show the expanded expression."),
472 &macrolist);
473 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
474 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
475 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
476 Show the expanded expression.\n\
477 \n\
478 This command differs from `macro expand' in that it only expands macro\n\
479 invocations that appear directly in EXPRESSION; if expanding a macro\n\
480 introduces further macro invocations, those are left unexpanded.\n\
481 \n\
482 `macro expand-once' helps you see how a particular macro expands,\n\
483 whereas `macro expand' shows you how all the macros involved in an\n\
484 expression work together to yield a pre-processed expression."),
485 &macrolist);
486 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
487
488 add_info ("macro", info_macro_command,
489 _("Show the definition of MACRO, and it's source location.\n\
490 Usage: info macro [-a|-all] [--] MACRO\n\
491 Options: \n\
492 -a, --all Output all definitions of MACRO in the current compilation\
493 unit.\n\
494 -- Specify the end of arguments and the beginning of the MACRO."));
495
496 add_info ("macros", info_macros_command,
497 _("Show the definitions of all macros at LINESPEC, or the current \
498 source location.\n\
499 Usage: info macros [LINESPEC]"));
500
501 add_cmd ("define", no_class, macro_define_command, _("\
502 Define a new C/C++ preprocessor macro.\n\
503 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
504 preprocessor directive of the form `#define DEFINITION' such that the\n\
505 definition is visible in all the inferior's source files.\n\
506 For example:\n\
507 (gdb) macro define PI (3.1415926)\n\
508 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
509 &macrolist);
510
511 add_cmd ("undef", no_class, macro_undef_command, _("\
512 Remove the definition of the C/C++ preprocessor macro with the given name."),
513 &macrolist);
514
515 add_cmd ("list", no_class, macro_list_command,
516 _("List all the macros defined using the `macro define' command."),
517 &macrolist);
518 }