bpf: Set long_bit and ptr_bit

Message ID 1793602812842387278.27500@kylinos.cn
State New
Headers
Series bpf: Set long_bit and ptr_bit |

Checks

Context Check Description
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_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

YuPeng Nov. 2, 2026, 7 a.m. UTC
  bpf_gdbarch_init sets no bit-size hooks, so long_bit and ptr_bit fall
back to their 32-bit defaults.  BPF is a 64-bit ISA: as a consequence,
bpf_register_type returns the 4-byte builtin_data_ptr and
builtin_func_ptr types for r10 and pc, while frame unwinders access
those registers as 8-byte values, and the first raw_read of r10 after
a backtrace trips:

  raw_read: Assertion `dst.size () == m_descr->sizeof_register[regnum]' failed.

Fix this by setting long_bit and ptr_bit to 64.  addr_bit defaults to
ptr_bit, and the short_bit, int_bit and long_long_bit defaults are
already correct for BPF, so no other hooks need to be set.

The mismatch is observable without a BPF target:

  $ gdb -batch -ex "set architecture bpf" -ex "maintenance print registers"

reports Size 4 for r10 and pc before the fix, and Size 8 after it.

The root cause analysis and the original version of this fix are from
the bug report, filed by choury.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34435
Co-Authored-By: choury <chouryzh@gmail.com>
---

The change was verified on master (commit b3874756), and gdb/bpf-tdep.c
is still identical on current master (commit 19c3a4c4): the patch
applies cleanly with git am, and gdb was rebuilt before and after it.
Before the fix, "set architecture bpf" followed by "maintenance print
registers" reports:

  r10        10   10   80       4     *1
  pc         11   11   84       4     *1

After the fix, r10 and pc report Size 8:

  r10        10   10   80       8     *1
  pc         11   11   88       8     *1

This is a small change (2 lines of code plus comment), so I believe
no FSF copyright assignment is needed.

gdb/bpf-tdep.c | 9 +++++++++
 1 file changed, 9 insertions(+)
  

Comments

Tom Tromey Nov. 5, 2026, 7:21 p.m. UTC | #1
>>>>> YuPeng  <yupeng@kylinos.cn> writes:

> Fix this by setting long_bit and ptr_bit to 64.  addr_bit defaults to
> ptr_bit, and the short_bit, int_bit and long_long_bit defaults are
> already correct for BPF, so no other hooks need to be set.

Thanks for the patch.  FWIW this looks good to me.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/bpf-tdep.c b/gdb/bpf-tdep.c
index 5ae9fc6..d059046 100644
--- a/gdb/bpf-tdep.c
+++ b/gdb/bpf-tdep.c
@@ -323,6 +323,15 @@  bpf_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   gdbarch *gdbarch
     = gdbarch_alloc (&info, gdbarch_tdep_up (new bpf_gdbarch_tdep));
 
+  /* Bit sizes of the fundamental types.  BPF is a 64-bit ISA; with the
+     32-bit defaults, bpf_register_type would return 4-byte pointer
+     types for r10 and pc, while the frame unwinders access them as
+     8-byte registers.  addr_bit defaults to ptr_bit, and the defaults
+     for short_bit, int_bit and long_long_bit (16, 32 and 64) are
+     already correct for BPF, so only these two need to be set.  */
+  set_gdbarch_long_bit (gdbarch, 64);
+  set_gdbarch_ptr_bit (gdbarch, 64);
+
   /* Information about registers, etc.  */
   set_gdbarch_num_regs (gdbarch, BPF_NUM_REGS);
   set_gdbarch_register_name (gdbarch, bpf_register_name);