2003-07-16 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b
AC
2
3 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2003 Free Software Foundation, Inc.
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
12 the Free Software Foundation; either version 2 of the License, or
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
21 along with this program; if not, write to the Free Software
c5aa993b
JM
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25
26/* This file contains support code for C++ demangling that is common
27 to a styles of demangling, and GDB specific. */
28
29#include "defs.h"
30#include "command.h"
31#include "gdbcmd.h"
32#include "demangle.h"
33#include "gdb_string.h"
34
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. */
42
43#ifndef DEFAULT_DEMANGLING_STYLE
44#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
45#endif
46
a14ed312 47extern void _initialize_demangler (void);
392a587b 48
c906108c
SS
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. */
52
53static char *current_demangling_style_string;
54
fa58ee11
EZ
55/* The array of names of the known demanglyng styles. Generated by
56 _initialize_demangler from libiberty_demanglers[] array. */
57
58static const char **demangling_style_names;
59
a14ed312 60static void set_demangling_command (char *, int, struct cmd_list_element *);
c906108c
SS
61
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.
65
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.
69
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
73 enum value.
74
75 Note: Assumes that current_demangling_style_string always points to
76 a malloc'd string, even if it is a null-string. */
77
78static void
fba45db2 79set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 80{
770de199 81 const struct demangler_engine *dem;
c906108c
SS
82
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. */
87
770de199
DB
88 for (dem = libiberty_demanglers;
89 dem->demangling_style != unknown_demangling;
90 dem++)
c906108c 91 {
bde58177
AC
92 if (strcmp (current_demangling_style_string,
93 dem->demangling_style_name) == 0)
c906108c
SS
94 {
95 current_demangling_style = dem->demangling_style;
96 break;
97 }
98 }
99
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. */
103
770de199 104 if (dem->demangling_style == unknown_demangling)
c906108c
SS
105 {
106 if (*current_demangling_style_string != '\0')
107 {
108 printf_unfiltered ("Unknown demangling style `%s'.\n",
109 current_demangling_style_string);
110 }
111 printf_unfiltered ("The currently understood settings are:\n\n");
770de199
DB
112 for (dem = libiberty_demanglers;
113 dem->demangling_style != unknown_demangling;
114 dem++)
c906108c
SS
115 {
116 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
117 dem->demangling_style_doc);
118 if (dem->demangling_style == current_demangling_style)
119 {
b8c9b27d 120 xfree (current_demangling_style_string);
c906108c
SS
121 current_demangling_style_string =
122 savestring (dem->demangling_style_name,
123 strlen (dem->demangling_style_name));
124 }
125 }
126 if (current_demangling_style == unknown_demangling)
127 {
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. */
770de199 131 current_demangling_style = libiberty_demanglers[0].demangling_style;
c906108c 132 current_demangling_style_string =
770de199
DB
133 savestring (
134 libiberty_demanglers[0].demangling_style_name,
135 strlen (libiberty_demanglers[0].demangling_style_name));
c906108c
SS
136 warning ("`%s' style demangling chosen as the default.\n",
137 current_demangling_style_string);
138 }
139 }
140}
141
142/* Fake a "set demangle-style" command. */
143
144void
fba45db2 145set_demangling_style (char *style)
c906108c
SS
146{
147 if (current_demangling_style_string != NULL)
148 {
b8c9b27d 149 xfree (current_demangling_style_string);
c906108c
SS
150 }
151 current_demangling_style_string = savestring (style, strlen (style));
152 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
153}
154
8343f86c
DJ
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
159 ELF targets)
160 - Occasionally '_' if neither of the above is usable
161
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. */
165
166static char cplus_markers[] = {'$', '.', '\0'};
c906108c
SS
167
168int
fba45db2 169is_cplus_marker (int c)
c906108c
SS
170{
171 return c && strchr (cplus_markers, c) != NULL;
172}
173
174void
fba45db2 175_initialize_demangler (void)
c906108c
SS
176{
177 struct cmd_list_element *set, *show;
fa58ee11
EZ
178 int i, ndems;
179
180 /* Fill the demangling_style_names[] array. */
181 for (ndems = 0;
182 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
183 ndems++)
184 ;
9e0c176c 185 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
fa58ee11
EZ
186 for (i = 0;
187 libiberty_demanglers[i].demangling_style != unknown_demangling;
188 i++)
189 demangling_style_names[i] =
190 xstrdup (libiberty_demanglers[i].demangling_style_name);
191
192 set = add_set_enum_cmd ("demangle-style", class_support,
193 demangling_style_names,
194 (const char **) &current_demangling_style_string,
195 "Set the current C++ demangling style.\n\
c906108c 196Use `set demangle-style' without arguments for a list of demangling styles.",
fa58ee11 197 &setlist);
c906108c 198 show = add_show_from_set (set, &showlist);
9f60d481 199 set_cmd_sfunc (set, set_demangling_command);
c906108c
SS
200
201 /* Set the default demangling style chosen at compilation time. */
202 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
c906108c 203}
This page took 0.261548 seconds and 4 git commands to generate.