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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [testsuite/] [lib/] [gdb.exp] - Blame information for rev 157

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
# Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2
# 2003, 2004, 2005, 2007, 2008 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 3 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, see .
16
 
17
# This file was written by Fred Fish. (fnf@cygnus.com)
18
 
19
# Generic gdb subroutines that should work for any target.  If these
20
# need to be modified for any target, it can be done with a variable
21
# or by passing arguments.
22
 
23
if {$tool == ""} {
24
    # Tests would fail, logs on get_compiler_info() would be missing.
25
    send_error "`site.exp' not found, run `make site.exp'!\n"
26
    exit 2
27
}
28
 
29
load_lib libgloss.exp
30
 
31
global GDB
32
 
33
if [info exists TOOL_EXECUTABLE] {
34
    set GDB $TOOL_EXECUTABLE;
35
}
36
if ![info exists GDB] {
37
    if ![is_remote host] {
38
        set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
39
    } else {
40
        set GDB [transform gdb];
41
    }
42
}
43
verbose "using GDB = $GDB" 2
44
 
45
global GDBFLAGS
46
if ![info exists GDBFLAGS] {
47
    set GDBFLAGS "-nx"
48
}
49
verbose "using GDBFLAGS = $GDBFLAGS" 2
50
 
51
# The variable gdb_prompt is a regexp which matches the gdb prompt.
52
# Set it if it is not already set.
53
global gdb_prompt
54
if ![info exists gdb_prompt] then {
55
    set gdb_prompt "\[(\]gdb\[)\]"
56
}
57
 
58
# The variable fullname_syntax_POSIX is a regexp which matches a POSIX
59
# absolute path ie. /foo/
60
set fullname_syntax_POSIX "/.*/"
61
# The variable fullname_syntax_UNC is a regexp which matches a Windows
62
# UNC path ie. \\D\foo\
63
set fullname_syntax_UNC {\\\\[^\\]+\\.+\\}
64
# The variable fullname_syntax_DOS_CASE is a regexp which matches a
65
# particular DOS case that GDB most likely will output
66
# ie. \foo\, but don't match \\.*\
67
set fullname_syntax_DOS_CASE {\\[^\\].*\\}
68
# The variable fullname_syntax_DOS is a regexp which matches a DOS path
69
# ie. a:\foo\ && a:foo\
70
set fullname_syntax_DOS {[a-zA-Z]:.*\\}
71
# The variable fullname_syntax is a regexp which matches what GDB considers
72
# an absolute path. It is currently debatable if the Windows style paths
73
# d:foo and \abc should be considered valid as an absolute path.
74
# Also, the purpse of this regexp is not to recognize a well formed
75
# absolute path, but to say with certainty that a path is absolute.
76
set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
77
 
78
# Needed for some tests under Cygwin.
79
global EXEEXT
80
global env
81
 
82
if ![info exists env(EXEEXT)] {
83
    set EXEEXT ""
84
} else {
85
    set EXEEXT $env(EXEEXT)
86
}
87
 
88
set octal "\[0-7\]+"
89
 
90
### Only procedures should come after this point.
91
 
92
#
93
# gdb_version -- extract and print the version number of GDB
94
#
95
proc default_gdb_version {} {
96
    global GDB
97
    global GDBFLAGS
98
    global gdb_prompt
99
    set fileid [open "gdb_cmd" w];
100
    puts $fileid "q";
101
    close $fileid;
102
    set cmdfile [remote_download host "gdb_cmd"];
103
    set output [remote_exec host "$GDB -nw --command $cmdfile"]
104
    remote_file build delete "gdb_cmd";
105
    remote_file host delete "$cmdfile";
106
    set tmp [lindex $output 1];
107
    set version ""
108
    regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
109
    if ![is_remote host] {
110
        clone_output "[which $GDB] version $version $GDBFLAGS\n"
111
    } else {
112
        clone_output "$GDB on remote host version $version $GDBFLAGS\n"
113
    }
114
}
115
 
116
proc gdb_version { } {
117
    return [default_gdb_version];
118
}
119
 
120
#
121
# gdb_unload -- unload a file if one is loaded
122
#
123
 
124
proc gdb_unload {} {
125
    global verbose
126
    global GDB
127
    global gdb_prompt
128
    send_gdb "file\n"
129
    gdb_expect 60 {
130
        -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
131
        -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
132
        -re "A program is being debugged already..*Kill it.*y or n. $"\
133
            { send_gdb "y\n"
134
                verbose "\t\tKilling previous program being debugged"
135
            exp_continue
136
        }
137
        -re "Discard symbol table from .*y or n.*$" {
138
            send_gdb "y\n"
139
            exp_continue
140
        }
141
        -re "$gdb_prompt $" {}
142
        timeout {
143
            perror "couldn't unload file in $GDB (timed out)."
144
            return -1
145
        }
146
    }
147
}
148
 
149
# Many of the tests depend on setting breakpoints at various places and
150
# running until that breakpoint is reached.  At times, we want to start
151
# with a clean-slate with respect to breakpoints, so this utility proc
152
# lets us do this without duplicating this code everywhere.
153
#
154
 
155
proc delete_breakpoints {} {
156
    global gdb_prompt
157
 
158
    # we need a larger timeout value here or this thing just confuses
159
    # itself.  May need a better implementation if possible. - guo
160
    #
161
    send_gdb "delete breakpoints\n"
162
    gdb_expect 100 {
163
         -re "Delete all breakpoints.*y or n.*$" {
164
            send_gdb "y\n";
165
            exp_continue
166
        }
167
         -re "$gdb_prompt $" { # This happens if there were no breakpoints
168
            }
169
         timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return }
170
    }
171
    send_gdb "info breakpoints\n"
172
    gdb_expect 100 {
173
         -re "No breakpoints or watchpoints..*$gdb_prompt $" {}
174
         -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return }
175
         -re "Delete all breakpoints.*or n.*$" {
176
            send_gdb "y\n";
177
            exp_continue
178
        }
179
         timeout { perror "info breakpoints (timeout)" ; return }
180
    }
181
}
182
 
183
 
184
#
185
# Generic run command.
186
#
187
# The second pattern below matches up to the first newline *only*.
188
# Using ``.*$'' could swallow up output that we attempt to match
189
# elsewhere.
190
#
191
proc gdb_run_cmd {args} {
192
    global gdb_prompt
193
 
194
    if [target_info exists gdb_init_command] {
195
        send_gdb "[target_info gdb_init_command]\n";
196
        gdb_expect 30 {
197
            -re "$gdb_prompt $" { }
198
            default {
199
                perror "gdb_init_command for target failed";
200
                return;
201
            }
202
        }
203
    }
204
 
205
    if [target_info exists use_gdb_stub] {
206
        if [target_info exists gdb,do_reload_on_run] {
207
            if { [gdb_reload] != 0 } {
208
                return;
209
            }
210
            send_gdb "continue\n";
211
            gdb_expect 60 {
212
                -re "Continu\[^\r\n\]*\[\r\n\]" {}
213
                default {}
214
            }
215
            return;
216
        }
217
 
218
        if [target_info exists gdb,start_symbol] {
219
            set start [target_info gdb,start_symbol];
220
        } else {
221
            set start "start";
222
        }
223
        send_gdb  "jump *$start\n"
224
        set start_attempt 1;
225
        while { $start_attempt } {
226
            # Cap (re)start attempts at three to ensure that this loop
227
            # always eventually fails.  Don't worry about trying to be
228
            # clever and not send a command when it has failed.
229
            if [expr $start_attempt > 3] {
230
                perror "Jump to start() failed (retry count exceeded)";
231
                return;
232
            }
233
            set start_attempt [expr $start_attempt + 1];
234
            gdb_expect 30 {
235
                -re "Continuing at \[^\r\n\]*\[\r\n\]" {
236
                    set start_attempt 0;
237
                }
238
                -re "No symbol \"_start\" in current.*$gdb_prompt $" {
239
                    perror "Can't find start symbol to run in gdb_run";
240
                    return;
241
                }
242
                -re "No symbol \"start\" in current.*$gdb_prompt $" {
243
                    send_gdb "jump *_start\n";
244
                }
245
                -re "No symbol.*context.*$gdb_prompt $" {
246
                    set start_attempt 0;
247
                }
248
                -re "Line.* Jump anyway.*y or n. $" {
249
                    send_gdb "y\n"
250
                }
251
                -re "The program is not being run.*$gdb_prompt $" {
252
                    if { [gdb_reload] != 0 } {
253
                        return;
254
                    }
255
                    send_gdb "jump *$start\n";
256
                }
257
                timeout {
258
                    perror "Jump to start() failed (timeout)";
259
                    return
260
                }
261
            }
262
        }
263
        if [target_info exists gdb_stub] {
264
            gdb_expect 60 {
265
                -re "$gdb_prompt $" {
266
                    send_gdb "continue\n"
267
                }
268
            }
269
        }
270
        return
271
    }
272
 
273
    if [target_info exists gdb,do_reload_on_run] {
274
        if { [gdb_reload] != 0 } {
275
            return;
276
        }
277
    }
278
    send_gdb "run $args\n"
279
# This doesn't work quite right yet.
280
# Use -notransfer here so that test cases (like chng-sym.exp)
281
# may test for additional start-up messages.
282
   gdb_expect 60 {
283
        -re "The program .* has been started already.*y or n. $" {
284
            send_gdb "y\n"
285
            exp_continue
286
        }
287
        -notransfer -re "Starting program: \[^\r\n\]*" {}
288
    }
289
}
290
 
291
# Generic start command.  Return 0 if we could start the program, -1
292
# if we could not.
293
 
294
proc gdb_start_cmd {args} {
295
    global gdb_prompt
296
 
297
    if [target_info exists gdb_init_command] {
298
        send_gdb "[target_info gdb_init_command]\n";
299
        gdb_expect 30 {
300
            -re "$gdb_prompt $" { }
301
            default {
302
                perror "gdb_init_command for target failed";
303
                return;
304
            }
305
        }
306
    }
307
 
308
    if [target_info exists use_gdb_stub] {
309
        return -1
310
    }
311
 
312
    send_gdb "start $args\n"
313
    gdb_expect 60 {
314
        -re "The program .* has been started already.*y or n. $" {
315
            send_gdb "y\n"
316
            exp_continue
317
        }
318
        # Use -notransfer here so that test cases (like chng-sym.exp)
319
        # may test for additional start-up messages.
320
        -notransfer -re "Starting program: \[^\r\n\]*" {
321
            return 0
322
        }
323
    }
324
    return -1
325
}
326
 
327
# Set a breakpoint at FUNCTION.  If there is an additional argument it is
328
# a list of options; the supported options are allow-pending and temporary.
329
 
330
proc gdb_breakpoint { function args } {
331
    global gdb_prompt
332
    global decimal
333
 
334
    set pending_response n
335
    if {[lsearch -exact [lindex $args 0] allow-pending] != -1} {
336
        set pending_response y
337
    }
338
 
339
    set break_command "break"
340
    if {[lsearch -exact [lindex $args 0] temporary] != -1} {
341
        set break_command "tbreak"
342
    }
343
 
344
    send_gdb "$break_command $function\n"
345
    # The first two regexps are what we get with -g, the third is without -g.
346
    gdb_expect 30 {
347
        -re "Breakpoint \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
348
        -re "Breakpoint \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
349
        -re "Breakpoint \[0-9\]* at .*$gdb_prompt $" {}
350
        -re "Breakpoint \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
351
                if {$pending_response == "n"} {
352
                        fail "setting breakpoint at $function"
353
                        return 0
354
                }
355
        }
356
        -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
357
                send_gdb "$pending_response\n"
358
                exp_continue
359
        }
