gdb/demangle.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Basic C++ demangling support for GDB.

  2.    Copyright (C) 1991-2015 Free Software Foundation, Inc.

  3.    Written by Fred Fish at Cygnus Support.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  15. /*  This file contains support code for C++ demangling that is common
  16.    to a styles of demangling, and GDB specific.  */

  17. #include "defs.h"
  18. #include "command.h"
  19. #include "gdbcmd.h"
  20. #include "demangle.h"
  21. #include "gdb-demangle.h"
  22. /* Select the default C++ demangling style to use.  The default is "auto",
  23.    which allows gdb to attempt to pick an appropriate demangling style for
  24.    the executable it has loaded.  It can be set to a specific style ("gnu",
  25.    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
  26.    selection of the style unless you do an explicit "set demangle auto".
  27.    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
  28.    the appropriate target configuration file.  */

  29. #ifndef DEFAULT_DEMANGLING_STYLE
  30. #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
  31. #endif

  32. /* See documentation in gdb-demangle.h.  */
  33. int demangle = 1;

  34. static void
  35. show_demangle (struct ui_file *file, int from_tty,
  36.                struct cmd_list_element *c, const char *value)
  37. {
  38.   fprintf_filtered (file,
  39.                     _("Demangling of encoded C++/ObjC names "
  40.                       "when displaying symbols is %s.\n"),
  41.                     value);
  42. }

  43. /* See documentation in gdb-demangle.h.  */
  44. int asm_demangle = 0;

  45. static void
  46. show_asm_demangle (struct ui_file *file, int from_tty,
  47.                    struct cmd_list_element *c, const char *value)
  48. {
  49.   fprintf_filtered (file,
  50.                     _("Demangling of C++/ObjC names in "
  51.                       "disassembly listings is %s.\n"),
  52.                     value);
  53. }

  54. /* String name for the current demangling style.  Set by the
  55.    "set demangle-style" command, printed as part of the output by the
  56.    "show demangle-style" command.  */

  57. static const char *current_demangling_style_string;

  58. /* The array of names of the known demanglyng styles.  Generated by
  59.    _initialize_demangler from libiberty_demanglers[] array.  */

  60. static const char **demangling_style_names;
  61. static void
  62. show_demangling_style_names(struct ui_file *file, int from_tty,
  63.                             struct cmd_list_element *c, const char *value)
  64. {
  65.   fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
  66.                     value);
  67. }

  68. /* Set current demangling style.  Called by the "set demangle-style"
  69.    command after it has updated the current_demangling_style_string to
  70.    match what the user has entered.

  71.    If the user has entered a string that matches a known demangling style
  72.    name in the demanglers[] array then just leave the string alone and update
  73.    the current_demangling_style enum value to match.

  74.    If the user has entered a string that doesn't match, including an empty
  75.    string, then print a list of the currently known styles and restore
  76.    the current_demangling_style_string to match the current_demangling_style
  77.    enum value.

  78.    Note:  Assumes that current_demangling_style_string always points to
  79.    a malloc'd string, even if it is a null-string.  */

  80. static void
  81. set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
  82. {
  83.   const struct demangler_engine *dem;
  84.   int i;

  85.   /*  First just try to match whatever style name the user supplied with
  86.      one of the known ones.  Don't bother special casing for an empty
  87.      name, we just treat it as any other style name that doesn't match.
  88.      If we match, update the current demangling style enum.  */

  89.   for (dem = libiberty_demanglers, i = 0;
  90.        dem->demangling_style != unknown_demangling;
  91.        dem++)
  92.     {
  93.       if (strcmp (current_demangling_style_string,
  94.                   dem->demangling_style_name) == 0)
  95.         {
  96.           current_demangling_style = dem->demangling_style;
  97.           current_demangling_style_string = demangling_style_names[i];
  98.           break;
  99.         }
  100.       i++;
  101.     }

  102.   /* We should have found a match, given we only add known styles to
  103.      the enumeration list.  */
  104.   gdb_assert (dem->demangling_style != unknown_demangling);
  105. }

  106. /* G++ uses a special character to indicate certain internal names.  Which
  107.    character it is depends on the platform:
  108.    - Usually '$' on systems where the assembler will accept that
  109.    - Usually '.' otherwise (this includes most sysv4-like systems and most
  110.      ELF targets)
  111.    - Occasionally '_' if neither of the above is usable

  112.    We check '$' first because it is the safest, and '.' often has another
  113.    meaning.  We don't currently try to handle '_' because the precise forms
  114.    of the names are different on those targets.  */

  115. static char cplus_markers[] = {'$', '.', '\0'};

  116. /* See documentation in gdb-demangle.h.  */

  117. int
  118. is_cplus_marker (int c)
  119. {
  120.   return c && strchr (cplus_markers, c) != NULL;
  121. }

  122. extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */

  123. void
  124. _initialize_demangler (void)
  125. {
  126.   int i, ndems;

  127.   /* Fill the demangling_style_names[] array, and set the default
  128.      demangling style chosen at compilation time.  */
  129.   for (ndems = 0;
  130.        libiberty_demanglers[ndems].demangling_style != unknown_demangling;
  131.        ndems++)
  132.     ;
  133.   demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
  134.   for (i = 0;
  135.        libiberty_demanglers[i].demangling_style != unknown_demangling;
  136.        i++)
  137.     {
  138.       demangling_style_names[i]
  139.         = xstrdup (libiberty_demanglers[i].demangling_style_name);

  140.       if (current_demangling_style_string == NULL
  141.           && strcmp (DEFAULT_DEMANGLING_STYLE, demangling_style_names[i]) == 0)
  142.         current_demangling_style_string = demangling_style_names[i];
  143.     }

  144.   add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
  145. Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
  146. Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
  147.                            NULL,
  148.                            show_demangle,
  149.                            &setprintlist, &showprintlist);

  150.   add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
  151. Set demangling of C++/ObjC names in disassembly listings."), _("\
  152. Show demangling of C++/ObjC names in disassembly listings."), NULL,
  153.                            NULL,
  154.                            show_asm_demangle,
  155.                            &setprintlist, &showprintlist);

  156.   add_setshow_enum_cmd ("demangle-style", class_support,
  157.                         demangling_style_names,
  158.                         &current_demangling_style_string, _("\
  159. Set the current C++ demangling style."), _("\
  160. Show the current C++ demangling style."), _("\
  161. Use `set demangle-style' without arguments for a list of demangling styles."),
  162.                         set_demangling_command,
  163.                         show_demangling_style_names,
  164.                         &setlist, &showlist);
  165. }