binutils-gdb/gdb/cp-support.h
Pedro Alves cb2cd8cba8 Fix "b f(std::string)", always use DMGL_VERBOSE
Currently, on any remotely modern GNU/Linux system,
gdb.cp/no-dmgl-verbose.exp fails like so:

  break 'f(std::string)'
  Function "f(std::string)" not defined.
  (gdb) FAIL: gdb.cp/no-dmgl-verbose.exp: gdb_breakpoint: set breakpoint at 'f(std::string)'
  break 'f(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
  Function "f(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)" not defined.
  (gdb) PASS: gdb.cp/no-dmgl-verbose.exp: DMGL_VERBOSE-demangled f(std::string) is not defined

This testcase was added back in 2011, here:

  [patch] Remove DMGL_VERBOSE
  https://sourceware.org/pipermail/gdb-patches/2011-June/083081.html

Back then, the testcase would pass cleanly.  It turns out that the
reason it fails today is that the testcase is exercising something in
GDB that only makes sense if the program is built for the pre-C++11
libstc++ ABI.  Back then the C++11 ABI didn't exist yet, but nowadays,
you need to compile with -D_GLIBCXX_USE_CXX11_ABI=0 to use the old
ABI.  See "Dual ABI" in the libstdc++ manual, at
<https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html>.

If we tweak the gdb.cp/no-dmgl-verbose.exp testcase to force the old
ABI with -D_GLIBCXX_USE_CXX11_ABI=0, then it passes cleanly.

So why is it that setting a breakpoint at "f(std::string)" fails with
modern ABI, but passes with old ABI?

This is where libiberty demangler's DMGL_VERBOSE option comes in.  The
Itanium ABI mangling scheme has a shorthand form for std::string (and
some other types).  See
<https://itanium-cxx-abi.github.io/cxx-abi/abi.html>:

  "In addition, the following catalog of abbreviations of the form "Sx" are used:

     <substitution> ::= St # ::std::
     <substitution> ::= Sa # ::std::allocator
     <substitution> ::= Sb # ::std::basic_string
     <substitution> ::= Ss # ::std::basic_string < char,
						   ::std::char_traits<char>,
						   ::std::allocator<char> >
     <substitution> ::= Si # ::std::basic_istream<char,  std::char_traits<char> >
     <substitution> ::= So # ::std::basic_ostream<char,  std::char_traits<char> >
     <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
  "

When the libiberty demangler encounters such a abbreviation, by
default, it expands it to the user-friendly typedef "std::string",
"std::iostream", etc..  If OTOH DMGL_VERBOSE is specified, the
abbreviation is expanded to the underlying, non-typedefed fullname
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >"
etc. as documented in the Itanium ABI, and pasted above.  You can see
the standard abbreviations/substitutions in
libiberty/cp-demangle.c:standard_subs.

Back before Jan's patch in 2011, there were parts of GDB that used
DMGL_VERBOSE, and others that did not, leading to mismatches.  The
solution back then was to stop using DMGL_VERBOSE throughout.

GDB has code in place to let users set a breakpoint at a function with
typedefs in its parameters, like "b f(uint32_t)".  Demangled function
names as they appear in the symbol tables almost (more on this is in a
bit) never have typedefs in them, so when processing "b f(uint32_t)"
GDB first replaces "uint32_t" for its underlying type, and then sets a
breakpoint on the resulting prototype, in this case "f(unsigned int)".

Now, if DMGL_VERBOSE is _not_ used, then the demangler demangles the
mangled name of a function such as "void f(std::string)" that was
mangled using the standard abbreviations mentioned above really as:

  "void f(std::string)".

For example, the mangled name of "void f(std::string)" if you compile
with old pre-C++11 ABI is "_Z1fSs".  That uses the abbreviation "Ss",
so if you demangle that without DMGL_VERBOSE, you get:

  $ echo "_Z1fSs" | c++filt --no-verbose
  f(std::string)

while with DMGL_VERBOSE you'd get:

  $ echo "_Z1fSs" | c++filt
  f(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)

If, when the user sets a breakpoint at "f(std::string)", GDB would
replace the std::string typedef for its underlying type using the same
mechanism I mentioned for the "f(uint32_t)" example above, then GDB
would really try to set a breakpoint at "f(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)", and that would fail,
as the function symbol GDB knows about for that function, given no
DMGL_VERBOSE, is "f(std::string)".

