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