OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [gdb/] [testsuite/] [lib/] [gdb.exp] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
# Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
2
# Free Software Foundation, Inc.
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
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
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.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
 
18
# Please email any bugs, comments, and/or additions to this file to:
19
# bug-gdb@prep.ai.mit.edu
20
 
21
# This file was written by Fred Fish. (fnf@cygnus.com)
22
 
23
# Generic gdb subroutines that should work for any target.  If these
24
# need to be modified for any target, it can be done with a variable
25
# or by passing arguments.
26
 
27
load_lib libgloss.exp
28
 
29
global GDB
30
# OBSOLETE global CHILL_LIB
31
# OBSOLETE global CHILL_RT0
32
 
33
# OBSOLETE if ![info exists CHILL_LIB] {
34
# OBSOLETE     set CHILL_LIB [findfile $base_dir/../../gcc/ch/runtime/libchill.a "$base_dir/../../gcc/ch/runtime/libchill.a" [transform -lchill]]
35
# OBSOLETE }
36
# OBSOLETE verbose "using CHILL_LIB = $CHILL_LIB" 2
37
# OBSOLETE if ![info exists CHILL_RT0] {
38
# OBSOLETE     set CHILL_RT0 [findfile $base_dir/../../gcc/ch/runtime/chillrt0.o "$base_dir/../../gcc/ch/runtime/chillrt0.o" ""]
39
# OBSOLETE }
40
# OBSOLETE verbose "using CHILL_RT0 = $CHILL_RT0" 2
41
 
42
if [info exists TOOL_EXECUTABLE] {
43
    set GDB $TOOL_EXECUTABLE;
44
}
45
if ![info exists GDB] {
46
    if ![is_remote host] {
47
        set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
48
    } else {
49
        set GDB [transform gdb];
50
    }
51
}
52
verbose "using GDB = $GDB" 2
53
 
54
global GDBFLAGS
55
if ![info exists GDBFLAGS] {
56
    set GDBFLAGS "-nx"
57
}
58
verbose "using GDBFLAGS = $GDBFLAGS" 2
59
 
60
# The variable gdb_prompt is a regexp which matches the gdb prompt.
61
# Set it if it is not already set.
62
global gdb_prompt
63
if ![info exists gdb_prompt] then {
64
    set gdb_prompt "\[(\]gdb\[)\]"
65
}
66
 
67
# Needed for some tests under Cygwin.
68
global EXEEXT
69
global env
70
 
71
if ![info exists env(EXEEXT)] {
72
    set EXEEXT ""
73
} else {
74
    set EXEEXT $env(EXEEXT)
75
}
76
 
77
### Only procedures should come after this point.
78
 
79
#
80
# gdb_version -- extract and print the version number of GDB
81
#
82
proc default_gdb_version {} {
83
    global GDB
84
    global GDBFLAGS
85
    global gdb_prompt
86
    set fileid [open "gdb_cmd" w];
87
    puts $fileid "q";
88
    close $fileid;
89
    set cmdfile [remote_download host "gdb_cmd"];
90
    set output [remote_exec host "$GDB -nw --command $cmdfile"]
91
    remote_file build delete "gdb_cmd";
92
    remote_file host delete "$cmdfile";
93
    set tmp [lindex $output 1];
94
    set version ""
95
    regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
96
    if ![is_remote host] {
97
        clone_output "[which $GDB] version $version $GDBFLAGS\n"
98
    } else {
99
        clone_output "$GDB on remote host version $version $GDBFLAGS\n"
100
    }
101
}
102
 
103
proc gdb_version { } {
104
    return [default_gdb_version];
105
}
106
 
107
#
108
# gdb_unload -- unload a file if one is loaded
109
#
110
 
111
proc gdb_unload {} {
112
    global verbose
113
    global GDB
114
    global gdb_prompt
115
    send_gdb "file\n"
116
    gdb_expect 60 {
117
        -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
118
        -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
119
        -re "A program is being debugged already..*Kill it.*y or n. $"\
120
            { send_gdb "y\n"
121
                verbose "\t\tKilling previous program being debugged"
122
            exp_continue
123
        }
124
        -re "Discard symbol table from .*y or n.*$" {
125
            send_gdb "y\n"
126
            exp_continue
127
        }
128
        -re "$gdb_prompt $" {}
129
        timeout {
130
            perror "couldn't unload file in $GDB (timed out)."
131
            return -1
132
        }
133
    }
134
}
135
 
136
# Many of the tests depend on setting breakpoints at various places and
137
# running until that breakpoint is reached.  At times, we want to start
138
# with a clean-slate with respect to breakpoints, so this utility proc
139
# lets us do this without duplicating this code everywhere.
140
#
141
 
142
proc delete_breakpoints {} {
143
    global gdb_prompt
144
 
145
    # we need a larger timeout value here or this thing just confuses
146
    # itself.  May need a better implementation if possible. - guo
147
    #
148
    send_gdb "delete breakpoints\n"
149
    gdb_expect 100 {
150
         -re "Delete all breakpoints.*y or n.*$" {
151
            send_gdb "y\n";
152
            exp_continue
153
        }
154
         -re "$gdb_prompt $" { # This happens if there were no breakpoints
155
            }
156
         timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
157
    }
158
    send_gdb "info breakpoints\n"
159
    gdb_expect 100 {
160
         -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
161
         -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
162
         -re "Delete all breakpoints.*or n.*$" {
163
            send_gdb "y\n";
164
            exp_continue
165
        }
166
         timeout { perror "info breakpoints (timeout)" ; return }
167
    }
168
}
169
 
170
 
