From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 2616F388CC13 for ; Thu, 23 Jul 2020 16:10:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2616F388CC13 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id EB9BD11688D; Thu, 23 Jul 2020 12:10:44 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 15fOgtlAtO03; Thu, 23 Jul 2020 12:10:44 -0400 (EDT) Received: from murgatroyd.Home (174-16-104-48.hlrn.qwest.net [174.16.104.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id A6A261167F7; Thu, 23 Jul 2020 12:10:44 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Subject: [PATCH] Demangle function names when disassembling Date: Thu, 23 Jul 2020 10:10:43 -0600 Message-Id: <20200723161043.2191184-1-tromey@adacore.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jul 2020 16:10:47 -0000 From: Andrew Burgess 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-23 Tom Tromey 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 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 . */ + +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 . + +# 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