xref: /dflybsd-src/contrib/gdb-7/gdb/demangle.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Basic C++ demangling support for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1991-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Written by Fred Fish at Cygnus Support.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert /*  This file contains support code for C++ demangling that is common
245796c8dcSSimon Schubert    to a styles of demangling, and GDB specific.  */
255796c8dcSSimon Schubert 
265796c8dcSSimon Schubert #include "defs.h"
275796c8dcSSimon Schubert #include "command.h"
285796c8dcSSimon Schubert #include "gdbcmd.h"
295796c8dcSSimon Schubert #include "demangle.h"
30a45ae5f8SJohn Marino #include "gdb-demangle.h"
315796c8dcSSimon Schubert #include "gdb_string.h"
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert /* Select the default C++ demangling style to use.  The default is "auto",
345796c8dcSSimon Schubert    which allows gdb to attempt to pick an appropriate demangling style for
355796c8dcSSimon Schubert    the executable it has loaded.  It can be set to a specific style ("gnu",
365796c8dcSSimon Schubert    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
375796c8dcSSimon Schubert    selection of the style unless you do an explicit "set demangle auto".
385796c8dcSSimon Schubert    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
395796c8dcSSimon Schubert    the appropriate target configuration file.  */
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert #ifndef DEFAULT_DEMANGLING_STYLE
425796c8dcSSimon Schubert #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
435796c8dcSSimon Schubert #endif
445796c8dcSSimon Schubert 
45a45ae5f8SJohn Marino /* See documentation in gdb-demangle.h.  */
46a45ae5f8SJohn Marino int demangle = 1;
47a45ae5f8SJohn Marino 
48a45ae5f8SJohn Marino static void
show_demangle(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)49a45ae5f8SJohn Marino show_demangle (struct ui_file *file, int from_tty,
50a45ae5f8SJohn Marino 	       struct cmd_list_element *c, const char *value)
51a45ae5f8SJohn Marino {
52a45ae5f8SJohn Marino   fprintf_filtered (file,
53a45ae5f8SJohn Marino 		    _("Demangling of encoded C++/ObjC names "
54a45ae5f8SJohn Marino 		      "when displaying symbols is %s.\n"),
55a45ae5f8SJohn Marino 		    value);
56a45ae5f8SJohn Marino }
57a45ae5f8SJohn Marino 
58a45ae5f8SJohn Marino /* See documentation in gdb-demangle.h.  */
59a45ae5f8SJohn Marino int asm_demangle = 0;
60a45ae5f8SJohn Marino 
61a45ae5f8SJohn Marino static void
show_asm_demangle(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)62a45ae5f8SJohn Marino show_asm_demangle (struct ui_file *file, int from_tty,
63a45ae5f8SJohn Marino 		   struct cmd_list_element *c, const char *value)
64a45ae5f8SJohn Marino {
65a45ae5f8SJohn Marino   fprintf_filtered (file,
66a45ae5f8SJohn Marino 		    _("Demangling of C++/ObjC names in "
67a45ae5f8SJohn Marino 		      "disassembly listings is %s.\n"),
68a45ae5f8SJohn Marino 		    value);
69a45ae5f8SJohn Marino }
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert /* String name for the current demangling style.  Set by the
725796c8dcSSimon Schubert    "set demangle-style" command, printed as part of the output by the
735796c8dcSSimon Schubert    "show demangle-style" command.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert static char *current_demangling_style_string;
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* The array of names of the known demanglyng styles.  Generated by
785796c8dcSSimon Schubert    _initialize_demangler from libiberty_demanglers[] array.  */
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert static const char **demangling_style_names;
815796c8dcSSimon Schubert static void
show_demangling_style_names(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)825796c8dcSSimon Schubert show_demangling_style_names(struct ui_file *file, int from_tty,
835796c8dcSSimon Schubert 			    struct cmd_list_element *c, const char *value)
845796c8dcSSimon Schubert {
855796c8dcSSimon Schubert   fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
865796c8dcSSimon Schubert 		    value);
875796c8dcSSimon Schubert }
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert /* Set current demangling style.  Called by the "set demangle-style"
905796c8dcSSimon Schubert    command after it has updated the current_demangling_style_string to
915796c8dcSSimon Schubert    match what the user has entered.
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert    If the user has entered a string that matches a known demangling style
945796c8dcSSimon Schubert    name in the demanglers[] array then just leave the string alone and update
955796c8dcSSimon Schubert    the current_demangling_style enum value to match.
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert    If the user has entered a string that doesn't match, including an empty
985796c8dcSSimon Schubert    string, then print a list of the currently known styles and restore
995796c8dcSSimon Schubert    the current_demangling_style_string to match the current_demangling_style
1005796c8dcSSimon Schubert    enum value.
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert    Note:  Assumes that current_demangling_style_string always points to
1035796c8dcSSimon Schubert    a malloc'd string, even if it is a null-string.  */
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert static void
set_demangling_command(char * ignore,int from_tty,struct cmd_list_element * c)1065796c8dcSSimon Schubert set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
1075796c8dcSSimon Schubert {
1085796c8dcSSimon Schubert   const struct demangler_engine *dem;
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert   /*  First just try to match whatever style name the user supplied with
1115796c8dcSSimon Schubert      one of the known ones.  Don't bother special casing for an empty
1125796c8dcSSimon Schubert      name, we just treat it as any other style name that doesn't match.
1135796c8dcSSimon Schubert      If we match, update the current demangling style enum.  */
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   for (dem = libiberty_demanglers;
1165796c8dcSSimon Schubert        dem->demangling_style != unknown_demangling;
1175796c8dcSSimon Schubert        dem++)
1185796c8dcSSimon Schubert     {
1195796c8dcSSimon Schubert       if (strcmp (current_demangling_style_string,
1205796c8dcSSimon Schubert 		  dem->demangling_style_name) == 0)
1215796c8dcSSimon Schubert 	{
1225796c8dcSSimon Schubert 	  current_demangling_style = dem->demangling_style;
1235796c8dcSSimon Schubert 	  break;
1245796c8dcSSimon Schubert 	}
1255796c8dcSSimon Schubert     }
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert   /* Check to see if we found a match.  If not, gripe about any non-empty
1285796c8dcSSimon Schubert      style name and supply a list of valid ones.  FIXME: This should
1295796c8dcSSimon Schubert      probably be done with some sort of completion and with help.  */
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert   if (dem->demangling_style == unknown_demangling)
1325796c8dcSSimon Schubert     {
1335796c8dcSSimon Schubert       if (*current_demangling_style_string != '\0')
1345796c8dcSSimon Schubert 	{
1355796c8dcSSimon Schubert 	  printf_unfiltered (_("Unknown demangling style `%s'.\n"),
1365796c8dcSSimon Schubert 			     current_demangling_style_string);
1375796c8dcSSimon Schubert 	}
1385796c8dcSSimon Schubert       printf_unfiltered (_("The currently understood settings are:\n\n"));
1395796c8dcSSimon Schubert       for (dem = libiberty_demanglers;
1405796c8dcSSimon Schubert 	   dem->demangling_style != unknown_demangling;
1415796c8dcSSimon Schubert 	   dem++)
1425796c8dcSSimon Schubert 	{
1435796c8dcSSimon Schubert 	  printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
1445796c8dcSSimon Schubert 			     dem->demangling_style_doc);
1455796c8dcSSimon Schubert 	  if (dem->demangling_style == current_demangling_style)
1465796c8dcSSimon Schubert 	    {
1475796c8dcSSimon Schubert 	      xfree (current_demangling_style_string);
1485796c8dcSSimon Schubert 	      current_demangling_style_string =
1495796c8dcSSimon Schubert 		xstrdup (dem->demangling_style_name);
1505796c8dcSSimon Schubert 	    }
1515796c8dcSSimon Schubert 	}
1525796c8dcSSimon Schubert       if (current_demangling_style == unknown_demangling)
1535796c8dcSSimon Schubert 	{
1545796c8dcSSimon Schubert 	  /* This can happen during initialization if gdb is compiled with
1555796c8dcSSimon Schubert 	     a DEMANGLING_STYLE value that is unknown, so pick the first
1565796c8dcSSimon Schubert 	     one as the default.  */
1575796c8dcSSimon Schubert 	  current_demangling_style = libiberty_demanglers[0].demangling_style;
1585796c8dcSSimon Schubert 	  current_demangling_style_string =
1595796c8dcSSimon Schubert 	    xstrdup (libiberty_demanglers[0].demangling_style_name);
1605796c8dcSSimon Schubert 	  warning (_("`%s' style demangling chosen as the default."),
1615796c8dcSSimon Schubert 		   current_demangling_style_string);
1625796c8dcSSimon Schubert 	}
1635796c8dcSSimon Schubert     }
1645796c8dcSSimon Schubert }
1655796c8dcSSimon Schubert 
166a45ae5f8SJohn Marino /* See documentation in gdb-demangle.h.  */
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert void
set_demangling_style(char * style)1695796c8dcSSimon Schubert set_demangling_style (char *style)
1705796c8dcSSimon Schubert {
1715796c8dcSSimon Schubert   if (current_demangling_style_string != NULL)
1725796c8dcSSimon Schubert     {
1735796c8dcSSimon Schubert       xfree (current_demangling_style_string);
1745796c8dcSSimon Schubert     }
1755796c8dcSSimon Schubert   current_demangling_style_string = xstrdup (style);
1765796c8dcSSimon Schubert   set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
1775796c8dcSSimon Schubert }
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert /* G++ uses a special character to indicate certain internal names.  Which
1805796c8dcSSimon Schubert    character it is depends on the platform:
1815796c8dcSSimon Schubert    - Usually '$' on systems where the assembler will accept that
1825796c8dcSSimon Schubert    - Usually '.' otherwise (this includes most sysv4-like systems and most
1835796c8dcSSimon Schubert      ELF targets)
1845796c8dcSSimon Schubert    - Occasionally '_' if neither of the above is usable
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert    We check '$' first because it is the safest, and '.' often has another
1875796c8dcSSimon Schubert    meaning.  We don't currently try to handle '_' because the precise forms
1885796c8dcSSimon Schubert    of the names are different on those targets.  */
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert static char cplus_markers[] = {'$', '.', '\0'};
1915796c8dcSSimon Schubert 
192a45ae5f8SJohn Marino /* See documentation in gdb-demangle.h.  */
193a45ae5f8SJohn Marino 
1945796c8dcSSimon Schubert int
is_cplus_marker(int c)1955796c8dcSSimon Schubert is_cplus_marker (int c)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert   return c && strchr (cplus_markers, c) != NULL;
1985796c8dcSSimon Schubert }
1995796c8dcSSimon Schubert 
200a45ae5f8SJohn Marino extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
201a45ae5f8SJohn Marino 
2025796c8dcSSimon Schubert void
_initialize_demangler(void)2035796c8dcSSimon Schubert _initialize_demangler (void)
2045796c8dcSSimon Schubert {
2055796c8dcSSimon Schubert   int i, ndems;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* Fill the demangling_style_names[] array.  */
2085796c8dcSSimon Schubert   for (ndems = 0;
2095796c8dcSSimon Schubert        libiberty_demanglers[ndems].demangling_style != unknown_demangling;
2105796c8dcSSimon Schubert        ndems++)
2115796c8dcSSimon Schubert     ;
2125796c8dcSSimon Schubert   demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
2135796c8dcSSimon Schubert   for (i = 0;
2145796c8dcSSimon Schubert        libiberty_demanglers[i].demangling_style != unknown_demangling;
2155796c8dcSSimon Schubert        i++)
2165796c8dcSSimon Schubert     demangling_style_names[i] =
2175796c8dcSSimon Schubert       xstrdup (libiberty_demanglers[i].demangling_style_name);
2185796c8dcSSimon Schubert 
219a45ae5f8SJohn Marino   add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
220a45ae5f8SJohn Marino Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
221a45ae5f8SJohn Marino Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
222a45ae5f8SJohn Marino 			   NULL,
223a45ae5f8SJohn Marino 			   show_demangle,
224a45ae5f8SJohn Marino 			   &setprintlist, &showprintlist);
225a45ae5f8SJohn Marino 
226a45ae5f8SJohn Marino   add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
227a45ae5f8SJohn Marino Set demangling of C++/ObjC names in disassembly listings."), _("\
228a45ae5f8SJohn Marino Show demangling of C++/ObjC names in disassembly listings."), NULL,
229a45ae5f8SJohn Marino 			   NULL,
230a45ae5f8SJohn Marino 			   show_asm_demangle,
231a45ae5f8SJohn Marino 			   &setprintlist, &showprintlist);
232a45ae5f8SJohn Marino 
2335796c8dcSSimon Schubert   /* FIXME: cagney/2005-02-20: The code implementing this variable are
2345796c8dcSSimon Schubert      malloc-ing and free-ing current_demangling_style_string when it
2355796c8dcSSimon Schubert      should instead just point to an element of
2365796c8dcSSimon Schubert      demangling_style_names.  */
2375796c8dcSSimon Schubert   add_setshow_enum_cmd ("demangle-style", class_support,
2385796c8dcSSimon Schubert 			demangling_style_names,
2395796c8dcSSimon Schubert 			(const char **) &current_demangling_style_string, _("\
2405796c8dcSSimon Schubert Set the current C++ demangling style."), _("\
2415796c8dcSSimon Schubert Show the current C++ demangling style."), _("\
2425796c8dcSSimon Schubert Use `set demangle-style' without arguments for a list of demangling styles."),
2435796c8dcSSimon Schubert 			set_demangling_command,
2445796c8dcSSimon Schubert 			show_demangling_style_names,
2455796c8dcSSimon Schubert 			&setlist, &showlist);
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert   /* Set the default demangling style chosen at compilation time.  */
2485796c8dcSSimon Schubert   set_demangling_style (DEFAULT_DEMANGLING_STYLE);
2495796c8dcSSimon Schubert }
250