171
#
172
# Generic run command.
173
#
174
# The second pattern below matches up to the first newline *only*.
175
# Using ``.*$'' could swallow up output that we attempt to match
176
# elsewhere.
177
#
178
proc gdb_run_cmd {args} {
179
    global gdb_prompt
180
 
181
    if [target_info exists gdb_init_command] {
182
        send_gdb "[target_info gdb_init_command]\n";
183
        gdb_expect 30 {
184
            -re "$gdb_prompt $" { }
185
            default {
186
                perror "gdb_init_command for target failed";
187
                return;
188
            }
189
        }
190
    }
191
 
192
    if [target_info exists use_gdb_stub] {
193
        if [target_info exists gdb,do_reload_on_run] {
194
            # Specifying no file, defaults to the executable
195
            # currently being debugged.
196
            if { [gdb_load ""] < 0 } {
197
                return;
198
            }
199
            send_gdb "continue\n";
200
            gdb_expect 60 {
201
                -re "Continu\[^\r\n\]*\[\r\n\]" {}
202
                default {}
203
            }
204
            return;
205
        }
206
 
207
        if [target_info exists gdb,start_symbol] {
208
            set start [target_info gdb,start_symbol];
209
        } else {
210
            set start "start";
211
        }
212
        send_gdb  "jump *$start\n"
213
        set start_attempt 1;
214
        while { $start_attempt } {
215
            # Cap (re)start attempts at three to ensure that this loop
216
            # always eventually fails.  Don't worry about trying to be
217
            # clever and not send a command when it has failed.
218
            if [expr $start_attempt > 3] {
219
                perror "Jump to start() failed (retry count exceeded)";
220
                return;
221
            }
222
            set start_attempt [expr $start_attempt + 1];
223
            gdb_expect 30 {
224
                -re "Continuing at \[^\r\n\]*\[\r\n\]" {
225
                    set start_attempt 0;
226
                }
227
                -re "No symbol \"_start\" in current.*$gdb_prompt $" {
228
                    perror "Can't find start symbol to run in gdb_run";
229
                    return;
230
                }
231
                -re "No symbol \"start\" in current.*$gdb_prompt $" {
232
                    send_gdb "jump *_start\n";
233
                }
234
                -re "No symbol.*context.*$gdb_prompt $" {
235
                    set start_attempt 0;
236
                }
237
                -re "Line.* Jump anyway.*y or n. $" {
238
                    send_gdb "y\n"
239
                }
240
                -re "The program is not being run.*$gdb_prompt $" {
241
                    if { [gdb_load ""] < 0 } {
242
                        return;
243
                    }
244
                    send_gdb "jump *$start\n";
245
                }
246
                timeout {
247
                    perror "Jump to start() failed (timeout)";
248
                    return
249
                }
250
            }
251
        }
252
        if [target_info exists gdb_stub] {
253
            gdb_expect 60 {
254
                -re "$gdb_prompt $" {
255
                    send_gdb "continue\n"
256
                }
257
            }
258
        }
259
        return
260
    }
261
    send_gdb "run $args\n"
262
# This doesn't work quite right yet.
263
    gdb_expect 60 {
264
        -re "The program .* has been started already.*y or n. $" {
265
            send_gdb "y\n"
266
            exp_continue
267
        }
268
        -re "Starting program: \[^\r\n\]*" {}
269
    }
270
}
271
 
272
proc gdb_breakpoint { function } {
273
    global gdb_prompt
274
    global decimal
275
 
276
    send_gdb "break $function\n"
277
    # The first two regexps are what we get with -g, the third is without -g.
278
    gdb_expect 30 {
279
        -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
280
        -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
281
        -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
282
        -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
283
        timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
284
    }
285
    return 1;
286
}
287
 
288
# Set breakpoint at function and run gdb until it breaks there.
289
# Since this is the only breakpoint that will be set, if it stops
290
# at a breakpoint, we will assume it is the one we want.  We can't
291
# just compare to "function" because it might be a fully qualified,
292
# single quoted C++ function specifier.
293
 
294
proc runto { function } {
295
    global gdb_prompt
296
    global decimal
297
 
298
    delete_breakpoints
299
 
300
    if ![gdb_breakpoint $function] {
301
        return 0;
302
    }
303
 
304
    gdb_run_cmd
305
 
306
    # the "at foo.c:36" output we get with -g.
307
    # the "in func" output we get without -g.
308
    gdb_expect 30 {
309
        -re "Break.* at .*:$decimal.*$gdb_prompt $" {
310
            return 1
311
        }
312
        -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
313
            return 1
314
        }
315
        -re "$gdb_prompt $" {
316
            fail "running to $function in runto"
317
            return 0
318
        }
319
        timeout {
320
            fail "running to $function in runto (timeout)"
321
            return 0
322
        }
323
    }
324
    return 1
325
}
326
 
327
#
328
# runto_main -- ask gdb to run until we hit a breakpoint at main.
329
#               The case where the target uses stubs has to be handled
330
#               specially--if it uses stubs, assuming we hit
331
#               breakpoint() and just step out of the function.
332
#
333
proc runto_main { } {
334
    global gdb_prompt
335
    global decimal
336
 
337
    if ![target_info exists gdb_stub] {
338
        return [runto main]
339
    }
340
 
341
    delete_breakpoints
342
 
343
    gdb_step_for_stub;
344
 
345
    return 1
346
}
347
 
348
 
349
### Continue, and expect to hit a breakpoint.
350
### Report a pass or fail, depending on whether it seems to have
351
### worked.  Use NAME as part of the test name; each call to
352
### continue_to_breakpoint should use a NAME which is unique within
353
### that test file.
354
proc gdb_continue_to_breakpoint {name} {
355
    global gdb_prompt
356
    set full_name "continue to breakpoint: $name"
357
 
358
    send_gdb "continue\n"
359
    gdb_expect {
360
        -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
361
            pass $full_name
362
        }
363
        -re ".*$gdb_prompt $" {
364
            fail $full_name
365
        }
366
        timeout {
367
            fail "$full_name (timeout)"
368
        }
369
    }
370
}
371
 
372
 
373
# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
374
# Send a command to gdb; test the result.
375
#
376
# COMMAND is the command to execute, send to GDB with send_gdb.  If
377
#   this is the null string no command is sent.
378
# PATTERN is the pattern to match for a PASS, and must NOT include
379
#   the \r\n sequence immediately before the gdb prompt.
380
# MESSAGE is an optional message to be printed.  If this is
381
#   omitted, then the pass/fail messages use the command string as the
382
#   message.  (If this is the empty string, then sometimes we don't
383
#   call pass or fail at all; I don't understand this at all.)
384
# QUESTION is a question GDB may ask in response to COMMAND, like
385
#   "are you sure?"
386
# RESPONSE is the response to send if QUESTION appears.
387
#
388
# Returns:
389
#    1 if the test failed,
390
#    0 if the test passes,
391
#   -1 if there was an internal error.
392
#
393
proc gdb_test { args } {
394
    global verbose
395
    global gdb_prompt
396
    global GDB
397
    upvar timeout timeout
398
 
399
    if [llength $args]>2 then {
400
        set message [lindex $args 2]
401
    } else {
402
        set message [lindex $args 0]
403
    }
404
    set command [lindex $args 0]
405
    set pattern [lindex $args 1]
406
 
407
    if [llength $args]==5 {
408
        set question_string [lindex $args 3];
409
        set response_string [lindex $args 4];
410
    } else {
411
        set question_string "^FOOBAR$"
412
    }
413
 
414
    if $verbose>2 then {
415
        send_user "Sending \"$command\" to gdb\n"
416
        send_user "Looking to match \"$pattern\"\n"
417
        send_user "Message is \"$message\"\n"
418
    }
419
 
420
    set result -1
421
    set string "${command}\n";
422
    if { $command != "" } {
423
        while { "$string" != "" } {
424
            set foo [string first "\n" "$string"];
425
            set len [string length "$string"];
426
            if { $foo < [expr $len - 1] } {
427
                set str [string range "$string" 0 $foo];
428
                if { [send_gdb "$str"] != "" } {
429
                    global suppress_flag;
430
 
431
                    if { ! $suppress_flag } {
432
                        perror "Couldn't send $command to GDB.";
433
                    }
434
                    fail "$message";
435
                    return $result;
436
                }
437
                # since we're checking if each line of the multi-line
438
                # command are 'accepted' by GDB here,
439
                # we need to set -notransfer expect option so that
440
                # command output is not lost for pattern matching
441
                # - guo
442
                gdb_expect 2 {
443
                    -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
444
                    timeout { verbose "partial: timeout" 3 }
445
                }
446
                set string [string range "$string" [expr $foo + 1] end];
447
            } else {
448
                break;
449
            }
450
        }
451
        if { "$string" != "" } {
452
            if { [send_gdb "$string"] != "" } {
453
                global suppress_flag;
454
 
455
                if { ! $suppress_flag } {
456
                    perror "Couldn't send $command to GDB.";
457
                }
458
                fail "$message";
459
                return $result;
460
            }
461
        }
462
    }
463
 
464
    if [target_info exists gdb,timeout] {
465
        set tmt [target_info gdb,timeout];
466
    } else {
467
        if [info exists timeout] {
468
            set tmt $timeout;
469
        } else {
470
            global timeout;
471
            if [info exists timeout] {
472
                set tmt $timeout;
473
            } else {
474
                set tmt 60;
475
            }
476
        }
477
    }
478
    gdb_expect $tmt {
479
         -re "\\*\\*\\* DOSEXIT code.*" {
480
             if { $message != "" } {
481
                 fail "$message";
482
             }
483
             gdb_suppress_entire_file "GDB died";
484
             return -1;
485
         }
486
         -re "Ending remote debugging.*$gdb_prompt $" {
487
            if ![isnative] then {
488
                warning "Can`t communicate to remote target."
489
            }
490
            gdb_exit
491
            gdb_start
492
            set result -1
493
        }
494
         -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
495
            if ![string match "" $message] then {
496
                pass "$message"
497
            }
498
            set result 0
499
        }
500
         -re "(${question_string})$" {
501
            send_gdb "$response_string\n";
502
            exp_continue;
503
        }
504
         -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
505
            perror "Undefined command \"$command\"."
506
            fail "$message"
507
            set result 1
508
        }
