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