From f895c8244f61a24d7d8661a033324706940bfef5 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Thu, 24 Apr 2025 20:09:16 +0100 Subject: [PATCH 1/2] Fix test races Change-Id: Ia98a3195282a82fc68104f61cba1216bf0a296a0 --- .../gdb.threads/info-threads-stopped.c | 27 +++++---- .../gdb.threads/info-threads-stopped.exp | 56 ++++++++++--------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/gdb/testsuite/gdb.threads/info-threads-stopped.c b/gdb/testsuite/gdb.threads/info-threads-stopped.c index 2c38ecca074..2c4cd854bb7 100644 --- a/gdb/testsuite/gdb.threads/info-threads-stopped.c +++ b/gdb/testsuite/gdb.threads/info-threads-stopped.c @@ -22,30 +22,29 @@ #define NUM 4 -volatile int should_spin = 1; +static pthread_barrier_t threads_started_barrier; static void -something () +stop_here () { } static void spin () { - while (should_spin) + while (1) usleep (1); } static void * work (void *arg) { - int id = *((int *) arg); + int id = *(int *) arg; - /* Sleep a bit to give the other threads a chance to run. */ - usleep (1); + pthread_barrier_wait (&threads_started_barrier); if (id % 2 == 0) - something (); /* break-here */ + stop_here (); else spin (); @@ -59,20 +58,20 @@ main () alarm (10); pthread_t threads[NUM]; - void *thread_result; int ids[NUM]; + pthread_barrier_init (&threads_started_barrier, NULL, NUM + 1); + for (int i = 0; i < NUM; i++) { - ids[i] = i + 2; - pthread_create (&threads[i], NULL, work, &(ids[i])); + ids[i] = i; + pthread_create (&threads[i], NULL, work, &ids[i]); } - sleep (10); - should_spin = 0; + /* Wait until all threads are seen running. */ + pthread_barrier_wait (&threads_started_barrier); - for (int i = 0; i < NUM; i++) - pthread_join(threads[i], &thread_result); + stop_here (); return 0; } diff --git a/gdb/testsuite/gdb.threads/info-threads-stopped.exp b/gdb/testsuite/gdb.threads/info-threads-stopped.exp index 37d6622697c..d554bd7ef2c 100644 --- a/gdb/testsuite/gdb.threads/info-threads-stopped.exp +++ b/gdb/testsuite/gdb.threads/info-threads-stopped.exp @@ -27,28 +27,29 @@ save_vars { GDBFLAGS } { clean_restart $binfile } -gdb_breakpoint "something" -gdb_run_cmd +if ![runto_main] { + return -1 +} + +gdb_breakpoint "stop_here" +gdb_test_multiple "continue -a&" "" { + -re "Continuing.\r\n$gdb_prompt " { + pass $gdb_test_name + } +} -# Two threads hit the bp. +set expected_hits 3 set fill "\[^\r\n\]+" set num_hits 0 gdb_test_multiple "" "hit the breakpoint" -lbl { - -re "\r\nThread ${fill} hit Breakpoint 1${fill}" { + -re "\r\nThread ${fill} hit Breakpoint ${decimal}," { incr num_hits - if {$num_hits < 2} { + if {$num_hits < $expected_hits} { exp_continue } } - -re "\r\n$gdb_prompt " { - exp_continue - } } -gdb_assert {$num_hits == 2} "two threads hit the bp" - -# We are in non-stop mode. -# Send a simple command to resync the command prompt. -gdb_test "p 42" " = 42" +gdb_assert {$num_hits == $expected_hits} "expected threads hit the bp" # Count the number of running/stopped threads reported # by the "info threads" command. We also capture thread ids @@ -56,36 +57,37 @@ gdb_test "p 42" " = 42" set running_tid "invalid" set stopped_tid "invalid" +set eol "(?=\r\n)" + foreach flag {"" "-stopped"} { set num_running 0 set num_stopped 0 - gdb_test_multiple "info threads $flag" "info threads $flag" { - -re "Id${fill}Target Id${fill}Frame${fill}" { + gdb_test_multiple "info threads $flag" "info threads $flag" -lbl { + -re "Id${fill}Target Id${fill}Frame${fill}${eol}" { exp_continue } - -re "^\r\n. (${decimal})${fill}Thread ${fill}.running." { + -re "^\r\n. (${decimal})${fill}Thread ${fill}.running.${eol}" { incr num_running set running_tid $expect_out(1,string) exp_continue } - -re "^\r\n. (${decimal})${fill}Thread ${fill}something ${fill}${srcfile}:${decimal}" { + -re "^\r\n. (${decimal})${fill}Thread ${fill}stop_here ${fill}${srcfile}:${decimal}${fill}${eol}" { incr num_stopped set stopped_tid $expect_out(1,string) exp_continue } -re "^\r\n$gdb_prompt $" { - gdb_assert {$num_stopped == 2} "$gdb_test_name: num stopped" - if {$flag eq ""} { - gdb_assert {$num_running == 3} "$gdb_test_name: num running" - } else { - gdb_assert {$num_running == 0} "$gdb_test_name: num running" - } + pass $gdb_test_name } } } -gdb_assert {$running_tid != "invalid"} "found a running thread" -gdb_assert {$stopped_tid != "invalid"} "found a stopped thread" +gdb_assert {$num_stopped == 3} "num stopped" +if {$flag eq ""} { + gdb_assert {$num_running == 2} "num running" +} else { + gdb_assert {$num_running == 0} "num running" +} # Test specifying thread ids. gdb_test "info threads -stopped $running_tid" \ @@ -97,11 +99,11 @@ set ws "\[ \t\]+" gdb_test "info threads -stopped $stopped_tid" \ [multi_line \ "${ws}Id${ws}Target Id${ws}Frame${ws}" \ - "${ws}${stopped_tid}${ws}Thread ${fill} something ${fill}"] \ + "${ws}${stopped_tid}${ws}Thread ${fill} stop_here ${fill}"] \ "info thread -stopped for a stopped thread" gdb_test "info threads -stopped $running_tid $stopped_tid" \ [multi_line \ "${ws}Id${ws}Target Id${ws}Frame${ws}" \ - "${ws}${stopped_tid}${ws}Thread ${fill} something ${fill}"] \ + "${ws}${stopped_tid}${ws}Thread ${fill} stop_here ${fill}"] \ "info thread -stopped for a running and a stopped thread" base-commit: e32b976a152894c00a01e4e0a48cd1f1d82d1d39 prerequisite-patch-id: 38786aad181a17816a63708dcae88f8bffed5469 prerequisite-patch-id: f64471d0b4bbbb7cb787ae826e654baf8c1a5bf3 -- 2.49.0