GDB (API)
/home/stan/gdb/src/gdb/macrocmd.c
Go to the documentation of this file.
00001 /* C preprocessor macro expansion commands for GDB.
00002    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00003    Contributed by Red Hat, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 
00021 #include "defs.h"
00022 #include "macrotab.h"
00023 #include "macroexp.h"
00024 #include "macroscope.h"
00025 #include "cli/cli-utils.h"
00026 #include "command.h"
00027 #include "gdbcmd.h"
00028 #include "gdb_string.h"
00029 #include "linespec.h"
00030 
00031 
00032 /* The `macro' prefix command.  */
00033 
00034 static struct cmd_list_element *macrolist;
00035 
00036 static void
00037 macro_command (char *arg, int from_tty)
00038 {
00039   printf_unfiltered
00040     ("\"macro\" must be followed by the name of a macro command.\n");
00041   help_list (macrolist, "macro ", -1, gdb_stdout);
00042 }
00043 
00044 
00045 
00046 /* Macro expansion commands.  */
00047 
00048 
00049 /* Prints an informational message regarding the lack of macro information.  */
00050 static void
00051 macro_inform_no_debuginfo (void)
00052 {
00053   puts_filtered ("GDB has no preprocessor macro information for that code.\n");
00054 }
00055 
00056 static void
00057 macro_expand_command (char *exp, int from_tty)
00058 {
00059   struct macro_scope *ms = NULL;
00060   char *expanded = NULL;
00061   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
00062 
00063   make_cleanup (free_current_contents, &expanded);
00064 
00065   /* You know, when the user doesn't specify any expression, it would be
00066      really cool if this defaulted to the last expression evaluated.
00067      Then it would be easy to ask, "Hey, what did I just evaluate?"  But
00068      at the moment, the `print' commands don't save the last expression
00069      evaluated, just its value.  */
00070   if (! exp || ! *exp)
00071     error (_("You must follow the `macro expand' command with the"
00072            " expression you\n"
00073            "want to expand."));
00074 
00075   ms = default_macro_scope ();
00076   if (ms)
00077     {
00078       expanded = macro_expand (exp, standard_macro_lookup, ms);
00079       fputs_filtered ("expands to: ", gdb_stdout);
00080       fputs_filtered (expanded, gdb_stdout);
00081       fputs_filtered ("\n", gdb_stdout);
00082     }
00083   else
00084     macro_inform_no_debuginfo ();
00085 
00086   do_cleanups (cleanup_chain);
00087   return;
00088 }
00089 
00090 
00091 static void
00092 macro_expand_once_command (char *exp, int from_tty)
00093 {
00094   struct macro_scope *ms = NULL;
00095   char *expanded = NULL;
00096   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
00097   make_cleanup (free_current_contents, &expanded);
00098 
00099   /* You know, when the user doesn't specify any expression, it would be
00100      really cool if this defaulted to the last expression evaluated.
00101      And it should set the once-expanded text as the new `last
00102      expression'.  That way, you could just hit return over and over and
00103      see the expression expanded one level at a time.  */
00104   if (! exp || ! *exp)
00105     error (_("You must follow the `macro expand-once' command with"
00106            " the expression\n"
00107            "you want to expand."));
00108 
00109   ms = default_macro_scope ();
00110   if (ms)
00111     {
00112       expanded = macro_expand_once (exp, standard_macro_lookup, ms);
00113       fputs_filtered ("expands to: ", gdb_stdout);
00114       fputs_filtered (expanded, gdb_stdout);
00115       fputs_filtered ("\n", gdb_stdout);
00116     }
00117   else
00118     macro_inform_no_debuginfo ();
00119 
00120   do_cleanups (cleanup_chain);
00121   return;
00122 }
00123 
00124 /*  Outputs the include path of a macro starting at FILE and LINE to STREAM.
00125 
00126     Care should be taken that this function does not cause any lookups into
00127     the splay tree so that it can be safely used while iterating.  */
00128 static void
00129 show_pp_source_pos (struct ui_file *stream,
00130                     struct macro_source_file *file,
00131                     int line)
00132 {
00133   char *fullname;
00134 
00135   fullname = macro_source_fullname (file);
00136   fprintf_filtered (stream, "%s:%d\n", fullname, line);
00137   xfree (fullname);
00138 
00139   while (file->included_by)
00140     {
00141       fullname = macro_source_fullname (file->included_by);
00142       fprintf_filtered (gdb_stdout, "  included at %s:%d\n", fullname,
00143                         file->included_at_line);
00144       xfree (fullname);
00145       file = file->included_by;
00146     }
00147 }
00148 
00149 /* Outputs a macro for human consumption, detailing the include path
00150    and macro definition.  NAME is the name of the macro.
00151    D the definition.  FILE the start of the include path, and LINE the
00152    line number in FILE.
00153 
00154    Care should be taken that this function does not cause any lookups into
00155    the splay tree so that it can be safely used while iterating.  */
00156 static void
00157 print_macro_definition (const char *name,
00158                         const struct macro_definition *d,
00159                         struct macro_source_file *file,
00160                         int line)
00161 {
00162       fprintf_filtered (gdb_stdout, "Defined at ");
00163       show_pp_source_pos (gdb_stdout, file, line);
00164 
00165       if (line != 0)
00166         fprintf_filtered (gdb_stdout, "#define %s", name);
00167       else
00168         fprintf_filtered (gdb_stdout, "-D%s", name);
00169 
00170       if (d->kind == macro_function_like)
00171         {
00172           int i;
00173 
00174           fputs_filtered ("(", gdb_stdout);
00175           for (i = 0; i < d->argc; i++)
00176             {
00177               fputs_filtered (d->argv[i], gdb_stdout);
00178               if (i + 1 < d->argc)
00179                 fputs_filtered (", ", gdb_stdout);
00180             }
00181           fputs_filtered (")", gdb_stdout);
00182         }
00183 
00184       if (line != 0)
00185         fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
00186       else
00187         fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
00188 }
00189 
00190 /* A callback function for usage with macro_for_each and friends.
00191    If USER_DATA is null all macros will be printed.
00192    Otherwise USER_DATA is considered to be a string, printing
00193    only macros who's NAME matches USER_DATA.  Other arguments are
00194    routed to print_macro_definition.  */
00195 static void
00196 print_macro_callback (const char *name, const struct macro_definition *macro,
00197                    struct macro_source_file *source, int line,
00198                    void *user_data)
00199 {
00200   if (! user_data || strcmp (user_data, name) == 0)
00201     print_macro_definition (name, macro, source, line);
00202 }
00203 
00204 /* The implementation of the `info macro' command.  */
00205 static void
00206 info_macro_command (char *args, int from_tty)
00207 {
00208   struct macro_scope *ms = NULL;
00209   struct cleanup *cleanup_chain;
00210   char *name;
00211   int show_all_macros_named = 0;
00212   char *arg_start = args;
00213   int processing_args = 1;
00214 
00215   while (processing_args
00216          && arg_start && *arg_start == '-' && *arg_start != '\0')
00217     {
00218       char *p = skip_to_space (arg_start);
00219 
00220       if (strncmp (arg_start, "-a", p - arg_start) == 0
00221           || strncmp (arg_start, "-all", p - arg_start) == 0)
00222         show_all_macros_named = 1;
00223       else if (strncmp (arg_start, "--", p - arg_start) == 0)
00224           /* Our macro support seems rather C specific but this would
00225              seem necessary for languages allowing - in macro names.
00226              e.g. Scheme's (defmacro ->foo () "bar\n")  */
00227         processing_args = 0;
00228       else
00229         {
00230           /* Relies on modified 'args' not making it in to history */
00231           *p = '\0';
00232           error (_("Unrecognized option '%s' to info macro command.  "
00233                    "Try \"help info macro\"."), arg_start);
00234         }
00235 
00236         arg_start = skip_spaces (p);
00237     }
00238 
00239   name = arg_start;
00240 
00241   if (! name || ! *name)
00242     error (_("You must follow the `info macro' command with the name"
00243              " of the macro\n"
00244              "whose definition you want to see."));
00245 
00246   ms = default_macro_scope ();
00247   cleanup_chain = make_cleanup (free_current_contents, &ms);
00248 
00249   if (! ms)
00250     macro_inform_no_debuginfo ();
00251   else if (show_all_macros_named)
00252     macro_for_each (ms->file->table, print_macro_callback, name);
00253   else
00254     {
00255       struct macro_definition *d;
00256 
00257       d = macro_lookup_definition (ms->file, ms->line, name);
00258       if (d)
00259         {
00260           int line;
00261           struct macro_source_file *file
00262             = macro_definition_location (ms->file, ms->line, name, &line);
00263 
00264           print_macro_definition (name, d, file, line);
00265         }
00266       else
00267         {
00268           fprintf_filtered (gdb_stdout,
00269                             "The symbol `%s' has no definition as a C/C++"
00270                             " preprocessor macro\n"
00271                             "at ", name);
00272           show_pp_source_pos (gdb_stdout, ms->file, ms->line);
00273         }
00274     }
00275 
00276   do_cleanups (cleanup_chain);
00277 }
00278 
00279 /* Implementation of the "info macros" command. */
00280 static void
00281 info_macros_command (char *args, int from_tty)
00282 {
00283   struct macro_scope *ms = NULL;
00284   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
00285 
00286   if (args == NULL)
00287     ms = default_macro_scope ();
00288   else
00289     {
00290       struct symtabs_and_lines sals =
00291         decode_line_with_current_source (args, 0);
00292 
00293       if (sals.nelts)
00294         ms = sal_macro_scope (sals.sals[0]);
00295     }
00296 
00297   if (! ms || ! ms->file || ! ms->file->table)
00298     macro_inform_no_debuginfo ();
00299   else
00300     macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
00301 
00302   do_cleanups (cleanup_chain);
00303 }
00304 
00305 
00306 /* User-defined macros.  */
00307 
00308 static void
00309 skip_ws (char **expp)
00310 {
00311   while (macro_is_whitespace (**expp))
00312     ++*expp;
00313 }
00314 
00315 /* Try to find the bounds of an identifier.  If an identifier is
00316    found, returns a newly allocated string; otherwise returns NULL.
00317    EXPP is a pointer to an input string; it is updated to point to the
00318    text following the identifier.  If IS_PARAMETER is true, this
00319    function will also allow "..." forms as used in varargs macro
00320    parameters.  */
00321 
00322 static char *
00323 extract_identifier (char **expp, int is_parameter)
00324 {
00325   char *result;
00326   char *p = *expp;
00327   unsigned int len;
00328 
00329   if (is_parameter && !strncmp (p, "...", 3))
00330     {
00331       /* Ok.  */
00332     }
00333   else
00334     {
00335       if (! *p || ! macro_is_identifier_nondigit (*p))
00336         return NULL;
00337       for (++p;
00338            *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
00339            ++p)
00340         ;
00341     }
00342 
00343   if (is_parameter && !strncmp (p, "...", 3))      
00344     p += 3;
00345 
00346   len = p - *expp;
00347   result = (char *) xmalloc (len + 1);
00348   memcpy (result, *expp, len);
00349   result[len] = '\0';
00350   *expp += len;
00351   return result;
00352 }
00353 
00354 /* Helper function to clean up a temporarily-constructed macro object.
00355    This assumes that the contents were all allocated with xmalloc.  */
00356 static void
00357 free_macro_definition_ptr (void *ptr)
00358 {
00359   int i;
00360   struct macro_definition *loc = (struct macro_definition *) ptr;
00361 
00362   for (i = 0; i < loc->argc; ++i)
00363     xfree ((char *) loc->argv[i]);
00364   xfree ((char *) loc->argv);
00365   /* Note that the 'replacement' field is not allocated.  */
00366 }
00367 
00368 static void
00369 macro_define_command (char *exp, int from_tty)
00370 {
00371   struct macro_definition new_macro;
00372   char *name = NULL;
00373   struct cleanup *cleanup_chain;
00374 
00375   if (!exp)
00376     error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
00377 
00378   cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
00379   make_cleanup (free_current_contents, &name);
00380 
00381   memset (&new_macro, 0, sizeof (struct macro_definition));
00382 
00383   skip_ws (&exp);
00384   name = extract_identifier (&exp, 0);
00385   if (! name)
00386     error (_("Invalid macro name."));
00387   if (*exp == '(')
00388     {
00389       /* Function-like macro.  */
00390       int alloced = 5;
00391       char **argv = (char **) xmalloc (alloced * sizeof (char *));
00392 
00393       new_macro.kind = macro_function_like;
00394       new_macro.argc = 0;
00395       new_macro.argv = (const char * const *) argv;
00396 
00397       /* Skip the '(' and whitespace.  */
00398       ++exp;
00399       skip_ws (&exp);
00400 
00401       while (*exp != ')')
00402         {
00403           int i;
00404 
00405           if (new_macro.argc == alloced)
00406             {
00407               alloced *= 2;
00408               argv = (char **) xrealloc (argv, alloced * sizeof (char *));
00409               /* Must update new_macro as well...  */
00410               new_macro.argv = (const char * const *) argv;
00411             }
00412           argv[new_macro.argc] = extract_identifier (&exp, 1);
00413           if (! argv[new_macro.argc])
00414             error (_("Macro is missing an argument."));
00415           ++new_macro.argc;
00416 
00417           for (i = new_macro.argc - 2; i >= 0; --i)
00418             {
00419               if (! strcmp (argv[i], argv[new_macro.argc - 1]))
00420                 error (_("Two macro arguments with identical names."));
00421             }
00422 
00423           skip_ws (&exp);
00424           if (*exp == ',')
00425             {
00426               ++exp;
00427               skip_ws (&exp);
00428             }
00429           else if (*exp != ')')
00430             error (_("',' or ')' expected at end of macro arguments."));
00431         }
00432       /* Skip the closing paren.  */
00433       ++exp;
00434       skip_ws (&exp);
00435 
00436       macro_define_function (macro_main (macro_user_macros), -1, name,
00437                              new_macro.argc, (const char **) new_macro.argv,
00438                              exp);
00439     }
00440   else
00441     {
00442       skip_ws (&exp);
00443       macro_define_object (macro_main (macro_user_macros), -1, name, exp);
00444     }
00445 
00446   do_cleanups (cleanup_chain);
00447 }
00448 
00449 
00450 static void
00451 macro_undef_command (char *exp, int from_tty)
00452 {
00453   char *name;
00454 
00455   if (!exp)
00456     error (_("usage: macro undef NAME"));
00457 
00458   skip_ws (&exp);
00459   name = extract_identifier (&exp, 0);
00460   if (! name)
00461     error (_("Invalid macro name."));
00462   macro_undef (macro_main (macro_user_macros), -1, name);
00463   xfree (name);
00464 }
00465 
00466 
00467 static void
00468 print_one_macro (const char *name, const struct macro_definition *macro,
00469                  struct macro_source_file *source, int line,
00470                  void *ignore)
00471 {
00472   fprintf_filtered (gdb_stdout, "macro define %s", name);
00473   if (macro->kind == macro_function_like)
00474     {
00475       int i;
00476 
00477       fprintf_filtered (gdb_stdout, "(");
00478       for (i = 0; i < macro->argc; ++i)
00479         fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
00480                           macro->argv[i]);
00481       fprintf_filtered (gdb_stdout, ")");
00482     }
00483   fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
00484 }
00485 
00486 
00487 static void
00488 macro_list_command (char *exp, int from_tty)
00489 {
00490   macro_for_each (macro_user_macros, print_one_macro, NULL);
00491 }
00492 
00493 
00494 /* Initializing the `macrocmd' module.  */
00495 
00496 extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
00497 
00498 void
00499 _initialize_macrocmd (void)
00500 {
00501   /* We introduce a new command prefix, `macro', under which we'll put
00502      the various commands for working with preprocessor macros.  */
00503   add_prefix_cmd ("macro", class_info, macro_command,
00504                   _("Prefix for commands dealing with C preprocessor macros."),
00505                   &macrolist, "macro ", 0, &cmdlist);
00506 
00507   add_cmd ("expand", no_class, macro_expand_command, _("\
00508 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
00509 Show the expanded expression."),
00510            &macrolist);
00511   add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
00512   add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
00513 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
00514 Show the expanded expression.\n\
00515 \n\
00516 This command differs from `macro expand' in that it only expands macro\n\
00517 invocations that appear directly in EXPRESSION; if expanding a macro\n\
00518 introduces further macro invocations, those are left unexpanded.\n\
00519 \n\
00520 `macro expand-once' helps you see how a particular macro expands,\n\
00521 whereas `macro expand' shows you how all the macros involved in an\n\
00522 expression work together to yield a pre-processed expression."),
00523            &macrolist);
00524   add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
00525 
00526   add_cmd ("macro", no_class, info_macro_command,
00527            _("Show the definition of MACRO, and it's source location.\n\
00528 Usage: info macro [-a|-all] [--] MACRO\n\
00529 Options: \n\
00530   -a, --all    Output all definitions of MACRO in the current compilation\
00531  unit.\n\
00532   --           Specify the end of arguments and the beginning of the MACRO."),
00533 
00534            &infolist);
00535 
00536   add_cmd ("macros", no_class, info_macros_command,
00537            _("Show the definitions of all macros at LINESPEC, or the current \
00538 source location.\n\
00539 Usage: info macros [LINESPEC]"),
00540            &infolist);
00541 
00542   add_cmd ("define", no_class, macro_define_command, _("\
00543 Define a new C/C++ preprocessor macro.\n\
00544 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
00545 preprocessor directive of the form `#define DEFINITION' such that the\n\
00546 definition is visible in all the inferior's source files.\n\
00547 For example:\n\
00548   (gdb) macro define PI (3.1415926)\n\
00549   (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
00550            &macrolist);
00551 
00552   add_cmd ("undef", no_class, macro_undef_command, _("\
00553 Remove the definition of the C/C++ preprocessor macro with the given name."),
00554            &macrolist);
00555 
00556   add_cmd ("list", no_class, macro_list_command,
00557            _("List all the macros defined using the `macro define' command."),
00558            &macrolist);
00559 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines