xref: /freebsd/sys/tools/gdb/acttrace.py (revision ef7b81340b8e0ad06584f364aa02105c03ca5f7c)
1#
2# Copyright (c) 2022 The FreeBSD Foundation
3#
4# This software was developed by Mark Johnston under sponsorship from the
5# FreeBSD Foundation.
6#
7# SPDX-License-Identifier: BSD-2-Clause
8#
9
10import gdb
11from freebsd import *
12from pcpu import *
13
14class acttrace(gdb.Command):
15    """
16    Print the stack trace of all threads that were on-CPU at the time of
17    the panic.
18    """
19    def __init__(self):
20        super(acttrace, self).__init__("acttrace", gdb.COMMAND_USER)
21
22    def invoke(self, arg, from_tty):
23        # Save the current thread so that we can switch back after.
24        curthread = gdb.selected_thread()
25
26        for pcpu in pcpu_foreach():
27            td = pcpu['pc_curthread']
28            tid = td['td_tid']
29
30            gdb_thread = tid_to_gdb_thread(tid)
31            if gdb_thread is None:
32                raise gdb.error(f"failed to find GDB thread with TID {tid}")
33            else:
34                gdb_thread.switch()
35
36                p = td['td_proc']
37                print("Tracing command {} pid {} tid {} (CPU {})".format(
38                      p['p_comm'], p['p_pid'], td['td_tid'], pcpu['pc_cpuid']))
39                gdb.execute("bt")
40                print()
41
42        curthread.switch()
43
44
45# Registers the command with gdb, doesn't do anything.
46acttrace()
47