360
        -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 }
361
        timeout { fail "setting breakpoint at $function (timeout)" ; return 0 }
362
    }
363
    return 1;
364
}
365
 
366
# Set breakpoint at function and run gdb until it breaks there.
367
# Since this is the only breakpoint that will be set, if it stops
368
# at a breakpoint, we will assume it is the one we want.  We can't
369
# just compare to "function" because it might be a fully qualified,
370
# single quoted C++ function specifier.  If there's an additional argument,
371
# pass it to gdb_breakpoint.
372
 
373
proc runto { function args } {
374
    global gdb_prompt
375
    global decimal
376
 
377
    delete_breakpoints
378
 
379
    if ![gdb_breakpoint $function [lindex $args 0]] {
380
        return 0;
381
    }
382
 
383
    gdb_run_cmd
384
 
385
    # the "at foo.c:36" output we get with -g.
386
    # the "in func" output we get without -g.
387
    gdb_expect 30 {
388
        -re "Break.* at .*:$decimal.*$gdb_prompt $" {
389
            return 1
390
        }
391
        -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
392
            return 1
393
        }
394
        -re "$gdb_prompt $" {
395
            fail "running to $function in runto"
396
            return 0
397
        }
398
        timeout {
399
            fail "running to $function in runto (timeout)"
400
            return 0
401
        }
402
    }
403
    return 1
404
}
405
 
406
#
407
# runto_main -- ask gdb to run until we hit a breakpoint at main.
408
#               The case where the target uses stubs has to be handled
409
#               specially--if it uses stubs, assuming we hit
410
#               breakpoint() and just step out of the function.
411
#
412
proc runto_main { } {
413
    global gdb_prompt
414
    global decimal
415
 
416
    if ![target_info exists gdb_stub] {
417
        return [runto main]
418
    }
419
 
420
    delete_breakpoints
421
 
422
    gdb_step_for_stub;
423
 
424
    return 1
425
}
426
 
427
 
428
### Continue, and expect to hit a breakpoint.
429
### Report a pass or fail, depending on whether it seems to have
430
### worked.  Use NAME as part of the test name; each call to
431
### continue_to_breakpoint should use a NAME which is unique within
432
### that test file.
433
proc gdb_continue_to_breakpoint {name} {
434
    global gdb_prompt
435
    set full_name "continue to breakpoint: $name"
436
 
437
    send_gdb "continue\n"
438
    gdb_expect {
439
        -re "Breakpoint .* at .*\r\n$gdb_prompt $" {
440
            pass $full_name
441
        }
442
        -re ".*$gdb_prompt $" {
443
            fail $full_name
444
        }
445
        timeout {
446
            fail "$full_name (timeout)"
447
        }
448
    }
449
}
450
 
451
 
452
# gdb_internal_error_resync:
453
#
454
# Answer the questions GDB asks after it reports an internal error
455
# until we get back to a GDB prompt.  Decline to quit the debugging
456
# session, and decline to create a core file.  Return non-zero if the
457
# resync succeeds.
458
#
459
# This procedure just answers whatever questions come up until it sees
460
# a GDB prompt; it doesn't require you to have matched the input up to
461
# any specific point.  However, it only answers questions it sees in
462
# the output itself, so if you've matched a question, you had better
463
# answer it yourself before calling this.
464
#
465
# You can use this function thus:
466
#
467
# gdb_expect {
468
#     ...
469
#     -re ".*A problem internal to GDB has been detected" {
470
#         gdb_internal_error_resync
471
#     }
472
#     ...
473
# }
474
#
475
proc gdb_internal_error_resync {} {
476
    global gdb_prompt
477
 
478
    set count 0
479
    while {$count < 10} {
480
        gdb_expect {
481
            -re "Quit this debugging session\\? \\(y or n\\) $" {
482
                send_gdb "n\n"
483
                incr count
484
            }
485
            -re "Create a core file of GDB\\? \\(y or n\\) $" {
486
                send_gdb "n\n"
487
                incr count
488
            }
489
            -re "$gdb_prompt $" {
490
                # We're resynchronized.
491
                return 1
492
            }
493
            timeout {
494
                perror "Could not resync from internal error (timeout)"
495
                return 0
496
            }
497
        }
498
    }
499
    perror "Could not resync from internal error (resync count exceeded)"
500
    return 0
501
}
502
 
503
 
504
# gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS
505
# Send a command to gdb; test the result.
506
#
507
# COMMAND is the command to execute, send to GDB with send_gdb.  If
508
#   this is the null string no command is sent.
509
# MESSAGE is a message to be printed with the built-in failure patterns
510
#   if one of them matches.  If MESSAGE is empty COMMAND will be used.
511
# EXPECT_ARGUMENTS will be fed to expect in addition to the standard
512
#   patterns.  Pattern elements will be evaluated in the caller's
513
#   context; action elements will be executed in the caller's context.
514
#   Unlike patterns for gdb_test, these patterns should generally include
515
#   the final newline and prompt.
516
#
517
# Returns:
518
#    1 if the test failed, according to a built-in failure pattern
519
#    0 if only user-supplied patterns matched
520
#   -1 if there was an internal error.
521
#
522
# You can use this function thus:
523
#
524
# gdb_test_multiple "print foo" "test foo" {
525
#    -re "expected output 1" {
526
#        pass "print foo"
527
#    }
528
#    -re "expected output 2" {
529
#        fail "print foo"
530
#    }
531
# }
532
#
533
# The standard patterns, such as "Program exited..." and "A problem
534
# ...", all being implicitly appended to that list.
535
#
536
proc gdb_test_multiple { command message user_code } {
537
    global verbose
538
    global gdb_prompt
539
    global GDB
540
    upvar timeout timeout
541
    upvar expect_out expect_out
542
 
543
    if { $message == "" } {
544
        set message $command
545
    }
546
 
547
    # TCL/EXPECT WART ALERT
548
    # Expect does something very strange when it receives a single braced
549
    # argument.  It splits it along word separators and performs substitutions.
550
    # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
551
    # evaluated as "\[ab\]".  But that's not how TCL normally works; inside a
552
    # double-quoted list item, "\[ab\]" is just a long way of representing
553
    # "[ab]", because the backslashes will be removed by lindex.
554
 
555
    # Unfortunately, there appears to be no easy way to duplicate the splitting
556
    # that expect will do from within TCL.  And many places make use of the
557
    # "\[0-9\]" construct, so we need to support that; and some places make use
558
    # of the "[func]" construct, so we need to support that too.  In order to
559
    # get this right we have to substitute quoted list elements differently
560
    # from braced list elements.
561
 
562
    # We do this roughly the same way that Expect does it.  We have to use two
563
    # lists, because if we leave unquoted newlines in the argument to uplevel
564
    # they'll be treated as command separators, and if we escape newlines
565
    # we mangle newlines inside of command blocks.  This assumes that the
566
    # input doesn't contain a pattern which contains actual embedded newlines
567
    # at this point!
568
 
569
    regsub -all {\n} ${user_code} { } subst_code
570
    set subst_code [uplevel list $subst_code]
571
 
572
    set processed_code ""
573
    set patterns ""
574
    set expecting_action 0
575
    foreach item $user_code subst_item $subst_code {
576
        if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
577
            lappend processed_code $item
578
            continue
579
        }
580
        if {$item == "-indices" || $item == "-re" || $item == "-ex"} {
581
            lappend processed_code $item
582
            continue
583
        }
584
        if { $expecting_action } {
585
            lappend processed_code "uplevel [list $item]"
586
            set expecting_action 0
587
            # Cosmetic, no effect on the list.
588
            append processed_code "\n"
589
            continue
590
        }
591
        set expecting_action 1
592
        lappend processed_code $subst_item
593
        if {$patterns != ""} {
594
            append patterns "; "
595
        }
596
        append patterns "\"$subst_item\""
597
    }
598
 
599
    # Also purely cosmetic.
600
    regsub -all {\r} $patterns {\\r} patterns
601
    regsub -all {\n} $patterns {\\n} patterns
602
 
603
    if $verbose>2 then {
604
        send_user "Sending \"$command\" to gdb\n"
605
        send_user "Looking to match \"$patterns\"\n"
606
        send_user "Message is \"$message\"\n"
607
    }
608
 
609
    set result -1
610
    set string "${command}\n";
611
    if { $command != "" } {
612
        while { "$string" != "" } {
613
            set foo [string first "\n" "$string"];
614
            set len [string length "$string"];
615
            if { $foo < [expr $len - 1] } {
616
                set str [string range "$string" 0 $foo];
617
                if { [send_gdb "$str"] != "" } {
618
                    global suppress_flag;
619
 
620
                    if { ! $suppress_flag } {
621
                        perror "Couldn't send $command to GDB.";
622
                    }
623
                    fail "$message";
624
                    return $result;
625
                }
626
                # since we're checking if each line of the multi-line
627
                # command are 'accepted' by GDB here,
628
                # we need to set -notransfer expect option so that
629
                # command output is not lost for pattern matching
630
                # - guo
631
                gdb_expect 2 {
632
                    -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 }
633
                    timeout { verbose "partial: timeout" 3 }
634
                }
635
                set string [string range "$string" [expr $foo + 1] end];
636
            } else {
637
                break;
638
            }
639
        }
640
        if { "$string" != "" } {
641
            if { [send_gdb "$string"] != "" } {
642
                global suppress_flag;
643
 
644
                if { ! $suppress_flag } {
645
                    perror "Couldn't send $command to GDB.";
646
                }
647
                fail "$message";
648
                return $result;
649
            }
650
        }
651
    }
652
 
653
    if [target_info exists gdb,timeout] {
654
        set tmt [target_info gdb,timeout];
655
    } else {
656
        if [info exists timeout] {
657
            set tmt $timeout;
658
        } else {
659
            global timeout;
660
            if [info exists timeout] {
661
                set tmt $timeout;
662
            } else {
663
                set tmt 60;
664
            }
665
        }
666
    }
667
 
668
    set code {
669
         -re ".*A problem internal to GDB has been detected" {
670
             fail "$message (GDB internal error)"
671
             gdb_internal_error_resync
672
         }
673
         -re "\\*\\*\\* DOSEXIT code.*" {
674
             if { $message != "" } {
675
                 fail "$message";
676
             }
677
             gdb_suppress_entire_file "GDB died";
678
             set result -1;
679
         }
680
    }
681
    append code $processed_code
