xref: /freebsd/sys/tools/gdb/acttrace.py (revision ea675a43f09ba569adf1dd17b4f1ced970e48de4)
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    Register an acttrace command with gdb.
17
18    When run, acttrace prints the stack trace of all threads that were on-CPU
19    at the time of the panic.
20    """
21    def __init__(self):
22        super(acttrace, self).__init__("acttrace", gdb.COMMAND_USER)
23
24    def invoke(self, arg, from_tty):
25        # Save the current thread so that we can switch back after.
26        curthread = gdb.selected_thread()
27
28        for pcpu in pcpu_foreach():
29            td = pcpu['pc_curthread']
30            tid = td['td_tid']
31
32            gdb_thread = tid_to_gdb_thread(tid)
33            if gdb_thread is None:
34                raise gdb.error(f"failed to find GDB thread with TID {tid}")
35            else:
36                gdb_thread.switch()
37
38                p = td['td_proc']
39                print("Tracing command {} pid {} tid {} (CPU {})".format(
40                      p['p_comm'], p['p_pid'], td['td_tid'], pcpu['pc_cpuid']))
41                gdb.execute("bt")
42                print()
43
44        curthread.switch()
45
46
47# Registers the command with gdb, doesn't do anything.
48acttrace()
49