509
         -re "Ambiguous command.*$gdb_prompt $" {
510
            perror "\"$command\" is not a unique command name."
511
            fail "$message"
512
            set result 1
513
        }
514
         -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
515
            if ![string match "" $message] then {
516
                set errmsg "$message: the program exited"
517
            } else {
518
                set errmsg "$command: the program exited"
519
            }
520
            fail "$errmsg"
521
            return -1
522
        }
523
         -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
524
            if ![string match "" $message] then {
525
                set errmsg "$message: the program exited"
526
            } else {
527
                set errmsg "$command: the program exited"
528
            }
529
            fail "$errmsg"
530
            return -1
531
        }
532
         -re "The program is not being run.*$gdb_prompt $" {
533
            if ![string match "" $message] then {
534
                set errmsg "$message: the program is no longer running"
535
            } else {
536
                set errmsg "$command: the program is no longer running"
537
            }
538
            fail "$errmsg"
539
            return -1
540
        }
541
         -re ".*$gdb_prompt $" {
542
            if ![string match "" $message] then {
543
                fail "$message"
544
            }
545
            set result 1
546
        }
547
         "" {
548
            send_gdb "\n"
549
            perror "Window too small."
550
            fail "$message"
551
        }
552
         -re "\\(y or n\\) " {
553
            send_gdb "n\n"
554
            perror "Got interactive prompt."
555
            fail "$message"
556
        }
557
         eof {
558
             perror "Process no longer exists"
559
             if { $message != "" } {
560
                 fail "$message"
561
             }
562
             return -1
563
        }
564
         full_buffer {
565
            perror "internal buffer is full."
566
            fail "$message"
567
        }
568
        timeout {
569
            if ![string match "" $message] then {
570
                fail "$message (timeout)"
571
            }
572
            set result 1
573
        }
574
    }
575
    return $result
576
}
577
 
578
# Test that a command gives an error.  For pass or fail, return
579
# a 1 to indicate that more tests can proceed.  However a timeout
580
# is a serious error, generates a special fail message, and causes
581
# a 0 to be returned to indicate that more tests are likely to fail
582
# as well.
583
 
584
proc test_print_reject { args } {
585
    global gdb_prompt
586
    global verbose
587
 
588
    if [llength $args]==2 then {
589
        set expectthis [lindex $args 1]
590
    } else {
591
        set expectthis "should never match this bogus string"
592
    }
593
    set sendthis [lindex $args 0]
594
    if $verbose>2 then {
595
        send_user "Sending \"$sendthis\" to gdb\n"
596
        send_user "Looking to match \"$expectthis\"\n"
597
    }
598
    send_gdb "$sendthis\n"
599
    #FIXME: Should add timeout as parameter.
600
    gdb_expect {
601
        -re "A .* in expression.*\\.*$gdb_prompt $" {
602
            pass "reject $sendthis"
603
            return 1
604
        }
605
        -re "Invalid syntax in expression.*$gdb_prompt $" {
606
            pass "reject $sendthis"
607
            return 1
608
        }
609
        -re "Junk after end of expression.*$gdb_prompt $" {
610
            pass "reject $sendthis"
611
            return 1
612
        }
613
        -re "Invalid number.*$gdb_prompt $" {
614
            pass "reject $sendthis"
615
            return 1
616
        }
617
        -re "Invalid character constant.*$gdb_prompt $" {
618
            pass "reject $sendthis"
619
            return 1
620
        }
621
        -re "No symbol table is loaded.*$gdb_prompt $" {
622
            pass "reject $sendthis"
623
            return 1
624
        }
625
        -re "No symbol .* in current context.*$gdb_prompt $" {
626
            pass "reject $sendthis"
627
            return 1
628
        }
629
        -re "Unmatched single quote.*$gdb_prompt $" {
630
            pass "reject $sendthis"
631
            return 1
632
        }
633
        -re "A character constant must contain at least one character.*$gdb_prompt $" {
634
            pass "reject $sendthis"
635
            return 1
636
        }
637
        -re "$expectthis.*$gdb_prompt $" {
638
            pass "reject $sendthis"
639
            return 1
640
        }
641
        -re ".*$gdb_prompt $" {
642
            fail "reject $sendthis"
643
            return 1
644
        }
645
        default {
646
            fail "reject $sendthis (eof or timeout)"
647
            return 0
648
        }
649
    }
650
}
651
 
652
# Given an input string, adds backslashes as needed to create a
653
# regexp that will match the string.
654
 
655
proc string_to_regexp {str} {
656
    set result $str
657
    regsub -all {[]*+.|()^$\[]} $str {\\&} result
658
    return $result
659
}
660
 
661
# Same as gdb_test, but the second parameter is not a regexp,
662
# but a string that must match exactly.
663
 
664
proc gdb_test_exact { args } {
665
    upvar timeout timeout
666
 
667
    set command [lindex $args 0]
668
 
669
    # This applies a special meaning to a null string pattern.  Without
670
    # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
671
    # messages from commands that should have no output except a new
672
    # prompt.  With this, only results of a null string will match a null
673
    # string pattern.
674
 
675
    set pattern [lindex $args 1]
676
    if [string match $pattern ""] {
677
        set pattern [string_to_regexp [lindex $args 0]]
678
    } else {
679
        set pattern [string_to_regexp [lindex $args 1]]
680
    }
681
 
682
    # It is most natural to write the pattern argument with only
683
    # embedded \n's, especially if you are trying to avoid Tcl quoting
684
    # problems.  But gdb_expect really wants to see \r\n in patterns.  So
685
    # transform the pattern here.  First transform \r\n back to \n, in
686
    # case some users of gdb_test_exact already do the right thing.
687
    regsub -all "\r\n" $pattern "\n" pattern
688
    regsub -all "\n" $pattern "\r\n" pattern
689
    if [llength $args]==3 then {
690
        set message [lindex $args 2]
691
    } else {
692
        set message $command
693
    }
694
 
695
    return [gdb_test $command $pattern $message]
696
}
697
 