682
    append code {
683
         -re "Ending remote debugging.*$gdb_prompt $" {
684
            if ![isnative] then {
685
                warning "Can`t communicate to remote target."
686
            }
687
            gdb_exit
688
            gdb_start
689
            set result -1
690
        }
691
         -re "Undefined\[a-z\]* command:.*$gdb_prompt $" {
692
            perror "Undefined command \"$command\"."
693
            fail "$message"
694
            set result 1
695
        }
696
         -re "Ambiguous command.*$gdb_prompt $" {
697
            perror "\"$command\" is not a unique command name."
698
            fail "$message"
699
            set result 1
700
        }
701
         -re "Program exited with code \[0-9\]+.*$gdb_prompt $" {
702
            if ![string match "" $message] then {
703
                set errmsg "$message (the program exited)"
704
            } else {
705
                set errmsg "$command (the program exited)"
706
            }
707
            fail "$errmsg"
708
            set result -1
709
        }
710
         -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" {
711
            if ![string match "" $message] then {
712
                set errmsg "$message (the program exited)"
713
            } else {
714
                set errmsg "$command (the program exited)"
715
            }
716
            fail "$errmsg"
717
            set result -1
718
        }
719
         -re "The program is not being run.*$gdb_prompt $" {
720
            if ![string match "" $message] then {
721
                set errmsg "$message (the program is no longer running)"
722
            } else {
723
                set errmsg "$command (the program is no longer running)"
724
            }
725
            fail "$errmsg"
726
            set result -1
727
        }
728
         -re "\r\n$gdb_prompt $" {
729
            if ![string match "" $message] then {
730
                fail "$message"
731
            }
732
            set result 1
733
        }
734
         "" {
735
            send_gdb "\n"
736
            perror "Window too small."
737
            fail "$message"
738
            set result -1
739
        }
740
         -re "\\(y or n\\) " {
741
            send_gdb "n\n"
742
            perror "Got interactive prompt."
743
            fail "$message"
744
            set result -1
745
        }
746
         eof {
747
             perror "Process no longer exists"
748
             if { $message != "" } {
749
                 fail "$message"
750
             }
751
             return -1
752
        }
753
         full_buffer {
754
            perror "internal buffer is full."
755
            fail "$message"
756
            set result -1
757
        }
758
        timeout {
759
            if ![string match "" $message] then {
760
                fail "$message (timeout)"
761
            }
762
            set result 1
763
        }
764
    }
765
 
766
    set result 0
767
    set code [catch {gdb_expect $tmt $code} string]
768
    if {$code == 1} {
769
        global errorInfo errorCode;
770
        return -code error -errorinfo $errorInfo -errorcode $errorCode $string
771
    } elseif {$code == 2} {
772
        return -code return $string
773
    } elseif {$code == 3} {
774
        return
775
    } elseif {$code > 4} {
776
        return -code $code $string
777
    }
778
    return $result
779
}
780
 
781
# gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
782
# Send a command to gdb; test the result.
783
#
784
# COMMAND is the command to execute, send to GDB with send_gdb.  If
785
#   this is the null string no command is sent.
786
# PATTERN is the pattern to match for a PASS, and must NOT include
787
#   the \r\n sequence immediately before the gdb prompt.
788
# MESSAGE is an optional message to be printed.  If this is
789
#   omitted, then the pass/fail messages use the command string as the
790
#   message.  (If this is the empty string, then sometimes we don't
791
#   call pass or fail at all; I don't understand this at all.)
792
# QUESTION is a question GDB may ask in response to COMMAND, like
793
#   "are you sure?"
794
# RESPONSE is the response to send if QUESTION appears.
795
#
796
# Returns:
797
#    1 if the test failed,
798
#    0 if the test passes,
799
#   -1 if there was an internal error.
800
#
801
proc gdb_test { args } {
802
    global verbose
803
    global gdb_prompt
804
    global GDB
805
    upvar timeout timeout
806
 
807
    if [llength $args]>2 then {
808
        set message [lindex $args 2]
809
    } else {
810
        set message [lindex $args 0]
811
    }
812
    set command [lindex $args 0]
813
    set pattern [lindex $args 1]
814
 
815
    if [llength $args]==5 {
816
        set question_string [lindex $args 3];
817
        set response_string [lindex $args 4];
818
    } else {
819
        set question_string "^FOOBAR$"
820
    }
821
 
822
    return [gdb_test_multiple $command $message {
823
        -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" {
824
            if ![string match "" $message] then {
825
                pass "$message"
826
            }
827
        }
828
        -re "(${question_string})$" {
829
            send_gdb "$response_string\n";
830
            exp_continue;
831
        }
832
     }]
833
}
834
 
835
# Test that a command gives an error.  For pass or fail, return
836
# a 1 to indicate that more tests can proceed.  However a timeout
837
# is a serious error, generates a special fail message, and causes
838
# a 0 to be returned to indicate that more tests are likely to fail
839
# as well.
840
 
841
proc test_print_reject { args } {
842
    global gdb_prompt
843
    global verbose
844
 
845
    if [llength $args]==2 then {
846
        set expectthis [lindex $args 1]
847
    } else {
848
        set expectthis "should never match this bogus string"
849
    }
850
    set sendthis [lindex $args 0]
851
    if $verbose>2 then {
852
        send_user "Sending \"$sendthis\" to gdb\n"
853
        send_user "Looking to match \"$expectthis\"\n"
854
    }
855
    send_gdb "$sendthis\n"
856
    #FIXME: Should add timeout as parameter.
857
    gdb_expect {
858
        -re "A .* in expression.*\\.*$gdb_prompt $" {
859
            pass "reject $sendthis"
860
            return 1
861
        }
862
        -re "Invalid syntax in expression.*$gdb_prompt $" {
863
            pass "reject $sendthis"
864
            return 1
865
        }
866
        -re "Junk after end of expression.*$gdb_prompt $" {
867
            pass "reject $sendthis"
868
            return 1
869
        }
870
        -re "Invalid number.*$gdb_prompt $" {
871
            pass "reject $sendthis"
872
            return 1
873
        }
874
        -re "Invalid character constant.*$gdb_prompt $" {
875
            pass "reject $sendthis"
876
            return 1
877
        }
878
        -re "No symbol table is loaded.*$gdb_prompt $" {
879
            pass "reject $sendthis"
880
            return 1
881
        }
882
        -re "No symbol .* in current context.*$gdb_prompt $" {
883
            pass "reject $sendthis"
884
            return 1
885
        }
886
        -re "Unmatched single quote.*$gdb_prompt $" {
887
            pass "reject $sendthis"
888
            return 1
889
        }
890
        -re "A character constant must contain at least one character.*$gdb_prompt $" {
891
            pass "reject $sendthis"
892
            return 1
893
        }
894
        -re "$expectthis.*$gdb_prompt $" {
895
            pass "reject $sendthis"
896
            return 1
897
        }
898
        -re ".*$gdb_prompt $" {
899
            fail "reject $sendthis"
900
            return 1
901
        }
902
        default {
903
            fail "reject $sendthis (eof or timeout)"
904
            return 0
905
        }
906
    }
907
}
908
 
909
# Given an input string, adds backslashes as needed to create a
910
# regexp that will match the string.
911
 
912
proc string_to_regexp {str} {
913
    set result $str
914
    regsub -all {[]*+.|()^$\[\\]} $str {\\&} result
915
    return $result
916
}
917
 
918
# Same as gdb_test, but the second parameter is not a regexp,
919
# but a string that must match exactly.
920
 
921
proc gdb_test_exact { args } {
922
    upvar timeout timeout
923
 
924
    set command [lindex $args 0]
925
 
926
    # This applies a special meaning to a null string pattern.  Without
927
    # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
928
    # messages from commands that should have no output except a new
929
    # prompt.  With this, only results of a null string will match a null
930
    # string pattern.
931
 
932
    set pattern [lindex $args 1]
933
    if [string match $pattern ""] {
934
        set pattern [string_to_regexp [lindex $args 0]]
935
    } else {
936
        set pattern [string_to_regexp [lindex $args 1]]
937
    }
938
 
939
    # It is most natural to write the pattern argument with only
940
    # embedded \n's, especially if you are trying to avoid Tcl quoting
941
    # problems.  But gdb_expect really wants to see \r\n in patterns.  So
942
    # transform the pattern here.  First transform \r\n back to \n, in
943
    # case some users of gdb_test_exact already do the right thing.
944
    regsub -all "\r\n" $pattern "\n" pattern
945
    regsub -all "\n" $pattern "\r\n" pattern
946
    if [llength $args]==3 then {
947
        set message [lindex $args 2]
948
    } else {
949
        set message $command
950
    }
951
 
952
    return [gdb_test $command $pattern $message]
953
}
954
 
955
proc gdb_reinitialize_dir { subdir } {
956
    global gdb_prompt
957
 
958
    if [is_remote host] {
959
        return "";
960
    }
961
    send_gdb "dir\n"
962
    gdb_expect 60 {
963
        -re "Reinitialize source path to empty.*y or n. " {
964
            send_gdb "y\n"
965
            gdb_expect 60 {
966
                -re "Source directories searched.*$gdb_prompt $" {
967
                    send_gdb "dir $subdir\n"
968
                    gdb_expect 60 {
969
                        -re "Source directories searched.*$gdb_prompt $" {
970
                            verbose "Dir set to $subdir"
971
                        }
972
                        -re "$gdb_prompt $" {
973
                            perror "Dir \"$subdir\" failed."
974
                        }
975
                    }
976
                }
977
                -re "$gdb_prompt $" {
978
                    perror "Dir \"$subdir\" failed."
979
                }
980
            }
981
        }
982
        -re "$gdb_prompt $" {
983
            perror "Dir \"$subdir\" failed."
984
        }
985
    }
986
}
987
 
988
#
989
# gdb_exit -- exit the GDB, killing the target program if necessary
990
#
991
proc default_gdb_exit {} {
992
    global GDB
993
    global GDBFLAGS
994
    global verbose
995
    global gdb_spawn_id;
996
 
997
    gdb_stop_suppressing_tests;
998
 
999
    if ![info exists gdb_spawn_id] {
1000
        return;
1001
    }
1002
 
1003
    verbose "Quitting $GDB $GDBFLAGS"
1004
 
1005
    if { [is_remote host] && [board_info host exists fileid] } {
1006
        send_gdb "quit\n";
1007
        gdb_expect 10 {
1008
            -re "y or n" {
1009
                send_gdb "y\n";
1010
                exp_continue;
1011
            }
1012
            -re "DOSEXIT code" { }
1013
            default { }
1014
        }
1015
    }
1016
 
1017
    if ![is_remote host] {
1018
        remote_close host;
1019
    }
1020
    unset gdb_spawn_id
1021
}
1022
 
1023
# Load a file into the debugger.
1024
# The return value is 0 for success, -1 for failure.
1025
#
1026
# This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1027
# to one of these values:
1028
#
1029
#   debug    file was loaded successfully and has debug information
1030
#   nodebug  file was loaded successfully and has no debug information
1031
#   fail     file was not loaded
1032
#
1033
# I tried returning this information as part of the return value,
1034
# but ran into a mess because of the many re-implementations of
1035
# gdb_load in config/*.exp.
1036
#
1037
# TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1038
# this if they can get more information set.
1039
 
