gdb/macrocmd.c - gdb

Global variables defined

Functions defined

Source code

  1. /* C preprocessor macro expansion commands for GDB.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  15. #include "defs.h"
  16. #include "macrotab.h"
  17. #include "macroexp.h"
  18. #include "macroscope.h"
  19. #include "cli/cli-utils.h"
  20. #include "command.h"
  21. #include "gdbcmd.h"
  22. #include "linespec.h"


  23. /* The `macro' prefix command.  */

  24. static struct cmd_list_element *macrolist;

  25. static void
  26. macro_command (char *arg, int from_tty)
  27. {
  28.   printf_unfiltered
  29.     ("\"macro\" must be followed by the name of a macro command.\n");
  30.   help_list (macrolist, "macro ", all_commands, gdb_stdout);
  31. }



  32. /* Macro expansion commands.  */


  33. /* Prints an informational message regarding the lack of macro information.  */
  34. static void
  35. macro_inform_no_debuginfo (void)
  36. {
  37.   puts_filtered ("GDB has no preprocessor macro information for that code.\n");
  38. }

  39. static void
  40. macro_expand_command (char *exp, int from_tty)
  41. {
  42.   struct macro_scope *ms = NULL;
  43.   char *expanded = NULL;
  44.   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);

  45.   make_cleanup (free_current_contents, &expanded);

  46.   /* You know, when the user doesn't specify any expression, it would be
  47.      really cool if this defaulted to the last expression evaluated.
  48.      Then it would be easy to ask, "Hey, what did I just evaluate?"  But
  49.      at the moment, the `print' commands don't save the last expression
  50.      evaluated, just its value.  */
  51.   if (! exp || ! *exp)
  52.     error (_("You must follow the `macro expand' command with the"
  53.            " expression you\n"
  54.            "want to expand."));

  55.   ms = default_macro_scope ();
  56.   if (ms)
  57.     {
  58.       expanded = macro_expand (exp, standard_macro_lookup, ms);
  59.       fputs_filtered ("expands to: ", gdb_stdout);
  60.       fputs_filtered (expanded, gdb_stdout);
  61.       fputs_filtered ("\n", gdb_stdout);
  62.     }
  63.   else
  64.     macro_inform_no_debuginfo ();

  65.   do_cleanups (cleanup_chain);
  66.   return;
  67. }


  68. static void
  69. macro_expand_once_command (char *exp, int from_tty)
  70. {
  71.   struct macro_scope *ms = NULL;
  72.   char *expanded = NULL;
  73.   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
  74.   make_cleanup (free_current_contents, &expanded);

  75.   /* You know, when the user doesn't specify any expression, it would be
  76.      really cool if this defaulted to the last expression evaluated.
  77.      And it should set the once-expanded text as the new `last
  78.      expression'.  That way, you could just hit return over and over and
  79.      see the expression expanded one level at a time.  */
  80.   if (! exp || ! *exp)
  81.     error (_("You must follow the `macro expand-once' command with"
  82.            " the expression\n"
  83.            "you want to expand."));

  84.   ms = default_macro_scope ();
  85.   if (ms)
  86.     {
  87.       expanded = macro_expand_once (exp, standard_macro_lookup, ms);
  88.       fputs_filtered ("expands to: ", gdb_stdout);
  89.       fputs_filtered (expanded, gdb_stdout);
  90.       fputs_filtered ("\n", gdb_stdout);
  91.     }
  92.   else
  93.     macro_inform_no_debuginfo ();

  94.   do_cleanups (cleanup_chain);
  95.   return;
  96. }

  97. /*  Outputs the include path of a macro starting at FILE and LINE to STREAM.

  98.     Care should be taken that this function does not cause any lookups into
  99.     the splay tree so that it can be safely used while iterating.  */
  100. static void
  101. show_pp_source_pos (struct ui_file *stream,
  102.                     struct macro_source_file *file,
  103.                     int line)
  104. {
  105.   char *fullname;

  106.   fullname = macro_source_fullname (file);
  107.   fprintf_filtered (stream, "%s:%d\n", fullname, line);
  108.   xfree (fullname);

  109.   while (file->included_by)
  110.     {
  111.       fullname = macro_source_fullname (file->included_by);
  112.       fprintf_filtered (gdb_stdout, "  included at %s:%d\n", fullname,
  113.                         file->included_at_line);
  114.       xfree (fullname);
  115.       file = file->included_by;
  116.     }
  117. }

  118. /* Outputs a macro for human consumption, detailing the include path
  119.    and macro definition.  NAME is the name of the macro.
  120.    D the definition.  FILE the start of the include path, and LINE the
  121.    line number in FILE.

  122.    Care should be taken that this function does not cause any lookups into
  123.    the splay tree so that it can be safely used while iterating.  */
  124. static void
  125. print_macro_definition (const char *name,
  126.                         const struct macro_definition *d,
  127.                         struct macro_source_file *file,
  128.                         int line)
  129. {
  130.   fprintf_filtered (gdb_stdout, "Defined at ");
  131.   show_pp_source_pos (gdb_stdout, file, line);

  132.   if (line != 0)
  133.     fprintf_filtered (gdb_stdout, "#define %s", name);
  134.   else
  135.     fprintf_filtered (gdb_stdout, "-D%s", name);

  136.   if (d->kind == macro_function_like)
  137.     {
  138.       int i;

  139.       fputs_filtered ("(", gdb_stdout);
  140.       for (i = 0; i < d->argc; i++)
  141.         {
  142.           fputs_filtered (d->argv[i], gdb_stdout);
  143.           if (i + 1 < d->argc)
  144.             fputs_filtered (", ", gdb_stdout);
  145.         }
  146.       fputs_filtered (")", gdb_stdout);
  147.     }

  148.   if (line != 0)
  149.     fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
  150.   else
  151.     fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
  152. }

  153. /* A callback function for usage with macro_for_each and friends.
  154.    If USER_DATA is null all macros will be printed.
  155.    Otherwise USER_DATA is considered to be a string, printing
  156.    only macros who's NAME matches USER_DATA.  Other arguments are
  157.    routed to print_macro_definition.  */
  158. static void
  159. print_macro_callback (const char *name, const struct macro_definition *macro,
  160.                    struct macro_source_file *source, int line,
  161.                    void *user_data)
  162. {
  163.   if (! user_data || strcmp (user_data, name) == 0)
  164.     print_macro_definition (name, macro, source, line);
  165. }

  166. /* The implementation of the `info macro' command.  */
  167. static void
  168. info_macro_command (char *args, int from_tty)
  169. {
  170.   struct macro_scope *ms = NULL;
  171.   struct cleanup *cleanup_chain;
  172.   char *name;
  173.   int show_all_macros_named = 0;
  174.   char *arg_start = args;
  175.   int processing_args = 1;

  176.   while (processing_args
  177.          && arg_start && *arg_start == '-' && *arg_start != '\0')
  178.     {
  179.       char *p = skip_to_space (arg_start);

  180.       if (strncmp (arg_start, "-a", p - arg_start) == 0
  181.           || strncmp (arg_start, "-all", p - arg_start) == 0)
  182.         show_all_macros_named = 1;
  183.       else if (strncmp (arg_start, "--", p - arg_start) == 0)
  184.           /* Our macro support seems rather C specific but this would
  185.              seem necessary for languages allowing - in macro names.
  186.              e.g. Scheme's (defmacro ->foo () "bar\n")  */
  187.         processing_args = 0;
  188.       else
  189.         {
  190.           /* Relies on modified 'args' not making it in to history */
  191.           *p = '\0';
  192.           error (_("Unrecognized option '%s' to info macro command.  "
  193.                    "Try \"help info macro\"."), arg_start);
  194.         }

  195.         arg_start = skip_spaces (p);
  196.     }

  197.   name = arg_start;

  198.   if (! name || ! *name)
  199.     error (_("You must follow the `info macro' command with the name"
  200.              " of the macro\n"
  201.              "whose definition you want to see."));

  202.   ms = default_macro_scope ();
  203.   cleanup_chain = make_cleanup (free_current_contents, &ms);

  204.   if (! ms)
  205.     macro_inform_no_debuginfo ();
  206.   else if (show_all_macros_named)
  207.     macro_for_each (ms->file->table, print_macro_callback, name);
  208.   else
  209.     {
  210.       struct macro_definition *d;

  211.       d = macro_lookup_definition (ms->file, ms->line, name);
  212.       if (d)
  213.         {
  214.           int line;
  215.           struct macro_source_file *file
  216.             = macro_definition_location (ms->file, ms->line, name, &line);

  217.           print_macro_definition (name, d, file, line);
  218.         }
  219.       else
  220.         {
  221.           fprintf_filtered (gdb_stdout,
  222.                             "The symbol `%s' has no definition as a C/C++"
  223.                             " preprocessor macro\n"
  224.                             "at ", name);
  225.           show_pp_source_pos (gdb_stdout, ms->file, ms->line);
  226.         }
  227.     }

  228.   do_cleanups (cleanup_chain);
  229. }

  230. /* Implementation of the "info macros" command. */
  231. static void
  232. info_macros_command (char *args, int from_tty)
  233. {
  234.   struct macro_scope *ms = NULL;
  235.   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);

  236.   if (args == NULL)
  237.     ms = default_macro_scope ();
  238.   else
  239.     {
  240.       struct symtabs_and_lines sals =
  241.         decode_line_with_current_source (args, 0);

  242.       if (sals.nelts)
  243.         ms = sal_macro_scope (sals.sals[0]);
  244.     }

  245.   if (! ms || ! ms->file || ! ms->file->table)
  246.     macro_inform_no_debuginfo ();
  247.   else
  248.     macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);

  249.   do_cleanups (cleanup_chain);
  250. }


  251. /* User-defined macros.  */

  252. static void
  253. skip_ws (char **expp)
  254. {
  255.   while (macro_is_whitespace (**expp))
  256.     ++*expp;
  257. }

  258. /* Try to find the bounds of an identifier.  If an identifier is
  259.    found, returns a newly allocated string; otherwise returns NULL.
  260.    EXPP is a pointer to an input string; it is updated to point to the
  261.    text following the identifier.  If IS_PARAMETER is true, this
  262.    function will also allow "..." forms as used in varargs macro
  263.    parameters.  */

  264. static char *
  265. extract_identifier (char **expp, int is_parameter)
  266. {
  267.   char *result;
  268.   char *p = *expp;
  269.   unsigned int len;

  270.   if (is_parameter && !strncmp (p, "...", 3))
  271.     {
  272.       /* Ok.  */
  273.     }
  274.   else
  275.     {
  276.       if (! *p || ! macro_is_identifier_nondigit (*p))
  277.         return NULL;
  278.       for (++p;
  279.            *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
  280.            ++p)
  281.         ;
  282.     }

  283.   if (is_parameter && !strncmp (p, "...", 3))
  284.     p += 3;

  285.   len = p - *expp;
  286.   result = (char *) xmalloc (len + 1);
  287.   memcpy (result, *expp, len);
  288.   result[len] = '\0';
  289.   *expp += len;
  290.   return result;
  291. }

  292. /* Helper function to clean up a temporarily-constructed macro object.
  293.    This assumes that the contents were all allocated with xmalloc.  */
  294. static void
  295. free_macro_definition_ptr (void *ptr)
  296. {
  297.   int i;
  298.   struct macro_definition *loc = (struct macro_definition *) ptr;

  299.   for (i = 0; i < loc->argc; ++i)
  300.     xfree ((char *) loc->argv[i]);
  301.   xfree ((char *) loc->argv);
  302.   /* Note that the 'replacement' field is not allocated.  */
  303. }

  304. static void
  305. macro_define_command (char *exp, int from_tty)
  306. {
  307.   struct macro_definition new_macro;
  308.   char *name = NULL;
  309.   struct cleanup *cleanup_chain;

  310.   if (!exp)
  311.     error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));

  312.   cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
  313.   make_cleanup (free_current_contents, &name);

  314.   memset (&new_macro, 0, sizeof (struct macro_definition));

  315.   skip_ws (&exp);
  316.   name = extract_identifier (&exp, 0);
  317.   if (! name)
  318.     error (_("Invalid macro name."));
  319.   if (*exp == '(')
  320.     {
  321.       /* Function-like macro.  */
  322.       int alloced = 5;
  323.       char **argv = (char **) xmalloc (alloced * sizeof (char *));

  324.       new_macro.kind = macro_function_like;
  325.       new_macro.argc = 0;
  326.       new_macro.argv = (const char * const *) argv;

  327.       /* Skip the '(' and whitespace.  */
  328.       ++exp;
  329.       skip_ws (&exp);

  330.       while (*exp != ')')
  331.         {
  332.           int i;

  333.           if (new_macro.argc == alloced)
  334.             {
  335.               alloced *= 2;
  336.               argv = (char **) xrealloc (argv, alloced * sizeof (char *));
  337.               /* Must update new_macro as well...  */
  338.               new_macro.argv = (const char * const *) argv;
  339.             }
  340.           argv[new_macro.argc] = extract_identifier (&exp, 1);
  341.           if (! argv[new_macro.argc])
  342.             error (_("Macro is missing an argument."));
  343.           ++new_macro.argc;

  344.           for (i = new_macro.argc - 2; i >= 0; --i)
  345.             {
  346.               if (! strcmp (argv[i], argv[new_macro.argc - 1]))
  347.                 error (_("Two macro arguments with identical names."));
  348.             }

  349.           skip_ws (&exp);
  350.           if (*exp == ',')
  351.             {
  352.               ++exp;
  353.               skip_ws (&exp);
  354.             }
  355.           else if (*exp != ')')
  356.             error (_("',' or ')' expected at end of macro arguments."));
  357.         }
  358.       /* Skip the closing paren.  */
  359.       ++exp;
  360.       skip_ws (&exp);

  361.       macro_define_function (macro_main (macro_user_macros), -1, name,
  362.                              new_macro.argc, (const char **) new_macro.argv,
  363.                              exp);
  364.     }
  365.   else
  366.     {
  367.       skip_ws (&exp);
  368.       macro_define_object (macro_main (macro_user_macros), -1, name, exp);
  369.     }

  370.   do_cleanups (cleanup_chain);
  371. }


  372. static void
  373. macro_undef_command (char *exp, int from_tty)
  374. {
  375.   char *name;

  376.   if (!exp)
  377.     error (_("usage: macro undef NAME"));

  378.   skip_ws (&exp);
  379.   name = extract_identifier (&exp, 0);
  380.   if (! name)
  381.     error (_("Invalid macro name."));
  382.   macro_undef (macro_main (macro_user_macros), -1, name);
  383.   xfree (name);
  384. }


  385. static void
  386. print_one_macro (const char *name, const struct macro_definition *macro,
  387.                  struct macro_source_file *source, int line,
  388.                  void *ignore)
  389. {
  390.   fprintf_filtered (gdb_stdout, "macro define %s", name);
  391.   if (macro->kind == macro_function_like)
  392.     {
  393.       int i;

  394.       fprintf_filtered (gdb_stdout, "(");
  395.       for (i = 0; i < macro->argc; ++i)
  396.         fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
  397.                           macro->argv[i]);
  398.       fprintf_filtered (gdb_stdout, ")");
  399.     }
  400.   fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
  401. }


  402. static void
  403. macro_list_command (char *exp, int from_tty)
  404. {
  405.   macro_for_each (macro_user_macros, print_one_macro, NULL);
  406. }


  407. /* Initializing the `macrocmd' module.  */

  408. extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */

  409. void
  410. _initialize_macrocmd (void)
  411. {
  412.   /* We introduce a new command prefix, `macro', under which we'll put
  413.      the various commands for working with preprocessor macros.  */
  414.   add_prefix_cmd ("macro", class_info, macro_command,
  415.                   _("Prefix for commands dealing with C preprocessor macros."),
  416.                   &macrolist, "macro ", 0, &cmdlist);

  417.   add_cmd ("expand", no_class, macro_expand_command, _("\
  418. Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
  419. Show the expanded expression."),
  420.            &macrolist);
  421.   add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
  422.   add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
  423. Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
  424. Show the expanded expression.\n\
  425. \n\
  426. This command differs from `macro expand' in that it only expands macro\n\
  427. invocations that appear directly in EXPRESSION; if expanding a macro\n\
  428. introduces further macro invocations, those are left unexpanded.\n\
  429. \n\
  430. `macro expand-once' helps you see how a particular macro expands,\n\
  431. whereas `macro expand' shows you how all the macros involved in an\n\
  432. expression work together to yield a pre-processed expression."),
  433.            &macrolist);
  434.   add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);

  435.   add_cmd ("macro", no_class, info_macro_command,
  436.            _("Show the definition of MACRO, and it's source location.\n\
  437. Usage: info macro [-a|-all] [--] MACRO\n\
  438. Options: \n\
  439.   -a, --all    Output all definitions of MACRO in the current compilation\
  440. unit.\n\
  441.   --           Specify the end of arguments and the beginning of the MACRO."),

  442.            &infolist);

  443.   add_cmd ("macros", no_class, info_macros_command,
  444.            _("Show the definitions of all macros at LINESPEC, or the current \
  445. source location.\n\
  446. Usage: info macros [LINESPEC]"),
  447.            &infolist);

  448.   add_cmd ("define", no_class, macro_define_command, _("\
  449. Define a new C/C++ preprocessor macro.\n\
  450. The GDB command `macro define DEFINITION' is equivalent to placing a\n\
  451. preprocessor directive of the form `#define DEFINITION' such that the\n\
  452. definition is visible in all the inferior's source files.\n\
  453. For example:\n\
  454.   (gdb) macro define PI (3.1415926)\n\
  455.   (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
  456.            &macrolist);

  457.   add_cmd ("undef", no_class, macro_undef_command, _("\
  458. Remove the definition of the C/C++ preprocessor macro with the given name."),
  459.            &macrolist);

  460.   add_cmd ("list", no_class, macro_list_command,
  461.            _("List all the macros defined using the `macro define' command."),
  462.            &macrolist);
  463. }