Re: aarch64 devel/gdb for kgdb use on main [so: 15] (and, likely, 14.0-????): dump core.txt.?'s kgdb backtraces are messed up

From: Mark Millard <marklmi_at_yahoo.com>
Date: Wed, 13 Sep 2023 06:28:33 UTC

On Sep 12, 2023, at 22:11, Kyle Evans <kevans@FreeBSD.org> wrote:

> On 9/12/23 23:28, Mark Millard wrote:
>> [Trying to send to freebsd-ports accurately this time.]
>> On Sep 12, 2023, at 21:23, Mark Millard <marklmi@yahoo.com> wrote:
>> [I've cc'd the last 2 devel/gdb authors of kgdb-related material.]
>> kgdb 13.1_4 is an improvement over 13.1_3 for aarch64 but is still
>> broken. 13.1_3 example:
>> 0x0000000000000000 in ?? ()
>> (kgdb) #0  0x0000000000000000 in ?? ()
>> #1  <unavailable> in ?? ()
>> Backtrace stopped: not enough registers or memory available to unwind further
>> (kgdb)
>> 13.1_4 example:
>> get_curthread () at /usr/src/sys/arm64/include/pcpu.h:77
>> 77 __asm __volatile("ldr %0, [x18]" : "=&r"(td));
>> (kgdb) #0 get_curthread () at /usr/src/sys/arm64/include/pcpu.h:77
>> #1 doadump (textdump=0, textdump@entry=1576585744)
>> at /usr/src/sys/kern/kern_shutdown.c:405
>> #2 0xffff0000000ec18c in db_dump (dummy=<optimized out>, dummy2=<optimized out>, dummy3=<optimized out>, dummy4=<optimized out>)
>> at /usr/src/sys/ddb/db_command.c:591
>> #3 0xffff0000000ebf88 in db_command (last_cmdp=<optimized out>, cmd_table=<optimized out>, dopager=true)
>> at /usr/src/sys/ddb/db_command.c:504
>> #4 0xffff0000000ebc80 in db_command_loop ()
>> at /usr/src/sys/ddb/db_command.c:551
>> #5 0xffff0000000ef440 in db_trap (type=<optimized out>, code=<optimized out>)
>> at /usr/src/sys/ddb/db_main.c:268
>> #6 0xffff0000004b4860 in kdb_trap (type=60, code=0, tf=<optimized out>)
>> at /usr/src/sys/kern/subr_kdb.c:790
>> #7 <signal handler called>
>> #8 <signal handler called>
>> #9 <signal handler called>
>> #10 <signal handler called>
>> #11 <signal handler called>
>> #12 <signal handler called>
>> #13 <signal handler called>
>> #14 <signal handler called>
>> #15 <signal handler called>
>> #16 <signal handler called>
>> #17 <signal handler called>
>> #18 <signal handler called>
>> #19 <signal handler called>
>> #20 <signal handler called>
>> #21 <signal handler called>
>> #22 <signal handler called>
>> #23 <signal handler called>
>> Backtrace stopped: Cannot access memory at address 0x10
>> (kgdb)
> 
> Yeah, sorry, I see the problem now; looks like I didn't test it after one last change I ported from jhb's cheri branch:
> 
> >      tf_size = regcache_map_entry_size (trapframe_map);
> 
> regcache_map_entry_size() is in-fact what we want, but I didn't realize that it doesn't do the magical translation from 0 -> register_size that seems to be done everywhere else.  With the below patch[0] to populate all of the sizes, things look sane again.
> 
> Thanks,
> 
> Kyle Evans
> 
> [0] https://people.freebsd.org/~kevans/kgdb-fix.diff
> 
> @@ -126,13 +126,13 @@ aarch64_fbsd_supply_pcb(struct regcache *regcache, CORE_ADDR pcb_addr) 
> 
> 
> static const struct regcache_map_entry aarch64_fbsd_trapframe_map[] = 
>   { 
> -    { 1, AARCH64_SP_REGNUM, 0 },
> -    { 1, AARCH64_LR_REGNUM, 0 },
> -    { 1, AARCH64_PC_REGNUM, 0 },
> -    { 1, AARCH64_CPSR_REGNUM, 0 },
> +    { 1, AARCH64_SP_REGNUM, 8 },
> +    { 1, AARCH64_LR_REGNUM, 8 },
> +    { 1, AARCH64_PC_REGNUM, 8 },
> +    { 1, AARCH64_CPSR_REGNUM, 8 },
>     { 1, REGCACHE_MAP_SKIP, 8 },       /* esr */
>     { 1, REGCACHE_MAP_SKIP, 8 },       /* far */
> -    { 30, AARCH64_X0_REGNUM, 0 },      /* x0 ... x29 */
> +    { 30, AARCH64_X0_REGNUM, 8 },      /* x0 ... x29 */
>     { 0 },
>   };
> 
> @@ -141,12 +141,12 @@ static const struct regcache_map_entry aarch64_fbsd_trapframe_map[] =
> 
> static const struct regcache_map_entry aarch64_fbsd13_trapframe_map[] =
>   {
> -    { 1, AARCH64_SP_REGNUM, 0 },
> -    { 1, AARCH64_LR_REGNUM, 0 },
> -    { 1, AARCH64_PC_REGNUM, 0 },
> +    { 1, AARCH64_SP_REGNUM, 8 },
> +    { 1, AARCH64_LR_REGNUM, 8 },
> +    { 1, AARCH64_PC_REGNUM, 8 },
>     { 1, AARCH64_CPSR_REGNUM, 4 },
>     { 1, REGCACHE_MAP_SKIP, 4 },       /* esr */
> -    { 30, AARCH64_X0_REGNUM, 0 },      /* x0 ... x29 */
> +    { 30, AARCH64_X0_REGNUM, 8 },      /* x0 ... x29 */
>     { 0 },
>   };
> 

