Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan@vrany.io>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan@vrany.io>, Simon Marchi <simon.marchi@efficios.com>
Subject: [pushed] gdb/testsuite: add support for running tests using QEMU
Date: Fri, 20 Nov 2026 10:40:35 +0000	[thread overview]
Message-ID: <20261120104016.649450-1-jan@vrany.io> (raw)

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/

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33590
---
 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 <http://www.gnu.org/licenses/>.
+
+# 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 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 the need for minimal sysroot altogether, force static compilation
+# by 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-aarch64 QEMU_FLAGS='-L /usr/aarch64-linux-gnu' \
+#        CROSS=aarch64-linux-gnu- \
+#        RUNTESTFLAGS='--target=aarch64-linux-gnu --target_board=qemu-user'
+#
+#     make check CROSS=/opt/gcc-8-armhf/bin/arm-linux-gnueabihf- \
+#        RUNTESTFLAGS='--target=arm-unknown-linux-gnueabihf --target_board=qemu-user'
+#
+#     make check \
+#        RUNTESTFLAGS='CFLAGS_FOR_TARGET=-static --target=aarch64-unknown-linux-gnu --target_board=qemu-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" {
+	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-"
+    }
+    default {
+	if { ![info exist ::env(QEMU)] || ![info exists ::env(CROSS)] } {
+	    error "No defaults for target $target_triplet - you may want to specify QEMU and CROSS variables."
+	}
+    }
+}
+
+#
+# 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 simplistic
+    # method by splitting by space - so shell-style quoting and escaping is
+    # not supported.
+    set qemu_flags [regexp -all -inline {\S+} $::env(QEMU_FLAGS)]
+}
+
+#
+# Allow user to specify target toolchain by setting CROSS environment variable
+#
+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 != "" } {
+	verbose -log "qemu-user: closing previous spawn id $qemu_spawn_id"
+	catch { close -i $qemu_spawn_id }
+
+	# Some tests get QEMU/its gdb stub into a state where the process
+	# doesn't want to die.  Use -nowait to tell TCL to wait in background
+	# instead of blocking.
+	wait -nowait -i $qemu_spawn_id
+	set qemu_spawn_id ""
+	unset ::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 {
+	set portnum [get_portnum]
+	set qemu_args [list {*}$qemu_flags -g $portnum $file {*}$inferior_args]
+
+	if {[catch { spawn $qemu {*}$qemu_args } err ]} {
+	    verbose -log "qemu-user: failed to spawn 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 "qemu-user: port $portnum is already in use."
+
+		qemu_reap
+		continue
+	    }
+	    eof {
+		verbose -log "qemu-user: QEMU terminated prematurely."
+
+		qemu_reap
+		return 0
+	    }
+
+	}
+
+	verbose -log "qemu-user: 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 "qemu-user: 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.
+	qemu_reap
+	exit 0
+    }
+    if { $res != 0 } {
+	qemu_reap
+	return 1
+    }
+    return 0
+}
+
+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} {
+	qemu_reap
+	return -1
+    }
+
+    return 0
+}
+
+set GDBFLAGS "${GDBFLAGS} -iex \"set auto-connect-native-target off\""
-- 
2.53.0



                 reply	other threads:[~2026-11-20 10:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120104016.649450-1-jan@vrany.io \
    --to=jan@vrany.io \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox