]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Demangle function names when disassembling
authorAndrew Burgess <andrew.burgess@embecosm.com>
Tue, 28 Jul 2020 17:48:15 +0000 (11:48 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 28 Jul 2020 17:48:15 +0000 (11:48 -0600)
Andrew Burgess pointed out a regression, which he described in
PR symtab/26270:

================
After commit:

  commit bcfe6157ca288efed127c5efe21ad7924e0d98cf (refs/bisect/bad)
  Date:   Fri Apr 24 15:35:01 2020 -0600

      Use the linkage name if it exists

The disassembler no longer demangles function names in its output.  So
we see things like this:

  (gdb) disassemble tree_insert
  Dump of assembler code for function _Z11tree_insertP4nodei:
    ....

Instead of this:

  (gdb) disassemble tree_insert
  Dump of assembler code for function tree_insert(node*, int):
    ....

This is because find_pc_partial_function now returns the linkage name
rather than the demangled name.
================

This patch fixes the problem by introducing a new "overload" of
find_pc_partial_function, which returns the general_symbol_info rather
than simply the name.  This lets the disassemble command choose which
name to show.

Regression tested on x86-64 Fedora 32.

gdb/ChangeLog
2020-07-28  Tom Tromey  <tromey@adacore.com>

PR symtab/26270:
* symtab.h (find_pc_partial_function_sym): Declare.
* cli/cli-cmds.c (disassemble_command): Use
find_pc_partial_function_sym.  Check asm_demangle.
* blockframe.c (cache_pc_function_sym): New global.
(cache_pc_function_name): Remove.
(clear_pc_function_cache): Update.
(find_pc_partial_function_sym): New function, from
find_pc_partial_function.
(find_pc_partial_function): Rewrite using
find_pc_partial_function_sym.

gdb/testsuite/ChangeLog
2020-07-28  Andrew Burgess  <andrew.burgess@embecosm.com>

PR symtab/26270:
* gdb.cp/disasm-func-name.cc: New file.
* gdb.cp/disasm-func-name.exp: New file.

gdb/ChangeLog
gdb/blockframe.c
gdb/cli/cli-cmds.c
gdb/symtab.h
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.cp/disasm-func-name.cc [new file with mode: 0644]
gdb/testsuite/gdb.cp/disasm-func-name.exp [new file with mode: 0644]

index aaa2a4edfa9d60e4eb3f209ee7769437162863a4..9a9161561fdeeeea46022eb73209e05a21011582 100644 (file)
@@ -1,3 +1,17 @@
+2020-07-28  Tom Tromey  <tromey@adacore.com>
+
+       PR symtab/26270:
+       * symtab.h (find_pc_partial_function_sym): Declare.
+       * cli/cli-cmds.c (disassemble_command): Use
+       find_pc_partial_function_sym.  Check asm_demangle.
+       * blockframe.c (cache_pc_function_sym): New global.
+       (cache_pc_function_name): Remove.
+       (clear_pc_function_cache): Update.
+       (find_pc_partial_function_sym): New function, from
+       find_pc_partial_function.
+       (find_pc_partial_function): Rewrite using
+       find_pc_partial_function_sym.
+
 2020-07-28  Tom Tromey  <tromey@adacore.com>
 
        * cli/cli-cmds.c (_initialize_cli_cmds): Rearrange "disassemble"
index 05c26bc2c2a208cbd97f4d7e141515705d790735..80b769514ebb1d1b5c1fbe02c6e07d1ede6e9ea5 100644 (file)
@@ -191,7 +191,7 @@ find_pc_sect_containing_function (CORE_ADDR pc, struct obj_section *section)
 
 static CORE_ADDR cache_pc_function_low = 0;
 static CORE_ADDR cache_pc_function_high = 0;
-static const char *cache_pc_function_name = 0;
+static const general_symbol_info *cache_pc_function_sym = nullptr;
 static struct obj_section *cache_pc_function_section = NULL;
 static const struct block *cache_pc_function_block = nullptr;
 
@@ -202,7 +202,7 @@ clear_pc_function_cache (void)
 {
   cache_pc_function_low = 0;
   cache_pc_function_high = 0;
-  cache_pc_function_name = (char *) 0;
+  cache_pc_function_sym = nullptr;
   cache_pc_function_section = NULL;
   cache_pc_function_block = nullptr;
 }
@@ -210,8 +210,10 @@ clear_pc_function_cache (void)
 /* See symtab.h.  */
 
 bool
-find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
-                         CORE_ADDR *endaddr, const struct block **block)
+find_pc_partial_function_sym (CORE_ADDR pc,
+                             const struct general_symbol_info **sym,
+                             CORE_ADDR *address, CORE_ADDR *endaddr,
+                             const struct block **block)
 {
   struct obj_section *section;
   struct symbol *f;
@@ -257,7 +259,7 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
        {
          const struct block *b = SYMBOL_BLOCK_VALUE (f);
 
-         cache_pc_function_name = f->linkage_name ();
+         cache_pc_function_sym = f;
          cache_pc_function_section = section;
          cache_pc_function_block = b;
 
@@ -313,8 +315,8 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
   if (msymbol.minsym == NULL)
     {
       /* No available symbol.  */
-      if (name != NULL)
-       *name = 0;
+      if (sym != nullptr)
+       *sym = 0;
       if (address != NULL)
        *address = 0;
       if (endaddr != NULL)
@@ -325,7 +327,7 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
     }
 
   cache_pc_function_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
-  cache_pc_function_name = msymbol.minsym->linkage_name ();
+  cache_pc_function_sym = msymbol.minsym;
   cache_pc_function_section = section;
   cache_pc_function_high = minimal_symbol_upper_bound (msymbol);
   cache_pc_function_block = nullptr;
@@ -340,8 +342,8 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
        *address = cache_pc_function_low;
     }
 
-  if (name)
-    *name = cache_pc_function_name;
+  if (sym != nullptr)
+    *sym = cache_pc_function_sym;
 
   if (endaddr)
     {
@@ -365,6 +367,20 @@ find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
   return true;
 }
 
+/* See symtab.h.  */
+
+bool
+find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
+                         CORE_ADDR *endaddr, const struct block **block)
+{
+  const general_symbol_info *gsi;
+  bool r = find_pc_partial_function_sym (pc, &gsi, address, endaddr, block);
+  if (name != nullptr)
+    *name = r ? gsi->linkage_name () : nullptr;
+  return r;
+}
+
+
 /* See symtab.h.  */
 
 bool
