| 1 | /* Basic C++ demangling support for GDB. |
| 2 | Copyright 1991, 1992, 1996, 1999 Free Software Foundation, Inc. |
| 3 | Written by Fred Fish at Cygnus Support. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | Boston, MA 02111-1307, USA. */ |
| 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_string.h" |
| 31 | |
| 32 | /* Select the default C++ demangling style to use. The default is "auto", |
| 33 | which allows gdb to attempt to pick an appropriate demangling style for |
| 34 | the executable it has loaded. It can be set to a specific style ("gnu", |
| 35 | "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto |
| 36 | selection of the style unless you do an explicit "set demangle auto". |
| 37 | To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in |
| 38 | the appropriate target configuration file. */ |
| 39 | |
| 40 | #ifndef DEFAULT_DEMANGLING_STYLE |
| 41 | #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING |
| 42 | #endif |
| 43 | |
| 44 | extern void _initialize_demangler (void); |
| 45 | |
| 46 | /* String name for the current demangling style. Set by the |
| 47 | "set demangle-style" command, printed as part of the output by the |
| 48 | "show demangle-style" command. */ |
| 49 | |
| 50 | static char *current_demangling_style_string; |
| 51 | |
| 52 | static void set_demangling_command (char *, int, struct cmd_list_element *); |
| 53 | |
| 54 | /* Set current demangling style. Called by the "set demangle-style" |
| 55 | command after it has updated the current_demangling_style_string to |
| 56 | match what the user has entered. |
| 57 | |
| 58 | If the user has entered a string that matches a known demangling style |
| 59 | name in the demanglers[] array then just leave the string alone and update |
| 60 | the current_demangling_style enum value to match. |
| 61 | |
| 62 | If the user has entered a string that doesn't match, including an empty |
| 63 | string, then print a list of the currently known styles and restore |
| 64 | the current_demangling_style_string to match the current_demangling_style |
| 65 | enum value. |
| 66 | |
| 67 | Note: Assumes that current_demangling_style_string always points to |
| 68 | a malloc'd string, even if it is a null-string. */ |
| 69 | |
| 70 | static void |
| 71 | set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c) |
| 72 | { |
| 73 | const struct demangler_engine *dem; |
| 74 | |
| 75 | /* First just try to match whatever style name the user supplied with |
| 76 | one of the known ones. Don't bother special casing for an empty |
| 77 | name, we just treat it as any other style name that doesn't match. |
| 78 | If we match, update the current demangling style enum. */ |
| 79 | |
| 80 | for (dem = libiberty_demanglers; |
| 81 | dem->demangling_style != unknown_demangling; |
| 82 | dem++) |
| 83 | { |
| 84 | if (STREQ (current_demangling_style_string, |
| 85 | dem->demangling_style_name)) |
| 86 | { |
| 87 | current_demangling_style = dem->demangling_style; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* Check to see if we found a match. If not, gripe about any non-empty |
| 93 | style name and supply a list of valid ones. FIXME: This should |
| 94 | probably be done with some sort of completion and with help. */ |
| 95 | |
| 96 | if (dem->demangling_style == unknown_demangling) |
| 97 | { |
| 98 | if (*current_demangling_style_string != '\0') |
| 99 | { |
| 100 | printf_unfiltered ("Unknown demangling style `%s'.\n", |
| 101 | current_demangling_style_string); |
| 102 | } |
| 103 | printf_unfiltered ("The currently understood settings are:\n\n"); |
| 104 | for (dem = libiberty_demanglers; |
| 105 | dem->demangling_style != unknown_demangling; |
| 106 | dem++) |
| 107 | { |
| 108 | printf_unfiltered ("%-10s %s\n", dem->demangling_style_name, |
| 109 | dem->demangling_style_doc); |
| 110 | if (dem->demangling_style == current_demangling_style) |
| 111 | { |
| 112 | free (current_demangling_style_string); |
| 113 | current_demangling_style_string = |
| 114 | savestring (dem->demangling_style_name, |
| 115 | strlen (dem->demangling_style_name)); |
| 116 | } |
| 117 | } |
| 118 | if (current_demangling_style == unknown_demangling) |
| 119 | { |
| 120 | /* This can happen during initialization if gdb is compiled with |
| 121 | a DEMANGLING_STYLE value that is unknown, so pick the first |
| 122 | one as the default. */ |
| 123 | current_demangling_style = libiberty_demanglers[0].demangling_style; |
| 124 | current_demangling_style_string = |
| 125 | savestring ( |
| 126 | libiberty_demanglers[0].demangling_style_name, |
| 127 | strlen (libiberty_demanglers[0].demangling_style_name)); |
| 128 | warning ("`%s' style demangling chosen as the default.\n", |
| 129 | current_demangling_style_string); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Fake a "set demangle-style" command. */ |
| 135 | |
| 136 | void |
| 137 | set_demangling_style (char *style) |
| 138 | { |
| 139 | if (current_demangling_style_string != NULL) |
| 140 | { |
| 141 | free (current_demangling_style_string); |
| 142 | } |
| 143 | current_demangling_style_string = savestring (style, strlen (style)); |
| 144 | set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL); |
| 145 | } |
| 146 | |
| 147 | /* In order to allow a single demangler executable to demangle strings |
| 148 | using various common values of CPLUS_MARKER, as well as any specific |
| 149 | one set at compile time, we maintain a string containing all the |
| 150 | commonly used ones, and check to see if the marker we are looking for |
| 151 | is in that string. CPLUS_MARKER is usually '$' on systems where the |
| 152 | assembler can deal with that. Where the assembler can't, it's usually |
| 153 | '.' (but on many systems '.' is used for other things). We put the |
| 154 | current defined CPLUS_MARKER first (which defaults to '$'), followed |
| 155 | by the next most common value, followed by an explicit '$' in case |
| 156 | the value of CPLUS_MARKER is not '$'. |
| 157 | |
| 158 | We could avoid this if we could just get g++ to tell us what the actual |
| 159 | cplus marker character is as part of the debug information, perhaps by |
| 160 | ensuring that it is the character that terminates the gcc<n>_compiled |
| 161 | marker symbol (FIXME). */ |
| 162 | |
| 163 | static char cplus_markers[] = |
| 164 | {CPLUS_MARKER, '.', '$', '\0'}; |
| 165 | |
| 166 | int |
| 167 | is_cplus_marker (int c) |
| 168 | { |
| 169 | return c && strchr (cplus_markers, c) != NULL; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | _initialize_demangler (void) |
| 174 | { |
| 175 | struct cmd_list_element *set, *show; |
| 176 | |
| 177 | set = add_set_cmd ("demangle-style", class_support, var_string_noescape, |
| 178 | (char *) ¤t_demangling_style_string, |
| 179 | "Set the current C++ demangling style.\n\ |
| 180 | Use `set demangle-style' without arguments for a list of demangling styles.", |
| 181 | &setlist); |
| 182 | show = add_show_from_set (set, &showlist); |
| 183 | set->function.sfunc = set_demangling_command; |
| 184 | |
| 185 | /* Set the default demangling style chosen at compilation time. */ |
| 186 | set_demangling_style (DEFAULT_DEMANGLING_STYLE); |
| 187 | set_cplus_marker_for_demangling (CPLUS_MARKER); |
| 188 | } |