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