access-mem-running-thread-exit.exp revision 1.1.1.1.4.2 1 1.1.1.1.4.2 perseant # Copyright (C) 2021-2023 Free Software Foundation, Inc.
2 1.1.1.1.4.2 perseant
3 1.1.1.1.4.2 perseant # This program is free software; you can redistribute it and/or modify
4 1.1.1.1.4.2 perseant # it under the terms of the GNU General Public License as published by
5 1.1.1.1.4.2 perseant # the Free Software Foundation; either version 3 of the License, or
6 1.1.1.1.4.2 perseant # (at your option) any later version.
7 1.1.1.1.4.2 perseant #
8 1.1.1.1.4.2 perseant # This program is distributed in the hope that it will be useful,
9 1.1.1.1.4.2 perseant # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 1.1.1.1.4.2 perseant # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 1.1.1.1.4.2 perseant # GNU General Public License for more details.
12 1.1.1.1.4.2 perseant #
13 1.1.1.1.4.2 perseant # You should have received a copy of the GNU General Public License
14 1.1.1.1.4.2 perseant # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 1.1.1.1.4.2 perseant
16 1.1.1.1.4.2 perseant # Test that we can access memory while all the threads of the inferior
17 1.1.1.1.4.2 perseant # are running, and even if:
18 1.1.1.1.4.2 perseant #
19 1.1.1.1.4.2 perseant # - the leader thread exits
20 1.1.1.1.4.2 perseant # - the selected thread exits
21 1.1.1.1.4.2 perseant #
22 1.1.1.1.4.2 perseant # This test constantly spawns short lived threads to make sure that on
23 1.1.1.1.4.2 perseant # systems with debug APIs that require passing down a specific thread
24 1.1.1.1.4.2 perseant # to work with (e.g., GNU/Linux ptrace and /proc filesystem), GDB
25 1.1.1.1.4.2 perseant # copes with accessing memory just while the thread it is accessing
26 1.1.1.1.4.2 perseant # memory through exits.
27 1.1.1.1.4.2 perseant #
28 1.1.1.1.4.2 perseant # The test spawns two processes and alternates memory accesses between
29 1.1.1.1.4.2 perseant # them to force flushing per-process caches. When the testcase was
30 1.1.1.1.4.2 perseant # originally written, the Linux backend would access inferior memory
31 1.1.1.1.4.2 perseant # via /proc/PID/mem, and kept one such file open, as a cache.
32 1.1.1.1.4.2 perseant # Alternating inferiors would force re-opening such file for a
33 1.1.1.1.4.2 perseant # different process, which would fail if GDB tried to open the file
34 1.1.1.1.4.2 perseant # for a thread that exited. The test thus ensured those reopen/fail
35 1.1.1.1.4.2 perseant # code paths were exercised. Nowadays, GDB keeps one /proc/PID/mem
36 1.1.1.1.4.2 perseant # file open per inferior.
37 1.1.1.1.4.2 perseant
38 1.1.1.1.4.2 perseant standard_testfile
39 1.1.1.1.4.2 perseant
40 1.1.1.1.4.2 perseant if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
41 1.1.1.1.4.2 perseant return -1
42 1.1.1.1.4.2 perseant }
43 1.1.1.1.4.2 perseant
44 1.1.1.1.4.2 perseant # The test proper. NON_STOP indicates whether we're testing in
45 1.1.1.1.4.2 perseant # non-stop, or all-stop mode.
46 1.1.1.1.4.2 perseant
47 1.1.1.1.4.2 perseant proc test { non_stop } {
48 1.1.1.1.4.2 perseant global binfile
49 1.1.1.1.4.2 perseant global gdb_prompt
50 1.1.1.1.4.2 perseant global GDBFLAGS
51 1.1.1.1.4.2 perseant
52 1.1.1.1.4.2 perseant save_vars { GDBFLAGS } {
53 1.1.1.1.4.2 perseant append GDBFLAGS " -ex \"set non-stop $non_stop\""
54 1.1.1.1.4.2 perseant clean_restart ${binfile}
55 1.1.1.1.4.2 perseant }
56 1.1.1.1.4.2 perseant
57 1.1.1.1.4.2 perseant if ![runto_main] {
58 1.1.1.1.4.2 perseant return -1
59 1.1.1.1.4.2 perseant }
60 1.1.1.1.4.2 perseant
61 1.1.1.1.4.2 perseant # If debugging with target remote, check whether the all-stop variant
62 1.1.1.1.4.2 perseant # of the RSP is being used. If so, we can't run the background tests.
63 1.1.1.1.4.2 perseant if {!$non_stop
64 1.1.1.1.4.2 perseant && [target_info exists gdb_protocol]
65 1.1.1.1.4.2 perseant && ([target_info gdb_protocol] == "remote"
66 1.1.1.1.4.2 perseant || [target_info gdb_protocol] == "extended-remote")} {
67 1.1.1.1.4.2 perseant
68 1.1.1.1.4.2 perseant gdb_test_multiple "maint show target-non-stop" "" {
69 1.1.1.1.4.2 perseant -wrap -re "(is|currently) on.*" {
70 1.1.1.1.4.2 perseant }
71 1.1.1.1.4.2 perseant -wrap -re "(is|currently) off.*" {
72 1.1.1.1.4.2 perseant unsupported "can't issue commands while target is running"
73 1.1.1.1.4.2 perseant return 0
74 1.1.1.1.4.2 perseant }
75 1.1.1.1.4.2 perseant }
76 1.1.1.1.4.2 perseant }
77 1.1.1.1.4.2 perseant
78 1.1.1.1.4.2 perseant delete_breakpoints
79 1.1.1.1.4.2 perseant
80 1.1.1.1.4.2 perseant # Start the second inferior.
81 1.1.1.1.4.2 perseant with_test_prefix "second inferior" {
82 1.1.1.1.4.2 perseant # With stub targets that do reload on run, if we let the new
83 1.1.1.1.4.2 perseant # inferior share inferior 1's connection, runto_main would
84 1.1.1.1.4.2 perseant # fail because GDB is already connected to something, like
85 1.1.1.1.4.2 perseant # e.g. with --target_board=native-gdbserver:
86 1.1.1.1.4.2 perseant #
87 1.1.1.1.4.2 perseant # (gdb) kill
88 1.1.1.1.4.2 perseant # ...
89 1.1.1.1.4.2 perseant # (gdb) target remote localhost:2348
90 1.1.1.1.4.2 perseant # Already connected to a remote target. Disconnect? (y or n)
91 1.1.1.1.4.2 perseant #
92 1.1.1.1.4.2 perseant # Instead, start the inferior with no connection, and let
93 1.1.1.1.4.2 perseant # gdb_load/runto_main spawn a new remote connection/gdbserver.
94 1.1.1.1.4.2 perseant #
95 1.1.1.1.4.2 perseant # OTOH, with extended-remote, we must let the new inferior
96 1.1.1.1.4.2 perseant # reuse the current connection, so that runto_main below can
97 1.1.1.1.4.2 perseant # issue the "run" command, and have the inferior run on the
98 1.1.1.1.4.2 perseant # remote target. If we forced no connection, then "run" would
99 1.1.1.1.4.2 perseant # either fail if "set auto-connect-native-target" is on, like
100 1.1.1.1.4.2 perseant # the native-extended-gdbserver board enforces, or it would
101 1.1.1.1.4.2 perseant # run the inferior on the native target, which isn't what is
102 1.1.1.1.4.2 perseant # being tested.
103 1.1.1.1.4.2 perseant #
104 1.1.1.1.4.2 perseant # Since it's reload_on_run targets that need special care, we
105 1.1.1.1.4.2 perseant # default to reusing the connection on most targets.
106 1.1.1.1.4.2 perseant if [target_info exists gdb,do_reload_on_run] {
107 1.1.1.1.4.2 perseant gdb_test "add-inferior -no-connection" "New inferior 2.*"
108 1.1.1.1.4.2 perseant } else {
109 1.1.1.1.4.2 perseant gdb_test "add-inferior" "New inferior 2.*"
110 1.1.1.1.4.2 perseant }
111 1.1.1.1.4.2 perseant gdb_test "inferior 2" "Switching to inferior 2 .*"
112 1.1.1.1.4.2 perseant
113 1.1.1.1.4.2 perseant gdb_load $binfile
114 1.1.1.1.4.2 perseant
115 1.1.1.1.4.2 perseant if ![runto_main] {
116 1.1.1.1.4.2 perseant return -1
117 1.1.1.1.4.2 perseant }
118 1.1.1.1.4.2 perseant }
119 1.1.1.1.4.2 perseant
120 1.1.1.1.4.2 perseant delete_breakpoints
121 1.1.1.1.4.2 perseant
122 1.1.1.1.4.2 perseant # These put too much noise in the logs.
123 1.1.1.1.4.2 perseant gdb_test_no_output "set print thread-events off"
124 1.1.1.1.4.2 perseant
125 1.1.1.1.4.2 perseant # Continue all threads of both processes.
126 1.1.1.1.4.2 perseant gdb_test_no_output "set schedule-multiple on"
127 1.1.1.1.4.2 perseant if {$non_stop == "off"} {
128 1.1.1.1.4.2 perseant set cmd "continue &"
129 1.1.1.1.4.2 perseant } else {
130 1.1.1.1.4.2 perseant set cmd "continue -a &"
131 1.1.1.1.4.2 perseant }
132 1.1.1.1.4.2 perseant gdb_test_multiple $cmd "continuing" {
133 1.1.1.1.4.2 perseant -re "Continuing\.\r\n$gdb_prompt " {
134 1.1.1.1.4.2 perseant pass $gdb_test_name
135 1.1.1.1.4.2 perseant }
136 1.1.1.1.4.2 perseant }
137 1.1.1.1.4.2 perseant
138 1.1.1.1.4.2 perseant # Like gdb_test, but:
139 1.1.1.1.4.2 perseant # - don't issue a pass on success.
140 1.1.1.1.4.2 perseant # - on failure, clear the ok variable in the calling context, and
141 1.1.1.1.4.2 perseant # break it.
142 1.1.1.1.4.2 perseant proc my_gdb_test {cmd pattern message} {
143 1.1.1.1.4.2 perseant upvar inf inf
144 1.1.1.1.4.2 perseant upvar iter iter
145 1.1.1.1.4.2 perseant if {[gdb_test -nopass \
146 1.1.1.1.4.2 perseant $cmd $pattern "access mem ($message, inf=$inf, iter=$iter)"] \
147 1.1.1.1.4.2 perseant != 0} {
148 1.1.1.1.4.2 perseant uplevel 1 {set ok 0}
149 1.1.1.1.4.2 perseant return -code break
150 1.1.1.1.4.2 perseant }
151 1.1.1.1.4.2 perseant }
152 1.1.1.1.4.2 perseant
153 1.1.1.1.4.2 perseant # Hammer away for 5 seconds, alternating between inferiors.
154 1.1.1.1.4.2 perseant set ::done 0
155 1.1.1.1.4.2 perseant after 5000 { set ::done 1 }
156 1.1.1.1.4.2 perseant
157 1.1.1.1.4.2 perseant set inf 1
158 1.1.1.1.4.2 perseant set ok 1
159 1.1.1.1.4.2 perseant set iter 0
160 1.1.1.1.4.2 perseant while {!$::done && $ok} {
161 1.1.1.1.4.2 perseant incr iter
162 1.1.1.1.4.2 perseant verbose -log "xxxxx: iteration $iter"
163 1.1.1.1.4.2 perseant gdb_test -nopass "info threads"
164 1.1.1.1.4.2 perseant
165 1.1.1.1.4.2 perseant if {$inf == 1} {
166 1.1.1.1.4.2 perseant set inf 2
167 1.1.1.1.4.2 perseant } else {
168 1.1.1.1.4.2 perseant set inf 1
169 1.1.1.1.4.2 perseant }
170 1.1.1.1.4.2 perseant
171 1.1.1.1.4.2 perseant my_gdb_test "inferior $inf" ".*" "inferior $inf"
172 1.1.1.1.4.2 perseant
173 1.1.1.1.4.2 perseant my_gdb_test "print global_var = 555" " = 555" \
174 1.1.1.1.4.2 perseant "write to global_var"
175 1.1.1.1.4.2 perseant my_gdb_test "print global_var" " = 555" \
176 1.1.1.1.4.2 perseant "print global_var after writing"
177 1.1.1.1.4.2 perseant my_gdb_test "print global_var = 333" " = 333" \
178 1.1.1.1.4.2 perseant "write to global_var again"
179 1.1.1.1.4.2 perseant my_gdb_test "print global_var" " = 333" \
180 1.1.1.1.4.2 perseant "print global_var after writing again"
181 1.1.1.1.4.2 perseant }
182 1.1.1.1.4.2 perseant
183 1.1.1.1.4.2 perseant if {$ok} {
184 1.1.1.1.4.2 perseant pass "access mem"
185 1.1.1.1.4.2 perseant }
186 1.1.1.1.4.2 perseant }
187 1.1.1.1.4.2 perseant
188 1.1.1.1.4.2 perseant foreach non_stop { "off" "on" } {
189 1.1.1.1.4.2 perseant set stop_mode [expr ($non_stop=="off")?"all-stop":"non-stop"]
190 1.1.1.1.4.2 perseant with_test_prefix "$stop_mode" {
191 1.1.1.1.4.2 perseant test $non_stop
192 1.1.1.1.4.2 perseant }
193 1.1.1.1.4.2 perseant }
194