698
proc gdb_reinitialize_dir { subdir } {
699
    global gdb_prompt
700
 
701
    if [is_remote host] {
702
        return "";
703
    }
704
    send_gdb "dir\n"
705
    gdb_expect 60 {
706
        -re "Reinitialize source path to empty.*y or n. " {
707
            send_gdb "y\n"
708
            gdb_expect 60 {
709
                -re "Source directories searched.*$gdb_prompt $" {
710
                    send_gdb "dir $subdir\n"
711
                    gdb_expect 60 {
712
                        -re "Source directories searched.*$gdb_prompt $" {
713
                            verbose "Dir set to $subdir"
714
                        }
715
                        -re "$gdb_prompt $" {
716
                            perror "Dir \"$subdir\" failed."
717
                        }
718
                    }
719
                }
720
                -re "$gdb_prompt $" {
721
                    perror "Dir \"$subdir\" failed."
722
                }
723
            }
724
        }
725
        -re "$gdb_prompt $" {
726
            perror "Dir \"$subdir\" failed."
727
        }
728
    }
729
}
730
 
731
#
732
# gdb_exit -- exit the GDB, killing the target program if necessary
733
#
734
proc default_gdb_exit {} {
735
    global GDB
736
    global GDBFLAGS
737
    global verbose
738
    global gdb_spawn_id;
739
 
740
    gdb_stop_suppressing_tests;
741
 
742
    if ![info exists gdb_spawn_id] {
743
        return;
744
    }
745
 
746
    verbose "Quitting $GDB $GDBFLAGS"
747
 
748
    if { [is_remote host] && [board_info host exists fileid] } {
749
        send_gdb "quit\n";
750
        gdb_expect 10 {
751
            -re "y or n" {
752
                send_gdb "y\n";
753
                exp_continue;
754
            }
755
            -re "DOSEXIT code" { }
756
            default { }
757
        }
758
    }
759
 
760
    if ![is_remote host] {
761
        remote_close host;
762
    }
763
    unset gdb_spawn_id
764
}
765
 
766
#
767
# load a file into the debugger.
768
# return a -1 if anything goes wrong.
769
#
770
proc gdb_file_cmd { arg } {
771
    global verbose
772
    global loadpath
773
    global loadfile
774
    global GDB
775
    global gdb_prompt
776
    upvar timeout timeout
777
 
778
    if [is_remote host] {
779
        set arg [remote_download host $arg];
780
        if { $arg == "" } {
781
            error "download failed"
782
            return -1;
783
        }
784
    }
785
 
786
    send_gdb "file $arg\n"
787
    gdb_expect 120 {
788
        -re "Reading symbols from.*done.*$gdb_prompt $" {
789
            verbose "\t\tLoaded $arg into the $GDB"
790
            return 0
791
        }
792
        -re "has no symbol-table.*$gdb_prompt $" {
793
            perror "$arg wasn't compiled with \"-g\""
794
            return -1
795
        }
796
        -re "A program is being debugged already.*Kill it.*y or n. $" {
797
            send_gdb "y\n"
798
                verbose "\t\tKilling previous program being debugged"
799
            exp_continue
800
        }
801
        -re "Load new symbol table from \".*\".*y or n. $" {
802
            send_gdb "y\n"
803
            gdb_expect 120 {
804
                -re "Reading symbols from.*done.*$gdb_prompt $" {
805
                    verbose "\t\tLoaded $arg with new symbol table into $GDB"
806
                    return 0
807
                }
808
                timeout {
809
                    perror "(timeout) Couldn't load $arg, other program already loaded."
810
                    return -1
811
                }
812
            }
813
        }
814
        -re "No such file or directory.*$gdb_prompt $" {
815
            perror "($arg) No such file or directory\n"
816
            return -1
817
        }
818
        -re "$gdb_prompt $" {
819
            perror "couldn't load $arg into $GDB."
820
            return -1
821
            }
822
        timeout {
823
            perror "couldn't load $arg into $GDB (timed out)."
824
            return -1
825
        }
826
        eof {
827
            # This is an attempt to detect a core dump, but seems not to
828
            # work.  Perhaps we need to match .* followed by eof, in which
829
            # gdb_expect does not seem to have a way to do that.
830
            perror "couldn't load $arg into $GDB (end of file)."
831
            return -1
832
        }
833
    }
834
}
835
 
836
#
837
# start gdb -- start gdb running, default procedure
838
#
839
# When running over NFS, particularly if running many simultaneous
840
# tests on different hosts all using the same server, things can
841
# get really slow.  Give gdb at least 3 minutes to start up.
842
#
843
proc default_gdb_start { } {
844
    global verbose
845
    global GDB
846
    global GDBFLAGS
847
    global gdb_prompt
848
    global timeout
849
    global gdb_spawn_id;
850
 
851
    gdb_stop_suppressing_tests;
852
 
853
    verbose "Spawning $GDB -nw $GDBFLAGS"
854
 
855
    if [info exists gdb_spawn_id] {
856
        return 0;
857
    }
858
 
859
    if ![is_remote host] {
860
        if { [which $GDB] == 0 } then {
861
            perror "$GDB does not exist."
862
            exit 1
863
        }
864
    }
865
    set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
866
    if { $res < 0 || $res == "" } {
867
        perror "Spawning $GDB failed."
868
        return 1;
869
    }
870
    gdb_expect 360 {
871
        -re "\[\r\n\]$gdb_prompt $" {
872
            verbose "GDB initialized."
873
        }
874
        -re "$gdb_prompt $"     {
875
            perror "GDB never initialized."
876
            return -1
877
        }
878
        timeout {
879
            perror "(timeout) GDB never initialized after 10 seconds."
880
            remote_close host;
881
            return -1
882
        }
883
    }
884
    set gdb_spawn_id -1;
885
    # force the height to "unlimited", so no pagers get used
886
 
887
    send_gdb "set height 0\n"
888
    gdb_expect 10 {
889
        -re "$gdb_prompt $" {
890
            verbose "Setting height to 0." 2
891
        }
892
        timeout {
893
            warning "Couldn't set the height to 0"
894
        }
895
    }
896
    # force the width to "unlimited", so no wraparound occurs
897
    send_gdb "set width 0\n"
898
    gdb_expect 10 {
899
        -re "$gdb_prompt $" {
900
            verbose "Setting width to 0." 2
901
        }
902
        timeout {
903
            warning "Couldn't set the width to 0."
904
        }
905
    }
906
    return 0;
907
}
908
 
909
# Return a 1 for configurations for which we don't even want to try to
910
# test C++.
911
 
912
proc skip_cplus_tests {} {
913
    if { [istarget "d10v-*-*"] } {
914
        return 1
915
    }
916
    if { [istarget "h8300-*-*"] } {
917
        return 1
918
    }
919
    return 0
920
}
921
 
