Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Basic C++ demangling support for GDB. |
1bac305b | 2 | |
ecd75fc8 | 3 | Copyright (C) 1991-2014 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" | |
27 | #include "command.h" | |
28 | #include "gdbcmd.h" | |
29 | #include "demangle.h" | |
50f182aa | 30 | #include "gdb-demangle.h" |
c906108c SS |
31 | /* Select the default C++ demangling style to use. The default is "auto", |
32 | which allows gdb to attempt to pick an appropriate demangling style for | |
33 | the executable it has loaded. It can be set to a specific style ("gnu", | |
34 | "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto | |
35 | selection of the style unless you do an explicit "set demangle auto". | |
36 | To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in | |
0963b4bd | 37 | the appropriate target configuration file. */ |
c906108c SS |
38 | |
39 | #ifndef DEFAULT_DEMANGLING_STYLE | |
40 | #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING | |
41 | #endif | |
42 | ||
50f182aa DE |
43 | /* See documentation in gdb-demangle.h. */ |
44 | int demangle = 1; | |
45 | ||
46 | static void | |
47 | show_demangle (struct ui_file *file, int from_tty, | |
48 | struct cmd_list_element *c, const char *value) | |
49 | { | |
50 | fprintf_filtered (file, | |
51 | _("Demangling of encoded C++/ObjC names " | |
52 | "when displaying symbols is %s.\n"), | |
53 | value); | |
54 | } | |
55 | ||
56 | /* See documentation in gdb-demangle.h. */ | |
57 | int asm_demangle = 0; | |
58 | ||
59 | static void | |
60 | show_asm_demangle (struct ui_file *file, int from_tty, | |
61 | struct cmd_list_element *c, const char *value) | |
62 | { | |
63 | fprintf_filtered (file, | |
64 | _("Demangling of C++/ObjC names in " | |
65 | "disassembly listings is %s.\n"), | |
66 | value); | |
67 | } | |
392a587b | 68 | |
c906108c SS |
69 | /* String name for the current demangling style. Set by the |
70 | "set demangle-style" command, printed as part of the output by the | |
0963b4bd | 71 | "show demangle-style" command. */ |
c906108c | 72 | |
10217050 | 73 | static const char *current_demangling_style_string; |
c906108c | 74 | |
fa58ee11 EZ |
75 | /* The array of names of the known demanglyng styles. Generated by |
76 | _initialize_demangler from libiberty_demanglers[] array. */ | |
77 | ||
78 | static const char **demangling_style_names; | |
920d2a44 AC |
79 | static void |
80 | show_demangling_style_names(struct ui_file *file, int from_tty, | |
81 | struct cmd_list_element *c, const char *value) | |
82 | { | |
83 | fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"), | |
84 | value); | |
85 | } | |
86 | ||
c906108c SS |
87 | /* Set current demangling style. Called by the "set demangle-style" |
88 | command after it has updated the current_demangling_style_string to | |
89 | match what the user has entered. | |
90 | ||
91 | If the user has entered a string that matches a known demangling style | |
92 | name in the demanglers[] array then just leave the string alone and update | |
93 | the current_demangling_style enum value to match. | |
94 | ||
95 | If the user has entered a string that doesn't match, including an empty | |
96 | string, then print a list of the currently known styles and restore | |
97 | the current_demangling_style_string to match the current_demangling_style | |
98 | enum value. | |
99 | ||
100 | Note: Assumes that current_demangling_style_string always points to | |
0963b4bd | 101 | a malloc'd string, even if it is a null-string. */ |
c906108c SS |
102 | |
103 | static void | |
fba45db2 | 104 | set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c) |
c906108c | 105 | { |
770de199 | 106 | const struct demangler_engine *dem; |
10217050 | 107 | int i; |
c906108c SS |
108 | |
109 | /* First just try to match whatever style name the user supplied with | |
110 | one of the known ones. Don't bother special casing for an empty | |
111 | name, we just treat it as any other style name that doesn't match. | |
0963b4bd | 112 | If we match, update the current demangling style enum. */ |
c906108c | 113 | |
10217050 | 114 | for (dem = libiberty_demanglers, i = 0; |
770de199 DB |
115 | dem->demangling_style != unknown_demangling; |
116 | dem++) | |
c906108c | 117 | { |
bde58177 AC |
118 | if (strcmp (current_demangling_style_string, |
119 | dem->demangling_style_name) == 0) | |
c906108c SS |
120 | { |
121 | current_demangling_style = dem->demangling_style; | |
10217050 | 122 | current_demangling_style_string = demangling_style_names[i]; |
c906108c SS |
123 | break; |
124 | } | |
10217050 | 125 | i++; |
c906108c SS |
126 | } |
127 | ||
10217050 PA |
128 | /* We should have found a match, given we only add known styles to |
129 | the enumeration list. */ | |
130 | gdb_assert (dem->demangling_style != unknown_demangling); | |
c906108c SS |
131 | } |
132 | ||
8343f86c DJ |
133 | /* G++ uses a special character to indicate certain internal names. Which |
134 | character it is depends on the platform: | |
135 | - Usually '$' on systems where the assembler will accept that | |
136 | - Usually '.' otherwise (this includes most sysv4-like systems and most | |
137 | ELF targets) | |
138 | - Occasionally '_' if neither of the above is usable | |
139 | ||
140 | We check '$' first because it is the safest, and '.' often has another | |
141 | meaning. We don't currently try to handle '_' because the precise forms | |
142 | of the names are different on those targets. */ | |
143 | ||
144 | static char cplus_markers[] = {'$', '.', '\0'}; | |
c906108c | 145 | |
50f182aa DE |
146 | /* See documentation in gdb-demangle.h. */ |
147 | ||
c906108c | 148 | int |
fba45db2 | 149 | is_cplus_marker (int c) |
c906108c SS |
150 | { |
151 | return c && strchr (cplus_markers, c) != NULL; | |
152 | } | |
153 | ||
50f182aa DE |
154 | extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */ |
155 | ||
c906108c | 156 | void |
fba45db2 | 157 | _initialize_demangler (void) |
c906108c | 158 | { |
fa58ee11 EZ |
159 | int i, ndems; |
160 | ||
10217050 PA |
161 | /* Fill the demangling_style_names[] array, and set the default |
162 | demangling style chosen at compilation time. */ | |
fa58ee11 EZ |
163 | for (ndems = 0; |
164 | libiberty_demanglers[ndems].demangling_style != unknown_demangling; | |
165 | ndems++) | |
166 | ; | |
9e0c176c | 167 | demangling_style_names = xcalloc (ndems + 1, sizeof (char *)); |
fa58ee11 EZ |
168 | for (i = 0; |
169 | libiberty_demanglers[i].demangling_style != unknown_demangling; | |
170 | i++) | |
10217050 PA |
171 | { |
172 | demangling_style_names[i] | |
173 | = xstrdup (libiberty_demanglers[i].demangling_style_name); | |
174 | ||
175 | if (current_demangling_style_string == NULL | |
176 | && strcmp (DEFAULT_DEMANGLING_STYLE, demangling_style_names[i]) == 0) | |
177 | current_demangling_style_string = demangling_style_names[i]; | |
178 | } | |
fa58ee11 | 179 | |
50f182aa DE |
180 | add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\ |
181 | Set demangling of encoded C++/ObjC names when displaying symbols."), _("\ | |
182 | Show demangling of encoded C++/ObjC names when displaying symbols."), NULL, | |
183 | NULL, | |
184 | show_demangle, | |
185 | &setprintlist, &showprintlist); | |
186 | ||
187 | add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\ | |
188 | Set demangling of C++/ObjC names in disassembly listings."), _("\ | |
189 | Show demangling of C++/ObjC names in disassembly listings."), NULL, | |
190 | NULL, | |
191 | show_asm_demangle, | |
192 | &setprintlist, &showprintlist); | |
193 | ||
7ab04401 AC |
194 | add_setshow_enum_cmd ("demangle-style", class_support, |
195 | demangling_style_names, | |
10217050 | 196 | ¤t_demangling_style_string, _("\ |
7ab04401 AC |
197 | Set the current C++ demangling style."), _("\ |
198 | Show the current C++ demangling style."), _("\ | |
199 | Use `set demangle-style' without arguments for a list of demangling styles."), | |
200 | set_demangling_command, | |
920d2a44 | 201 | show_demangling_style_names, |
7ab04401 | 202 | &setlist, &showlist); |
c906108c | 203 | } |