1040
proc gdb_file_cmd { arg } {
1041
    global gdb_prompt
1042
    global verbose
1043
    global GDB
1044
    global last_loaded_file
1045
 
1046
    set last_loaded_file $arg
1047
 
1048
    # Set whether debug info was found.
1049
    # Default to "fail".
1050
    global gdb_file_cmd_debug_info
1051
    set gdb_file_cmd_debug_info "fail"
1052
 
1053
    if [is_remote host] {
1054
        set arg [remote_download host $arg]
1055
        if { $arg == "" } {
1056
            perror "download failed"
1057
            return -1
1058
        }
1059
    }
1060
 
1061
    # The file command used to kill the remote target.  For the benefit
1062
    # of the testsuite, preserve this behavior.
1063
    send_gdb "kill\n"
1064
    gdb_expect 120 {
1065
        -re "Kill the program being debugged. .y or n. $" {
1066
            send_gdb "y\n"
1067
            verbose "\t\tKilling previous program being debugged"
1068
            exp_continue
1069
        }
1070
        -re "$gdb_prompt $" {
1071
            # OK.
1072
        }
1073
    }
1074
 
1075
    send_gdb "file $arg\n"
1076
    gdb_expect 120 {
1077
        -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" {
1078
            verbose "\t\tLoaded $arg into the $GDB with no debugging symbols"
1079
            set gdb_file_cmd_debug_info "nodebug"
1080
            return 0
1081
        }
1082
        -re "Reading symbols from.*done.*$gdb_prompt $" {
1083
            verbose "\t\tLoaded $arg into the $GDB"
1084
            set gdb_file_cmd_debug_info "debug"
1085
            return 0
1086
        }
1087
        -re "Load new symbol table from \".*\".*y or n. $" {
1088
            send_gdb "y\n"
1089
            gdb_expect 120 {
1090
                -re "Reading symbols from.*done.*$gdb_prompt $" {
1091
                    verbose "\t\tLoaded $arg with new symbol table into $GDB"
1092
                    set gdb_file_cmd_debug_info "debug"
1093
                    return 0
1094
                }
1095
                timeout {
1096
                    perror "(timeout) Couldn't load $arg, other program already loaded."
1097
                    return -1
1098
                }
1099
            }
1100
        }
1101
        -re "No such file or directory.*$gdb_prompt $" {
1102
            perror "($arg) No such file or directory"
1103
            return -1
1104
        }
1105
        -re "$gdb_prompt $" {
1106
            perror "couldn't load $arg into $GDB."
1107
            return -1
1108
            }
1109
        timeout {
1110
            perror "couldn't load $arg into $GDB (timed out)."
1111
            return -1
1112
        }
1113
        eof {
1114
            # This is an attempt to detect a core dump, but seems not to
1115
            # work.  Perhaps we need to match .* followed by eof, in which
1116
            # gdb_expect does not seem to have a way to do that.
1117
            perror "couldn't load $arg into $GDB (end of file)."
1118
            return -1
1119
        }
1120
    }
1121
}
1122
 
1123
#
1124
# start gdb -- start gdb running, default procedure
1125
#
1126
# When running over NFS, particularly if running many simultaneous
1127
# tests on different hosts all using the same server, things can
1128
# get really slow.  Give gdb at least 3 minutes to start up.
1129
#
1130
proc default_gdb_start { } {
1131
    global verbose
1132
    global GDB
1133
    global GDBFLAGS
1134
    global gdb_prompt
1135
    global timeout
1136
    global gdb_spawn_id;
1137
 
1138
    gdb_stop_suppressing_tests;
1139
 
1140
    verbose "Spawning $GDB -nw $GDBFLAGS"
1141
 
1142
    if [info exists gdb_spawn_id] {
1143
        return 0;
1144
    }
1145
 
1146
    if ![is_remote host] {
1147
        if { [which $GDB] == 0 } then {
1148
            perror "$GDB does not exist."
1149
            exit 1
1150
        }
1151
    }
1152
    set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"];
1153
    if { $res < 0 || $res == "" } {
1154
        perror "Spawning $GDB failed."
1155
        return 1;
1156
    }
1157
    gdb_expect 360 {
1158
        -re "\[\r\n\]$gdb_prompt $" {
1159
            verbose "GDB initialized."
1160
        }
1161
        -re "$gdb_prompt $"     {
1162
            perror "GDB never initialized."
1163
            return -1
1164
        }
1165
        timeout {
1166
            perror "(timeout) GDB never initialized after 10 seconds."
1167
            remote_close host;
1168
            return -1
1169
        }
1170
    }
1171
    set gdb_spawn_id -1;
1172
    # force the height to "unlimited", so no pagers get used
1173
 
1174
    send_gdb "set height 0\n"
1175
    gdb_expect 10 {
1176
        -re "$gdb_prompt $" {
1177
            verbose "Setting height to 0." 2
1178
        }
1179
        timeout {
1180
            warning "Couldn't set the height to 0"
1181
        }
1182
    }
1183
    # force the width to "unlimited", so no wraparound occurs
1184
    send_gdb "set width 0\n"
1185
    gdb_expect 10 {
1186
        -re "$gdb_prompt $" {
1187
            verbose "Setting width to 0." 2
1188
        }
1189
        timeout {
1190
            warning "Couldn't set the width to 0."
1191
        }
1192
    }
1193
    return 0;
1194
}
1195
 
1196
# Return a 1 for configurations for which we don't even want to try to
1197
# test C++.
1198
 
1199
proc skip_cplus_tests {} {
1200
    if { [istarget "h8300-*-*"] } {
1201
        return 1
1202
    }
1203
 
1204
    # The C++ IO streams are too large for HC11/HC12 and are thus not
1205
    # available.  The gdb C++ tests use them and don't compile.
1206
    if { [istarget "m6811-*-*"] } {
1207
        return 1
1208
    }
1209
    if { [istarget "m6812-*-*"] } {
1210
        return 1
1211
    }
1212
    return 0
1213
}
1214
 
1215
# Return a 1 if I don't even want to try to test FORTRAN.
1216
 
1217
proc skip_fortran_tests {} {
1218
    return 0
1219
}
1220
 
1221
# Return a 1 if we should skip shared library tests.
1222
 
1223
proc skip_shlib_tests {} {
1224
    # Run the shared library tests on native systems.
1225
    if {[isnative]} {
1226
        return 0
1227
    }
1228
 
1229
    # An abbreviated list of remote targets where we should be able to
1230
    # run shared library tests.
1231
    if {([istarget *-*-linux*]
1232
         || [istarget *-*-*bsd*]
1233
         || [istarget *-*-solaris2*]
1234
         || [istarget arm*-*-symbianelf*]
1235
         || [istarget *-*-mingw*]
1236
         || [istarget *-*-cygwin*]
1237
         || [istarget *-*-pe*])} {
1238
        return 0
1239
    }
1240
 
1241
    return 1
1242
}
1243
 
1244
# Run a test on the target to see if it supports vmx hardware.  Return 0 if so,
1245
# 1 if it does not.  Based on 'check_vmx_hw_available' from the GCC testsuite.
1246
 
1247
proc skip_altivec_tests {} {
1248
    global skip_vmx_tests_saved
1249
    global srcdir subdir gdb_prompt
1250
 
1251
    # Use the cached value, if it exists.
1252
    set me "skip_altivec_tests"
1253
    if [info exists skip_vmx_tests_saved] {
1254
        verbose "$me:  returning saved $skip_vmx_tests_saved" 2
1255
        return $skip_vmx_tests_saved
1256
    }
1257
 
1258
    # Some simulators are known to not support VMX instructions.
1259
    if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
1260
        verbose "$me:  target known to not support VMX, returning 1" 2
1261
        return [set skip_vmx_tests_saved 1]
1262
    }
1263
 
1264
    # Make sure we have a compiler that understands altivec.
1265
    set compile_flags {debug nowarnings}
1266
    if [get_compiler_info not-used] {
1267
       warning "Could not get compiler info"
1268
       return 1
1269
    }
1270
    if [test_compiler_info gcc*] {
1271
        set compile_flags "$compile_flags additional_flags=-maltivec"
1272
    } elseif [test_compiler_info xlc*] {
1273
        set compile_flags "$compile_flags additional_flags=-qaltivec"
1274
    } else {
1275
        verbose "Could not compile with altivec support, returning 1" 2
1276
        return 1
1277
    }
1278
 
1279
    # Set up, compile, and execute a test program containing VMX instructions.
1280
    # Include the current process ID in the file names to prevent conflicts
1281
    # with invocations for multiple testsuites.
1282
    set src vmx[pid].c
1283
    set exe vmx[pid].x
1284
 
1285
    set f [open $src "w"]
1286
    puts $f "int main() {"
1287
    puts $f "#ifdef __MACH__"
1288
    puts $f "  asm volatile (\"vor v0,v0,v0\");"
1289
    puts $f "#else"
1290
    puts $f "  asm volatile (\"vor 0,0,0\");"
1291
    puts $f "#endif"
1292
    puts $f "  return 0; }"
1293
    close $f
1294
 
1295
    verbose "$me:  compiling testfile $src" 2
1296
    set lines [gdb_compile $src $exe executable $compile_flags]
1297
    file delete $src
1298
 
1299
    if ![string match "" $lines] then {
1300
        verbose "$me:  testfile compilation failed, returning 1" 2
1301
        return [set skip_vmx_tests_saved 1]
1302
    }
1303
 
1304
    # No error message, compilation succeeded so now run it via gdb.
1305
 
1306
    gdb_exit
1307
    gdb_start
1308
    gdb_reinitialize_dir $srcdir/$subdir
1309
    gdb_load "$exe"
1310
    gdb_run_cmd
1311
    gdb_expect {
1312
        -re ".*Illegal instruction.*${gdb_prompt} $" {
1313
            verbose -log "\n$me altivec hardware not detected"
1314
            set skip_vmx_tests_saved 1
1315
        }
1316
        -re ".*Program exited normally.*${gdb_prompt} $" {
1317
            verbose -log "\n$me: altivec hardware detected"
1318
            set skip_vmx_tests_saved 0
1319
        }
1320
        default {
1321
          warning "\n$me: default case taken"
1322
            set skip_vmx_tests_saved 1
1323
        }
1324
    }
1325
    gdb_exit
1326
    remote_file build delete $exe
1327
 
1328
    verbose "$me:  returning $skip_vmx_tests_saved" 2
1329
    return $skip_vmx_tests_saved
1330
}
1331
 
1332
# Skip all the tests in the file if you are not on an hppa running
1333
# hpux target.
1334
 
1335
proc skip_hp_tests {} {
1336
    eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ]
1337
    verbose "Skip hp tests is $skip_hp"
1338
    return $skip_hp
1339
}
1340
 
1341
set compiler_info               "unknown"
1342
set gcc_compiled                0
1343
set hp_cc_compiler              0
1344
set hp_aCC_compiler             0
1345
 
