binutils-gdb/gdb/testsuite/gdb.threads/current-lwp-dead.exp
Pedro Alves b7b1008c0b Fix gdb.threads/current-lwp-dead.exp race
If we make GDB report the process EXIT event for the leader thread, as
will be done in a latter patch of this series, then
gdb.threads/current-lwp-dead.exp starts failing:

 (gdb) break fn_return
 Breakpoint 2 at 0x5555555551b5: file /home/pedro/rocm/gdb/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.threads/current-lwp-dead.c, line 45.
 (gdb) continue
 Continuing.
 [New LWP 2138466]
 [Inferior 1 (process 2138459) exited normally]
 (gdb) FAIL: gdb.threads/current-lwp-dead.exp: continue to breakpoint: fn_return (the program exited)

The inferior exit reported is actually correct.  The main thread has
indeed exited, and that's the thread that has the right exit code to
report to the user, as that's the exit code that is reported to the
program's parent.  In this case, GDB managed to collect the exit code
for the leader thread before reaping the other thread, because in
reality, the testcase isn't creating standard threads, it is using raw
clone, and the new clones are put in their own thread group.

Fix it by making the main "thread" not exit until the scenario we're
exercising plays out.  Also, run the program to completion for
completeness.

The original program really wanted the leader thread to exit before
the fn_return function was reached -- it was important that the
current thread as pointed by inferior_ptid was gone when infrun got
the breakpoint event.  I've tweaked the testcase to ensure that that
condition is still held, though it is no longer the main thread that
exits.  This required a bit of synchronization between the threads,
which required using CLONE_VM unconditionally.  The #ifdef guards were
added as a fix for
https://sourceware.org/bugzilla/show_bug.cgi?id=11214, though I don't
think they were necessary because the program is not using TLS.  If it
turns out they were necessary, we can link the testcase with "-z now"
instead, which was mentioned as an alternative workaround in that
Bugzilla.

Change-Id: I7be2f0da4c2fe8f80a60bdde5e6c623d8bd5a0aa
2022-03-10 11:35:53 +00:00

53 lines
1.6 KiB
Plaintext

# This testcase is part of GDB, the GNU debugger.
# Copyright 2009-2022 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/>.
# Regression test for issue originally described here:
#
# https://sourceware.org/legacy-ml/gdb-patches/2009-06/msg00802.html
#
# The relevant code has since been removed from GDB, but it doesn't
# hurt to keep the testcase.
standard_testfile
# This only works with on Linux targets.
if ![istarget *-*-linux*] then {
return
}
if { [prepare_for_testing "failed to prepare" current-lwp-dead] } {
return -1
}
if {[runto_main] <= 0} {
return -1
}
# Run to "fn" so that thread 2 is made current.
gdb_breakpoint "fn"
gdb_continue_to_breakpoint "fn" ".*do_clone.*"
# Run to thread 3, at a point where thread 2 is gone.
set line [gdb_get_line_number "at-fn_return"]
gdb_breakpoint $line
gdb_continue_to_breakpoint "fn_return" ".*at-fn_return.*"
# Confirm thread 2 is really gone.
gdb_test "info threads 2" "No threads match '2'\\."
gdb_continue_to_end "" continue 1