From patchwork Mon Jun 13 11:22:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 1642767 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=Fs8xoljb; 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=JTYAIa98; 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 4LM8V41lcgz9s75 for ; Mon, 13 Jun 2022 21:29:16 +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=xs9+vcZYOuzIN5vrkS8YsU4r6VrQQEkfmu/8BKndNXQ=; b=Fs8xoljbJnQrLB EtDpS/G4eofGVOPefkEgvnOtmC3BKIhc22yZlIv2tNQyDiE25e5stcWk/Rz1URqNCuGhqGuzS2xPg WW8fXDzjldOpitpNBAkoQzazX8QHQvL5BBNmEmtWhQdCRr+b5BgnwK8S6jH7jXRY+TupVeKPf3CQ5 fjkbA0o6FZWfSGfhhKqq5ob8LeuR2w4IYCJftFVDRhrD76GHWjNrZPrFMIHjRbwqDV4xsa93dxiyl Zcuh/3eFTzHnLje1lPh//cwG/xRHdLLAs0XRux2R9hSd807BQwMseN0HVA7ctHKF68NFWtN7GZybt ipJuKcPgJK9dBIIKVXCA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0iEc-003Dvx-Qs; Mon, 13 Jun 2022 11:27:55 +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 1o0i9R-003BL2-M3 for hostap@lists.infradead.org; Mon, 13 Jun 2022 11:22:35 +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=GpJ8VNAMY/9uvqwtMCEtLL3t66935zve4JNg3xFfJsk=; t=1655119350; x=1656328950; b=JTYAIa98GyrORMERroGlmryqEt6MiWwv1ZiEem2v1NlftHoHGxLE/msb4nQZam50ydMhObZrfvy uHSzs+eZJYI2rA0u1tZofwN+Z6XjAXv0T1PGK6d/HzZx7SEEO3sfqAoBc1IcZZFdIyGtGAqTAq6oz 34wOCmJ8yKWBkAE0IgFl83r6u6UFOIMEeEt9Jgocr4HmoCgA6a8jcsBwh2gndKQBOGa+ls2Uoo30x 2LGGhpGKVfuo+g+nAZr5Zkw2PLAtoVxJ6Yx+HVfCsaHgLbJhAYFBHA6ioKLwW79mXzwuDy8FCQuz9 s74BP6lc6u5q8yEMyfFiNCxfdCyz3yhC/aEg==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.95) (envelope-from ) id 1o0i9K-004U5a-9c; Mon, 13 Jun 2022 13:22:26 +0200 From: Johannes Berg To: hostap@lists.infradead.org Cc: Johannes Berg Subject: [PATCH] tests: hwsim: vm-run: add mode for running under gdb Date: Mon, 13 Jun 2022 13:22:23 +0200 Message-Id: <20220613132223.c6525dc83f2e.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_042233_778132_271DBF38 X-CRM114-Status: GOOD ( 20.97 ) 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 | 20 +++++++++--- 2 files changed, 83 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..520edb38efc0 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,23 @@ fi A+="ro" if [ -z $KVM ]; then - $KERNEL \ - mem=${MEMORY}M \ + set -x + 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 \