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