xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.threads/step-bg-decr-pc-switch-thread.exp (revision 798021b4d6f148a9f5df94f78cc2c6ff5708e950)
1# Copyright (C) 2014-2024 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# On decr_pc_after_break targets, GDB used to adjust the PC
17# incorrectly if a background single-step stopped somewhere where
18# PC-$decr_pc had a breakpoint, and the thread was not the current
19# thread, like:
20#
21#   ADDR1 nop <-- breakpoint here
22#   ADDR2 jmp PC
23#
24#  IOW, say thread A is stepping ADDR2's line in the background (an
25#  infinite loop), and the user switches focus to thread B.  GDB's
26#  adjust_pc_after_break logic would confuse the single-step stop of
27#  thread A for a hit of the breakpoint at ADDR1, and thus adjust
28#  thread A's PC to point at ADDR1 when it should not: the thread had
29#  been single-stepped, not continued.
30
31standard_testfile
32
33if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
34    return -1
35}
36
37if ![runto_main] {
38    return
39}
40
41# Make sure it's GDB's decr_pc logic that's being tested, not the
42# target's.
43gdb_test_no_output "set range-stepping off"
44
45delete_breakpoints
46
47gdb_breakpoint [gdb_get_line_number "set breakpoint here"]
48gdb_continue_to_breakpoint "run to nop breakpoint"
49gdb_test "info threads" "  1 .*\\\* 2 .*" "info threads shows all threads"
50
51gdb_test "next" "while.*" "next over nop"
52
53gdb_test_no_output "next&" "next& over inf loop"
54
55set test "switch to main thread"
56gdb_test_multiple "thread 1" $test {
57    -re "Cannot execute this command while the target is running.*$gdb_prompt $" {
58
59	# With remote targets, we can't send any other remote packet
60	# until the target stops.  Switching thread wants to ask the
61	# remote side whether the thread is alive.
62	unsupported $gdb_test_name
63
64	# Interrupt running target to allow subsequent "monitor exit" to
65	# succeed.
66	gdb_test_multiple interrupt "" {
67	    -re -wrap "" {
68		exp_continue
69	    }
70	    -re "received signal SIGINT, Interrupt\\.\r\n.*" {
71		pass $gdb_test_name
72	    }
73	}
74	return
75    }
76    -re "Switching to thread 1.*\\(running\\)\r\n$gdb_prompt " {
77	# Prefer to match the prompt without an anchor.  If there's a
78	# bug and output comes after the prompt immediately, it's
79	# faster to handle that in the following test, instead of
80	# waiting for a timeout here.
81	pass $test
82    }
83}
84
85# Wait a bit.  Use gdb_expect instead of sleep so that any (bad) GDB
86# output is visible in the log.
87gdb_expect 4 {}
88
89set test "no output while stepping"
90gdb_test_multiple "" $test {
91    -timeout 1
92    timeout {
93	pass $test
94    }
95    -re "." {
96	# If we see any output, it's a failure.  On the original bug,
97	# this would be a breakpoint hit.
98	fail $test
99    }
100}
101