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/3] [gdb/testsuite] Kill left-over process in gdb.base/add-symbol-file-attach.exp
Date: Mon, 17 Feb 2025 17:36:18 +0100	[thread overview]
Message-ID: <20250217163619.8662-3-tdevries@suse.de> (raw)
In-Reply-To: <20250217163619.8662-1-tdevries@suse.de>

When running test-case gdb.base/add-symbol-file-attach.exp, the process
spawned by:
...
set test_spawn_id [spawn_wait_for_attach $binfile]
...
is not cleaned up.

The usual way to fix this is to add "kill_wait_spawned_process $test_spawn_id"
at the end of the test-case.

However, that doesn't look robust enough to me: in case of running into some
form of trouble in the test-case, that cleanup may not be reached.

I considered adding a wrapper proc like save_vars, but that makes fixing this
a bit intrusive since it requires re-indenting most of the test-case.

Instead, add a new proc at_finish_kill_wait:
...
 set test_spawn_id [spawn_wait_for_attach $binfile]
+at_finish_kill_wait $test_spawn_id
...
that schedules the cleanup for test-case finish.

While we're at it, make sure that we check for the result of
spawn_wait_for_attach, and since this:
...
UNSUPPORTED: $exp: require failed: !string equal $test_spawn_id "-1"
...
is somewhat hard to parse, add proc valid_spawn_id to get the more readable:
...
UNSUPPORTED: $exp: require failed: valid_spawn_id $test_spawn_id
...

Tested on x86_64-linux.

PR testsuite/32709
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32709
---
 .../gdb.base/add-symbol-file-attach.exp       |  3 +++
 gdb/testsuite/lib/gdb.exp                     | 21 +++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gdb/testsuite/gdb.base/add-symbol-file-attach.exp b/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
index e39a26ab666..97e0e018fc7 100644
--- a/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
+++ b/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
@@ -44,6 +44,9 @@ if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
 # so that GDB may attach to it.
 
 set test_spawn_id [spawn_wait_for_attach $binfile]
+require {valid_spawn_id $test_spawn_id}
+at_finish_kill_wait $test_spawn_id
+
 set testpid [spawn_id_get_pid $test_spawn_id]
 
 gdb_start
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index d4634c85725..7c2d185b690 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -101,6 +101,16 @@ append_environment_default ASAN_OPTIONS detect_leaks 0
 # List of procs to run in gdb_finish.
 set gdb_finish_hooks [list]
 
+# List of spawn_ids to kill-and-wait-for-exit in gdb_finish.
+set gdb_finish_kill_wait {}
+
+# Schedule each arg for kill-and-wait-for-exit in gdb_finish.
+proc at_finish_kill_wait { args } {
+    foreach arg $args {
+	lappend ::gdb_finish_kill_wait $arg
+    }
+}
+
 # Variable in which we keep track of globals that are allowed to be live
 # across test-cases.
 array set gdb_persistent_globals {}
@@ -6913,6 +6923,12 @@ proc spawn_wait_for_attach { executable_list } {
     return [spawn_wait_for_attach_1 $executable_list]
 }
 
+# Return 1 if SPAWN_ID is a valid spawn id.
+
+proc valid_spawn_id { spawn_id } {
+    return [expr ![string equal $spawn_id "-1"]]
+}
+
 #
 # gdb_load_cmd -- load a file into the debugger.
 #		  ARGS - additional args to load command.
@@ -7884,6 +7900,11 @@ proc gdb_finish { } {
     set gdb_finish_hooks [list]
 
     gdb_cleanup_globals
+
+    foreach spawn_id $::gdb_finish_kill_wait {
+	kill_wait_spawned_process $spawn_id
+    }
+    set ::gdb_finish_kill_wait {}
 }
 
 global debug_format
-- 
2.43.0


  parent reply	other threads:[~2025-02-17 16:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-17 16:36 [PATCH 0/3] [gdb/testsuite] Clean up left-over processes Tom de Vries
2025-02-17 16:36 ` [PATCH 1/3] [gdb/testsuite] Don't start gdb in gdb.base/gstack.exp Tom de Vries
2025-02-17 17:20   ` Keith Seitz
2025-02-18  7:59     ` Tom de Vries
2025-02-17 16:36 ` Tom de Vries [this message]
2025-02-24 17:26   ` [PATCH 2/3] [gdb/testsuite] Kill left-over process in gdb.base/add-symbol-file-attach.exp Tom Tromey
2025-02-24 17:57     ` Tom de Vries
2025-02-24 18:05       ` Tom Tromey
2025-02-24 18:43         ` Tom de Vries
2025-02-17 16:36 ` [PATCH 3/3] [gdb/testsuite] Exit left-over gdb in gdb.mi/mi-break.exp Tom de Vries
2025-02-24 17:28   ` Tom Tromey

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=20250217163619.8662-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