]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/attach.exp
testsuite: tcl exec& -> 'kill -9 $pid' is racy (attach-many-short-lived-thread.exp...
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / attach.exp
CommitLineData
32d0add0 1# Copyright 1997-2015 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
74cf1395 16
33c3e192
FF
17# On HP-UX 11.0, this test is causing a process running the program
18# "attach" to be left around spinning. Until we figure out why, I am
19# commenting out the test to avoid polluting tiamat (our 11.0 nightly
20# test machine) with these processes. RT
21#
22# Setting the magic bit in the target app should work. I added a
23# "kill", and also a test for the R3 register warning. JB
24if { [istarget "hppa*-*-hpux*"] } {
25 return 0
26}
74cf1395 27
60b3033e 28if {![can_spawn_for_attach]} {
74cf1395
JM
29 return 0
30}
31
a64d2530
TT
32standard_testfile attach.c attach2.c
33set binfile2 ${binfile}2
34set escapedbinfile [string_to_regexp $binfile]
74cf1395
JM
35
36#execute_anywhere "rm -f ${binfile} ${binfile2}"
37remote_exec build "rm -f ${binfile} ${binfile2}"
38# For debugging this test
39#
40#log_user 1
41
74cf1395
JM
42# build the first test case
43#
44if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
45 untested attach.exp
46 return -1
74cf1395
JM
47}
48
74cf1395
JM
49# Build the in-system-call test
50
51if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
b60f0898
JB
52 untested attach.exp
53 return -1
74cf1395
JM
54}
55
4c93b1db 56if [get_compiler_info] {
74cf1395
JM
57 return -1
58}
59
60proc do_attach_tests {} {
1279f4ff
AC
61 global gdb_prompt
62 global binfile
63 global escapedbinfile
64 global srcfile
65 global testfile
1279f4ff
AC
66 global subdir
67 global timeout
68
1586c8fb
GB
69 # Figure out a regular expression that will match the sysroot,
70 # noting that the default sysroot is "target:", and also noting
71 # that GDB will strip "target:" from the start of filenames when
72 # operating on the local filesystem
73 set sysroot ""
74 set test "show sysroot"
75 gdb_test_multiple $test $test {
76 -re "The current system root is \"(.*)\"\..*${gdb_prompt} $" {
77 set sysroot $expect_out(1,string)
78 }
79 }
80 regsub "^target:" "$sysroot" "(target:)?" sysroot
81
1279f4ff
AC
82 # Start the program running and then wait for a bit, to be sure
83 # that it can be attached to.
84
2c8c5d37
PA
85 set test_spawn_id [spawn_wait_for_attach $binfile]
86 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
87
88 # Verify that we cannot attach to nonsense.
89
8b1b3228
AC
90 set test "attach to nonsense is prohibited"
91 gdb_test_multiple "attach abc" "$test" {
74164c56
JK
92 -re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
93 pass "$test"
94 }
95 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
96 # Response expected from /proc-based systems.
97 pass "$test"
98 }
99 -re "Can't attach to process..*$gdb_prompt $" {
100 # Response expected on Cygwin
101 pass "$test"
102 }
103 -re "Attaching to.*$gdb_prompt $" {
104 fail "$test (bogus pid allowed)"
105 }
106 }
107
108 # Verify that we cannot attach to nonsense even if its initial part is
109 # a valid PID.
110
111 set test "attach to digits-starting nonsense is prohibited"
112 gdb_test_multiple "attach ${testpid}x" "$test" {
113 -re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
8b1b3228 114 pass "$test"
1279f4ff
AC
115 }
116 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
117 # Response expected from /proc-based systems.
8b1b3228 118 pass "$test"
1279f4ff 119 }
8b1b3228 120 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 121 # Response expected on Cygwin
8b1b3228 122 pass "$test"
1279f4ff
AC
123 }
124 -re "Attaching to.*$gdb_prompt $" {
8b1b3228 125 fail "$test (bogus pid allowed)"
1279f4ff
AC
126 }
127 }
128
129 # Verify that we cannot attach to what appears to be a valid
130 # process ID, but is a process that doesn't exist. Traditionally,
131 # most systems didn't have a process with ID 0, so we take that as
132 # the default. However, there are a few exceptions.
133
134 set boguspid 0
135 if { [istarget "*-*-*bsd*"] } {
136 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
137 # (which should have the desired effect on any version of
138 # FreeBSD, and probably other *BSD's too).
139 set boguspid -1
140 }
8b1b3228
AC
141 set test "attach to nonexistent process is prohibited"
142 gdb_test_multiple "attach $boguspid" "$test" {
1279f4ff
AC
143 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
144 # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
8b1b3228 145 pass "$test"
1279f4ff
AC
146 }
147 -re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
148 # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
8b1b3228 149 pass "$test"
1279f4ff
AC
150 }
151 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
8b1b3228 152 pass "$test"
1279f4ff
AC
153 }
154 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
8b1b3228 155 pass "$test"
1279f4ff
AC
156 }
157 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
158 # Response expected from /proc-based systems.
8b1b3228 159 pass "$test"
1279f4ff 160 }
8b1b3228 161 -re "Can't attach to process..*$gdb_prompt $" {
1279f4ff 162 # Response expected on Cygwin
8b1b3228 163 pass "$test"
1279f4ff 164 }
7cee1e54
PA
165 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
166 # Response expected on the extended-remote target.
167 pass "$test"
168 }
1279f4ff
AC
169 }
170
171 # Verify that we can attach to the process by first giving its
172 # executable name via the file command, and using attach with the
173 # process ID.
174
175 # (Actually, the test system appears to do this automatically for
176 # us. So, we must also be prepared to be asked if we want to
177 # discard an existing set of symbols.)
178
8b1b3228
AC
179 set test "set file, before attach1"
180 gdb_test_multiple "file $binfile" "$test" {
181 -re "Load new symbol table from.*y or n. $" {
182 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
183 "$test (re-read)"
1279f4ff
AC
184 }
185 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
8b1b3228 186 pass "$test"
1279f4ff
AC
187 }
188 }
189
8b1b3228
AC
190 set test "attach1, after setting file"
191 gdb_test_multiple "attach $testpid" "$test" {
1279f4ff 192 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
8b1b3228 193 pass "$test"
1279f4ff
AC
194 }
195 -re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
196 # Response expected on Cygwin
8b1b3228 197 pass "$test"
1279f4ff
AC
198 }
199 }
200
201 # Verify that we can "see" the variable "should_exit" in the
202 # program, and that it is zero.
203
8b1b3228 204 gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
1279f4ff
AC
205
206 # Detach the process.
207
8b1b3228
AC
208 gdb_test "detach" \
209 "Detaching from program: .*$escapedbinfile, process $testpid" \
210 "attach1 detach"
1279f4ff
AC
211
212 # Wait a bit for gdb to finish detaching
213
214 exec sleep 5
215
216 # Purge the symbols from gdb's brain. (We want to be certain the
217 # next attach, which won't be preceded by a "file" command, is
218 # really getting the executable file without our help.)
219
220 set old_timeout $timeout
221 set timeout 15
8b1b3228
AC
222 set test "attach1, purging symbols after detach"
223 gdb_test_multiple "file" "$test" {
224 -re "No executable file now.*Discard symbol table.*y or n. $" {
225 gdb_test "y" "No symbol file now." "$test"
1279f4ff
AC
226 }
227 }
228 set timeout $old_timeout
229
230 # Verify that we can attach to the process just by giving the
231 # process ID.
232
7cee1e54
PA
233 set test "attach2, with no file"
234 set found_exec_file 0
8b1b3228 235 gdb_test_multiple "attach $testpid" "$test" {
1586c8fb 236 -re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
1279f4ff
AC
237 # On Cygwin, the DLL's symbol tables are loaded prior to the
238 # executable's symbol table. This in turn always results in
239 # asking the user for actually loading the symbol table of the
240 # executable.
1586c8fb 241 gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*done." \
8b1b3228 242 "$test (reset file)"
7cee1e54
PA
243
244 set found_exec_file 1
1279f4ff 245 }
1586c8fb 246 -re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
8b1b3228 247 pass "$test"
7cee1e54
PA
248 set found_exec_file 1
249 }
250 }
251
252 if {$found_exec_file == 0} {
253 set test "load file manually, after attach2"
254 gdb_test_multiple "file $binfile" "$test" {
255 -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
256 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
257 "$test (re-read)"
258 }
259 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
260 pass "$test"
261 }
1279f4ff
AC
262 }
263 }
264
265 # Verify that we can modify the variable "should_exit" in the
266 # program.
267
27d3a1a2 268 gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
1279f4ff
AC
269
270 # Verify that the modification really happened.
271
1cf2f1b0
JK
272 gdb_breakpoint [gdb_get_line_number "postloop"] temporary
273 gdb_continue_to_breakpoint "postloop" ".* postloop .*"
1279f4ff
AC
274
275 # Allow the test process to exit, to cleanup after ourselves.
276
fda326dd 277 gdb_continue_to_end "after attach2, exit"
1279f4ff
AC
278
279 # Make sure we don't leave a process around to confuse
280 # the next test run (and prevent the compile by keeping
281 # the text file busy), in case the "set should_exit" didn't
282 # work.
8b1b3228 283
2c8c5d37
PA
284 kill_wait_spawned_process $test_spawn_id
285
286 set test_spawn_id [spawn_wait_for_attach $binfile]
287 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
288
289 # Verify that we can attach to the process, and find its a.out
290 # when we're cd'd to some directory that doesn't contain the
291 # a.out. (We use the source path set by the "dir" command.)
292
a64d2530 293 gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
8b1b3228 294 "set source path"
1279f4ff 295
8b1b3228
AC
296 gdb_test "cd /tmp" "Working directory /tmp." \
297 "cd away from process working directory"
1279f4ff
AC
298
299 # Explicitly flush out any knowledge of the previous attachment.
1279f4ff 300
8b1b3228 301 set test "before attach3, flush symbols"
6c95b8df 302 gdb_test_multiple "symbol-file" "$test" {
8b1b3228
AC
303 -re "Discard symbol table from.*y or n. $" {
304 gdb_test "y" "No symbol file now." \
305 "$test"
1279f4ff 306 }
8b1b3228
AC
307 -re "No symbol file now.*$gdb_prompt $" {
308 pass "$test"
1279f4ff
AC
309 }
310 }
311
8b1b3228
AC
312 gdb_test "exec" "No executable file now." \
313 "before attach3, flush exec"
314
315 gdb_test "attach $testpid" \
1586c8fb 316 "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
8b1b3228
AC
317 "attach when process' a.out not in cwd"
318
319 set test "after attach3, exit"
dfb88a23
MS
320 gdb_test "kill" \
321 "" \
322 "$test" \
323 "Kill the program being debugged.*y or n. $" \
324 "y"
1279f4ff
AC
325
326 # Another "don't leave a process around"
2c8c5d37 327 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
328}
329
330proc do_call_attach_tests {} {
1279f4ff
AC
331 global gdb_prompt
332 global binfile2
333
2c8c5d37
PA
334 set test_spawn_id [spawn_wait_for_attach $binfile2]
335 set testpid [spawn_id_get_pid $test_spawn_id]
1279f4ff
AC
336
337 # Attach
338
f6978de9 339 gdb_test "file $binfile2" ".*" "force switch to gdb64, if necessary"
8b1b3228
AC
340 set test "attach call"
341 gdb_test_multiple "attach $testpid" "$test" {
342 -re "warning: reading register.*I.*O error.*$gdb_prompt $" {
343 fail "$test (read register error)"
1279f4ff
AC
344 }
345 -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
8b1b3228 346 pass "$test"
1279f4ff
AC
347 }
348 -re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
8b1b3228 349 pass "$test"
1279f4ff
AC
350 }
351 }
352
353 # See if other registers are problems
354
8b1b3228
AC
355 set test "info other register"
356 gdb_test_multiple "i r r3" "$test" {
357 -re "warning: reading register.*$gdb_prompt $" {
358 fail "$test"
1279f4ff 359 }
8b1b3228
AC
360 -re "r3.*$gdb_prompt $" {
361 pass "$test"
1279f4ff 362 }
1279f4ff 363 }
74cf1395 364
1279f4ff
AC
365 # Get rid of the process
366
8b1b3228 367 gdb_test "p should_exit = 1"
fda326dd 368 gdb_continue_to_end
1279f4ff
AC
369
370 # Be paranoid
371
2c8c5d37 372 kill_wait_spawned_process $test_spawn_id
74cf1395
JM
373}
374
ccdd1909
HZ
375proc do_command_attach_tests {} {
376 global gdb_prompt
377 global binfile
378 global verbose
379 global GDB
380 global INTERNAL_GDBFLAGS
381 global GDBFLAGS
382
383 if ![isnative] then {
384 unsupported "command attach test"
385 return 0
386 }
387
2c8c5d37
PA
388 set test_spawn_id [spawn_wait_for_attach $binfile]
389 set testpid [spawn_id_get_pid $test_spawn_id]
ccdd1909
HZ
390
391 gdb_exit
ccdd1909 392
2c8c5d37 393 set res [gdb_spawn_with_cmdline_opts "--pid=$testpid"]
ccdd1909 394 set test "starting with --pid"
2c8c5d37 395 gdb_test_multiple "" $test {
ccdd1909
HZ
396 -re "Reading symbols from.*$gdb_prompt $" {
397 pass "$test"
398 }
ccdd1909
HZ
399 }
400
401 # Get rid of the process
2c8c5d37 402 kill_wait_spawned_process $test_spawn_id
ccdd1909
HZ
403}
404
98880d46
PA
405# Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
406# "run" would run before the attach finished - PR17347.
407
408proc test_command_line_attach_run {} {
409 global gdb_prompt
410 global binfile
411
412 if ![isnative] then {
413 unsupported "commandline attach run test"
414 return 0
415 }
416
417 with_test_prefix "cmdline attach run" {
2c8c5d37
PA
418 set test_spawn_id [spawn_wait_for_attach $binfile]
419 set testpid [spawn_id_get_pid $test_spawn_id]
98880d46
PA
420
421 set test "run to prompt"
422 gdb_exit
423
424 set res [gdb_spawn_with_cmdline_opts \
425 "-iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
426 if { $res != 0} {
427 fail $test
2c8c5d37 428 kill_wait_spawned_process $test_spawn_id
98880d46
PA
429 return $res
430 }
431 gdb_test_multiple "" $test {
432 -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
433 pass $test
434 }
435 }
436
437 send_gdb "y\n"
438
439 set test "run to main"
440 gdb_test_multiple "" $test {
441 -re "Temporary breakpoint .* main .*$gdb_prompt $" {
442 pass $test
443 }
444 }
445
446 # Get rid of the process
2c8c5d37 447 kill_wait_spawned_process $test_spawn_id
98880d46
PA
448 }
449}
74cf1395
JM
450
451# Start with a fresh gdb
1279f4ff 452
74cf1395
JM
453gdb_exit
454gdb_start
455gdb_reinitialize_dir $srcdir/$subdir
456gdb_load ${binfile}
457
458# This is a test of gdb's ability to attach to a running process.
1279f4ff 459
74cf1395
JM
460do_attach_tests
461
462# Test attaching when the target is inside a system call
1279f4ff 463
74cf1395
JM
464gdb_exit
465gdb_start
466
74cf1395
JM
467gdb_reinitialize_dir $srcdir/$subdir
468do_call_attach_tests
469
ccdd1909
HZ
470# Test "gdb --pid"
471
472do_command_attach_tests
473
98880d46
PA
474test_command_line_attach_run
475
74cf1395 476return 0