binutils-gdb/gdb/testsuite/gdb.threads
Simon Marchi 152a174956 gdb: prune inferiors at end of fetch_inferior_event, fix intermittent failure of gdb.threads/fork-plus-threads.exp
This test sometimes fail like this:

    info threads^M
      Id   Target Id         Frame ^M
      11.12 process 2270719   Couldn't get registers: No such process.^M
    (gdb) FAIL: gdb.threads/fork-plus-threads.exp: detach-on-fork=off: no threads left
    [Inferior 11 (process 2270719) exited normally]^M
    info inferiors^M
      Num  Description       Connection           Executable        ^M
    * 1    <null>                                 /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/fork-plus-threads/fork-plus-threads ^M
      11   <null>                                 /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.threads/fork-plus-threads/fork-plus-threads ^M
    (gdb) FAIL: gdb.threads/fork-plus-threads.exp: detach-on-fork=off: only inferior 1 left (the program exited)

I can get it to fail quite reliably by pinning it to a core:

  $ taskset -c 5 make check TESTS="gdb.threads/fork-plus-threads.exp"

The previous attempt at fixing this was:

  https://sourceware.org/pipermail/gdb-patches/2021-October/182846.html

What we see is part due to a possible unfortunate ordering of events
given by the kernel, and what could be considered a bug in GDB.

The test program makes a number of forks, waits them all, then exits.
Most of the time, GDB will get and process the exit event for inferior 1
after the exit events of all the children.  But this is not guaranteed.
After the last child exits and is waited by the parent, the parent can
exit quickly, such that GDB collects from the kernel the exit events for
the parent and that child at the same time.  It then chooses one event
at random, which can be the event for the parent.  This will result in
the parent appearing to exit before its child.  There's not much we can
do about it, so I think we have to adjust the test to cope.

After expect has seen the "exited normally" notification for inferior 1,
it immediately does an "info thread" that it expects to come back empty.
But at this point, GDB might not have processed inferior 11's (the last
child) exit event, so it will look like there is still a thread.  Of
course that thread is dead, we just don't know it yet.  But that makes
the "no thread" test fail.  If the test waited just a bit more for the
"exited normally" notification for inferior 11, then the list of threads
would be empty.

So, first change, make the test collect all the "exited normally"
notifications for all inferiors before proceeding, that should ensure we
see an empty thread list.  That would fix the first FAIL above.

However, we would still have the second FAIL, as we expect inferior 11
to not be there, it should have been deleted automatically.  Inferior 11
is normally deleted when prune_inferiors is called.  That is called by
normal_stop, which is only called by fetch_inferior_event only if the
event thread completed an execution command FSM (thread_fsm).  But the
FSM for the continue command completed when inferior 1 exited.  At that
point inferior 11 was not prunable, as it still had a thread.  When
inferior 11 exits, prune_inferiors is not called.

I think that can be considered a GDB bug.  From the user point of view,
there's no reason why in one case inferior 11 would be deleted and not
in the other case.

This patch makes the somewhat naive change to call prune_inferiors in
fetch_inferior_event, so that it is called in this case.  It is placed
at this particular point in the function so that it is called after the
user inferior / thread selection is restored.  If it was called before
that, inferior 11 wouldn't be pruned, because it would still be the
current inferior.

