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