gdb-demangle.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Basic C++ demangling support for GDB.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Fred Fish at Cygnus Support.
  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. /* This file contains support code for C++ demangling that is common
  16. to a styles of demangling, and GDB specific. */
  17. #include "defs.h"
  18. #include "cli/cli-utils.h" /* for skip_to_space */
  19. #include "command.h"
  20. #include "gdbcmd.h"
  21. #include "demangle.h"
  22. #include "gdb-demangle.h"
  23. #include "language.h"
  24. /* Select the default C++ demangling style to use. The default is "auto",
  25. which allows gdb to attempt to pick an appropriate demangling style for
  26. the executable it has loaded. It can be set to a specific style ("gnu",
  27. "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
  28. selection of the style unless you do an explicit "set demangle auto".
  29. To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
  30. the appropriate target configuration file. */
  31. #ifndef DEFAULT_DEMANGLING_STYLE
  32. #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
  33. #endif
  34. /* See documentation in gdb-demangle.h. */
  35. bool demangle = true;
  36. static void
  37. show_demangle (struct ui_file *file, int from_tty,
  38. struct cmd_list_element *c, const char *value)
  39. {
  40. gdb_printf (file,
  41. _("Demangling of encoded C++/ObjC names "
  42. "when displaying symbols is %s.\n"),
  43. value);
  44. }
  45. /* See documentation in gdb-demangle.h. */
  46. bool asm_demangle = false;
  47. static void
  48. show_asm_demangle (struct ui_file *file, int from_tty,
  49. struct cmd_list_element *c, const char *value)
  50. {
  51. gdb_printf (file,
  52. _("Demangling of C++/ObjC names in "
  53. "disassembly listings is %s.\n"),
  54. value);
  55. }
  56. /* String name for the current demangling style. Set by the
  57. "set demangle-style" command, printed as part of the output by the
  58. "show demangle-style" command. */
  59. static const char *current_demangling_style_string;
  60. /* The array of names of the known demangling styles. Generated by
  61. _initialize_demangler from libiberty_demanglers[] array. */
  62. static const char **demangling_style_names;
  63. static void
  64. show_demangling_style_names(struct ui_file *file, int from_tty,
  65. struct cmd_list_element *c, const char *value)
  66. {
  67. gdb_printf (file, _("The current C++ demangling style is \"%s\".\n"),
  68. value);
  69. }
  70. /* Set current demangling style. Called by the "set demangle-style"
  71. command after it has updated the current_demangling_style_string to
  72. match what the user has entered.
  73. If the user has entered a string that matches a known demangling style
  74. name in the demanglers[] array then just leave the string alone and update
  75. the current_demangling_style enum value to match.
  76. If the user has entered a string that doesn't match, including an empty
  77. string, then print a list of the currently known styles and restore
  78. the current_demangling_style_string to match the current_demangling_style
  79. enum value.
  80. Note: Assumes that current_demangling_style_string always points to
  81. a malloc'd string, even if it is a null-string. */
  82. static void
  83. set_demangling_command (const char *ignore,
  84. int from_tty, struct cmd_list_element *c)
  85. {
  86. const struct demangler_engine *dem;
  87. int i;
  88. /* First just try to match whatever style name the user supplied with
  89. one of the known ones. Don't bother special casing for an empty
  90. name, we just treat it as any other style name that doesn't match.
  91. If we match, update the current demangling style enum. */
  92. for (dem = libiberty_demanglers, i = 0;
  93. dem->demangling_style != unknown_demangling;
  94. dem++)
  95. {
  96. if (strcmp (current_demangling_style_string,
  97. dem->demangling_style_name) == 0)
  98. {
  99. current_demangling_style = dem->demangling_style;
  100. current_demangling_style_string = demangling_style_names[i];
  101. break;
  102. }
  103. i++;
  104. }
  105. /* We should have found a match, given we only add known styles to
  106. the enumeration list. */
  107. gdb_assert (dem->demangling_style != unknown_demangling);
  108. }
  109. /* G++ uses a special character to indicate certain internal names. Which
  110. character it is depends on the platform:
  111. - Usually '$' on systems where the assembler will accept that
  112. - Usually '.' otherwise (this includes most sysv4-like systems and most
  113. ELF targets)
  114. - Occasionally '_' if neither of the above is usable
  115. We check '$' first because it is the safest, and '.' often has another
  116. meaning. We don't currently try to handle '_' because the precise forms
  117. of the names are different on those targets. */
  118. static char cplus_markers[] = {'$', '.', '\0'};
  119. /* See documentation in gdb-demangle.h. */
  120. bool
  121. is_cplus_marker (int c)
  122. {
  123. return c && strchr (cplus_markers, c) != NULL;
  124. }
  125. /* Demangle the given string in the current language. */
  126. static void
  127. demangle_command (const char *args, int from_tty)
  128. {
  129. const char *name;
  130. const char *arg_start;
  131. int processing_args = 1;
  132. const struct language_defn *lang;
  133. std::string arg_buf = args != NULL ? args : "";
  134. arg_start = arg_buf.c_str ();
  135. std::string lang_name;
  136. while (processing_args
  137. && *arg_start == '-')
  138. {
  139. const char *p = skip_to_space (arg_start);
  140. if (strncmp (arg_start, "-l", p - arg_start) == 0)
  141. lang_name = extract_arg (&p);
  142. else if (strncmp (arg_start, "--", p - arg_start) == 0)
  143. processing_args = 0;
  144. else
  145. report_unrecognized_option_error ("demangle", arg_start);
  146. arg_start = skip_spaces (p);
  147. }
  148. name = arg_start;
  149. if (*name == '\0')
  150. error (_("Usage: demangle [-l LANGUAGE] [--] NAME"));
  151. if (!lang_name.empty ())
  152. {
  153. enum language lang_enum;
  154. lang_enum = language_enum (lang_name.c_str ());
  155. if (lang_enum == language_unknown)
  156. error (_("Unknown language \"%s\""), lang_name.c_str ());
  157. lang = language_def (lang_enum);
  158. }
  159. else
  160. lang = current_language;
  161. gdb::unique_xmalloc_ptr<char> demangled
  162. = language_demangle (lang, name, DMGL_ANSI | DMGL_PARAMS);
  163. if (demangled != NULL)
  164. gdb_printf ("%s\n", demangled.get ());
  165. else
  166. error (_("Can't demangle \"%s\""), name);
  167. }
  168. void _initialize_gdb_demangle ();
  169. void
  170. _initialize_gdb_demangle ()
  171. {
  172. int i, ndems;
  173. /* Fill the demangling_style_names[] array, and set the default
  174. demangling style chosen at compilation time. */
  175. for (ndems = 0;
  176. libiberty_demanglers[ndems].demangling_style != unknown_demangling;
  177. ndems++)
  178. ;
  179. demangling_style_names = XCNEWVEC (const char *, ndems + 1);
  180. for (i = 0;
  181. libiberty_demanglers[i].demangling_style != unknown_demangling;
  182. i++)
  183. {
  184. demangling_style_names[i]
  185. = xstrdup (libiberty_demanglers[i].demangling_style_name);
  186. if (current_demangling_style_string == NULL
  187. && strcmp (DEFAULT_DEMANGLING_STYLE, demangling_style_names[i]) == 0)
  188. current_demangling_style_string = demangling_style_names[i];
  189. }
  190. add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
  191. Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
  192. Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
  193. NULL,
  194. show_demangle,
  195. &setprintlist, &showprintlist);
  196. add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
  197. Set demangling of C++/ObjC names in disassembly listings."), _("\
  198. Show demangling of C++/ObjC names in disassembly listings."), NULL,
  199. NULL,
  200. show_asm_demangle,
  201. &setprintlist, &showprintlist);
  202. add_setshow_enum_cmd ("demangle-style", class_support,
  203. demangling_style_names,
  204. &current_demangling_style_string, _("\
  205. Set the current C++ demangling style."), _("\
  206. Show the current C++ demangling style."), _("\
  207. Use `set demangle-style' without arguments for a list of demangling styles."),
  208. set_demangling_command,
  209. show_demangling_style_names,
  210. &setlist, &showlist);
  211. add_cmd ("demangle", class_support, demangle_command, _("\
  212. Demangle a mangled name.\n\
  213. Usage: demangle [-l LANGUAGE] [--] NAME\n\
  214. If LANGUAGE is not specified, NAME is demangled in the current language."),
  215. &cmdlist);
  216. }