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