macrocmd.c 14 KB

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