1 /* Basic C++ demangling support for GDB.
3 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2003 Free Software Foundation, Inc.
6 Written by Fred Fish at Cygnus Support.
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
26 /* This file contains support code for C++ demangling that is common
27 to a styles of demangling, and GDB specific. */
33 #include "gdb_string.h"
35 /* Select the default C++ demangling style to use. The default is "auto",
36 which allows gdb to attempt to pick an appropriate demangling style for
37 the executable it has loaded. It can be set to a specific style ("gnu",
38 "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
39 selection of the style unless you do an explicit "set demangle auto".
40 To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
41 the appropriate target configuration file. */
43 #ifndef DEFAULT_DEMANGLING_STYLE
44 #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
47 extern void _initialize_demangler (void);
49 /* String name for the current demangling style. Set by the
50 "set demangle-style" command, printed as part of the output by the
51 "show demangle-style" command. */
53 static char *current_demangling_style_string;
55 /* The array of names of the known demanglyng styles. Generated by
56 _initialize_demangler from libiberty_demanglers[] array. */
58 static const char **demangling_style_names;
60 static void set_demangling_command (char *, int, struct cmd_list_element *);
62 /* Set current demangling style. Called by the "set demangle-style"
63 command after it has updated the current_demangling_style_string to
64 match what the user has entered.
66 If the user has entered a string that matches a known demangling style
67 name in the demanglers[] array then just leave the string alone and update
68 the current_demangling_style enum value to match.
70 If the user has entered a string that doesn't match, including an empty
71 string, then print a list of the currently known styles and restore
72 the current_demangling_style_string to match the current_demangling_style
75 Note: Assumes that current_demangling_style_string always points to
76 a malloc'd string, even if it is a null-string. */
79 set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
81 const struct demangler_engine *dem;
83 /* First just try to match whatever style name the user supplied with
84 one of the known ones. Don't bother special casing for an empty
85 name, we just treat it as any other style name that doesn't match.
86 If we match, update the current demangling style enum. */
88 for (dem = libiberty_demanglers;
89 dem->demangling_style != unknown_demangling;
92 if (strcmp (current_demangling_style_string,
93 dem->demangling_style_name) == 0)
95 current_demangling_style = dem->demangling_style;
100 /* Check to see if we found a match. If not, gripe about any non-empty
101 style name and supply a list of valid ones. FIXME: This should
102 probably be done with some sort of completion and with help. */
104 if (dem->demangling_style == unknown_demangling)
106 if (*current_demangling_style_string != '\0')
108 printf_unfiltered ("Unknown demangling style `%s'.\n",
109 current_demangling_style_string);
111 printf_unfiltered ("The currently understood settings are:\n\n");
112 for (dem = libiberty_demanglers;
113 dem->demangling_style != unknown_demangling;
116 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
117 dem->demangling_style_doc);
118 if (dem->demangling_style == current_demangling_style)
120 xfree (current_demangling_style_string);
121 current_demangling_style_string =
122 savestring (dem->demangling_style_name,
123 strlen (dem->demangling_style_name));
126 if (current_demangling_style == unknown_demangling)
128 /* This can happen during initialization if gdb is compiled with
129 a DEMANGLING_STYLE value that is unknown, so pick the first
130 one as the default. */
131 current_demangling_style = libiberty_demanglers[0].demangling_style;
132 current_demangling_style_string =
134 libiberty_demanglers[0].demangling_style_name,
135 strlen (libiberty_demanglers[0].demangling_style_name));
136 warning ("`%s' style demangling chosen as the default.\n",
137 current_demangling_style_string);
142 /* Fake a "set demangle-style" command. */
145 set_demangling_style (char *style)
147 if (current_demangling_style_string != NULL)
149 xfree (current_demangling_style_string);
151 current_demangling_style_string = savestring (style, strlen (style));
152 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
155 /* G++ uses a special character to indicate certain internal names. Which
156 character it is depends on the platform:
157 - Usually '$' on systems where the assembler will accept that
158 - Usually '.' otherwise (this includes most sysv4-like systems and most
160 - Occasionally '_' if neither of the above is usable
162 We check '$' first because it is the safest, and '.' often has another
163 meaning. We don't currently try to handle '_' because the precise forms
164 of the names are different on those targets. */
166 static char cplus_markers[] = {'$', '.', '\0'};
169 is_cplus_marker (int c)
171 return c && strchr (cplus_markers, c) != NULL;
175 _initialize_demangler (void)
177 struct cmd_list_element *set, *show;
180 /* Fill the demangling_style_names[] array. */
182 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
185 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
187 libiberty_demanglers[i].demangling_style != unknown_demangling;
189 demangling_style_names[i] =
190 xstrdup (libiberty_demanglers[i].demangling_style_name);
192 set = add_set_enum_cmd ("demangle-style", class_support,
193 demangling_style_names,
194 (const char **) ¤t_demangling_style_string,
195 "Set the current C++ demangling style.\n\
196 Use `set demangle-style' without arguments for a list of demangling styles.",
198 show = deprecated_add_show_from_set (set, &showlist);
199 set_cmd_sfunc (set, set_demangling_command);
201 /* Set the default demangling style chosen at compilation time. */
202 set_demangling_style (DEFAULT_DEMANGLING_STYLE);