binutils-gdb/gdb/testsuite/gdb.base/fork-running-state.exp
Pedro Alves 873657b9e8 Preserve selected thread in all-stop w/ background execution
In non-stop mode, if you resume the program in the background (with
"continue&", for example), then gdb makes sure to not switch the
current thread behind your back.  That means that you can be sure that
the commands you type apply to the thread you selected, even if some
other thread that was running in the background hits some event just
while you're typing.

In all-stop mode, however, if you resume the program in the
background, gdb let's the current thread switch behind your back.

This is bogus, of course.  All-stop and non-stop background
resumptions should behave the same.

This patch fixes that, and adds a testcase that exposes the bad
behavior in current master.

The fork-running-state.exp changes are necessary because that
preexisting testcase was expecting the old behavior:

Before:

  continue &
  Continuing.
  (gdb)
  [Attaching after process 8199 fork to child process 8203]
  [New inferior 2 (process 8203)]
  info threads
    Id   Target Id                      Frame
    1.1  process 8199 "fork-running-st" (running)
  * 2.1  process 8203 "fork-running-st" (running)
  (gdb)

After:

  continue &
  Continuing.
  (gdb)
  [Attaching after process 24660 fork to child process 24664]
  [New inferior 2 (process 24664)]
  info threads
    Id   Target Id                       Frame
  * 1.1  process 24660 "fork-running-st" (running)
    2.1  process 24664 "fork-running-st" (running)
  (gdb)

Here we see that before this patch GDB switches current inferior to
the new inferior behind the user's back, as a side effect of handling
the fork.

The delete_exited_threads call in inferior_appeared is there to fix an
issue that Baris found in a previous version of this patch.  The
fetch_inferior_event change increases the refcount of the current
thread, and in case the fetched inferior event denotes a thread exit,
the thread will not be deleted right away.  A non-deleted but exited
thread stays in the inferior's thread list.  This, in turn, causes the
"init_thread_list" call in inferior.c to be skipped.  A consequence is
that the global thread ID counter is not restarted if the current
thread exits, and then the inferior is restarted:

 (gdb) start
 Temporary breakpoint 1 at 0x4004d6: file main.c, line 21.
 Starting program: /tmp/main

 Temporary breakpoint 1, main () at main.c:21
 21        foo ();
 (gdb) info threads -gid
   Id   GId  Target Id            Frame
 * 1    1    process 16106 "main" main () at main.c:21
 (gdb) c
 Continuing.
 [Inferior 1 (process 16106) exited normally]
 (gdb) start
 Temporary breakpoint 2 at 0x4004d6: file main.c, line 21.
 Starting program: /tmp/main

 Temporary breakpoint 2, main () at main.c:21
 21        foo ();
 (gdb) info threads -gid
   Id   GId  Target Id            Frame
 * 1    2    process 16138 "main" main () at main.c:21
       ^^^

Notice that GId == 2 above.  It should have been "1" instead.

The new tids-git-reset.exp testcase exercises the problem above.

gdb/ChangeLog:
2020-01-10  Pedro Alves  <palves@redhat.com>

	* gdbthread.h (scoped_restore_current_thread)
	<dont_restore, restore, m_dont_restore>: Declare.
	* thread.c (thread_alive): Add assertion.  Return bool.
	(switch_to_thread_if_alive): New.
	(prune_threads): Switch inferior/thread.
	(print_thread_info_1): Switch thread before calling target methods.
	(scoped_restore_current_thread::restore): New, factored out from
	...
	(scoped_restore_current_thread::~scoped_restore_current_thread):
	... this.
	(scoped_restore_current_thread::scoped_restore_current_thread):
	Add assertion.
	(thread_apply_all_command, thread_select): Use
	switch_to_thread_if_alive.

gdb/testsuite/ChangeLog:
2020-01-10  Pedro Alves  <palves@redhat.com>

	* gdb.base/fork-running-state.exp (do_test): Adjust expected
	output.
	* gdb.threads/async.c: New.
	* gdb.threads/async.exp: New.
	* gdb.multi/tids-gid-reset.c: New.
	* gdb.multi/tids-gid-reset.exp: New.
2020-01-10 20:05:41 +00:00

160 lines
4.8 KiB
Plaintext