For this reason, the code that expands typedefs in function parameter
names has an exception for std::string (and other standard
abbreviation types), such that "std::string" is never
typedef-expanded.

And here lies the problem when you try to do "b f(std::string)" with a
program compiled with the C++11 ABI.  In that case, std::string
expands to a different underlying type, like so:

  f(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

and this symbol naturally mangles differently, as:

  _Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

and then because this doesn't use the shorthand mangling abbreviation
for "std::string" anymore, it always demangles as:

  f(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

Now, when using the C++11 ABI, and you set a breakpoint at
"f(std::string)", GDB's typedefs-in-parameters expansion code hits the
exception for "std::string" and doesn't expand it, so the breakpoint
fails to be inserted, because the symbol that exists is really the

  f(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

one, not "f(std::string)".

So to fix things for C++11 ABI, clearly we need to remove the
"std::string" exception from the typedef-in-parameters expansion
logic.  If we do just that, then "b f(std::string)" starts working
with the C++11 ABI.

However, if we do _just_ that, and nothing else, then we break
pre-C++11 ABI...

The solution is then to in addition switch GDB to always use
DMGL_VERBOSE.  If we do this, then pre-C++11 ABI symbols works the
same as C++11 ABI symbols overall -- the demangler expands the
standard abbreviation for "std::string" as "std::basic_string<char,
std::char_traits<char>, std::allocator<char> >" and letting GDB expand
the "std::string" typedef (etc.) too is no longer a problem.

To avoid getting in the situation where some parts of GDB use
DMGL_VERBOSE and others not, this patch adds wrappers around the
demangler's entry points that GDB uses, and makes those force
DMGL_VERBOSE.

The point of the gdb.cp/no-dmgl-verbose.exp testcase was to try to
ensure that DMGL_VERBOSE doesn't creep back in:

 gdb_test {break 'f(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'} \
	  {Function ".*" not defined\.} \
	  "DMGL_VERBOSE-demangled f(std::string) is not defined"

This obviously no longer makes sense to have, since we now depend on
DMGL_VERBOSE.  So the patch replaces gdb.cp/no-dmgl-verbose.exp with a
new gdb.cp/break-f-std-string.exp testcase whose purpose is to make
sure that setting a breakpoint at "f(std::string)" works.  It
exercises both pre-C++11 ABI and C++11 ABI.

Change-Id: Ib54fab4cf0fd307bfd55bf1dd5056830096a653b
2022-05-10 14:16:20 +01:00

217 lines
6.9 KiB
C++

/* Helper routines for C++ support in GDB.
Copyright (C) 2002-2022 Free Software Foundation, Inc.
Contributed by MontaVista Software.
Namespace support contributed by David Carlton.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef CP_SUPPORT_H
#define CP_SUPPORT_H
/* We need this for 'domain_enum', alas... */
#include "symtab.h"
#include "gdbsupport/gdb_vecs.h"
#include "gdbsupport/gdb_obstack.h"
#include "gdbsupport/array-view.h"
#include <vector>
/* Opaque declarations. */
struct symbol;
struct block;
struct buildsym_compunit;
struct objfile;
struct type;
struct demangle_component;
struct using_direct;
/* A string representing the name of the anonymous namespace used in GDB. */
#define CP_ANONYMOUS_NAMESPACE_STR "(anonymous namespace)"
/* The length of the string representing the anonymous namespace. */
#define CP_ANONYMOUS_NAMESPACE_LEN 21
/* A string representing the start of an operator name. */
#define CP_OPERATOR_STR "operator"
/* The length of CP_OPERATOR_STR. */
#define CP_OPERATOR_LEN 8
/* The result of parsing a name. */
struct demangle_parse_info
{
demangle_parse_info ();
~demangle_parse_info ();
/* The memory used during the parse. */
struct demangle_info *info;
/* The result of the parse. */
struct demangle_component *tree;
/* Any temporary memory used during typedef replacement. */
struct obstack obstack;
};
/* Functions from cp-support.c. */
extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string
(const char *string);
extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_no_typedefs
(const char *string);
typedef const char *(canonicalization_ftype) (struct type *, void *);
extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_full
(const char *string, canonicalization_ftype *finder, void *data);
extern char *cp_class_name_from_physname (const char *physname);
extern char *method_name_from_physname (const char *physname);
extern unsigned int cp_find_first_component (const char *name);
extern unsigned int cp_entire_prefix_len (const char *name);
extern gdb::unique_xmalloc_ptr<char> cp_func_name (const char *full_name);
extern gdb::unique_xmalloc_ptr<char> cp_remove_params
(const char *demanged_name);
/* DEMANGLED_NAME is the name of a function, (optionally) including
parameters and (optionally) a return type. Return the name of the
function without parameters or return type, or NULL if we can not
parse the name. If COMPLETION_MODE is true, then tolerate a
non-existing or unbalanced parameter list. */
extern gdb::unique_xmalloc_ptr<char> cp_remove_params_if_any
(const char *demangled_name, bool completion_mode);
extern std::vector<symbol *> make_symbol_overload_list (const char *,
const char *);
extern void add_symbol_overload_list_adl
(gdb::array_view<type *> arg_types,
const char *func_name,
std::vector<symbol *> *overload_list);
extern struct type *cp_lookup_rtti_type (const char *name,
const struct block *block);
/* Produce an unsigned hash value from SEARCH_NAME that is compatible
with cp_symbol_name_matches. Only the last component in
"foo::bar::function()" is considered for hashing purposes (i.e.,
the entire prefix is skipped), so that later on looking up for
"function" or "bar::function" in all namespaces is possible. */
extern unsigned int cp_search_name_hash (const char *search_name);
/* Implement the "get_symbol_name_matcher" language_defn method for C++. */
extern symbol_name_matcher_ftype *cp_get_symbol_name_matcher
(const lookup_name_info &lookup_name);
/* Functions/variables from cp-namespace.c. */
extern int cp_is_in_anonymous (const char *symbol_name);
extern void cp_scan_for_anonymous_namespaces (struct buildsym_compunit *,
const struct symbol *symbol,
struct objfile *objfile);
extern struct block_symbol cp_lookup_symbol_nonlocal
(const struct language_defn *langdef,
const char *name,
const struct block *block,
const domain_enum domain);
extern struct block_symbol
cp_lookup_symbol_namespace (const char *the_namespace,
const char *name,
const struct block *block,
const domain_enum domain);
extern struct block_symbol cp_lookup_symbol_imports_or_template
(const char *scope,
const char *name,
const struct block *block,
const domain_enum domain);
extern struct block_symbol
cp_lookup_nested_symbol (struct type *parent_type,
const char *nested_name,
const struct block *block,
const domain_enum domain);
struct type *cp_lookup_transparent_type (const char *name);
/* See description in cp-namespace.c. */
struct type *cp_find_type_baseclass_by_name (struct type *parent_type,
const char *name);
/* Functions from cp-name-parser.y. */
extern std::unique_ptr<demangle_parse_info> cp_demangled_name_to_comp
(const char *demangled_name, std::string *errmsg);
/* Convert RESULT to a string. ESTIMATED_LEN is used only as a guide
to the length of the result. */
extern gdb::unique_xmalloc_ptr<char> cp_comp_to_string
(struct demangle_component *result, int estimated_len);
extern void cp_merge_demangle_parse_infos (struct demangle_parse_info *,
struct demangle_component *,
struct demangle_parse_info *);
/* The list of "maint cplus" commands. */
extern struct cmd_list_element *maint_cplus_cmd_list;
/* Wrappers for bfd and libiberty demangling entry points. Note they
all force DMGL_VERBOSE so that callers don't need to. This is so
that GDB consistently uses DMGL_VERBOSE throughout -- we want
libiberty's demangler to expand standard substitutions to their
full template name. */
/* A wrapper for bfd_demangle. */
gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options);
/* A wrapper for cplus_demangle_print. */
extern char *gdb_cplus_demangle_print (int options,
struct demangle_component *tree,
int estimated_length,
size_t *p_allocated_size);
/* Find an instance of the character C in the string S that is outside
of all parenthesis pairs, single-quoted strings, and double-quoted
strings. Also, ignore the char within a template name, like a ','
within foo<int, int>. */
extern const char *find_toplevel_char (const char *s, char c);
#endif /* CP_SUPPORT_H */