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