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