gdb/disasm: fix demangling when disassembling the current function

Message ID 5f0154ae78e367cc9acfa6aa1041afa5f4441446.1731932552.git.aburgess@redhat.com
State New
Headers
Series gdb/disasm: fix demangling when disassembling the current function |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Andrew Burgess Nov. 18, 2024, 12:22 p.m. UTC
  When disassembling function symbols in C++ code, if GDB is asked to
disassemble a function by name then the function name in the header
line can be demangled by turning on `set print asm-demangle on`, e.g.:

  (gdb) disassemble foo_type::some_function
  Dump of assembler code for function _ZN8foo_type13some_functionE7my_type:
     0x0000000000401142 <+0>:	push   %rbp
     ... etc ...
  End of assembler dump.
  (gdb) set print asm-demangle on
  (gdb) disassemble foo_type::some_function
  Dump of assembler code for function foo_type::some_function(my_type):
     0x0000000000401142 <+0>:	push   %rbp
     ... etc ...                                                        │
  End of assembler dump.                                                │

However, if GDB is disassembling the current function, then this
demangling doesn't work, e.g.:

  (gdb) break foo_type::some_function
  Breakpoint 1 at 0x401152: file mangle.cc, line 16.
  (gdb) run
  Starting program: /tmp/mangle

  Breakpoint 1, foo_type::some_function (this=0x7fffffffa597, obj=...) at mangle.cc:16
  16	    obj.update ();
  (gdb) disassemble
  Dump of assembler code for function _ZN8foo_type13some_functionE7my_type:
     0x0000000000401142 <+0>:	push   %rbp
     ... etc ...
  End of assembler dump.
  (gdb) set print asm-demangle on
  (gdb) disassemble
  Dump of assembler code for function _ZN8foo_type13some_functionE7my_type:
     0x0000000000401142 <+0>:	push   %rbp
     ... etc ...
  End of assembler dump.

This commit fixes this issue, and extends gdb.cp/disasm-func-name.exp,
which was already testing the first case (disassemble by name) to also
cover disassembling the current function.
---
 gdb/cli/cli-cmds.c                        | 20 +++++++++++---------
 gdb/testsuite/gdb.cp/disasm-func-name.exp | 23 +++++++++++++++++++++++
 2 files changed, 34 insertions(+), 9 deletions(-)


base-commit: 82eff6743b77908a502b4cf9030acc93caf69e74
  

Comments

Tom Tromey Nov. 22, 2024, 3:33 p.m. UTC | #1
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> This commit fixes this issue, and extends gdb.cp/disasm-func-name.exp,
Andrew> which was already testing the first case (disassemble by name) to also
Andrew> cover disassembling the current function.

Thanks.  This looks good to me.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Andrew Burgess Nov. 22, 2024, 4:36 p.m. UTC | #2
Tom Tromey <tom@tromey.com> writes:

>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> This commit fixes this issue, and extends gdb.cp/disasm-func-name.exp,
> Andrew> which was already testing the first case (disassemble by name) to also
> Andrew> cover disassembling the current function.
>
> Thanks.  This looks good to me.
> Approved-By: Tom Tromey <tom@tromey.com>

Pushed.

Thanks,
Andrew
  

Patch

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 299064f5802..bc6cb5813f9 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1551,17 +1551,19 @@  print_disassembly (struct gdbarch *gdbarch, const char *name,
 static void
 disassemble_current_function (gdb_disassembly_flags flags)
 {
-  frame_info_ptr frame;
-  struct gdbarch *gdbarch;
-  CORE_ADDR low, high, pc;
-  const char *name;
-  const struct block *block;
+  frame_info_ptr frame = get_selected_frame (_("No frame selected."));
+  struct gdbarch *gdbarch = get_frame_arch (frame);
+  CORE_ADDR pc = get_frame_address_in_block (frame);
 
-  frame = get_selected_frame (_("No frame selected."));
-  gdbarch = get_frame_arch (frame);
-  pc = get_frame_address_in_block (frame);
-  if (find_pc_partial_function (pc, &name, &low, &high, &block) == 0)
+  const general_symbol_info *gsi;
+  const struct block *block;
+  CORE_ADDR low, high;
+  if (find_pc_partial_function_sym (pc, &gsi, &low, &high, &block) == 0)
     error (_("No function contains program counter for selected frame."));
+
+  gdb_assert (gsi != nullptr);
+  const char *name = asm_demangle ? gsi->print_name () : gsi->linkage_name ();
+
 #if defined(TUI)
   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
      `tui_version'.  */
diff --git a/gdb/testsuite/gdb.cp/disasm-func-name.exp b/gdb/testsuite/gdb.cp/disasm-func-name.exp
index 0d37fbd200a..0b5dd15115f 100644
--- a/gdb/testsuite/gdb.cp/disasm-func-name.exp
+++ b/gdb/testsuite/gdb.cp/disasm-func-name.exp
@@ -43,3 +43,26 @@  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\\)"
+
+# Place a breakpoint at STOP_LOC and then continue until a breakpoint
+# is hit (we assume this is the breakpoint we just created.
+#
+# Then disassemble the current function, EXPECT should appear in the
+# disassembly header as the name of the current function.
+proc continue_and_check { stop_loc expected } {
+    gdb_breakpoint $stop_loc
+    gdb_continue_to_breakpoint "continue to $stop_loc"
+
+    with_test_prefix "at $stop_loc" {
+	check_disassembly_header "" $expected
+    }
+}
+
+# Initially we are already in main.
+check_disassembly_header "" "main\\(\\)"
+
+# Now move forward and check the disassembly at various locations.
+continue_and_check "A::A" "A::A\\(int\\)"
+continue_and_check "process" "process\\(A\\*, int\\)"
+continue_and_check "A::set_i" "A::set_i\\(int\\)"
+continue_and_check "A::get_i" "A::get_i\\(\\) const"