summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.hp/attach.exp
blob: 372acd05d7ec37172bb5c2c756c91e9b3ec23e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#   Copyright (C) 1997 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

# On HP-UX 11.0, this test is causing a process running
# the program "attach" to be left around spinning.
# Until we figure out why, I am commenting out the test
# to avoid polluting tiamat (our 11.0 nightly test machine)
# with these processes. RT
#
# Setting the magic bit in the target app should work.
# I added a "kill", and also a test for the R3 register
#  warning.           JB
# 
if $tracelevel then {
	strace $tracelevel
	}

set prms_id 0
set bug_id 0


# are we on a target board
if ![isnative] then {
    return
}

if { ![istarget "hppa*-*-hpux10.30"] && ![istarget "hppa*-*-hpux11.*"] } {
    #setup_xfail "*-*-*"
    return 0
}

set testfile "attach"
set srcfile  ${testfile}.c
set srcfile2 ${testfile}2.c
set binfile  ${objdir}/${subdir}/${testfile}
set binfile2 ${objdir}/${subdir}/${testfile}2
set cleanupfile ${objdir}/${subdir}/${testfile}.awk

#execute_anywhere "rm -f ${binfile} ${binfile2}"
remote_exec build "rm -f ${binfile} ${binfile2}"
# For debugging this test
#
#log_user 1

# Clean out any old files from past runs.
#
remote_exec build "${cleanupfile}"

# build the first test case
#
if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
}

# Build the in-system-call test

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
}

#if  { [compile "-E ${srcdir}/${subdir}/compiler.c >> ${objdir}/${subdir}/${testfile}.tmp"] != ""# } {
#    perror "Couldn't make ${testfile}.tmp"
#    return -1
#}

#execute_anywhere "mv ${objdir}/${subdir}/${testfile}.tmp ${binfile}.ci"

#source ${binfile}.ci

