GDB (API)
/home/stan/gdb/src/gdb/demangle.c
Go to the documentation of this file.
00001 /* Basic C++ demangling support for GDB.
00002 
00003    Copyright (C) 1991-2013 Free Software Foundation, Inc.
00004 
00005    Written by Fred Fish at Cygnus Support.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 
00023 /*  This file contains support code for C++ demangling that is common
00024    to a styles of demangling, and GDB specific.  */
00025 
00026 #include "defs.h"
00027 #include "command.h"
00028 #include "gdbcmd.h"
00029 #include "demangle.h"
00030 #include "gdb-demangle.h"
00031 #include "gdb_string.h"
00032 
00033 /* Select the default C++ demangling style to use.  The default is "auto",
00034    which allows gdb to attempt to pick an appropriate demangling style for
00035    the executable it has loaded.  It can be set to a specific style ("gnu",
00036    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
00037    selection of the style unless you do an explicit "set demangle auto".
00038    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
00039    the appropriate target configuration file.  */
00040 
00041 #ifndef DEFAULT_DEMANGLING_STYLE
00042 #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
00043 #endif
00044 
00045 /* See documentation in gdb-demangle.h.  */
00046 int demangle = 1;
00047 
00048 static void
00049 show_demangle (struct ui_file *file, int from_tty,
00050                struct cmd_list_element *c, const char *value)
00051 {
00052   fprintf_filtered (file,
00053                     _("Demangling of encoded C++/ObjC names "
00054                       "when displaying symbols is %s.\n"),
00055                     value);
00056 }
00057 
00058 /* See documentation in gdb-demangle.h.  */
00059 int asm_demangle = 0;
00060 
00061 static void
00062 show_asm_demangle (struct ui_file *file, int from_tty,
00063                    struct cmd_list_element *c, const char *value)
00064 {
00065   fprintf_filtered (file,
00066                     _("Demangling of C++/ObjC names in "
00067                       "disassembly listings is %s.\n"),
00068                     value);
00069 }
00070 
00071 /* String name for the current demangling style.  Set by the
00072    "set demangle-style" command, printed as part of the output by the
00073    "show demangle-style" command.  */
00074 
00075 static const char *current_demangling_style_string;
00076 
00077 /* The array of names of the known demanglyng styles.  Generated by
00078    _initialize_demangler from libiberty_demanglers[] array.  */
00079 
00080 static const char **demangling_style_names;
00081 static void
00082 show_demangling_style_names(struct ui_file *file, int from_tty,
00083                             struct cmd_list_element *c, const char *value)
00084 {
00085   fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
00086                     value);
00087 }
00088 
00089 /* Set current demangling style.  Called by the "set demangle-style"
00090    command after it has updated the current_demangling_style_string to
00091    match what the user has entered.
00092 
00093    If the user has entered a string that matches a known demangling style
00094    name in the demanglers[] array then just leave the string alone and update
00095    the current_demangling_style enum value to match.
00096 
00097    If the user has entered a string that doesn't match, including an empty
00098    string, then print a list of the currently known styles and restore
00099    the current_demangling_style_string to match the current_demangling_style
00100    enum value.
00101 
00102    Note:  Assumes that current_demangling_style_string always points to
00103    a malloc'd string, even if it is a null-string.  */
00104 
00105 static void
00106 set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
00107 {
00108   const struct demangler_engine *dem;
00109   int i;
00110 
00111   /*  First just try to match whatever style name the user supplied with
00112      one of the known ones.  Don't bother special casing for an empty
00113      name, we just treat it as any other style name that doesn't match.
00114      If we match, update the current demangling style enum.  */
00115 
00116   for (dem = libiberty_demanglers, i = 0;
00117        dem->demangling_style != unknown_demangling; 
00118        dem++)
00119     {
00120       if (strcmp (current_demangling_style_string,
00121                   dem->demangling_style_name) == 0)
00122         {
00123           current_demangling_style = dem->demangling_style;
00124           current_demangling_style_string = demangling_style_names[i];
00125           break;
00126         }
00127       i++;
00128     }
00129 
00130   /* We should have found a match, given we only add known styles to
00131      the enumeration list.  */
00132   gdb_assert (dem->demangling_style != unknown_demangling);
00133 }
00134 
00135 /* G++ uses a special character to indicate certain internal names.  Which
00136    character it is depends on the platform:
00137    - Usually '$' on systems where the assembler will accept that
00138    - Usually '.' otherwise (this includes most sysv4-like systems and most
00139      ELF targets)
00140    - Occasionally '_' if neither of the above is usable
00141 
00142    We check '$' first because it is the safest, and '.' often has another
00143    meaning.  We don't currently try to handle '_' because the precise forms
00144    of the names are different on those targets.  */
00145 
00146 static char cplus_markers[] = {'$', '.', '\0'};
00147 
00148 /* See documentation in gdb-demangle.h.  */
00149 
00150 int
00151 is_cplus_marker (int c)
00152 {
00153   return c && strchr (cplus_markers, c) != NULL;
00154 }
00155 
00156 extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
00157 
00158 void
00159 _initialize_demangler (void)
00160 {
00161   int i, ndems;
00162 
00163   /* Fill the demangling_style_names[] array, and set the default
00164      demangling style chosen at compilation time.  */
00165   for (ndems = 0;
00166        libiberty_demanglers[ndems].demangling_style != unknown_demangling; 
00167        ndems++)
00168     ;
00169   demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
00170   for (i = 0;
00171        libiberty_demanglers[i].demangling_style != unknown_demangling; 
00172        i++)
00173     {
00174       demangling_style_names[i]
00175         = xstrdup (libiberty_demanglers[i].demangling_style_name);
00176 
00177       if (current_demangling_style_string == NULL
00178           && strcmp (DEFAULT_DEMANGLING_STYLE, demangling_style_names[i]) == 0)
00179         current_demangling_style_string = demangling_style_names[i];
00180     }
00181 
00182   add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
00183 Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
00184 Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
00185                            NULL,
00186                            show_demangle,
00187                            &setprintlist, &showprintlist);
00188 
00189   add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
00190 Set demangling of C++/ObjC names in disassembly listings."), _("\
00191 Show demangling of C++/ObjC names in disassembly listings."), NULL,
00192                            NULL,
00193                            show_asm_demangle,
00194                            &setprintlist, &showprintlist);
00195 
00196   add_setshow_enum_cmd ("demangle-style", class_support,
00197                         demangling_style_names,
00198                         &current_demangling_style_string, _("\
00199 Set the current C++ demangling style."), _("\
00200 Show the current C++ demangling style."), _("\
00201 Use `set demangle-style' without arguments for a list of demangling styles."),
00202                         set_demangling_command,
00203                         show_demangling_style_names,
00204                         &setlist, &showlist);
00205 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines