From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gWeLAP6o82rdYAAAWB0awg (envelope-from ) for ; Tue, 10 Nov 2026 18:30:06 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=vrany.io header.i=@vrany.io header.a=rsa-sha256 header.s=protonmail header.b=Gquc4gDa; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id F04BF378002; Tue, 10 Nov 2026 18:30:05 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 823921E043 for ; Tue, 10 Nov 2026 18:30:04 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A1334BA23D6 for ; Tue, 10 Nov 2026 23:30:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A1334BA23D6 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=vrany.io header.i=@vrany.io header.a=rsa-sha256 header.s=protonmail header.b=Gquc4gDa Received: from mail-4317.protonmail.ch (mail-4317.protonmail.ch [185.70.43.17]) by sourceware.org (Postfix) with ESMTPS id 663B84BA2E21 for ; Tue, 10 Nov 2026 23:29:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 663B84BA2E21 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=vrany.io Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrany.io ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 663B84BA2E21 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=185.70.43.17 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794353375; cv=none; b=bB4+FmLCai0gKopb1+MPhCttpCyijU1rOAnqibYvRTH2hH+7+GHCeqlfH7D5MVYnikSmW7XFk2QskbKn1Rm5sPUvIVocrGts6rc4vojZTbUbaljVZLQjyEMtG1bfj6QhlB2uwBMyBAm9BnHaWEyRBWyk7sO/WsaFopx38s0vZdU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794353375; c=relaxed/simple; bh=1whzGSEOXZfXc38CxHQXqQ3P95CrANKPTJlWT94jhaA=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=S9qPJiDJybX6F/o5EQHxfzadLgEJH13uM68PkWt4yz6rBPSXVKp3HF42TcrRkRmACbzveUhUsNJsMZ2La3AcTnFmI96JrnzZEi6u07LVekcmf/7YZoVrItYVq47/tnTRlLxUjNcFZwdlhLesUENfU0ZcTUD1Ms0gZE1kwl3JDkI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=vrany.io header.i=@vrany.io header.a=rsa-sha256 header.s=protonmail header.b=Gquc4gDa DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 663B84BA2E21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrany.io; s=protonmail; t=1794353367; x=1794612567; bh=1hNmLBUz8jF7F+vC3XND37P0kq9GstchM1E3zdUOlHA=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=Gquc4gDaPlFj6JQs6mbcg5tETGecHd2clZshtIBM/TeT74Vr0LzoS4LWsqhBv/FGA 6FHLa+R0yjGU1fJrVKHDJAdQfTeNSLx/4T9TGXRzMLETr3U9FzA1JGxeqYHejBXgYM twWAuOK+5NYwKGDY3+VmX4QvB7L1T6QeCHdo60IE7jfKH4tMys+GINoifW7N9clH3R 2hhuSEMTsN2DI/hJ9UsFRfOouhmq5QUMG5kk5l+Sj2euWXUT4R3Dbl+nhW7VrubUWM 21xc0jecoZf9czHrrAjLTuS9Ji6jkqzUItG+N6ZABioqSpqlSzAE+onwekXngW17cz S6ZZqLoQlzNdw== Date: Tue, 10 Nov 2026 23:29:21 +0000 To: simark@simark.ca, gdb-patches@sourceware.org From: Jan Vrany Cc: Jan Vrany Subject: [PATCH v3] gdb/testsuite: add support for running tests using QEMU Message-ID: <20261110232643.2945321-1-jan@vrany.io> Feedback-ID: 40767693:user:proton X-Pm-Message-ID: 6ac035423db553ff07223c003c8d2bd9855e728f MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Below is a v3 of patch adding support for running tests using QEMU. The v1 submission is here: https://inbox.sourceware.org/gdb-patches/20261026100153.1789950-1-jan@vr= any.io/ The v2 submission is here: https://inbox.sourceware.org/gdb-patches/20261027135827.3868769-1-jan@vr= any.io/ Changes in v3: * This version *does not* use unix sockets. I tried but quickly run into a issues with limited path length and socket cleanups so I left it for later. * Updated the comment in qemu-user.exp and added example with QEMU_FLAGS= . * Fixed a number of TCL coding issues pointed out by Simon * If given --target is not yet supported by qemu-user board, do not make it hard error as long as both QEMU and CROSS are supported (if both are provided, --target value is effectively ignored). * Extracted qemu cleanup code to new function - qemu_reap - and use it. Since I decided to use it also in case QEMU terminates immediately, I dropped the warning about failing to close QEMU (since in that case, channel would be already closed). * Drop handling of CFLAGS_FOR_TARGET environment (when I tried, I did no= t realize I had to put it into RUNTESTFLAGS in orde to make it work with= out extra code). * Use get_portnum instead of generating port randomly. * Consistently prefix all log messages / warnings with "qemu-user:" to ease debugging. > I noticed that GDB reads the target libraries through the remote > protocol. Since the sysroot is on disk, we could point GDB to it, which > would make it faster. > > I added: > > set sysroot /home/smarchi/x-tools/armv7-rpi2-linux-gnueabihf/armv7-rpi2= -linux-gnueabihf/sysroot > > in my GDBFLAGS, and the time to run gdb.base/break.exp went from 209s to > 50s. > > Maybe we can have the board accept a SYSROOT variable, which it could > use to: > > 1. pass `-L $SYSROOT` to qemu > 2. add `set sysroot $SYSROOT` to GDBFLAGS > > If SYSROOT is not set, then we can query qemu for its default: > > $ qemu-aarch64 -h | grep ^QEMU_LD_PREFIX > QEMU_LD_PREFIX =3D /usr/gnemul/qemu-aarch64 > > And then we can pass that as a sysroot to GDB. It doesn't have to be in > the first version, I can work on that as an optimization after the board > is merged. That's a good idea. We can check QEMU_FLAGS and QEMU_LD_PREFIX rather than introducing yet another variable. But I'll leave it for later. > ... > > > + > > +=09verbose -log "Spawning qemu: $cmd" > > + > > +=09if {[catch { eval $cmd } err ]} { > > I could imagine all sorts of problems due to eval, like if inferior_args > contains some special characters that have a meaning in tcl. I would > write it directly here, even if that means duplication with the logging > message. > > spawn -noecho $qemu {*}$qemu_flags -g $portnum $file {*}$inferior_args > I changed that to do exactly the above but this requires splitting QEMU_FLA= GS according to shell quoting/escaping rules. As I could not find a TCL functi= on for that (like Python's shlex.split()) so in v3 I merely split it by spaces= . Thanks! Jan -- 8< -- gdb/testsuite: add support for running tests using QEMU While debugging failing RISC-V tests, I found myself once again in a situation where running tests easily using QEMU usermode emulation would be useful. Since this is something that resurfaces every now and then [1], I decided to have another stab at it. This commit adds a new "board" file - qemu-user.exp - that allows running tests using QEMU usermode emulation. The main goal is to make it easier to test changes on multiple different architectures. However using QEMU to run tests is no substitute for running tests on real hardware (or in a VM). See limitations below. The implementation based on simavr.exp and on an earlier patch by Zeck S [2] and somewhat simplified. Unlike previous attempts, test programs are no compiled statically by default in order to support broader set of testcases. A static compilation can be forced by setting CFLAGS_FOR_TARGET=3D-static. It also supports more targets (new targets should be easy to add). Not all test that pass on real hardware pass when run under usermode QEMU. Some failures stem from limitations of QEMU's GDB stub - it does not support non-stop mode, multiple threads, hardware watchpoints. In some other cases GDB crashes, perhaps due to a bug in either GDB or QEMU. I have made no attempt to investigate these failures in detail. Running all tests in gdb.base on x86_64 Linux using qemu-aarch64 I got: # of unexpected core files=091 # of expected passes=0966182 # of unexpected failures=09558 # of expected failures=0920 # of known failures=09=0929 # of untested testcases=0914 # of unresolved testcases=094 # of unsupported tests=09146 # of paths in test names=097 # of duplicate test names=093 Running same set of tests using qemu-x86_64 I got: # of unexpected core files=091 # of expected passes=0964988 # of unexpected failures=092527 # of expected failures=0920 # of known failures=09=0929 # of untested testcases=098 # of unresolved testcases=095 # of unsupported tests=09174 # of paths in test names=097 # of duplicate test names=095 [1]: https://inbox.sourceware.org/gdb/938d2b5574b94d9584b758eee3c0c064@ti.c= om/ [2]: https://inbox.sourceware.org/gdb-patches/20231204024634.419445-3-zeck6= 54321@gmail.com/ Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D33590 --- gdb/testsuite/boards/qemu-user.exp | 244 +++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 gdb/testsuite/boards/qemu-user.exp diff --git a/gdb/testsuite/boards/qemu-user.exp b/gdb/testsuite/boards/qemu= -user.exp new file mode 100644 index 00000000000..0206d6e9d40 --- /dev/null +++ b/gdb/testsuite/boards/qemu-user.exp @@ -0,0 +1,244 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is a dejagnu "board file" and is used to run the testsuite +# using QEMU's usermode emulation. Use --target to specify desired target= . +# +# To use a specific QEMU, pass custom options to QEMU, specify the target +# toolchain to use, set environment variable QEMU, QEMU_FLAGS, CROSS (see +# examples below). +# +# Note that to execute dynamically linked programs with qemu-user, a minim= al +# sysroot is required. It contains the target system's dynamic linker and= the +# shared libraries needed by the program. If it is not located at the def= ault +# path, either set QEMU_LD_PREFIX to the sysroot path or set QEMU_FLAGS to +# "-L /path/to/sysroot". +# +# To avoid the need for minimal sysroot altogether, force static compilati= on +# by setting CFLAGS_FOR_TARGET to "-static" (see example below). Note how= ever +# that some tests need dynamic linking (for example, gdb.base/solib-vanish= .exp) +# +# Example usage: +# +# make check RUNTESTFLAGS=3D'--target=3Driscv64-unknown-linux-gnu --ta= rget_board=3Dqemu-user' +# +# make check QEMU=3Dqemu-aarch64 QEMU_FLAGS=3D'-L /usr/aarch64-linux-g= nu' \ +# CROSS=3Daarch64-linux-gnu- \ +# RUNTESTFLAGS=3D'--target=3Daarch64-linux-gnu --target_board=3Dqem= u-user' +# +# make check CROSS=3D/opt/gcc-8-armhf/bin/arm-linux-gnueabihf- \ +# RUNTESTFLAGS=3D'--target=3Darm-unknown-linux-gnueabihf --target_b= oard=3Dqemu-user' +# +# make check \ +# RUNTESTFLAGS=3D'CFLAGS_FOR_TARGET=3D-static --target=3Daarch64-un= known-linux-gnu --target_board=3Dqemu-user' +# + +# Load this for "gdb_target_cmd_ext" +load_lib gdbserver-support.exp + +# +# Setup +# +set qemu "" +set qemu_flags {} +set cross "" + +# Provide sensible defaults based on TARGET_TRIPLET (--target option). +# Add more if needed. +switch -glob $target_triplet { + "x86_64-*-linux-gnu" { +=09set qemu "qemu-x86_64" +=09set cross "x86_64-linux-gnu-" + } + "riscv64-*-linux-gnu" { +=09set qemu "qemu-riscv64" +=09set cross "riscv64-linux-gnu-" + } + "aarch64-*-linux-gnu" { +=09set qemu "qemu-aarch64" +=09set cross "aarch64-linux-gnu-" + } + "arm-*-linux-gnueabihf" { +=09set qemu "qemu-arm" +=09set cross "arm-linux-gnueabihf-" + } + "powerpc-*-linux-gnu" { +=09set qemu "qemu-ppc" +=09set cross "powerpc-linux-gnu-" + } + "powerpc64le-*-linux-gnu" { +=09set qemu "qemu-ppc64le" +=09set cross "powerpc64le-linux-gnu-" + } + default { +=09if { ![info exist ::env(QEMU)] || ![info exists ::env(CROSS)] } { +=09 error "No defaults for target $target_triplet - you may want to spe= cify QEMU and CROSS variables." +=09} + } +} + +# +# Allow user to specify QEMU binary and/or custom options by setting +# QEMU and/or QEMU_FLAGS environment variables. +# +if [info exists ::env(QEMU)] { + set qemu $::env(QEMU) +} +if [info exists ::env(QEMU_FLAGS)] { + # Here we split QEMU_FLAGS to a list of arguments. We use a simplisti= c + # method by splitting by space - so shell-style quoting and escaping i= s + # not supported. + set qemu_flags [regexp -all -inline {\S+} $::env(QEMU_FLAGS)] +} + +# +# Allow user to specify target toolchain by setting CROSS environment vari= able +# +if [info exists ::env(CROSS)] { + set cross $::env(CROSS) +} + +set qemu_spawn_id "" + +set_board_info compiler "${cross}gcc" +set_board_info c++compiler "${cross}g++" + +set_board_info use_gdb_stub 1 +set_board_info gdb_protocol "remote" +set_board_info gdb,do_reload_on_run 1 +set_board_info gdb,nofileio 1 +set_board_info exit_is_reliable 1 +# QEMU does not support HW watchpoints +set_board_info gdb,no_hardware_watchpoints 1 + + + +# Helper function to reap previously spawn QEMU. Calling this function +# if no QEMU is running is no-op. + +proc qemu_reap { } { + global qemu_spawn_id + + if { $qemu_spawn_id !=3D "" } { +=09verbose -log "qemu-user: closing previous spawn id $qemu_spawn_id" +=09catch { close -i $qemu_spawn_id } + +=09# Some tests get QEMU/its gdb stub into a state where the process +=09# doesn't want to die. Use -nowait to tell TCL to wait in background +=09# instead of blocking. +=09wait -nowait -i $qemu_spawn_id +=09set qemu_spawn_id "" +=09unset ::inferior_spawn_id + } +} + +# Helper function to run QEMU. Returns the port number on which QEMU +# stub listens or 0 on failure. + +proc qemu_spawn { file inferior_args } { + global qemu_spawn_id + global qemu + global qemu_flags + + # Close any previous QEMU instance. + qemu_reap + + # Run QEMU. + while 1 { +=09set portnum [get_portnum] +=09set qemu_args [list {*}$qemu_flags -g $portnum $file {*}$inferior_args] + +=09if {[catch { spawn $qemu {*}$qemu_args } err ]} { +=09 verbose -log "qemu-user: failed to spawn QEMU: $err" +=09 return 0 +=09} +=09set qemu_spawn_id $spawn_id + +=09# QEMU stub doesn't do inferior I/O through GDB. But we can +=09# talk to the program using QEMU's tty instead. +=09global inferior_spawn_id +=09set inferior_spawn_id $spawn_id + +=09expect { +=09 -i $qemu_spawn_id +=09 -timeout 1 +=09 -re ".*Address already in use.*" { +=09=09# Port already in use. Cleanup and retry. +=09=09verbose -log "qemu-user: port $portnum is already in use." + +=09=09qemu_reap +=09=09continue +=09 } +=09 eof { +=09=09verbose -log "qemu-user: QEMU terminated prematurely." + +=09=09qemu_reap +=09=09return 0 +=09 } + +=09} + +=09verbose -log "qemu-user: spawned with spawn id $qemu_spawn_id, pid [exp= _pid $qemu_spawn_id], stub listening at $portnum" +=09break + } + + return $portnum +} + + + +# gdb_reload -- load a file into the target. + +proc gdb_reload { {inferior_args {}} } { + global last_loaded_file + set portnum [qemu_spawn $last_loaded_file $inferior_args] + + if { $portnum =3D=3D 0 } { +=09return 1 + } + + set res [gdb_target_cmd_ext "remote" ":$portnum"] + + if { $res =3D=3D 2 } { +=09unsupported "qemu-user: cannot connect to QEMU stub (likely because QEM= U does not support non-stop mode)." +=09# QEMU does not support non-stop mode. Exit here to avoid subsequent +=09# failures / timeouts. +=09qemu_reap +=09exit 0 + } + if { $res !=3D 0 } { +=09qemu_reap +=09return 1 + } + return 0 +} + +proc mi_gdb_target_load { } { + global last_loaded_file + set portnum [qemu_spawn $last_loaded_file ""] + + if { $portnum =3D=3D 0 } { +=09return -1 + } + + if {[mi_gdb_target_cmd "remote" ":$portnum"] !=3D 0} { +=09qemu_reap +=09return -1 + } + + return 0 +} + +set GDBFLAGS "${GDBFLAGS} -iex \"set auto-connect-native-target off\"" --=20 2.53.0