Thanks. For my personal build's non-debug kernel the
updated kgdb that I built and installed produced:

get_curthread () at /usr/main-src/sys/arm64/include/pcpu.h:77
77              __asm __volatile("ldr   %0, [x18]" : "=&r"(td));
(kgdb) #0  get_curthread () at /usr/main-src/sys/arm64/include/pcpu.h:77
#1  doadump (textdump=0, textdump@entry=1648929296)
    at /usr/main-src/sys/kern/kern_shutdown.c:405
#2  0xffff0000000f7704 in db_dump (dummy=<optimized out>,      dummy2=<optimized out>, dummy3=<optimized out>, dummy4=<optimized out>)
    at /usr/main-src/sys/ddb/db_command.c:591
#3  0xffff0000000f74e0 in db_command (last_cmdp=<optimized out>,      cmd_table=<optimized out>, dopager=true)
    at /usr/main-src/sys/ddb/db_command.c:504
#4  0xffff0000000f71b8 in db_command_loop ()
    at /usr/main-src/sys/ddb/db_command.c:551
#5  0xffff0000000fad9c in db_trap (type=<optimized out>, code=<optimized out>)
    at /usr/main-src/sys/ddb/db_main.c:268
#6  0xffff0000004f4ec4 in kdb_trap (type=60, code=0, tf=<optimized out>)
    at /usr/main-src/sys/kern/subr_kdb.c:790
#7  <signal handler called>
#8  kdb_enter (why=<optimized out>, msg=<optimized out>)
    at /usr/main-src/sys/kern/subr_kdb.c:556
#9  0xffff0000004a5350 in vpanic (
    fmt=0xffff00000099d39c "vm_fault failed: 0x%lx error %d", ap=...)
    at /usr/main-src/sys/kern/kern_shutdown.c:958
#10 0xffff0000004a5168 in panic (
    fmt=0x12 <error: Cannot access memory at address 0x12>)
    at /usr/main-src/sys/kern/kern_shutdown.c:894
#11 0xffff00000083c708 in data_abort (td=0xffff000157796260,      frame=0xffff00016248b360, esr=2516582404, far=<optimized out>, lower=0)
    at /usr/main-src/sys/arm64/arm64/trap.c:392
#12 <signal handler called>
#13 dump_sa (nw=nw@entry=0xffff00016248b7f0, attr=attr@entry=1,      sa=0x44572d4338374143) at /usr/main-src/sys/netlink/route/iface.c:226
#14 0xffff0000006b8fe0 in dump_iface (nw=0xffff00016248b7f0,      ifp=0xffffa00316119800, hdr=<optimized out>,      if_flags_mask=<optimized out>, if_flags_mask@entry=1648932336)
    at /usr/main-src/sys/netlink/route/iface.c:327
#15 0xffff0000006bb0bc in dump_cb (ifp=0x1, _arg=<optimized out>,      _arg@entry=0xffff00016248b690)
    at /usr/main-src/sys/netlink/route/iface.c:433
#16 0xffff0000005cf488 in if_foreach_sleep (match_cb=<optimized out>,      match_arg=match_arg@entry=0xffff00016248b6d0,      cb=0xffff0000006bb0a0 <dump_cb>, cb_arg=cb_arg@entry=0xffff00016248b690)
    at /usr/main-src/sys/net/if.c:4594
#17 0xffff0000006ba1c0 in rtnl_handle_getlink (hdr=0xffffa003198a6800,      nlp=0xffffa002ef454300, npt=<optimized out>)
    at /usr/main-src/sys/netlink/route/iface.c:503
#18 0xffff0000006b8390 in rtnl_handle_message (
    hdr=hdr@entry=0xffffa003198a6800, npt=npt@entry=0xffff00016248b830)
    at /usr/main-src/sys/netlink/netlink_route.c:104
#19 0xffff0000006b5818 in nl_receive_message (hdr=0xffffa003198a6800,      remaining_length=32, nlp=0xffffa002ef454300, npt=0xffff00016248b830)
    at /usr/main-src/sys/netlink/netlink_io.c:508
#20 nl_process_mbuf (m=<optimized out>, nlp=0xffffa002ef454300)
    at /usr/main-src/sys/netlink/netlink_io.c:582
#21 nl_process_received_one (nlp=0xffffa002ef454300)
    at /usr/main-src/sys/netlink/netlink_io.c:295
#22 nl_process_received (nlp=0xffffa002ef454300)
    at /usr/main-src/sys/netlink/netlink_io.c:322
#23 nl_taskqueue_handler (_arg=0xffffa002ef454300, pending=<optimized out>)
    at /usr/main-src/sys/netlink/netlink_io.c:373
#24 0xffff00000050c668 in taskqueue_run_locked (
    queue=queue@entry=0xffffa009ff851180)
    at /usr/main-src/sys/kern/subr_taskqueue.c:512
#25 0xffff00000050db70 in taskqueue_thread_loop (arg=<optimized out>,      arg@entry=0xffffa002ef454360)
    at /usr/main-src/sys/kern/subr_taskqueue.c:824
#26 0xffff00000045b7bc in fork_exit (
    callout=0xffff00000050daa0 <taskqueue_thread_loop>,      arg=0xffffa002ef454360, frame=0xffff00016248ba00)
    at /usr/main-src/sys/kern/kern_fork.c:1160
#27 <signal handler called>
(kgdb) 

(I do not strip symbols and such from non-debug builds.)

Much better.

===
Mark Millard
marklmi at yahoo.com