1346
# Figure out what compiler I am using.
1347
#
1348
# BINFILE is a "compiler information" output file.  This implementation
1349
# does not use BINFILE.
1350
#
1351
# ARGS can be empty or "C++".  If empty, "C" is assumed.
1352
#
1353
# There are several ways to do this, with various problems.
1354
#
1355
# [ gdb_compile -E $ifile -o $binfile.ci ]
1356
# source $binfile.ci
1357
#
1358
#   Single Unix Spec v3 says that "-E -o ..." together are not
1359
#   specified.  And in fact, the native compiler on hp-ux 11 (among
1360
#   others) does not work with "-E -o ...".  Most targets used to do
1361
#   this, and it mostly worked, because it works with gcc.
1362
#
1363
# [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
1364
# source $binfile.ci
1365
#
1366
#   This avoids the problem with -E and -o together.  This almost works
1367
#   if the build machine is the same as the host machine, which is
1368
#   usually true of the targets which are not gcc.  But this code does
1369
#   not figure which compiler to call, and it always ends up using the C
1370
#   compiler.  Not good for setting hp_aCC_compiler.  Targets
1371
#   hppa*-*-hpux* and mips*-*-irix* used to do this.
1372
#
1373
# [ gdb_compile -E $ifile > $binfile.ci ]
1374
# source $binfile.ci
1375
#
1376
#   dejagnu target_compile says that it supports output redirection,
1377
#   but the code is completely different from the normal path and I
1378
#   don't want to sweep the mines from that path.  So I didn't even try
1379
#   this.
1380
#
1381
# set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
1382
# eval $cppout
1383
#
1384
#   I actually do this for all targets now.  gdb_compile runs the right
1385
#   compiler, and TCL captures the output, and I eval the output.
1386
#
1387
#   Unfortunately, expect logs the output of the command as it goes by,
1388
#   and dejagnu helpfully prints a second copy of it right afterwards.
1389
#   So I turn off expect logging for a moment.
1390
#
1391
# [ gdb_compile $ifile $ciexe_file executable $args ]
1392
# [ remote_exec $ciexe_file ]
1393
# [ source $ci_file.out ]
1394
#
1395
#   I could give up on -E and just do this.
1396
#   I didn't get desperate enough to try this.
1397
#
1398
# -- chastain 2004-01-06
1399
 
1400
proc get_compiler_info {binfile args} {
1401
    # For compiler.c and compiler.cc
1402
    global srcdir
1403
 
1404
    # I am going to play with the log to keep noise out.
1405
    global outdir
1406
    global tool
1407
 
1408
    # These come from compiler.c or compiler.cc
1409
    global compiler_info
1410
 
1411
    # Legacy global data symbols.
1412
    global gcc_compiled
1413
    global hp_cc_compiler
1414
    global hp_aCC_compiler
1415
 
1416
    # Choose which file to preprocess.
1417
    set ifile "${srcdir}/lib/compiler.c"
1418
    if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
1419
        set ifile "${srcdir}/lib/compiler.cc"
1420
    }
1421
 
1422
    # Run $ifile through the right preprocessor.
1423
    # Toggle gdb.log to keep the compiler output out of the log.
1424
    log_file
1425
    set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ]
1426
    log_file -a "$outdir/$tool.log"
1427
 
1428
    # Eval the output.
1429
    set unknown 0
1430
    foreach cppline [ split "$cppout" "\n" ] {
1431
        if { [ regexp "^#" "$cppline" ] } {
1432
            # line marker
1433
        } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
1434
            # blank line
1435
        } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
1436
            # eval this line
1437
            verbose "get_compiler_info: $cppline" 2
1438
            eval "$cppline"
1439
        } else {
1440
            # unknown line
1441
            verbose -log "get_compiler_info: $cppline"
1442
            set unknown 1
1443
        }
1444
    }
1445
 
1446
    # Reset to unknown compiler if any diagnostics happened.
1447
    if { $unknown } {
1448
        set compiler_info "unknown"
1449
    }
1450
 
1451
    # Set the legacy symbols.
1452
    set gcc_compiled     0
1453
    set hp_cc_compiler   0
1454
    set hp_aCC_compiler  0
1455
    if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 }
1456
    if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 }
1457
    if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 }
1458
    if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 }
1459
    if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 }
1460
    if { [regexp "^hpcc-"  "$compiler_info" ] } { set hp_cc_compiler 1 }
1461
    if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 }
1462
 
1463
    # Log what happened.
1464
    verbose -log "get_compiler_info: $compiler_info"
1465
 
1466
    # Most compilers will evaluate comparisons and other boolean
1467
    # operations to 0 or 1.
1468
    uplevel \#0 { set true 1 }
1469
    uplevel \#0 { set false 0 }
1470
 
1471
    # Use of aCC results in boolean results being displayed as
1472
    # "true" or "false"
1473
    if { $hp_aCC_compiler } {
1474
      uplevel \#0 { set true true }
1475
      uplevel \#0 { set false false }
1476
    }
1477
 
1478
    return 0;
1479
}
1480
 
1481
proc test_compiler_info { {compiler ""} } {
1482
    global compiler_info
1483
 
1484
     # if no arg, return the compiler_info string
1485
 
1486
     if [string match "" $compiler] {
1487
         if [info exists compiler_info] {
1488
             return $compiler_info
1489
         } else {
1490
             perror "No compiler info found."
1491
         }
1492
     }
1493
 
1494
    return [string match $compiler $compiler_info]
1495
}
1496
 
1497
set gdb_wrapper_initialized 0
1498
 
1499
proc gdb_wrapper_init { args } {
1500
    global gdb_wrapper_initialized;
1501
    global gdb_wrapper_file;
1502
    global gdb_wrapper_flags;
1503
 
1504
    if { $gdb_wrapper_initialized == 1 } { return; }
1505
 
1506
    if {[target_info exists needs_status_wrapper] && \
1507
            [target_info needs_status_wrapper] != "0"} {
1508
        set result [build_wrapper "testglue.o"];
1509
        if { $result != "" } {
1510
            set gdb_wrapper_file [lindex $result 0];
1511
            set gdb_wrapper_flags [lindex $result 1];
1512
        } else {
1513
            warning "Status wrapper failed to build."
1514
        }
1515
    }
1516
    set gdb_wrapper_initialized 1
1517
}
1518
 
1519
proc gdb_compile {source dest type options} {
1520
    global GDB_TESTCASE_OPTIONS;
1521
    global gdb_wrapper_file;
1522
    global gdb_wrapper_flags;
1523
    global gdb_wrapper_initialized;
1524
 
1525
    set outdir [file dirname $dest]
1526
 
1527
    # Add platform-specific options if a shared library was specified using
1528
    # "shlib=librarypath" in OPTIONS.
1529
    set new_options ""
1530
    set shlib_found 0
1531
    foreach opt $options {
1532
        if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
1533
            if [test_compiler_info "xlc-*"] {
1534
                # IBM xlc compiler doesn't accept shared library named other
1535
                # than .so: use "-Wl," to bypass this
1536
                lappend source "-Wl,$shlib_name"
1537
            } elseif { ([istarget "*-*-mingw*"]
1538
                        || [istarget *-*-cygwin*]
1539
                        || [istarget *-*-pe*])} {
1540
                lappend source "${shlib_name}.a"
1541
            } else {
1542
               lappend source $shlib_name
1543
            }
1544
            if {$shlib_found == 0} {
1545
                set shlib_found 1
1546
                if { ([test_compiler_info "gcc-*"]
1547
                      &&  ([istarget "powerpc*-*-aix*"]
1548
                        || [istarget "rs6000*-*-aix*"] )) } {
1549
                    lappend options "additional_flags=-L${outdir}"
1550
                } elseif { [istarget "mips-sgi-irix*"] } {
1551
                    lappend options "additional_flags=-rpath ${outdir}"
1552
                }
1553
            }
1554
        } elseif { $opt == "shlib_load" } {
1555
            if { ([istarget "*-*-mingw*"]
1556
                  || [istarget *-*-cygwin*]
1557
                  || [istarget *-*-pe*]
1558
                  || [istarget arm*-*-symbianelf*]
1559
                  || [istarget hppa*-*-hpux*])} {
1560
                # Do not need anything.
1561
            } elseif { [istarget *-*-openbsd*] } {
1562
                lappend new_options "additional_flags=-Wl,-rpath,${outdir}"
1563
            } else {
1564
                lappend new_options "libs=-ldl"
1565
                lappend new_options "additional_flags=-Wl,-rpath,\\\$ORIGIN"
1566
            }
1567
        } else {
1568
            lappend new_options $opt
1569
        }
1570
    }
1571
    set options $new_options
1572
 
1573
    if [target_info exists gdb_stub] {
1574
        set options2 { "additional_flags=-Dusestubs" }
1575
        lappend options "libs=[target_info gdb_stub]";
1576
        set options [concat $options2 $options]
1577
    }
1578
    if [target_info exists is_vxworks] {
1579
        set options2 { "additional_flags=-Dvxworks" }
1580
        lappend options "libs=[target_info gdb_stub]";
1581
        set options [concat $options2 $options]
1582
    }
1583
    if [info exists GDB_TESTCASE_OPTIONS] {
1584
        lappend options "additional_flags=$GDB_TESTCASE_OPTIONS";
1585
    }
1586
    verbose "options are $options"
1587
    verbose "source is $source $dest $type $options"
1588
 
1589
    if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init }
1590
 
1591
    if {[target_info exists needs_status_wrapper] && \
1592
            [target_info needs_status_wrapper] != "0" && \
1593
            [info exists gdb_wrapper_file]} {
1594
        lappend options "libs=${gdb_wrapper_file}"
1595
        lappend options "ldflags=${gdb_wrapper_flags}"
1596
    }
1597
 
1598
    # Replace the "nowarnings" option with the appropriate additional_flags
1599
    # to disable compiler warnings.
1600
    set nowarnings [lsearch -exact $options nowarnings]
1601
    if {$nowarnings != -1} {
1602
        if [target_info exists gdb,nowarnings_flag] {
1603
            set flag "additional_flags=[target_info gdb,nowarnings_flag]"
1604
        } else {
1605
            set flag "additional_flags=-w"
1606
        }
1607
        set options [lreplace $options $nowarnings $nowarnings $flag]
1608
    }
1609
 
1610
    set result [target_compile $source $dest $type $options];
1611
 
1612
    # Prune uninteresting compiler (and linker) output.
1613
    regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
1614
 
1615
    regsub "\[\r\n\]*$" "$result" "" result;
1616
    regsub "^\[\r\n\]*" "$result" "" result;
1617
 
1618
    if { $result != "" && [lsearch $options quiet] == -1} {
1619
        clone_output "gdb compile failed, $result"
1620
    }
1621
    return $result;
1622
}
1623
 
1624
 
1625
# This is just like gdb_compile, above, except that it tries compiling
1626
# against several different thread libraries, to see which one this
1627
# system has.
1628
proc gdb_compile_pthreads {source dest type options} {
1629
    set built_binfile 0
1630
    set why_msg "unrecognized error"
1631
    foreach lib {-lpthreads -lpthread -lthread} {
1632
        # This kind of wipes out whatever libs the caller may have
1633
        # set.  Or maybe theirs will override ours.  How infelicitous.
1634
        set options_with_lib [concat $options [list libs=$lib quiet]]
1635
        set ccout [gdb_compile $source $dest $type $options_with_lib]
1636
        switch -regexp -- $ccout {
1637
            ".*no posix threads support.*" {
1638
                set why_msg "missing threads include file"
1639
                break
1640
            }
1641
            ".*cannot open -lpthread.*" {
1642
                set why_msg "missing runtime threads library"
1643
            }
1644
            ".*Can't find library for -lpthread.*" {
1645
                set why_msg "missing runtime threads library"
1646
            }
1647
            {^$} {
1648
                pass "successfully compiled posix threads test case"
1649
                set built_binfile 1
1650
                break
1651
            }
1652
        }
1653
    }
1654
    if {!$built_binfile} {
1655
        unsupported "Couldn't compile $source: ${why_msg}"
1656
        return -1
1657
    }
1658
}
1659
 
