1*b725ae77Skettenis /* C preprocessor macro expansion commands for GDB.
2*b725ae77Skettenis Copyright 2002 Free Software Foundation, Inc.
3*b725ae77Skettenis Contributed by Red Hat, Inc.
4*b725ae77Skettenis
5*b725ae77Skettenis This file is part of GDB.
6*b725ae77Skettenis
7*b725ae77Skettenis This program is free software; you can redistribute it and/or modify
8*b725ae77Skettenis it under the terms of the GNU General Public License as published by
9*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or
10*b725ae77Skettenis (at your option) any later version.
11*b725ae77Skettenis
12*b725ae77Skettenis This program is distributed in the hope that it will be useful,
13*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of
14*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*b725ae77Skettenis GNU General Public License for more details.
16*b725ae77Skettenis
17*b725ae77Skettenis You should have received a copy of the GNU General Public License
18*b725ae77Skettenis along with this program; if not, write to the Free Software
19*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
20*b725ae77Skettenis Boston, MA 02111-1307, USA. */
21*b725ae77Skettenis
22*b725ae77Skettenis
23*b725ae77Skettenis #include "defs.h"
24*b725ae77Skettenis #include "macrotab.h"
25*b725ae77Skettenis #include "macroexp.h"
26*b725ae77Skettenis #include "macroscope.h"
27*b725ae77Skettenis #include "command.h"
28*b725ae77Skettenis #include "gdbcmd.h"
29*b725ae77Skettenis
30*b725ae77Skettenis
31*b725ae77Skettenis /* The `macro' prefix command. */
32*b725ae77Skettenis
33*b725ae77Skettenis static struct cmd_list_element *macrolist;
34*b725ae77Skettenis
35*b725ae77Skettenis static void
macro_command(char * arg,int from_tty)36*b725ae77Skettenis macro_command (char *arg, int from_tty)
37*b725ae77Skettenis {
38*b725ae77Skettenis printf_unfiltered
39*b725ae77Skettenis ("\"macro\" must be followed by the name of a macro command.\n");
40*b725ae77Skettenis help_list (macrolist, "macro ", -1, gdb_stdout);
41*b725ae77Skettenis }
42*b725ae77Skettenis
43*b725ae77Skettenis
44*b725ae77Skettenis
45*b725ae77Skettenis /* Macro expansion commands. */
46*b725ae77Skettenis
47*b725ae77Skettenis
48*b725ae77Skettenis static void
macro_expand_command(char * exp,int from_tty)49*b725ae77Skettenis macro_expand_command (char *exp, int from_tty)
50*b725ae77Skettenis {
51*b725ae77Skettenis struct macro_scope *ms = NULL;
52*b725ae77Skettenis char *expanded = NULL;
53*b725ae77Skettenis struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
54*b725ae77Skettenis make_cleanup (free_current_contents, &expanded);
55*b725ae77Skettenis
56*b725ae77Skettenis /* You know, when the user doesn't specify any expression, it would be
57*b725ae77Skettenis really cool if this defaulted to the last expression evaluated.
58*b725ae77Skettenis Then it would be easy to ask, "Hey, what did I just evaluate?" But
59*b725ae77Skettenis at the moment, the `print' commands don't save the last expression
60*b725ae77Skettenis evaluated, just its value. */
61*b725ae77Skettenis if (! exp || ! *exp)
62*b725ae77Skettenis error ("You must follow the `macro expand' command with the"
63*b725ae77Skettenis " expression you\n"
64*b725ae77Skettenis "want to expand.");
65*b725ae77Skettenis
66*b725ae77Skettenis ms = default_macro_scope ();
67*b725ae77Skettenis if (ms)
68*b725ae77Skettenis {
69*b725ae77Skettenis expanded = macro_expand (exp, standard_macro_lookup, ms);
70*b725ae77Skettenis fputs_filtered ("expands to: ", gdb_stdout);
71*b725ae77Skettenis fputs_filtered (expanded, gdb_stdout);
72*b725ae77Skettenis fputs_filtered ("\n", gdb_stdout);
73*b725ae77Skettenis }
74*b725ae77Skettenis else
75*b725ae77Skettenis fputs_filtered ("GDB has no preprocessor macro information for "
76*b725ae77Skettenis "that code.\n",
77*b725ae77Skettenis gdb_stdout);
78*b725ae77Skettenis
79*b725ae77Skettenis do_cleanups (cleanup_chain);
80*b725ae77Skettenis return;
81*b725ae77Skettenis }
82*b725ae77Skettenis
83*b725ae77Skettenis
84*b725ae77Skettenis static void
macro_expand_once_command(char * exp,int from_tty)85*b725ae77Skettenis macro_expand_once_command (char *exp, int from_tty)
86*b725ae77Skettenis {
87*b725ae77Skettenis struct macro_scope *ms = NULL;
88*b725ae77Skettenis char *expanded = NULL;
89*b725ae77Skettenis struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
90*b725ae77Skettenis make_cleanup (free_current_contents, &expanded);
91*b725ae77Skettenis
92*b725ae77Skettenis /* You know, when the user doesn't specify any expression, it would be
93*b725ae77Skettenis really cool if this defaulted to the last expression evaluated.
94*b725ae77Skettenis And it should set the once-expanded text as the new `last
95*b725ae77Skettenis expression'. That way, you could just hit return over and over and
96*b725ae77Skettenis see the expression expanded one level at a time. */
97*b725ae77Skettenis if (! exp || ! *exp)
98*b725ae77Skettenis error ("You must follow the `macro expand-once' command with"
99*b725ae77Skettenis " the expression\n"
100*b725ae77Skettenis "you want to expand.");
101*b725ae77Skettenis
102*b725ae77Skettenis ms = default_macro_scope ();
103*b725ae77Skettenis if (ms)
104*b725ae77Skettenis {
105*b725ae77Skettenis expanded = macro_expand_once (exp, standard_macro_lookup, ms);
106*b725ae77Skettenis fputs_filtered ("expands to: ", gdb_stdout);
107*b725ae77Skettenis fputs_filtered (expanded, gdb_stdout);
108*b725ae77Skettenis fputs_filtered ("\n", gdb_stdout);
109*b725ae77Skettenis }
110*b725ae77Skettenis else
111*b725ae77Skettenis fputs_filtered ("GDB has no preprocessor macro information for "
112*b725ae77Skettenis "that code.\n",
113*b725ae77Skettenis gdb_stdout);
114*b725ae77Skettenis
115*b725ae77Skettenis do_cleanups (cleanup_chain);
116*b725ae77Skettenis return;
117*b725ae77Skettenis }
118*b725ae77Skettenis
119*b725ae77Skettenis
120*b725ae77Skettenis static void
show_pp_source_pos(struct ui_file * stream,struct macro_source_file * file,int line)121*b725ae77Skettenis show_pp_source_pos (struct ui_file *stream,
122*b725ae77Skettenis struct macro_source_file *file,
123*b725ae77Skettenis int line)
124*b725ae77Skettenis {
125*b725ae77Skettenis fprintf_filtered (stream, "%s:%d\n", file->filename, line);
126*b725ae77Skettenis
127*b725ae77Skettenis while (file->included_by)
128*b725ae77Skettenis {
129*b725ae77Skettenis fprintf_filtered (gdb_stdout, " included at %s:%d\n",
130*b725ae77Skettenis file->included_by->filename,
131*b725ae77Skettenis file->included_at_line);
132*b725ae77Skettenis file = file->included_by;
133*b725ae77Skettenis }
134*b725ae77Skettenis }
135*b725ae77Skettenis
136*b725ae77Skettenis
137*b725ae77Skettenis static void
info_macro_command(char * name,int from_tty)138*b725ae77Skettenis info_macro_command (char *name, int from_tty)
139*b725ae77Skettenis {
140*b725ae77Skettenis struct macro_scope *ms = NULL;
141*b725ae77Skettenis struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
142*b725ae77Skettenis struct macro_definition *d;
143*b725ae77Skettenis
144*b725ae77Skettenis if (! name || ! *name)
145*b725ae77Skettenis error ("You must follow the `info macro' command with the name"
146*b725ae77Skettenis " of the macro\n"
147*b725ae77Skettenis "whose definition you want to see.");
148*b725ae77Skettenis
149*b725ae77Skettenis ms = default_macro_scope ();
150*b725ae77Skettenis if (! ms)
151*b725ae77Skettenis error ("GDB has no preprocessor macro information for that code.");
152*b725ae77Skettenis
153*b725ae77Skettenis d = macro_lookup_definition (ms->file, ms->line, name);
154*b725ae77Skettenis if (d)
155*b725ae77Skettenis {
156*b725ae77Skettenis int line;
157*b725ae77Skettenis struct macro_source_file *file
158*b725ae77Skettenis = macro_definition_location (ms->file, ms->line, name, &line);
159*b725ae77Skettenis
160*b725ae77Skettenis fprintf_filtered (gdb_stdout, "Defined at ");
161*b725ae77Skettenis show_pp_source_pos (gdb_stdout, file, line);
162*b725ae77Skettenis fprintf_filtered (gdb_stdout, "#define %s", name);
163*b725ae77Skettenis if (d->kind == macro_function_like)
164*b725ae77Skettenis {
165*b725ae77Skettenis int i;
166*b725ae77Skettenis
167*b725ae77Skettenis fputs_filtered ("(", gdb_stdout);
168*b725ae77Skettenis for (i = 0; i < d->argc; i++)
169*b725ae77Skettenis {
170*b725ae77Skettenis fputs_filtered (d->argv[i], gdb_stdout);
171*b725ae77Skettenis if (i + 1 < d->argc)
172*b725ae77Skettenis fputs_filtered (", ", gdb_stdout);
173*b725ae77Skettenis }
174*b725ae77Skettenis fputs_filtered (")", gdb_stdout);
175*b725ae77Skettenis }
176*b725ae77Skettenis fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
177*b725ae77Skettenis }
178*b725ae77Skettenis else
179*b725ae77Skettenis {
180*b725ae77Skettenis fprintf_filtered (gdb_stdout,
181*b725ae77Skettenis "The symbol `%s' has no definition as a C/C++"
182*b725ae77Skettenis " preprocessor macro\n"
183*b725ae77Skettenis "at ", name);
184*b725ae77Skettenis show_pp_source_pos (gdb_stdout, ms->file, ms->line);
185*b725ae77Skettenis }
186*b725ae77Skettenis
187*b725ae77Skettenis do_cleanups (cleanup_chain);
188*b725ae77Skettenis }
189*b725ae77Skettenis
190*b725ae77Skettenis
191*b725ae77Skettenis
192*b725ae77Skettenis /* User-defined macros. */
193*b725ae77Skettenis
194*b725ae77Skettenis /* A table of user-defined macros. Unlike the macro tables used for
195*b725ae77Skettenis symtabs, this one uses xmalloc for all its allocation, not an
196*b725ae77Skettenis obstack, and it doesn't bcache anything; it just xmallocs things. So
197*b725ae77Skettenis it's perfectly possible to remove things from this, or redefine
198*b725ae77Skettenis things. */
199*b725ae77Skettenis static struct macro_table *user_macros;
200*b725ae77Skettenis
201*b725ae77Skettenis static void
macro_define_command(char * exp,int from_tty)202*b725ae77Skettenis macro_define_command (char *exp, int from_tty)
203*b725ae77Skettenis {
204*b725ae77Skettenis error ("Command not implemented yet.");
205*b725ae77Skettenis }
206*b725ae77Skettenis
207*b725ae77Skettenis
208*b725ae77Skettenis static void
macro_undef_command(char * exp,int from_tty)209*b725ae77Skettenis macro_undef_command (char *exp, int from_tty)
210*b725ae77Skettenis {
211*b725ae77Skettenis error ("Command not implemented yet.");
212*b725ae77Skettenis }
213*b725ae77Skettenis
214*b725ae77Skettenis
215*b725ae77Skettenis static void
macro_list_command(char * exp,int from_tty)216*b725ae77Skettenis macro_list_command (char *exp, int from_tty)
217*b725ae77Skettenis {
218*b725ae77Skettenis error ("Command not implemented yet.");
219*b725ae77Skettenis }
220*b725ae77Skettenis
221*b725ae77Skettenis
222*b725ae77Skettenis
223*b725ae77Skettenis /* Initializing the `macrocmd' module. */
224*b725ae77Skettenis
225*b725ae77Skettenis extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
226*b725ae77Skettenis
227*b725ae77Skettenis void
_initialize_macrocmd(void)228*b725ae77Skettenis _initialize_macrocmd (void)
229*b725ae77Skettenis {
230*b725ae77Skettenis struct cmd_list_element *c;
231*b725ae77Skettenis
232*b725ae77Skettenis /* We introduce a new command prefix, `macro', under which we'll put
233*b725ae77Skettenis the various commands for working with preprocessor macros. */
234*b725ae77Skettenis add_prefix_cmd
235*b725ae77Skettenis ("macro", class_info, macro_command,
236*b725ae77Skettenis "Prefix for commands dealing with C preprocessor macros.",
237*b725ae77Skettenis ¯olist, "macro ", 0, &cmdlist);
238*b725ae77Skettenis
239*b725ae77Skettenis add_cmd
240*b725ae77Skettenis ("expand", no_class, macro_expand_command,
241*b725ae77Skettenis "Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n"
242*b725ae77Skettenis "Show the expanded expression.",
243*b725ae77Skettenis ¯olist);
244*b725ae77Skettenis add_alias_cmd ("exp", "expand", no_class, 1, ¯olist);
245*b725ae77Skettenis add_cmd
246*b725ae77Skettenis ("expand-once", no_class, macro_expand_once_command,
247*b725ae77Skettenis "Expand C/C++ preprocessor macro invocations appearing directly in"
248*b725ae77Skettenis " EXPRESSION.\n"
249*b725ae77Skettenis "Show the expanded expression.\n"
250*b725ae77Skettenis "\n"
251*b725ae77Skettenis "This command differs from `macro expand' in that it only expands macro\n"
252*b725ae77Skettenis "invocations that appear directly in EXPRESSION; if expanding a macro\n"
253*b725ae77Skettenis "introduces further macro invocations, those are left unexpanded.\n"
254*b725ae77Skettenis "\n"
255*b725ae77Skettenis "`macro expand-once' helps you see how a particular macro expands,\n"
256*b725ae77Skettenis "whereas `macro expand' shows you how all the macros involved in an\n"
257*b725ae77Skettenis "expression work together to yield a pre-processed expression.",
258*b725ae77Skettenis ¯olist);
259*b725ae77Skettenis add_alias_cmd ("exp1", "expand-once", no_class, 1, ¯olist);
260*b725ae77Skettenis
261*b725ae77Skettenis add_cmd
262*b725ae77Skettenis ("macro", no_class, info_macro_command,
263*b725ae77Skettenis "Show the definition of MACRO, and its source location.",
264*b725ae77Skettenis &infolist);
265*b725ae77Skettenis
266*b725ae77Skettenis add_cmd
267*b725ae77Skettenis ("define", no_class, macro_define_command,
268*b725ae77Skettenis "Define a new C/C++ preprocessor macro.\n"
269*b725ae77Skettenis "The GDB command `macro define DEFINITION' is equivalent to placing a\n"
270*b725ae77Skettenis "preprocessor directive of the form `#define DEFINITION' such that the\n"
271*b725ae77Skettenis "definition is visible in all the inferior's source files.\n"
272*b725ae77Skettenis "For example:\n"
273*b725ae77Skettenis " (gdb) macro define PI (3.1415926)\n"
274*b725ae77Skettenis " (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))",
275*b725ae77Skettenis ¯olist);
276*b725ae77Skettenis
277*b725ae77Skettenis add_cmd
278*b725ae77Skettenis ("undef", no_class, macro_undef_command,
279*b725ae77Skettenis "Remove the definition of the C/C++ preprocessor macro with the"
280*b725ae77Skettenis " given name.",
281*b725ae77Skettenis ¯olist);
282*b725ae77Skettenis
283*b725ae77Skettenis add_cmd
284*b725ae77Skettenis ("list", no_class, macro_list_command,
285*b725ae77Skettenis "List all the macros defined using the `macro define' command.",
286*b725ae77Skettenis ¯olist);
287*b725ae77Skettenis
288*b725ae77Skettenis user_macros = new_macro_table (0, 0);
289*b725ae77Skettenis }
290