]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/demangle.c
Protoization.
[thirdparty/binutils-gdb.git] / gdb / demangle.c
CommitLineData
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 44extern 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
50static char *current_demangling_style_string;
51
52/* List of supported demangling styles. Contains the name of the style as
53 seen by the user, and the enum value that corresponds to that style. */
54
55static const struct demangler
56 {
57 char *demangling_style_name;
58 enum demangling_styles demangling_style;
59 char *demangling_style_doc;
60 }
61demanglers[] =
62{
63 {
64 AUTO_DEMANGLING_STYLE_STRING,
65 auto_demangling,
66 "Automatic selection based on executable"
67 }
68 ,
69 {
70 GNU_DEMANGLING_STYLE_STRING,
71 gnu_demangling,
72 "GNU (g++) style demangling"
73 }
74 ,
75 {
76 LUCID_DEMANGLING_STYLE_STRING,
77 lucid_demangling,
78 "Lucid (lcc) style demangling"
79 }
80 ,
81 {
82 ARM_DEMANGLING_STYLE_STRING,
83 arm_demangling,
84 "ARM style demangling"
85 }
86 ,
87 {
88 HP_DEMANGLING_STYLE_STRING,
89 hp_demangling,
90 "HP (aCC) style demangling"
91 }
92 ,
93 {
94 EDG_DEMANGLING_STYLE_STRING,
95 edg_demangling,
96 "EDG style demangling"
97 }
98 ,
99 {
100 NULL, unknown_demangling, NULL
101 }
102};
103
a14ed312 104static void set_demangling_command (char *, int, struct cmd_list_element *);
c906108c
SS
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
122static void
fba45db2 123set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c
SS
124{
125 const struct demangler *dem;
126
127 /* First just try to match whatever style name the user supplied with
128 one of the known ones. Don't bother special casing for an empty
129 name, we just treat it as any other style name that doesn't match.
130 If we match, update the current demangling style enum. */
131
132 for (dem = demanglers; dem->demangling_style_name != NULL; dem++)
133 {
134 if (STREQ (current_demangling_style_string,
135 dem->demangling_style_name))
136 {
137 current_demangling_style = dem->demangling_style;
138 break;
139 }
140 }
141
142 /* Check to see if we found a match. If not, gripe about any non-empty
143 style name and supply a list of valid ones. FIXME: This should
144 probably be done with some sort of completion and with help. */
145
146 if (dem->demangling_style_name == NULL)
147 {
148 if (*current_demangling_style_string != '\0')
149 {
150 printf_unfiltered ("Unknown demangling style `%s'.\n",
151 current_demangling_style_string);
152 }
153 printf_unfiltered ("The currently understood settings are:\n\n");
154 for (dem = demanglers; dem->demangling_style_name != NULL; dem++)
155 {
156 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
157 dem->demangling_style_doc);
158 if (dem->demangling_style == current_demangling_style)
159 {
160 free (current_demangling_style_string);
161 current_demangling_style_string =
162 savestring (dem->demangling_style_name,
163 strlen (dem->demangling_style_name));
164 }
165 }
166 if (current_demangling_style == unknown_demangling)
167 {
168 /* This can happen during initialization if gdb is compiled with
169 a DEMANGLING_STYLE value that is unknown, so pick the first
170 one as the default. */
171 current_demangling_style = demanglers[0].demangling_style;
172 current_demangling_style_string =
173 savestring (demanglers[0].demangling_style_name,
174 strlen (demanglers[0].demangling_style_name));
175 warning ("`%s' style demangling chosen as the default.\n",
176 current_demangling_style_string);
177 }
178 }
179}
180
181/* Fake a "set demangle-style" command. */
182
183void
fba45db2 184set_demangling_style (char *style)
c906108c
SS
185{
186 if (current_demangling_style_string != NULL)
187 {
188 free (current_demangling_style_string);
189 }
190 current_demangling_style_string = savestring (style, strlen (style));
191 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
192}
193
194/* In order to allow a single demangler executable to demangle strings
195 using various common values of CPLUS_MARKER, as well as any specific
196 one set at compile time, we maintain a string containing all the
197 commonly used ones, and check to see if the marker we are looking for
198 is in that string. CPLUS_MARKER is usually '$' on systems where the
199 assembler can deal with that. Where the assembler can't, it's usually
200 '.' (but on many systems '.' is used for other things). We put the
201 current defined CPLUS_MARKER first (which defaults to '$'), followed
202 by the next most common value, followed by an explicit '$' in case
203 the value of CPLUS_MARKER is not '$'.
204
205 We could avoid this if we could just get g++ to tell us what the actual
206 cplus marker character is as part of the debug information, perhaps by
207 ensuring that it is the character that terminates the gcc<n>_compiled
208 marker symbol (FIXME). */
209
c5aa993b
JM
210static char cplus_markers[] =
211{CPLUS_MARKER, '.', '$', '\0'};
c906108c
SS
212
213int
fba45db2 214is_cplus_marker (int c)
c906108c
SS
215{
216 return c && strchr (cplus_markers, c) != NULL;
217}
218
219void
fba45db2 220_initialize_demangler (void)
c906108c
SS
221{
222 struct cmd_list_element *set, *show;
223
224 set = add_set_cmd ("demangle-style", class_support, var_string_noescape,
225 (char *) &current_demangling_style_string,
226 "Set the current C++ demangling style.\n\
227Use `set demangle-style' without arguments for a list of demangling styles.",
228 &setlist);
229 show = add_show_from_set (set, &showlist);
230 set->function.sfunc = set_demangling_command;
231
232 /* Set the default demangling style chosen at compilation time. */
233 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
234 set_cplus_marker_for_demangling (CPLUS_MARKER);
235}