1660
# Build a shared library from SOURCES.  You must use get_compiler_info
1661
# first.
1662
 
1663
proc gdb_compile_shlib {sources dest options} {
1664
    set obj_options $options
1665
 
1666
    switch -glob [test_compiler_info] {
1667
        "xlc-*" {
1668
            lappend obj_options "additional_flags=-qpic"
1669
        }
1670
        "gcc-*" {
1671
            if { !([istarget "powerpc*-*-aix*"]
1672
                   || [istarget "rs6000*-*-aix*"]
1673
                   || [istarget "*-*-cygwin*"]
1674
                   || [istarget "*-*-mingw*"]
1675
                   || [istarget "*-*-pe*"]) } {
1676
                lappend obj_options "additional_flags=-fpic"
1677
            }
1678
        }
1679
        default {
1680
            switch -glob [istarget] {
1681
                "hppa*-hp-hpux*" {
1682
                    lappend obj_options "additional_flags=+z"
1683
                }
1684
                "mips-sgi-irix*" {
1685
                    # Disable SGI compiler's implicit -Dsgi
1686
                    lappend obj_options "additional_flags=-Usgi"
1687
                }
1688
                default {
1689
                    # don't know what the compiler is...
1690
                }
1691
            }
1692
        }
1693
    }
1694
 
1695
    set outdir [file dirname $dest]
1696
    set objects ""
1697
    foreach source $sources {
1698
       set sourcebase [file tail $source]
1699
       if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
1700
           return -1
1701
       }
1702
       lappend objects ${outdir}/${sourcebase}.o
1703
    }
1704
 
1705
    if [istarget "hppa*-*-hpux*"] {
1706
       remote_exec build "ld -b ${objects} -o ${dest}"
1707
    } else {
1708
       set link_options $options
1709
       if [test_compiler_info "xlc-*"] {
1710
          lappend link_options "additional_flags=-qmkshrobj"
1711
       } else {
1712
          lappend link_options "additional_flags=-shared"
1713
 
1714
           if { ([istarget "*-*-mingw*"]
1715
                 || [istarget *-*-cygwin*]
1716
                 || [istarget *-*-pe*])} {
1717
               lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a"
1718
           }
1719
       }
1720
       if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
1721
           return -1
1722
       }
1723
    }
1724
}
1725
 
1726
# This is just like gdb_compile_pthreads, above, except that we always add the
1727
# objc library for compiling Objective-C programs
1728
proc gdb_compile_objc {source dest type options} {
1729
    set built_binfile 0
1730
    set why_msg "unrecognized error"
1731
    foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
1732
        # This kind of wipes out whatever libs the caller may have
1733
        # set.  Or maybe theirs will override ours.  How infelicitous.
1734
        if { $lib == "solaris" } {
1735
            set lib "-lpthread -lposix4"
1736
        }
1737
        if { $lib != "-lobjc" } {
1738
          set lib "-lobjc $lib"
1739
        }
1740
        set options_with_lib [concat $options [list libs=$lib quiet]]
1741
        set ccout [gdb_compile $source $dest $type $options_with_lib]
1742
        switch -regexp -- $ccout {
1743
            ".*no posix threads support.*" {
1744
                set why_msg "missing threads include file"
1745
                break
1746
            }
1747
            ".*cannot open -lpthread.*" {
1748
                set why_msg "missing runtime threads library"
1749
            }
1750
            ".*Can't find library for -lpthread.*" {
1751
                set why_msg "missing runtime threads library"
1752
            }
1753
            {^$} {
1754
                pass "successfully compiled objc with posix threads test case"
1755
                set built_binfile 1
1756
                break
1757
            }
1758
        }
1759
    }
1760
    if {!$built_binfile} {
1761
        unsupported "Couldn't compile $source: ${why_msg}"
1762
        return -1
1763
    }
1764
}
1765
 
1766
proc send_gdb { string } {
1767
    global suppress_flag;
1768
    if { $suppress_flag } {
1769
        return "suppressed";
1770
    }
1771
    return [remote_send host "$string"];
1772
}
1773
 
1774
#
1775
#
1776
 
1777
proc gdb_expect { args } {
1778
    if { [llength $args] == 2  && [lindex $args 0] != "-re" } {
1779
        set gtimeout [lindex $args 0];
1780
        set expcode [list [lindex $args 1]];
1781
    } else {
1782
        upvar timeout timeout;
1783
 
1784
        set expcode $args;
1785
        if [target_info exists gdb,timeout] {
1786
            if [info exists timeout] {
1787
                if { $timeout < [target_info gdb,timeout] } {
1788
                    set gtimeout [target_info gdb,timeout];
1789
                } else {
1790
                    set gtimeout $timeout;
1791
                }
1792
            } else {
1793
                set gtimeout [target_info gdb,timeout];
1794
            }
1795
        }
1796
 
1797
        if ![info exists gtimeout] {
1798
            global timeout;
1799
            if [info exists timeout] {
1800
                set gtimeout $timeout;
1801
            } else {
1802
                # Eeeeew.
1803
                set gtimeout 60;
1804
            }
1805
        }
1806
    }
1807
    global suppress_flag;
1808
    global remote_suppress_flag;
1809
    if [info exists remote_suppress_flag] {
1810
        set old_val $remote_suppress_flag;
1811
    }
1812
    if [info exists suppress_flag] {
1813
        if { $suppress_flag } {
1814
            set remote_suppress_flag 1;
1815
        }
1816
    }
1817
    set code [catch \
1818
        {uplevel remote_expect host $gtimeout $expcode} string];
1819
    if [info exists old_val] {
1820
        set remote_suppress_flag $old_val;
1821
    } else {
1822
        if [info exists remote_suppress_flag] {
1823
            unset remote_suppress_flag;
1824
        }
1825
    }
1826
 
1827
    if {$code == 1} {
1828
        global errorInfo errorCode;
1829
 
1830
        return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1831
    } elseif {$code == 2} {
1832
        return -code return $string
1833
    } elseif {$code == 3} {
1834
        return
1835
    } elseif {$code > 4} {
1836
        return -code $code $string
1837
    }
1838
}
1839
 
1840
# gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs
1841
#
1842
# Check for long sequence of output by parts.
1843
# MESSAGE: is the test message to be printed with the test success/fail.
1844
# SENTINEL: Is the terminal pattern indicating that output has finished.
1845
# LIST: is the sequence of outputs to match.
1846
# If the sentinel is recognized early, it is considered an error.
1847
#
1848
# Returns:
1849
#    1 if the test failed,
1850
#    0 if the test passes,
1851
#   -1 if there was an internal error.
1852
#
1853
proc gdb_expect_list {test sentinel list} {
1854
    global gdb_prompt
1855
    global suppress_flag
1856
    set index 0
1857
    set ok 1
1858
    if { $suppress_flag } {
1859
        set ok 0
1860
        unresolved "${test}"
1861
    }
1862
    while { ${index} < [llength ${list}] } {
1863
        set pattern [lindex ${list} ${index}]
1864
        set index [expr ${index} + 1]
1865
        if { ${index} == [llength ${list}] } {
1866
            if { ${ok} } {
1867
                gdb_expect {
1868
                    -re "${pattern}${sentinel}" {
1869
                        # pass "${test}, pattern ${index} + sentinel"
1870
                    }
1871
                    -re "${sentinel}" {
1872
                        fail "${test} (pattern ${index} + sentinel)"
1873
                        set ok 0
1874
                    }
1875
                    -re ".*A problem internal to GDB has been detected" {
1876
                        fail "${test} (GDB internal error)"
1877
                        set ok 0
1878
                        gdb_internal_error_resync
1879
                    }
1880
                    timeout {
1881
                        fail "${test} (pattern ${index} + sentinel) (timeout)"
1882
                        set ok 0
1883
                    }
1884
                }
1885
            } else {
1886
                # unresolved "${test}, pattern ${index} + sentinel"
1887
            }
1888
        } else {
1889
            if { ${ok} } {
1890
                gdb_expect {
1891
                    -re "${pattern}" {
1892
                        # pass "${test}, pattern ${index}"
1893
                    }
1894
                    -re "${sentinel}" {
1895
                        fail "${test} (pattern ${index})"
1896
                        set ok 0
1897
                    }
1898
                    -re ".*A problem internal to GDB has been detected" {
1899
                        fail "${test} (GDB internal error)"
1900
                        set ok 0
1901
                        gdb_internal_error_resync
1902
                    }
1903
                    timeout {
1904
                        fail "${test} (pattern ${index}) (timeout)"
1905
                        set ok 0
1906
                    }
1907
                }
1908
            } else {
1909
                # unresolved "${test}, pattern ${index}"
1910
            }
1911
        }
1912
    }
1913
    if { ${ok} } {
1914
        pass "${test}"
1915
        return 0
1916
    } else {
1917
        return 1
1918
    }
1919
}
1920
 
1921
#
1922
#
1923
proc gdb_suppress_entire_file { reason } {
1924
    global suppress_flag;
1925
 
1926
    warning "$reason\n";
1927
    set suppress_flag -1;
1928
}
1929
 
1930
#
1931
# Set suppress_flag, which will cause all subsequent calls to send_gdb and
1932
# gdb_expect to fail immediately (until the next call to
1933
# gdb_stop_suppressing_tests).
1934
#
1935
proc gdb_suppress_tests { args } {
1936
    global suppress_flag;
1937
 
1938
    return;  # fnf - disable pending review of results where
1939
             # testsuite ran better without this
1940
    incr suppress_flag;
1941
 
1942
    if { $suppress_flag == 1 } {
1943
        if { [llength $args] > 0 } {
1944
            warning "[lindex $args 0]\n";
1945
        } else {
1946
            warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n";
1947
        }
1948
    }
1949
}
1950
 
1951
#
1952
# Clear suppress_flag.
1953
#
1954
proc gdb_stop_suppressing_tests { } {
1955
    global suppress_flag;
1956
 
1957
    if [info exists suppress_flag] {
1958
        if { $suppress_flag > 0 } {
1959
            set suppress_flag 0;
1960
            clone_output "Tests restarted.\n";
1961
        }
1962
    } else {
1963
        set suppress_flag 0;
1964
    }
1965
}
1966
 
1967
proc gdb_clear_suppressed { } {
1968
    global suppress_flag;
1969
 
1970
    set suppress_flag 0;
1971
}
1972
 
1973
proc gdb_start { } {
1974
    default_gdb_start
1975
}
1976
 
1977
proc gdb_exit { } {
1978
    catch default_gdb_exit
1979
}
1980
 