proc do_attach_tests {} {
   global gdb_prompt
   global binfile
   global srcfile
   global testfile
   global objdir
   global subdir
   global timeout

   # Start the program running and then wait for a bit, to be sure
   # that it can be attached to.
   #
   set testpid [eval exec $binfile &]
   exec sleep 2

   # Verify that we cannot attach to nonsense.
   #
   send_gdb "attach abc\n"
   gdb_expect {
      -re "Illegal process-id: abc.*$gdb_prompt $"\
                      {pass "attach to nonsense is prohibited"}
      -re "Attaching to.*$gdb_prompt $"\
                      {fail "attach to nonsense is prohibited (bogus pid allowed)"}
      -re "$gdb_prompt $" {fail "attach to nonsense is prohibited"}
      timeout         {fail "(timeout) attach to nonsense is prohibited"}
   }

   # Verify that we cannot attach to what appears to be a valid
   # process ID, but is a process that doesn't exist.  (I don't
   # believe any process is ever assigned #0, at least on HPUX.)
   #
   send_gdb "attach 0\n"
   gdb_expect {
      # This reponse is expected on HP-UX 10.20 (i.e., ptrace-based).
      -re "Attaching to.*, process 0.*No such process.*$gdb_prompt $"\
                      {pass "attach to nonexistent process is prohibited"}
      # This response is expected on HP-UX 10.30 & 11.0 (i.e., ttrace-based).
      -re "Attaching to.*, process 0.*Permission denied.*$gdb_prompt $"\
                      {pass "attach to nonexistent process is prohibited"}
      -re "$gdb_prompt $" {fail "attach to nonexistent process is prohibited"}
      timeout         {fail "(timeout) attach to nonexistent process is prohibited"}
   }

   # Verify that we can attach to the process by first giving its
   # executable name via the file command, and using attach with
   # the process ID.
   #
   # (Actually, the test system appears to do this automatically
   # for us.  So, we must also be prepared to be asked if we want
   # to discard an existing set of symbols.)
   #
   send_gdb "file $binfile\n"
   gdb_expect {
      -re "Load new symbol table from.*y or n.*$" {
         send_gdb "y\n"
         gdb_expect {
            -re "Reading symbols from $binfile\.\.\.*done.*$gdb_prompt $"\
                            {pass "(re)set file, before attach1"}
            -re "$gdb_prompt $" {fail "(re)set file, before attach1"}
            timeout         {fail "(timeout) (re)set file, before attach1"}
         }
      }
      -re "Reading symbols from $binfile\.\.\.*done.*$gdb_prompt $"\
                      {pass "set file, before attach1"}
      -re "$gdb_prompt $" {fail "set file, before attach1"}
      timeout         {fail "(timeout) set file, before attach1"}
   }

   send_gdb "attach $testpid\n"
   gdb_expect {
      -re "Attaching to program.*$binfile, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $"\
                      {pass "attach1, after setting file"}
      -re "$gdb_prompt $" {fail "attach1, after setting file"}
      timeout         {fail "(timeout) attach1, after setting file"}
   }

   # Verify that we can "see" the variable "should_exit" in the
   # program, and that it is zero.
   #
   send_gdb "print should_exit\n"
   gdb_expect {
      -re ".* = 0.*$gdb_prompt $"\
                      {pass "after attach1, print should_exit"}
      -re "$gdb_prompt $" {fail "after attach1, print should_exit"}
      timeout         {fail "(timeout) after attach1, print should_exit"}
   }

   # Detach the process.
   #
   send_gdb "detach\n"
   gdb_expect {
      -re "Detaching from program: .*$binfile.*$gdb_prompt $"\
                      {pass "attach1 detach"}
      -re "$gdb_prompt $" {fail "attach1 detach"}
      timeout         {fail "(timeout) attach1 detach"}
   }

   # Wait a bit for gdb to finish detaching
   #
   exec sleep 5

   # Purge the symbols from gdb's brain.  (We want to be certain
   # the next attach, which won't be preceded by a "file" command,
   # is really getting the executable file without our help.)
   #
   set old_timeout $timeout
   set timeout     [expr $timeout + 20]
   send_gdb "file\n"
   gdb_expect {
      -re ".*gdb internal error.*$" { 
          fail "Internal error, prob. Memory corruption" 
      }
      -re "No exec file now.*Discard symbol table.*y or n.*$" {
         send_gdb "y\n"
         gdb_expect {
            -re "No symbol file now.*$gdb_prompt $"\
                            {pass "attach1, purging symbols after detach"}
            -re "$gdb_prompt $" {fail "attach1, purging symbols after detach"}
            timeout         {fail "(timeout) attach1, purging symbols after detach"}
         }
      }
      -re "$gdb_prompt $" {fail "attach1, purging file after detach"}
      timeout         {
          fail "(timeout) attach1, purging file after detach"
      }
   }
   set timeout $old_timeout

   # Verify that we can attach to the process just by giving the
   # process ID.
   #
   send_gdb "attach $testpid\n"
   gdb_expect {
      -re "Attaching to process $testpid.*Reading symbols from $binfile.*main.*at .*$gdb_prompt $"\
                      {pass "attach2"}
      -re "$gdb_prompt $" {fail "attach2"}
      timeout         {fail "(timeout) attach2"}
   }

   # Verify that we can modify the variable "should_exit" in the
   # program.
   #
   send_gdb "set should_exit=1\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "after attach2, set should_exit"}
      timeout         {fail "(timeout) after attach2, set should_exit"}
   }

   # Verify that the modification really happened.
   #
   send_gdb "tbreak 19\n"
   gdb_expect {
      -re "Breakpoint .*at.*$srcfile, line 19.*$gdb_prompt $"\
                      {pass "after attach2, set tbreak postloop"}
      -re "$gdb_prompt $" {fail "after attach2, set tbreak postloop"}
      timeout         {fail "(timeout) after attach2, set tbreak postloop"}
   }
   send_gdb "continue\n"
   gdb_expect {
      -re "main.*at.*$srcfile:19.*$gdb_prompt $"\
                      {pass "after attach2, reach tbreak postloop"}
      -re "$gdb_prompt $" {fail "after attach2, reach tbreak postloop"}
      timeout         {fail "(timeout) after attach2, reach tbreak postloop"}
   }

   # Allow the test process to exit, to cleanup after ourselves.
   #
   send_gdb "continue\n"
   gdb_expect {
      -re "Program exited normally.*$gdb_prompt $"\
                      {pass "after attach2, exit"}
      -re "$gdb_prompt $" {fail "after attach2, exit"}
      timeout         {fail "(timeout) after attach2, exit"}
   }

   # Make sure we don't leave a process around to confuse
   # the next test run (and prevent the compile by keeping
   # the text file busy), in case the "set should_exit" didn't
   # work.
   #