922
# OBSOLETE # * For crosses, the CHILL runtime doesn't build because it
923
# OBSOLETE # can't find setjmp.h, stdio.h, etc.
924
# OBSOLETE # * For AIX (as of 16 Mar 95), (a) there is no language code for
925
# OBSOLETE # CHILL in output_epilog in gcc/config/rs6000/rs6000.c, (b) collect2
926
# OBSOLETE # does not get along with AIX's too-clever linker.
927
# OBSOLETE # * On Irix5, there is a bug whereby set of bool, etc., don't get
928
# OBSOLETE # TYPE_LOW_BOUND for the bool right because force_to_range_type doesn't
929
# OBSOLETE # work with stub types.
930
# OBSOLETE # Lots of things seem to fail on the PA, and since it's not a supported
931
# OBSOLETE # chill target at the moment, don't run the chill tests.
932
 
933
# OBSOLETE proc skip_chill_tests {} {
934
# OBSOLETE     if ![info exists do_chill_tests] {
935
# OBSOLETE      return 1;
936
# OBSOLETE     }
937
# OBSOLETE     eval set skip_chill [expr ![isnative] || [istarget "*-*-aix*"] || [istarget "*-*-irix5*"] || [istarget "*-*-irix6*"] || [istarget "alpha-*-osf*"] || [istarget "hppa*-*-*"]]
938
# OBSOLETE     verbose "Skip chill tests is $skip_chill"
939
# OBSOLETE     return $skip_chill
940
# OBSOLETE }
941
 
942
# Skip all the tests in the file if you are not on an hppa running
943
# hpux target.
944
 
945
proc skip_hp_tests {} {
946
    eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
947
    verbose "Skip hp tests is $skip_hp"
948
    return $skip_hp
949
}
950
 
951
proc get_compiler_info {binfile args} {
952
    # Create and source the file that provides information about the compiler
953
    # used to compile the test case.
954
    # Compiler_type can be null or c++. If null we assume c.
955
    global srcdir
956
    global subdir
957
    # These two come from compiler.c.
958
    global signed_keyword_not_used
959
    global gcc_compiled
960
 
961
    if {![istarget "hppa*-*-hpux*"] && ![istarget "mips*-*-irix*"]} {
962
        if { [llength $args] > 0 } {
963
            if {$args == "c++"} {
964
                if { [gdb_compile "${srcdir}/lib/compiler.cc" "${binfile}.ci" preprocess {}] != "" } {
965
                    perror "Couldn't make ${binfile}.ci file"
966
                    return 1;
967
                }
968
            }
969
        } else {
970
            if { [gdb_compile "${srcdir}/lib/compiler.c" "${binfile}.ci" preprocess {}] != "" } {
971
                perror "Couldn't make ${binfile}.ci file"
972
                return 1;
973
            }
974
        }
975
    } else {
976
        if { [llength $args] > 0 } {
977
            if {$args == "c++"} {
978
                if { [eval gdb_preprocess \
979
                        [list "${srcdir}/lib/compiler.cc" "${binfile}.ci"] \
980
                        $args] != "" } {
981
                    perror "Couldn't make ${binfile}.ci file"
982
                    return 1;
983
                }
984
            }
985
        } elseif { $args != "f77" } {
986
            if { [eval gdb_preprocess \
987
                    [list "${srcdir}/lib/compiler.c" "${binfile}.ci"] \
988
                    $args] != "" } {
989
                perror "Couldn't make ${binfile}.ci file"
990
                return 1;
991
            }
992
        }
993
    }
994
 
995
    uplevel \#0 { set gcc_compiled 0 }
996
 
997
    if { [llength $args] == 0 || $args != "f77" } {
998
        source ${binfile}.ci
999
    }
1000
 
1001
    # Most compilers will evaluate comparisons and other boolean
1002
    # operations to 0 or 1.
1003
    uplevel \#0 { set true 1 }
1004
    uplevel \#0 { set false 0 }
1005
 
1006
    uplevel \#0 { set hp_cc_compiler 0 }
1007
    uplevel \#0 { set hp_aCC_compiler 0 }
1008
    uplevel \#0 { set hp_f77_compiler 0 }
1009
    uplevel \#0 { set hp_f90_compiler 0 }
1010
    if { !$gcc_compiled && [istarget "hppa*-*-hpux*"] } {
1011
        # Check for the HP compilers
1012
        set compiler [lindex [split [get_compiler $args] " "] 0]
1013
        catch "exec what $compiler" output
1014
        if [regexp ".*HP aC\\+\\+.*" $output] {
1015
            uplevel \#0 { set hp_aCC_compiler 1 }
1016
            # Use of aCC results in boolean results being displayed as
1017
            # "true" or "false"
1018
            uplevel \#0 { set true true }
1019
            uplevel \#0 { set false false }
1020
        } elseif [regexp ".*HP C Compiler.*" $output] {
1021
            uplevel \#0 { set hp_cc_compiler 1 }
1022
        } elseif [regexp ".*HP-UX f77.*" $output] {
1023
            uplevel \#0 { set hp_f77_compiler 1 }
1024
        } elseif [regexp ".*HP-UX f90.*" $output] {
1025
            uplevel \#0 { set hp_f90_compiler 1 }
1026
        }
1027
    }
1028
 
1029
    return 0;
1030
}
1031
 
1032
proc get_compiler {args} {
1033
    global CC CC_FOR_TARGET CXX CXX_FOR_TARGET F77_FOR_TARGET
1034
 
1035
    if { [llength $args] == 0
1036
         || ([llength $args] == 1 && [lindex $args 0] == "") } {
1037
        set which_compiler "c"
1038
    } else {
1039
        if { $args =="c++" } {
1040
            set which_compiler "c++"
1041
        } elseif { $args =="f77" } {
1042
            set which_compiler "f77"
1043
        } else {
1044
            perror "Unknown compiler type supplied to gdb_preprocess"
1045
            return ""
1046
        }
1047
    }
1048
 
1049
    if [info exists CC_FOR_TARGET] {
1050
        if {$which_compiler == "c"} {
1051
            set compiler $CC_FOR_TARGET
1052
        }
1053
    }
1054
 
1055
    if [info exists CXX_FOR_TARGET] {
1056
        if {$which_compiler == "c++"} {
1057
            set compiler $CXX_FOR_TARGET
1058
        }
1059
    }
1060
 
1061
    if [info exists F77_FOR_TARGET] {
1062
        if {$which_compiler == "f77"} {
1063
            set compiler $F77_FOR_TARGET
1064
        }
1065
    }
1066
 
1067
    if { ![info exists compiler] } {
1068
        if { $which_compiler == "c" } {
1069
            if {[info exists CC]} {
1070
                set compiler $CC
1071
            }
1072
        }
1073
        if { $which_compiler == "c++" } {
1074
            if {[info exists CXX]} {
1075
                set compiler $CXX
1076
            }
1077
        }
1078
        if {![info exists compiler]} {
1079
            set compiler [board_info [target_info name] compiler];
1080
            if { $compiler == "" } {
1081
                perror "get_compiler: No compiler found"
1082
                return ""
1083
            }
1084
        }
1085
    }
1086
 
1087
    return $compiler
1088
}
1089
 
