Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Basic C++ demangling support for GDB. |
2 | Copyright 1991, 1992, 1996, 1999 Free Software Foundation, Inc. | |
3 | Written by Fred Fish at Cygnus Support. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
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 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | ||
22 | /* This file contains support code for C++ demangling that is common | |
23 | to a styles of demangling, and GDB specific. */ | |
24 | ||
25 | #include "defs.h" | |
26 | #include "command.h" | |
27 | #include "gdbcmd.h" | |
28 | #include "demangle.h" | |
29 | #include "gdb_string.h" | |
30 | ||
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 | |
37 | the appropriate target configuration file. */ | |
38 | ||
39 | #ifndef DEFAULT_DEMANGLING_STYLE | |
40 | #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING | |
41 | #endif | |
42 | ||
392a587b JM |
43 | extern void _initialize_demangler PARAMS ((void)); |
44 | ||
c906108c SS |
45 | /* String name for the current demangling style. Set by the |
46 | "set demangle-style" command, printed as part of the output by the | |
47 | "show demangle-style" command. */ | |
48 | ||
49 | static char *current_demangling_style_string; | |
50 | ||
51 | /* List of supported demangling styles. Contains the name of the style as | |
52 | seen by the user, and the enum value that corresponds to that style. */ | |
53 | ||
54 | static const struct demangler | |
55 | { | |
56 | char *demangling_style_name; | |
57 | enum demangling_styles demangling_style; | |
58 | char *demangling_style_doc; | |
59 | } | |
60 | demanglers[] = | |
61 | { | |
62 | { | |
63 | AUTO_DEMANGLING_STYLE_STRING, | |
64 | auto_demangling, | |
65 | "Automatic selection based on executable" | |
66 | } | |
67 | , | |
68 | { | |
69 | GNU_DEMANGLING_STYLE_STRING, | |
70 | gnu_demangling, | |
71 | "GNU (g++) style demangling" | |
72 | } | |
73 | , | |
74 | { | |
75 | LUCID_DEMANGLING_STYLE_STRING, | |
76 | lucid_demangling, | |
77 | "Lucid (lcc) style demangling" | |
78 | } | |
79 | , | |
80 | { | |
81 | ARM_DEMANGLING_STYLE_STRING, | |
82 | arm_demangling, | |
83 | "ARM style demangling" | |
84 | } | |
85 | , | |
86 | { | |
87 | HP_DEMANGLING_STYLE_STRING, | |
88 | hp_demangling, | |
89 | "HP (aCC) style demangling" | |
90 | } | |
91 | , | |
92 | { | |
93 | EDG_DEMANGLING_STYLE_STRING, | |
94 | edg_demangling, | |
95 | "EDG style demangling" | |
96 | } | |
97 | , | |
98 | { | |
99 | NULL, unknown_demangling, NULL | |
100 | } | |
101 | }; | |
102 | ||
103 | static void | |
104 | set_demangling_command PARAMS ((char *, int, struct cmd_list_element *)); | |
105 | ||
106 | /* Set current demangling style. Called by the "set demangle-style" | |
107 | command after it has updated the current_demangling_style_string to | |
108 | match what the user has entered. | |
109 | ||
110 | If the user has entered a string that matches a known demangling style | |
111 | name in the demanglers[] array then just leave the string alone and update | |
112 | the current_demangling_style enum value to match. | |
113 | ||
114 | If the user has entered a string that doesn't match, including an empty | |
115 | string, then print a list of the currently known styles and restore | |
116 | the current_demangling_style_string to match the current_demangling_style | |
117 | enum value. | |
118 | ||
119 | Note: Assumes that current_demangling_style_string always points to | |
120 | a malloc'd string, even if it is a null-string. */ | |
121 | ||
122 | static void | |
123 | set_demangling_command (ignore, from_tty, c) | |
124 | char *ignore; | |
125 | int from_tty; | |
126 | struct cmd_list_element *c; | |
127 | { | |
128 | const struct demangler *dem; | |
129 | ||
130 | /* First just try to match whatever style name the user supplied with | |
131 | one of the known ones. Don't bother special casing for an empty | |
132 | name, we just treat it as any other style name that doesn't match. | |
133 | If we match, update the current demangling style enum. */ | |
134 | ||
135 | for (dem = demanglers; dem->demangling_style_name != NULL; dem++) | |
136 | { | |
137 | if (STREQ (current_demangling_style_string, | |
138 | dem->demangling_style_name)) | |
139 | { | |
140 | current_demangling_style = dem->demangling_style; | |
141 | break; | |
142 | } | |
143 | } | |
144 | ||
145 | /* Check to see if we found a match. If not, gripe about any non-empty | |
146 | style name and supply a list of valid ones. FIXME: This should | |
147 | probably be done with some sort of completion and with help. */ | |
148 | ||
149 | if (dem->demangling_style_name == NULL) | |
150 | { | |
151 | if (*current_demangling_style_string != '\0') | |
152 | { | |
153 | printf_unfiltered ("Unknown demangling style `%s'.\n", | |
154 | current_demangling_style_string); | |
155 | } | |
156 | printf_unfiltered ("The currently understood settings are:\n\n"); | |
157 | for (dem = demanglers; dem->demangling_style_name != NULL; dem++) | |
158 | { | |
159 | printf_unfiltered ("%-10s %s\n", dem->demangling_style_name, | |
160 | dem->demangling_style_doc); | |
161 | if (dem->demangling_style == current_demangling_style) | |
162 | { | |
163 | free (current_demangling_style_string); | |
164 | current_demangling_style_string = | |
165 | savestring (dem->demangling_style_name, | |
166 | strlen (dem->demangling_style_name)); | |
167 | } | |
168 | } | |
169 | if (current_demangling_style == unknown_demangling) | |
170 | { | |
171 | /* This can happen during initialization if gdb is compiled with | |
172 | a DEMANGLING_STYLE value that is unknown, so pick the first | |
173 | one as the default. */ | |
174 | current_demangling_style = demanglers[0].demangling_style; | |
175 | current_demangling_style_string = | |
176 | savestring (demanglers[0].demangling_style_name, | |
177 | strlen (demanglers[0].demangling_style_name)); | |
178 | warning ("`%s' style demangling chosen as the default.\n", | |
179 | current_demangling_style_string); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | /* Fake a "set demangle-style" command. */ | |
185 | ||
186 | void | |
187 | set_demangling_style (style) | |
188 | char *style; | |
189 | { | |
190 | if (current_demangling_style_string != NULL) | |
191 | { | |
192 | free (current_demangling_style_string); | |
193 | } | |
194 | current_demangling_style_string = savestring (style, strlen (style)); | |
195 | set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL); | |
196 | } | |
197 | ||
198 | /* In order to allow a single demangler executable to demangle strings | |
199 | using various common values of CPLUS_MARKER, as well as any specific | |
200 | one set at compile time, we maintain a string containing all the | |
201 | commonly used ones, and check to see if the marker we are looking for | |
202 | is in that string. CPLUS_MARKER is usually '$' on systems where the | |
203 | assembler can deal with that. Where the assembler can't, it's usually | |
204 | '.' (but on many systems '.' is used for other things). We put the | |
205 | current defined CPLUS_MARKER first (which defaults to '$'), followed | |
206 | by the next most common value, followed by an explicit '$' in case | |
207 | the value of CPLUS_MARKER is not '$'. | |
208 | ||
209 | We could avoid this if we could just get g++ to tell us what the actual | |
210 | cplus marker character is as part of the debug information, perhaps by | |
211 | ensuring that it is the character that terminates the gcc<n>_compiled | |
212 | marker symbol (FIXME). */ | |
213 | ||
214 | static char cplus_markers[] = {CPLUS_MARKER, '.', '$', '\0'}; | |
215 | ||
216 | int | |
217 | is_cplus_marker (c) | |
218 | int c; | |
219 | { | |
220 | return c && strchr (cplus_markers, c) != NULL; | |
221 | } | |
222 | ||
223 | void | |
224 | _initialize_demangler () | |
225 | { | |
226 | struct cmd_list_element *set, *show; | |
227 | ||
228 | set = add_set_cmd ("demangle-style", class_support, var_string_noescape, | |
229 | (char *) ¤t_demangling_style_string, | |
230 | "Set the current C++ demangling style.\n\ | |
231 | Use `set demangle-style' without arguments for a list of demangling styles.", | |
232 | &setlist); | |
233 | show = add_show_from_set (set, &showlist); | |
234 | set->function.sfunc = set_demangling_command; | |
235 | ||
236 | /* Set the default demangling style chosen at compilation time. */ | |
237 | set_demangling_style (DEFAULT_DEMANGLING_STYLE); | |
238 | set_cplus_marker_for_demangling (CPLUS_MARKER); | |
239 | } |