]>
Commit | Line | Data |
---|---|---|
213516ef | 1 | # Copyright 1997-2023 Free Software Foundation, Inc. |
74cf1395 JM |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
74cf1395 | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
74cf1395 JM |
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. | |
e22f8b7c | 12 | # |
74cf1395 | 13 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
74cf1395 | 15 | |
60b3033e | 16 | if {![can_spawn_for_attach]} { |
74cf1395 JM |
17 | return 0 |
18 | } | |
19 | ||
48e9cc84 | 20 | standard_testfile attach.c attach2.c attach3.c |
a64d2530 | 21 | set binfile2 ${binfile}2 |
48e9cc84 | 22 | set binfile3 ${binfile}3 |
a64d2530 | 23 | set escapedbinfile [string_to_regexp $binfile] |
74cf1395 JM |
24 | |
25 | #execute_anywhere "rm -f ${binfile} ${binfile2}" | |
48e9cc84 | 26 | remote_exec build "rm -f ${binfile} ${binfile2} ${binfile3}" |
74cf1395 JM |
27 | # For debugging this test |
28 | # | |
29 | #log_user 1 | |
30 | ||
74cf1395 JM |
31 | # build the first test case |
32 | # | |
33 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { | |
84c93cd5 | 34 | untested "failed to compile" |
b60f0898 | 35 | return -1 |
74cf1395 JM |
36 | } |
37 | ||
74cf1395 JM |
38 | # Build the in-system-call test |
39 | ||
40 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } { | |
84c93cd5 | 41 | untested "failed to compile in-system-call test" |
b60f0898 | 42 | return -1 |
74cf1395 JM |
43 | } |
44 | ||
48e9cc84 PW |
45 | # Build the third file, used to check attach when the exec-file has changed. |
46 | ||
47 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile3}" "${binfile3}" executable {debug}] != "" } { | |
48 | untested "failed to compile attach exec-file changed test" | |
49 | return -1 | |
50 | } | |
51 | ||
df0da8a2 AH |
52 | # This is a test of the error cases for gdb's ability to attach to a |
53 | # running process. | |
54 | ||
55 | proc_with_prefix do_attach_failure_tests {} { | |
1279f4ff AC |
56 | global gdb_prompt |
57 | global binfile | |
58 | global escapedbinfile | |
59 | global srcfile | |
df0da8a2 AH |
60 | |
61 | clean_restart $binfile | |
62 | ||
1586c8fb GB |
63 | # Figure out a regular expression that will match the sysroot, |
64 | # noting that the default sysroot is "target:", and also noting | |
65 | # that GDB will strip "target:" from the start of filenames when | |
d4d38844 YQ |
66 | # operating on the local filesystem. However the default sysroot |
67 | # can be set via configure option --with-sysroot, which can be "/". | |
68 | # If $binfile is a absolute path, so pattern | |
69 | # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make | |
70 | # $sysroot simple. | |
71 | set sysroot "\[^\r\n\]*" | |
1586c8fb | 72 | |
1279f4ff AC |
73 | # Start the program running and then wait for a bit, to be sure |
74 | # that it can be attached to. | |
75 | ||
2c8c5d37 PA |
76 | set test_spawn_id [spawn_wait_for_attach $binfile] |
77 | set testpid [spawn_id_get_pid $test_spawn_id] | |
1279f4ff AC |
78 | |
79 | # Verify that we cannot attach to nonsense. | |
80 | ||
8b1b3228 AC |
81 | set test "attach to nonsense is prohibited" |
82 | gdb_test_multiple "attach abc" "$test" { | |
74164c56 JK |
83 | -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" { |
84 | pass "$test" | |
85 | } | |
86 | -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { | |
87 | # Response expected from /proc-based systems. | |
88 | pass "$test" | |
89 | } | |
90 | -re "Can't attach to process..*$gdb_prompt $" { | |
91 | # Response expected on Cygwin | |
92 | pass "$test" | |
93 | } | |
94 | -re "Attaching to.*$gdb_prompt $" { | |
95 | fail "$test (bogus pid allowed)" | |
96 | } | |
97 | } | |
98 | ||
99 | # Verify that we cannot attach to nonsense even if its initial part is | |
100 | # a valid PID. | |
101 | ||
102 | set test "attach to digits-starting nonsense is prohibited" | |
103 | gdb_test_multiple "attach ${testpid}x" "$test" { | |
104 | -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" { | |
8b1b3228 | 105 | pass "$test" |
1279f4ff AC |
106 | } |
107 | -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { | |
108 | # Response expected from /proc-based systems. | |
8b1b3228 | 109 | pass "$test" |
1279f4ff | 110 | } |
8b1b3228 | 111 | -re "Can't attach to process..*$gdb_prompt $" { |
1279f4ff | 112 | # Response expected on Cygwin |
8b1b3228 | 113 | pass "$test" |
1279f4ff AC |
114 | } |
115 | -re "Attaching to.*$gdb_prompt $" { | |
8b1b3228 | 116 | fail "$test (bogus pid allowed)" |
1279f4ff AC |
117 | } |
118 | } | |
119 | ||
120 | # Verify that we cannot attach to what appears to be a valid | |
121 | # process ID, but is a process that doesn't exist. Traditionally, | |
122 | # most systems didn't have a process with ID 0, so we take that as | |
123 | # the default. However, there are a few exceptions. | |
124 | ||
125 | set boguspid 0 | |
126 | if { [istarget "*-*-*bsd*"] } { | |
127 | # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead | |
128 | # (which should have the desired effect on any version of | |
129 | # FreeBSD, and probably other *BSD's too). | |
130 | set boguspid -1 | |
131 | } | |
8b1b3228 AC |
132 | set test "attach to nonexistent process is prohibited" |
133 | gdb_test_multiple "attach $boguspid" "$test" { | |
1279f4ff AC |
134 | -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" { |
135 | # Response expected on ptrace-based systems (i.e. HP-UX 10.20). | |
8b1b3228 | 136 | pass "$test" |
1279f4ff AC |
137 | } |
138 | -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" { | |
139 | # Response expected on ttrace-based systems (i.e. HP-UX 11.0). | |
8b1b3228 | 140 | pass "$test" |
1279f4ff AC |
141 | } |
142 | -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" { | |
8b1b3228 | 143 | pass "$test" |
1279f4ff AC |
144 | } |
145 | -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" { | |
8b1b3228 | 146 | pass "$test" |
1279f4ff AC |
147 | } |
148 | -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { | |
149 | # Response expected from /proc-based systems. | |
8b1b3228 | 150 | pass "$test" |
1279f4ff | 151 | } |
8b1b3228 | 152 | -re "Can't attach to process..*$gdb_prompt $" { |
1279f4ff | 153 | # Response expected on Cygwin |
8b1b3228 | 154 | pass "$test" |
1279f4ff | 155 | } |
7cee1e54 PA |
156 | -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" { |
157 | # Response expected on the extended-remote target. | |
158 | pass "$test" | |
159 | } | |
1279f4ff AC |
160 | } |
161 | ||
df0da8a2 AH |
162 | # Verify that we can't double attach to the process. |
163 | ||
164 | set test "first attach" | |
165 | gdb_test_multiple "attach $testpid" "$test" { | |
166 | -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" { | |
167 | pass "$test" | |
168 | } | |
169 | -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { | |
170 | # Response expected on Cygwin. | |
171 | pass "$test" | |
172 | } | |
173 | } | |
174 | ||
175 | gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2" | |
176 | gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2" | |
177 | ||
4b0cf3d6 SM |
178 | # Probe this before the failing attach: the failed attach against GDBserver |
179 | # currently leaves the extended-remote target in a bad state. | |
180 | set do_kfail [target_is_gdbserver] | |
181 | ||
df0da8a2 AH |
182 | set test "fail to attach again" |
183 | gdb_test_multiple "attach $testpid" "$test" { | |
184 | -re "Attaching to process $testpid.*warning: process .* is already traced by process .*$gdb_prompt $" { | |
185 | pass "$test" | |
186 | } | |
187 | -re "Attaching to process .* failed.*$gdb_prompt $" { | |
188 | # Response expected when using gdbserver. | |
189 | pass "$test" | |
190 | } | |
191 | } | |
192 | ||
4b0cf3d6 SM |
193 | # To ensure the target is still alive and working after this, try to run |
194 | # inferior 1. | |
195 | gdb_test_no_output "set confirm off" | |
df0da8a2 | 196 | gdb_test "inferior 1" "Switching to inferior 1.*" "switch to inferior 1" |
4b0cf3d6 SM |
197 | |
198 | if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" } | |
199 | gdb_test "kill" "killed.*" "exit after attach failures" | |
200 | ||
201 | # This can probably be replaced with a call to runto or runto_main once | |
202 | # the kfail is removed. | |
203 | gdb_breakpoint "main" | |
204 | gdb_run_cmd | |
205 | if { $do_kfail } { setup_kfail "gdb/19558" "*-*-*" } | |
206 | gdb_test_multiple "" "stop at main" { | |
207 | -wrap -re "Breakpoint $::decimal, main .*" { | |
208 | pass $gdb_test_name | |
209 | } | |
210 | } | |
df0da8a2 AH |
211 | |
212 | # Another "don't leave a process around" | |
213 | kill_wait_spawned_process $test_spawn_id | |
214 | } | |
215 | ||
216 | # This is a test of gdb's ability to attach to a running process. | |
217 | ||
218 | proc_with_prefix do_attach_tests {} { | |
219 | global gdb_prompt | |
220 | global binfile | |
221 | global escapedbinfile | |
222 | global srcfile | |
df0da8a2 AH |
223 | global timeout |
224 | global decimal | |
225 | ||
226 | clean_restart $binfile | |
227 | ||
228 | # Figure out a regular expression that will match the sysroot, | |
229 | # noting that the default sysroot is "target:", and also noting | |
230 | # that GDB will strip "target:" from the start of filenames when | |
231 | # operating on the local filesystem. However the default sysroot | |
232 | # can be set via configure option --with-sysroot, which can be "/". | |
233 | # If $binfile is a absolute path, so pattern | |
234 | # "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make | |
235 | # $sysroot simple. | |
236 | set sysroot "\[^\r\n\]*" | |
237 | ||
238 | # Start the program running and then wait for a bit, to be sure | |
239 | # that it can be attached to. | |
240 | ||
241 | set test_spawn_id [spawn_wait_for_attach $binfile] | |
242 | set testpid [spawn_id_get_pid $test_spawn_id] | |
243 | ||
1279f4ff AC |
244 | # Verify that we can attach to the process by first giving its |
245 | # executable name via the file command, and using attach with the | |
246 | # process ID. | |
247 | ||
248 | # (Actually, the test system appears to do this automatically for | |
249 | # us. So, we must also be prepared to be asked if we want to | |
250 | # discard an existing set of symbols.) | |
251 | ||
8b1b3228 AC |
252 | set test "set file, before attach1" |
253 | gdb_test_multiple "file $binfile" "$test" { | |
254 | -re "Load new symbol table from.*y or n. $" { | |
3453e7e4 | 255 | gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \ |
8b1b3228 | 256 | "$test (re-read)" |
1279f4ff | 257 | } |
3453e7e4 | 258 | -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" { |
8b1b3228 | 259 | pass "$test" |
1279f4ff AC |
260 | } |
261 | } | |
262 | ||
8b1b3228 AC |
263 | set test "attach1, after setting file" |
264 | gdb_test_multiple "attach $testpid" "$test" { | |
1279f4ff | 265 | -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" { |
8b1b3228 | 266 | pass "$test" |
1279f4ff AC |
267 | } |
268 | -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { | |
269 | # Response expected on Cygwin | |
8b1b3228 | 270 | pass "$test" |
1279f4ff AC |
271 | } |
272 | } | |
273 | ||
274 | # Verify that we can "see" the variable "should_exit" in the | |
275 | # program, and that it is zero. | |
276 | ||
8b1b3228 | 277 | gdb_test "print should_exit" " = 0" "after attach1, print should_exit" |
1279f4ff AC |
278 | |
279 | # Detach the process. | |
280 | ||
8b1b3228 | 281 | gdb_test "detach" \ |
f67c0c91 | 282 | "Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \ |
8b1b3228 | 283 | "attach1 detach" |
1279f4ff AC |
284 | |
285 | # Wait a bit for gdb to finish detaching | |
286 | ||
287 | exec sleep 5 | |
288 | ||
289 | # Purge the symbols from gdb's brain. (We want to be certain the | |
290 | # next attach, which won't be preceded by a "file" command, is | |
291 | # really getting the executable file without our help.) | |
292 | ||
293 | set old_timeout $timeout | |
294 | set timeout 15 | |
8b1b3228 AC |
295 | set test "attach1, purging symbols after detach" |
296 | gdb_test_multiple "file" "$test" { | |
297 | -re "No executable file now.*Discard symbol table.*y or n. $" { | |
298 | gdb_test "y" "No symbol file now." "$test" | |
1279f4ff AC |
299 | } |
300 | } | |
301 | set timeout $old_timeout | |
302 | ||
303 | # Verify that we can attach to the process just by giving the | |
304 | # process ID. | |
305 | ||
7cee1e54 PA |
306 | set test "attach2, with no file" |
307 | set found_exec_file 0 | |
8b1b3228 | 308 | gdb_test_multiple "attach $testpid" "$test" { |
1586c8fb | 309 | -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" { |
1279f4ff AC |
310 | # On Cygwin, the DLL's symbol tables are loaded prior to the |
311 | # executable's symbol table. This in turn always results in | |
312 | # asking the user for actually loading the symbol table of the | |
313 | # executable. | |
3453e7e4 | 314 | gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*" \ |
8b1b3228 | 315 | "$test (reset file)" |
7cee1e54 PA |
316 | |
317 | set found_exec_file 1 | |
1279f4ff | 318 | } |
1586c8fb | 319 | -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" { |
8b1b3228 | 320 | pass "$test" |
7cee1e54 PA |
321 | set found_exec_file 1 |
322 | } | |
323 | } | |
324 | ||
325 | if {$found_exec_file == 0} { | |
326 | set test "load file manually, after attach2" | |
327 | gdb_test_multiple "file $binfile" "$test" { | |
328 | -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" { | |
3453e7e4 | 329 | gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*" \ |
7cee1e54 PA |
330 | "$test (re-read)" |
331 | } | |
3453e7e4 | 332 | -re "Reading symbols from $escapedbinfile\.\.\.*$gdb_prompt $" { |
7cee1e54 PA |
333 | pass "$test" |
334 | } | |
1279f4ff AC |
335 | } |
336 | } | |
337 | ||
338 | # Verify that we can modify the variable "should_exit" in the | |
339 | # program. | |
340 | ||
27d3a1a2 | 341 | gdb_test_no_output "set should_exit=1" "after attach2, set should_exit" |
1279f4ff AC |
342 | |
343 | # Verify that the modification really happened. | |
344 | ||
1cf2f1b0 JK |
345 | gdb_breakpoint [gdb_get_line_number "postloop"] temporary |
346 | gdb_continue_to_breakpoint "postloop" ".* postloop .*" | |
1279f4ff AC |
347 | |
348 | # Allow the test process to exit, to cleanup after ourselves. | |
349 | ||
fda326dd | 350 | gdb_continue_to_end "after attach2, exit" |
1279f4ff AC |
351 | |
352 | # Make sure we don't leave a process around to confuse | |
353 | # the next test run (and prevent the compile by keeping | |
354 | # the text file busy), in case the "set should_exit" didn't | |
355 | # work. | |
8b1b3228 | 356 | |
2c8c5d37 PA |
357 | kill_wait_spawned_process $test_spawn_id |
358 | ||
359 | set test_spawn_id [spawn_wait_for_attach $binfile] | |
360 | set testpid [spawn_id_get_pid $test_spawn_id] | |
1279f4ff AC |
361 | |
362 | # Verify that we can attach to the process, and find its a.out | |
363 | # when we're cd'd to some directory that doesn't contain the | |
364 | # a.out. (We use the source path set by the "dir" command.) | |
365 | ||
a64d2530 | 366 | gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \ |
8b1b3228 | 367 | "set source path" |
1279f4ff | 368 | |
8b1b3228 AC |
369 | gdb_test "cd /tmp" "Working directory /tmp." \ |
370 | "cd away from process working directory" | |
1279f4ff AC |
371 | |
372 | # Explicitly flush out any knowledge of the previous attachment. | |
1279f4ff | 373 | |
8b1b3228 | 374 | set test "before attach3, flush symbols" |
6c95b8df | 375 | gdb_test_multiple "symbol-file" "$test" { |
8b1b3228 AC |
376 | -re "Discard symbol table from.*y or n. $" { |
377 | gdb_test "y" "No symbol file now." \ | |
378 | "$test" | |
1279f4ff | 379 | } |
8b1b3228 AC |
380 | -re "No symbol file now.*$gdb_prompt $" { |
381 | pass "$test" | |
1279f4ff AC |
382 | } |
383 | } | |
384 | ||
8b1b3228 AC |
385 | gdb_test "exec" "No executable file now." \ |
386 | "before attach3, flush exec" | |
387 | ||
388 | gdb_test "attach $testpid" \ | |
1586c8fb | 389 | "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \ |
8b1b3228 AC |
390 | "attach when process' a.out not in cwd" |
391 | ||
392 | set test "after attach3, exit" | |
dfb88a23 MS |
393 | gdb_test "kill" \ |
394 | "" \ | |
395 | "$test" \ | |
396 | "Kill the program being debugged.*y or n. $" \ | |
397 | "y" | |
1279f4ff AC |
398 | |
399 | # Another "don't leave a process around" | |
2c8c5d37 | 400 | kill_wait_spawned_process $test_spawn_id |
74cf1395 JM |
401 | } |
402 | ||
df0da8a2 AH |
403 | # Test attaching when the target is inside a system call. |
404 | ||
405 | proc_with_prefix do_call_attach_tests {} { | |
1279f4ff AC |
406 | global gdb_prompt |
407 | global binfile2 | |
df0da8a2 AH |
408 | |
409 | clean_restart | |
410 | ||
2c8c5d37 PA |
411 | set test_spawn_id [spawn_wait_for_attach $binfile2] |
412 | set testpid [spawn_id_get_pid $test_spawn_id] | |
1279f4ff AC |
413 | |
414 | # Attach | |
415 | ||
8aed1c0d | 416 | gdb_test "file $binfile2" ".*" "load file" |
8b1b3228 AC |
417 | set test "attach call" |
418 | gdb_test_multiple "attach $testpid" "$test" { | |
419 | -re "warning: reading register.*I.*O error.*$gdb_prompt $" { | |
420 | fail "$test (read register error)" | |
1279f4ff AC |
421 | } |
422 | -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" { | |
8b1b3228 | 423 | pass "$test" |
1279f4ff AC |
424 | } |
425 | -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" { | |
8b1b3228 | 426 | pass "$test" |
1279f4ff AC |
427 | } |
428 | } | |
429 | ||
430 | # See if other registers are problems | |
431 | ||
8b1b3228 AC |
432 | set test "info other register" |
433 | gdb_test_multiple "i r r3" "$test" { | |
434 | -re "warning: reading register.*$gdb_prompt $" { | |
435 | fail "$test" | |
1279f4ff | 436 | } |
8b1b3228 AC |
437 | -re "r3.*$gdb_prompt $" { |
438 | pass "$test" | |
1279f4ff | 439 | } |
1279f4ff | 440 | } |
74cf1395 | 441 | |
1279f4ff AC |
442 | # Get rid of the process |
443 | ||
8b1b3228 | 444 | gdb_test "p should_exit = 1" |
fda326dd | 445 | gdb_continue_to_end |
1279f4ff AC |
446 | |
447 | # Be paranoid | |
448 | ||
2c8c5d37 | 449 | kill_wait_spawned_process $test_spawn_id |
74cf1395 JM |
450 | } |
451 | ||
df0da8a2 | 452 | proc_with_prefix do_command_attach_tests {} { |
ccdd1909 HZ |
453 | global gdb_prompt |
454 | global binfile | |
ccdd1909 | 455 | |
65a33d75 | 456 | if {![isnative]} { |
ccdd1909 HZ |
457 | unsupported "command attach test" |
458 | return 0 | |
459 | } | |
460 | ||
2c8c5d37 PA |
461 | set test_spawn_id [spawn_wait_for_attach $binfile] |
462 | set testpid [spawn_id_get_pid $test_spawn_id] | |
ccdd1909 HZ |
463 | |
464 | gdb_exit | |
ccdd1909 | 465 | |
b750766a LS |
466 | # gdb_spawn_attach_cmdline records test results. No need to explicitly |
467 | # call pass/fail here. | |
468 | gdb_spawn_attach_cmdline $testpid | |
ccdd1909 HZ |
469 | |
470 | # Get rid of the process | |
2c8c5d37 | 471 | kill_wait_spawned_process $test_spawn_id |
ccdd1909 HZ |
472 | } |
473 | ||
98880d46 PA |
474 | # Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where |
475 | # "run" would run before the attach finished - PR17347. | |
476 | ||
f1854e35 | 477 | proc_with_prefix test_command_line_attach_run {} { |
98880d46 PA |
478 | global gdb_prompt |
479 | global binfile | |
480 | ||
52e0e32b SM |
481 | # The --pid option is used to attach to a process using the native target. |
482 | # Start GDB and run to main just to see what the execution target is, skip | |
483 | # if it's not the native target. | |
484 | clean_restart $binfile | |
485 | ||
486 | if { ![runto_main] } { | |
52e0e32b SM |
487 | return |
488 | } | |
489 | ||
490 | if { ![gdb_is_target_native] } { | |
98880d46 | 491 | unsupported "commandline attach run test" |
52e0e32b | 492 | return |
98880d46 PA |
493 | } |
494 | ||
f1854e35 SM |
495 | set test_spawn_id [spawn_wait_for_attach $binfile] |
496 | set testpid [spawn_id_get_pid $test_spawn_id] | |
98880d46 | 497 | |
f1854e35 SM |
498 | set test "run to prompt" |
499 | gdb_exit | |
98880d46 | 500 | |
f1854e35 SM |
501 | set res [gdb_spawn_with_cmdline_opts \ |
502 | "-quiet -iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""] | |
503 | if { $res != 0} { | |
504 | fail $test | |
505 | kill_wait_spawned_process $test_spawn_id | |
506 | return $res | |
507 | } | |
508 | gdb_test_multiple "" $test { | |
509 | -re {Attaching to.*Start it from the beginning\? \(y or n\) } { | |
510 | pass $test | |
98880d46 | 511 | } |
f1854e35 | 512 | } |
98880d46 | 513 | |
f1854e35 | 514 | send_gdb "y\n" |
98880d46 | 515 | |
f1854e35 SM |
516 | set test "run to main" |
517 | gdb_test_multiple "" $test { | |
518 | -re "Temporary breakpoint .* main .*$gdb_prompt $" { | |
519 | pass $test | |
98880d46 | 520 | } |
98880d46 | 521 | } |
f1854e35 SM |
522 | |
523 | # Get rid of the process | |
524 | kill_wait_spawned_process $test_spawn_id | |
98880d46 | 525 | } |
74cf1395 | 526 | |
b1468492 PW |
527 | |
528 | # This is a test of 'set exec-file-mismatch' handling. | |
529 | ||
530 | proc_with_prefix do_attach_exec_mismatch_handling_tests {} { | |
531 | global gdb_prompt | |
532 | global binfile | |
533 | global binfile2 | |
48e9cc84 | 534 | global binfile3 |
b1468492 PW |
535 | |
536 | clean_restart $binfile | |
537 | ||
538 | # Start two programs that can be attached to. | |
539 | # The first program contains a 'int bidule' variable, the second a 'float bidule'. | |
540 | ||
541 | set test_spawn_id [spawn_wait_for_attach $binfile] | |
542 | set testpid [spawn_id_get_pid $test_spawn_id] | |
543 | set test_spawn_id2 [spawn_wait_for_attach $binfile2] | |
544 | set testpid2 [spawn_id_get_pid $test_spawn_id2] | |
545 | ||
546 | ||
547 | # Test with the default value of 'set exec-file-mismatch load". | |
548 | set test "mismatch load" | |
549 | gdb_test "attach $testpid" "Attaching to program.*" "$test attach1" | |
550 | # Verify that we can "see" the variable "bidule" in the | |
551 | # program, and that it is an integer. | |
552 | gdb_test "ptype bidule" " = int" "$test after attach1, bidule is int" | |
553 | # Detach the process. | |
554 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach1" | |
555 | gdb_test_multiple "attach $testpid2" "$test attach2" { | |
556 | -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" { | |
557 | pass "$test attach2" | |
558 | } | |
559 | } | |
560 | gdb_test "y" "Reading symbols from .*attach2.*" "$test load attach2" | |
561 | # Verify that we can "see" the variable "bidule" in the | |
562 | # program, and that it is a float. | |
563 | gdb_test "ptype bidule" " = float" "$test after attach2 and load, bidule is float" | |
564 | # Detach the process. | |
565 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach2" | |
566 | ||
567 | ||
568 | # Test with 'set exec-file-mismatch warn". | |
569 | set test "mismatch warn" | |
570 | gdb_test_no_output "set exec-file-mismatch warn" | |
571 | gdb_test_multiple "attach $testpid" "$test attach" { | |
572 | -re "Attaching to program.*exec-file-mismatch handling is currently \"warn\".*$gdb_prompt" { | |
573 | pass "$test attach" | |
574 | } | |
575 | } | |
576 | # Verify that we still (wrongly) "see" the variable "bidule" as a float, | |
577 | # as we have not loaded the correct exec-file. | |
578 | gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float" | |
579 | # Detach the process. | |
580 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach" | |
581 | ||
582 | ||
583 | # Same test but with 'set exec-file-mismatch off". | |
584 | set test "mismatch off" | |
585 | gdb_test_no_output "set exec-file-mismatch off" | |
586 | gdb_test_multiple "attach $testpid" "$test attach" { | |
587 | -re "Attaching to program.*$gdb_prompt" { | |
588 | pass "$test attach" | |
589 | } | |
590 | } | |
591 | # Verify that we still (wrongly) "see" the variable "bidule" as a float, | |
592 | # as we have not warned the user and not loaded the correct exec-file | |
593 | gdb_test "ptype bidule" " = float" "$test after attach and warn, bidule is float" | |
594 | # Detach the process. | |
595 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach" | |
596 | ||
48e9cc84 PW |
597 | # Test that the 'exec-file' changed is checked before exec-file-mismatch. |
598 | set test "mismatch exec-file changed has priority" | |
599 | gdb_test_no_output "set exec-file-mismatch ask" | |
600 | gdb_test_multiple "attach $testpid" "$test attach1 again, initial exec-file" { | |
601 | -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach\".*\(y or n\)" { | |
602 | gdb_test "y" "Reading symbols from .*attach.*" $gdb_test_name | |
603 | } | |
604 | } | |
605 | ||
606 | ||
607 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach attach initial exec-file" | |
608 | ||
609 | # Change the exec-file and attach to a new process using the changed file. | |
610 | remote_exec build "mv ${binfile} ${binfile}.initial" | |
611 | remote_exec build "mv ${binfile3} ${binfile}" | |
612 | # Ensure GDB detects ${binfile} has changed when checking timestamp. | |
613 | sleep 1 | |
614 | remote_exec build "touch ${binfile}" | |
615 | set test_spawn_id3 [spawn_wait_for_attach $binfile] | |
616 | set testpid3 [spawn_id_get_pid $test_spawn_id3] | |
617 | ||
618 | gdb_test "attach $testpid3" "Attaching to program.*attach' has changed; re-reading symbols.*" \ | |
619 | "$test attach1 again, after changing exec-file" | |
620 | gdb_test "detach" "Detaching from program: .* detached\\\]" "$test detach after attach changed exec-file" | |
621 | ||
622 | # Now, test the situation when current exec-file has changed | |
623 | # and we attach to a pid using another file. | |
624 | # Ensure GDB detects ${binfile} has changed when checking timestamp. | |
625 | sleep 1 | |
626 | remote_exec build "touch ${binfile}" | |
627 | ||
628 | gdb_test_multiple "attach $testpid2" "$test attach2" { | |
629 | -re "Attaching to program.*exec-file-mismatch handling is currently \"ask\".*Load new symbol table from .*attach2\".*\(y or n\)" { | |
630 | gdb_test "y" "Reading symbols from .*attach2.*" $gdb_test_name | |
631 | } | |
632 | } | |
633 | ||
634 | # Restore initial build situation. | |
635 | remote_exec build "mv ${binfile} ${binfile3}" | |
636 | remote_exec build "mv ${binfile}.initial ${binfile}" | |
b1468492 PW |
637 | |
638 | # Don't leave a process around | |
639 | kill_wait_spawned_process $test_spawn_id | |
640 | kill_wait_spawned_process $test_spawn_id2 | |
48e9cc84 | 641 | kill_wait_spawned_process $test_spawn_id3 |
b1468492 PW |
642 | } |
643 | ||
74cf1395 | 644 | do_attach_tests |
df0da8a2 | 645 | do_attach_failure_tests |
74cf1395 | 646 | do_call_attach_tests |
b1468492 | 647 | do_attach_exec_mismatch_handling_tests |
74cf1395 | 648 | |
ccdd1909 HZ |
649 | # Test "gdb --pid" |
650 | ||
651 | do_command_attach_tests | |
652 | ||
b1468492 | 653 | |
98880d46 PA |
654 | test_command_line_attach_run |
655 | ||
74cf1395 | 656 | return 0 |