Search code examples
multithreadingdebuggingkernellldbxnu

Show backtrace for remote threads in kernel space


When debugging in kernel space, I sometimes wish to search for thread according to its backtrace frames among a group of threads, like all the threads on a specific task.

For example, getting kernel_task id

(lldb) showalltasks
task                 vm_map              ...command             
0xffffff800d828550   0xffffff800a1038d8  ...kernel_task         

Dumping all threads belonged to kernel_task

(lldb) showtaskthreads 0xffffff800d828550
task                 vm_map               ipc_space            #acts flags    pid       process             io_policy  wq_state  command
0xffffff800d828550   0xffffff800a1038d8   0xffffff800d5d17c0     140            0   0xffffff8007abb460                -1 -1 -1    kernel_task         
thread                   thread_id  processor            base   pri    sched_mode      io_policy       state    ast          waitq                            wait_event           wmesg                thread_name         
    0xffffff8007acf098       0x65       0xffffff8007a8a7b8   92     92     fixed bound                     WU       L            0xffffff804119e550               0xffffff8007a87a30 <vm_page_free_wanted>                                          
    0xffffff800d83f4c0       0x66       0xffffff8007a8a7b8   0      0      fixed bound                     RI       L                                                                                                           
    0xffffff800d83f958       0x67       0xffffff8041ad6000   95     95     fixed                           WU       L            0xffffff804119c240               0xffffff8007303840 <sched_timeshare_maintenance_continue>                      sched_maintenance_thread
    0xffffff800d83fdf0       0x68       0xffffff8041ad6000   80     80     fixed                           WU       L            0xffffff804119e850               0xffffff8007acf9f0                                            
    0xffffff800d83f028       0x69       0xffffff8007a8a7b8   93     93     fixed                           WU                    0xffffff804119e5e0               0xffffff8007acfa08                                            

Now I can see thread id's and lots of other information about the threads, but how can I observe the threads' backtrace ?


Solution

  • For whatever reason, the xnu kernel debugging macros use both "thread" and "activation" (abbreviated "act") terminology when talking about threads. With this information, you'll quickly find:

    showactstack <activation>

    Where <activation> is the thread address (pointer value, not ID), so e.g. showactstack 0xffffff8007acf098.

    Note also the following helpful commands:

    showtaskstacks <task address>
    showtaskstacks -F <taskname>
    

    These show all the stacks corresponding to a task/process.

    showallstacks
    

    This prints the kernel stacks for all threads in the system. Be warned: this one can take a while to complete. (IIRC it's faster over Firewire than ethernet kdp, but can still take minutes.)