xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/continue-pending-status.exp (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1# Copyright (C) 2015-2020 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# This test exercises the case of stopping for a breakpoint hit of one
17# thread, then switching to a thread that has a status pending and
18# continuing.
19
20if [target_info exists gdb,nointerrupts] {
21    verbose "Skipping continue-pending-status.exp because of nointerrupts."
22    return
23}
24
25standard_testfile
26
27if [prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] {
28    return -1
29}
30
31if ![runto_main] {
32    untested "could not run to main"
33    return -1
34}
35
36set break_line [gdb_get_line_number "break here"]
37
38# Return current thread's number.
39
40proc get_current_thread {} {
41    global gdb_prompt
42
43    set thread ""
44    set msg "get thread number"
45    gdb_test_multiple "print /x \$_thread" $msg {
46	-re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
47	    set thread $expect_out(1,string)
48	    pass "$msg"
49	}
50    }
51    return ${thread}
52}
53
54# There are two threads in the program that are running the same tight
55# loop, where we place a breakpoint.  Sometimes we'll get a breakpoint
56# trigger for thread 2, with the breakpoint event of thread 3 pending,
57# other times the opposite.  The original bug that motivated this test
58# depended on the event thread being the highest numbered thread.  We
59# try the same multiple times, which should cover both threads
60# reporting the event.
61
62set attempts 20
63
64# These track whether we saw events for both threads 2 and 3.  If the
65# backend always returns the breakpoint hit for the same thread, then
66# it fails to make sure threads aren't starved, and we'll fail the
67# assert after the loop.
68set saw_thread_2 0
69set saw_thread_3 0
70
71for {set i 0} {$i < $attempts} {incr i} {
72    with_test_prefix "attempt $i" {
73	gdb_test "b $srcfile:$break_line" \
74	    "Breakpoint .* at .*$srcfile, line $break_line.*" \
75	    "set break in tight loop"
76	gdb_test "continue" \
77	    "$srcfile:$break_line.*" \
78	    "continue to tight loop"
79
80	# Switch to the thread that did _not_ report the event (and
81	# thus may have a pending status).  At the time this test was
82	# written this was necessary to make linux-nat.c short-circuit
83	# the resume and go straight to consuming the pending event.
84	set thread [get_current_thread]
85	if {$thread == 2} {
86	    incr saw_thread_2
87	    set thread 3
88	} else {
89	    incr saw_thread_3
90	    set thread 2
91	}
92	gdb_test "thread $thread" \
93	    "Switching to thread $thread .*" \
94	    "switch to non-event thread"
95
96	# Delete all breakpoints so that continuing doesn't switch
97	# back to the event thread to do a step-over, which would mask
98	# away the original bug, which depended on the event thread
99	# still having TARGET_STOPPED_BY_SW_BREAKPOINT stop_reason.
100	delete_breakpoints
101
102	# In the original bug, continuing would trigger an internal
103	# error in the linux-nat.c backend.
104
105	set msg "continue for ctrl-c"
106	gdb_test_multiple "continue" $msg {
107	    -re "Continuing" {
108		pass $msg
109	    }
110	}
111
112	# Wait a bit for GDB to give the terminal to the inferior,
113	# otherwise ctrl-c too soon can result in a "Quit".
114	sleep 1
115	send_gdb "\003"
116
117	set msg "caught interrupt"
118	gdb_test_multiple "" $msg {
119	    -re "Thread .* received signal SIGINT.*$gdb_prompt $" {
120		pass $msg
121	    }
122	}
123    }
124}
125
126verbose -log "saw_thread_2=$saw_thread_2"
127verbose -log "saw_thread_3=$saw_thread_3"
128
129gdb_assert {$saw_thread_2 > 0 && $saw_thread_3 > 0} "no thread starvation"
130