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