2012-09-26 Jan Kratochvil <jan.kratochvil@redhat.com>
[deliverable/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b 2
0b302171
JB
3 Copyright (C) 1991-1996, 1998-2001, 2003, 2007-2012 Free Software
4 Foundation, Inc.
1bac305b 5
c906108c
SS
6 Written by Fred Fish at Cygnus Support.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23
24/* This file contains support code for C++ demangling that is common
0963b4bd 25 to a styles of demangling, and GDB specific. */
c906108c
SS
26
27#include "defs.h"
28#include "command.h"
29#include "gdbcmd.h"
30#include "demangle.h"
50f182aa 31#include "gdb-demangle.h"
c906108c
SS
32#include "gdb_string.h"
33
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. */
47int demangle = 1;
48
49static void
50show_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. */
60int asm_demangle = 0;
61
62static void
63show_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
SS
75
76static char *current_demangling_style_string;
77
fa58ee11
EZ
78/* The array of names of the known demanglyng styles. Generated by
79 _initialize_demangler from libiberty_demanglers[] array. */
80
81static const char **demangling_style_names;
920d2a44
AC
82static void
83show_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
106static void
fba45db2 107set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 108{
770de199 109 const struct demangler_engine *dem;
c906108c
SS
110
111 /* First just try to match whatever style name the user supplied with
112 one of the known ones. Don't bother special casing for an empty
113 name, we just treat it as any other style name that doesn't match.
0963b4bd 114 If we match, update the current demangling style enum. */
c906108c 115
770de199
DB
116 for (dem = libiberty_demanglers;
117 dem->demangling_style != unknown_demangling;
118 dem++)
c906108c 119 {
bde58177
AC
120 if (strcmp (current_demangling_style_string,
121 dem->demangling_style_name) == 0)
c906108c
SS
122 {
123 current_demangling_style = dem->demangling_style;
124 break;
125 }
126 }
127
128 /* Check to see if we found a match. If not, gripe about any non-empty
0963b4bd
MS
129 style name and supply a list of valid ones. FIXME: This should
130 probably be done with some sort of completion and with help. */
c906108c 131
770de199 132 if (dem->demangling_style == unknown_demangling)
c906108c
SS
133 {
134 if (*current_demangling_style_string != '\0')
135 {
a3f17187 136 printf_unfiltered (_("Unknown demangling style `%s'.\n"),
c906108c
SS
137 current_demangling_style_string);
138 }
a3f17187 139 printf_unfiltered (_("The currently understood settings are:\n\n"));
770de199
DB
140 for (dem = libiberty_demanglers;
141 dem->demangling_style != unknown_demangling;
142 dem++)
c906108c
SS
143 {
144 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
145 dem->demangling_style_doc);
146 if (dem->demangling_style == current_demangling_style)
147 {
b8c9b27d 148 xfree (current_demangling_style_string);
c906108c 149 current_demangling_style_string =
1b36a34b 150 xstrdup (dem->demangling_style_name);
c906108c
SS
151 }
152 }
153 if (current_demangling_style == unknown_demangling)
154 {
155 /* This can happen during initialization if gdb is compiled with
156 a DEMANGLING_STYLE value that is unknown, so pick the first
0963b4bd 157 one as the default. */
770de199 158 current_demangling_style = libiberty_demanglers[0].demangling_style;
c906108c 159 current_demangling_style_string =
1b36a34b 160 xstrdup (libiberty_demanglers[0].demangling_style_name);
8a3fe4f8 161 warning (_("`%s' style demangling chosen as the default."),
c906108c
SS
162 current_demangling_style_string);
163 }
164 }
165}
166
50f182aa 167/* See documentation in gdb-demangle.h. */
c906108c
SS
168
169void
fba45db2 170set_demangling_style (char *style)
c906108c
SS
171{
172 if (current_demangling_style_string != NULL)
173 {
b8c9b27d 174 xfree (current_demangling_style_string);
c906108c 175 }
1b36a34b 176 current_demangling_style_string = xstrdup (style);
c906108c
SS
177 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
178}
179
8343f86c
DJ
180/* G++ uses a special character to indicate certain internal names. Which
181 character it is depends on the platform:
182 - Usually '$' on systems where the assembler will accept that
183 - Usually '.' otherwise (this includes most sysv4-like systems and most
184 ELF targets)
185 - Occasionally '_' if neither of the above is usable
186
187 We check '$' first because it is the safest, and '.' often has another
188 meaning. We don't currently try to handle '_' because the precise forms
189 of the names are different on those targets. */
190
191static char cplus_markers[] = {'$', '.', '\0'};
c906108c 192
50f182aa
DE
193/* See documentation in gdb-demangle.h. */
194
c906108c 195int
fba45db2 196is_cplus_marker (int c)
c906108c
SS
197{
198 return c && strchr (cplus_markers, c) != NULL;
199}
200
50f182aa
DE
201extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
202
c906108c 203void
fba45db2 204_initialize_demangler (void)
c906108c 205{
fa58ee11
EZ
206 int i, ndems;
207
208 /* Fill the demangling_style_names[] array. */
209 for (ndems = 0;
210 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
211 ndems++)
212 ;
9e0c176c 213 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
fa58ee11
EZ
214 for (i = 0;
215 libiberty_demanglers[i].demangling_style != unknown_demangling;
216 i++)
217 demangling_style_names[i] =
218 xstrdup (libiberty_demanglers[i].demangling_style_name);
219
50f182aa
DE
220 add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
221Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
222Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
223 NULL,
224 show_demangle,
225 &setprintlist, &showprintlist);
226
227 add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
228Set demangling of C++/ObjC names in disassembly listings."), _("\
229Show demangling of C++/ObjC names in disassembly listings."), NULL,
230 NULL,
231 show_asm_demangle,
232 &setprintlist, &showprintlist);
233
7ab04401
AC
234 /* FIXME: cagney/2005-02-20: The code implementing this variable are
235 malloc-ing and free-ing current_demangling_style_string when it
236 should instead just point to an element of
237 demangling_style_names. */
238 add_setshow_enum_cmd ("demangle-style", class_support,
239 demangling_style_names,
240 (const char **) &current_demangling_style_string, _("\
241Set the current C++ demangling style."), _("\
242Show the current C++ demangling style."), _("\
243Use `set demangle-style' without arguments for a list of demangling styles."),
244 set_demangling_command,
920d2a44 245 show_demangling_style_names,
7ab04401 246 &setlist, &showlist);
c906108c 247
0963b4bd 248 /* Set the default demangling style chosen at compilation time. */
c906108c 249 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
c906108c 250}
This page took 0.885347 seconds and 4 git commands to generate.