| 1 | /* Basic C++ demangling support for GDB. |
| 2 | |
| 3 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, |
| 4 | 2001, 2003 Free Software Foundation, Inc. |
| 5 | |
| 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 |
| 22 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | Boston, MA 02111-1307, USA. */ |
| 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 | |
| 47 | extern void _initialize_demangler (void); |
| 48 | |
| 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 | |
| 53 | static char *current_demangling_style_string; |
| 54 | |
| 55 | /* The array of names of the known demanglyng styles. Generated by |
| 56 | _initialize_demangler from libiberty_demanglers[] array. */ |
| 57 | |
| 58 | static const char **demangling_style_names; |
| 59 | |
| 60 | static void set_demangling_command (char *, int, struct cmd_list_element *); |
| 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 | |
| 78 | static void |
| 79 | set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c) |
| 80 | { |
| 81 | const struct demangler_engine *dem; |
| 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 | |
| 88 | for (dem = libiberty_demanglers; |
| 89 | dem->demangling_style != unknown_demangling; |
| 90 | dem++) |
| 91 | { |
| 92 | if (strcmp (current_demangling_style_string, |
| 93 | dem->demangling_style_name) == 0) |
| 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 | |
| 104 | if (dem->demangling_style == unknown_demangling) |
| 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"); |
| 112 | for (dem = libiberty_demanglers; |
| 113 | dem->demangling_style != unknown_demangling; |
| 114 | dem++) |
| 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 | { |
| 120 | xfree (current_demangling_style_string); |
| 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. */ |
| 131 | current_demangling_style = libiberty_demanglers[0].demangling_style; |
| 132 | current_demangling_style_string = |
| 133 | savestring ( |
| 134 | libiberty_demanglers[0].demangling_style_name, |
| 135 | strlen (libiberty_demanglers[0].demangling_style_name)); |
| 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 | |
| 144 | void |
| 145 | set_demangling_style (char *style) |
| 146 | { |
| 147 | if (current_demangling_style_string != NULL) |
| 148 | { |
| 149 | xfree (current_demangling_style_string); |
| 150 | } |
| 151 | current_demangling_style_string = savestring (style, strlen (style)); |
| 152 | set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL); |
| 153 | } |
| 154 | |
| 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 | |
| 166 | static char cplus_markers[] = {'$', '.', '\0'}; |
| 167 | |
| 168 | int |
| 169 | is_cplus_marker (int c) |
| 170 | { |
| 171 | return c && strchr (cplus_markers, c) != NULL; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | _initialize_demangler (void) |
| 176 | { |
| 177 | struct cmd_list_element *set, *show; |
| 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 | ; |
| 185 | demangling_style_names = xcalloc (ndems + 1, sizeof (char *)); |
| 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 **) ¤t_demangling_style_string, |
| 195 | "Set the current C++ demangling style.\n\ |
| 196 | Use `set demangle-style' without arguments for a list of demangling styles.", |
| 197 | &setlist); |
| 198 | show = deprecated_add_show_from_set (set, &showlist); |
| 199 | set_cmd_sfunc (set, set_demangling_command); |
| 200 | |
| 201 | /* Set the default demangling style chosen at compilation time. */ |
| 202 | set_demangling_style (DEFAULT_DEMANGLING_STYLE); |
| 203 | } |