]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.server/target-exec-file.exp
408635387853722f2559a59313afaa125eb39d58
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.server / target-exec-file.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2023 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Test GDB's handling of using a file with a 'target:' prefix as the
19 # executable file. This test includes checking what happens when the
20 # file on the target system changes and GDB needs to reload it.
21
22 load_lib gdbserver-support.exp
23
24 require allow_gdbserver_tests !use_gdb_stub
25
26 standard_testfile
27
28 if { [build_executable "failed to prepare" $testfile $srcfile debug] } {
29 return -1
30 }
31
32 clean_restart
33
34 # Some boards specifically set the sysroot to the empty string to
35 # avoid copying files from the target. But for this test we do want
36 # to copy files from the target, so set the sysroot back to 'target:'.
37 #
38 # This is fine so long as we're not using a board file that sets the
39 # sysroot to something else -- but none of the standard boards do
40 # this, and plenty of other tests mess with the sysroot, so I guess we
41 # don't worry about that too much.
42 gdb_test "set sysroot target:" ".*"
43
44 # Make sure we're disconnected, in case we're testing with an
45 # extended-remote board, therefore already connected.
46 gdb_test "disconnect" ".*"
47
48 # Ensure the executable is on the target.
49 set target_exec [gdb_remote_download target $binfile]
50
51 # We're going to be restarting the inferior. Lets ask GDB not to
52 # prompt us if this is the right thing to do.
53 gdb_test_no_output "set confirm off"
54
55 if { [allow_python_tests] } {
56 # Register an event handler for the executable changed event.
57 # This handler just copies the event into a global Python object.
58 gdb_test_multiline "Add connection_removed event" \
59 "python" "" \
60 "global_exec_changed_event = None" "" \
61 "def executable_changed(event):" "" \
62 " global global_exec_changed_event" "" \
63 " global_exec_changed_event = event" "" \
64 "gdb.events.executable_changed.connect (executable_changed)" "" \
65 "end" ""
66 }
67
68 # Start gdbserver, but always in extended-remote mode, and then
69 # connect to it from GDB.
70 set res [gdbserver_start "--multi" $target_exec]
71 set gdbserver_protocol "extended-remote"
72 set gdbserver_gdbport [lindex $res 1]
73 gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
74
75 if { [allow_python_tests] } {
76 # When connecting to a remote target, if the user has not told GDB
77 # which executable to use, then GDB will figure out an executable
78 # from the remote target.
79 #
80 # As a result we expect to have seen an executable changed event.
81 with_test_prefix "after connecting" {
82 gdb_test "python print(global_exec_changed_event)" \
83 "<gdb.ExecutableChangedEvent object at $hex>"
84 gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \
85 [string_to_regexp target:$target_exec]
86 gdb_test "python print(global_exec_changed_event.reload)" "False"
87 gdb_test_no_output "python global_exec_changed_event = None"
88 }
89 }
90
91 # Issue a 'file' command and parse the output. We look for a couple
92 # of specific things to ensure that we are correctly reading the exec
93 # from the remote target.
94 set saw_read_of_remote_exec false
95 set saw_read_of_syms_from_exec false
96 gdb_test_multiple "file target:$target_exec" "run file command" {
97 -re "^file target:\[^\r\n\]+\r\n" {
98 exp_continue
99 }
100
101 -re "^Reading (\[^\r\n\]+) from remote target\\.\\.\\.\r\n" {
102 set filename $expect_out(1,string)
103 if { $filename eq $target_exec } {
104 set saw_read_of_remote_exec true
105 }
106 exp_continue
107 }
108
109 -re "^warning: File transfers from remote targets can be slow\[^\r\n\]+\r\n" {
110 exp_continue
111 }
112
113 -re "^Reading symbols from target:(\[^\r\n\]+)\\.\\.\\.\r\n" {
114 set filename $expect_out(1,string)
115 if { $filename eq $target_exec } {
116 set saw_read_of_syms_from_exec true
117 }
118 exp_continue
119 }
120
121 -re "^Expanding full symbols from \[^\r\n\]+\r\n" {
122 exp_continue
123 }
124
125 -re "^$gdb_prompt $" {
126 pass $gdb_test_name
127 }
128 }
129
130 gdb_assert { $saw_read_of_remote_exec } \
131 "exec was read from the remote target"
132
133 gdb_assert { $saw_read_of_syms_from_exec } \
134 "symbols were read from remote exec file"
135
136 if { [allow_python_tests] } {
137 # The 'file' command forces GDB to always load the executable,
138 # even if the same filename is used. In this case, as the
139 # filename is the same, this will show as a reload event.
140 with_test_prefix "after 'file' command" {
141 gdb_test "python print(global_exec_changed_event)" \
142 "<gdb.ExecutableChangedEvent object at $hex>"
143 gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \
144 [string_to_regexp target:$target_exec]
145 gdb_test "python print(global_exec_changed_event.reload)" "True"
146 gdb_test_no_output "python global_exec_changed_event = None"
147 }
148 }
149
150 # Start the inferior (with the 'start' command), use TESTNAME for any
151 # pass/fail calls. EXPECT_REREAD should be true or false and
152 # indicates if we expect to too a line like:
153 #
154 # `FILE' has changed; re-reading symbols.
155 proc start_inferior { testname expect_reread } {
156 with_test_prefix $testname {
157 if { [gdb_start_cmd] < 0 } {
158 fail "start command"
159 return -1
160 }
161
162 set saw_reread false
163 gdb_test_multiple "" "stopped at main" {
164 -re "^start\\s*\r\n" {
165 exp_continue
166 }
167 -re "^`\[^\r\n\]+' has changed; re-reading symbols\\.\r\n" {
168 set saw_reread true
169 exp_continue
170 }
171 -re "^Reading \[^\r\n\]+ from remote target\\.\\.\\.\r\n" {
172 exp_continue
173 }
174 -re "^Expanding full symbols from \[^\r\n\]+\\.\\.\\.\r\n" {
175 exp_continue
176 }
177 -re "^Temporary breakpoint $::decimal at $::hex: \[^\r\n\]+\r\n" {
178 exp_continue
179 }
180 -re "^Starting program: \[^\r\n\]+\r\n" {
181 exp_continue
182 }
183 -re "^\\s*\r\n" {
184 exp_continue
185 }
186 -re "^Temporary breakpoint $::decimal, main \\(\\) at .*$::gdb_prompt $" {
187 pass $testname
188 }
189 }
190
191 gdb_assert { $expect_reread == $saw_reread } \
192 "check symbol re-read behaviour"
193 }
194 }
195
196 # Start the inferior for the first time. The symbols were already
197 # read from the file when the 'file' command was used, we should not
198 # see the symbols re-read now.
199 start_inferior "start inferior the first time" false
200
201 if { [allow_python_tests] } {
202 # The executable hasn't changed.
203 with_test_prefix "after starting inferior for the first time" {
204 gdb_test "python print(global_exec_changed_event)" "None"
205 }
206 }
207
208 # Re-start the inferior. The executable is unchanged so we should not
209 # see the symbol file being re-read.
210 start_inferior "start inferior a second time" false
211
212 if { [allow_python_tests] } {
213 # The executable still hasn't changed.
214 with_test_prefix "after starting inferior for the second time" {
215 gdb_test "python print(global_exec_changed_event)" "None"
216 }
217 }
218
219 # Delay for a short while so, when we touch the exec, we know the
220 # timestamp will change.
221 sleep 1
222 set res [remote_exec target "touch $target_exec"]
223 set status [lindex $res 0]
224 if { $status != 0 } {
225 fail "touching executable on target"
226 return -1
227 }
228
229 # Start the inferior again, we expect to see the symbols being re-read
230 # from the remote file.
231 start_inferior "start inferior a third time" true
232
233 if { [allow_python_tests] } {
234 # The executable has now changed on disk. This will be a reload
235 # event.
236 with_test_prefix "after starting inferior for the third time" {
237 gdb_test "python print(global_exec_changed_event)" \
238 "<gdb.ExecutableChangedEvent object at $hex>"
239 gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \
240 [string_to_regexp target:$target_exec]
241 gdb_test "python print(global_exec_changed_event.reload)" "True"
242 gdb_test_no_output "python global_exec_changed_event = None"
243 }
244 }