1981
#
1982
# gdb_load_cmd -- load a file into the debugger.
1983
#                 ARGS - additional args to load command.
1984
#                 return a -1 if anything goes wrong.
1985
#
1986
proc gdb_load_cmd { args } {
1987
    global gdb_prompt
1988
 
1989
    if [target_info exists gdb_load_timeout] {
1990
        set loadtimeout [target_info gdb_load_timeout]
1991
    } else {
1992
        set loadtimeout 1600
1993
    }
1994
    send_gdb "load $args\n"
1995
    verbose "Timeout is now $loadtimeout seconds" 2
1996
    gdb_expect $loadtimeout {
1997
        -re "Loading section\[^\r\]*\r\n" {
1998
            exp_continue
1999
        }
2000
        -re "Start address\[\r\]*\r\n" {
2001
            exp_continue
2002
        }
2003
        -re "Transfer rate\[\r\]*\r\n" {
2004
            exp_continue
2005
        }
2006
        -re "Memory access error\[^\r\]*\r\n" {
2007
            perror "Failed to load program"
2008
            return -1
2009
        }
2010
        -re "$gdb_prompt $" {
2011
            return 0
2012
        }
2013
        -re "(.*)\r\n$gdb_prompt " {
2014
            perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
2015
            return -1
2016
        }
2017
        timeout {
2018
            perror "Timed out trying to load $arg."
2019
            return -1
2020
        }
2021
    }
2022
    return -1
2023
}
2024
 
2025
# gdb_download
2026
#
2027
# Copy a file to the remote target and return its target filename.
2028
# Schedule the file to be deleted at the end of this test.
2029
 
2030
proc gdb_download { filename } {
2031
    global cleanfiles
2032
 
2033
    set destname [remote_download target $filename]
2034
    lappend cleanfiles $destname
2035
    return $destname
2036
}
2037
 
2038
# gdb_load_shlibs LIB...
2039
#
2040
# Copy the listed libraries to the target.
2041
 
2042
proc gdb_load_shlibs { args } {
2043
    if {![is_remote target]} {
2044
        return
2045
    }
2046
 
2047
    foreach file $args {
2048
        gdb_download $file
2049
    }
2050
 
2051
    # Even if the target supplies full paths for shared libraries,
2052
    # they may not be paths for this system.
2053
    gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" ""
2054
}
2055
 
2056
#
2057
# gdb_load -- load a file into the debugger.
2058
# Many files in config/*.exp override this procedure.
2059
#
2060
proc gdb_load { arg } {
2061
    return [gdb_file_cmd $arg]
2062
}
2063
 
2064
# gdb_reload -- load a file into the target.  Called before "running",
2065
# either the first time or after already starting the program once,
2066
# for remote targets.  Most files that override gdb_load should now
2067
# override this instead.
2068
 
2069
proc gdb_reload { } {
2070
    # For the benefit of existing configurations, default to gdb_load.
2071
    # Specifying no file defaults to the executable currently being
2072
    # debugged.
2073
    return [gdb_load ""]
2074
}
2075
 
2076
proc gdb_continue { function } {
2077
    global decimal
2078
 
2079
    return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"];
2080
}
2081
 
2082
proc default_gdb_init { args } {
2083
    global gdb_wrapper_initialized
2084
    global cleanfiles
2085
 
2086
    set cleanfiles {}
2087
 
2088
    gdb_clear_suppressed;
2089
 
2090
    # Make sure that the wrapper is rebuilt
2091
    # with the appropriate multilib option.
2092
    set gdb_wrapper_initialized 0
2093
 
2094
    # Unlike most tests, we have a small number of tests that generate
2095
    # a very large amount of output.  We therefore increase the expect
2096
    # buffer size to be able to contain the entire test output.
2097
    match_max -d 30000
2098
 
2099
    # We want to add the name of the TCL testcase to the PASS/FAIL messages.
2100
    if { [llength $args] > 0 } {
2101
        global pf_prefix
2102
 
2103
        set file [lindex $args 0];
2104
 
2105
        set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:";
2106
    }
2107
    global gdb_prompt;
2108
    if [target_info exists gdb_prompt] {
2109
        set gdb_prompt [target_info gdb_prompt];
2110
    } else {
2111
        set gdb_prompt "\\(gdb\\)"
2112
    }
2113
}
2114
 
2115
proc gdb_init { args } {
2116
    return [eval default_gdb_init $args];
2117
}
2118
 
2119
proc gdb_finish { } {
2120
    global cleanfiles
2121
 
2122
    # Exit first, so that the files are no longer in use.
2123
    gdb_exit
2124
 
2125
    if { [llength $cleanfiles] > 0 } {
2126
        eval remote_file target delete $cleanfiles
2127
        set cleanfiles {}
2128
    }
2129
}
2130
 
2131
global debug_format
2132
set debug_format "unknown"
2133
 
2134
# Run the gdb command "info source" and extract the debugging format
2135
# information from the output and save it in debug_format.
2136
 
2137
proc get_debug_format { } {
2138
    global gdb_prompt
2139
    global verbose
2140
    global expect_out
2141
    global debug_format
2142
 
2143
    set debug_format "unknown"
2144
    send_gdb "info source\n"
2145
    gdb_expect 10 {
2146
        -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
2147
            set debug_format $expect_out(1,string)
2148
            verbose "debug format is $debug_format"
2149
            return 1;
2150
        }
2151
        -re "No current source file.\r\n$gdb_prompt $" {
2152
            perror "get_debug_format used when no current source file"
2153
            return 0;
2154
        }
2155
        -re "$gdb_prompt $" {
2156
            warning "couldn't check debug format (no valid response)."
2157
            return 1;
2158
        }
2159
        timeout {
2160
            warning "couldn't check debug format (timed out)."
2161
            return 1;
2162
        }
2163
    }
2164
}
2165
 
2166
# Return true if FORMAT matches the debug format the current test was
2167
# compiled with.  FORMAT is a shell-style globbing pattern; it can use
2168
# `*', `[...]', and so on.
2169
#
2170
# This function depends on variables set by `get_debug_format', above.
2171
 
2172
proc test_debug_format {format} {
2173
    global debug_format
2174
 
2175
    return [expr [string match $format $debug_format] != 0]
2176
}
2177
 
2178
# Like setup_xfail, but takes the name of a debug format (DWARF 1,
2179
# COFF, stabs, etc).  If that format matches the format that the
2180
# current test was compiled with, then the next test is expected to
2181
# fail for any target.  Returns 1 if the next test or set of tests is
2182
# expected to fail, 0 otherwise (or if it is unknown).  Must have
2183
# previously called get_debug_format.
2184
proc setup_xfail_format { format } {
2185
    set ret [test_debug_format $format];
2186
 
2187
    if {$ret} then {
2188
        setup_xfail "*-*-*"
2189
    }
2190
    return $ret;
2191
}
2192
 
2193
proc gdb_step_for_stub { } {
2194
    global gdb_prompt;
2195
 
2196
    if ![target_info exists gdb,use_breakpoint_for_stub] {
2197
        if [target_info exists gdb_stub_step_command] {
2198
            set command [target_info gdb_stub_step_command];
2199
        } else {
2200
            set command "step";
2201
        }
2202
        send_gdb "${command}\n";
2203
        set tries 0;
2204
        gdb_expect 60 {
2205
            -re "(main.* at |.*in .*start).*$gdb_prompt" {
2206
                return;
2207
            }
2208
            -re ".*$gdb_prompt" {
2209
                incr tries;
2210
                if { $tries == 5 } {
2211
                    fail "stepping out of breakpoint function";
2212
                    return;
2213
                }
2214
                send_gdb "${command}\n";
2215
                exp_continue;
2216
            }
2217
            default {
2218
                fail "stepping out of breakpoint function";
2219
                return;
2220
            }
2221
        }
2222
    }
2223
    send_gdb "where\n";
2224
    gdb_expect {
2225
        -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" {
2226
            set file $expect_out(1,string);
2227
            set linenum [expr $expect_out(2,string) + 1];
2228
            set breakplace "${file}:${linenum}";
2229
        }
2230
        default {}
2231
    }
2232
    send_gdb "break ${breakplace}\n";
2233
    gdb_expect 60 {
2234
        -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" {
2235
            set breakpoint $expect_out(1,string);
2236
        }
2237
        -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" {
2238
            set breakpoint $expect_out(1,string);
2239
        }
2240
        default {}
2241
    }
2242
    send_gdb "continue\n";
2243
    gdb_expect 60 {
2244
        -re "Breakpoint ${breakpoint},.*$gdb_prompt" {
2245
            gdb_test "delete $breakpoint" ".*" "";
2246
            return;
2247
        }
2248
        default {}
2249
    }
2250
}
2251
 
2252
# gdb_get_line_number TEXT [FILE]
2253
#
2254
# Search the source file FILE, and return the line number of the
2255
# first line containing TEXT.  If no match is found, return -1.
2256
#
2257
# TEXT is a string literal, not a regular expression.
2258
#
2259
# The default value of FILE is "$srcdir/$subdir/$srcfile".  If FILE is
2260
# specified, and does not start with "/", then it is assumed to be in
2261
# "$srcdir/$subdir".  This is awkward, and can be fixed in the future,
2262
# by changing the callers and the interface at the same time.
2263
# In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
2264
# gdb.base/ena-dis-br.exp.
2265
#
2266
# Use this function to keep your test scripts independent of the
2267
# exact line numbering of the source file.  Don't write:
2268
#
2269
#   send_gdb "break 20"
2270
#
2271
# This means that if anyone ever edits your test's source file,
2272
# your test could break.  Instead, put a comment like this on the
2273
# source file line you want to break at:
2274
#
2275
#   /* breakpoint spot: frotz.exp: test name */
2276
#
2277
# and then write, in your test script (which we assume is named
2278
# frotz.exp):
2279
#
2280
#   send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
2281
#
2282
# (Yes, Tcl knows how to handle the nested quotes and brackets.
2283
# Try this:
2284
#       $ tclsh
2285
#       % puts "foo [lindex "bar baz" 1]"
2286
#       foo baz
2287
#       %
2288
# Tcl is quite clever, for a little stringy language.)
2289
#
2290
# ===
2291
#
2292
# The previous implementation of this procedure used the gdb search command.
2293
# This version is different:
2294
#
2295
#   . It works with MI, and it also works when gdb is not running.
2296
#
2297
#   . It operates on the build machine, not the host machine.
2298
#
2299
#   . For now, this implementation fakes a current directory of
2300
#     $srcdir/$subdir to be compatible with the old implementation.
2301
#     This will go away eventually and some callers will need to
2302
#     be changed.
2303
#
2304
#   . The TEXT argument is literal text and matches literally,
2305
#     not a regular expression as it was before.
2306
#
2307
#   . State changes in gdb, such as changing the current file
2308
#     and setting $_, no longer happen.
2309
#
2310
# After a bit of time we can forget about the differences from the
2311
# old implementation.
2312
#
2313
# --chastain 2004-08-05
2314
 
2315
proc gdb_get_line_number { text { file "" } } {
2316
    global srcdir
2317
    global subdir
2318
    global srcfile
2319
 
2320
    if { "$file" == "" } then {
2321
        set file "$srcfile"
2322
    }
2323
    if { ! [regexp "^/" "$file"] } then {
2324
        set file "$srcdir/$subdir/$file"
2325
    }
2326
 
2327
    if { [ catch { set fd [open "$file"] } message ] } then {
2328
        perror "$message"
2329
        return -1
2330
    }
2331
 
2332
    set found -1
2333
    for { set line 1 } { 1 } { incr line } {
2334
        if { [ catch { set nchar [gets "$fd" body] } message ] } then {
2335
            perror "$message"
2336
            return -1
2337
        }
2338
        if { $nchar < 0 } then {
2339
            break
2340
        }
2341
        if { [string first "$text" "$body"] >= 0 } then {
2342
            set found $line
2343
            break
2344
        }
2345
    }
2346
 
2347
    if { [ catch { close "$fd" } message ] } then {
2348
        perror "$message"
2349
        return -1
2350
    }
2351
 
2352
    return $found
2353
}
2354
 
