Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 08/11] [gdb/testsuite] Refactor exception handling in gdb_test_multiple
Date: Mon, 24 Aug 2026 15:58:52 +0200	[thread overview]
Message-ID: <20260824135855.1195963-9-tdevries@suse.de> (raw)
In-Reply-To: <20260824135855.1195963-1-tdevries@suse.de>

Simplify gdb_test_multiple using return -options and try/finally.

In the process, we also try to fix PR34553.

I've added a test to verify this, but it'll be only useful after remote_expect
gets fixed [1].

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34552
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34553

[1] https://debbugs.gnu.org/cgi/bugreport.cgi?bug=81691
---
 gdb/testsuite/gdb.testsuite/gdb-test.exp | 51 ++++++++++++++++++++++++
 gdb/testsuite/lib/gdb.exp                | 37 +++++++++--------
 2 files changed, 69 insertions(+), 19 deletions(-)

diff --git a/gdb/testsuite/gdb.testsuite/gdb-test.exp b/gdb/testsuite/gdb.testsuite/gdb-test.exp
index dab65f21bdb..231f0755f58 100644
--- a/gdb/testsuite/gdb.testsuite/gdb-test.exp
+++ b/gdb/testsuite/gdb.testsuite/gdb-test.exp
@@ -49,6 +49,57 @@ with_test_prefix "cmd with trailing control code" {
     }
 }
 
+foreach_with_prefix variant {0 1 2} {
+    proc level_2_inner {} {
+	if {$::variant == 0} {
+	    return -level 2 "return_level_2"
+	} elseif {$::variant == 1} {
+	    send_gdb "print 1\n"
+	    set prompt_re [string_to_regexp "(gdb) "]
+	    remote_expect host 10 {
+		-re " = 1\r\n$prompt_re$" {
+		    return -level 2 "return_level_2"
+		}
+	    }
+	} else {
+	    gdb_test_multiple "print 1" "" {
+		-re -wrap " = 1" {
+		    return -level 2 "return_level_2"
+		}
+	    }
+	}
+	return "inner"
+    }
+
+    proc level_2_outer {} {
+	level_2_inner
+	return "outer"
+    }
+
+    try {
+	set res "initial"
+	set res [level_2_outer]
+    } finally {
+	if {$variant == 0} {
+	    # trivial case.
+	    gdb_assert {$res == "return_level_2"}
+	} elseif {$variant == 1} {
+	    # Remove_expect case.
+	    if {$res == "outer"} {
+		# Dejagnu bug.
+		# https://debbugs.gnu.org/cgi/bugreport.cgi?bug=81691
+		setup_xfail *-*-*
+	    }
+	    gdb_assert {$res == "return_level_2"}
+	    set variant1_res $res
+	} else {
+	    # Gdb_test_multiple case. This should work if the remote_expect
+	    # case works.
+	    gdb_assert {$res == $variant1_res}
+	}
+    }
+}
+
 # Change the prompt.
 set prompt "(GDB) "
 set prompt_re "\\(GDB\\) $"
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 479d1229142..99468ac3ef3 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -1369,7 +1369,6 @@ proc gdb_test_multiple { command message args } {
 	send_user "Message is \"$message\"\n"
     }
 
-    set result -1
     set string "${command}\n"
     if { $command != "" } {
 	set multi_line_re "\[\r\n\] *>"
@@ -1574,25 +1573,25 @@ proc gdb_test_multiple { command message args } {
     }
     set gdb_test_name "$message"
 
-    set result 0
-    set code [catch {gdb_expect $code} string]
-
-    # Clean up the gdb_test_name variable.  If we had a
-    # previous value then restore it, otherwise, delete the variable
-    # from the parent scope.
-    if { [info exists gdb_test_name_old] } {
-	set gdb_test_name "$gdb_test_name_old"
-    } else {
-	unset gdb_test_name
-    }
-
-    if {$code == 1} {
-	global errorInfo errorCode
-	return -code error -errorinfo $errorInfo -errorcode $errorCode $string
-    } elseif {$code > 1} {
-	return -code $code $string
+    try {
+	if {[info exists result]} {
+	    error "result set but not used"
+	}
+	set result 0
+	if {[catch {gdb_expect $code} string opts] == 0} {
+	    return $result
+	}
+	return -options [dict incr opts -level] $string
+    } finally {
+	# Clean up the gdb_test_name variable.  If we had a
+	# previous value then restore it, otherwise, delete the variable
+	# from the parent scope.
+	if { [info exists gdb_test_name_old] } {
+	    set gdb_test_name "$gdb_test_name_old"
+	} else {
+	    unset gdb_test_name
+	}
     }
-    return $result
 }
 
 # Usage: gdb_test_multiline NAME INPUT RESULT {INPUT RESULT} ...
-- 
2.51.0


  parent reply	other threads:[~2026-08-24 14:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24 13:58 [PATCH 00/11] [gdb/testsuite] Refactor exception handling Tom de Vries
2026-08-24 13:58 ` [PATCH 01/11] [gdb/testsuite] Normalize indentation in with_test_prefix Tom de Vries
2026-08-24 13:58 ` [PATCH 02/11] [gdb/testsuite] Fix return -level 2 bug " Tom de Vries
2026-08-24 13:58 ` [PATCH 03/11] [gdb/testsuite] Simplify foreach_with_prefix Tom de Vries
2026-08-24 13:58 ` [PATCH 04/11] [gdb/testsuite] Refactor exception handling in foreach_with_prefix Tom de Vries
2026-08-24 13:58 ` [PATCH 05/11] [gdb/testsuite] Add transparent_uplevel Tom de Vries
2026-08-24 13:58 ` [PATCH 06/11] [gdb/testsuite] Refactor exception handling Tom de Vries
2026-08-24 13:58 ` [PATCH 07/11] [gdb/testsuite] Refactor exception handling in gdb_expect Tom de Vries
2026-08-24 13:58 ` Tom de Vries [this message]
2026-08-25  7:29   ` [PATCH 08/11] [gdb/testsuite] Refactor exception handling in gdb_test_multiple Tom de Vries
2026-08-24 13:58 ` [PATCH 09/11] [gdb/testsuite] Refactor exception handling in lock_file_acquire/release Tom de Vries
2026-08-24 13:58 ` [PATCH 10/11] [gdb/testsuite] Refactor exception handling in tentative_rename Tom de Vries
2026-08-24 13:58 ` [PATCH 11/11] [gdb/testsuite] Refactor exception handling in with_stub_devices Tom de Vries

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=20260824135855.1195963-9-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /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