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