#   execute_anywhere "kill -9 ${testpid}"
remote_exec build "kill -9 ${testpid}"
   # Start the program running and then wait for a bit, to be sure
   # that it can be attached to.
   #
   set testpid [eval exec $binfile &]
   exec sleep 2

   # Verify that we can attach to the process, and find its a.out
   # when we're cd'd to some directory that doesn't contain the
   # a.out.  (We use the source path set by the "dir" command.)
   #
   send_gdb "dir ${objdir}/${subdir}\n"
   gdb_expect {
      -re ".*Source directories searched: .*$gdb_prompt $"\
                      {pass "set source path"}
      -re "$gdb_prompt $" {fail "set source path"}
      timeout         {fail "(timeout) set source path"}
   }

   send_gdb "cd /tmp\n"
   gdb_expect {
      -re ".*Working directory /tmp.*$gdb_prompt $"\
                      {pass "cd away from process' a.out"}
      -re "$gdb_prompt $" {fail "cd away from process' a.out"}
      timeout         {fail "(timeout) cd away from process' a.out"}
   }

   # Explicitly flush out any knowledge of the previous attachment.
   send_gdb "symbol\n"
   gdb_expect {
      -re ".*Discard symbol table from.*y or n. $"\
                      {send_gdb "y\n"
                       gdb_expect {
                          -re ".*No symbol file now.*$gdb_prompt $"\
                                          {pass "before attach3, flush symbols"}
                          -re "$gdb_prompt $" {fail "before attach3, flush symbols"}
                          timeout         {fail "(timeout) before attach3, flush symbols"}
                       }
                      }
      -re ".*No symbol file now.*$gdb_prompt $"\
                      {pass "before attach3, flush symbols"}
      -re "$gdb_prompt $" {fail "before attach3, flush symbols"}
      timeout         {fail "(timeout) before attach3, flush symbols"}
   }
   send_gdb "exec\n"
   gdb_expect {
      -re ".*No exec file now.*$gdb_prompt $"\
                      {pass "before attach3, flush exec"}
      -re "$gdb_prompt $" {fail "before attach3, flush exec"}
      timeout         {fail "(timeout) before attach3, flush exec"}
   }

   send_gdb "attach $testpid\n"
   gdb_expect {
      -re "Attaching to process $testpid.*Reading symbols from $binfile.*main.*at .*$gdb_prompt $"\
                      {pass "attach when process' a.out not in cwd"}
      -re "$gdb_prompt $" {fail "attach when process' a.out not in cwd"}
      timeout         {fail "(timeout) attach when process' a.out not in cwd"}
   }

   send_gdb "kill\n"
   gdb_expect {
      -re ".*Kill the program being debugged.*y or n. $"\
                      {send_gdb "y\n"
                       gdb_expect {
                          -re "$gdb_prompt $" {pass "after attach3, exit"}
                          timeout {fail "(timeout) after attach3, exit"}
                       }
                      }
      -re "$gdb_prompt $" {fail "after attach3, exit"}
      timeout         {fail "(timeout) after attach3, exit"}
   }
}

proc do_call_attach_tests {} {
   global gdb_prompt
   global binfile2

   # Start the program running and then wait for a bit, to be sure
   # that it can be attached to.
   #
   set testpid [eval exec $binfile2 &]
   exec sleep 2

   # Attach
   #
   send_gdb "attach $testpid\n"
   gdb_expect {
      -re ".*warning: reading register.*I.*O error.*$gdb_prompt $" {
         fail "attach call, read register 3 error"
     }
     -re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
         pass "attach call"
     }
      -re "$gdb_prompt $" {fail "attach call"}
      timeout         {fail "(timeout) attach call"}
   }

   # See if other registers are problems
   #
   send_gdb "i r r3\n"
   gdb_expect {
       -re ".*warning: reading register.*$gdb_prompt $" {
           pass "CHFts23490: known bug"
       }
       -re ".*r3.*$gdb_prompt $" {
           pass "Bug fixed, Yayyy!"
       }
       timeout { fail "timeout on info reg" }
   }

   # Get rid of the process
   #
   gdb_test "p should_exit = 1" ".*" ""
   gdb_test "c" ".*Program exited normally.*" ""
   
   # Be paranoid
   #
#   execute_anywhere "kill -9 ${testpid}"
remote_exec build "kill -9 ${testpid}"

}


# Start with a fresh gdb
#
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}

# This is a test of gdb's ability to attach to a running process.
#
do_attach_tests

# Test attaching when the target is inside a system call
#
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
do_call_attach_tests

# Until "set follow-fork-mode" and "catch fork" are implemented on
# other targets...
#
if ![istarget "hppa*-hp-hpux*"] then {
   setup_xfail "*-*-*"
}

return 0