2355
# gdb_continue_to_end:
2356
#       The case where the target uses stubs has to be handled specially. If a
2357
#       stub is used, we set a breakpoint at exit because we cannot rely on
2358
#       exit() behavior of a remote target.
2359
#
2360
# mssg is the error message that gets printed.
2361
 
2362
proc gdb_continue_to_end {mssg} {
2363
  if [target_info exists use_gdb_stub] {
2364
    if {![gdb_breakpoint "exit"]} {
2365
      return 0
2366
    }
2367
    gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \
2368
      "continue until exit at $mssg"
2369
  } else {
2370
    # Continue until we exit.  Should not stop again.
2371
    # Don't bother to check the output of the program, that may be
2372
    # extremely tough for some remote systems.
2373
    gdb_test "continue"\
2374
      "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\
2375
      "continue until exit at $mssg"
2376
  }
2377
}
2378
 
2379
proc rerun_to_main {} {
2380
  global gdb_prompt
2381
 
2382
  if [target_info exists use_gdb_stub] {
2383
    gdb_run_cmd
2384
    gdb_expect {
2385
      -re ".*Breakpoint .*main .*$gdb_prompt $"\
2386
              {pass "rerun to main" ; return 0}
2387
      -re "$gdb_prompt $"\
2388
              {fail "rerun to main" ; return 0}
2389
      timeout {fail "(timeout) rerun to main" ; return 0}
2390
    }
2391
  } else {
2392
    send_gdb "run\n"
2393
    gdb_expect {
2394
      -re "The program .* has been started already.*y or n. $" {
2395
          send_gdb "y\n"
2396
          exp_continue
2397
      }
2398
      -re "Starting program.*$gdb_prompt $"\
2399
              {pass "rerun to main" ; return 0}
2400
      -re "$gdb_prompt $"\
2401
              {fail "rerun to main" ; return 0}
2402
      timeout {fail "(timeout) rerun to main" ; return 0}
2403
    }
2404
  }
2405
}
2406
 
2407
# Print a message and return true if a test should be skipped
2408
# due to lack of floating point suport.
2409
 
2410
proc gdb_skip_float_test { msg } {
2411
    if [target_info exists gdb,skip_float_tests] {
2412
        verbose "Skipping test '$msg': no float tests.";
2413
        return 1;
2414
    }
2415
    return 0;
2416
}
2417
 
2418
# Print a message and return true if a test should be skipped
2419
# due to lack of stdio support.
2420
 
2421
proc gdb_skip_stdio_test { msg } {
2422
    if [target_info exists gdb,noinferiorio] {
2423
        verbose "Skipping test '$msg': no inferior i/o.";
2424
        return 1;
2425
    }
2426
    return 0;
2427
}
2428
 
2429
proc gdb_skip_bogus_test { msg } {
2430
    return 0;
2431
}
2432
 
2433
# Return true if a test should be skipped due to lack of XML support
2434
# in the host GDB.
2435
 
2436
proc gdb_skip_xml_test { } {
2437
    global gdb_prompt
2438
    global srcdir
2439
    global xml_missing_cached
2440
 
2441
    if {[info exists xml_missing_cached]} {
2442
        return $xml_missing_cached
2443
    }
2444
 
2445
    gdb_start
2446
    set xml_missing_cached 0
2447
    gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" {
2448
        -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
2449
            set xml_missing_cached 1
2450
        }
2451
        -re ".*$gdb_prompt $" { }
2452
    }
2453
    gdb_exit
2454
    return $xml_missing_cached
2455
}
2456
 
2457
# Note: the procedure gdb_gnu_strip_debug will produce an executable called
2458
# ${binfile}.dbglnk, which is just like the executable ($binfile) but without
2459
# the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
2460
# the name of a debuginfo only file. This file will be stored in the
2461
# gdb.base/.debug subdirectory.
2462
 
2463
# Functions for separate debug info testing
2464
 
2465
# starting with an executable:
2466
# foo --> original executable
2467
 
2468
# at the end of the process we have:
2469
# foo.stripped --> foo w/o debug info
2470
# .debug/foo.debug --> foo's debug info
2471
# foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
2472
 
2473
# Return the name of the file in which we should stor EXEC's separated
2474
# debug info. EXEC contains the full path.
2475
proc separate_debug_filename { exec } {
2476
 
2477
    # In a .debug subdirectory off the same directory where the testcase
2478
    # executable is going to be. Something like:
2479
    # /gdb/testsuite/gdb.base/.debug/blah.debug.
2480
    # This is the default location where gdb expects to findi
2481
    # the debug info file.
2482
 
2483
    set exec_dir [file dirname $exec]
2484
    set exec_file [file tail $exec]
2485
    set debug_dir [file join $exec_dir ".debug"]
2486
    set debug_file [file join $debug_dir "${exec_file}.debug"]
2487
 
2488
    return $debug_file
2489
}
2490
 
2491
# Return the build-id hex string (usually 160 bits as 40 hex characters)
2492
# converted to the form: .build-id/ab/cdef1234...89.debug
2493
# Return "" if no build-id found.
2494
proc build_id_debug_filename_get { exec } {
2495
    set tmp "${exec}-tmp"
2496
    set objcopy_program [transform objcopy]
2497
 
2498
    set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output]
2499
    verbose "result is $result"
2500
    verbose "output is $output"
2501
    if {$result == 1} {
2502
        return ""
2503
    }
2504
    set fi [open $tmp]
2505
    fconfigure $fi -translation binary
2506
    # Skip the NOTE header.
2507
    read $fi 16
2508
    set data [read $fi]
2509
    close $fi
2510
    file delete $tmp
2511
    if ![string compare $data ""] then {
2512
        return ""
2513
    }
2514
    # Convert it to hex.
2515
    binary scan $data H* data
2516
    set data [regsub {^..} $data {\0/}]
2517
    return ".build-id/${data}.debug";
2518
}
2519
 
2520
# Create stripped files for DEST, replacing it.  If ARGS is passed, it is a
2521
# list of optional flags.  The only currently supported flag is no-main,
2522
# which removes the symbol entry for main from the separate debug file.
2523
 
2524
proc gdb_gnu_strip_debug { dest args } {
2525
 
2526
    set debug_file [separate_debug_filename $dest]
2527
    set strip_to_file_program [transform strip]
2528
    set objcopy_program [transform objcopy]
2529
 
2530
    # Make sure the directory that will hold the separated debug
2531
    # info actually exists.
2532
    set debug_dir [file dirname $debug_file]
2533
    if {! [file isdirectory $debug_dir]} {
2534
        file mkdir $debug_dir
2535
    }
2536
 
2537
    set debug_link [file tail $debug_file]
2538
    set stripped_file "${dest}.stripped"
2539
 
2540
    # Get rid of the debug info, and store result in stripped_file
2541
    # something like gdb/testsuite/gdb.base/blah.stripped.
2542
    set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
2543
    verbose "result is $result"
2544
    verbose "output is $output"
2545
    if {$result == 1} {
2546
      return 1
2547
    }
2548
 
2549
    # Get rid of everything but the debug info, and store result in debug_file
2550
    # This will be in the .debug subdirectory, see above.
2551
    set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
2552
    verbose "result is $result"
2553
    verbose "output is $output"
2554
    if {$result == 1} {
2555
      return 1
2556
    }
2557
 
2558
    # If no-main is passed, strip the symbol for main from the separate
2559
    # file.  This is to simulate the behavior of elfutils's eu-strip, which
2560
    # leaves the symtab in the original file only.  There's no way to get
2561
    # objcopy or strip to remove the symbol table without also removing the
2562
    # debugging sections, so this is as close as we can get.
2563
    if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
2564
        set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
2565
        verbose "result is $result"
2566
        verbose "output is $output"
2567
        if {$result == 1} {
2568
            return 1
2569
        }
2570
        file delete "${debug_file}"
2571
        file rename "${debug_file}-tmp" "${debug_file}"
2572
    }
2573
 
2574
    # Link the two previous output files together, adding the .gnu_debuglink
2575
    # section to the stripped_file, containing a pointer to the debug_file,
2576
    # save the new file in dest.
2577
    # This will be the regular executable filename, in the usual location.
2578
    set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
2579
    verbose "result is $result"
2580
    verbose "output is $output"
2581
    if {$result == 1} {
2582
      return 1
2583
    }
2584
 
2585
   return 0
2586
}
2587
 
2588
# Test the output of GDB_COMMAND matches the pattern obtained
2589
# by concatenating all elements of EXPECTED_LINES.  This makes
2590
# it possible to split otherwise very long string into pieces.
2591
# If third argument is not empty, it's used as the name of the
2592
# test to be printed on pass/fail.
2593
proc help_test_raw { gdb_command expected_lines args } {
2594
    set message $gdb_command
2595
    if [llength $args]>0 then {
2596
        set message [lindex $args 0]
2597
    }
2598
    set expected_output [join $expected_lines ""]
2599
    gdb_test "${gdb_command}" "${expected_output}" $message
2600
}
2601
 
2602
# Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES
2603
# are regular expressions that should match the beginning of output,
2604
# before the list of commands in that class.  The presence of
2605
# command list and standard epilogue will be tested automatically.
2606
proc test_class_help { command_class expected_initial_lines args } {
2607
    set l_stock_body {
2608
        "List of commands\:.*\[\r\n\]+"
2609
        "Type \"help\" followed by command name for full documentation\.\[\r\n\]+"
2610
        "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+"
2611
        "Command name abbreviations are allowed if unambiguous\."
2612
    }
2613
    set l_entire_body [concat $expected_initial_lines $l_stock_body]
2614
 
2615
    eval [list help_test_raw "help ${command_class}" $l_entire_body] $args
2616
}
2617
 
2618
# COMMAND_LIST should have either one element -- command to test, or
2619
# two elements -- abbreviated command to test, and full command the first
2620
# element is abbreviation of.
2621
# The command must be a prefix command.  EXPECTED_INITIAL_LINES
2622
# are regular expressions that should match the beginning of output,
2623
# before the list of subcommands.  The presence of
2624
# subcommand list and standard epilogue will be tested automatically.
2625
proc test_prefix_command_help { command_list expected_initial_lines args } {
2626
    set command [lindex $command_list 0]
2627
    if {[llength $command_list]>1} {
2628
        set full_command [lindex $command_list 1]
2629
    } else {
2630
        set full_command $command
2631
    }
2632
    # Use 'list' and not just {} because we want variables to
2633
    # be expanded in this list.
2634
    set l_stock_body [list\
2635
         "List of $full_command subcommands\:.*\[\r\n\]+"\
2636
         "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\
2637
         "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\
2638
         "Command name abbreviations are allowed if unambiguous\."]
2639
    set l_entire_body [concat $expected_initial_lines $l_stock_body]
2640
    if {[llength $args]>0} {
2641
        help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
2642
    } else {
2643
        help_test_raw "help ${command}" $l_entire_body
2644
    }
2645
}

powered by: WebSVN 2.1.0

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