1090
proc gdb_preprocess {source dest args} {
1091
    set compiler [get_compiler "$args"]
1092
    if { $compiler == "" } {
1093
        return 1
1094
    }
1095
 
1096
    set cmdline "$compiler -E $source > $dest"
1097
 
1098
    verbose "Invoking $compiler -E $source > $dest"
1099
    verbose -log "Executing on local host: $cmdline" 2
1100
    set status [catch "exec ${cmdline}" exec_output]
1101
 
1102
    set result [prune_warnings $exec_output]
1103
    regsub "\[\r\n\]*$" "$result" "" result;
1104
    regsub "^\[\r\n\]*" "$result" "" result;
1105
    if { $result != "" } {
1106
        clone_output "gdb compile failed, $result"
1107
    }
1108
    return $result;
1109
}
1110
 
1111
set gdb_wrapper_initialized 0
1112
 
1113
proc gdb_wrapper_init { args } {
1114
    global gdb_wrapper_initialized;
1115
    global gdb_wrapper_file;
1116
    global gdb_wrapper_flags;
1117
 
1118
    if { $gdb_wrapper_initialized == 1 } { return; }
1119
 
1120
    if {[target_info exists needs_status_wrapper] && \
1121
            [target_info needs_status_wrapper] != "0"} {
1122
        set result [build_wrapper "testglue.o"];
1123
        if { $result != "" } {
1124
            set gdb_wrapper_file [lindex $result 0];
1125
            set gdb_wrapper_flags [lindex $result 1];
1126
        } else {
1127
            warning "Status wrapper failed to build."
1128
        }
1129
    }
1130
    set gdb_wrapper_initialized 1
1131
}
1132
 
1133
proc gdb_compile {source dest type options} {
1134
    global GDB_TESTCASE_OPTIONS;
1135
    global gdb_wrapper_file;
1136
    global gdb_wrapper_flags;
1137
    global gdb_wrapper_initialized;
1138
 
1139
    if [target_info exists gdb_stub] {
1140
        set options2 { "additional_flags=-Dusestubs" }
1141
        lappend options "libs=[target_info gdb_stub]";
1142
        set options [concat $options2 $options]
1143
    }
1144
    if [target_info exists is_vxworks] {
1145
        set options2 { "additional_flags=-Dvxworks" }
1146
        lappend options "libs=[target_info gdb_stub]";
1147
        set options [concat $options2 $options]
1148
    }
1149
    if [info exists GDB_TESTCASE_OPTIONS] {
1150
        lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1151
    }
1152
    verbose "options are $options"
1153
    verbose "source is $source $dest $type $options"
1154
 
1155
    if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1156
 
1157
    if {[target_info exists needs_status_wrapper] && \
1158
            [target_info needs_status_wrapper] != "0" && \
1159
            [info exists gdb_wrapper_file]} {
1160
        lappend options "libs=${gdb_wrapper_file}"
1161
        lappend options "ldflags=${gdb_wrapper_flags}"
1162
    }
1163
 
1164
    set result [target_compile $source $dest $type $options];
1165
    regsub "\[\r\n\]*$" "$result" "" result;
1166
    regsub "^\[\r\n\]*" "$result" "" result;
1167
    if { $result != "" } {
1168
        clone_output "gdb compile failed, $result"
1169
    }
1170
    return $result;
1171
}
1172
 
1173
 
1174
# This is just like gdb_compile, above, except that it tries compiling
1175
# against several different thread libraries, to see which one this
1176
# system has.
1177
proc gdb_compile_pthreads {source dest type options} {
1178
    set built_binfile 0
1179
    set why_msg "unrecognized error"
1180
    foreach lib {-lpthreads -lpthread -lthread} {
1181
        # This kind of wipes out whatever libs the caller may have
1182
        # set.  Or maybe theirs will override ours.  How infelicitous.
1183
        set options_with_lib [concat $options [list libs=$lib]]
1184
        set ccout [gdb_compile $source $dest $type $options_with_lib]
1185
        switch -regexp -- $ccout {
1186
            ".*no posix threads support.*" {
1187
                set why_msg "missing threads include file"
1188
                break
1189
            }
1190
            ".*cannot open -lpthread.*" {
1191
                set why_msg "missing runtime threads library"
1192
            }
1193
            ".*Can't find library for -lpthread.*" {
1194
                set why_msg "missing runtime threads library"
1195
            }
1196
            {^$} {
1197
                pass "successfully compiled posix threads test case"
1198
                set built_binfile 1
1199
                break
1200
            }
1201
        }
1202
    }
1203
    if {!$built_binfile} {
1204
        unsupported "Couldn't compile $source: ${why_msg}"
1205
        return -1
1206
    }
1207
}
1208
 
1209
proc send_gdb { string } {
1210
    global suppress_flag;
1211
    if { $suppress_flag } {
1212
        return "suppressed";
1213
    }
1214
    return [remote_send host "$string"];
1215
}
1216
 
1217
#
1218
#
1219
 
1220
proc gdb_expect { args } {
1221
    if { [llength $args] == 2  && [lindex $args 0] != "-re" } {
1222
        set gtimeout [lindex $args 0];
1223
        set expcode [list [lindex $args 1]];
1224
    } else {
1225
        upvar timeout timeout;
1226
 
1227
        set expcode $args;
1228
        if [target_info exists gdb,timeout] {
1229
            if [info exists timeout] {
1230
                if { $timeout < [target_info gdb,timeout] } {
1231
                    set gtimeout [target_info gdb,timeout];
1232
                } else {
1233
                    set gtimeout $timeout;
1234
                }
1235
            } else {
1236
                set gtimeout [target_info gdb,timeout];
1237
            }
1238
        }
1239
 
1240
        if ![info exists gtimeout] {
1241
            global timeout;
1242
            if [info exists timeout] {
1243
                set gtimeout $timeout;
1244
            } else {
1245
                # Eeeeew.
1246
                set gtimeout 60;
1247
            }
1248
        }
1249
    }
1250
    global suppress_flag;
1251
    global remote_suppress_flag;
1252
    if [info exists remote_suppress_flag] {
1253
        set old_val $remote_suppress_flag;
1254
    }
1255
    if [info exists suppress_flag] {
1256
        if { $suppress_flag } {
1257
            set remote_suppress_flag 1;
1258
        }
1259
    }
1260
    set code [catch \
1261
        {uplevel remote_expect host $gtimeout $expcode} string];
1262
    if [info exists old_val] {
1263
        set remote_suppress_flag $old_val;
1264
    } else {
1265
        if [info exists remote_suppress_flag] {
1266
            unset remote_suppress_flag;
1267
        }
1268
    }
1269
 
1270
    if {$code == 1} {
1271
        global errorInfo errorCode;
1272
 
1273
        return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1274
    } elseif {$code == 2} {
1275
        return -code return $string
1276
    } elseif {$code == 3} {
1277
        return
1278
    } elseif {$code > 4} {
1279
        return -code $code $string
1280
    }
1281
}
1282
 
