xref: /dflybsd-src/contrib/gdb-7/gdb/demangle.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Basic C++ demangling support for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4*5796c8dcSSimon Schubert    2003, 2007, 2008, 2009 Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    Written by Fred Fish at Cygnus Support.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This file is part of GDB.
9*5796c8dcSSimon Schubert 
10*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
11*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
12*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
13*5796c8dcSSimon Schubert    (at your option) any later version.
14*5796c8dcSSimon Schubert 
15*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
16*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*5796c8dcSSimon Schubert    GNU General Public License for more details.
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
21*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert 
24*5796c8dcSSimon Schubert /*  This file contains support code for C++ demangling that is common
25*5796c8dcSSimon Schubert    to a styles of demangling, and GDB specific. */
26*5796c8dcSSimon Schubert 
27*5796c8dcSSimon Schubert #include "defs.h"
28*5796c8dcSSimon Schubert #include "command.h"
29*5796c8dcSSimon Schubert #include "gdbcmd.h"
30*5796c8dcSSimon Schubert #include "demangle.h"
31*5796c8dcSSimon Schubert #include "gdb_string.h"
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert /* Select the default C++ demangling style to use.  The default is "auto",
34*5796c8dcSSimon Schubert    which allows gdb to attempt to pick an appropriate demangling style for
35*5796c8dcSSimon Schubert    the executable it has loaded.  It can be set to a specific style ("gnu",
36*5796c8dcSSimon Schubert    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
37*5796c8dcSSimon Schubert    selection of the style unless you do an explicit "set demangle auto".
38*5796c8dcSSimon Schubert    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
39*5796c8dcSSimon Schubert    the appropriate target configuration file. */
40*5796c8dcSSimon Schubert 
41*5796c8dcSSimon Schubert #ifndef DEFAULT_DEMANGLING_STYLE
42*5796c8dcSSimon Schubert #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
43*5796c8dcSSimon Schubert #endif
44*5796c8dcSSimon Schubert 
45*5796c8dcSSimon Schubert extern void _initialize_demangler (void);
46*5796c8dcSSimon Schubert 
47*5796c8dcSSimon Schubert /* String name for the current demangling style.  Set by the
48*5796c8dcSSimon Schubert    "set demangle-style" command, printed as part of the output by the
49*5796c8dcSSimon Schubert    "show demangle-style" command. */
50*5796c8dcSSimon Schubert 
51*5796c8dcSSimon Schubert static char *current_demangling_style_string;
52*5796c8dcSSimon Schubert 
53*5796c8dcSSimon Schubert /* The array of names of the known demanglyng styles.  Generated by
54*5796c8dcSSimon Schubert    _initialize_demangler from libiberty_demanglers[] array.  */
55*5796c8dcSSimon Schubert 
56*5796c8dcSSimon Schubert static const char **demangling_style_names;
57*5796c8dcSSimon Schubert static void
58*5796c8dcSSimon Schubert show_demangling_style_names(struct ui_file *file, int from_tty,
59*5796c8dcSSimon Schubert 			    struct cmd_list_element *c, const char *value)
60*5796c8dcSSimon Schubert {
61*5796c8dcSSimon Schubert   fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
62*5796c8dcSSimon Schubert 		    value);
63*5796c8dcSSimon Schubert }
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert static void set_demangling_command (char *, int, struct cmd_list_element *);
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert /* Set current demangling style.  Called by the "set demangle-style"
69*5796c8dcSSimon Schubert    command after it has updated the current_demangling_style_string to
70*5796c8dcSSimon Schubert    match what the user has entered.
71*5796c8dcSSimon Schubert 
72*5796c8dcSSimon Schubert    If the user has entered a string that matches a known demangling style
73*5796c8dcSSimon Schubert    name in the demanglers[] array then just leave the string alone and update
74*5796c8dcSSimon Schubert    the current_demangling_style enum value to match.
75*5796c8dcSSimon Schubert 
76*5796c8dcSSimon Schubert    If the user has entered a string that doesn't match, including an empty
77*5796c8dcSSimon Schubert    string, then print a list of the currently known styles and restore
78*5796c8dcSSimon Schubert    the current_demangling_style_string to match the current_demangling_style
79*5796c8dcSSimon Schubert    enum value.
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert    Note:  Assumes that current_demangling_style_string always points to
82*5796c8dcSSimon Schubert    a malloc'd string, even if it is a null-string. */
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert static void
85*5796c8dcSSimon Schubert set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
86*5796c8dcSSimon Schubert {
87*5796c8dcSSimon Schubert   const struct demangler_engine *dem;
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert   /*  First just try to match whatever style name the user supplied with
90*5796c8dcSSimon Schubert      one of the known ones.  Don't bother special casing for an empty
91*5796c8dcSSimon Schubert      name, we just treat it as any other style name that doesn't match.
92*5796c8dcSSimon Schubert      If we match, update the current demangling style enum. */
93*5796c8dcSSimon Schubert 
94*5796c8dcSSimon Schubert   for (dem = libiberty_demanglers;
95*5796c8dcSSimon Schubert        dem->demangling_style != unknown_demangling;
96*5796c8dcSSimon Schubert        dem++)
97*5796c8dcSSimon Schubert     {
98*5796c8dcSSimon Schubert       if (strcmp (current_demangling_style_string,
99*5796c8dcSSimon Schubert 		  dem->demangling_style_name) == 0)
100*5796c8dcSSimon Schubert 	{
101*5796c8dcSSimon Schubert 	  current_demangling_style = dem->demangling_style;
102*5796c8dcSSimon Schubert 	  break;
103*5796c8dcSSimon Schubert 	}
104*5796c8dcSSimon Schubert     }
105*5796c8dcSSimon Schubert 
106*5796c8dcSSimon Schubert   /* Check to see if we found a match.  If not, gripe about any non-empty
107*5796c8dcSSimon Schubert      style name and supply a list of valid ones.  FIXME:  This should
108*5796c8dcSSimon Schubert      probably be done with some sort of completion and with help. */
109*5796c8dcSSimon Schubert 
110*5796c8dcSSimon Schubert   if (dem->demangling_style == unknown_demangling)
111*5796c8dcSSimon Schubert     {
112*5796c8dcSSimon Schubert       if (*current_demangling_style_string != '\0')
113*5796c8dcSSimon Schubert 	{
114*5796c8dcSSimon Schubert 	  printf_unfiltered (_("Unknown demangling style `%s'.\n"),
115*5796c8dcSSimon Schubert 			     current_demangling_style_string);
116*5796c8dcSSimon Schubert 	}
117*5796c8dcSSimon Schubert       printf_unfiltered (_("The currently understood settings are:\n\n"));
118*5796c8dcSSimon Schubert       for (dem = libiberty_demanglers;
119*5796c8dcSSimon Schubert 	   dem->demangling_style != unknown_demangling;
120*5796c8dcSSimon Schubert 	   dem++)
121*5796c8dcSSimon Schubert 	{
122*5796c8dcSSimon Schubert 	  printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
123*5796c8dcSSimon Schubert 			     dem->demangling_style_doc);
124*5796c8dcSSimon Schubert 	  if (dem->demangling_style == current_demangling_style)
125*5796c8dcSSimon Schubert 	    {
126*5796c8dcSSimon Schubert 	      xfree (current_demangling_style_string);
127*5796c8dcSSimon Schubert 	      current_demangling_style_string =
128*5796c8dcSSimon Schubert 		xstrdup (dem->demangling_style_name);
129*5796c8dcSSimon Schubert 	    }
130*5796c8dcSSimon Schubert 	}
131*5796c8dcSSimon Schubert       if (current_demangling_style == unknown_demangling)
132*5796c8dcSSimon Schubert 	{
133*5796c8dcSSimon Schubert 	  /* This can happen during initialization if gdb is compiled with
134*5796c8dcSSimon Schubert 	     a DEMANGLING_STYLE value that is unknown, so pick the first
135*5796c8dcSSimon Schubert 	     one as the default. */
136*5796c8dcSSimon Schubert 	  current_demangling_style = libiberty_demanglers[0].demangling_style;
137*5796c8dcSSimon Schubert 	  current_demangling_style_string =
138*5796c8dcSSimon Schubert 	    xstrdup (libiberty_demanglers[0].demangling_style_name);
139*5796c8dcSSimon Schubert 	  warning (_("`%s' style demangling chosen as the default."),
140*5796c8dcSSimon Schubert 		   current_demangling_style_string);
141*5796c8dcSSimon Schubert 	}
142*5796c8dcSSimon Schubert     }
143*5796c8dcSSimon Schubert }
144*5796c8dcSSimon Schubert 
145*5796c8dcSSimon Schubert /* Fake a "set demangle-style" command. */
146*5796c8dcSSimon Schubert 
147*5796c8dcSSimon Schubert void
148*5796c8dcSSimon Schubert set_demangling_style (char *style)
149*5796c8dcSSimon Schubert {
150*5796c8dcSSimon Schubert   if (current_demangling_style_string != NULL)
151*5796c8dcSSimon Schubert     {
152*5796c8dcSSimon Schubert       xfree (current_demangling_style_string);
153*5796c8dcSSimon Schubert     }
154*5796c8dcSSimon Schubert   current_demangling_style_string = xstrdup (style);
155*5796c8dcSSimon Schubert   set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
156*5796c8dcSSimon Schubert }
157*5796c8dcSSimon Schubert 
158*5796c8dcSSimon Schubert /* G++ uses a special character to indicate certain internal names.  Which
159*5796c8dcSSimon Schubert    character it is depends on the platform:
160*5796c8dcSSimon Schubert    - Usually '$' on systems where the assembler will accept that
161*5796c8dcSSimon Schubert    - Usually '.' otherwise (this includes most sysv4-like systems and most
162*5796c8dcSSimon Schubert      ELF targets)
163*5796c8dcSSimon Schubert    - Occasionally '_' if neither of the above is usable
164*5796c8dcSSimon Schubert 
165*5796c8dcSSimon Schubert    We check '$' first because it is the safest, and '.' often has another
166*5796c8dcSSimon Schubert    meaning.  We don't currently try to handle '_' because the precise forms
167*5796c8dcSSimon Schubert    of the names are different on those targets.  */
168*5796c8dcSSimon Schubert 
169*5796c8dcSSimon Schubert static char cplus_markers[] = {'$', '.', '\0'};
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert int
172*5796c8dcSSimon Schubert is_cplus_marker (int c)
173*5796c8dcSSimon Schubert {
174*5796c8dcSSimon Schubert   return c && strchr (cplus_markers, c) != NULL;
175*5796c8dcSSimon Schubert }
176*5796c8dcSSimon Schubert 
177*5796c8dcSSimon Schubert void
178*5796c8dcSSimon Schubert _initialize_demangler (void)
179*5796c8dcSSimon Schubert {
180*5796c8dcSSimon Schubert   struct cmd_list_element *set, *show;
181*5796c8dcSSimon Schubert   int i, ndems;
182*5796c8dcSSimon Schubert 
183*5796c8dcSSimon Schubert   /* Fill the demangling_style_names[] array.  */
184*5796c8dcSSimon Schubert   for (ndems = 0;
185*5796c8dcSSimon Schubert        libiberty_demanglers[ndems].demangling_style != unknown_demangling;
186*5796c8dcSSimon Schubert        ndems++)
187*5796c8dcSSimon Schubert     ;
188*5796c8dcSSimon Schubert   demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
189*5796c8dcSSimon Schubert   for (i = 0;
190*5796c8dcSSimon Schubert        libiberty_demanglers[i].demangling_style != unknown_demangling;
191*5796c8dcSSimon Schubert        i++)
192*5796c8dcSSimon Schubert     demangling_style_names[i] =
193*5796c8dcSSimon Schubert       xstrdup (libiberty_demanglers[i].demangling_style_name);
194*5796c8dcSSimon Schubert 
195*5796c8dcSSimon Schubert   /* FIXME: cagney/2005-02-20: The code implementing this variable are
196*5796c8dcSSimon Schubert      malloc-ing and free-ing current_demangling_style_string when it
197*5796c8dcSSimon Schubert      should instead just point to an element of
198*5796c8dcSSimon Schubert      demangling_style_names.  */
199*5796c8dcSSimon Schubert   add_setshow_enum_cmd ("demangle-style", class_support,
200*5796c8dcSSimon Schubert 			demangling_style_names,
201*5796c8dcSSimon Schubert 			(const char **) &current_demangling_style_string, _("\
202*5796c8dcSSimon Schubert Set the current C++ demangling style."), _("\
203*5796c8dcSSimon Schubert Show the current C++ demangling style."), _("\
204*5796c8dcSSimon Schubert Use `set demangle-style' without arguments for a list of demangling styles."),
205*5796c8dcSSimon Schubert 			set_demangling_command,
206*5796c8dcSSimon Schubert 			show_demangling_style_names,
207*5796c8dcSSimon Schubert 			&setlist, &showlist);
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert   /* Set the default demangling style chosen at compilation time. */
210*5796c8dcSSimon Schubert   set_demangling_style (DEFAULT_DEMANGLING_STYLE);
211*5796c8dcSSimon Schubert }
212