Change-Id: I48a15d118f30b1c72c528a9f805ed4974170484a
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=26272
2022-04-22 13:40:57 -04:00
..
access-mem-running-thread-exit.c
access-mem-running-thread-exit.exp
async.c
async.exp
attach-into-signal.c
attach-into-signal.exp
attach-many-short-lived-threads.c
attach-many-short-lived-threads.exp
attach-non-stop.c
attach-non-stop.exp
attach-slow-waitpid.c
attach-slow-waitpid.exp
attach-stopped.c
attach-stopped.exp
bp_in_thread.c
bp_in_thread.exp
break-while-running.c
break-while-running.exp
check-libthread-db.c
check-libthread-db.exp
clone-attach-detach.c
clone-attach-detach.exp
clone-new-thread-event.c
clone-new-thread-event.exp
clone-thread_db.c
clone-thread_db.exp
continue-pending-after-query.c
continue-pending-after-query.exp
continue-pending-status.c
continue-pending-status.exp
corethreads.c
corethreads.exp
create-fail.c
create-fail.exp
current-lwp-dead.c
current-lwp-dead.exp
detach-step-over.c
detach-step-over.exp
dlopen-libpthread-lib.c
dlopen-libpthread.c
dlopen-libpthread.exp
execl1.c
execl.c
execl.exp
fork-child-threads.c
fork-child-threads.exp
fork-plus-threads.c
fork-plus-threads.exp gdb: prune inferiors at end of fetch_inferior_event, fix intermittent failure of gdb.threads/fork-plus-threads.exp 2022-04-22 13:40:57 -04:00
fork-thread-pending.c
fork-thread-pending.exp
forking-threads-plus-breakpoint.c
forking-threads-plus-breakpoint.exp
gcore-stale-thread.c
gcore-stale-thread.exp
gcore-thread.exp
hand-call-in-threads.c
hand-call-in-threads.exp
hand-call-new-thread.c
hand-call-new-thread.exp
ia64-sigill.c
ia64-sigill.exp
info-threads-cur-sal-2.c
info-threads-cur-sal.c
info-threads-cur-sal.exp
interrupt-while-step-over.c
interrupt-while-step-over.exp
interrupted-hand-call.c
interrupted-hand-call.exp
kill.c
kill.exp
killed-outside.c
killed-outside.exp
killed.c
killed.exp
leader-exit.c
leader-exit.exp
linux-dp.c
linux-dp.exp
local-watch-wrong-thread.c
local-watch-wrong-thread.exp
manythreads.c
manythreads.exp
multi-create-ns-info-thr.exp
multi-create.c
multi-create.exp
multiple-step-overs.c
multiple-step-overs.exp
multiple-successive-infcall.c
multiple-successive-infcall.exp
names.c
names.exp
next-bp-other-thread.c
next-bp-other-thread.exp
next-fork-other-thread.c
next-fork-other-thread.exp
next-while-other-thread-longjmps.c
next-while-other-thread-longjmps.exp
no-unwaited-for-left.c
no-unwaited-for-left.exp
non-ldr-exc-1.c
non-ldr-exc-1.exp
non-ldr-exc-2.c
non-ldr-exc-2.exp
non-ldr-exc-3.c
non-ldr-exc-3.exp
non-ldr-exc-4.c
non-ldr-exc-4.exp
non-ldr-exit.c
non-ldr-exit.exp
non-stop-fair-events.c
non-stop-fair-events.exp
omp-par-scope.c
omp-par-scope.exp
pending-fork-event-detach-ns.c
pending-fork-event-detach-ns.exp
pending-fork-event-detach-touch-file.c
pending-fork-event-detach.c
pending-fork-event-detach.exp
pending-step.c
pending-step.exp
print-threads.c
print-threads.exp
process-dies-while-detaching.c
process-dies-while-detaching.exp
process-dies-while-handling-bp.c
process-dies-while-handling-bp.exp
process-exit-status-is-leader-exit-status.c
process-exit-status-is-leader-exit-status.exp
pthread_cond_wait.c
pthread_cond_wait.exp
pthreads.c
pthreads.exp
queue-signal.c
queue-signal.exp
reconnect-signal.c
reconnect-signal.exp
schedlock-thread-exit.c
schedlock-thread-exit.exp
schedlock.c
schedlock.exp
siginfo-threads.c
siginfo-threads.exp
signal-command-handle-nopass.c
signal-command-handle-nopass.exp
signal-command-multiple-signals-pending.c
signal-command-multiple-signals-pending.exp
signal-delivered-right-thread.c
signal-delivered-right-thread.exp
signal-sigtrap.c
signal-sigtrap.exp
signal-while-stepping-over-bp-other-thread.c
signal-while-stepping-over-bp-other-thread.exp
sigstep-threads.c
sigstep-threads.exp
sigthread.c
sigthread.exp
slow-waitpid.c
staticthreads.c
staticthreads.exp
step-bg-decr-pc-switch-thread.c
step-bg-decr-pc-switch-thread.exp
step-over-exec-execd.c
step-over-exec.c
step-over-exec.exp gdb/testsuite: add text_segment option to gdb_compile 2022-04-18 10:24:48 -04:00
step-over-lands-on-breakpoint.c
step-over-lands-on-breakpoint.exp
step-over-trips-on-watchpoint.c
step-over-trips-on-watchpoint.exp
stepi-random-signal.c
stepi-random-signal.exp
stop-with-handle.c
stop-with-handle.exp
switch-threads.c
switch-threads.exp
thread_check.c
thread_check.exp
thread_events.c
thread_events.exp
thread-execl.c
thread-execl.exp
thread-find.exp
thread-specific-bp.c
thread-specific-bp.exp
thread-specific.c
thread-specific.exp
thread-unwindonsignal.exp
threadapply.c
threadapply.exp
threxit-hop-specific.c
threxit-hop-specific.exp
tid-reuse.c
tid-reuse.exp
tls2.c
tls-core.c
tls-core.exp
tls-main.c
tls-nodebug-pie.c
tls-nodebug-pie.exp
tls-nodebug.c
tls-nodebug.exp
tls-shared.c
tls-shared.exp
tls-so_extern_main.c
tls-so_extern.c
tls-so_extern.exp
tls-var-main.c
tls-var.c
tls-var.exp
tls.c
tls.exp
vfork-follow-child-exec.c
vfork-follow-child-exec.exp
vfork-follow-child-exit.c
vfork-follow-child-exit.exp
vfork-multi-inferior-sleep.c
vfork-multi-inferior.c
vfork-multi-inferior.exp
vfork-multi-thread.c
vfork-multi-thread.exp
watchpoint-fork-child.c
watchpoint-fork-mt.c
watchpoint-fork-parent.c
watchpoint-fork-st.c
watchpoint-fork.exp
watchpoint-fork.h
watchthreads2.c
watchthreads2.exp
watchthreads-reorder.c
watchthreads-reorder.exp
watchthreads.c
watchthreads.exp
wp-replication.c
wp-replication.exp