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 2/2] [gdb/testsuite] Make gdb.threads/inf-thr-count.exp more readable
Date: Fri, 18 Apr 2025 11:23:56 +0200	[thread overview]
Message-ID: <20250418092356.4579-3-tdevries@suse.de> (raw)
In-Reply-To: <20250418092356.4579-1-tdevries@suse.de>

While investigating a timeout in gdb.threads/inf-thr-count.exp I noticed that
it uses quite some escaping, resulting in hard-to-parse regexps like
"\\\$$::decimal".

Fix this by reducing the escaping using:
- quoting strings using {} instead of "", and
- string_to_regexp.

Also use multi_line to split up long multi-line regexps.

Tested on x86_64-linux.
---
 gdb/testsuite/gdb.threads/inf-thr-count.exp | 55 +++++++++++++--------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/gdb/testsuite/gdb.threads/inf-thr-count.exp b/gdb/testsuite/gdb.threads/inf-thr-count.exp
index 9f52655364a..1174326f2c6 100644
--- a/gdb/testsuite/gdb.threads/inf-thr-count.exp
+++ b/gdb/testsuite/gdb.threads/inf-thr-count.exp
@@ -43,7 +43,7 @@ if {[build_executable "failed to prepare" $testfile $srcfile \
 # Start GDB.  Ensure we are in non-stop mode as we need to read from
 # the inferior while it is running.
 save_vars {GDBFLAGS} {
-    append GDBFLAGS " -ex \"set non-stop on\""
+    append GDBFLAGS { -ex "set non-stop on"}
     clean_restart $binfile
 }
 
@@ -54,22 +54,20 @@ if ![runto_main] {
 gdb_breakpoint breakpt
 gdb_continue_to_breakpoint "first breakpt call"
 
+set re_var [string_to_regexp "$"]$decimal
+
 # Check we can see a single thread to begin with.
-gdb_test "p \$_inferior_thread_count" \
-    "^\\\$$::decimal = 1" \
-    "only one thread in \$_inferior_thread_count"
+gdb_test {p $_inferior_thread_count} \
+    "^$re_var = 1" \
+    {only one thread in $_inferior_thread_count}
 
 # We don't want thread events, it makes it harder to match GDB's
 # output.
 gdb_test_no_output "set print thread-events off"
 
 # Continue the program in the background.
-set test "continue&"
-gdb_test_multiple "continue&" $test {
-    -re "Continuing\\.\r\n$gdb_prompt " {
-	pass $test
-    }
-}
+gdb_test -no-prompt-anchor "continue&" \
+    [string_to_regexp "Continuing."]
 
 # Read the 'stage' flag from the inferior.  This is initially 0, but
 # will be set to 1 once the extra thread has been created, and then 2
@@ -88,8 +86,17 @@ proc wait_for_stage { num } {
     set failure_count 0
     set cmd "print /d stage"
     set stage_flag 0
+
+    set re_int [string_to_regexp "-"]?$::decimal
+
+    set re_msg \
+	[multi_line \
+	     "Cannot execute this command while the target is running" \
+	     {Use the "interrupt" command to stop the target} \
+	     [string_to_regexp "and then try again."]]
+
     gdb_test_multiple "$cmd" "wait for 'stage' flag to be $num" {
-	-re -wrap "^Cannot execute this command while the target is running\\.\r\nUse the \"interrupt\" command to stop the target\r\nand then try again\\." {
+	-re -wrap ^$re_msg {
 	    fail "$gdb_test_name (can't read asynchronously)"
 	    gdb_test_no_output "interrupt"
 
@@ -101,7 +108,7 @@ proc wait_for_stage { num } {
 	    }
 	}
 
-	-re -wrap "^\\$\[0-9\]* = (\[-\]*\[0-9\]*).*" {
+	-re -wrap "^$::re_var = ($re_int).*" {
 	    set stage_flag $expect_out(1,string)
 	    if {$stage_flag != $num} {
 		set stage_flag 0
@@ -131,8 +138,8 @@ if {![wait_for_stage 1]} {
 if {[target_info exists gdb_protocol]
     && ([target_info gdb_protocol] == "remote"
 	|| [target_info gdb_protocol] == "extended-remote")} {
-    set new_thread_re "\\\[New Thread \[^\r\n\]+\\\]\r\n"
-    set exit_thread_re "\\\[Thread \[^\r\n\]+ exited\\\]\r\n"
+    set new_thread_re {\[New Thread [^\r\n]+\]\r\n}
+    set exit_thread_re {\[Thread [^\r\n]+ exited\]\r\n}
 } else {
     set new_thread_re ""
     set exit_thread_re ""
@@ -141,9 +148,9 @@ if {[target_info exists gdb_protocol]
 # This is the test we actually care about.  Check that the
 # $_inferior_thread_count convenience variable shows the correct
 # thread count; the new thread should be visible.
-gdb_test "with print thread-events on -- p \$_inferior_thread_count" \
-    "^${new_thread_re}\\\$$::decimal = 2" \
-    "second thread visible in \$_inferior_thread_count"
+gdb_test {with print thread-events on -- p $_inferior_thread_count} \
+    "^${new_thread_re}$re_var = 2" \
+    {second thread visible in $_inferior_thread_count}
 
 # Set a variable in the inferior, this will cause the second thread to
 # exit.
@@ -157,9 +164,9 @@ if {![wait_for_stage 2]} {
 }
 
 # Check that the second thread has gone away.
-gdb_test "with print thread-events on -- p \$_inferior_thread_count" \
-    "^${exit_thread_re}\\\$$::decimal = 1" \
-    "back to one thread visible in \$_inferior_thread_count"
+gdb_test {with print thread-events on -- p $_inferior_thread_count} \
+    "^${exit_thread_re}$re_var = 1" \
+    {back to one thread visible in $_inferior_thread_count}
 
 # Set a variable in the inferior, this will cause the second thread to
 # exit.
@@ -168,8 +175,14 @@ gdb_test_no_output -no-prompt-anchor "set variable spin = 0" \
 
 # When the second thread exits, the main thread joins with it, and
 # then proceeds to hit the breakpt function again.
+set re_breakpt [string_to_regexp "breakpt ()"]
+set re \
+    [multi_line \
+	 "Thread 1 \[^\r\n\]+ hit Breakpoint $decimal, $re_breakpt\[^\r\n\]+" \
+	 "\[^\r\n\]+" \
+	 ""]
 gdb_test_multiple "" "wait for main thread to stop" {
-    -re "Thread 1 \[^\r\n\]+ hit Breakpoint $decimal, breakpt \\(\\)\[^\r\n\]+\r\n\[^\r\n\]+\r\n" {
+    -re $re {
 	pass $gdb_test_name
     }
 }
-- 
2.43.0


  parent reply	other threads:[~2025-04-18  9:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-18  9:23 [PATCH 0/2] [gdb/testsuite] Fixes in gdb.threads/inf-thr-count.exp Tom de Vries
2025-04-18  9:23 ` [PATCH 1/2] [gdb/testsuite] Fix timeout " Tom de Vries
2025-04-18  9:23 ` Tom de Vries [this message]
2025-04-18  9:39   ` [PATCH 2/2] [gdb/testsuite] Make gdb.threads/inf-thr-count.exp more readable Andreas Schwab
2025-04-18 10:47     ` Tom de Vries
2025-04-18 16:57       ` Tom Tromey
2025-05-02  8:37         ` Tom de Vries
2025-05-02  8:31       ` 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=20250418092356.4579-3-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