# Copyright (C) 2016-2020 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 PR threads/19461 (strange "info thread" behavior
# in non-stop). GDB used to miss updating the parent/child running
# states after a fork.
standard_testfile
# The test proper.
proc do_test { detach_on_fork follow_fork non_stop schedule_multiple } {
global GDBFLAGS
global srcfile testfile
global gdb_prompt
save_vars { GDBFLAGS } {
append GDBFLAGS " -ex \"set non-stop $non_stop\""
if {[prepare_for_testing "failed to prepare" \
$testfile $srcfile {debug}] == -1} {
return -1
}
}
if ![runto_main] then {
fail "can't run to main"
return 0
}
# If debugging with target remote, check whether the all-stop
# variant of the RSP is being used. If so, we can't run the
# all-stop tests.
if { [target_info exists gdb_protocol]
&& ([target_info gdb_protocol] == "remote"
|| [target_info gdb_protocol] == "extended-remote")} {
set test "maint show target-non-stop"
gdb_test_multiple "maint show target-non-stop" $test {
-re "(is|currently) on.*$gdb_prompt $" {
}
-re "(is|currently) off.*$gdb_prompt $" {
unsupported "can't issue info threads while target is running"
return 0
}
}
}
# We want to catch "[New inferior ...]" below, to avoid sleeping.
if {$detach_on_fork == "off" || $follow_fork == "child"} {
gdb_test_no_output "set print inferior-events on"
}
gdb_test_no_output "set detach-on-fork $detach_on_fork"
gdb_test_no_output "set follow-fork $follow_fork"
if {$non_stop == "off"} {
gdb_test_no_output "set schedule-multiple $schedule_multiple"
}
# If we're detaching from the parent (or child), then tell it to
# exit itself when its child (or parent) exits. If we stay
# attached, we take care of killing it.
if {$detach_on_fork == "on"} {
gdb_test "print exit_if_relative_exits = 1" " = 1"
}
set test "continue &"
gdb_test_multiple $test $test {
-re "$gdb_prompt " {
pass $test
}
}
if {$detach_on_fork == "off" || $follow_fork == "child"} {
set test "fork child appears"
gdb_test_multiple "" $test {
-re "\\\[New inferior " {
pass $test
}
}
} else {
# All we can do is wait a little bit for the parent to fork.
sleep 1
}
set not_nl "\[^\r\n\]*"
if {$detach_on_fork == "on" && $follow_fork == "child"} {
gdb_test "info threads" \
" 2.1 ${not_nl}\\\(running\\\).*No selected thread.*"
} elseif {$detach_on_fork == "on"} {
gdb_test "info threads" \
"\\\* 1 ${not_nl}\\\(running\\\)"
} elseif {$non_stop == "on" || $schedule_multiple == "on"} {
# Both parent and child should be marked running, and the
# parent should be selected.
gdb_test "info threads" \
[multi_line \
"\\\* 1.1 ${not_nl} \\\(running\\\)${not_nl}" \
" 2.1 ${not_nl} \\\(running\\\)"]
} else {
set test "only $follow_fork marked running"
gdb_test_multiple "info threads" $test {
-re "\\\(running\\\)${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
fail $test
}
-re "\\\* 1.1 ${not_nl}\\\(running\\\)\r\n 2.1 ${not_nl}\r\n$gdb_prompt $" {
gdb_assert [string eq $follow_fork "parent"] $test
}
-re "\\\* 1.1 ${not_nl}\r\n 2.1 ${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
gdb_assert [string eq $follow_fork "child"] $test
}
}
}
# We don't want to see "Inferior exited" in reaction to the kills.
gdb_test_no_output "set print inferior-events off"
# Kill both parent and child.
if {$detach_on_fork == "off" || $follow_fork == "parent"} {
gdb_test_no_output "kill inferior 1" "kill parent"
}
if {$detach_on_fork == "off" || $follow_fork == "child"} {
gdb_test_no_output "kill inferior 2" "kill child"
}
}
# Exercise all permutations of:
#
# set detach-on-fork off|on
# set follow-fork parent|child
# set non-stop on|off
# set schedule-multiple on|off
foreach_with_prefix detach-on-fork {"off" "on"} {
foreach_with_prefix follow-fork {"parent" "child"} {
with_test_prefix "non-stop" {
do_test ${detach-on-fork} ${follow-fork} "on" "-"
}
with_test_prefix "all-stop" {
foreach_with_prefix schedule-multiple {"on" "off"} {
do_test ${detach-on-fork} ${follow-fork} "off" ${schedule-multiple}
}
}
}
}