| 1 | /* Basic C++ demangling support for GDB. |
| 2 | Copyright 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | |
| 22 | /* This file contains support code for C++ demangling that is common |
| 23 | to a styles of demangling, and GDB specific. */ |
| 24 | |
| 25 | #include "defs.h" |
| 26 | #include "command.h" |
| 27 | #include "gdbcmd.h" |
| 28 | #include "demangle.h" |
| 29 | #include <string.h> |
| 30 | |
| 31 | /* Select the default C++ demangling style to use. The default is "auto", |
| 32 | which allows gdb to attempt to pick an appropriate demangling style for |
| 33 | the executable it has loaded. It can be set to a specific style ("gnu", |
| 34 | "lucid", "arm", etc.) in which case gdb will never attempt to do auto |
| 35 | selection of the style unless you do an explicit "set demangle auto". |
| 36 | To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in |
| 37 | the appropriate target configuration file. */ |
| 38 | |
| 39 | #ifndef DEFAULT_DEMANGLING_STYLE |
| 40 | # define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING |
| 41 | #endif |
| 42 | |
| 43 | /* String name for the current demangling style. Set by the "set demangling" |
| 44 | command, printed as part of the output by the "show demangling" command. */ |
| 45 | |
| 46 | static char *current_demangling_style_string; |
| 47 | |
| 48 | /* List of supported demangling styles. Contains the name of the style as |
| 49 | seen by the user, and the enum value that corresponds to that style. */ |
| 50 | |
| 51 | static const struct demangler |
| 52 | { |
| 53 | char *demangling_style_name; |
| 54 | enum demangling_styles demangling_style; |
| 55 | char *demangling_style_doc; |
| 56 | } demanglers [] = |
| 57 | { |
| 58 | {AUTO_DEMANGLING_STYLE_STRING, |
| 59 | auto_demangling, |
| 60 | "Automatic selection based on executable"}, |
| 61 | {GNU_DEMANGLING_STYLE_STRING, |
| 62 | gnu_demangling, |
| 63 | "GNU (g++) style demangling"}, |
| 64 | {LUCID_DEMANGLING_STYLE_STRING, |
| 65 | lucid_demangling, |
| 66 | "Lucid (lcc) style demangling"}, |
| 67 | {ARM_DEMANGLING_STYLE_STRING, |
| 68 | arm_demangling, |
| 69 | "ARM style demangling"}, |
| 70 | {NULL, unknown_demangling, NULL} |
| 71 | }; |
| 72 | |
| 73 | /* show current demangling style. */ |
| 74 | |
| 75 | static void |
| 76 | show_demangling_command (ignore, from_tty) |
| 77 | char *ignore; |
| 78 | int from_tty; |
| 79 | { |
| 80 | /* done automatically by show command. */ |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* set current demangling style. called by the "set demangling" command |
| 85 | after it has updated the current_demangling_style_string to match |
| 86 | what the user has entered. |
| 87 | |
| 88 | if the user has entered a string that matches a known demangling style |
| 89 | name in the demanglers[] array then just leave the string alone and update |
| 90 | the current_demangling_style enum value to match. |
| 91 | |
| 92 | if the user has entered a string that doesn't match, including an empty |
| 93 | string, then print a list of the currently known styles and restore |
| 94 | the current_demangling_style_string to match the current_demangling_style |
| 95 | enum value. |
| 96 | |
| 97 | Note: Assumes that current_demangling_style_string always points to |
| 98 | a malloc'd string, even if it is a null-string. */ |
| 99 | |
| 100 | static void |
| 101 | set_demangling_command (ignore, from_tty) |
| 102 | char *ignore; |
| 103 | int from_tty; |
| 104 | { |
| 105 | const struct demangler *dem; |
| 106 | |
| 107 | /* First just try to match whatever style name the user supplied with |
| 108 | one of the known ones. Don't bother special casing for an empty |
| 109 | name, we just treat it as any other style name that doesn't match. |
| 110 | If we match, update the current demangling style enum. */ |
| 111 | |
| 112 | for (dem = demanglers; dem -> demangling_style_name != NULL; dem++) |
| 113 | { |
| 114 | if (STREQ (current_demangling_style_string, |
| 115 | dem -> demangling_style_name)) |
| 116 | { |
| 117 | current_demangling_style = dem -> demangling_style; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Check to see if we found a match. If not, gripe about any non-empty |
| 123 | style name and supply a list of valid ones. FIXME: This should |
| 124 | probably be done with some sort of completion and with help. */ |
| 125 | |
| 126 | if (dem -> demangling_style_name == NULL) |
| 127 | { |
| 128 | if (*current_demangling_style_string != '\0') |
| 129 | { |
| 130 | printf ("Unknown demangling style `%s'.\n", |
| 131 | current_demangling_style_string); |
| 132 | } |
| 133 | printf ("The currently understood settings are:\n\n"); |
| 134 | for (dem = demanglers; dem -> demangling_style_name != NULL; dem++) |
| 135 | { |
| 136 | printf ("%-10s %s\n", dem -> demangling_style_name, |
| 137 | dem -> demangling_style_doc); |
| 138 | if (dem -> demangling_style == current_demangling_style) |
| 139 | { |
| 140 | free (current_demangling_style_string); |
| 141 | current_demangling_style_string = |
| 142 | strdup (dem -> demangling_style_name); |
| 143 | } |
| 144 | } |
| 145 | if (current_demangling_style == unknown_demangling) |
| 146 | { |
| 147 | /* This can happen during initialization if gdb is compiled with |
| 148 | a DEMANGLING_STYLE value that is unknown, so pick the first |
| 149 | one as the default. */ |
| 150 | current_demangling_style = demanglers[0].demangling_style; |
| 151 | current_demangling_style_string = |
| 152 | strdup (demanglers[0].demangling_style_name); |
| 153 | warning ("`%s' style demangling chosen as the default.\n", |
| 154 | current_demangling_style_string); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* Fake a "set demangling" command. */ |
| 160 | |
| 161 | void |
| 162 | set_demangling_style (style) |
| 163 | char *style; |
| 164 | { |
| 165 | if (current_demangling_style_string != NULL) |
| 166 | { |
| 167 | free (current_demangling_style_string); |
| 168 | } |
| 169 | current_demangling_style_string = strdup (style); |
| 170 | set_demangling_command ((char *) NULL, 0); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | _initialize_demangler () |
| 175 | { |
| 176 | struct cmd_list_element *set, *show; |
| 177 | |
| 178 | set = add_set_cmd ("demangle-style", class_support, var_string_noescape, |
| 179 | (char *) ¤t_demangling_style_string, |
| 180 | "Set the current C++ demangling style.", |
| 181 | &setlist); |
| 182 | show = add_show_from_set (set, &showlist); |
| 183 | set -> function.cfunc = set_demangling_command; |
| 184 | show -> function.cfunc = show_demangling_command; |
| 185 | |
| 186 | /* Set the default demangling style chosen at compilation time. */ |
| 187 | set_demangling_style (DEFAULT_DEMANGLING_STYLE); |
| 188 | set_cplus_marker_for_demangling (CPLUS_MARKER); |
| 189 | } |