[PATCH] Demangle function names when disassembling
Andrew Burgess
andrew.burgess@embecosm.com
Thu Jul 23 16:22:32 GMT 2020
* Tom Tromey <tromey@adacore.com> [2020-07-23 10:10:43 -0600]:
> From: Andrew Burgess <andrew.burgess@embecosm.com>
>
> 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.
LGTM.
Thanks for fixing this one.
Andrew
>
> gdb/ChangeLog
> 2020-07-23 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-23 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 | 14 +++++++
> gdb/blockframe.c | 36 +++++++++++-----
> gdb/cli/cli-cmds.c | 9 +++-
> gdb/symtab.h | 8 ++++
> gdb/testsuite/ChangeLog | 6 +++
> gdb/testsuite/gdb.cp/disasm-func-name.cc | 48 ++++++++++++++++++++++
> gdb/testsuite/gdb.cp/disasm-func-name.exp | 50 +++++++++++++++++++++++
> 7 files changed, 160 insertions(+), 11 deletions(-)
> create mode 100644 gdb/testsuite/gdb.cp/disasm-func-name.cc
> create mode 100644 gdb/testsuite/gdb.cp/disasm-func-name.exp
>
> diff --git a/gdb/blockframe.c b/gdb/blockframe.c
> index 05c26bc2c2a..80b769514eb 100644
> --- a/gdb/blockframe.c
> +++ b/gdb/blockframe.c
> @@ -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
> diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
> index 14718d1181a..07b119038d2 100644
> --- a/gdb/cli/cli-cmds.c
> +++ b/gdb/cli/cli-cmds.c
> @@ -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'. */
> diff --git a/gdb/symtab.h b/gdb/symtab.h
> index 0b186554ea1..026ffcaa016 100644
> --- a/gdb/symtab.h
> +++ b/gdb/symtab.h
> @@ -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.
>
> diff --git a/gdb/testsuite/gdb.cp/disasm-func-name.cc b/gdb/testsuite/gdb.cp/disasm-func-name.cc
> new file mode 100644
> index 00000000000..428baf98964
> --- /dev/null
> +++ b/gdb/testsuite/gdb.cp/disasm-func-name.cc
> @@ -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
> index 00000000000..3fb63c89a28
> --- /dev/null
> +++ b/gdb/testsuite/gdb.cp/disasm-func-name.exp
> @@ -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\\)"
> --
> 2.26.2
>
More information about the Gdb-patches
mailing list