From nobody Mon Jan 15 21:38:29 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TDQVs3DgYz57cx9; Mon, 15 Jan 2024 21:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TDQVs2Zhwz3wZy; Mon, 15 Jan 2024 21:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705354709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=d2trC9m+zjkyhFeNCBzdqdqWt/strAq5/F9b4UNgAeo=; b=LNuuhcRqJSrMkx0rMsxE5i1BfgQlOENN5LCRKPBpWme8CwSFSL9Udw19Z9v9SIiKx72PsR kKotYbmZPKH9wcdEdp3GDRYJ7f2bTRNQUWeMbc0AOCjnFlrpjssNYBf4epBpn/AUo8+Vdc p92FYhsH2lsZWD/GXPJ8k/YsjQ5+/gS8wSPEY0ZYnhR4P2q2vNJlwm+jACD/ayR/zrXZyS ktLXFYAOnXG0BpVDgUfYAAXpAAjVGa6aL1o9vFTE7GenqdIiJceE8eemuzWw2K5U/3kU2Q /FQJI1d0aaf6snmbv2I9YjGbXdyirsc27K6PWlxsDHNWbiUoxQflwRlNt4R1AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705354709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=d2trC9m+zjkyhFeNCBzdqdqWt/strAq5/F9b4UNgAeo=; b=amNyUKPp5HFy9Aw9hX5LwL/8vQzsjzEMRRzDCdqIEIpE0vXZRaE5FALf4gvymSFGNISmOR x/QP8ERGBl+9E6rCOIMak3uws40F4IunP2gFMDqvpL3k7qwwgeVy+XK3nU12eTmcHtBak3 NRkO6y7TTwjGpXTjl4w8VNIc02rQZ55OPuHEnvCZzYpZQGKSeX8mAveA/4cxJmBj8zuRAR KYtkR6vbwEmnXmc9tonYr3W2y6yjKVw0xR1VCIw8gXry3Ev349Nyll1gl3fYO+WHUW37YO qO/q5Jjl0wcX8jQC0pl2MOJdzTdgRa8DZnNzft5q8A63UGIW3bynD3dyRbgaUA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705354709; a=rsa-sha256; cv=none; b=smFEhCHuzwhm5hg5xmQZjFJvukMP0ndexC3qTzjXt8Lo8kGc9RjUAlslEb3YgdyW7NIzvW noyDU8CDCWiN1d8WMRQmGMuiECRqRGFDdFhc0AvlHWB/zesg2Xbpbo/UUmobpyFKxEc5YL sWGOJrsGgkuESSP3LCZZAElIFeosoimjbrmgpUA/x/iJjhn+q4mLr64Fk3kHFM3EAfYP7q 41OsSOYnj1qvW6BKIZlannOfUgkwmN1wd8oQHxwpniqFNxBxPT3Kj/5Rcg+Icqwcjudm+z tXw4gt++yhfP6CFZ/9Qi1TrzbxjLLBAYB3aYC/jd8SePdJEldJfl02ihpDbxgQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TDQVs1gtdz1C9n; Mon, 15 Jan 2024 21:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40FLcTCU034449; Mon, 15 Jan 2024 21:38:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40FLcT8j034446; Mon, 15 Jan 2024 21:38:29 GMT (envelope-from git) Date: Mon, 15 Jan 2024 21:38:29 GMT Message-Id: <202401152138.40FLcT8j034446@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 2524b7dfb0df - main - crashinfo: Print stack traces for all on-CPU threads List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2524b7dfb0df7e37ea9651559ce6c027b006e2a9 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=2524b7dfb0df7e37ea9651559ce6c027b006e2a9 commit 2524b7dfb0df7e37ea9651559ce6c027b006e2a9 Author: Mark Johnston AuthorDate: 2024-01-15 20:39:26 +0000 Commit: Mark Johnston CommitDate: 2024-01-15 21:36:40 +0000 crashinfo: Print stack traces for all on-CPU threads Add a python script which implements the bulk of this functionality. Over time, this would ideally evolve into a library of python routines which can be used to inspect kernel data structures and automate some debugging tasks, similar to jhb's out-of-tree scripts, but written in a somewhat nicer language and with better integration into the kgdb command prompt. Note that kgdb currently won't auto-load scripts in this directory. This should perhaps change in the future. It probably also makes more sense to have a crashinfo.py which provides all the kgdb output that we want to include in core.txt, rather than having crashinfo.sh pipe in several commands. Reviewed by: avg, imp Discussed with: jhb MFC after: 3 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33817 --- etc/mtree/BSD.usr.dist | 2 ++ libexec/Makefile | 2 +- libexec/kgdb/Makefile | 5 ++++ libexec/kgdb/acttrace.py | 63 +++++++++++++++++++++++++++++++++++++++++ usr.sbin/crashinfo/crashinfo.sh | 8 ++++-- 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/etc/mtree/BSD.usr.dist b/etc/mtree/BSD.usr.dist index 83d8de034caf..a7738aaf6f78 100644 --- a/etc/mtree/BSD.usr.dist +++ b/etc/mtree/BSD.usr.dist @@ -161,6 +161,8 @@ .. hyperv .. + kgdb + .. lpr ru .. diff --git a/libexec/Makefile b/libexec/Makefile index 5f7f652df6ed..ee354fa60e79 100644 --- a/libexec/Makefile +++ b/libexec/Makefile @@ -1,4 +1,3 @@ - .include .include @@ -11,6 +10,7 @@ SUBDIR= ${_atf} \ flua \ getty \ ${_hyperv} \ + kgdb \ ${_mail.local} \ ${_makewhatis.local} \ ${_mknetid} \ diff --git a/libexec/kgdb/Makefile b/libexec/kgdb/Makefile new file mode 100644 index 000000000000..f6b255ab4f60 --- /dev/null +++ b/libexec/kgdb/Makefile @@ -0,0 +1,5 @@ +FILESDIR?= /usr/libexec/kgdb + +FILES= acttrace.py + +.include diff --git a/libexec/kgdb/acttrace.py b/libexec/kgdb/acttrace.py new file mode 100644 index 000000000000..3229ff708de1 --- /dev/null +++ b/libexec/kgdb/acttrace.py @@ -0,0 +1,63 @@ +#- +# Copyright (c) 2022 The FreeBSD Foundation +# +# This software was developed by Mark Johnston under sponsorship from the +# FreeBSD Foundation. +# + +import gdb + + +def symval(name): + return gdb.lookup_global_symbol(name).value() + + +def tid_to_gdb_thread(tid): + for thread in gdb.inferiors()[0].threads(): + if thread.ptid[2] == tid: + return thread + else: + return None + + +def all_pcpus(): + mp_maxid = symval("mp_maxid") + cpuid_to_pcpu = symval("cpuid_to_pcpu") + + cpu = 0 + while cpu <= mp_maxid: + pcpu = cpuid_to_pcpu[cpu] + if pcpu: + yield pcpu + cpu = cpu + 1 + + +class acttrace(gdb.Command): + def __init__(self): + super(acttrace, self).__init__("acttrace", gdb.COMMAND_USER) + + def invoke(self, arg, from_tty): + # Save the current thread so that we can switch back after. + curthread = gdb.selected_thread() + + for pcpu in all_pcpus(): + td = pcpu['pc_curthread'] + tid = td['td_tid'] + + gdb_thread = tid_to_gdb_thread(tid) + if gdb_thread is None: + print("failed to find GDB thread with TID {}".format(tid)) + else: + gdb_thread.switch() + + p = td['td_proc'] + print("Tracing command {} pid {} tid {} (CPU {})".format( + p['p_comm'], p['p_pid'], td['td_tid'], pcpu['pc_cpuid'])) + gdb.execute("bt") + print() + + curthread.switch() + + +# Registers the command with gdb, doesn't do anything. +acttrace() diff --git a/usr.sbin/crashinfo/crashinfo.sh b/usr.sbin/crashinfo/crashinfo.sh index 9a3d26020654..3bb1e1456462 100755 --- a/usr.sbin/crashinfo/crashinfo.sh +++ b/usr.sbin/crashinfo/crashinfo.sh @@ -215,13 +215,15 @@ echo sed -ne '/^ Panic String: /{s//panic: /;p;}' $INFO echo -# XXX: /bin/sh on 7.0+ is broken so we can't simply pipe the commands to -# kgdb via stdin and have to use a temporary file instead. file=`mktemp /tmp/crashinfo.XXXXXX` if [ $? -eq 0 ]; then + scriptdir=/usr/libexec/kgdb + echo "bt -full" >> $file + echo "source ${scriptdir}/acttrace.py" >> $file + echo "acttrace" >> $file echo "quit" >> $file - ${GDB%gdb}kgdb $KERNEL $VMCORE < $file + ${GDB%gdb}kgdb -q $KERNEL $VMCORE < $file rm -f $file echo fi