1# Copyright 1997, 1999, 2002, 2003, 2004 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 2 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, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
16
17if $tracelevel then {
18    strace $tracelevel
19}
20
21set prms_id 0
22set bug_id 0
23
24# On HP-UX 11.0, this test is causing a process running the program
25# "attach" to be left around spinning.  Until we figure out why, I am
26# commenting out the test to avoid polluting tiamat (our 11.0 nightly
27# test machine) with these processes. RT
28#
29# Setting the magic bit in the target app should work.  I added a
30# "kill", and also a test for the R3 register warning.  JB
31if { [istarget "hppa*-*-hpux*"] } {
32    return 0
33}
34
35# are we on a target board
36if [is_remote target] then {
37    return 0
38}
39
40set testfile "attach"
41set srcfile  ${testfile}.c
42set srcfile2 ${testfile}2.c
43set binfile  ${objdir}/${subdir}/${testfile}
44set binfile2 ${objdir}/${subdir}/${testfile}2
45set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
46
47#execute_anywhere "rm -f ${binfile} ${binfile2}"
48remote_exec build "rm -f ${binfile} ${binfile2}"
49# For debugging this test
50#
51#log_user 1
52
53# build the first test case
54#
55if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
56    gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
57}
58
59# Build the in-system-call test
60
61if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
62    gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
63}
64
65if [get_compiler_info ${binfile}] {
66    return -1
67}
68
69proc do_attach_tests {} {
70    global gdb_prompt
71    global binfile
72    global escapedbinfile
73    global srcfile
74    global testfile
75    global objdir
76    global subdir
77    global timeout
78
79    # Start the program running and then wait for a bit, to be sure
80    # that it can be attached to.
81
82    set testpid [eval exec $binfile &]
83    exec sleep 2
84    if { [istarget "*-*-cygwin*"] } {
85	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
86	# different due to the way fork/exec works.
87	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
88    }
89
90    # Verify that we cannot attach to nonsense.
91
92    set test "attach to nonsense is prohibited"
93    gdb_test_multiple "attach abc" "$test" {
94	-re "Illegal process-id: abc.*$gdb_prompt $" {
95	    pass "$test"
96	}
97	-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
98	    # Response expected from /proc-based systems.
99	    pass "$test"
100	}
101	-re "Can't attach to process..*$gdb_prompt $" {
102	    # Response expected on Cygwin
103	    pass "$test"
104	}
105	-re "Attaching to.*$gdb_prompt $" {
106	    fail "$test (bogus pid allowed)"
107	}
108    }
109
110    # Verify that we cannot attach to what appears to be a valid
111    # process ID, but is a process that doesn't exist.  Traditionally,
112    # most systems didn't have a process with ID 0, so we take that as
113    # the default.  However, there are a few exceptions.
114
115    set boguspid 0
116    if { [istarget "*-*-*bsd*"] } {
117	# In FreeBSD 5.0, PID 0 is used for "swapper".  Use -1 instead
118	# (which should have the desired effect on any version of
119	# FreeBSD, and probably other *BSD's too).
120	set boguspid -1
121    }
122    set test "attach to nonexistent process is prohibited"
123    gdb_test_multiple "attach $boguspid" "$test" {
124	-re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
125	    # Response expected on ptrace-based systems (i.e. HP-UX 10.20).
126	    pass "$test"
127	}
128	-re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
129	    # Response expected on ttrace-based systems (i.e. HP-UX 11.0).
130	    pass "$test"
131	}
132	-re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
133	    pass "$test"
134	}
135	-re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
136	    pass "$test"
137	}
138	-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
139	    # Response expected from /proc-based systems.
140	    pass "$test"
141	}
142	-re "Can't attach to process..*$gdb_prompt $" {
143	    # Response expected on Cygwin
144	    pass "$test"
145	}
146    }
147
148    # Verify that we can attach to the process by first giving its
149    # executable name via the file command, and using attach with the
150    # process ID.
151
152    # (Actually, the test system appears to do this automatically for
153    # us.  So, we must also be prepared to be asked if we want to
154    # discard an existing set of symbols.)
155
156    set test "set file, before attach1"
157    gdb_test_multiple "file $binfile" "$test" {
158	-re "Load new symbol table from.*y or n. $" {
159	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
160		"$test (re-read)"
161	}
162	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
163	    pass "$test"
164	}
165    }
166
167    set test "attach1, after setting file"
168    gdb_test_multiple "attach $testpid" "$test" {
169	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
170	    pass "$test"
171	}
172	-re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
173	    # Response expected on Cygwin
174	    pass "$test"
175	}
176    }
177
178    # Verify that we can "see" the variable "should_exit" in the
179    # program, and that it is zero.
180
181    gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
182
183    # Detach the process.
184
185    gdb_test "detach" \
186	"Detaching from program: .*$escapedbinfile, process $testpid" \
187	"attach1 detach"
188
189    # Wait a bit for gdb to finish detaching
190
191    exec sleep 5
192
193    # Purge the symbols from gdb's brain.  (We want to be certain the
194    # next attach, which won't be preceded by a "file" command, is
195    # really getting the executable file without our help.)
196
197    set old_timeout $timeout
198    set timeout 15
199    set test "attach1, purging symbols after detach"
200    gdb_test_multiple "file" "$test" {
201	-re "No executable file now.*Discard symbol table.*y or n. $" {
202	    gdb_test "y" "No symbol file now." "$test"
203	}
204    }
205    set timeout $old_timeout
206
207    # Verify that we can attach to the process just by giving the
208    # process ID.
209
210    set test "set file, before attach2"
211    gdb_test_multiple "attach $testpid" "$test" {
212	-re "Attaching to process $testpid.*Load new symbol table from \"$escapedbinfile\.exe\".*y or n. $" {
213	    # On Cygwin, the DLL's symbol tables are loaded prior to the
214	    # executable's symbol table.  This in turn always results in
215	    # asking the user for actually loading the symbol table of the
216	    # executable.
217	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
218		"$test (reset file)"
219	}
220	-re "Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*$gdb_prompt $" {
221	    pass "$test"
222	}
223    }
224
225    # Verify that we can modify the variable "should_exit" in the
226    # program.
227
228    gdb_test "set should_exit=1" "" "after attach2, set should_exit"
229
230    # Verify that the modification really happened.
231
232    send_gdb "tbreak 19\n"
233    gdb_expect {
234	-re "Breakpoint .*at.*$srcfile, line 19.*$gdb_prompt $" {
235	    pass "after attach2, set tbreak postloop"
236	}
237	-re "$gdb_prompt $" {
238	    fail "after attach2, set tbreak postloop"
239	}
240	timeout {
241	    fail "(timeout) after attach2, set tbreak postloop"
242	}
243    }
244    send_gdb "continue\n"
245    gdb_expect {
246	-re "main.*at.*$srcfile:19.*$gdb_prompt $" {
247	    pass "after attach2, reach tbreak postloop"
248	}
249	-re "$gdb_prompt $" {
250	    fail "after attach2, reach tbreak postloop"
251	}
252	timeout {
253	    fail "(timeout) after attach2, reach tbreak postloop"
254	}
255    }
256
257    # Allow the test process to exit, to cleanup after ourselves.
258
259    gdb_test "continue" "Program exited normally." "after attach2, exit"
260
261    # Make sure we don't leave a process around to confuse
262    # the next test run (and prevent the compile by keeping
263    # the text file busy), in case the "set should_exit" didn't
264    # work.
265
266    remote_exec build "kill -9 ${testpid}"
267
268    # Start the program running and then wait for a bit, to be sure
269    # that it can be attached to.
270
271    set testpid [eval exec $binfile &]
272    exec sleep 2
273    if { [istarget "*-*-cygwin*"] } {
274	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
275	# different due to the way fork/exec works.
276	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
277    }
278
279    # Verify that we can attach to the process, and find its a.out
280    # when we're cd'd to some directory that doesn't contain the
281    # a.out.  (We use the source path set by the "dir" command.)
282
283    gdb_test "dir ${objdir}/${subdir}" "Source directories searched: .*" \
284	"set source path"
285
286    gdb_test "cd /tmp" "Working directory /tmp." \
287	"cd away from process working directory"
288
289    # Explicitly flush out any knowledge of the previous attachment.
290
291    set test "before attach3, flush symbols"
292    gdb_test_multiple "symbol" "$test" {
293	-re "Discard symbol table from.*y or n. $" {
294	    gdb_test "y" "No symbol file now." \
295		"$test"
296	}
297	-re "No symbol file now.*$gdb_prompt $" {
298	    pass "$test"
299	}
300    }
301
302    gdb_test "exec" "No executable file now." \
303	"before attach3, flush exec"
304
305    gdb_test "attach $testpid" \
306	"Attaching to process $testpid.*Reading symbols from $escapedbinfile.*main.*at .*" \
307	"attach when process' a.out not in cwd"
308
309    set test "after attach3, exit"
310    gdb_test_multiple "kill" "$test" {
311	-re "Kill the program being debugged.*y or n. $" {
312	    gdb_test "y" "" "$test"
313	}
314    }
315
316    # Another "don't leave a process around"
317    remote_exec build "kill -9 ${testpid}"
318}
319
320proc do_call_attach_tests {} {
321    global gdb_prompt
322    global binfile2
323
324    # Start the program running and then wait for a bit, to be sure
325    # that it can be attached to.
326
327    set testpid [eval exec $binfile2 &]
328    exec sleep 2
329    if { [istarget "*-*-cygwin*"] } {
330	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
331	# different due to the way fork/exec works.
332	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
333    }
334
335    # Attach
336
337    gdb_test "file $binfile2" "" "force switch to gdb64, if necessary"
338    set test "attach call"
339    gdb_test_multiple "attach $testpid" "$test" {
340	-re "warning: reading register.*I.*O error.*$gdb_prompt $" {
341	    fail "$test (read register error)"
342	}
343	-re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
344	    pass "$test"
345	}
346	-re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
347	    pass "$test"
348	}
349    }
350
351    # See if other registers are problems
352
353    set test "info other register"
354    gdb_test_multiple "i r r3" "$test" {
355	-re "warning: reading register.*$gdb_prompt $" {
356	    fail "$test"
357	}
358	-re "r3.*$gdb_prompt $" {
359	    pass "$test"
360	}
361    }
362
363    # Get rid of the process
364
365    gdb_test "p should_exit = 1"
366    gdb_test "c" "Program exited normally."
367
368    # Be paranoid
369
370    remote_exec build "kill -9 ${testpid}"
371}
372
373
374# Start with a fresh gdb
375
376gdb_exit
377gdb_start
378gdb_reinitialize_dir $srcdir/$subdir
379gdb_load ${binfile}
380
381# This is a test of gdb's ability to attach to a running process.
382
383do_attach_tests
384
385# Test attaching when the target is inside a system call
386
387gdb_exit
388gdb_start
389
390gdb_reinitialize_dir $srcdir/$subdir
391do_call_attach_tests
392
393return 0
394