Skip to content

Commit 75c3a4b

Browse files
committed
scripts/gdb/symbols: Fix BPF debug on older GDB
The GDB python script for BPF uses the recently added '.bytes' method [0], which is only available with GDB 14+ and otherwise leads to failures: (gdb) lx-symbols loading vmlinux scanning for modules in /home/kodidev/linux loading @0xbf008000: .../tools/testing/selftests/bpf/bpf_testmod.ko loading @0xbf000000: .../kernel/bpf/preload/bpf_preload.ko Traceback (most recent call last): File "/home/kodidev/linux/scripts/gdb/linux/symbols.py", line 321, in invoke self.bpf_ksym_monitor = bpf.KsymMonitor(self.add_bpf_ksym, File "/home/kodidev/linux/scripts/gdb/linux/bpf.py", line 67, in __init__ self.notify_initial() File "/home/kodidev/linux/scripts/gdb/linux/bpf.py", line 71, in notify_initial self.add(ksym) File "/home/kodidev/linux/scripts/gdb/linux/symbols.py", line 217, in add_bpf_ksym name = bpf.get_ksym_name(ksym) File "/home/kodidev/linux/scripts/gdb/linux/bpf.py", line 19, in get_ksym_name name = ksym["name"].bytes AttributeError: 'gdb.Value' object has no attribute 'bytes' Error occurred in Python: 'gdb.Value' object has no attribute 'bytes' Implement '.bytes' method equivalents to make wonderful BPF debugging work on less recent systems (e.g. GDB 12.1 on Ubuntu 22 LTS): (gdb) lx-symbols loading vmlinux scanning for modules in /home/kodidev/linux loading @0xbf008000: .../tools/testing/selftests/bpf/bpf_testmod.ko loading @0xbf000000: .../kernel/bpf/preload/bpf_preload.ko loading @0xbf00ea0c: bpf_prog_1e1c76c33a07abcc_dump_bpf_map loading @0xbf010630: bpf_prog_bee9db790ebbd5a6_dump_bpf_prog loading @0xbf01e70c: bpf_prog_24c071e52e49653a___tld_fetch_key loading @0xbf01c4c8: bpf_prog_89487142fd0ba34f_task_main (gdb) list bpf_prog_89487142fd0ba34f_task_main 30 struct tld_object tld_obj; 31 struct test_tld_struct *struct_p; 32 struct task_struct *task; 33 int err, *int_p; 34 35 task = bpf_get_current_task_btf(); 36 err = tld_object_init(task, &tld_obj); 37 if (err) 38 return 1; Link 0: https://sourceware.org/bugzilla/show_bug.cgi?id=13267 Fixes: XXXXXXXXXX ("scripts/gdb/symbols: make BPF debug info available to GDB") Signed-off-by: Tony Ambardar <[email protected]>
1 parent 252565d commit 75c3a4b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

scripts/gdb/linux/bpf.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def get_ksym_name(ksym):
19-
name = ksym["name"].bytes
19+
name = bytes(ksym["name"].string(), 'utf-8')
2020
end = name.find(b"\x00")
2121
if end != -1:
2222
name = name[:end]
@@ -210,9 +210,10 @@ def generate_debug_obj(ksym, prog):
210210
name = get_ksym_name(ksym)
211211
# Avoid read_memory(); it throws bogus gdb.MemoryError in some contexts.
212212
start = ksym["start"]
213+
length = int(ksym["end"]) - int(start)
213214
code = start.cast(gdb.lookup_type("unsigned char")
214-
.array(int(ksym["end"]) - int(start))
215-
.pointer()).dereference().bytes
215+
.array(length)
216+
.pointer()).dereference()
216217
linfo_iter = LInfoIter(prog)
217218

218219
result = tempfile.NamedTemporaryFile(suffix=".o", mode="wb")
@@ -227,7 +228,8 @@ def generate_debug_obj(ksym, prog):
227228
src.write(".globl {}\n".format(name))
228229
src.write(".type {},@function\n".format(name))
229230
src.write("{}:\n".format(name))
230-
for code_off, code_byte in enumerate(code):
231+
for code_off in range(length):
232+
code_byte = code[code_off]
231233
if linfo_iter.get_code_off() == code_off:
232234
fileno, file_name = linfo_iter.get_fileno()
233235
if file_name is not None:
@@ -237,8 +239,8 @@ def generate_debug_obj(ksym, prog):
237239
src.write(".loc {} {} {}\n".format(fileno, line, col))
238240
src.write("0:\n")
239241
linfo_iter.advance()
240-
src.write(".byte {}\n".format(code_byte))
241-
src.write(".size {},{}\n".format(name, len(code)))
242+
src.write(".byte {}\n".format(hex(code_byte)))
243+
src.write(".size {},{}\n".format(name, length))
242244
src.flush()
243245

244246
try:

0 commit comments

Comments
 (0)