aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/a1-selftest.exp
blob: 62435732588a45e758355a9bd088605a9cef208d (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#   Copyright (C) 1988, 1990, 1991, 1992, 1994 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

# This file was written by Rob Savoye. (rob@cygnus.com)

if $tracelevel then {
	strace $tracelevel
	}

set prms_id 0
set bug_id 0

# are we on a target board
if ![isnative] then {
    continue
}
if [istarget "m68k*-*-hpux*"] then {
    # The top-level makefile passes CFLAGS= (no -g) for hp300.  This probably
    # should be fixed (it is only needed for gcc bootstrapping, not gdb),
    # but until then.....
    setup_xfail "*-*-*"
    fail "cannot test self if compiled without debug info"
    continue
}

proc test_with_self {} {
    global prompt
    global tool
    global det_file
    global decimal

    # load yourself into the debugger
    # This can take a relatively long time, particularly for testing where
    # the executable is being accessed over a network, or where gdb does not
    # support partial symbols for a particular target and has to load the
    # entire symbol table.  Set the timeout to 10 minutes, which should be
    # adequate for most environments (it *has* timed out with 5 min on a
    # SPARCstation SLC under moderate load, so this isn't unreasonable).
    # After gdb is started, set the timeout to 30 seconds for the duration
    # of this test, and then back to the original value.

    set timeout 600
    if {[gdb_load "./x$tool"] <0} then {
	return -1
    }

    # disassemble yourself
    send "x/10i main\n"
    expect {
	-re "x/10i.*main.*main.$decimal.*main.$decimal.*$prompt $"\
	    { pass "Disassemble main" }
	-re ".*$prompt $"	{ fail "Disassemble main" }
	timeout		{ fail "(timeout) Disassemble main" }
    }

    set description "set breakpoint at main"
    send "break main\n"
    expect {
	-re "Breakpoint.*at.* file.*, line.*$prompt $" {
	    pass "$description"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    # We'll need this when we send a ^C to GDB.  Need to do it before we
    # run the program and gdb starts saving and restoring tty states.
    # On Ultrix, we don't need it and it is really slow (because shell_escape
    # doesn't use vfork).
    if ![istarget "*-*-ultrix*"] then {
	gdb_test "shell stty intr '^C'" "" \
	    "set interrupt character in test_with_self"
    }

    # run yourself
    set description "run until breakpoint at main"
    send "run\n"
    expect {
	-re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.* at .*main.c:.*$prompt $" {
	    pass "$description"
	}
	-re "Starting program.*Breakpoint \[0-9\]+,.*main .argc.*argv.*$prompt $" {
	    xfail "$description (line numbers scrambled?)"
	}
	-re "vfork: No more processes.*$prompt $" {
	    fail "$description (out of virtual memory)"
	    return -1
	}
	-re ".*$prompt $" {
	    fail "$description"
	    return -1
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    # It may take a very long time for the inferior gdb to start (lynx),
    # so we wait until after it's started to reset the timeout value.
    set timeout 30

    # do we have a version number ?
    send "print version\n"
    expect {
	-re ".\[0-9\]+ = +0x.*\[0-9.\]+.*$prompt $" {
	    pass "printed version"
	}
	-re ".\[0-9\]+ = +.+ +0x.*\[0-9.\]+.*$prompt $" {
	    pass "printed version with cast"
	}
	-re ".*$prompt $"	{ fail "printed version" }
	timeout		{ fail "(timeout) printed version" }
    }

    # lets do a few single steps

    set description "step over symarg initialization"
    send "step\n"
    expect {
	-re "char \[*\]+execarg = NULL;.*$prompt $" {
	    pass "$description"
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    set description "step over execarg initialization"
    send "step\n"
    expect {
	-re "char \[*\]corearg = NULL;.*$prompt $" {
	    pass "$description"
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    set description "step over corearg initialization"
    send "step\n"
    expect {
	-re "char \[*\]cdarg = NULL;.*$prompt $" {
	    pass "$description"
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }
    
    set description "step over cdarg initialization"
    send "step\n"
    expect {
	-re "char \[*\]ttyarg = NULL;.*$prompt $" {
	    pass "$description"
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }
    
    set description "step over ttyarg initialization"
    send "step\n"
    expect {
	-re ".*time_at_startup = get_run_time .*$prompt $" {
	    pass "$description"
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    # now jump over a few functions

    set description "next over get_run_time and everything it calls"
    set got_over_get_run_time 0
    send "next\n"
    expect {
	-re ".*init_malloc.*$prompt $" {
	    pass "$description"
	    set got_over_get_run_time 1
       }
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	    set got_over_get_run_time 1
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	    set got_over_get_run_time 1
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }
    #    -re "if \(setmp \(to_top_level\)\).*$prompt $" { pass "first next" }

    if $got_over_get_run_time then {
	set description "next over init_malloc and everything it calls"
	send "next\n"
	expect {
	    -re "if.*SET_TOP_LEVEL.*$prompt $" {
	        pass "$description"
	    }
	    -re "i = .*count.*$prompt $" {
	      pass "$description"
	      set description "next over ALIGN_STACK_ON_STARTUP code"
	      send "next\n"
	      expect {
		-re "if.*i != 0.*$prompt $" {
		    pass "$description"
		    send "next\n"
		    expect {
			-re "if.*SET_TOP_LEVEL.*$prompt $" {
			    pass "$description"
			}
			-re "alloca.*$prompt $" {
			    send "next\n"
			    expect {
				-re "if.*level.*$prompt $" {
				    pass "$description"
				}
			    }
			}
		    }
		}
	      }
	    }
	    -re ".*No such file or directory.\r\n$prompt $" {
		pass "$description (no source available)"
	    }
	    -re ".*A file or directory .* does not exist..\r\n$prompt $" {
		pass "$description (no source available)"
	    }
	    -re ".*$prompt $" {
		fail "$description"
	    }
	    default {
		fail "$description (timeout or eof)"
	    }
	}
    }

    set next_ok 1
    set need_to_step_over_cmdsize 1
    set description "next over SET_TOP_LEVEL call"
    send "next\n"
    expect {
	-re "cmdsize = 1;.*$prompt $" {
	    pass "$description"
	}
	-re "cmdarg = .*xmalloc.*$prompt $" {
	    # This can happen in optimized code where the "cmdsize = 1" line
	    # is basically removed.
	    pass "$description"
	    set need_to_step_over_cmdsize 0
	}
	-re ".*No such file or directory.\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*A file or directory .* does not exist..\r\n$prompt $" {
	    pass "$description (no source available)"
	}
	-re ".*$prompt $" {
	    fail "$description"
	    set next_ok 0
	}
	timeout {
	    fail "$description (timeout)"
	    set next_ok 0
	}
    }
    
    set description "print a string"
    send "print \"foo\"\n"
    expect {
	-re ".\[0-9\]+ = \"foo\".*$prompt $" {
	    pass "$description"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    if $need_to_step_over_cmdsize {    
	set description "step over cmdsize initialization"
	send "step\n"
	expect {
	    -re "cmdarg = .*xmalloc.*$prompt $" {
		pass "$description"
	    }
	    -re ".*No such file or directory.\r\n$prompt $" {
		pass "$description (no source available)"
	    }
	    -re ".*A file or directory .* does not exist..\r\n$prompt $" {
		pass "$description (no source available)"
	    }
	    -re ".*$prompt $" {
		fail "$description"
	    }
	    timeout {
		fail "$description (timeout)"
	    }
	}
    }

    # and another step for good luck
    if $next_ok {
	set description "step into xmalloc call"
        send "step\n"
        expect {
	    -re "xmalloc.*size=.*at.*utils.c.*$prompt $" {
		pass "$description"
	    }
	    -re ".*No such file or directory.\r\n$prompt $" {
	        pass "$description (no source available)"
	    }
	    -re "A file or directory .* does not exist..\r\n$prompt $" {
		pass "$description (no source available)"
	    }
	    -re ".*$prompt $" {
		fail "$description"
	    }
	    timeout {
		fail "$description (timeout)"
	    }
	}
    }

    # start the "xgdb" process
    send "continue\n"
    expect {
	-re "GDB is free software and you are welcome to distribute copies of it.*
 under certain conditions; type \"show copying\" to see the conditions..*
There is absolutely no warranty for GDB; type \"show warranty\" for details..*
GDB.*Copyright \[0-9\]+ Free Software Foundation, Inc..*$prompt $"\
	    { pass "xgdb is at prompt" }
	-re ".*$prompt $"       { fail "xgdb is at prompt" }
	timeout	            { fail "(timeout) xgdb is at prompt" }
    }
    
    # set xgdb prompt so we can tell which is which
    send "set prompt (xgdb) \n"
    expect {
	-re "\[(\]xgdb\[)\].*\[(\]xgdb\[)\] $"	{ pass "Set xgdb prompt" }
	-re ".*$prompt $"		{ fail "Set xgdb prompt" }
	default				{ fail "(timeout) Set xgdb prompt" }
    }
    
    # kill the xgdb process
    set description "send ^C to child process"
    send "\003"
    expect {
	-re "Program received signal SIGINT.*$prompt $" {
	    pass "$description"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }
    
    set description "send SIGINT signal to child process"
    send "signal SIGINT\n"
    expect {
	-re "Continuing with signal SIGINT.*$prompt $" {
	    pass "$description"
	}
	-re ".*$prompt $" {
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }
    
    # get a stack trace
    set description "backtrace through signal handler"
    send "backtrace\n"
    expect {
	-re "#0.*read.*in main \\(.*\\) at .*main\\.c.*$prompt $" {
	    pass "$description"
	}
	-re ".*$prompt $" {
	    # On the alpha, we hit the infamous problem about gdb
	    # being unable to get the frame pointer (mentioned in
	    # gdb/README).  As it is intermittent, there is no way to
	    # XFAIL it which will give us an XPASS if the problem goes
	    # away.
	    setup_xfail "alpha*-*-osf*"
	    fail "$description"
	}
	timeout {
	    fail "$description (timeout)"
	}
    }

    gdb_exit

    # Set the timeout back to the value it had when we were called.

    # Restart gdb in case next test expects it to be started already.
    gdb_start
    return 0
}

# Find a pathname to a file that we would execute if the shell was asked
# to run $arg using the current PATH.

proc find_gdb { arg } {

    # If the arg directly specifies an existing executable file, then
    # simply use it.

    if [file executable $arg] then {
	return $arg
    }

    set result [which $arg]
    if [string match "/" [ string range $result 0 0 ]] then {
	return $result
    }

    # If everything fails, just return the unqualified pathname as default
    # and hope for best.

    return $arg
}

# Run the test with self.
# Copy the file executable file in case this OS doesn't like to edit it's own
# text space.

set GDB_FULLPATH [find_gdb $GDB]

# Remove any old copy lying around.
catch "exec rm -f ./x$tool"

if ![file executable $GDB_FULLPATH] then {
    fail "couldn't convert $GDB to absolute pathname to make local copy"
} else {
    if [catch "exec cp $GDB_FULLPATH ./x$tool"] then {
	fail "couldn't copy $GDB_FULLPATH to current directory"
    } else {
	verbose "\t\tCopied $GDB_FULLPATH to ./x$tool"
	if {[test_with_self] <0} then {
	    warning "Couldn't test self"
	    catch "exec rm -f ./x$tool"
	    return -1
	}
	catch "exec rm -f ./x$tool"
    }
}