Add basic support for AArch64.
[deliverable/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b 2
28e7fd62 3 Copyright (C) 1991-2013 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#include "gdb_string.h"
32
33/* Select the default C++ demangling style to use. The default is "auto",
34 which allows gdb to attempt to pick an appropriate demangling style for
35 the executable it has loaded. It can be set to a specific style ("gnu",
36 "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
37 selection of the style unless you do an explicit "set demangle auto".
38 To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
0963b4bd 39 the appropriate target configuration file. */
c906108c
SS
40
41#ifndef DEFAULT_DEMANGLING_STYLE
42#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
43#endif
44
50f182aa
DE
45/* See documentation in gdb-demangle.h. */
46int demangle = 1;
47
48static void
49show_demangle (struct ui_file *file, int from_tty,
50 struct cmd_list_element *c, const char *value)
51{
52 fprintf_filtered (file,
53 _("Demangling of encoded C++/ObjC names "
54 "when displaying symbols is %s.\n"),
55 value);
56}
57
58/* See documentation in gdb-demangle.h. */
59int asm_demangle = 0;
60
61static void
62show_asm_demangle (struct ui_file *file, int from_tty,
63 struct cmd_list_element *c, const char *value)
64{
65 fprintf_filtered (file,
66 _("Demangling of C++/ObjC names in "
67 "disassembly listings is %s.\n"),
68 value);
69}
392a587b 70
c906108c
SS
71/* String name for the current demangling style. Set by the
72 "set demangle-style" command, printed as part of the output by the
0963b4bd 73 "show demangle-style" command. */
c906108c
SS
74
75static char *current_demangling_style_string;
76
fa58ee11
EZ
77/* The array of names of the known demanglyng styles. Generated by
78 _initialize_demangler from libiberty_demanglers[] array. */
79
80static const char **demangling_style_names;
920d2a44
AC
81static void
82show_demangling_style_names(struct ui_file *file, int from_tty,
83 struct cmd_list_element *c, const char *value)
84{
85 fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
86 value);
87}
88
c906108c
SS
89/* Set current demangling style. Called by the "set demangle-style"
90 command after it has updated the current_demangling_style_string to
91 match what the user has entered.
92
93 If the user has entered a string that matches a known demangling style
94 name in the demanglers[] array then just leave the string alone and update
95 the current_demangling_style enum value to match.
96
97 If the user has entered a string that doesn't match, including an empty
98 string, then print a list of the currently known styles and restore
99 the current_demangling_style_string to match the current_demangling_style
100 enum value.
101
102 Note: Assumes that current_demangling_style_string always points to
0963b4bd 103 a malloc'd string, even if it is a null-string. */
c906108c
SS
104
105static void
fba45db2 106set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 107{
770de199 108 const struct demangler_engine *dem;
c906108c
SS
109
110 /* First just try to match whatever style name the user supplied with
111 one of the known ones. Don't bother special casing for an empty
112 name, we just treat it as any other style name that doesn't match.
0963b4bd 113 If we match, update the current demangling style enum. */
c906108c 114
770de199
DB
115 for (dem = libiberty_demanglers;
116 dem->demangling_style != unknown_demangling;
117 dem++)
c906108c 118 {
bde58177
AC
119 if (strcmp (current_demangling_style_string,
120 dem->demangling_style_name) == 0)
c906108c
SS
121 {
122 current_demangling_style = dem->demangling_style;
123 break;
124 }
125 }
126
127 /* Check to see if we found a match. If not, gripe about any non-empty
0963b4bd
MS
128 style name and supply a list of valid ones. FIXME: This should
129 probably be done with some sort of completion and with help. */
c906108c 130
770de199 131 if (dem->demangling_style == unknown_demangling)
c906108c
SS
132 {
133 if (*current_demangling_style_string != '\0')
134 {
a3f17187 135 printf_unfiltered (_("Unknown demangling style `%s'.\n"),
c906108c
SS
136 current_demangling_style_string);
137 }
a3f17187 138 printf_unfiltered (_("The currently understood settings are:\n\n"));
770de199
DB
139 for (dem = libiberty_demanglers;
140 dem->demangling_style != unknown_demangling;
141 dem++)
c906108c
SS
142 {
143 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
144 dem->demangling_style_doc);
145 if (dem->demangling_style == current_demangling_style)
146 {
b8c9b27d 147 xfree (current_demangling_style_string);
c906108c 148 current_demangling_style_string =
1b36a34b 149 xstrdup (dem->demangling_style_name);
c906108c
SS
150 }
151 }
152 if (current_demangling_style == unknown_demangling)
153 {
154 /* This can happen during initialization if gdb is compiled with
155 a DEMANGLING_STYLE value that is unknown, so pick the first
0963b4bd 156 one as the default. */
770de199 157 current_demangling_style = libiberty_demanglers[0].demangling_style;
c906108c 158 current_demangling_style_string =
1b36a34b 159 xstrdup (libiberty_demanglers[0].demangling_style_name);
8a3fe4f8 160 warning (_("`%s' style demangling chosen as the default."),
c906108c
SS
161 current_demangling_style_string);
162 }
163 }
164}
165
50f182aa 166/* See documentation in gdb-demangle.h. */
c906108c
SS
167
168void
fba45db2 169set_demangling_style (char *style)
c906108c
SS
170{
171 if (current_demangling_style_string != NULL)
172 {
b8c9b27d 173 xfree (current_demangling_style_string);
c906108c 174 }
1b36a34b 175 current_demangling_style_string = xstrdup (style);
c906108c
SS
176 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
177}
178
8343f86c
DJ
179/* G++ uses a special character to indicate certain internal names. Which
180 character it is depends on the platform:
181 - Usually '$' on systems where the assembler will accept that
182 - Usually '.' otherwise (this includes most sysv4-like systems and most
183 ELF targets)
184 - Occasionally '_' if neither of the above is usable
185
186 We check '$' first because it is the safest, and '.' often has another
187 meaning. We don't currently try to handle '_' because the precise forms
188 of the names are different on those targets. */
189
190static char cplus_markers[] = {'$', '.', '\0'};
c906108c 191
50f182aa
DE
192/* See documentation in gdb-demangle.h. */
193
c906108c 194int
fba45db2 195is_cplus_marker (int c)
c906108c
SS
196{
197 return c && strchr (cplus_markers, c) != NULL;
198}
199
50f182aa
DE
200extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
201
c906108c 202void
fba45db2 203_initialize_demangler (void)
c906108c 204{
fa58ee11
EZ
205 int i, ndems;
206
207 /* Fill the demangling_style_names[] array. */
208 for (ndems = 0;
209 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
210 ndems++)
211 ;
9e0c176c 212 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
fa58ee11
EZ
213 for (i = 0;
214 libiberty_demanglers[i].demangling_style != unknown_demangling;
215 i++)
216 demangling_style_names[i] =
217 xstrdup (libiberty_demanglers[i].demangling_style_name);
218
50f182aa
DE
219 add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
220Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
221Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
222 NULL,
223 show_demangle,
224 &setprintlist, &showprintlist);
225
226 add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
227Set demangling of C++/ObjC names in disassembly listings."), _("\
228Show demangling of C++/ObjC names in disassembly listings."), NULL,
229 NULL,
230 show_asm_demangle,
231 &setprintlist, &showprintlist);
232
7ab04401
AC
233 /* FIXME: cagney/2005-02-20: The code implementing this variable are
234 malloc-ing and free-ing current_demangling_style_string when it
235 should instead just point to an element of
236 demangling_style_names. */
237 add_setshow_enum_cmd ("demangle-style", class_support,
238 demangling_style_names,
239 (const char **) &current_demangling_style_string, _("\
240Set the current C++ demangling style."), _("\
241Show the current C++ demangling style."), _("\
242Use `set demangle-style' without arguments for a list of demangling styles."),
243 set_demangling_command,
920d2a44 244 show_demangling_style_names,
7ab04401 245 &setlist, &showlist);
c906108c 246
0963b4bd 247 /* Set the default demangling style chosen at compilation time. */
c906108c 248 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
c906108c 249}
This page took 1.136645 seconds and 4 git commands to generate.