GDB (xrefs)
Loading...
Searching...
No Matches
gdb-demangle.c
Go to the documentation of this file.
1/* Basic C++ demangling support for GDB.
2
3 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4
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
11 the Free Software Foundation; either version 3 of the License, or
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
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 "cli/cli-utils.h"
28#include "command.h"
29#include "gdbcmd.h"
30#include "demangle.h"
31#include "gdb-demangle.h"
32#include "language.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
40 the appropriate target configuration file. */
41
42#ifndef DEFAULT_DEMANGLING_STYLE
43#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
44#endif
45
46/* See documentation in gdb-demangle.h. */
47bool demangle = true;
48
49static void
50show_demangle (struct ui_file *file, int from_tty,
51 struct cmd_list_element *c, const char *value)
52{
53 gdb_printf (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. */
60bool asm_demangle = false;
61
62static void
63show_asm_demangle (struct ui_file *file, int from_tty,
64 struct cmd_list_element *c, const char *value)
65{
66 gdb_printf (file,
67 _("Demangling of C++/ObjC names in "
68 "disassembly listings is %s.\n"),
69 value);
70}
71
72/* String name for the current demangling style. Set by the
73 "set demangle-style" command, printed as part of the output by the
74 "show demangle-style" command. */
75
77
78/* The array of names of the known demangling styles. Generated by
79 _initialize_demangler from libiberty_demanglers[] array. */
80
81static const char **demangling_style_names;
82static void
83show_demangling_style_names(struct ui_file *file, int from_tty,
84 struct cmd_list_element *c, const char *value)
85{
86 gdb_printf (file, _("The current C++ demangling style is \"%s\".\n"),
87 value);
88}
89
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
104 a malloc'd string, even if it is a null-string. */
105
106static void
107set_demangling_command (const char *ignore,
108 int from_tty, struct cmd_list_element *c)
109{
110 const struct demangler_engine *dem;
111 int i;
112
113 /* First just try to match whatever style name the user supplied with
114 one of the known ones. Don't bother special casing for an empty
115 name, we just treat it as any other style name that doesn't match.
116 If we match, update the current demangling style enum. */
117
118 for (dem = libiberty_demanglers, i = 0;
119 dem->demangling_style != unknown_demangling;
120 dem++)
121 {
123 dem->demangling_style_name) == 0)
124 {
125 current_demangling_style = dem->demangling_style;
127 break;
128 }
129 i++;
130 }
131
132 /* We should have found a match, given we only add known styles to
133 the enumeration list. */
134 gdb_assert (dem->demangling_style != unknown_demangling);
135}
136
137/* G++ uses a special character to indicate certain internal names. Which
138 character it is depends on the platform:
139 - Usually '$' on systems where the assembler will accept that
140 - Usually '.' otherwise (this includes most sysv4-like systems and most
141 ELF targets)
142 - Occasionally '_' if neither of the above is usable
143
144 We check '$' first because it is the safest, and '.' often has another
145 meaning. We don't currently try to handle '_' because the precise forms
146 of the names are different on those targets. */
147
148static char cplus_markers[] = {'$', '.', '\0'};
149
150/* See documentation in gdb-demangle.h. */
151
152bool
154{
155 return c && strchr (cplus_markers, c) != NULL;
156}
157
158/* Demangle the given string in the current language. */
159
160static void
161demangle_command (const char *args, int from_tty)
162{
163 const char *name;
164 const char *arg_start;
165 int processing_args = 1;
166 const struct language_defn *lang;
167
168 std::string arg_buf = args != NULL ? args : "";
169 arg_start = arg_buf.c_str ();
170
171 std::string lang_name;
172 while (processing_args
173 && *arg_start == '-')
174 {
175 const char *p = skip_to_space (arg_start);
176
177 if (strncmp (arg_start, "-l", p - arg_start) == 0)
178 lang_name = extract_arg (&p);
179 else if (strncmp (arg_start, "--", p - arg_start) == 0)
180 processing_args = 0;
181 else
182 report_unrecognized_option_error ("demangle", arg_start);
183
184 arg_start = skip_spaces (p);
185 }
186
187 name = arg_start;
188
189 if (*name == '\0')
190 error (_("Usage: demangle [-l LANGUAGE] [--] NAME"));
191
192 if (!lang_name.empty ())
193 {
194 enum language lang_enum;
195
196 lang_enum = language_enum (lang_name.c_str ());
197 if (lang_enum == language_unknown)
198 error (_("Unknown language \"%s\""), lang_name.c_str ());
199 lang = language_def (lang_enum);
200 }
201 else
202 lang = current_language;
203
204 gdb::unique_xmalloc_ptr<char> demangled
205 = lang->demangle_symbol (name, DMGL_ANSI | DMGL_PARAMS);
206 if (demangled != NULL)
207 gdb_printf ("%s\n", demangled.get ());
208 else
209 error (_("Can't demangle \"%s\""), name);
210}
211
213void
215{
216 int i, ndems;
217
218 /* Fill the demangling_style_names[] array, and set the default
219 demangling style chosen at compilation time. */
220 for (ndems = 0;
221 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
222 ndems++)
223 ;
224 demangling_style_names = XCNEWVEC (const char *, ndems + 1);
225 for (i = 0;
226 libiberty_demanglers[i].demangling_style != unknown_demangling;
227 i++)
228 {
230 = xstrdup (libiberty_demanglers[i].demangling_style_name);
231
235 }
236
238Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
239Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
240 NULL,
243
244 add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
245Set demangling of C++/ObjC names in disassembly listings."), _("\
246Show demangling of C++/ObjC names in disassembly listings."), NULL,
247 NULL,
250
251 add_setshow_enum_cmd ("demangle-style", class_support,
254Set the current C++ demangling style."), _("\
255Show the current C++ demangling style."), _("\
256Use `set demangle-style' without arguments for a list of demangling styles."),
259 &setlist, &showlist);
260
261 add_cmd ("demangle", class_support, demangle_command, _("\
262Demangle a mangled name.\n\
263Usage: demangle [-l LANGUAGE] [--] NAME\n\
264If LANGUAGE is not specified, NAME is demangled in the current language."),
265 &cmdlist);
266}
const char *const name
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * showprintlist
Definition cli-cmds.c:163
struct cmd_list_element * cmdlist
Definition cli-cmds.c:87
struct cmd_list_element * setprintlist
Definition cli-cmds.c:161
struct cmd_list_element * setlist
Definition cli-cmds.c:119
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
set_show_commands add_setshow_enum_cmd(const char *name, enum command_class theclass, const char *const *enumlist, const char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:688
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
void report_unrecognized_option_error(const char *command, const char *args)
Definition cli-utils.c:184
std::string extract_arg(const char **arg)
Definition cli-utils.c:383
@ class_support
Definition command.h:58
language
Definition defs.h:211
@ language_unknown
Definition defs.h:212
static void show_demangle(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static void show_demangling_style_names(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
bool is_cplus_marker(int c)
bool demangle
void _initialize_gdb_demangle()
static void set_demangling_command(const char *ignore, int from_tty, struct cmd_list_element *c)
bool asm_demangle
static char cplus_markers[]
static const char ** demangling_style_names
static void show_asm_demangle(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
#define DEFAULT_DEMANGLING_STYLE
static const char * current_demangling_style_string
static void demangle_command(const char *args, int from_tty)
enum language language_enum(const char *str)
Definition language.c:427
const struct language_defn * language_def(enum language lang)
Definition language.c:439
const struct language_defn * current_language
Definition language.c:82
virtual gdb::unique_xmalloc_ptr< char > demangle_symbol(const char *mangled, int options) const
Definition language.h:448
Definition value.h:130
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886