GDB (xrefs)
/tmp/gdb-8.1/gdb/macrocmd.c
Go to the documentation of this file.
1 /* C preprocessor macro expansion commands for GDB.
2  Copyright (C) 2002-2018 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 
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 {
39  ("\"macro\" must be followed by the name of a macro command.\n");
41 }
42 
43 
44 
45 /* Macro expansion commands. */
46 
47 
48 /* Prints an informational message regarding the lack of macro information. */
49 static 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  struct macro_scope *ms = NULL;
59  char *expanded = NULL;
61 
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)
70  error (_("You must follow the `macro expand' command with the"
71  " expression you\n"
72  "want to expand."));
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
84 
86  return;
87 }
88 
89 
90 static void
91 macro_expand_once_command (const char *exp, int from_tty)
92 {
93  struct macro_scope *ms = NULL;
94  char *expanded = NULL;
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)
104  error (_("You must follow the `macro expand-once' command with"
105  " the expression\n"
106  "you want to expand."));
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
118 
120  return;
121 }
122 
123 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
124 
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. */
127 static void
128 show_pp_source_pos (struct ui_file *stream,
129  struct macro_source_file *file,
130  int line)
131 {
132  char *fullname;
133 
134  fullname = macro_source_fullname (file);
135  fprintf_filtered (stream, "%s:%d\n", fullname, line);
136  xfree (fullname);
137 
138  while (file->included_by)
139  {
140  fullname = macro_source_fullname (file->included_by);
141  fprintf_filtered (gdb_stdout, " included at %s:%d\n", fullname,
142  file->included_at_line);
143  xfree (fullname);
144  file = file->included_by;
145  }
146 }
147 
148 /* Outputs a macro for human consumption, detailing the include path
149  and macro definition. NAME is the name of the macro.
150  D the definition. FILE the start of the include path, and LINE the
151  line number in FILE.
152 
153  Care should be taken that this function does not cause any lookups into
154  the splay tree so that it can be safely used while iterating. */
155 static void
157  const struct macro_definition *d,
158  struct macro_source_file *file,
159  int line)
160 {
161  fprintf_filtered (gdb_stdout, "Defined at ");
162  show_pp_source_pos (gdb_stdout, file, line);
163 
164  if (line != 0)
165  fprintf_filtered (gdb_stdout, "#define %s", name);
166  else
167  fprintf_filtered (gdb_stdout, "-D%s", name);
168 
169  if (d->kind == macro_function_like)
170  {
171  int i;
172 
173  fputs_filtered ("(", gdb_stdout);
174  for (i = 0; i < d->argc; i++)
175  {
176  fputs_filtered (d->argv[i], gdb_stdout);
177  if (i + 1 < d->argc)
178  fputs_filtered (", ", gdb_stdout);
179  }
180  fputs_filtered (")", gdb_stdout);
181  }
182 
183  if (line != 0)
184  fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
185  else
186  fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
187 }
188 
189 /* The implementation of the `info macro' command. */
190 static void
191 info_macro_command (const char *args, int from_tty)
192 {
193  struct macro_scope *ms = NULL;
194  struct cleanup *cleanup_chain;
195  const char *name;
196  int show_all_macros_named = 0;
197  const char *arg_start = args;
198  int processing_args = 1;
199 
200  while (processing_args
201  && arg_start && *arg_start == '-' && *arg_start != '\0')
202  {
203  const char *p = skip_to_space (arg_start);
204 
205  if (strncmp (arg_start, "-a", p - arg_start) == 0
206  || strncmp (arg_start, "-all", p - arg_start) == 0)
207  show_all_macros_named = 1;
208  else if (strncmp (arg_start, "--", p - arg_start) == 0)
209  /* Our macro support seems rather C specific but this would
210  seem necessary for languages allowing - in macro names.
211  e.g. Scheme's (defmacro ->foo () "bar\n") */
212  processing_args = 0;
213  else
214  {
215  error (_("Unrecognized option '%.*s' to info macro command. "
216  "Try \"help info macro\"."),
217  int (p - arg_start), arg_start);
218  }
219 
220  arg_start = skip_spaces (p);
221  }
222 
223  name = arg_start;
224 
225  if (! name || ! *name)
226  error (_("You must follow the `info macro' command with the name"
227  " of the macro\n"
228  "whose definition you want to see."));
229 
230  ms = default_macro_scope ();
232 
233  if (! ms)
235  else if (show_all_macros_named)
236  macro_for_each (ms->file->table, [&] (const char *macro_name,
237  const macro_definition *macro,
238  macro_source_file *source,
239  int line)
240  {
241  if (strcmp (name, macro_name) == 0)
242  print_macro_definition (name, macro, source, line);
243  });
244  else
245  {
246  struct macro_definition *d;
247 
248  d = macro_lookup_definition (ms->file, ms->line, name);
249  if (d)
250  {
251  int line;
252  struct macro_source_file *file
253  = macro_definition_location (ms->file, ms->line, name, &line);
254 
255  print_macro_definition (name, d, file, line);
256  }
257  else
258  {
260  "The symbol `%s' has no definition as a C/C++"
261  " preprocessor macro\n"
262  "at ", name);
264  }
265  }
266 
268 }
269 
270 /* Implementation of the "info macros" command. */
271 static void
272 info_macros_command (const char *args, int from_tty)
273 {
274  struct macro_scope *ms = NULL;
276 
277  if (args == NULL)
278  ms = default_macro_scope ();
279  else
280  {
281  std::vector<symtab_and_line> sals
283 
284  if (!sals.empty ())
285  ms = sal_macro_scope (sals[0]);
286  }
287 
288  if (! ms || ! ms->file || ! ms->file->table)
290  else
292 
294 }
295 
296 
297 /* User-defined macros. */
298 
299 static void
300 skip_ws (const char **expp)
301 {
302  while (macro_is_whitespace (**expp))
303  ++*expp;
304 }
305 
306 /* Try to find the bounds of an identifier. If an identifier is
307  found, returns a newly allocated string; otherwise returns NULL.
308  EXPP is a pointer to an input string; it is updated to point to the
309  text following the identifier. If IS_PARAMETER is true, this
310  function will also allow "..." forms as used in varargs macro
311  parameters. */
312 
313 static char *
314 extract_identifier (const char **expp, int is_parameter)
315 {
316  char *result;
317  const char *p = *expp;
318  unsigned int len;
319 
320  if (is_parameter && startswith (p, "..."))
321  {
322  /* Ok. */
323  }
324  else
325  {
326  if (! *p || ! macro_is_identifier_nondigit (*p))
327  return NULL;
328  for (++p;
329  *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
330  ++p)
331  ;
332  }
333 
334  if (is_parameter && startswith (p, "..."))
335  p += 3;
336 
337  len = p - *expp;
338  result = (char *) xmalloc (len + 1);
339  memcpy (result, *expp, len);
340  result[len] = '\0';
341  *expp += len;
342  return result;
343 }
344 
345 /* Helper function to clean up a temporarily-constructed macro object.
346  This assumes that the contents were all allocated with xmalloc. */
347 static void
349 {
350  int i;
351  struct macro_definition *loc = (struct macro_definition *) ptr;
352 
353  for (i = 0; i < loc->argc; ++i)
354  xfree ((char *) loc->argv[i]);
355  xfree ((char *) loc->argv);
356  /* Note that the 'replacement' field is not allocated. */
357 }
358 
359 static void
360 macro_define_command (const char *exp, int from_tty)
361 {
362  struct macro_definition new_macro;
363  char *name = NULL;
364  struct cleanup *cleanup_chain;
365 
366  if (!exp)
367  error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
368 
371 
372  memset (&new_macro, 0, sizeof (struct macro_definition));
373 
374  skip_ws (&exp);
375  name = extract_identifier (&exp, 0);
376  if (! name)
377  error (_("Invalid macro name."));
378  if (*exp == '(')
379  {
380  /* Function-like macro. */
381  int alloced = 5;
382  char **argv = XNEWVEC (char *, alloced);
383 
384  new_macro.kind = macro_function_like;
385  new_macro.argc = 0;
386  new_macro.argv = (const char * const *) argv;
387 
388  /* Skip the '(' and whitespace. */
389  ++exp;
390  skip_ws (&exp);
391 
392  while (*exp != ')')
393  {
394  int i;
395 
396  if (new_macro.argc == alloced)
397  {
398  alloced *= 2;
399  argv = (char **) xrealloc (argv, alloced * sizeof (char *));
400  /* Must update new_macro as well... */
401  new_macro.argv = (const char * const *) argv;
402  }
403  argv[new_macro.argc] = extract_identifier (&exp, 1);
404  if (! argv[new_macro.argc])
405  error (_("Macro is missing an argument."));
406  ++new_macro.argc;
407 
408  for (i = new_macro.argc - 2; i >= 0; --i)
409  {
410  if (! strcmp (argv[i], argv[new_macro.argc - 1]))
411  error (_("Two macro arguments with identical names."));
412  }
413 
414  skip_ws (&exp);
415  if (*exp == ',')
416  {
417  ++exp;
418  skip_ws (&exp);
419  }
420  else if (*exp != ')')
421  error (_("',' or ')' expected at end of macro arguments."));
422  }
423  /* Skip the closing paren. */
424  ++exp;
425  skip_ws (&exp);
426 
428  new_macro.argc, (const char **) new_macro.argv,
429  exp);
430  }
431  else
432  {
433  skip_ws (&exp);
435  }
436 
438 }
439 
440 
441 static void
442 macro_undef_command (const char *exp, int from_tty)
443 {
444  char *name;
445 
446  if (!exp)
447  error (_("usage: macro undef NAME"));
448 
449  skip_ws (&exp);
450  name = extract_identifier (&exp, 0);
451  if (! name)
452  error (_("Invalid macro name."));
454  xfree (name);
455 }
456 
457 
458 static void
459 print_one_macro (const char *name, const struct macro_definition *macro,
460  struct macro_source_file *source, int line)
461 {
462  fprintf_filtered (gdb_stdout, "macro define %s", name);
463  if (macro->kind == macro_function_like)
464  {
465  int i;
466 
468  for (i = 0; i < macro->argc; ++i)
469  fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
470  macro->argv[i]);
472  }
473  fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
474 }
475 
476 
477 static void
478 macro_list_command (const char *exp, int from_tty)
479 {
481 }
482 
483 /* Initializing the `macrocmd' module. */
484 
485 void
487 {
488  /* We introduce a new command prefix, `macro', under which we'll put
489  the various commands for working with preprocessor macros. */
491  _("Prefix for commands dealing with C preprocessor macros."),
492  &macrolist, "macro ", 0, &cmdlist);
493 
494  add_cmd ("expand", no_class, macro_expand_command, _("\
495 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
496 Show the expanded expression."),
497  &macrolist);
498  add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
499  add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
500 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
501 Show the expanded expression.\n\
502 \n\
503 This command differs from `macro expand' in that it only expands macro\n\
504 invocations that appear directly in EXPRESSION; if expanding a macro\n\
505 introduces further macro invocations, those are left unexpanded.\n\
506 \n\
507 `macro expand-once' helps you see how a particular macro expands,\n\
508 whereas `macro expand' shows you how all the macros involved in an\n\
509 expression work together to yield a pre-processed expression."),
510  &macrolist);
511  add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
512 
513  add_info ("macro", info_macro_command,
514  _("Show the definition of MACRO, and it's source location.\n\
515 Usage: info macro [-a|-all] [--] MACRO\n\
516 Options: \n\
517  -a, --all Output all definitions of MACRO in the current compilation\
518  unit.\n\
519  -- Specify the end of arguments and the beginning of the MACRO."));
520 
521  add_info ("macros", info_macros_command,
522  _("Show the definitions of all macros at LINESPEC, or the current \
523 source location.\n\
524 Usage: info macros [LINESPEC]"));
525 
526  add_cmd ("define", no_class, macro_define_command, _("\
527 Define a new C/C++ preprocessor macro.\n\
528 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
529 preprocessor directive of the form `#define DEFINITION' such that the\n\
530 definition is visible in all the inferior's source files.\n\
531 For example:\n\
532  (gdb) macro define PI (3.1415926)\n\
533  (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
534  &macrolist);
535 
536  add_cmd ("undef", no_class, macro_undef_command, _("\
537 Remove the definition of the C/C++ preprocessor macro with the given name."),
538  &macrolist);
539 
541  _("List all the macros defined using the `macro define' command."),
542  &macrolist);
543 }
void macro_define_function(struct macro_source_file *source, int line, const char *name, int argc, const char **argv, const char *replacement)
Definition: macrotab.c:803
int macro_is_digit(int c)
Definition: macroexp.c:201
const char *const * argv
Definition: macrotab.h:302
static char * extract_identifier(const char **expp, int is_parameter)
Definition: macrocmd.c:314
struct macro_source_file * file
Definition: macroscope.h:34
char * macro_expand(const char *source, macro_lookup_ftype *lookup_func, void *lookup_func_baton)
Definition: macroexp.c:1489
int macro_is_identifier_nondigit(int c)
Definition: macroexp.c:208
__extension__ enum macro_kind kind
Definition: macrotab.h:294
void xfree(void *)
static struct cleanup * cleanup_chain
Definition: cleanups.c:63
static void skip_ws(const char **expp)
Definition: macrocmd.c:300
struct macro_table * table
Definition: macrotab.h:129
struct macro_source_file * included_by
Definition: macrotab.h:138
struct cmd_list_element * add_info(const char *name, cmd_const_cfunc_ftype *fun, const char *doc)
Definition: cli-decode.c:886
void * memset(T *s, int c, size_t n)=delete
struct macro_definition * standard_macro_lookup(const char *name, void *baton)
Definition: macroscope.c:147
static void print_macro_definition(const char *name, const struct macro_definition *d, struct macro_source_file *file, int line)
Definition: macrocmd.c:156
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
static void macro_inform_no_debuginfo(void)
Definition: macrocmd.c:50
static void free_macro_definition_ptr(void *ptr)
Definition: macrocmd.c:348
struct cmd_list_element * cmdlist
Definition: cli-cmds.c:79
char * skip_spaces(char *chp)
Definition: common-utils.c:337
#define _(String)
Definition: gdb_locale.h:35
struct macro_scope * sal_macro_scope(struct symtab_and_line sal)
Definition: macroscope.c:39
struct macro_scope * default_macro_scope(void)
Definition: macroscope.c:102
struct macro_table * macro_user_macros
Definition: macroscope.c:35
void macro_define_object(struct macro_source_file *source, int line, const char *name, const char *replacement)
Definition: macrotab.c:784
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_const_cfunc_ftype *fun, const char *doc, struct cmd_list_element **prefixlist, const char *prefixname, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:367
const char *const name
Definition: aarch64-tdep.c:76
void macro_for_each(struct macro_table *table, gdb::function_view< macro_callback_fn > fn)
Definition: macrotab.c:993
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
struct macro_definition * macro_lookup_definition(struct macro_source_file *source, int line, const char *name)
Definition: macrotab.c:922
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
void puts_filtered(const char *string)
Definition: utils.c:2085
void free_current_contents(void *ptr)
Definition: utils.c:199
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
static int startswith(const char *string, const char *pattern)
Definition: common-utils.h:107
static void macro_expand_once_command(const char *exp, int from_tty)
Definition: macrocmd.c:91
const char * skip_to_space(const char *chp)
Definition: common-utils.c:361
void printf_unfiltered(const char *format,...)
Definition: utils.c:2056
char * macro_source_fullname(struct macro_source_file *file)
Definition: macrotab.c:1090
void * xmalloc(YYSIZE_T)
struct macro_source_file * macro_definition_location(struct macro_source_file *source, int line, const char *name, int *definition_line)
Definition: macrotab.c:944
static void info_macros_command(const char *args, int from_tty)
Definition: macrocmd.c:272
static void macro_list_command(const char *exp, int from_tty)
Definition: macrocmd.c:478
struct cmd_list_element * add_alias_cmd(const char *name, cmd_list_element *old, enum command_class theclass, int abbrev_flag, struct cmd_list_element **list)
Definition: cli-decode.c:306
PTR xrealloc(PTR ptr, size_t size)
Definition: common-utils.c:52
void help_list(struct cmd_list_element *list, const char *cmdtype, enum command_class theclass, struct ui_file *stream)
Definition: cli-decode.c:1071
#define XNEWVEC(T, N)
Definition: poison.h:145
static void macro_command(const char *arg, int from_tty)
Definition: macrocmd.c:36
static void macro_define_command(const char *exp, int from_tty)
Definition: macrocmd.c:360
static void macro_expand_command(const char *exp, int from_tty)
Definition: macrocmd.c:56
std::vector< symtab_and_line > decode_line_with_current_source(const char *string, int flags)
Definition: linespec.c:3371
static void show_pp_source_pos(struct ui_file *stream, struct macro_source_file *file, int line)
Definition: macrocmd.c:128
static void macro_undef_command(const char *exp, int from_tty)
Definition: macrocmd.c:442
void macro_for_each_in_scope(struct macro_source_file *file, int line, gdb::function_view< macro_callback_fn > fn)
Definition: macrotab.c:1030
struct macro_source_file * macro_main(struct macro_table *t)
Definition: macrotab.c:433
argv
Definition: __init__.py:63
const char * replacement
Definition: macrotab.h:309
static void print_one_macro(const char *name, const struct macro_definition *macro, struct macro_source_file *source, int line)
Definition: macrocmd.c:459
static struct cmd_list_element * macrolist
Definition: macrocmd.c:33
void _initialize_macrocmd(void)
Definition: macrocmd.c:486
static void info_macro_command(const char *args, int from_tty)
Definition: macrocmd.c:191
void error(const char *fmt,...)
Definition: errors.c:38
void macro_undef(struct macro_source_file *source, int line, const char *name)
Definition: macrotab.c:831
char * macro_expand_once(const char *source, macro_lookup_ftype *lookup_func, void *lookup_func_baton)
Definition: macroexp.c:1512
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:174
#define gdb_stdout
Definition: utils.h:340
int macro_is_whitespace(int c)
Definition: macroexp.c:190