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