1283
# gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1284
#
1285
# Check for long sequence of output by parts.
1286
# MESSAGE: is the test message to be printed with the test success/fail.
1287
# SENTINEL: Is the terminal pattern indicating that output has finished.
1288
# LIST: is the sequence of outputs to match.
1289
# If the sentinel is recognized early, it is considered an error.
1290
#
1291
# Returns:
1292
#    1 if the test failed,
1293
#    0 if the test passes,
1294
#   -1 if there was an internal error.
1295
#
1296
proc gdb_expect_list {test sentinel list} {
1297
    global gdb_prompt
1298
    global suppress_flag
1299
    set index 0
1300
    set ok 1
1301
    if { $suppress_flag } {
1302
        set ok 0
1303
        unresolved "${test}"
1304
    }
1305
    while { ${index} < [llength ${list}] } {
1306
        set pattern [lindex ${list} ${index}]
1307
        set index [expr ${index} + 1]
1308
        if { ${index} == [llength ${list}] } {
1309
            if { ${ok} } {
1310
                gdb_expect {
1311
                    -re "${pattern}${sentinel}" {
1312
                        # pass "${test}, pattern ${index} + sentinel"
1313
                    }
1314
                    -re "${sentinel}" {
1315
                        fail "${test} (pattern ${index} + sentinel)"
1316
                        set ok 0
1317
                    }
1318
                    timeout {
1319
                        fail "${test} (pattern ${index} + sentinel) (timeout)"
1320
                        set ok 0
1321
                    }
1322
                }
1323
            } else {
1324
                # unresolved "${test}, pattern ${index} + sentinel"
1325
            }
1326
        } else {
1327
            if { ${ok} } {
1328
                gdb_expect {
1329
                    -re "${pattern}" {
1330
                        # pass "${test}, pattern ${index}"
1331
                    }
1332
                    -re "${sentinel}" {
1333
                        fail "${test} (pattern ${index})"
1334
                        set ok 0
1335
                    }
1336
                    timeout {
1337
                        fail "${test} (pattern ${index}) (timeout)"
1338
                        set ok 0
1339
                    }
1340
                }
1341
            } else {
1342
                # unresolved "${test}, pattern ${index}"
1343
            }
1344
        }
1345
    }
1346
    if { ${ok} } {
1347
        pass "${test}"
1348
        return 0
1349
    } else {
1350
        return 1
1351
    }
1352
}
1353
 
1354
#
1355
#
1356
proc gdb_suppress_entire_file { reason } {
1357
    global suppress_flag;
1358
 
1359
    warning "$reason\n";
1360
    set suppress_flag -1;
1361
}
1362
 
1363
#
1364
# Set suppress_flag, which will cause all subsequent calls to send_gdb and
1365
# gdb_expect to fail immediately (until the next call to
1366
# gdb_stop_suppressing_tests).
1367
#
1368
proc gdb_suppress_tests { args } {
1369
    global suppress_flag;
1370
 
1371
    return;  # fnf - disable pending review of results where
1372
             # testsuite ran better without this
1373
    incr suppress_flag;
1374
 
1375
    if { $suppress_flag == 1 } {
1376
        if { [llength $args] > 0 } {
1377
            warning "[lindex $args 0]\n";
1378
        } else {
1379
            warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1380
        }
1381
    }
1382
}
1383
 
1384
#
1385
# Clear suppress_flag.
1386
#
1387
proc gdb_stop_suppressing_tests { } {
1388
    global suppress_flag;
1389
 
1390
    if [info exists suppress_flag] {
1391
        if { $suppress_flag > 0 } {
1392
            set suppress_flag 0;
1393
            clone_output "Tests restarted.\n";
1394
        }
1395
    } else {
1396
        set suppress_flag 0;
1397
    }
1398
}
1399
 
1400
proc gdb_clear_suppressed { } {
1401
    global suppress_flag;
1402
 
1403
    set suppress_flag 0;
1404
}
1405
 
1406
proc gdb_start { } {
1407
    default_gdb_start
1408
}
1409
 
1410
proc gdb_exit { } {
1411
    catch default_gdb_exit
1412
}
1413
 
1414
#
1415
# gdb_load -- load a file into the debugger.
1416
#             return a -1 if anything goes wrong.
1417
#
1418
proc gdb_load { arg } {
1419
    return [gdb_file_cmd $arg]
1420
}
1421
 
1422
proc gdb_continue { function } {
1423
    global decimal
1424
 
1425
    return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
1426
}
1427
 
1428
proc default_gdb_init { args } {
1429
    global gdb_wrapper_initialized
1430
 
1431
    gdb_clear_suppressed;
1432
 
1433
    # Make sure that the wrapper is rebuilt
1434
    # with the appropriate multilib option.
1435
    set gdb_wrapper_initialized 0
1436
 
1437
    # Uh, this is lame. Really, really, really lame. But there's this *one*
1438
    # testcase that will fail in random places if we don't increase this.
1439
    match_max -d 20000
1440
 
1441
    # We want to add the name of the TCL testcase to the PASS/FAIL messages.
1442
    if { [llength $args] > 0 } {
1443
        global pf_prefix
1444
 
1445
        set file [lindex $args 0];
1446
 
1447
        set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
1448
    }
1449
    global gdb_prompt;
1450
    if [target_info exists gdb_prompt] {
1451
        set gdb_prompt [target_info gdb_prompt];
1452
    } else {
1453
        set gdb_prompt "\\(gdb\\)"
1454
    }
1455
}
1456
 
1457
proc gdb_init { args } {
1458
    return [eval default_gdb_init $args];
1459
}
1460
 
1461
proc gdb_finish { } {
1462
    gdb_exit;
1463
}
1464
 
1465
global debug_format
1466
set debug_format "unknown"
1467
 
1468
# Run the gdb command "info source" and extract the debugging format
1469
# information from the output and save it in debug_format.
1470
 
1471
proc get_debug_format { } {
1472
    global gdb_prompt
1473
    global verbose
1474
    global expect_out
1475
    global debug_format
1476
 
1477
    set debug_format "unknown"
1478
    send_gdb "info source\n"
1479
    gdb_expect 10 {
1480
        -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
1481
            set debug_format $expect_out(1,string)
1482
            verbose "debug format is $debug_format"
1483
            return 1;
1484
        }
1485
        -re "No current source file.\r\n$gdb_prompt $" {
1486
            perror "get_debug_format used when no current source file"
1487
            return 0;
1488
        }
1489
        -re "$gdb_prompt $" {
1490
            warning "couldn't check debug format (no valid response)."
1491
            return 1;
1492
        }
1493
        timeout {
1494
            warning "couldn't check debug format (timed out)."
1495
            return 1;
1496
        }
1497
    }
1498
}
1499
 
1500
# Return true if FORMAT matches the debug format the current test was
1501
# compiled with.  FORMAT is a shell-style globbing pattern; it can use
1502
# `*', `[...]', and so on.
1503
#
1504
# This function depends on variables set by `get_debug_format', above.
1505
 
1506
proc test_debug_format {format} {
1507
    global debug_format
1508
 
1509
    return [expr [string match $format $debug_format] != 0]
1510
}
1511
 
