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