119370Spst/* Basic C++ demangling support for GDB.
2130809Smarcel
3130809Smarcel   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4130809Smarcel   2001, 2003 Free Software Foundation, Inc.
5130809Smarcel
619370Spst   Written by Fred Fish at Cygnus Support.
719370Spst
846283Sdfr   This file is part of GDB.
919370Spst
1046283Sdfr   This program is free software; you can redistribute it and/or modify
1146283Sdfr   it under the terms of the GNU General Public License as published by
1246283Sdfr   the Free Software Foundation; either version 2 of the License, or
1346283Sdfr   (at your option) any later version.
1419370Spst
1546283Sdfr   This program is distributed in the hope that it will be useful,
1646283Sdfr   but WITHOUT ANY WARRANTY; without even the implied warranty of
1746283Sdfr   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1846283Sdfr   GNU General Public License for more details.
1919370Spst
2046283Sdfr   You should have received a copy of the GNU General Public License
2146283Sdfr   along with this program; if not, write to the Free Software
2298948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2398948Sobrien   Boston, MA 02111-1307, USA.  */
2419370Spst
2519370Spst
2619370Spst/*  This file contains support code for C++ demangling that is common
2746283Sdfr   to a styles of demangling, and GDB specific. */
2819370Spst
2919370Spst#include "defs.h"
3019370Spst#include "command.h"
3119370Spst#include "gdbcmd.h"
3219370Spst#include "demangle.h"
3319370Spst#include "gdb_string.h"
3419370Spst
3519370Spst/* Select the default C++ demangling style to use.  The default is "auto",
3619370Spst   which allows gdb to attempt to pick an appropriate demangling style for
3719370Spst   the executable it has loaded.  It can be set to a specific style ("gnu",
3846283Sdfr   "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
3919370Spst   selection of the style unless you do an explicit "set demangle auto".
4019370Spst   To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
4119370Spst   the appropriate target configuration file. */
4219370Spst
4319370Spst#ifndef DEFAULT_DEMANGLING_STYLE
4446283Sdfr#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
4519370Spst#endif
4619370Spst
4798948Sobrienextern void _initialize_demangler (void);
4898948Sobrien
4946283Sdfr/* String name for the current demangling style.  Set by the
5046283Sdfr   "set demangle-style" command, printed as part of the output by the
5146283Sdfr   "show demangle-style" command. */
5219370Spst
5319370Spststatic char *current_demangling_style_string;
5419370Spst
5598948Sobrien/* The array of names of the known demanglyng styles.  Generated by
5698948Sobrien   _initialize_demangler from libiberty_demanglers[] array.  */
5746283Sdfr
5898948Sobrienstatic const char **demangling_style_names;
5919370Spst
6098948Sobrienstatic void set_demangling_command (char *, int, struct cmd_list_element *);
6119370Spst
6246283Sdfr/* Set current demangling style.  Called by the "set demangle-style"
6346283Sdfr   command after it has updated the current_demangling_style_string to
6446283Sdfr   match what the user has entered.
6546283Sdfr
6646283Sdfr   If the user has entered a string that matches a known demangling style
6719370Spst   name in the demanglers[] array then just leave the string alone and update
6819370Spst   the current_demangling_style enum value to match.
6919370Spst
7046283Sdfr   If the user has entered a string that doesn't match, including an empty
7119370Spst   string, then print a list of the currently known styles and restore
7219370Spst   the current_demangling_style_string to match the current_demangling_style
7319370Spst   enum value.
7419370Spst
7519370Spst   Note:  Assumes that current_demangling_style_string always points to
7619370Spst   a malloc'd string, even if it is a null-string. */
7719370Spst
7819370Spststatic void
7998948Sobrienset_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
8019370Spst{
8198948Sobrien  const struct demangler_engine *dem;
8219370Spst
8319370Spst  /*  First just try to match whatever style name the user supplied with
8446283Sdfr     one of the known ones.  Don't bother special casing for an empty
8546283Sdfr     name, we just treat it as any other style name that doesn't match.
8646283Sdfr     If we match, update the current demangling style enum. */
8719370Spst
8898948Sobrien  for (dem = libiberty_demanglers;
8998948Sobrien       dem->demangling_style != unknown_demangling;
9098948Sobrien       dem++)
9119370Spst    {
92130809Smarcel      if (strcmp (current_demangling_style_string,
93130809Smarcel		  dem->demangling_style_name) == 0)
9419370Spst	{
9546283Sdfr	  current_demangling_style = dem->demangling_style;
9619370Spst	  break;
9719370Spst	}
9819370Spst    }
9919370Spst
10019370Spst  /* Check to see if we found a match.  If not, gripe about any non-empty
10119370Spst     style name and supply a list of valid ones.  FIXME:  This should
10219370Spst     probably be done with some sort of completion and with help. */
10319370Spst
10498948Sobrien  if (dem->demangling_style == unknown_demangling)
10519370Spst    {
10619370Spst      if (*current_demangling_style_string != '\0')
10719370Spst	{
10819370Spst	  printf_unfiltered ("Unknown demangling style `%s'.\n",
10946283Sdfr			     current_demangling_style_string);
11019370Spst	}
11119370Spst      printf_unfiltered ("The currently understood settings are:\n\n");
11298948Sobrien      for (dem = libiberty_demanglers;
11398948Sobrien	   dem->demangling_style != unknown_demangling;
11498948Sobrien	   dem++)
11519370Spst	{
11646283Sdfr	  printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
11746283Sdfr			     dem->demangling_style_doc);
11846283Sdfr	  if (dem->demangling_style == current_demangling_style)
11919370Spst	    {
12098948Sobrien	      xfree (current_demangling_style_string);
12119370Spst	      current_demangling_style_string =
12246283Sdfr		savestring (dem->demangling_style_name,
12346283Sdfr			    strlen (dem->demangling_style_name));
12419370Spst	    }
12519370Spst	}
12619370Spst      if (current_demangling_style == unknown_demangling)
12719370Spst	{
12819370Spst	  /* This can happen during initialization if gdb is compiled with
12919370Spst	     a DEMANGLING_STYLE value that is unknown, so pick the first
13019370Spst	     one as the default. */
13198948Sobrien	  current_demangling_style = libiberty_demanglers[0].demangling_style;
13219370Spst	  current_demangling_style_string =
13398948Sobrien	    savestring (
13498948Sobrien              libiberty_demanglers[0].demangling_style_name,
13598948Sobrien	      strlen (libiberty_demanglers[0].demangling_style_name));
13619370Spst	  warning ("`%s' style demangling chosen as the default.\n",
13719370Spst		   current_demangling_style_string);
13819370Spst	}
13919370Spst    }
14019370Spst}
14119370Spst
14246283Sdfr/* Fake a "set demangle-style" command. */
14319370Spst
14419370Spstvoid
14598948Sobrienset_demangling_style (char *style)
14619370Spst{
14719370Spst  if (current_demangling_style_string != NULL)
14819370Spst    {
14998948Sobrien      xfree (current_demangling_style_string);
15019370Spst    }
15119370Spst  current_demangling_style_string = savestring (style, strlen (style));
15246283Sdfr  set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
15319370Spst}
15419370Spst
155130809Smarcel/* G++ uses a special character to indicate certain internal names.  Which
156130809Smarcel   character it is depends on the platform:
157130809Smarcel   - Usually '$' on systems where the assembler will accept that
158130809Smarcel   - Usually '.' otherwise (this includes most sysv4-like systems and most
159130809Smarcel     ELF targets)
160130809Smarcel   - Occasionally '_' if neither of the above is usable
16119370Spst
162130809Smarcel   We check '$' first because it is the safest, and '.' often has another
163130809Smarcel   meaning.  We don't currently try to handle '_' because the precise forms
164130809Smarcel   of the names are different on those targets.  */
16519370Spst
166130809Smarcelstatic char cplus_markers[] = {'$', '.', '\0'};
16719370Spst
16819370Spstint
16998948Sobrienis_cplus_marker (int c)
17019370Spst{
17119370Spst  return c && strchr (cplus_markers, c) != NULL;
17219370Spst}
17319370Spst
17419370Spstvoid
17598948Sobrien_initialize_demangler (void)
17619370Spst{
17746283Sdfr  struct cmd_list_element *set, *show;
17898948Sobrien  int i, ndems;
17919370Spst
18098948Sobrien  /* Fill the demangling_style_names[] array.  */
18198948Sobrien  for (ndems = 0;
18298948Sobrien       libiberty_demanglers[ndems].demangling_style != unknown_demangling;
18398948Sobrien       ndems++)
18498948Sobrien    ;
18598948Sobrien  demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
18698948Sobrien  for (i = 0;
18798948Sobrien       libiberty_demanglers[i].demangling_style != unknown_demangling;
18898948Sobrien       i++)
18998948Sobrien    demangling_style_names[i] =
19098948Sobrien      xstrdup (libiberty_demanglers[i].demangling_style_name);
19198948Sobrien
19298948Sobrien  set = add_set_enum_cmd ("demangle-style", class_support,
19398948Sobrien			  demangling_style_names,
19498948Sobrien			  (const char **) &current_demangling_style_string,
19598948Sobrien			  "Set the current C++ demangling style.\n\
19619370SpstUse `set demangle-style' without arguments for a list of demangling styles.",
19798948Sobrien			  &setlist);
19846283Sdfr  show = add_show_from_set (set, &showlist);
19998948Sobrien  set_cmd_sfunc (set, set_demangling_command);
20019370Spst
20146283Sdfr  /* Set the default demangling style chosen at compilation time. */
20246283Sdfr  set_demangling_style (DEFAULT_DEMANGLING_STYLE);
20319370Spst}
204