15796c8dcSSimon Schubert /* C preprocessor macro expansion commands for GDB. 2*a45ae5f8SJohn Marino Copyright (C) 2002, 2007-2012 Free Software Foundation, Inc. 35796c8dcSSimon Schubert Contributed by Red Hat, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "macrotab.h" 235796c8dcSSimon Schubert #include "macroexp.h" 245796c8dcSSimon Schubert #include "macroscope.h" 25*a45ae5f8SJohn Marino #include "cli/cli-utils.h" 265796c8dcSSimon Schubert #include "command.h" 275796c8dcSSimon Schubert #include "gdbcmd.h" 285796c8dcSSimon Schubert #include "gdb_string.h" 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert /* The `macro' prefix command. */ 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert static struct cmd_list_element *macrolist; 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert static void 365796c8dcSSimon Schubert macro_command (char *arg, int from_tty) 375796c8dcSSimon Schubert { 385796c8dcSSimon Schubert printf_unfiltered 395796c8dcSSimon Schubert ("\"macro\" must be followed by the name of a macro command.\n"); 405796c8dcSSimon Schubert help_list (macrolist, "macro ", -1, gdb_stdout); 415796c8dcSSimon Schubert } 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert /* Macro expansion commands. */ 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert 48*a45ae5f8SJohn Marino /* Prints an informational message regarding the lack of macro information. */ 49*a45ae5f8SJohn Marino static void macro_inform_no_debuginfo() 50*a45ae5f8SJohn Marino { 51*a45ae5f8SJohn Marino fputs_filtered ("GDB has no preprocessor macro information for " 52*a45ae5f8SJohn Marino "that code.", 53*a45ae5f8SJohn Marino gdb_stdout); 54*a45ae5f8SJohn Marino } 55*a45ae5f8SJohn Marino 565796c8dcSSimon Schubert static void 575796c8dcSSimon Schubert macro_expand_command (char *exp, int from_tty) 585796c8dcSSimon Schubert { 595796c8dcSSimon Schubert struct macro_scope *ms = NULL; 605796c8dcSSimon Schubert char *expanded = NULL; 615796c8dcSSimon Schubert struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); 62cf7f2e2dSJohn Marino 635796c8dcSSimon Schubert make_cleanup (free_current_contents, &expanded); 645796c8dcSSimon Schubert 655796c8dcSSimon Schubert /* You know, when the user doesn't specify any expression, it would be 665796c8dcSSimon Schubert really cool if this defaulted to the last expression evaluated. 675796c8dcSSimon Schubert Then it would be easy to ask, "Hey, what did I just evaluate?" But 685796c8dcSSimon Schubert at the moment, the `print' commands don't save the last expression 695796c8dcSSimon Schubert evaluated, just its value. */ 705796c8dcSSimon Schubert if (! exp || ! *exp) 715796c8dcSSimon Schubert error (_("You must follow the `macro expand' command with the" 725796c8dcSSimon Schubert " expression you\n" 735796c8dcSSimon Schubert "want to expand.")); 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert ms = default_macro_scope (); 765796c8dcSSimon Schubert if (ms) 775796c8dcSSimon Schubert { 785796c8dcSSimon Schubert expanded = macro_expand (exp, standard_macro_lookup, ms); 795796c8dcSSimon Schubert fputs_filtered ("expands to: ", gdb_stdout); 805796c8dcSSimon Schubert fputs_filtered (expanded, gdb_stdout); 815796c8dcSSimon Schubert fputs_filtered ("\n", gdb_stdout); 825796c8dcSSimon Schubert } 835796c8dcSSimon Schubert else 84*a45ae5f8SJohn Marino macro_inform_no_debuginfo (); 855796c8dcSSimon Schubert 865796c8dcSSimon Schubert do_cleanups (cleanup_chain); 875796c8dcSSimon Schubert return; 885796c8dcSSimon Schubert } 895796c8dcSSimon Schubert 905796c8dcSSimon Schubert 915796c8dcSSimon Schubert static void 925796c8dcSSimon Schubert macro_expand_once_command (char *exp, int from_tty) 935796c8dcSSimon Schubert { 945796c8dcSSimon Schubert struct macro_scope *ms = NULL; 955796c8dcSSimon Schubert char *expanded = NULL; 965796c8dcSSimon Schubert struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); 975796c8dcSSimon Schubert make_cleanup (free_current_contents, &expanded); 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert /* You know, when the user doesn't specify any expression, it would be 1005796c8dcSSimon Schubert really cool if this defaulted to the last expression evaluated. 1015796c8dcSSimon Schubert And it should set the once-expanded text as the new `last 1025796c8dcSSimon Schubert expression'. That way, you could just hit return over and over and 1035796c8dcSSimon Schubert see the expression expanded one level at a time. */ 1045796c8dcSSimon Schubert if (! exp || ! *exp) 1055796c8dcSSimon Schubert error (_("You must follow the `macro expand-once' command with" 1065796c8dcSSimon Schubert " the expression\n" 1075796c8dcSSimon Schubert "you want to expand.")); 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert ms = default_macro_scope (); 1105796c8dcSSimon Schubert if (ms) 1115796c8dcSSimon Schubert { 1125796c8dcSSimon Schubert expanded = macro_expand_once (exp, standard_macro_lookup, ms); 1135796c8dcSSimon Schubert fputs_filtered ("expands to: ", gdb_stdout); 1145796c8dcSSimon Schubert fputs_filtered (expanded, gdb_stdout); 1155796c8dcSSimon Schubert fputs_filtered ("\n", gdb_stdout); 1165796c8dcSSimon Schubert } 1175796c8dcSSimon Schubert else 118*a45ae5f8SJohn Marino macro_inform_no_debuginfo (); 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert do_cleanups (cleanup_chain); 1215796c8dcSSimon Schubert return; 1225796c8dcSSimon Schubert } 1235796c8dcSSimon Schubert 124*a45ae5f8SJohn Marino /* Outputs the include path of a macro starting at FILE and LINE to STREAM. 1255796c8dcSSimon Schubert 126*a45ae5f8SJohn Marino Care should be taken that this function does not cause any lookups into 127*a45ae5f8SJohn Marino the splay tree so that it can be safely used while iterating. */ 1285796c8dcSSimon Schubert static void 1295796c8dcSSimon Schubert show_pp_source_pos (struct ui_file *stream, 1305796c8dcSSimon Schubert struct macro_source_file *file, 1315796c8dcSSimon Schubert int line) 1325796c8dcSSimon Schubert { 1335796c8dcSSimon Schubert fprintf_filtered (stream, "%s:%d\n", file->filename, line); 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert while (file->included_by) 1365796c8dcSSimon Schubert { 1375796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, " included at %s:%d\n", 1385796c8dcSSimon Schubert file->included_by->filename, 1395796c8dcSSimon Schubert file->included_at_line); 1405796c8dcSSimon Schubert file = file->included_by; 1415796c8dcSSimon Schubert } 1425796c8dcSSimon Schubert } 1435796c8dcSSimon Schubert 144*a45ae5f8SJohn Marino /* Outputs a macro for human consumption, detailing the include path 145*a45ae5f8SJohn Marino and macro definition. NAME is the name of the macro. 146*a45ae5f8SJohn Marino D the definition. FILE the start of the include path, and LINE the 147*a45ae5f8SJohn Marino line number in FILE. 1485796c8dcSSimon Schubert 149*a45ae5f8SJohn Marino Care should be taken that this function does not cause any lookups into 150*a45ae5f8SJohn Marino the splay tree so that it can be safely used while iterating. */ 1515796c8dcSSimon Schubert static void 152*a45ae5f8SJohn Marino print_macro_definition (const char *name, 153*a45ae5f8SJohn Marino const struct macro_definition *d, 154*a45ae5f8SJohn Marino struct macro_source_file *file, 155*a45ae5f8SJohn Marino int line) 1565796c8dcSSimon Schubert { 1575796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "Defined at "); 1585796c8dcSSimon Schubert show_pp_source_pos (gdb_stdout, file, line); 159*a45ae5f8SJohn Marino 1605796c8dcSSimon Schubert if (line != 0) 1615796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "#define %s", name); 1625796c8dcSSimon Schubert else 1635796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "-D%s", name); 164*a45ae5f8SJohn Marino 1655796c8dcSSimon Schubert if (d->kind == macro_function_like) 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert int i; 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert fputs_filtered ("(", gdb_stdout); 1705796c8dcSSimon Schubert for (i = 0; i < d->argc; i++) 1715796c8dcSSimon Schubert { 1725796c8dcSSimon Schubert fputs_filtered (d->argv[i], gdb_stdout); 1735796c8dcSSimon Schubert if (i + 1 < d->argc) 1745796c8dcSSimon Schubert fputs_filtered (", ", gdb_stdout); 1755796c8dcSSimon Schubert } 1765796c8dcSSimon Schubert fputs_filtered (")", gdb_stdout); 1775796c8dcSSimon Schubert } 178*a45ae5f8SJohn Marino 1795796c8dcSSimon Schubert if (line != 0) 1805796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, " %s\n", d->replacement); 1815796c8dcSSimon Schubert else 1825796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "=%s\n", d->replacement); 1835796c8dcSSimon Schubert } 184*a45ae5f8SJohn Marino 185*a45ae5f8SJohn Marino /* A callback function for usage with macro_for_each and friends. 186*a45ae5f8SJohn Marino If USER_DATA is null all macros will be printed. 187*a45ae5f8SJohn Marino Otherwise USER_DATA is considered to be a string, printing 188*a45ae5f8SJohn Marino only macros who's NAME matches USER_DATA. Other arguments are 189*a45ae5f8SJohn Marino routed to print_macro_definition. */ 190*a45ae5f8SJohn Marino static void 191*a45ae5f8SJohn Marino print_macro_callback (const char *name, const struct macro_definition *macro, 192*a45ae5f8SJohn Marino struct macro_source_file *source, int line, 193*a45ae5f8SJohn Marino void *user_data) 194*a45ae5f8SJohn Marino { 195*a45ae5f8SJohn Marino if (! user_data || strcmp (user_data, name) == 0) 196*a45ae5f8SJohn Marino print_macro_definition (name, macro, source, line); 197*a45ae5f8SJohn Marino } 198*a45ae5f8SJohn Marino 199*a45ae5f8SJohn Marino /* The implementation of the `info macro' command. */ 200*a45ae5f8SJohn Marino static void 201*a45ae5f8SJohn Marino info_macro_command (char *args, int from_tty) 202*a45ae5f8SJohn Marino { 203*a45ae5f8SJohn Marino struct macro_scope *ms = NULL; 204*a45ae5f8SJohn Marino struct cleanup *cleanup_chain; 205*a45ae5f8SJohn Marino char *name; 206*a45ae5f8SJohn Marino int show_all_macros_named = 0; 207*a45ae5f8SJohn Marino char *arg_start = args; 208*a45ae5f8SJohn Marino int processing_args = 1; 209*a45ae5f8SJohn Marino 210*a45ae5f8SJohn Marino while (processing_args 211*a45ae5f8SJohn Marino && arg_start && *arg_start == '-' && *arg_start != '\0') 212*a45ae5f8SJohn Marino { 213*a45ae5f8SJohn Marino char *p = skip_to_space (arg_start); 214*a45ae5f8SJohn Marino 215*a45ae5f8SJohn Marino if (strncmp (arg_start, "-a", p - arg_start) == 0 216*a45ae5f8SJohn Marino || strncmp (arg_start, "-all", p - arg_start) == 0) 217*a45ae5f8SJohn Marino show_all_macros_named = 1; 218*a45ae5f8SJohn Marino else if (strncmp (arg_start, "--", p - arg_start) == 0) 219*a45ae5f8SJohn Marino /* Our macro support seems rather C specific but this would 220*a45ae5f8SJohn Marino seem necessary for languages allowing - in macro names. 221*a45ae5f8SJohn Marino e.g. Scheme's (defmacro ->foo () "bar\n") */ 222*a45ae5f8SJohn Marino processing_args = 0; 223*a45ae5f8SJohn Marino else 224*a45ae5f8SJohn Marino { 225*a45ae5f8SJohn Marino /* Relies on modified 'args' not making it in to history */ 226*a45ae5f8SJohn Marino *p = '\0'; 227*a45ae5f8SJohn Marino error (_("Unrecognized option '%s' to info macro command. " 228*a45ae5f8SJohn Marino "Try \"help info macro\"."), arg_start); 229*a45ae5f8SJohn Marino } 230*a45ae5f8SJohn Marino 231*a45ae5f8SJohn Marino arg_start = skip_spaces (p); 232*a45ae5f8SJohn Marino } 233*a45ae5f8SJohn Marino 234*a45ae5f8SJohn Marino name = arg_start; 235*a45ae5f8SJohn Marino 236*a45ae5f8SJohn Marino if (! name || ! *name) 237*a45ae5f8SJohn Marino error (_("You must follow the `info macro' command with the name" 238*a45ae5f8SJohn Marino " of the macro\n" 239*a45ae5f8SJohn Marino "whose definition you want to see.")); 240*a45ae5f8SJohn Marino 241*a45ae5f8SJohn Marino ms = default_macro_scope (); 242*a45ae5f8SJohn Marino cleanup_chain = make_cleanup (free_current_contents, &ms); 243*a45ae5f8SJohn Marino 244*a45ae5f8SJohn Marino if (! ms) 245*a45ae5f8SJohn Marino macro_inform_no_debuginfo (); 246*a45ae5f8SJohn Marino else if (show_all_macros_named) 247*a45ae5f8SJohn Marino macro_for_each (ms->file->table, print_macro_callback, name); 248*a45ae5f8SJohn Marino else 249*a45ae5f8SJohn Marino { 250*a45ae5f8SJohn Marino struct macro_definition *d; 251*a45ae5f8SJohn Marino 252*a45ae5f8SJohn Marino d = macro_lookup_definition (ms->file, ms->line, name); 253*a45ae5f8SJohn Marino if (d) 254*a45ae5f8SJohn Marino { 255*a45ae5f8SJohn Marino int line; 256*a45ae5f8SJohn Marino struct macro_source_file *file 257*a45ae5f8SJohn Marino = macro_definition_location (ms->file, ms->line, name, &line); 258*a45ae5f8SJohn Marino 259*a45ae5f8SJohn Marino print_macro_definition (name, d, file, line); 260*a45ae5f8SJohn Marino } 2615796c8dcSSimon Schubert else 2625796c8dcSSimon Schubert { 2635796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, 2645796c8dcSSimon Schubert "The symbol `%s' has no definition as a C/C++" 2655796c8dcSSimon Schubert " preprocessor macro\n" 2665796c8dcSSimon Schubert "at ", name); 2675796c8dcSSimon Schubert show_pp_source_pos (gdb_stdout, ms->file, ms->line); 2685796c8dcSSimon Schubert } 269*a45ae5f8SJohn Marino } 2705796c8dcSSimon Schubert 2715796c8dcSSimon Schubert do_cleanups (cleanup_chain); 2725796c8dcSSimon Schubert } 2735796c8dcSSimon Schubert 274*a45ae5f8SJohn Marino /* Implementation of the "info macros" command. */ 275*a45ae5f8SJohn Marino static void 276*a45ae5f8SJohn Marino info_macros_command (char *args, int from_tty) 277*a45ae5f8SJohn Marino { 278*a45ae5f8SJohn Marino struct macro_scope *ms = NULL; 279*a45ae5f8SJohn Marino struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); 280*a45ae5f8SJohn Marino 281*a45ae5f8SJohn Marino if (args == NULL) 282*a45ae5f8SJohn Marino ms = default_macro_scope (); 283*a45ae5f8SJohn Marino else 284*a45ae5f8SJohn Marino { 285*a45ae5f8SJohn Marino struct symtabs_and_lines sals = decode_line_spec (args, 0); 286*a45ae5f8SJohn Marino 287*a45ae5f8SJohn Marino if (sals.nelts) 288*a45ae5f8SJohn Marino ms = sal_macro_scope (sals.sals[0]); 289*a45ae5f8SJohn Marino } 290*a45ae5f8SJohn Marino 291*a45ae5f8SJohn Marino if (! ms || ! ms->file || ! ms->file->table) 292*a45ae5f8SJohn Marino macro_inform_no_debuginfo (); 293*a45ae5f8SJohn Marino else 294*a45ae5f8SJohn Marino macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL); 295*a45ae5f8SJohn Marino 296*a45ae5f8SJohn Marino do_cleanups (cleanup_chain); 297*a45ae5f8SJohn Marino } 2985796c8dcSSimon Schubert 2995796c8dcSSimon Schubert 3005796c8dcSSimon Schubert /* User-defined macros. */ 3015796c8dcSSimon Schubert 3025796c8dcSSimon Schubert static void 3035796c8dcSSimon Schubert skip_ws (char **expp) 3045796c8dcSSimon Schubert { 3055796c8dcSSimon Schubert while (macro_is_whitespace (**expp)) 3065796c8dcSSimon Schubert ++*expp; 3075796c8dcSSimon Schubert } 3085796c8dcSSimon Schubert 3095796c8dcSSimon Schubert /* Try to find the bounds of an identifier. If an identifier is 3105796c8dcSSimon Schubert found, returns a newly allocated string; otherwise returns NULL. 3115796c8dcSSimon Schubert EXPP is a pointer to an input string; it is updated to point to the 3125796c8dcSSimon Schubert text following the identifier. If IS_PARAMETER is true, this 3135796c8dcSSimon Schubert function will also allow "..." forms as used in varargs macro 3145796c8dcSSimon Schubert parameters. */ 3155796c8dcSSimon Schubert 3165796c8dcSSimon Schubert static char * 3175796c8dcSSimon Schubert extract_identifier (char **expp, int is_parameter) 3185796c8dcSSimon Schubert { 3195796c8dcSSimon Schubert char *result; 3205796c8dcSSimon Schubert char *p = *expp; 3215796c8dcSSimon Schubert unsigned int len; 3225796c8dcSSimon Schubert 3235796c8dcSSimon Schubert if (is_parameter && !strncmp (p, "...", 3)) 3245796c8dcSSimon Schubert { 3255796c8dcSSimon Schubert /* Ok. */ 3265796c8dcSSimon Schubert } 3275796c8dcSSimon Schubert else 3285796c8dcSSimon Schubert { 3295796c8dcSSimon Schubert if (! *p || ! macro_is_identifier_nondigit (*p)) 3305796c8dcSSimon Schubert return NULL; 3315796c8dcSSimon Schubert for (++p; 3325796c8dcSSimon Schubert *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p)); 3335796c8dcSSimon Schubert ++p) 3345796c8dcSSimon Schubert ; 3355796c8dcSSimon Schubert } 3365796c8dcSSimon Schubert 3375796c8dcSSimon Schubert if (is_parameter && !strncmp (p, "...", 3)) 3385796c8dcSSimon Schubert p += 3; 3395796c8dcSSimon Schubert 3405796c8dcSSimon Schubert len = p - *expp; 3415796c8dcSSimon Schubert result = (char *) xmalloc (len + 1); 3425796c8dcSSimon Schubert memcpy (result, *expp, len); 3435796c8dcSSimon Schubert result[len] = '\0'; 3445796c8dcSSimon Schubert *expp += len; 3455796c8dcSSimon Schubert return result; 3465796c8dcSSimon Schubert } 3475796c8dcSSimon Schubert 3485796c8dcSSimon Schubert /* Helper function to clean up a temporarily-constructed macro object. 3495796c8dcSSimon Schubert This assumes that the contents were all allocated with xmalloc. */ 3505796c8dcSSimon Schubert static void 3515796c8dcSSimon Schubert free_macro_definition_ptr (void *ptr) 3525796c8dcSSimon Schubert { 3535796c8dcSSimon Schubert int i; 3545796c8dcSSimon Schubert struct macro_definition *loc = (struct macro_definition *) ptr; 355cf7f2e2dSJohn Marino 3565796c8dcSSimon Schubert for (i = 0; i < loc->argc; ++i) 3575796c8dcSSimon Schubert xfree ((char *) loc->argv[i]); 3585796c8dcSSimon Schubert xfree ((char *) loc->argv); 3595796c8dcSSimon Schubert /* Note that the 'replacement' field is not allocated. */ 3605796c8dcSSimon Schubert } 3615796c8dcSSimon Schubert 3625796c8dcSSimon Schubert static void 3635796c8dcSSimon Schubert macro_define_command (char *exp, int from_tty) 3645796c8dcSSimon Schubert { 3655796c8dcSSimon Schubert struct macro_definition new_macro; 3665796c8dcSSimon Schubert char *name = NULL; 3675796c8dcSSimon Schubert struct cleanup *cleanup_chain; 3685796c8dcSSimon Schubert 3695796c8dcSSimon Schubert if (!exp) 3705796c8dcSSimon Schubert error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]")); 3715796c8dcSSimon Schubert 3725796c8dcSSimon Schubert cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro); 3735796c8dcSSimon Schubert make_cleanup (free_current_contents, &name); 3745796c8dcSSimon Schubert 3755796c8dcSSimon Schubert memset (&new_macro, 0, sizeof (struct macro_definition)); 3765796c8dcSSimon Schubert 3775796c8dcSSimon Schubert skip_ws (&exp); 3785796c8dcSSimon Schubert name = extract_identifier (&exp, 0); 3795796c8dcSSimon Schubert if (! name) 3805796c8dcSSimon Schubert error (_("Invalid macro name.")); 3815796c8dcSSimon Schubert if (*exp == '(') 3825796c8dcSSimon Schubert { 3835796c8dcSSimon Schubert /* Function-like macro. */ 3845796c8dcSSimon Schubert int alloced = 5; 3855796c8dcSSimon Schubert char **argv = (char **) xmalloc (alloced * sizeof (char *)); 3865796c8dcSSimon Schubert 3875796c8dcSSimon Schubert new_macro.kind = macro_function_like; 3885796c8dcSSimon Schubert new_macro.argc = 0; 3895796c8dcSSimon Schubert new_macro.argv = (const char * const *) argv; 3905796c8dcSSimon Schubert 3915796c8dcSSimon Schubert /* Skip the '(' and whitespace. */ 3925796c8dcSSimon Schubert ++exp; 3935796c8dcSSimon Schubert skip_ws (&exp); 3945796c8dcSSimon Schubert 3955796c8dcSSimon Schubert while (*exp != ')') 3965796c8dcSSimon Schubert { 3975796c8dcSSimon Schubert int i; 3985796c8dcSSimon Schubert 3995796c8dcSSimon Schubert if (new_macro.argc == alloced) 4005796c8dcSSimon Schubert { 4015796c8dcSSimon Schubert alloced *= 2; 4025796c8dcSSimon Schubert argv = (char **) xrealloc (argv, alloced * sizeof (char *)); 4035796c8dcSSimon Schubert /* Must update new_macro as well... */ 4045796c8dcSSimon Schubert new_macro.argv = (const char * const *) argv; 4055796c8dcSSimon Schubert } 4065796c8dcSSimon Schubert argv[new_macro.argc] = extract_identifier (&exp, 1); 4075796c8dcSSimon Schubert if (! argv[new_macro.argc]) 4085796c8dcSSimon Schubert error (_("Macro is missing an argument.")); 4095796c8dcSSimon Schubert ++new_macro.argc; 4105796c8dcSSimon Schubert 4115796c8dcSSimon Schubert for (i = new_macro.argc - 2; i >= 0; --i) 4125796c8dcSSimon Schubert { 4135796c8dcSSimon Schubert if (! strcmp (argv[i], argv[new_macro.argc - 1])) 4145796c8dcSSimon Schubert error (_("Two macro arguments with identical names.")); 4155796c8dcSSimon Schubert } 4165796c8dcSSimon Schubert 4175796c8dcSSimon Schubert skip_ws (&exp); 4185796c8dcSSimon Schubert if (*exp == ',') 4195796c8dcSSimon Schubert { 4205796c8dcSSimon Schubert ++exp; 4215796c8dcSSimon Schubert skip_ws (&exp); 4225796c8dcSSimon Schubert } 4235796c8dcSSimon Schubert else if (*exp != ')') 4245796c8dcSSimon Schubert error (_("',' or ')' expected at end of macro arguments.")); 4255796c8dcSSimon Schubert } 4265796c8dcSSimon Schubert /* Skip the closing paren. */ 4275796c8dcSSimon Schubert ++exp; 4285796c8dcSSimon Schubert skip_ws (&exp); 4295796c8dcSSimon Schubert 4305796c8dcSSimon Schubert macro_define_function (macro_main (macro_user_macros), -1, name, 4315796c8dcSSimon Schubert new_macro.argc, (const char **) new_macro.argv, 4325796c8dcSSimon Schubert exp); 4335796c8dcSSimon Schubert } 4345796c8dcSSimon Schubert else 4355796c8dcSSimon Schubert { 4365796c8dcSSimon Schubert skip_ws (&exp); 4375796c8dcSSimon Schubert macro_define_object (macro_main (macro_user_macros), -1, name, exp); 4385796c8dcSSimon Schubert } 4395796c8dcSSimon Schubert 4405796c8dcSSimon Schubert do_cleanups (cleanup_chain); 4415796c8dcSSimon Schubert } 4425796c8dcSSimon Schubert 4435796c8dcSSimon Schubert 4445796c8dcSSimon Schubert static void 4455796c8dcSSimon Schubert macro_undef_command (char *exp, int from_tty) 4465796c8dcSSimon Schubert { 4475796c8dcSSimon Schubert char *name; 4485796c8dcSSimon Schubert 4495796c8dcSSimon Schubert if (!exp) 4505796c8dcSSimon Schubert error (_("usage: macro undef NAME")); 4515796c8dcSSimon Schubert 4525796c8dcSSimon Schubert skip_ws (&exp); 4535796c8dcSSimon Schubert name = extract_identifier (&exp, 0); 4545796c8dcSSimon Schubert if (! name) 4555796c8dcSSimon Schubert error (_("Invalid macro name.")); 4565796c8dcSSimon Schubert macro_undef (macro_main (macro_user_macros), -1, name); 4575796c8dcSSimon Schubert xfree (name); 4585796c8dcSSimon Schubert } 4595796c8dcSSimon Schubert 4605796c8dcSSimon Schubert 4615796c8dcSSimon Schubert static void 4625796c8dcSSimon Schubert print_one_macro (const char *name, const struct macro_definition *macro, 463*a45ae5f8SJohn Marino struct macro_source_file *source, int line, 4645796c8dcSSimon Schubert void *ignore) 4655796c8dcSSimon Schubert { 4665796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "macro define %s", name); 4675796c8dcSSimon Schubert if (macro->kind == macro_function_like) 4685796c8dcSSimon Schubert { 4695796c8dcSSimon Schubert int i; 470cf7f2e2dSJohn Marino 4715796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "("); 4725796c8dcSSimon Schubert for (i = 0; i < macro->argc; ++i) 4735796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "", 4745796c8dcSSimon Schubert macro->argv[i]); 4755796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, ")"); 4765796c8dcSSimon Schubert } 4775796c8dcSSimon Schubert fprintf_filtered (gdb_stdout, " %s\n", macro->replacement); 4785796c8dcSSimon Schubert } 4795796c8dcSSimon Schubert 4805796c8dcSSimon Schubert 4815796c8dcSSimon Schubert static void 4825796c8dcSSimon Schubert macro_list_command (char *exp, int from_tty) 4835796c8dcSSimon Schubert { 4845796c8dcSSimon Schubert macro_for_each (macro_user_macros, print_one_macro, NULL); 4855796c8dcSSimon Schubert } 4865796c8dcSSimon Schubert 4875796c8dcSSimon Schubert 4885796c8dcSSimon Schubert /* Initializing the `macrocmd' module. */ 4895796c8dcSSimon Schubert 4905796c8dcSSimon Schubert extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */ 4915796c8dcSSimon Schubert 4925796c8dcSSimon Schubert void 4935796c8dcSSimon Schubert _initialize_macrocmd (void) 4945796c8dcSSimon Schubert { 4955796c8dcSSimon Schubert /* We introduce a new command prefix, `macro', under which we'll put 4965796c8dcSSimon Schubert the various commands for working with preprocessor macros. */ 4975796c8dcSSimon Schubert add_prefix_cmd ("macro", class_info, macro_command, 4985796c8dcSSimon Schubert _("Prefix for commands dealing with C preprocessor macros."), 4995796c8dcSSimon Schubert ¯olist, "macro ", 0, &cmdlist); 5005796c8dcSSimon Schubert 5015796c8dcSSimon Schubert add_cmd ("expand", no_class, macro_expand_command, _("\ 5025796c8dcSSimon Schubert Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\ 5035796c8dcSSimon Schubert Show the expanded expression."), 5045796c8dcSSimon Schubert ¯olist); 5055796c8dcSSimon Schubert add_alias_cmd ("exp", "expand", no_class, 1, ¯olist); 5065796c8dcSSimon Schubert add_cmd ("expand-once", no_class, macro_expand_once_command, _("\ 5075796c8dcSSimon Schubert Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\ 5085796c8dcSSimon Schubert Show the expanded expression.\n\ 5095796c8dcSSimon Schubert \n\ 5105796c8dcSSimon Schubert This command differs from `macro expand' in that it only expands macro\n\ 5115796c8dcSSimon Schubert invocations that appear directly in EXPRESSION; if expanding a macro\n\ 5125796c8dcSSimon Schubert introduces further macro invocations, those are left unexpanded.\n\ 5135796c8dcSSimon Schubert \n\ 5145796c8dcSSimon Schubert `macro expand-once' helps you see how a particular macro expands,\n\ 5155796c8dcSSimon Schubert whereas `macro expand' shows you how all the macros involved in an\n\ 5165796c8dcSSimon Schubert expression work together to yield a pre-processed expression."), 5175796c8dcSSimon Schubert ¯olist); 5185796c8dcSSimon Schubert add_alias_cmd ("exp1", "expand-once", no_class, 1, ¯olist); 5195796c8dcSSimon Schubert 5205796c8dcSSimon Schubert add_cmd ("macro", no_class, info_macro_command, 521*a45ae5f8SJohn Marino _("Show the definition of MACRO, and it's source location.\n\ 522*a45ae5f8SJohn Marino Usage: info macro [-a|-all] [--] MACRO\n\ 523*a45ae5f8SJohn Marino Options: \n\ 524*a45ae5f8SJohn Marino -a, --all Output all definitions of MACRO in the current compilation\ 525*a45ae5f8SJohn Marino unit.\n\ 526*a45ae5f8SJohn Marino -- Specify the end of arguments and the beginning of the MACRO."), 527*a45ae5f8SJohn Marino 528*a45ae5f8SJohn Marino &infolist); 529*a45ae5f8SJohn Marino 530*a45ae5f8SJohn Marino add_cmd ("macros", no_class, info_macros_command, 531*a45ae5f8SJohn Marino _("Show the definitions of all macros at LINESPEC, or the current \ 532*a45ae5f8SJohn Marino source location.\n\ 533*a45ae5f8SJohn Marino Usage: info macros [LINESPEC]"), 5345796c8dcSSimon Schubert &infolist); 5355796c8dcSSimon Schubert 5365796c8dcSSimon Schubert add_cmd ("define", no_class, macro_define_command, _("\ 5375796c8dcSSimon Schubert Define a new C/C++ preprocessor macro.\n\ 5385796c8dcSSimon Schubert The GDB command `macro define DEFINITION' is equivalent to placing a\n\ 5395796c8dcSSimon Schubert preprocessor directive of the form `#define DEFINITION' such that the\n\ 5405796c8dcSSimon Schubert definition is visible in all the inferior's source files.\n\ 5415796c8dcSSimon Schubert For example:\n\ 5425796c8dcSSimon Schubert (gdb) macro define PI (3.1415926)\n\ 5435796c8dcSSimon Schubert (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"), 5445796c8dcSSimon Schubert ¯olist); 5455796c8dcSSimon Schubert 5465796c8dcSSimon Schubert add_cmd ("undef", no_class, macro_undef_command, _("\ 5475796c8dcSSimon Schubert Remove the definition of the C/C++ preprocessor macro with the given name."), 5485796c8dcSSimon Schubert ¯olist); 5495796c8dcSSimon Schubert 5505796c8dcSSimon Schubert add_cmd ("list", no_class, macro_list_command, 5515796c8dcSSimon Schubert _("List all the macros defined using the `macro define' command."), 5525796c8dcSSimon Schubert ¯olist); 5535796c8dcSSimon Schubert } 554