aboutsummaryrefslogtreecommitdiff
path: root/gdb/cp-support.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2020-05-30 14:20:10 +0100
committerPedro Alves <palves@redhat.com>2020-05-30 14:20:10 +0100
commitf68f85b52b2897ba54e0b119322be1abb2d53afe (patch)
tree63467c56e90cce13fe3c99cc00503bc8b0b2327d /gdb/cp-support.c
parentbb6e246742f8795aacfbea2401e505e97c079ffa (diff)
downloadgdb-f68f85b52b2897ba54e0b119322be1abb2d53afe.zip
gdb-f68f85b52b2897ba54e0b119322be1abb2d53afe.tar.gz
gdb-f68f85b52b2897ba54e0b119322be1abb2d53afe.tar.bz2
replace_typedefs: handle templates in namespaces
GDB currently crashes with infinite recursion, if you set a breakpoint on a function inside a namespace that includes a template on its fully qualified name, and, the template's name is also used as typedef in the global scope that expands to a name that includes the template name in its qualified name. For example, from the testcase added by this commit: namespace NS1 { namespace NS2 { template<typename T> struct Templ1 { T x; Templ1 (object_p) {} }} // namespace NS1::NS2 using Templ1 = NS1::NS2::Templ1<unsigned>; Setting a breakpoint like so: (gdb) break NS1::NS2::Templ1<int>::Templ1(NS1::NS2::object*) Results in infinite recursion, with this cycle (started by cp_canonicalize_string_full) repeating until the stack is exhausted: ... #1709 0x000000000055533c in inspect_type (info=0x38ff720, ret_comp=0xd83be10, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:267 #1710 0x0000000000555a6f in replace_typedefs (info=0x38ff720, ret_comp=0xd83be10, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:475 #1711 0x0000000000555a36 in replace_typedefs (info=0x38ff720, ret_comp=0xd83be70, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:470 #1712 0x0000000000555800 in replace_typedefs_qualified_name (info=0x38ff720, ret_comp=0xd839470, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:389 #1713 0x0000000000555a8c in replace_typedefs (info=0x38ff720, ret_comp=0xd839470, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:479 ... The demangle component tree for that symbol name looks like this: d_dump tree for NS1::NS2::Templ1<int>::Templ1(NS1::NS2::object*): typed name qualified name name 'NS1' qualified name name 'NS2' qualified name template <<<<<<<<<< name 'Templ1' template argument list builtin type int name 'Templ1' function type argument list pointer qualified name name 'NS1' qualified name name 'NS2' name 'object' The recursion starts at replace_typedefs_qualified_name, which doesn't handle the "template" node, and thus doesn't realize that the template name actually has the fully qualified name NS1::NS2::Templ1. replace_typedefs_qualified_name calls into replace_typedefs on the template node, and that ends up in inspect_type looking up for a symbol named "Templ1", which finds the global namespace typedef, which itself expands to NS1::NS2::Templ1. GDB then tries replacing typedefs in that newly expanded name, which ends up again in replace_typedefs_qualified_name, trying to expand a fully qualified name with "NS::NS2::Templ1<unsigned>" in its name, which results in recursion whenever the template node is reached. Fix this by teaching replace_typedefs_qualified_name how to handle template nodes. It needs handling in two places: the first spot handles the symbol above; the second spot handles a symbol like this, from the new test: (gdb) b NS1::NS2::grab_it(NS1::NS2::Templ1<int>*) d_dump tree for NS1::NS2::grab_it(NS1::NS2::Templ1<int>*): typed name qualified name name 'NS1' qualified name name 'NS2' name 'grab_it' function type argument list pointer qualified name name 'NS1' qualified name name 'NS2' template <<<<<<<< name 'Templ1' template argument list builtin type int What's different in this case is that the template node appears on the right child node of a qualified name, instead of on the left child. The testcase includes a test that checks whether template aliases are correctly replaced by GDB too. That fails with GCC due to GCC PR 95437, which makes GDB not know about a typedef for "NS1::NS2::AliasTempl<int>". GCC emits a typedef named "NS1::NS2::AliasTempl" instead, with no template parameter info. The test passes with Clang (5.0.2 at least). See more details here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95437 gdb/ChangeLog: 2020-05-30 Pedro Alves <palves@redhat.com> * cp-support.c (replace_typedefs_template): New. (replace_typedefs_qualified_name): Handle DEMANGLE_COMPONENT_TEMPLATE. gdb/testsuite/ChangeLog: 2020-05-30 Pedro Alves <palves@redhat.com> * gdb.linespec/cp-replace-typedefs-ns-template.cc: New. * gdb.linespec/cp-replace-typedefs-ns-template.exp: New.
Diffstat (limited to 'gdb/cp-support.c')
-rw-r--r--gdb/cp-support.c76
1 files changed, 72 insertions, 4 deletions
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 1e54aae..11e54c2 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -294,6 +294,42 @@ inspect_type (struct demangle_parse_info *info,
return 0;
}
+/* Helper for replace_typedefs_qualified_name to handle
+ DEMANGLE_COMPONENT_TEMPLATE. TMPL is the template node. BUF is
+ the buffer that holds the qualified name being built by
+ replace_typedefs_qualified_name. REPL is the node that will be
+ rewritten as a DEMANGLE_COMPONENT_NAME node holding the 'template
+ plus template arguments' name with typedefs replaced. */
+
+static bool
+replace_typedefs_template (struct demangle_parse_info *info,
+ string_file &buf,
+ struct demangle_component *tmpl,
+ struct demangle_component *repl,
+ canonicalization_ftype *finder,
+ void *data)
+{
+ demangle_component *tmpl_arglist = d_right (tmpl);
+
+ /* Replace typedefs in the template argument list. */
+ replace_typedefs (info, tmpl_arglist, finder, data);
+
+ /* Convert 'template + replaced template argument list' to a string
+ and replace the REPL node. */
+ gdb::unique_xmalloc_ptr<char> tmpl_str = cp_comp_to_string (tmpl, 100);
+ if (tmpl_str == nullptr)
+ {
+ /* If something went astray, abort typedef substitutions. */
+ return false;
+ }
+ buf.puts (tmpl_str.get ());
+
+ repl->type = DEMANGLE_COMPONENT_NAME;
+ repl->u.s_name.s = obstack_strdup (&info->obstack, buf.string ());
+ repl->u.s_name.len = buf.size ();
+ return true;
+}
+
/* Replace any typedefs appearing in the qualified name
(DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
given in INFO. */
@@ -314,6 +350,29 @@ replace_typedefs_qualified_name (struct demangle_parse_info *info,
substituted name. */
while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
{
+ if (d_left (comp)->type == DEMANGLE_COMPONENT_TEMPLATE)
+ {
+ /* Convert 'template + replaced template argument list' to a
+ string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
+ node. */
+ if (!replace_typedefs_template (info, buf,
+ d_left (comp), d_left (ret_comp),
+ finder, data))
+ return;
+
+ buf.clear ();
+ d_right (ret_comp) = d_right (comp);
+ comp = ret_comp;
+
+ /* Fallback to DEMANGLE_COMPONENT_NAME processing. We want
+ to call inspect_type for this template, in case we have a
+ template alias, like:
+ template<typename T> using alias = base<int, t>;
+ in which case we want inspect_type to do a replacement like:
+ alias<int> -> base<int, int>
+ */
+ }
+
if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
{
struct demangle_component newobj;
@@ -370,11 +429,20 @@ replace_typedefs_qualified_name (struct demangle_parse_info *info,
comp = d_right (comp);
}
- /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
- name assembled above and append the name given by COMP. Then use this
- reassembled name to check for a typedef. */
+ /* If the next component is DEMANGLE_COMPONENT_TEMPLATE or
+ DEMANGLE_COMPONENT_NAME, save the qualified name assembled above
+ and append the name given by COMP. Then use this reassembled
+ name to check for a typedef. */
- if (comp->type == DEMANGLE_COMPONENT_NAME)
+ if (comp->type == DEMANGLE_COMPONENT_TEMPLATE)
+ {
+ /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node with a
+ DEMANGLE_COMPONENT_NAME node containing the whole name. */
+ if (!replace_typedefs_template (info, buf, comp, ret_comp, finder, data))
+ return;
+ inspect_type (info, ret_comp, finder, data);
+ }
+ else if (comp->type == DEMANGLE_COMPONENT_NAME)
{
buf.write (comp->u.s_name.s, comp->u.s_name.len);