index 503128b6e914da4b84b1aed71f744e8976e3965f..e3965fea0766fcd0ac9d4d714e2ff03a90488bd8 100644 (file)
@@ -1482,6 +1482,7 @@ disassemble_command (const char *arg, int from_tty)
 {
   struct gdbarch *gdbarch = get_current_arch ();
   CORE_ADDR low, high;
+  const general_symbol_info *symbol = nullptr;
   const char *name;
   CORE_ADDR pc;
   gdb_disassembly_flags flags;
@@ -1537,8 +1538,14 @@ disassemble_command (const char *arg, int from_tty)
   if (p[0] == '\0')
     {
       /* One argument.  */
-      if (find_pc_partial_function (pc, &name, &low, &high, &block) == 0)
+      if (!find_pc_partial_function_sym (pc, &symbol, &low, &high, &block))
        error (_("No function contains specified address."));
+
+      if (asm_demangle)
+       name = symbol->print_name ();
+      else
+       name = symbol->linkage_name ();
+
 #if defined(TUI)
       /* NOTE: cagney/2003-02-13 The `tui_active' was previously
         `tui_version'.  */
index 0b186554ea154d220f4d615b4b29c32066634217..026ffcaa0166e7bfe912e8010e1b28fa6503a6c5 100644 (file)
@@ -1769,6 +1769,14 @@ extern bool find_pc_partial_function (CORE_ADDR pc, const char **name,
                                      CORE_ADDR *address, CORE_ADDR *endaddr,
                                      const struct block **block = nullptr);
 
+/* Like find_pc_partial_function, above, but returns the underlying
+   general_symbol_info (rather than the name) as an out parameter.  */
+
+extern bool find_pc_partial_function_sym
+  (CORE_ADDR pc, const general_symbol_info **sym,
+   CORE_ADDR *address, CORE_ADDR *endaddr,
+   const struct block **block = nullptr);
+
 /* Like find_pc_partial_function, above, but *ADDRESS and *ENDADDR are
    set to start and end addresses of the range containing the entry pc.
 
index 40283fa532643e45b97996445bbc3e09fe950a8e..861438017e02395c4327ae18fecf0f3e410749d8 100644 (file)
@@ -1,3 +1,9 @@
+2020-07-28  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       PR symtab/26270:
+       * gdb.cp/disasm-func-name.cc: New file.
+       * gdb.cp/disasm-func-name.exp: New file.
+
 2020-07-28  Tom Tromey  <tromey@adacore.com>
 
        * gdb.dwarf2/varval.exp (setup_exec): Add 'or' instruction to
diff --git a/gdb/testsuite/gdb.cp/disasm-func-name.cc b/gdb/testsuite/gdb.cp/disasm-func-name.cc
new file mode 100644 (file)
index 0000000..428baf9
--- /dev/null
@@ -0,0 +1,48 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   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/>.  */
+
+class A
+{
+private:
+  int m_i;
+
+public:
+  A(int i);
+
+  int get_i () const
+  { return m_i; }
+
+  void set_i (int i)
+  { m_i = i; }
+};
+
+A::A(int i)
+  : m_i (i)
+{ /* Nothing.  */ }
+
+void process (A *obj, int num)
+{
+  obj->set_i (obj->get_i () + num);
+}
+
+int
+main (void)
+{
+  A a(42);
+  process (&a, 2);
+  return a.get_i ();
+}
diff --git a/gdb/testsuite/gdb.cp/disasm-func-name.exp b/gdb/testsuite/gdb.cp/disasm-func-name.exp
new file mode 100644 (file)
index 0000000..3fb63c8
--- /dev/null
@@ -0,0 +1,50 @@
+# Copyright 2020 Free Software Foundation, Inc.
+
+# 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/>.
+
+# This file is part of the gdb testsuite
+
+# Test that the disassembler correctly demangles C++ function names in
+# it's header line.
+
+if {[skip_cplus_tests]} { continue }
+
+standard_testfile .cc
+
+if [get_compiler_info "c++"] {
+    return -1
+}
+
+if {[prepare_for_testing "failed to prepare" $testfile \
+        $srcfile {debug c++}]} {
+    return -1
+}
+
+if ![runto_main] then {
+    perror "couldn't run to breakpoint"
+    continue
+}
+
+proc check_disassembly_header { request expected } {
+    gdb_test "disassemble ${request}" \
+       "Dump of assembler code for function ${expected}:\r\n.*"
+}
+
+gdb_test_no_output "set print asm-demangle on"
+
+check_disassembly_header "main" "main\\(\\)"
+check_disassembly_header "process" "process\\(A\\*, int\\)"
+check_disassembly_header "A::A" "A::A\\(int\\)"
+check_disassembly_header "A::get_i" "A::get_i\\(\\) const"
+check_disassembly_header "A::set_i" "A::set_i\\(int\\)"