cxxfilt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Demangler for GNU C++ - main program
  2. Copyright (C) 1989-2022 Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.uucp)
  4. Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  5. Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
  6. This file is part of GNU Binutils.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or (at
  10. your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING. If not, write to the Free
  17. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  18. 02110-1301, USA. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libiberty.h"
  22. #include "demangle.h"
  23. #include "getopt.h"
  24. #include "safe-ctype.h"
  25. #include "bucomm.h"
  26. static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
  27. static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
  28. static const struct option long_options[] =
  29. {
  30. {"strip-underscore", no_argument, NULL, '_'},
  31. {"format", required_argument, NULL, 's'},
  32. {"help", no_argument, NULL, 'h'},
  33. {"no-params", no_argument, NULL, 'p'},
  34. {"no-strip-underscores", no_argument, NULL, 'n'},
  35. {"no-verbose", no_argument, NULL, 'i'},
  36. {"types", no_argument, NULL, 't'},
  37. {"version", no_argument, NULL, 'v'},
  38. {"recurse-limit", no_argument, NULL, 'R'},
  39. {"recursion-limit", no_argument, NULL, 'R'},
  40. {"no-recurse-limit", no_argument, NULL, 'r'},
  41. {"no-recursion-limit", no_argument, NULL, 'r'},
  42. {NULL, no_argument, NULL, 0}
  43. };
  44. static void
  45. demangle_it (char *mangled_name)
  46. {
  47. char *result;
  48. unsigned int skip_first = 0;
  49. /* _ and $ are sometimes found at the start of function names
  50. in assembler sources in order to distinguish them from other
  51. names (eg register names). So skip them here. */
  52. if (mangled_name[0] == '.' || mangled_name[0] == '$')
  53. ++skip_first;
  54. if (strip_underscore && mangled_name[skip_first] == '_')
  55. ++skip_first;
  56. result = cplus_demangle (mangled_name + skip_first, flags);
  57. if (result == NULL)
  58. printf ("%s", mangled_name);
  59. else
  60. {
  61. if (mangled_name[0] == '.')
  62. putchar ('.');
  63. printf ("%s", result);
  64. free (result);
  65. }
  66. }
  67. static void
  68. print_demangler_list (FILE *stream)
  69. {
  70. const struct demangler_engine *demangler;
  71. fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
  72. for (demangler = libiberty_demanglers + 1;
  73. demangler->demangling_style != unknown_demangling;
  74. ++demangler)
  75. fprintf (stream, ",%s", demangler->demangling_style_name);
  76. fprintf (stream, "}");
  77. }
  78. ATTRIBUTE_NORETURN static void
  79. usage (FILE *stream, int status)
  80. {
  81. fprintf (stream, "\
  82. Usage: %s [options] [mangled names]\n", program_name);
  83. fprintf (stream, "\
  84. Options are:\n\
  85. [-_|--strip-underscore] Ignore first leading underscore%s\n",
  86. TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
  87. fprintf (stream, "\
  88. [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n",
  89. TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
  90. fprintf (stream, "\
  91. [-p|--no-params] Do not display function arguments\n\
  92. [-i|--no-verbose] Do not show implementation details (if any)\n\
  93. [-R|--recurse-limit] Enable a limit on recursion whilst demangling. [Default]\n\
  94. ]-r|--no-recurse-limit] Disable a limit on recursion whilst demangling\n\
  95. [-t|--types] Also attempt to demangle type encodings\n\
  96. [-s|--format ");
  97. print_demangler_list (stream);
  98. fprintf (stream, "]\n");
  99. fprintf (stream, "\
  100. [@<file>] Read extra options from <file>\n\
  101. [-h|--help] Display this information\n\
  102. [-v|--version] Show the version information\n\
  103. Demangled names are displayed to stdout.\n\
  104. If a name cannot be demangled it is just echoed to stdout.\n\
  105. If no names are provided on the command line, stdin is read.\n");
  106. if (REPORT_BUGS_TO[0] && status == 0)
  107. fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
  108. exit (status);
  109. }
  110. /* Return the string of non-alnum characters that may occur
  111. as a valid symbol component, in the standard assembler symbol
  112. syntax. */
  113. static const char *
  114. standard_symbol_characters (void)
  115. {
  116. return "_$.";
  117. }
  118. extern int main (int, char **);
  119. int
  120. main (int argc, char **argv)
  121. {
  122. int c;
  123. const char *valid_symbols;
  124. enum demangling_styles style = auto_demangling;
  125. program_name = argv[0];
  126. xmalloc_set_program_name (program_name);
  127. bfd_set_error_program_name (program_name);
  128. expandargv (&argc, &argv);
  129. while ((c = getopt_long (argc, argv, "_hinprRs:tv", long_options, (int *) 0)) != EOF)
  130. {
  131. switch (c)
  132. {
  133. case '?':
  134. usage (stderr, 1);
  135. break;
  136. case 'h':
  137. usage (stdout, 0);
  138. case 'n':
  139. strip_underscore = 0;
  140. break;
  141. case 'p':
  142. flags &= ~ DMGL_PARAMS;
  143. break;
  144. case 'r':
  145. flags |= DMGL_NO_RECURSE_LIMIT;
  146. break;
  147. case 'R':
  148. flags &= ~ DMGL_NO_RECURSE_LIMIT;
  149. break;
  150. case 't':
  151. flags |= DMGL_TYPES;
  152. break;
  153. case 'i':
  154. flags &= ~ DMGL_VERBOSE;
  155. break;
  156. case 'v':
  157. print_version ("c++filt");
  158. return 0;
  159. case '_':
  160. strip_underscore = 1;
  161. break;
  162. case 's':
  163. style = cplus_demangle_name_to_style (optarg);
  164. if (style == unknown_demangling)
  165. {
  166. fprintf (stderr, "%s: unknown demangling style `%s'\n",
  167. program_name, optarg);
  168. return 1;
  169. }
  170. cplus_demangle_set_style (style);
  171. break;
  172. }
  173. }
  174. if (optind < argc)
  175. {
  176. for ( ; optind < argc; optind++)
  177. {
  178. demangle_it (argv[optind]);
  179. putchar ('\n');
  180. }
  181. return 0;
  182. }
  183. switch (current_demangling_style)
  184. {
  185. case auto_demangling:
  186. case gnu_v3_demangling:
  187. case java_demangling:
  188. case gnat_demangling:
  189. case dlang_demangling:
  190. case rust_demangling:
  191. valid_symbols = standard_symbol_characters ();
  192. break;
  193. default:
  194. /* Folks should explicitly indicate the appropriate alphabet for
  195. each demangling. Providing a default would allow the
  196. question to go unconsidered. */
  197. fatal ("Internal error: no symbol alphabet for current style");
  198. }
  199. for (;;)
  200. {
  201. static char mbuffer[32767];
  202. unsigned i = 0;
  203. c = getchar ();
  204. /* Try to read a mangled name. */
  205. while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
  206. {
  207. if (i >= sizeof (mbuffer) - 1)
  208. break;
  209. mbuffer[i++] = c;
  210. c = getchar ();
  211. }
  212. if (i > 0)
  213. {
  214. mbuffer[i] = 0;
  215. demangle_it (mbuffer);
  216. }
  217. if (c == EOF)
  218. break;
  219. /* Echo the whitespace characters so that the output looks
  220. like the input, only with the mangled names demangled. */
  221. putchar (c);
  222. if (c == '\n')
  223. fflush (stdout);
  224. }
  225. fflush (stdout);
  226. return 0;
  227. }