From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id fw+tEn8L6mrIbgsAWB0awg (envelope-from ) for ; Tue, 03 Nov 2026 11:27:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793723263; bh=i2os01zhpy5H4IkwEwA3amOktr5SyCJVu88sLXDjKEI=; h=Date:Subject:To:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=tVeX8Ipp96fmc5twS8CS2CBPOfUrlQTgIxsU+zwuXj8Qa5OQdTPWTf18cFEPcJoNX 9NrxVx5h9LG774I1sK8CGAkeSqtJD8Pqhh68UOcVAtBmdC7qcbgaoGWplMpFbmJ736 ADykdeySvmFYcGHBa+2jjqadxQ0Xuh7xZCUH9eo0= Received: by simark.ca (Postfix, from userid 112) id 3A8771E052; Tue, 03 Nov 2026 11:27:43 -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 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=ZebkTxT3; dkim-atps=neutral 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 D07921E052 for ; Tue, 03 Nov 2026 11:27:41 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9C294BA7984 for ; Tue, 3 Nov 2026 16:27:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9C294BA7984 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=ZebkTxT3 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 161DE4BA2E0B for ; Tue, 3 Nov 2026 16:27:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 161DE4BA2E0B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 161DE4BA2E0B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793723225; cv=none; b=rkyL9LUA4UYXs/XeXKi6K1mMds+I5vPstUf9hfpW6bR3nKnmiFsjpB7raMQbuiC9SP/TgxIs+sX559KWyBpUFJ2P7eWtZUsDGFNzR9Ixkig7e24hKzLvOJ7NwWXmhQOF9qDypVsex/05RBq5aDT8S752gAaNoW89N8mPsJCti68= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793723225; c=relaxed/simple; bh=i2os01zhpy5H4IkwEwA3amOktr5SyCJVu88sLXDjKEI=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=et44ltskpRwGSt1Xvdw/0I/XMYtG7jJZW4mIiwM11Xs3syaGwQui1HOhVpbXCqEx+wxJe1LfYqvIguWger+qk9wOt7GjD3M2q30M1U12I5fH7bCQmOJ+n9Li3qTPOTT23B6AU6hqMI1Qt0/TTDr82AxcPt1RHdyLT3/lgchz0Bo= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=ZebkTxT3 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 161DE4BA2E0B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793723223; bh=i2os01zhpy5H4IkwEwA3amOktr5SyCJVu88sLXDjKEI=; h=Date:Subject:To:References:From:In-Reply-To:From; b=ZebkTxT37PrUVPbkwVSXS4G459KbWGDLrjbFY2+dE2U0Nj1bQCeBu0soqWcOR5HFB 9Vd1/2ROEGcP0zXd1ouK4Pb0ZAmNk/sGLiNBY+SEAOgrsRahnhOWdTGUJXrAu8wE8c u7zmz4Dnru0OVfr1fp48x/ZtBXBO6E1BpvQ2yHUI= Received: by simark.ca (Postfix) id 6DFBB1E052; Tue, 03 Nov 2026 11:27:03 -0500 (EST) Message-ID: <8781e8b4-e35c-46af-bc9b-362b4d7ea377@simark.ca> Date: Tue, 3 Nov 2026 11:27:02 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] gdb/testsuite: add support for running tests using QEMU To: Jan Vrany , gdb-patches@sourceware.org References: <20261027135827.3868769-1-jan@vrany.io> Content-Language: fr From: Simon Marchi In-Reply-To: <20261027135827.3868769-1-jan@vrany.io> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 On 10/27/26 9:59 AM, Jan Vrany wrote: >> I think that is really cool, I am in favor of adding it. Naming nit: >> perhaps name it qemu-user, I could see us also adding a qemu-system >> board to test bare metal. For example, I saw that qemu now has an AVR >> port, I would like to make a board based on that. > > OK, I renamed it. I just tried to save me some typing :-) > >> >> Do you pass a -L to the qemu user binary, or set QEMU_LD_PREFIX? I >> found it necessary for the right dynamic linker to be found: >> >> $ qemu-aarch64 ./a.out >> qemu-aarch64: Could not open '/lib/ld-linux-aarch64.so.1': No such file or directory /0.0s >> $ qemu-aarch64 -L /usr/aarch64-linux-gnu ./a.out >> Hello! >> >> If it's always going to be required to pass one or the other, could the >> board require that the user specify it, or could it try to guess it >> (e.g. "/usr/$triplet")? > > AFAIK no. If no QEMU_LD_PREFIX is set nor -L passed, QEMU uses default, > compiled-in path (if it exists). The value of that default depends, for > example in recent Debian it is /usr/gnemul/qemu-aarch64 (/usr/gnemul/qemu-riscv64, ...). > On my system I have things setup so the "minimal sysroot" is in that it > "just works". > > If one wants to use different prefix, he/she can either set QEMU_LD_PREFIX > or QEMU_FLAGS accordingly. > > In v2 (see below) I have added comment in the qemu-user.exp to clarify > this. I have also added a way to force static compilation - this still > allows running large set of tests without need to for dynamic linker. > >> >> I ran your patch through Claude, most of the important comments were >> about robustnessof the "non-happy" paths, which in my experience is very >> important when you're trying to use the board and figuring things out. >> I would suggest giving all the failure modes a try to see if the error >> reported is sensible and the user experience is good. >> > > Thanks. I hope I addressed all issues in v2 except following: > >> >> - gdb/testsuite/boards/qemu.exp:179: `exit 0` terminates the runtest >> process, not just the current test. In a serial run, all the >> remaining tests are skipped silently. Use `unsupported "..."` with a >> message and `return -1` instead; gdb_run_cmd already handles a >> non-zero return (lib/gdb.exp:549). > > This is intentional. My understanding is that in GDB testsuite there's > no distinction between test "setup" and tests itself in a sense that if > setup fails, then the tests are meaningless. So in one fail to connect > to the target, the failure is noted and testing proceeds, leading to > inevitable timeouts and tests are recorded as failures. The "exit 0" here > is to avoid these timeouts for tests that cannot work. > > It may cause some tests that could work to be silently skipped but seems > to me as an unfortunate consequence of lack of setup-test-teardown > structure in GDB tests. > > If simply returning a failure and let the rest of tests to timeout is > preferable, I have no problem. > > 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=-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 1 > # of expected passes 66182 > # of unexpected failures 558 > # of expected failures 20 > # of known failures 29 > # of untested testcases 14 > # of unresolved testcases 4 > # of unsupported tests 146 > # of paths in test names 7 > # of duplicate test names 3 > > Running same set of tests using qemu-x86_64 I got: > > # of unexpected core files 1 > # of expected passes 64988 > # of unexpected failures 2527 > # of expected failures 20 > # of known failures 29 > # of untested testcases 8 > # of unresolved testcases 5 > # of unsupported tests 174 > # of paths in test names 7 > # of duplicate test names 5 > > [1]: https://inbox.sourceware.org/gdb/938d2b5574b94d9584b758eee3c0c064@ti.com/ > [2]: https://inbox.sourceware.org/gdb-patches/20231204024634.419445-3-zeck654321@gmail.com/ > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33590 > --- > gdb/testsuite/boards/qemu-user.exp | 235 +++++++++++++++++++++++++++++ > 1 file changed, 235 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..286940f16a4 > --- /dev/null > +++ b/gdb/testsuite/boards/qemu-user.exp > @@ -0,0 +1,235 @@ > +# 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. Two spaces after period (elsewhere below too). > +# > +# To use specific QEMU, pass custom options to QEMU, target toolchain to use or "To use a specific QEMU" Maybe "specify the target toolchain to use". > +# to pass custom options to the compiler, set environment variable QEMU, > +# QEMU_FLAGS, CROSS or CFLAGS_FOR_TARGET (see examples below). > +# > +# Note that to execute dynamically linked programs with qemu-user, a minimal > +# 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 default > +# path, either set QEMU_LD_PREFIX to the sysroot path or set QEMU_FLAGS to > +# "-L /path/to/sysroot". > +# > +# To avoid need for minimal sysroot altogether, force static compilation by I would say "to avoid the need for..." > +# setting CFLAGS_FOR_TARGET to "-static" (see example below). Note however > +# that some tests need dynamic linking (for example, gdb.base/solib-vanish.exp) > +# > +# Example usage: > +# > +# make check RUNTESTFLAGS='--target=riscv64-unknown-linux-gnu --target_board=qemu-user' > +# > +# make check QEMU=qemu-arm CROSS=/opt/gcc-8-armhf/bin/arm-linux-gnueabihf- \ > +# RUNTESTFLAGS='--target=arm-unknown-linux-gnueabihf --target_board=qemu-user' > +# > +# make check CFLAGS_FOR_TARGET=-static \ > +# RUNTESTFLAGS='--target=aarch64-unknown-linux-gnu --target_board=qemu-user' For my own selfish benefit of remembering how to specify the path to the sysroot if qemu doesn't know it, can you add an example: make check QEMU=qemu-aarch64s QEMU_FLAGS='-L /usr/aarch64-linux-gnu' \ RUNTESTFLAGS='--target=aarch64-linux-gnu' CROSS=aarch64-linux-gnu- \ --target_board=qemu-user At the same time it will give an example of the QEMU_FLAGS variable. Using the qemu and aarch64 toolchain packages straight from the archlinux repos, I successfully ran this: $ make check TESTS="gdb.base/break.exp" RUNTESTFLAGS="--target_board=qemu-user" QEMU=qemu-aarch64 QEMU_FLAGS="-L /usr/aarch64-linux-gnu/" CROSS=aarch64-linux-gnu- Thanks to the switch below I can even shorten it to: $ make check TESTS="gdb.base/break.exp" RUNTESTFLAGS="--target_board=qemu-user" QEMU_FLAGS="-L /usr/aarch64-linux-gnu/" I also tested with a more "custom" setup on Ubuntu 24.04, self-built qemu and toolchain/sysroot build with crosstool-ng: $ make check TESTS="gdb.base/break.exp" RUNTESTFLAGS="--target_board=qemu-user" QEMU=/opt/qemu/11.1.3/bin/qemu-arm \ QEMU_FLAGS="-L /home/smarchi/x-tools/armv7-rpi2-linux-gnueabihf/armv7-rpi2-linux-gnueabihf/sysroot" \ CROSS=/home/smarchi/x-tools/armv7-rpi2-linux-gnueabihf/bin/armv7-rpi2-linux-gnueabihf- It worked but it needed a little timeout bump: $ echo 'set gdb_test_timeout [expr 5 * $timeout]' >> testsuite/site.exp 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 = /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. > +# > + > +# Load this for "gdb_target_cmd_ext" > +load_lib gdbserver-support.exp > + > +# > +# Setup > +# > +set qemu "" > +set qemu_flags "" > +set cross "" > + > +switch -glob $target_triplet { > + "x86_64-*-linux-gnu" { > + set qemu "qemu-x86_64" > + set cross "x86_64-linux-gnu-" > + } > + "riscv64-*-linux-gnu" { > + set qemu "qemu-riscv64" > + set cross "riscv64-linux-gnu-" > + } > + "aarch64-*-linux-gnu" { > + set qemu "qemu-aarch64" > + set cross "aarch64-linux-gnu-" > + } > + "arm-*-linux-gnueabihf" { > + set qemu "qemu-arm" > + set cross "arm-linux-gnueabihf-" > + } > + "powerpc-*-linux-gnu" { > + set qemu "qemu-ppc" > + set cross "powerpc-linux-gnu-" > + } > + "powerpc64le-*-linux-gnu" { > + set qemu "qemu-ppc64le" > + set cross "powerpc64le-linux-gnu-" > + } > + # > + # Add more here if needed. > + # Apparently, `#` in a switch is not a comment, it just happens to work because you have an even number of tokens. Add a word and it breaks down. You'll have to move this comment either above the switch or in the default { }. > + default { > + error "Target $target_triplet not yet supported by board" > + } Maybe this shouldn't be a hard error, since you could pass all that is needed for another arch on the command line, with variables QEMU and CROSS. I would see this switch as just providing to sensible commont defaults. > +} > + > +# > +# 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)] { > + set qemu_flags $::env(QEMU_FLAGS) > +} > + > +# > +# Allow user to specify target toolchain by setting CROSS environment variable > +# > +if [info exists ::env(CROSS)] { > + set cross $::env(CROSS) > +} > + > +# > +# Allow user to pass custom flags to the compiler. > +# > +if [info exists ::env(CFLAGS_FOR_TARGET)] { > + set CFLAGS_FOR_TARGET $::env(CFLAGS_FOR_TARGET) > +} I wonder if this is needed. The user can already pass some CFLAGS_FOR_TARGET in RUNTESTFLAGS like this: RUNTESTFLAGS="CFLAGS_FOR_TARGET='-mthumb' CXXFLAGS_FOR_TARGET='-mthumb'" I would rather not have the individual test boards re-implement that (plus, you'd have to support all the *_FOR_TARGET variables). The examples and comment on top of the file would need to be updated if remvoing this. > + > +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 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. > + # > + if { $qemu_spawn_id != "" } { > + verbose -log "qemu: closing previous spawn id $qemu_spawn_id" > + if [catch { close -i $qemu_spawn_id } != 0] { This passes `!=` and `0` as variable names to `catch`, it works by accident (should be fixed in simavr.exp too). Use: if {[catch {close -i $id}] != 0} > + warning "qemu: failed to close connection to previous qemu instance" > + } > + > + # some tests get QEMU/its gdb stub > + # into a state where the process doesn't want to die > + # so -nowait to not hang the entire test run > + wait -nowait -i $qemu_spawn_id > + set qemu_spawn_id "" > + } > + > + > + # > + # Run QEMU. > + # > + while 1 { > + set portnum [expr {1024+round(rand()*(65535-1024))}] > + set cmd "spawn -noecho $qemu $qemu_flags -g $portnum $file $inferior_args" You can re-use proc get_portnum from gdbserver-support.exp to get a port number. > + > + verbose -log "Spawning qemu: $cmd" > + > + if {[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 > + verbose -log "Failed to run QEMU: $err" > + return 0 > + } > + set qemu_spawn_id $spawn_id > + > + # QEMU stub doesn't do inferior I/O through GDB. But we can > + # talk to the program using QEMU's tty instead. > + global inferior_spawn_id > + set inferior_spawn_id $spawn_id > + > + expect { > + -i $qemu_spawn_id > + -timeout 1 > + -re ".*Address already in use.*" { > + # Port already in use. Cleanup and retry. > + verbose -log "Port $portnum is already in use." > + if [catch { close -i $qemu_spawn_id } != 0] { if {[catch {close -i $qemu_spawn_id}] != 0} > + warning "failed to terminate QEMU instance" > + } > + wait -i $qemu_spawn_id > + continue > + } > + eof { > + verbose -log "QEMU terminated prematurely." > + return 0 > + } On eof, call wait to reap the process and unset qemu_spawn_id/inferior_spawn_id. > + > + } > + > + verbose -log "QEMU spawned with spawn id $qemu_spawn_id, pid [exp_pid $qemu_spawn_id], stub listening at $portnum" > + break > + } > + > + 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 == 0 } { > + return 1 > + } > + > + set res [gdb_target_cmd_ext "remote" ":$portnum"] > + > + if { $res == 2 } { > + unsupported "Cannot connect to QEMU stub (likely because QEMU does not support non-stop mode)." > + # QEMU does not support non-stop mode. Exit here to avoid subsequent > + # failures / timeouts. > + exit 0 > + } On failure, it would be good to shut down and reap the QEMU process right now. > + return [expr {$res == 0 ? 0 : 1}] > +} > + > +proc mi_gdb_target_load { } { > + global last_loaded_file > + set portnum [qemu_spawn $last_loaded_file ""] > + > + if { $portnum == 0 } { > + return -1 > + } > + > + if { [mi_gdb_target_cmd "remote" ":$portnum"] != 0 } { > + return -1 > + } Likewise here, on failure, clean up QEMU. Simon