From patchwork Mon Jun 13 11:34:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 1642769 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Cb1aO/Ln; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=sipsolutions.net header.i=@sipsolutions.net header.a=rsa-sha256 header.s=mail header.b=GeMSjMr/; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LM8dk3WzTz9s75 for ; Mon, 13 Jun 2022 21:35:54 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=yyz3dT6VBzqEGu9Il9V6QA6++DS/WECNZui8LX8+IyU=; b=Cb1aO/LnR+TS+B ws7FMYUgwSOhC5X0O+ZU+xCKtHpCm6V3GPKunXPXMNiGUke1ydteGeOg/3MHTsAxa7AIj/wjL2ACT vrUAkjp53GrrGAmgxJBrwRB/K+b7p3MhNu7JAhee3nMmBtuOlFEne2UDhmF0TrXBw2r/aJCD/G+Pk iLczs9Rkp6A4DwRfdQh0NVjHXAsjaSCDOqx1jDl5E+qWEUqGbqcjmO+CVM9lG8cul+FXqDABAJWTI e5w7+2p+S3V69MbYxoJzYEghxedoyrUD65PFYmsYQ+NX6EeHcSkx2kChg4ZmvU7k4VeNGOSSCeoId q10VZbYX8FEyOmig1x4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0iLV-003H91-Ek; Mon, 13 Jun 2022 11:35:01 +0000 Received: from s3.sipsolutions.net ([2a01:4f8:191:4433::2] helo=sipsolutions.net) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0iLL-003H1b-OD for hostap@lists.infradead.org; Mon, 13 Jun 2022 11:34:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Content-Type:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-To:Resent-Cc: Resent-Message-ID:In-Reply-To:References; bh=ZQDZ1E7d1EBU+7kefrSPpBi8BKSf8ZmP9tbHg210HUQ=; t=1655120091; x=1656329691; b=GeMSjMr/OCTWViCLD76grbcyR2NM+EeljIPPzajZ3fBYnt8P6hkjrttpqS6DK8cJGXVQXx72Vm0 zuClzVMXEpWGrTZpq//eUzwEbdxwZrVb/8pMVWQ60onoEfNwRJMH575motuD2ZCWFmZSwLZ1sM7TP td/Wm99O4p8F+x9u9CXvNMi4l9naXB9q4lc51HVcv8mcrd44kdXdcgiZLdSaI2NbRZ9qaCm6AY6O2 Q31lWfr72aADF1/TeOuby6ZgXB/U6jUwoPDJb8B7h7QeQPShxS+ahwq/FtcZLpj/P+xv/j0gLeO3g iF626frGqkHfMA6OHijaha+UnJDltocy8eOA==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.95) (envelope-from ) id 1o0iLJ-004UIk-Uv; Mon, 13 Jun 2022 13:34:50 +0200 From: Johannes Berg To: hostap@lists.infradead.org Cc: Johannes Berg Subject: [PATCH v2] tests: hwsim: vm-run: add mode for running under gdb Date: Mon, 13 Jun 2022 13:34:47 +0200 Message-Id: <20220613133447.ace3da88d425.Ic1fccb979199d4f33e048e9361dd4231e421d4ab@changeid> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220613_043451_871960_68E2FD3B X-CRM114-Status: GOOD ( 20.98 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Johannes Berg The new --gdb option can be used when KERNELDIR (and optionally MODULEDIR) are set and we therefore run UML. It runs the entire VM under the debugger, with a script to load the right modules into gdb [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Johannes Berg The new --gdb option can be used when KERNELDIR (and optionally MODULEDIR) are set and we therefore run UML. It runs the entire VM under the debugger, with a script to load the right modules into gdb so you can debug easily. Signed-off-by: Johannes Berg --- tests/hwsim/vm/linux.gdb | 68 ++++++++++++++++++++++++++++++++++++++++ tests/hwsim/vm/vm-run.sh | 19 ++++++++--- 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 tests/hwsim/vm/linux.gdb diff --git a/tests/hwsim/vm/linux.gdb b/tests/hwsim/vm/linux.gdb new file mode 100644 index 000000000000..539d73e5659d --- /dev/null +++ b/tests/hwsim/vm/linux.gdb @@ -0,0 +1,68 @@ +python +import os, subprocess +kdir = os.environ['KERNELDIR'] +mdir = os.environ['MODULEDIR'] or '/lib/modules' +gdb.execute(f'add-auto-load-safe-path {kdir}/scripts/gdb/') +cwd=os.getcwd() +gdb.execute(f'cd {kdir}') +gdb.execute(f'source {kdir}/vmlinux-gdb.py') +p = subprocess.run([f'./linux', '--version'], capture_output=True) +ver = p.stdout.strip().decode('ascii') +gdb.execute(f'cd {cwd}') +end +break os_early_checks +commands +silent +python +gdb.execute(f'cd {kdir}') +gdb.execute(f'lx-symbols {mdir}/{ver}/') +gdb.execute(f'cd {cwd}') +end +# only once +del 1 +continue +end +handle 11 nostop noprint pass +# +# So ... this is complicated. When gdb installs a regular breakpoint +# on some place, it writes there a breakpoint instruction (which is +# a single 0xCC byte on x86). This breaks out into the debugger and +# it can then restart/simulate the correct instruction when continuing +# across the breakpoint. +# +# Additionally, gdb (correctly) removes these breakpoint instructions +# from forked children when detaching from them. This also seems fine. +# +# However, due to how user-mode-linux works, this causes issues with +# kernel modules. These are loaded into the vmalloc area, and even if +# that isn't quite part of physmem, it's still mapped as MAP_SHARED. +# +# Unfortunately, this means that gdb deletes breakpoints in modules +# when a new userspace process is started, since that causes a new +# process to be created by clone() and gdb has to detach from it. +# +# The other thing to know is that when gdb hits a breakpoint it will +# restore all the code to normal, and reinstall breakpoints when we +# continue. +# +# Thus we can use that behaviour to work around the module issue: +# simply put a breakpoint on init_new_ldt which happens just after +# the clone() for a new userspace process, and do nothing there but +# continue, which reinstalls all breakpoints, including the ones in +# modules. +# +break init_new_ldt +commands +silent +continue +end + +echo \n +echo Welcome to hwsim kernel debugging\n +echo ---------------------------------\n\n +echo You can install breakpoints in modules, they're treated\n +echo as shared libraries, so just say 'y' if asked to make the\n +echo breakpoint pending on future load.\n\n +echo Do NOT, however, delete the breakpoint on 'init_new_ldt'!\n\n +echo Now enter 'run' to start the run.\n\n +echo Have fun!\n\n diff --git a/tests/hwsim/vm/vm-run.sh b/tests/hwsim/vm/vm-run.sh index defea43e8723..0978581b47f2 100755 --- a/tests/hwsim/vm/vm-run.sh +++ b/tests/hwsim/vm/vm-run.sh @@ -62,6 +62,7 @@ TIMESTAMP=$(date +%s) DATE=$TIMESTAMP CODECOV=no TIMEWARP=0 +GDB=0 TELNET_QEMU= TELNET_ARG=0 CODECOV_DIR= @@ -85,6 +86,9 @@ while [ "$1" != "" ]; do --timewrap ) shift TIMEWARP=1 ;; + --gdb ) shift + GDB=1 + ;; --telnet ) shift TELNET_ARG=1 TELNET_QEMU="-net nic,model=virtio -net user,id=telnet,restrict=on,net=172.16.0.0/24,hostfwd=tcp:127.0.0.1:$1-:23" @@ -162,17 +166,22 @@ fi A+="ro" if [ -z $KVM ]; then - $KERNEL \ - mem=${MEMORY}M \ + UML_ARGS="mem=${MEMORY}M \ LOGDIR=$LOGDIR \ time-travel=inf-cpu \ $A \ root=none hostfs=/ rootfstype=hostfs rootflags=/ \ ssl0=fd:0,fd:1 \ ssl1=fd:100 \ - ssl-non-raw \ - 100<>$LOGDIR/console 2>&1 | \ - sed -u '0,/VM has started up/d' + ssl-non-raw" + + if [ "$GDB" = "1" ] ; then + export KERNELDIR=$KERNELDIR + export MODULEDIR=$MODULEDIR + gdb -ex "source linux.gdb" --args $KERNEL $UML_ARGS 100<>$LOGDIR/console + else + $KERNEL $UML_ARGS 100<>$LOGDIR/console #2>&1 | sed -u '0,/VM has started up/d' + fi else $KVM \ -kernel $KERNEL \