1512
# Like setup_xfail, but takes the name of a debug format (DWARF 1,
1513
# COFF, stabs, etc).  If that format matches the format that the
1514
# current test was compiled with, then the next test is expected to
1515
# fail for any target.  Returns 1 if the next test or set of tests is
1516
# expected to fail, 0 otherwise (or if it is unknown).  Must have
1517
# previously called get_debug_format.
1518
proc setup_xfail_format { format } {
1519
    set ret [test_debug_format $format];
1520
 
1521
    if {$ret} then {
1522
        setup_xfail "*-*-*"
1523
    }
1524
    return $ret;
1525
}
1526
 
1527
proc gdb_step_for_stub { } {
1528
    global gdb_prompt;
1529
 
1530
    if ![target_info exists gdb,use_breakpoint_for_stub] {
1531
        if [target_info exists gdb_stub_step_command] {
1532
            set command [target_info gdb_stub_step_command];
1533
        } else {
1534
            set command "step";
1535
        }
1536
        send_gdb "${command}\n";
1537
        set tries 0;
1538
        gdb_expect 60 {
1539
            -re "(main.* at |.*in .*start).*$gdb_prompt" {
1540
                return;
1541
            }
1542
            -re ".*$gdb_prompt" {
1543
                incr tries;
1544
                if { $tries == 5 } {
1545
                    fail "stepping out of breakpoint function";
1546
                    return;
1547
                }
1548
                send_gdb "${command}\n";
1549
                exp_continue;
1550
            }
1551
            default {
1552
                fail "stepping out of breakpoint function";
1553
                return;
1554
            }
1555
        }
1556
    }
1557
    send_gdb "where\n";
1558
    gdb_expect {
1559
        -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
1560
            set file $expect_out(1,string);
1561
            set linenum [expr $expect_out(2,string) + 1];
1562
            set breakplace "${file}:${linenum}";
1563
        }
1564
        default {}
1565
    }
1566
    send_gdb "break ${breakplace}\n";
1567
    gdb_expect 60 {
1568
        -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
1569
            set breakpoint $expect_out(1,string);
1570
        }
1571
        -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
1572
            set breakpoint $expect_out(1,string);
1573
        }
1574
        default {}
1575
    }
1576
    send_gdb "continue\n";
1577
    gdb_expect 60 {
1578
        -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
1579
            gdb_test "delete $breakpoint" ".*" "";
1580
            return;
1581
        }
1582
        default {}
1583
    }
1584
}
1585
 
1586
### gdb_get_line_number TEXT [FILE]
1587
###
1588
### Search the source file FILE, and return the line number of a line
1589
### containing TEXT.  Use this function instead of hard-coding line
1590
### numbers into your test script.
1591
###
1592
### Specifically, this function uses GDB's "search" command to search
1593
### FILE for the first line containing TEXT, and returns its line
1594
### number.  Thus, FILE must be a source file, compiled into the
1595
### executable you are running.  If omitted, FILE defaults to the
1596
### value of the global variable `srcfile'; most test scripts set
1597
### `srcfile' appropriately at the top anyway.
1598
###
1599
### Use this function to keep your test scripts independent of the
1600
### exact line numbering of the source file.  Don't write:
1601
###
1602
###   send_gdb "break 20"
1603
###
1604
### This means that if anyone ever edits your test's source file,
1605
### your test could break.  Instead, put a comment like this on the
1606
### source file line you want to break at:
1607
###
1608
###   /* breakpoint spot: frotz.exp: test name */
1609
###
1610
### and then write, in your test script (which we assume is named
1611
### frotz.exp):
1612
###
1613
###   send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
1614
###
1615
### (Yes, Tcl knows how to handle the nested quotes and brackets.
1616
### Try this:
1617
###     $ tclsh
1618
###     % puts "foo [lindex "bar baz" 1]"
1619
###     foo baz
1620
###     %
1621
### Tcl is quite clever, for a little stringy language.)
1622
 
1623
proc gdb_get_line_number {text {file /omitted/}} {
1624
    global gdb_prompt;
1625
    global srcfile;
1626
 
1627
    if {! [string compare $file /omitted/]} {
1628
        set file $srcfile
1629
    }
1630
 
1631
    set result -1;
1632
    gdb_test "list ${file}:1,1" ".*" ""
1633
    send_gdb "search ${text}\n"
1634
    gdb_expect {
1635
        -re "\[\r\n\]+(\[0-9\]+)\[ \t\].*${text}.*$gdb_prompt $" {
1636
            set result $expect_out(1,string)
1637
        }
1638
        -re ".*$gdb_prompt $" {
1639
            fail "find line number containing \"${text}\""
1640
        }
1641
        timeout {
1642
            fail "find line number containing \"${text}\" (timeout)"
1643
        }
1644
    }
1645
    return $result;
1646
}
1647
 
1648
# gdb_continue_to_end:
1649
#       The case where the target uses stubs has to be handled specially. If a
1650
#       stub is used, we set a breakpoint at exit because we cannot rely on
1651
#       exit() behavior of a remote target.
1652
#
1653
# mssg is the error message that gets printed.
1654
 
1655
proc gdb_continue_to_end {mssg} {
1656
  if [target_info exists use_gdb_stub] {
1657
    if {![gdb_breakpoint "exit"]} {
1658
      return 0
1659
    }
1660
    gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
1661
      "continue until exit at $mssg"
1662
  } else {
1663
    # Continue until we exit.  Should not stop again.
1664
    # Don't bother to check the output of the program, that may be
1665
    # extremely tough for some remote systems.
1666
    gdb_test "continue"\
1667
      "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|)Program exited normally\\..*"\
1668
      "continue until exit at $mssg"
1669
  }
1670
}
1671
 
1672
proc rerun_to_main {} {
1673
  global gdb_prompt
1674
 
1675
  if [target_info exists use_gdb_stub] {
1676
    gdb_run_cmd
1677
    gdb_expect {
1678
      -re ".*Breakpoint .*main .*$gdb_prompt $"\
1679
              {pass "rerun to main" ; return 0}
1680
      -re "$gdb_prompt $"\
1681
              {fail "rerun to main" ; return 0}
1682
      timeout {fail "(timeout) rerun to main" ; return 0}
1683
    }
1684
  } else {
1685
    send_gdb "run\n"
1686
    gdb_expect {
1687
      -re "Starting program.*$gdb_prompt $"\
1688
              {pass "rerun to main" ; return 0}
1689
      -re "$gdb_prompt $"\
1690
              {fail "rerun to main" ; return 0}
1691
      timeout {fail "(timeout) rerun to main" ; return 0}
1692
    }
1693
  }
1694
}
1695
 
1696
# Print a message and return true if a test should be skipped
1697
# due to lack of floating point suport.
1698
 
1699
proc gdb_skip_float_test { msg } {
1700
    if [target_info exists gdb,skip_float_tests] {
1701
        verbose "Skipping test '$msg': no float tests.";
1702
        return 1;
1703
    }
1704
    return 0;
1705
}
1706
 
1707
# Print a message and return true if a test should be skipped
1708
# due to lack of stdio support.
1709
 
1710
proc gdb_skip_stdio_test { msg } {
1711
    if [target_info exists gdb,noinferiorio] {
1712
        verbose "Skipping test '$msg': no inferior i/o.";
1713
        return 1;
1714
    }
1715
    return 0;
1716
}
1717
 
1718
proc gdb_skip_bogus_test { msg } {
1719
    return 0;
1720
}
1721
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.