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 | |
c5aa993b JM |
19 | Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | ||
23 | /* This file contains support code for C++ demangling that is common | |
24 | to a styles of demangling, and GDB specific. */ | |
25 | ||
26 | #include "defs.h" | |
27 | #include "command.h" | |
28 | #include "gdbcmd.h" | |
29 | #include "demangle.h" | |
30 | #include "gdb_string.h" | |
31 | ||
32 | /* Select the default C++ demangling style to use. The default is "auto", | |
33 | which allows gdb to attempt to pick an appropriate demangling style for | |
34 | the executable it has loaded. It can be set to a specific style ("gnu", | |
35 | "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto | |
36 | selection of the style unless you do an explicit "set demangle auto". | |
37 | To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in | |
38 | the appropriate target configuration file. */ | |
39 | ||
40 | #ifndef DEFAULT_DEMANGLING_STYLE | |
41 | #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING | |
42 | #endif | |
43 | ||
a14ed312 | 44 | extern void _initialize_demangler (void); |
392a587b | 45 | |
c906108c SS |
46 | /* String name for the current demangling style. Set by the |
47 | "set demangle-style" command, printed as part of the output by the | |
48 | "show demangle-style" command. */ | |
49 | ||
50 | static char *current_demangling_style_string; | |
51 | ||
fa58ee11 EZ |
52 | /* The array of names of the known demanglyng styles. Generated by |
53 | _initialize_demangler from libiberty_demanglers[] array. */ | |
54 | ||
55 | static const char **demangling_style_names; | |
56 | ||
a14ed312 | 57 | static void set_demangling_command (char *, int, struct cmd_list_element *); |
c906108c SS |
58 | |
59 | /* Set current demangling style. Called by the "set demangle-style" | |
60 | command after it has updated the current_demangling_style_string to | |
61 | match what the user has entered. | |
62 | ||
63 | If the user has entered a string that matches a known demangling style | |
64 | name in the demanglers[] array then just leave the string alone and update | |
65 | the current_demangling_style enum value to match. | |
66 | ||
67 | If the user has entered a string that doesn't match, including an empty | |
68 | string, then print a list of the currently known styles and restore | |
69 | the current_demangling_style_string to match the current_demangling_style | |
70 | enum value. | |
71 | ||
72 | Note: Assumes that current_demangling_style_string always points to | |
73 | a malloc'd string, even if it is a null-string. */ | |
74 | ||
75 | static void | |
fba45db2 | 76 | set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c) |
c906108c | 77 | { |
770de199 | 78 | const struct demangler_engine *dem; |
c906108c SS |
79 | |
80 | /* First just try to match whatever style name the user supplied with | |
81 | one of the known ones. Don't bother special casing for an empty | |
82 | name, we just treat it as any other style name that doesn't match. | |
83 | If we match, update the current demangling style enum. */ | |
84 | ||
770de199 DB |
85 | for (dem = libiberty_demanglers; |
86 | dem->demangling_style != unknown_demangling; | |
87 | dem++) | |
c906108c SS |
88 | { |
89 | if (STREQ (current_demangling_style_string, | |
90 | dem->demangling_style_name)) | |
91 | { | |
92 | current_demangling_style = dem->demangling_style; | |
93 | break; | |
94 | } | |
95 | } | |
96 | ||
97 | /* Check to see if we found a match. If not, gripe about any non-empty | |
98 | style name and supply a list of valid ones. FIXME: This should | |
99 | probably be done with some sort of completion and with help. */ | |
100 | ||
770de199 | 101 | if (dem->demangling_style == unknown_demangling) |
c906108c SS |
102 | { |
103 | if (*current_demangling_style_string != '\0') | |
104 | { | |
105 | printf_unfiltered ("Unknown demangling style `%s'.\n", | |
106 | current_demangling_style_string); | |
107 | } | |
108 | printf_unfiltered ("The currently understood settings are:\n\n"); | |
770de199 DB |
109 | for (dem = libiberty_demanglers; |
110 | dem->demangling_style != unknown_demangling; | |
111 | dem++) | |
c906108c SS |
112 | { |
113 | printf_unfiltered ("%-10s %s\n", dem->demangling_style_name, | |
114 | dem->demangling_style_doc); | |
115 | if (dem->demangling_style == current_demangling_style) | |
116 | { | |
b8c9b27d | 117 | xfree (current_demangling_style_string); |
c906108c SS |
118 | current_demangling_style_string = |
119 | savestring (dem->demangling_style_name, | |
120 | strlen (dem->demangling_style_name)); | |
121 | } | |
122 | } | |
123 | if (current_demangling_style == unknown_demangling) | |
124 | { | |
125 | /* This can happen during initialization if gdb is compiled with | |
126 | a DEMANGLING_STYLE value that is unknown, so pick the first | |
127 | one as the default. */ | |
770de199 | 128 | current_demangling_style = libiberty_demanglers[0].demangling_style; |
c906108c | 129 | current_demangling_style_string = |
770de199 DB |
130 | savestring ( |
131 | libiberty_demanglers[0].demangling_style_name, | |
132 | strlen (libiberty_demanglers[0].demangling_style_name)); | |
c906108c SS |
133 | warning ("`%s' style demangling chosen as the default.\n", |
134 | current_demangling_style_string); | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | /* Fake a "set demangle-style" command. */ | |
140 | ||
141 | void | |
fba45db2 | 142 | set_demangling_style (char *style) |
c906108c SS |
143 | { |
144 | if (current_demangling_style_string != NULL) | |
145 | { | |
b8c9b27d | 146 | xfree (current_demangling_style_string); |
c906108c SS |
147 | } |
148 | current_demangling_style_string = savestring (style, strlen (style)); | |
149 | set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL); | |
150 | } | |
151 | ||
152 | /* In order to allow a single demangler executable to demangle strings | |
153 | using various common values of CPLUS_MARKER, as well as any specific | |
154 | one set at compile time, we maintain a string containing all the | |
155 | commonly used ones, and check to see if the marker we are looking for | |
156 | is in that string. CPLUS_MARKER is usually '$' on systems where the | |
157 | assembler can deal with that. Where the assembler can't, it's usually | |
158 | '.' (but on many systems '.' is used for other things). We put the | |
159 | current defined CPLUS_MARKER first (which defaults to '$'), followed | |
160 | by the next most common value, followed by an explicit '$' in case | |
161 | the value of CPLUS_MARKER is not '$'. | |
162 | ||
163 | We could avoid this if we could just get g++ to tell us what the actual | |
164 | cplus marker character is as part of the debug information, perhaps by | |
165 | ensuring that it is the character that terminates the gcc<n>_compiled | |
166 | marker symbol (FIXME). */ | |
167 | ||
c5aa993b JM |
168 | static char cplus_markers[] = |
169 | {CPLUS_MARKER, '.', '$', '\0'}; | |
c906108c SS |
170 | |
171 | int | |
fba45db2 | 172 | is_cplus_marker (int c) |
c906108c SS |
173 | { |
174 | return c && strchr (cplus_markers, c) != NULL; | |
175 | } | |
176 | ||
177 | void | |
fba45db2 | 178 | _initialize_demangler (void) |
c906108c SS |
179 | { |
180 | struct cmd_list_element *set, *show; | |
fa58ee11 EZ |
181 | int i, ndems; |
182 | ||
183 | /* Fill the demangling_style_names[] array. */ | |
184 | for (ndems = 0; | |
185 | libiberty_demanglers[ndems].demangling_style != unknown_demangling; | |
186 | ndems++) | |
187 | ; | |
188 | demangling_style_names = xmalloc (ndems * sizeof (char *)); | |
189 | for (i = 0; | |
190 | libiberty_demanglers[i].demangling_style != unknown_demangling; | |
191 | i++) | |
192 | demangling_style_names[i] = | |
193 | xstrdup (libiberty_demanglers[i].demangling_style_name); | |
194 | ||
195 | set = add_set_enum_cmd ("demangle-style", class_support, | |
196 | demangling_style_names, | |
197 | (const char **) ¤t_demangling_style_string, | |
198 | "Set the current C++ demangling style.\n\ | |
c906108c | 199 | Use `set demangle-style' without arguments for a list of demangling styles.", |
fa58ee11 | 200 | &setlist); |
c906108c SS |
201 | show = add_show_from_set (set, &showlist); |
202 | set->function.sfunc = set_demangling_command; | |
203 | ||
204 | /* Set the default demangling style chosen at compilation time. */ | |
205 | set_demangling_style (DEFAULT_DEMANGLING_STYLE); | |
206 | set_cplus_marker_for_demangling (CPLUS_MARKER); | |
207 | } |