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