GDB (xrefs)
/tmp/gdb-7.10/gdb/demangle.c
Go to the documentation of this file.
1 /* Basic C++ demangling support for GDB.
2 
3  Copyright (C) 1991-2015 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" /* for skip_to_space */
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 static void demangle_command (char *, int);
47 
48 /* See documentation in gdb-demangle.h. */
49 int demangle = 1;
50 
51 static void
52 show_demangle (struct ui_file *file, int from_tty,
53  struct cmd_list_element *c, const char *value)
54 {
55  fprintf_filtered (file,
56  _("Demangling of encoded C++/ObjC names "
57  "when displaying symbols is %s.\n"),
58  value);
59 }
60 
61 /* See documentation in gdb-demangle.h. */
62 int asm_demangle = 0;
63 
64 static void
65 show_asm_demangle (struct ui_file *file, int from_tty,
66  struct cmd_list_element *c, const char *value)
67 {
68  fprintf_filtered (file,
69  _("Demangling of C++/ObjC names in "
70  "disassembly listings is %s.\n"),
71  value);
72 }
73 
74 /* String name for the current demangling style. Set by the
75  "set demangle-style" command, printed as part of the output by the
76  "show demangle-style" command. */
77 
79 
80 /* The array of names of the known demanglyng styles. Generated by
81  _initialize_demangler from libiberty_demanglers[] array. */
82 
83 static const char **demangling_style_names;
84 static void
85 show_demangling_style_names(struct ui_file *file, int from_tty,
86  struct cmd_list_element *c, const char *value)
87 {
88  fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
89  value);
90 }
91 
92 /* Set current demangling style. Called by the "set demangle-style"
93  command after it has updated the current_demangling_style_string to
94  match what the user has entered.
95 
96  If the user has entered a string that matches a known demangling style
97  name in the demanglers[] array then just leave the string alone and update
98  the current_demangling_style enum value to match.
99 
100  If the user has entered a string that doesn't match, including an empty
101  string, then print a list of the currently known styles and restore
102  the current_demangling_style_string to match the current_demangling_style
103  enum value.
104 
105  Note: Assumes that current_demangling_style_string always points to
106  a malloc'd string, even if it is a null-string. */
107 
108 static void
109 set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
110 {
111  const struct demangler_engine *dem;
112  int i;
113 
114  /* First just try to match whatever style name the user supplied with
115  one of the known ones. Don't bother special casing for an empty
116  name, we just treat it as any other style name that doesn't match.
117  If we match, update the current demangling style enum. */
118 
119  for (dem = libiberty_demanglers, i = 0;
120  dem->demangling_style != unknown_demangling;
121  dem++)
122  {
124  dem->demangling_style_name) == 0)
125  {
126  current_demangling_style = dem->demangling_style;
128  break;
129  }
130  i++;
131  }
132 
133  /* We should have found a match, given we only add known styles to
134  the enumeration list. */
135  gdb_assert (dem->demangling_style != unknown_demangling);
136 }
137 
138 /* G++ uses a special character to indicate certain internal names. Which
139  character it is depends on the platform:
140  - Usually '$' on systems where the assembler will accept that
141  - Usually '.' otherwise (this includes most sysv4-like systems and most
142  ELF targets)
143  - Occasionally '_' if neither of the above is usable
144 
145  We check '$' first because it is the safest, and '.' often has another
146  meaning. We don't currently try to handle '_' because the precise forms
147  of the names are different on those targets. */
148 
149 static char cplus_markers[] = {'$', '.', '\0'};
150 
151 /* See documentation in gdb-demangle.h. */
152 
153 int
155 {
156  return c && strchr (cplus_markers, c) != NULL;
157 }
158 
159 /* Demangle the given string in the current language. */
160 
161 static void
162 demangle_command (char *args, int from_tty)
163 {
164  char *demangled, *name, *lang_name = NULL;
165  char *arg_buf, *arg_start;
166  int processing_args = 1;
167  const struct language_defn *lang;
168  struct cleanup *cleanups;
169 
170  arg_buf = xstrdup (args != NULL ? args : "");
171  cleanups = make_cleanup (xfree, arg_buf);
172  arg_start = arg_buf;
173 
174  while (processing_args
175  && *arg_start == '-')
176  {
177  char *p = skip_to_space (arg_start);
178 
179  if (strncmp (arg_start, "-l", p - arg_start) == 0)
180  {
181  char *lang_name_end;
182 
183  lang_name = skip_spaces (p);
184  lang_name_end = skip_to_space (lang_name);
185  lang_name = savestring (lang_name, lang_name_end - lang_name);
186  make_cleanup (xfree, lang_name);
187  p = lang_name_end;
188  }
189  else if (strncmp (arg_start, "--", p - arg_start) == 0)
190  processing_args = 0;
191  else
192  {
193  *p = '\0';
194  error (_("Unrecognized option '%s' to demangle command. "
195  "Try \"help demangle\"."), arg_start);
196  }
197 
198  arg_start = skip_spaces (p);
199  }
200 
201  name = arg_start;
202 
203  if (*name == '\0')
204  error (_("Usage: demangle [-l language] [--] name"));
205 
206  if (lang_name != NULL)
207  {
208  enum language lang_enum;
209 
210  lang_enum = language_enum (lang_name);
211  if (lang_enum == language_unknown)
212  error (_("Unknown language \"%s\""), lang_name);
213  lang = language_def (lang_enum);
214  }
215  else
216  lang = current_language;
217 
218  demangled = language_demangle (lang, name, DMGL_ANSI | DMGL_PARAMS);
219  if (demangled != NULL)
220  {
221  printf_filtered ("%s\n", demangled);
222  xfree (demangled);
223  }
224  else
225  error (_("Can't demangle \"%s\""), name);
226 
227  do_cleanups (cleanups);
228 }
229 
230 extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
231 
232 void
234 {
235  int i, ndems;
236 
237  /* Fill the demangling_style_names[] array, and set the default
238  demangling style chosen at compilation time. */
239  for (ndems = 0;
240  libiberty_demanglers[ndems].demangling_style != unknown_demangling;
241  ndems++)
242  ;
243  demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
244  for (i = 0;
245  libiberty_demanglers[i].demangling_style != unknown_demangling;
246  i++)
247  {
249  = xstrdup (libiberty_demanglers[i].demangling_style_name);
250 
254  }
255 
257 Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
258 Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
259  NULL,
262 
263  add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
264 Set demangling of C++/ObjC names in disassembly listings."), _("\
265 Show demangling of C++/ObjC names in disassembly listings."), NULL,
266  NULL,
269 
270  add_setshow_enum_cmd ("demangle-style", class_support,
273 Set the current C++ demangling style."), _("\
274 Show the current C++ demangling style."), _("\
275 Use `set demangle-style' without arguments for a list of demangling styles."),
278  &setlist, &showlist);
279 
280  add_cmd ("demangle", class_support, demangle_command, _("\
281 Demangle a mangled name.\n\
282 Usage: demangle [-l language] [--] name\n\
283 If LANGUAGE is not specified, NAME is demangled in the current language."),
284  &cmdlist);
285 }
enum language language_enum(char *str)
Definition: language.c:457
int demangle
Definition: demangle.c:49
void xfree(void *)
Definition: common-utils.c:97
#define DEFAULT_DEMANGLING_STYLE
Definition: demangle.c:43
void 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_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:487
const struct language_defn * language_def(enum language lang)
Definition: language.c:471
struct cmd_list_element * cmdlist
Definition: cli-cmds.c:103
char * skip_spaces(char *chp)
Definition: common-utils.c:259
#define _(String)
Definition: gdb_locale.h:40
void printf_filtered(const char *format,...)
Definition: utils.c:2388
static char cplus_markers[]
Definition: demangle.c:149
static const char * current_demangling_style_string
Definition: demangle.c:78
struct cmd_list_element * setlist
Definition: cli-cmds.c:135
int is_cplus_marker(int c)
Definition: demangle.c:154
const char *const name
Definition: aarch64-tdep.c:68
void initialize_file_ftype(void)
Definition: defs.h:281
int asm_demangle
Definition: demangle.c:62
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2351
struct cmd_list_element * showlist
Definition: cli-cmds.c:143
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, cmd_cfunc_ftype *fun, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:192
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
#define gdb_assert(expr)
Definition: gdb_assert.h:33
struct cmd_list_element * setprintlist
Definition: cli-cmds.c:169
static void show_asm_demangle(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: demangle.c:65
Definition: value.c:172
static void show_demangling_style_names(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: demangle.c:85
initialize_file_ftype _initialize_demangler
const struct language_defn * current_language
Definition: language.c:85
char * language_demangle(const struct language_defn *current_language, const char *mangled, int options)
Definition: language.c:629
struct cmd_list_element * showprintlist
Definition: cli-cmds.c:171
static void set_demangling_command(char *ignore, int from_tty, struct cmd_list_element *c)
Definition: demangle.c:109
static int ignore(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: corelow.c:917
language
Definition: defs.h:167
char * savestring(const char *ptr, size_t len)
Definition: common-utils.c:148
static void show_demangle(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: demangle.c:52
static void demangle_command(char *, int)
Definition: demangle.c:162
PTR xcalloc(size_t number, size_t size)
Definition: common-utils.c:71
void error(const char *fmt,...)
Definition: errors.c:38
#define skip_to_space(INP)
Definition: common-utils.h:94
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:541
static const char ** demangling_style_names
Definition: demangle.c:83