[gdb/testsuite] Reduce errors after gdb exit in default_gdb_start
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdb.exp
1 # Copyright 1992-2020 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17
18 # Generic gdb subroutines that should work for any target. If these
19 # need to be modified for any target, it can be done with a variable
20 # or by passing arguments.
21
22 if {$tool == ""} {
23 # Tests would fail, logs on get_compiler_info() would be missing.
24 send_error "`site.exp' not found, run `make site.exp'!\n"
25 exit 2
26 }
27
28 load_lib libgloss.exp
29 load_lib cache.exp
30 load_lib gdb-utils.exp
31 load_lib memory.exp
32
33 global GDB
34
35 # The spawn ID used for I/O interaction with the inferior. For native
36 # targets, or remote targets that can do I/O through GDB
37 # (semi-hosting) this will be the same as the host/GDB's spawn ID.
38 # Otherwise, the board may set this to some other spawn ID. E.g.,
39 # when debugging with GDBserver, this is set to GDBserver's spawn ID,
40 # so input/output is done on gdbserver's tty.
41 global inferior_spawn_id
42
43 if [info exists TOOL_EXECUTABLE] {
44 set GDB $TOOL_EXECUTABLE
45 }
46 if ![info exists GDB] {
47 if ![is_remote host] {
48 set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]]
49 } else {
50 set GDB [transform gdb]
51 }
52 }
53 verbose "using GDB = $GDB" 2
54
55 # GDBFLAGS is available for the user to set on the command line.
56 # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble
57 # Testcases may use it to add additional flags, but they must:
58 # - append new flags, not overwrite
59 # - restore the original value when done
60 global GDBFLAGS
61 if ![info exists GDBFLAGS] {
62 set GDBFLAGS ""
63 }
64 verbose "using GDBFLAGS = $GDBFLAGS" 2
65
66 # Make the build data directory available to tests.
67 set BUILD_DATA_DIRECTORY "[pwd]/../data-directory"
68
69 # INTERNAL_GDBFLAGS contains flags that the testsuite requires.
70 global INTERNAL_GDBFLAGS
71 if ![info exists INTERNAL_GDBFLAGS] {
72 set INTERNAL_GDBFLAGS "-nw -nx -data-directory $BUILD_DATA_DIRECTORY"
73 }
74
75 # The variable gdb_prompt is a regexp which matches the gdb prompt.
76 # Set it if it is not already set. This is also set by default_gdb_init
77 # but it's not clear what removing one of them will break.
78 # See with_gdb_prompt for more details on prompt handling.
79 global gdb_prompt
80 if ![info exists gdb_prompt] then {
81 set gdb_prompt "\\(gdb\\)"
82 }
83
84 # A regexp that matches the pagination prompt.
85 set pagination_prompt \
86 "--Type <RET> for more, q to quit, c to continue without paging--"
87
88 # The variable fullname_syntax_POSIX is a regexp which matches a POSIX
89 # absolute path ie. /foo/
90 set fullname_syntax_POSIX {/[^\n]*/}
91 # The variable fullname_syntax_UNC is a regexp which matches a Windows
92 # UNC path ie. \\D\foo\
93 set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\}
94 # The variable fullname_syntax_DOS_CASE is a regexp which matches a
95 # particular DOS case that GDB most likely will output
96 # ie. \foo\, but don't match \\.*\
97 set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\}
98 # The variable fullname_syntax_DOS is a regexp which matches a DOS path
99 # ie. a:\foo\ && a:foo\
100 set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\}
101 # The variable fullname_syntax is a regexp which matches what GDB considers
102 # an absolute path. It is currently debatable if the Windows style paths
103 # d:foo and \abc should be considered valid as an absolute path.
104 # Also, the purpse of this regexp is not to recognize a well formed
105 # absolute path, but to say with certainty that a path is absolute.
106 set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)"
107
108 # Needed for some tests under Cygwin.
109 global EXEEXT
110 global env
111
112 if ![info exists env(EXEEXT)] {
113 set EXEEXT ""
114 } else {
115 set EXEEXT $env(EXEEXT)
116 }
117
118 set octal "\[0-7\]+"
119
120 set inferior_exited_re "(?:\\\[Inferior \[0-9\]+ \\(\[^\n\r\]*\\) exited)"
121
122 # A regular expression that matches a value history number.
123 # E.g., $1, $2, etc.
124 set valnum_re "\\\$$decimal"
125
126 ### Only procedures should come after this point.
127
128 #
129 # gdb_version -- extract and print the version number of GDB
130 #
131 proc default_gdb_version {} {
132 global GDB
133 global INTERNAL_GDBFLAGS GDBFLAGS
134 global gdb_prompt
135 global inotify_pid
136
137 if {[info exists inotify_pid]} {
138 eval exec kill $inotify_pid
139 }
140
141 set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"]
142 set tmp [lindex $output 1]
143 set version ""
144 regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version
145 if ![is_remote host] {
146 clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
147 } else {
148 clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n"
149 }
150 }
151
152 proc gdb_version { } {
153 return [default_gdb_version]
154 }
155
156 #
157 # gdb_unload -- unload a file if one is loaded
158 # Return 0 on success, -1 on error.
159 #
160
161 proc gdb_unload {} {
162 global GDB
163 global gdb_prompt
164 send_gdb "file\n"
165 gdb_expect 60 {
166 -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue }
167 -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue }
168 -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" {
169 send_gdb "y\n" answer
170 exp_continue
171 }
172 -re "Discard symbol table from .*y or n.*$" {
173 send_gdb "y\n" answer
174 exp_continue
175 }
176 -re "$gdb_prompt $" {}
177 timeout {
178 perror "couldn't unload file in $GDB (timeout)."
179 return -1
180 }
181 }
182 return 0
183 }
184
185 # Many of the tests depend on setting breakpoints at various places and
186 # running until that breakpoint is reached. At times, we want to start
187 # with a clean-slate with respect to breakpoints, so this utility proc
188 # lets us do this without duplicating this code everywhere.
189 #
190
191 proc delete_breakpoints {} {
192 global gdb_prompt
193
194 # we need a larger timeout value here or this thing just confuses
195 # itself. May need a better implementation if possible. - guo
196 #
197 set timeout 100
198
199 set msg "delete all breakpoints in delete_breakpoints"
200 set deleted 0
201 gdb_test_multiple "delete breakpoints" "$msg" {
202 -re "Delete all breakpoints.*y or n.*$" {
203 send_gdb "y\n" answer
204 exp_continue
205 }
206 -re "$gdb_prompt $" {
207 set deleted 1
208 }
209 }
210
211 if {$deleted} {
212 # Confirm with "info breakpoints".
213 set deleted 0
214 set msg "info breakpoints"
215 gdb_test_multiple $msg $msg {
216 -re "No breakpoints or watchpoints..*$gdb_prompt $" {
217 set deleted 1
218 }
219 -re "$gdb_prompt $" {
220 }
221 }
222 }
223
224 if {!$deleted} {
225 perror "breakpoints not deleted"
226 }
227 }
228
229 # Returns true iff the target supports using the "run" command.
230
231 proc target_can_use_run_cmd {} {
232 if [target_info exists use_gdb_stub] {
233 # In this case, when we connect, the inferior is already
234 # running.
235 return 0
236 }
237
238 # Assume yes.
239 return 1
240 }
241
242 # Generic run command.
243 #
244 # The second pattern below matches up to the first newline *only*.
245 # Using ``.*$'' could swallow up output that we attempt to match
246 # elsewhere.
247 #
248 # N.B. This function does not wait for gdb to return to the prompt,
249 # that is the caller's responsibility.
250
251 proc gdb_run_cmd {args} {
252 global gdb_prompt use_gdb_stub
253
254 foreach command [gdb_init_commands] {
255 send_gdb "$command\n"
256 gdb_expect 30 {
257 -re "$gdb_prompt $" { }
258 default {
259 perror "gdb_init_command for target failed"
260 return
261 }
262 }
263 }
264
265 if $use_gdb_stub {
266 if [target_info exists gdb,do_reload_on_run] {
267 if { [gdb_reload] != 0 } {
268 return
269 }
270 send_gdb "continue\n"
271 gdb_expect 60 {
272 -re "Continu\[^\r\n\]*\[\r\n\]" {}
273 default {}
274 }
275 return
276 }
277
278 if [target_info exists gdb,start_symbol] {
279 set start [target_info gdb,start_symbol]
280 } else {
281 set start "start"
282 }
283 send_gdb "jump *$start\n"
284 set start_attempt 1
285 while { $start_attempt } {
286 # Cap (re)start attempts at three to ensure that this loop
287 # always eventually fails. Don't worry about trying to be
288 # clever and not send a command when it has failed.
289 if [expr $start_attempt > 3] {
290 perror "Jump to start() failed (retry count exceeded)"
291 return
292 }
293 set start_attempt [expr $start_attempt + 1]
294 gdb_expect 30 {
295 -re "Continuing at \[^\r\n\]*\[\r\n\]" {
296 set start_attempt 0
297 }
298 -re "No symbol \"_start\" in current.*$gdb_prompt $" {
299 perror "Can't find start symbol to run in gdb_run"
300 return
301 }
302 -re "No symbol \"start\" in current.*$gdb_prompt $" {
303 send_gdb "jump *_start\n"
304 }
305 -re "No symbol.*context.*$gdb_prompt $" {
306 set start_attempt 0
307 }
308 -re "Line.* Jump anyway.*y or n. $" {
309 send_gdb "y\n" answer
310 }
311 -re "The program is not being run.*$gdb_prompt $" {
312 if { [gdb_reload] != 0 } {
313 return
314 }
315 send_gdb "jump *$start\n"
316 }
317 timeout {
318 perror "Jump to start() failed (timeout)"
319 return
320 }
321 }
322 }
323 return
324 }
325
326 if [target_info exists gdb,do_reload_on_run] {
327 if { [gdb_reload] != 0 } {
328 return
329 }
330 }
331 send_gdb "run $args\n"
332 # This doesn't work quite right yet.
333 # Use -notransfer here so that test cases (like chng-sym.exp)
334 # may test for additional start-up messages.
335 gdb_expect 60 {
336 -re "The program .* has been started already.*y or n. $" {
337 send_gdb "y\n" answer
338 exp_continue
339 }
340 -notransfer -re "Starting program: \[^\r\n\]*" {}
341 -notransfer -re "$gdb_prompt $" {
342 # There is no more input expected.
343 }
344 }
345 }
346
347 # Generic start command. Return 0 if we could start the program, -1
348 # if we could not.
349 #
350 # N.B. This function does not wait for gdb to return to the prompt,
351 # that is the caller's responsibility.
352
353 proc gdb_start_cmd {args} {
354 global gdb_prompt use_gdb_stub
355
356 foreach command [gdb_init_commands] {
357 send_gdb "$command\n"
358 gdb_expect 30 {
359 -re "$gdb_prompt $" { }
360 default {
361 perror "gdb_init_command for target failed"
362 return -1
363 }
364 }
365 }
366
367 if $use_gdb_stub {
368 return -1
369 }
370
371 send_gdb "start $args\n"
372 # Use -notransfer here so that test cases (like chng-sym.exp)
373 # may test for additional start-up messages.
374 gdb_expect 60 {
375 -re "The program .* has been started already.*y or n. $" {
376 send_gdb "y\n" answer
377 exp_continue
378 }
379 -notransfer -re "Starting program: \[^\r\n\]*" {
380 return 0
381 }
382 }
383 return -1
384 }
385
386 # Generic starti command. Return 0 if we could start the program, -1
387 # if we could not.
388 #
389 # N.B. This function does not wait for gdb to return to the prompt,
390 # that is the caller's responsibility.
391
392 proc gdb_starti_cmd {args} {
393 global gdb_prompt use_gdb_stub
394
395 foreach command [gdb_init_commands] {
396 send_gdb "$command\n"
397 gdb_expect 30 {
398 -re "$gdb_prompt $" { }
399 default {
400 perror "gdb_init_command for target failed"
401 return -1
402 }
403 }
404 }
405
406 if $use_gdb_stub {
407 return -1
408 }
409
410 send_gdb "starti $args\n"
411 gdb_expect 60 {
412 -re "The program .* has been started already.*y or n. $" {
413 send_gdb "y\n" answer
414 exp_continue
415 }
416 -re "Starting program: \[^\r\n\]*" {
417 return 0
418 }
419 }
420 return -1
421 }
422
423 # Set a breakpoint at FUNCTION. If there is an additional argument it is
424 # a list of options; the supported options are allow-pending, temporary,
425 # message, no-message, passfail and qualified.
426 # The result is 1 for success, 0 for failure.
427 #
428 # Note: The handling of message vs no-message is messed up, but it's based
429 # on historical usage. By default this function does not print passes,
430 # only fails.
431 # no-message: turns off printing of fails (and passes, but they're already off)
432 # message: turns on printing of passes (and fails, but they're already on)
433
434 proc gdb_breakpoint { function args } {
435 global gdb_prompt
436 global decimal
437
438 set pending_response n
439 if {[lsearch -exact $args allow-pending] != -1} {
440 set pending_response y
441 }
442
443 set break_command "break"
444 set break_message "Breakpoint"
445 if {[lsearch -exact $args temporary] != -1} {
446 set break_command "tbreak"
447 set break_message "Temporary breakpoint"
448 }
449
450 if {[lsearch -exact $args qualified] != -1} {
451 append break_command " -qualified"
452 }
453
454 set print_pass 0
455 set print_fail 1
456 set no_message_loc [lsearch -exact $args no-message]
457 set message_loc [lsearch -exact $args message]
458 # The last one to appear in args wins.
459 if { $no_message_loc > $message_loc } {
460 set print_fail 0
461 } elseif { $message_loc > $no_message_loc } {
462 set print_pass 1
463 }
464
465 set test_name "setting breakpoint at $function"
466
467 send_gdb "$break_command $function\n"
468 # The first two regexps are what we get with -g, the third is without -g.
469 gdb_expect 30 {
470 -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {}
471 -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {}
472 -re "$break_message \[0-9\]* at .*$gdb_prompt $" {}
473 -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" {
474 if {$pending_response == "n"} {
475 if { $print_fail } {
476 fail $test_name
477 }
478 return 0
479 }
480 }
481 -re "Make breakpoint pending.*y or \\\[n\\\]. $" {
482 send_gdb "$pending_response\n"
483 exp_continue
484 }
485 -re "A problem internal to GDB has been detected" {
486 if { $print_fail } {
487 fail "$test_name (GDB internal error)"
488 }
489 gdb_internal_error_resync
490 return 0
491 }
492 -re "$gdb_prompt $" {
493 if { $print_fail } {
494 fail $test_name
495 }
496 return 0
497 }
498 eof {
499 if { $print_fail } {
500 fail "$test_name (eof)"
501 }
502 return 0
503 }
504 timeout {
505 if { $print_fail } {
506 fail "$test_name (timeout)"
507 }
508 return 0
509 }
510 }
511 if { $print_pass } {
512 pass $test_name
513 }
514 return 1
515 }
516
517 # Set breakpoint at function and run gdb until it breaks there.
518 # Since this is the only breakpoint that will be set, if it stops
519 # at a breakpoint, we will assume it is the one we want. We can't
520 # just compare to "function" because it might be a fully qualified,
521 # single quoted C++ function specifier.
522 #
523 # If there are additional arguments, pass them to gdb_breakpoint.
524 # We recognize no-message/message ourselves.
525 # The default is no-message.
526 # no-message is messed up here, like gdb_breakpoint: to preserve
527 # historical usage fails are always printed by default.
528 # no-message: turns off printing of fails (and passes, but they're already off)
529 # message: turns on printing of passes (and fails, but they're already on)
530
531 proc runto { function args } {
532 global gdb_prompt
533 global decimal
534
535 delete_breakpoints
536
537 # Default to "no-message".
538 set args "no-message $args"
539
540 set print_pass 0
541 set print_fail 1
542 set no_message_loc [lsearch -exact $args no-message]
543 set message_loc [lsearch -exact $args message]
544 # The last one to appear in args wins.
545 if { $no_message_loc > $message_loc } {
546 set print_fail 0
547 } elseif { $message_loc > $no_message_loc } {
548 set print_pass 1
549 }
550
551 set test_name "running to $function in runto"
552
553 # We need to use eval here to pass our varargs args to gdb_breakpoint
554 # which is also a varargs function.
555 # But we also have to be careful because $function may have multiple
556 # elements, and we don't want Tcl to move the remaining elements after
557 # the first to $args. That is why $function is wrapped in {}.
558 if ![eval gdb_breakpoint {$function} $args] {
559 return 0
560 }
561
562 gdb_run_cmd
563
564 # the "at foo.c:36" output we get with -g.
565 # the "in func" output we get without -g.
566 gdb_expect 30 {
567 -re "Break.* at .*:$decimal.*$gdb_prompt $" {
568 if { $print_pass } {
569 pass $test_name
570 }
571 return 1
572 }
573 -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" {
574 if { $print_pass } {
575 pass $test_name
576 }
577 return 1
578 }
579 -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" {
580 if { $print_fail } {
581 unsupported "non-stop mode not supported"
582 }
583 return 0
584 }
585 -re ".*A problem internal to GDB has been detected" {
586 if { $print_fail } {
587 fail "$test_name (GDB internal error)"
588 }
589 gdb_internal_error_resync
590 return 0
591 }
592 -re "$gdb_prompt $" {
593 if { $print_fail } {
594 fail $test_name
595 }
596 return 0
597 }
598 eof {
599 if { $print_fail } {
600 fail "$test_name (eof)"
601 }
602 return 0
603 }
604 timeout {
605 if { $print_fail } {
606 fail "$test_name (timeout)"
607 }
608 return 0
609 }
610 }
611 if { $print_pass } {
612 pass $test_name
613 }
614 return 1
615 }
616
617 # Ask gdb to run until we hit a breakpoint at main.
618 #
619 # N.B. This function deletes all existing breakpoints.
620 # If you don't want that, use gdb_start_cmd.
621
622 proc runto_main { } {
623 return [runto main no-message]
624 }
625
626 ### Continue, and expect to hit a breakpoint.
627 ### Report a pass or fail, depending on whether it seems to have
628 ### worked. Use NAME as part of the test name; each call to
629 ### continue_to_breakpoint should use a NAME which is unique within
630 ### that test file.
631 proc gdb_continue_to_breakpoint {name {location_pattern .*}} {
632 global gdb_prompt
633 set full_name "continue to breakpoint: $name"
634
635 set kfail_pattern "Process record does not support instruction 0xfae64 at.*"
636 gdb_test_multiple "continue" $full_name {
637 -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" {
638 pass $full_name
639 }
640 -re "\[\r\n\]*(?:$kfail_pattern)\[\r\n\]+$gdb_prompt $" {
641 kfail "gdb/25038" $full_name
642 }
643 }
644 }
645
646
647 # gdb_internal_error_resync:
648 #
649 # Answer the questions GDB asks after it reports an internal error
650 # until we get back to a GDB prompt. Decline to quit the debugging
651 # session, and decline to create a core file. Return non-zero if the
652 # resync succeeds.
653 #
654 # This procedure just answers whatever questions come up until it sees
655 # a GDB prompt; it doesn't require you to have matched the input up to
656 # any specific point. However, it only answers questions it sees in
657 # the output itself, so if you've matched a question, you had better
658 # answer it yourself before calling this.
659 #
660 # You can use this function thus:
661 #
662 # gdb_expect {
663 # ...
664 # -re ".*A problem internal to GDB has been detected" {
665 # gdb_internal_error_resync
666 # }
667 # ...
668 # }
669 #
670 proc gdb_internal_error_resync {} {
671 global gdb_prompt
672
673 verbose -log "Resyncing due to internal error."
674
675 set count 0
676 while {$count < 10} {
677 gdb_expect {
678 -re "Quit this debugging session\\? \\(y or n\\) $" {
679 send_gdb "n\n" answer
680 incr count
681 }
682 -re "Create a core file of GDB\\? \\(y or n\\) $" {
683 send_gdb "n\n" answer
684 incr count
685 }
686 -re "$gdb_prompt $" {
687 # We're resynchronized.
688 return 1
689 }
690 timeout {
691 perror "Could not resync from internal error (timeout)"
692 return 0
693 }
694 }
695 }
696 perror "Could not resync from internal error (resync count exceeded)"
697 return 0
698 }
699
700
701 # gdb_test_multiple COMMAND MESSAGE [ -promp PROMPT_REGEXP] [ -lbl ]
702 # EXPECT_ARGUMENTS
703 # Send a command to gdb; test the result.
704 #
705 # COMMAND is the command to execute, send to GDB with send_gdb. If
706 # this is the null string no command is sent.
707 # MESSAGE is a message to be printed with the built-in failure patterns
708 # if one of them matches. If MESSAGE is empty COMMAND will be used.
709 # -prompt PROMPT_REGEXP specifies a regexp matching the expected prompt
710 # after the command output. If empty, defaults to "$gdb_prompt $".
711 # -lbl specifies that line-by-line matching will be used.
712 # EXPECT_ARGUMENTS will be fed to expect in addition to the standard
713 # patterns. Pattern elements will be evaluated in the caller's
714 # context; action elements will be executed in the caller's context.
715 # Unlike patterns for gdb_test, these patterns should generally include
716 # the final newline and prompt.
717 #
718 # Returns:
719 # 1 if the test failed, according to a built-in failure pattern
720 # 0 if only user-supplied patterns matched
721 # -1 if there was an internal error.
722 #
723 # You can use this function thus:
724 #
725 # gdb_test_multiple "print foo" "test foo" {
726 # -re "expected output 1" {
727 # pass "test foo"
728 # }
729 # -re "expected output 2" {
730 # fail "test foo"
731 # }
732 # }
733 #
734 # Within action elements you can also make use of the variable
735 # gdb_test_name. This variable is setup automatically by
736 # gdb_test_multiple, and contains the value of MESSAGE. You can then
737 # write this, which is equivalent to the above:
738 #
739 # gdb_test_multiple "print foo" "test foo" {
740 # -re "expected output 1" {
741 # pass $gdb_test_name
742 # }
743 # -re "expected output 2" {
744 # fail $gdb_test_name
745 # }
746 # }
747 #
748 # Like with "expect", you can also specify the spawn id to match with
749 # -i "$id". Interesting spawn ids are $inferior_spawn_id and
750 # $gdb_spawn_id. The former matches inferior I/O, while the latter
751 # matches GDB I/O. E.g.:
752 #
753 # send_inferior "hello\n"
754 # gdb_test_multiple "continue" "test echo" {
755 # -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" {
756 # pass "got echo"
757 # }
758 # -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" {
759 # fail "hit breakpoint"
760 # }
761 # }
762 #
763 # The standard patterns, such as "Inferior exited..." and "A problem
764 # ...", all being implicitly appended to that list. These are always
765 # expected from $gdb_spawn_id. IOW, callers do not need to worry
766 # about resetting "-i" back to $gdb_spawn_id explicitly.
767 #
768 # In EXPECT_ARGUMENTS we can use a -wrap pattern flag, that wraps the regexp
769 # pattern as gdb_test wraps its message argument.
770 # This allows us to rewrite:
771 # gdb_test <command> <pattern> <message>
772 # into:
773 # gdb_test_multiple <command> <message> {
774 # -re -wrap <pattern> {
775 # pass $gdb_test_name
776 # }
777 # }
778 #
779 # In EXPECT_ARGUMENTS, a pattern flag -early can be used. It makes sure the
780 # pattern is inserted before any implicit pattern added by gdb_test_multiple.
781 # Using this pattern flag, we can f.i. setup a kfail for an assertion failure
782 # <assert> during gdb_continue_to_breakpoint by the rewrite:
783 # gdb_continue_to_breakpoint <msg> <pattern>
784 # into:
785 # set breakpoint_pattern "(?:Breakpoint|Temporary breakpoint) .* (at|in)"
786 # gdb_test_multiple "continue" "continue to breakpoint: <msg>" {
787 # -early -re "internal-error: <assert>" {
788 # setup_kfail gdb/nnnnn "*-*-*"
789 # exp_continue
790 # }
791 # -re "$breakpoint_pattern <pattern>\r\n$gdb_prompt $" {
792 # pass $gdb_test_name
793 # }
794 # }
795 #
796 proc gdb_test_multiple { command message args } {
797 global verbose use_gdb_stub
798 global gdb_prompt pagination_prompt
799 global GDB
800 global gdb_spawn_id
801 global inferior_exited_re
802 upvar timeout timeout
803 upvar expect_out expect_out
804 global any_spawn_id
805
806 set line_by_line 0
807 set prompt_regexp ""
808 for {set i 0} {$i < [llength $args]} {incr i} {
809 set arg [lindex $args $i]
810 if { $arg == "-prompt" } {
811 incr i
812 set prompt_regexp [lindex $args $i]
813 } elseif { $arg == "-lbl" } {
814 set line_by_line 1
815 } else {
816 set user_code $arg
817 break
818 }
819 }
820 if { [expr $i + 1] < [llength $args] } {
821 error "Too many arguments to gdb_test_multiple"
822 } elseif { ![info exists user_code] } {
823 error "Too few arguments to gdb_test_multiple"
824 }
825
826 if { "$prompt_regexp" == "" } {
827 set prompt_regexp "$gdb_prompt $"
828 }
829
830 if { $message == "" } {
831 set message $command
832 }
833
834 if [string match "*\[\r\n\]" $command] {
835 error "Invalid trailing newline in \"$message\" test"
836 }
837
838 if [string match "*\[\r\n\]*" $message] {
839 error "Invalid newline in \"$message\" test"
840 }
841
842 if {$use_gdb_stub
843 && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \
844 $command]} {
845 error "gdbserver does not support $command without extended-remote"
846 }
847
848 # TCL/EXPECT WART ALERT
849 # Expect does something very strange when it receives a single braced
850 # argument. It splits it along word separators and performs substitutions.
851 # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is
852 # evaluated as "\[ab\]". But that's not how TCL normally works; inside a
853 # double-quoted list item, "\[ab\]" is just a long way of representing
854 # "[ab]", because the backslashes will be removed by lindex.
855
856 # Unfortunately, there appears to be no easy way to duplicate the splitting
857 # that expect will do from within TCL. And many places make use of the
858 # "\[0-9\]" construct, so we need to support that; and some places make use
859 # of the "[func]" construct, so we need to support that too. In order to
860 # get this right we have to substitute quoted list elements differently
861 # from braced list elements.
862
863 # We do this roughly the same way that Expect does it. We have to use two
864 # lists, because if we leave unquoted newlines in the argument to uplevel
865 # they'll be treated as command separators, and if we escape newlines
866 # we mangle newlines inside of command blocks. This assumes that the
867 # input doesn't contain a pattern which contains actual embedded newlines
868 # at this point!
869
870 regsub -all {\n} ${user_code} { } subst_code
871 set subst_code [uplevel list $subst_code]
872
873 set processed_code ""
874 set early_processed_code ""
875 # The variable current_list holds the name of the currently processed
876 # list, either processed_code or early_processed_code.
877 set current_list "processed_code"
878 set patterns ""
879 set expecting_action 0
880 set expecting_arg 0
881 set wrap_pattern 0
882 foreach item $user_code subst_item $subst_code {
883 if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } {
884 lappend $current_list $item
885 continue
886 }
887 if { $item == "-indices" || $item == "-re" || $item == "-ex" } {
888 lappend $current_list $item
889 continue
890 }
891 if { $item == "-early" } {
892 set current_list "early_processed_code"
893 continue
894 }
895 if { $item == "-timeout" || $item == "-i" } {
896 set expecting_arg 1
897 lappend $current_list $item
898 continue
899 }
900 if { $item == "-wrap" } {
901 set wrap_pattern 1
902 continue
903 }
904 if { $expecting_arg } {
905 set expecting_arg 0
906 lappend $current_list $subst_item
907 continue
908 }
909 if { $expecting_action } {
910 lappend $current_list "uplevel [list $item]"
911 set expecting_action 0
912 # Cosmetic, no effect on the list.
913 append $current_list "\n"
914 # End the effect of -early, it only applies to one action.
915 set current_list "processed_code"
916 continue
917 }
918 set expecting_action 1
919 if { $wrap_pattern } {
920 # Wrap subst_item as is done for the gdb_test PATTERN argument.
921 lappend $current_list \
922 "\[\r\n\]*(?:$subst_item)\[\r\n\]+$gdb_prompt $"
923 set wrap_pattern 0
924 } else {
925 lappend $current_list $subst_item
926 }
927 if {$patterns != ""} {
928 append patterns "; "
929 }
930 append patterns "\"$subst_item\""
931 }
932
933 # Also purely cosmetic.
934 regsub -all {\r} $patterns {\\r} patterns
935 regsub -all {\n} $patterns {\\n} patterns
936
937 if $verbose>2 then {
938 send_user "Sending \"$command\" to gdb\n"
939 send_user "Looking to match \"$patterns\"\n"
940 send_user "Message is \"$message\"\n"
941 }
942
943 set result -1
944 set string "${command}\n"
945 if { $command != "" } {
946 set multi_line_re "\[\r\n\] *>"
947 while { "$string" != "" } {
948 set foo [string first "\n" "$string"]
949 set len [string length "$string"]
950 if { $foo < [expr $len - 1] } {
951 set str [string range "$string" 0 $foo]
952 if { [send_gdb "$str"] != "" } {
953 global suppress_flag
954
955 if { ! $suppress_flag } {
956 perror "Couldn't send $command to GDB."
957 }
958 fail "$message"
959 return $result
960 }
961 # since we're checking if each line of the multi-line
962 # command are 'accepted' by GDB here,
963 # we need to set -notransfer expect option so that
964 # command output is not lost for pattern matching
965 # - guo
966 gdb_expect 2 {
967 -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 }
968 timeout { verbose "partial: timeout" 3 }
969 }
970 set string [string range "$string" [expr $foo + 1] end]
971 set multi_line_re "$multi_line_re.*\[\r\n\] *>"
972 } else {
973 break
974 }
975 }
976 if { "$string" != "" } {
977 if { [send_gdb "$string"] != "" } {
978 global suppress_flag
979
980 if { ! $suppress_flag } {
981 perror "Couldn't send $command to GDB."
982 }
983 fail "$message"
984 return $result
985 }
986 }
987 }
988
989 set code $early_processed_code
990 append code {
991 -re ".*A problem internal to GDB has been detected" {
992 fail "$message (GDB internal error)"
993 gdb_internal_error_resync
994 set result -1
995 }
996 -re "\\*\\*\\* DOSEXIT code.*" {
997 if { $message != "" } {
998 fail "$message"
999 }
1000 gdb_suppress_entire_file "GDB died"
1001 set result -1
1002 }
1003 }
1004 append code $processed_code
1005
1006 # Reset the spawn id, in case the processed code used -i.
1007 append code {
1008 -i "$gdb_spawn_id"
1009 }
1010
1011 append code {
1012 -re "Ending remote debugging.*$prompt_regexp" {
1013 if ![isnative] then {
1014 warning "Can`t communicate to remote target."
1015 }
1016 gdb_exit
1017 gdb_start
1018 set result -1
1019 }
1020 -re "Undefined\[a-z\]* command:.*$prompt_regexp" {
1021 perror "Undefined command \"$command\"."
1022 fail "$message"
1023 set result 1
1024 }
1025 -re "Ambiguous command.*$prompt_regexp" {
1026 perror "\"$command\" is not a unique command name."
1027 fail "$message"
1028 set result 1
1029 }
1030 -re "$inferior_exited_re with code \[0-9\]+.*$prompt_regexp" {
1031 if ![string match "" $message] then {
1032 set errmsg "$message (the program exited)"
1033 } else {
1034 set errmsg "$command (the program exited)"
1035 }
1036 fail "$errmsg"
1037 set result -1
1038 }
1039 -re "$inferior_exited_re normally.*$prompt_regexp" {
1040 if ![string match "" $message] then {
1041 set errmsg "$message (the program exited)"
1042 } else {
1043 set errmsg "$command (the program exited)"
1044 }
1045 fail "$errmsg"
1046 set result -1
1047 }
1048 -re "The program is not being run.*$prompt_regexp" {
1049 if ![string match "" $message] then {
1050 set errmsg "$message (the program is no longer running)"
1051 } else {
1052 set errmsg "$command (the program is no longer running)"
1053 }
1054 fail "$errmsg"
1055 set result -1
1056 }
1057 -re "\r\n$prompt_regexp" {
1058 if ![string match "" $message] then {
1059 fail "$message"
1060 }
1061 set result 1
1062 }
1063 -re "$pagination_prompt" {
1064 send_gdb "\n"
1065 perror "Window too small."
1066 fail "$message"
1067 set result -1
1068 }
1069 -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " {
1070 send_gdb "n\n" answer
1071 gdb_expect -re "$prompt_regexp"
1072 fail "$message (got interactive prompt)"
1073 set result -1
1074 }
1075 -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" {
1076 send_gdb "0\n"
1077 gdb_expect -re "$prompt_regexp"
1078 fail "$message (got breakpoint menu)"
1079 set result -1
1080 }
1081
1082 -i $gdb_spawn_id
1083 eof {
1084 perror "GDB process no longer exists"
1085 set wait_status [wait -i $gdb_spawn_id]
1086 verbose -log "GDB process exited with wait status $wait_status"
1087 if { $message != "" } {
1088 fail "$message"
1089 }
1090 return -1
1091 }
1092 }
1093
1094 if {$line_by_line} {
1095 append code {
1096 -re "\r\n\[^\r\n\]*(?=\r\n)" {
1097 exp_continue
1098 }
1099 }
1100 }
1101
1102 # Now patterns that apply to any spawn id specified.
1103 append code {
1104 -i $any_spawn_id
1105 eof {
1106 perror "Process no longer exists"
1107 if { $message != "" } {
1108 fail "$message"
1109 }
1110 return -1
1111 }
1112 full_buffer {
1113 perror "internal buffer is full."
1114 fail "$message"
1115 set result -1
1116 }
1117 timeout {
1118 if ![string match "" $message] then {
1119 fail "$message (timeout)"
1120 }
1121 set result 1
1122 }
1123 }
1124
1125 # remote_expect calls the eof section if there is an error on the
1126 # expect call. We already have eof sections above, and we don't
1127 # want them to get called in that situation. Since the last eof
1128 # section becomes the error section, here we define another eof
1129 # section, but with an empty spawn_id list, so that it won't ever
1130 # match.
1131 append code {
1132 -i "" eof {
1133 # This comment is here because the eof section must not be
1134 # the empty string, otherwise remote_expect won't realize
1135 # it exists.
1136 }
1137 }
1138
1139 # Create gdb_test_name in the parent scope. If this variable
1140 # already exists, which it might if we have nested calls to
1141 # gdb_test_multiple, then preserve the old value, otherwise,
1142 # create a new variable in the parent scope.
1143 upvar gdb_test_name gdb_test_name
1144 if { [info exists gdb_test_name] } {
1145 set gdb_test_name_old "$gdb_test_name"
1146 }
1147 set gdb_test_name "$message"
1148
1149 set result 0
1150 set code [catch {gdb_expect $code} string]
1151
1152 # Clean up the gdb_test_name variable. If we had a
1153 # previous value then restore it, otherwise, delete the variable
1154 # from the parent scope.
1155 if { [info exists gdb_test_name_old] } {
1156 set gdb_test_name "$gdb_test_name_old"
1157 } else {
1158 unset gdb_test_name
1159 }
1160
1161 if {$code == 1} {
1162 global errorInfo errorCode
1163 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
1164 } elseif {$code > 1} {
1165 return -code $code $string
1166 }
1167 return $result
1168 }
1169
1170 # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE
1171 # Send a command to gdb; test the result.
1172 #
1173 # COMMAND is the command to execute, send to GDB with send_gdb. If
1174 # this is the null string no command is sent.
1175 # PATTERN is the pattern to match for a PASS, and must NOT include
1176 # the \r\n sequence immediately before the gdb prompt. This argument
1177 # may be omitted to just match the prompt, ignoring whatever output
1178 # precedes it.
1179 # MESSAGE is an optional message to be printed. If this is
1180 # omitted, then the pass/fail messages use the command string as the
1181 # message. (If this is the empty string, then sometimes we don't
1182 # call pass or fail at all; I don't understand this at all.)
1183 # QUESTION is a question GDB may ask in response to COMMAND, like
1184 # "are you sure?"
1185 # RESPONSE is the response to send if QUESTION appears.
1186 #
1187 # Returns:
1188 # 1 if the test failed,
1189 # 0 if the test passes,
1190 # -1 if there was an internal error.
1191 #
1192 proc gdb_test { args } {
1193 global gdb_prompt
1194 upvar timeout timeout
1195
1196 if [llength $args]>2 then {
1197 set message [lindex $args 2]
1198 } else {
1199 set message [lindex $args 0]
1200 }
1201 set command [lindex $args 0]
1202 set pattern [lindex $args 1]
1203
1204 set user_code {}
1205 lappend user_code {
1206 -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$gdb_prompt $" {
1207 if ![string match "" $message] then {
1208 pass "$message"
1209 }
1210 }
1211 }
1212
1213 if { [llength $args] == 5 } {
1214 set question_string [lindex $args 3]
1215 set response_string [lindex $args 4]
1216 lappend user_code {
1217 -re "(${question_string})$" {
1218 send_gdb "$response_string\n"
1219 exp_continue
1220 }
1221 }
1222 }
1223
1224 set user_code [join $user_code]
1225 return [gdb_test_multiple $command $message $user_code]
1226 }
1227
1228 # Return 1 if version MAJOR.MINOR is at least AT_LEAST_MAJOR.AT_LEAST_MINOR.
1229 proc version_at_least { major minor at_least_major at_least_minor} {
1230 if { $major > $at_least_major } {
1231 return 1
1232 } elseif { $major == $at_least_major \
1233 && $minor >= $at_least_minor } {
1234 return 1
1235 } else {
1236 return 0
1237 }
1238 }
1239
1240 # Return 1 if tcl version used is at least MAJOR.MINOR
1241 proc tcl_version_at_least { major minor } {
1242 global tcl_version
1243 regexp {^([0-9]+)\.([0-9]+)$} $tcl_version \
1244 dummy tcl_version_major tcl_version_minor
1245 return [version_at_least $tcl_version_major $tcl_version_minor \
1246 $major $minor]
1247 }
1248
1249 if { [tcl_version_at_least 8 5] == 0 } {
1250 # lrepeat was added in tcl 8.5. Only add if missing.
1251 proc lrepeat { n element } {
1252 if { [string is integer -strict $n] == 0 } {
1253 error "expected integer but got \"$n\""
1254 }
1255 if { $n < 0 } {
1256 error "bad count \"$n\": must be integer >= 0"
1257 }
1258 set res [list]
1259 for {set i 0} {$i < $n} {incr i} {
1260 lappend res $element
1261 }
1262 return $res
1263 }
1264 }
1265
1266 # gdb_test_no_output COMMAND MESSAGE
1267 # Send a command to GDB and verify that this command generated no output.
1268 #
1269 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1270 # parameters. If MESSAGE is ommitted, then COMMAND will be used as
1271 # the message. (If MESSAGE is the empty string, then sometimes we do not
1272 # call pass or fail at all; I don't understand this at all.)
1273
1274 proc gdb_test_no_output { args } {
1275 global gdb_prompt
1276 set command [lindex $args 0]
1277 if [llength $args]>1 then {
1278 set message [lindex $args 1]
1279 } else {
1280 set message $command
1281 }
1282
1283 set command_regex [string_to_regexp $command]
1284 gdb_test_multiple $command $message {
1285 -re "^$command_regex\r\n$gdb_prompt $" {
1286 if ![string match "" $message] then {
1287 pass "$message"
1288 }
1289 }
1290 }
1291 }
1292
1293 # Send a command and then wait for a sequence of outputs.
1294 # This is useful when the sequence is long and contains ".*", a single
1295 # regexp to match the entire output can get a timeout much easier.
1296 #
1297 # COMMAND is the command to execute, send to GDB with send_gdb. If
1298 # this is the null string no command is sent.
1299 # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "".
1300 # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are
1301 # processed in order, and all must be present in the output.
1302 #
1303 # It is unnecessary to specify ".*" at the beginning or end of any regexp,
1304 # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST.
1305 # There is also an implicit ".*" between the last regexp and the gdb prompt.
1306 #
1307 # Like gdb_test and gdb_test_multiple, the output is expected to end with the
1308 # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST.
1309 #
1310 # Returns:
1311 # 1 if the test failed,
1312 # 0 if the test passes,
1313 # -1 if there was an internal error.
1314
1315 proc gdb_test_sequence { command test_name expected_output_list } {
1316 global gdb_prompt
1317 if { $test_name == "" } {
1318 set test_name $command
1319 }
1320 lappend expected_output_list ""; # implicit ".*" before gdb prompt
1321 if { $command != "" } {
1322 send_gdb "$command\n"
1323 }
1324 return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list]
1325 }
1326
1327 \f
1328 # Test that a command gives an error. For pass or fail, return
1329 # a 1 to indicate that more tests can proceed. However a timeout
1330 # is a serious error, generates a special fail message, and causes
1331 # a 0 to be returned to indicate that more tests are likely to fail
1332 # as well.
1333
1334 proc test_print_reject { args } {
1335 global gdb_prompt
1336 global verbose
1337
1338 if [llength $args]==2 then {
1339 set expectthis [lindex $args 1]
1340 } else {
1341 set expectthis "should never match this bogus string"
1342 }
1343 set sendthis [lindex $args 0]
1344 if $verbose>2 then {
1345 send_user "Sending \"$sendthis\" to gdb\n"
1346 send_user "Looking to match \"$expectthis\"\n"
1347 }
1348 send_gdb "$sendthis\n"
1349 #FIXME: Should add timeout as parameter.
1350 gdb_expect {
1351 -re "A .* in expression.*\\.*$gdb_prompt $" {
1352 pass "reject $sendthis"
1353 return 1
1354 }
1355 -re "Invalid syntax in expression.*$gdb_prompt $" {
1356 pass "reject $sendthis"
1357 return 1
1358 }
1359 -re "Junk after end of expression.*$gdb_prompt $" {
1360 pass "reject $sendthis"
1361 return 1
1362 }
1363 -re "Invalid number.*$gdb_prompt $" {
1364 pass "reject $sendthis"
1365 return 1
1366 }
1367 -re "Invalid character constant.*$gdb_prompt $" {
1368 pass "reject $sendthis"
1369 return 1
1370 }
1371 -re "No symbol table is loaded.*$gdb_prompt $" {
1372 pass "reject $sendthis"
1373 return 1
1374 }
1375 -re "No symbol .* in current context.*$gdb_prompt $" {
1376 pass "reject $sendthis"
1377 return 1
1378 }
1379 -re "Unmatched single quote.*$gdb_prompt $" {
1380 pass "reject $sendthis"
1381 return 1
1382 }
1383 -re "A character constant must contain at least one character.*$gdb_prompt $" {
1384 pass "reject $sendthis"
1385 return 1
1386 }
1387 -re "$expectthis.*$gdb_prompt $" {
1388 pass "reject $sendthis"
1389 return 1
1390 }
1391 -re ".*$gdb_prompt $" {
1392 fail "reject $sendthis"
1393 return 1
1394 }
1395 default {
1396 fail "reject $sendthis (eof or timeout)"
1397 return 0
1398 }
1399 }
1400 }
1401 \f
1402
1403 # Same as gdb_test, but the second parameter is not a regexp,
1404 # but a string that must match exactly.
1405
1406 proc gdb_test_exact { args } {
1407 upvar timeout timeout
1408
1409 set command [lindex $args 0]
1410
1411 # This applies a special meaning to a null string pattern. Without
1412 # this, "$pattern\r\n$gdb_prompt $" will match anything, including error
1413 # messages from commands that should have no output except a new
1414 # prompt. With this, only results of a null string will match a null
1415 # string pattern.
1416
1417 set pattern [lindex $args 1]
1418 if [string match $pattern ""] {
1419 set pattern [string_to_regexp [lindex $args 0]]
1420 } else {
1421 set pattern [string_to_regexp [lindex $args 1]]
1422 }
1423
1424 # It is most natural to write the pattern argument with only
1425 # embedded \n's, especially if you are trying to avoid Tcl quoting
1426 # problems. But gdb_expect really wants to see \r\n in patterns. So
1427 # transform the pattern here. First transform \r\n back to \n, in
1428 # case some users of gdb_test_exact already do the right thing.
1429 regsub -all "\r\n" $pattern "\n" pattern
1430 regsub -all "\n" $pattern "\r\n" pattern
1431 if [llength $args]==3 then {
1432 set message [lindex $args 2]
1433 return [gdb_test $command $pattern $message]
1434 }
1435
1436 return [gdb_test $command $pattern]
1437 }
1438
1439 # Wrapper around gdb_test_multiple that looks for a list of expected
1440 # output elements, but which can appear in any order.
1441 # CMD is the gdb command.
1442 # NAME is the name of the test.
1443 # ELM_FIND_REGEXP specifies how to partition the output into elements to
1444 # compare.
1445 # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare.
1446 # RESULT_MATCH_LIST is a list of exact matches for each expected element.
1447 # All elements of RESULT_MATCH_LIST must appear for the test to pass.
1448 #
1449 # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line
1450 # of text per element and then strip trailing \r\n's.
1451 # Example:
1452 # gdb_test_list_exact "foo" "bar" \
1453 # "\[^\r\n\]+\[\r\n\]+" \
1454 # "\[^\r\n\]+" \
1455 # { \
1456 # {expected result 1} \
1457 # {expected result 2} \
1458 # }
1459
1460 proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } {
1461 global gdb_prompt
1462
1463 set matches [lsort $result_match_list]
1464 set seen {}
1465 gdb_test_multiple $cmd $name {
1466 "$cmd\[\r\n\]" { exp_continue }
1467 -re $elm_find_regexp {
1468 set str $expect_out(0,string)
1469 verbose -log "seen: $str" 3
1470 regexp -- $elm_extract_regexp $str elm_seen
1471 verbose -log "extracted: $elm_seen" 3
1472 lappend seen $elm_seen
1473 exp_continue
1474 }
1475 -re "$gdb_prompt $" {
1476 set failed ""
1477 foreach got [lsort $seen] have $matches {
1478 if {![string equal $got $have]} {
1479 set failed $have
1480 break
1481 }
1482 }
1483 if {[string length $failed] != 0} {
1484 fail "$name ($failed not found)"
1485 } else {
1486 pass $name
1487 }
1488 }
1489 }
1490 }
1491
1492 # gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE
1493 # Send a command to gdb; expect inferior and gdb output.
1494 #
1495 # See gdb_test_multiple for a description of the COMMAND and MESSAGE
1496 # parameters.
1497 #
1498 # INFERIOR_PATTERN is the pattern to match against inferior output.
1499 #
1500 # GDB_PATTERN is the pattern to match against gdb output, and must NOT
1501 # include the \r\n sequence immediately before the gdb prompt, nor the
1502 # prompt. The default is empty.
1503 #
1504 # Both inferior and gdb patterns must match for a PASS.
1505 #
1506 # If MESSAGE is ommitted, then COMMAND will be used as the message.
1507 #
1508 # Returns:
1509 # 1 if the test failed,
1510 # 0 if the test passes,
1511 # -1 if there was an internal error.
1512 #
1513
1514 proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} {
1515 global inferior_spawn_id gdb_spawn_id
1516 global gdb_prompt
1517
1518 if {$message == ""} {
1519 set message $command
1520 }
1521
1522 set inferior_matched 0
1523 set gdb_matched 0
1524
1525 # Use an indirect spawn id list, and remove the inferior spawn id
1526 # from the expected output as soon as it matches, in case
1527 # $inferior_pattern happens to be a prefix of the resulting full
1528 # gdb pattern below (e.g., "\r\n").
1529 global gdb_test_stdio_spawn_id_list
1530 set gdb_test_stdio_spawn_id_list "$inferior_spawn_id"
1531
1532 # Note that if $inferior_spawn_id and $gdb_spawn_id are different,
1533 # then we may see gdb's output arriving before the inferior's
1534 # output.
1535 set res [gdb_test_multiple $command $message {
1536 -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" {
1537 set inferior_matched 1
1538 if {!$gdb_matched} {
1539 set gdb_test_stdio_spawn_id_list ""
1540 exp_continue
1541 }
1542 }
1543 -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" {
1544 set gdb_matched 1
1545 if {!$inferior_matched} {
1546 exp_continue
1547 }
1548 }
1549 }]
1550 if {$res == 0} {
1551 pass $message
1552 } else {
1553 verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched"
1554 }
1555 return $res
1556 }
1557
1558 # get_print_expr_at_depths EXP OUTPUTS
1559 #
1560 # Used for testing 'set print max-depth'. Prints the expression EXP
1561 # with 'set print max-depth' set to various depths. OUTPUTS is a list
1562 # of `n` different patterns to match at each of the depths from 0 to
1563 # (`n` - 1).
1564 #
1565 # This proc does one final check with the max-depth set to 'unlimited'
1566 # which is tested against the last pattern in the OUTPUTS list. The
1567 # OUTPUTS list is therefore required to match every depth from 0 to a
1568 # depth where the whole of EXP is printed with no ellipsis.
1569 #
1570 # This proc leaves the 'set print max-depth' set to 'unlimited'.
1571 proc gdb_print_expr_at_depths {exp outputs} {
1572 for { set depth 0 } { $depth <= [llength $outputs] } { incr depth } {
1573 if { $depth == [llength $outputs] } {
1574 set expected_result [lindex $outputs [expr [llength $outputs] - 1]]
1575 set depth_string "unlimited"
1576 } else {
1577 set expected_result [lindex $outputs $depth]
1578 set depth_string $depth
1579 }
1580
1581 with_test_prefix "exp='$exp': depth=${depth_string}" {
1582 gdb_test_no_output "set print max-depth ${depth_string}"
1583 gdb_test "p $exp" "$expected_result"
1584 }
1585 }
1586 }
1587
1588 \f
1589
1590 # Issue a PASS and return true if evaluating CONDITION in the caller's
1591 # frame returns true, and issue a FAIL and return false otherwise.
1592 # MESSAGE is the pass/fail message to be printed. If MESSAGE is
1593 # omitted or is empty, then the pass/fail messages use the condition
1594 # string as the message.
1595
1596 proc gdb_assert { condition {message ""} } {
1597 if { $message == ""} {
1598 set message $condition
1599 }
1600
1601 set res [uplevel 1 expr $condition]
1602 if {!$res} {
1603 fail $message
1604 } else {
1605 pass $message
1606 }
1607 return $res
1608 }
1609
1610 proc gdb_reinitialize_dir { subdir } {
1611 global gdb_prompt
1612
1613 if [is_remote host] {
1614 return ""
1615 }
1616 send_gdb "dir\n"
1617 gdb_expect 60 {
1618 -re "Reinitialize source path to empty.*y or n. " {
1619 send_gdb "y\n" answer
1620 gdb_expect 60 {
1621 -re "Source directories searched.*$gdb_prompt $" {
1622 send_gdb "dir $subdir\n"
1623 gdb_expect 60 {
1624 -re "Source directories searched.*$gdb_prompt $" {
1625 verbose "Dir set to $subdir"
1626 }
1627 -re "$gdb_prompt $" {
1628 perror "Dir \"$subdir\" failed."
1629 }
1630 }
1631 }
1632 -re "$gdb_prompt $" {
1633 perror "Dir \"$subdir\" failed."
1634 }
1635 }
1636 }
1637 -re "$gdb_prompt $" {
1638 perror "Dir \"$subdir\" failed."
1639 }
1640 }
1641 }
1642
1643 #
1644 # gdb_exit -- exit the GDB, killing the target program if necessary
1645 #
1646 proc default_gdb_exit {} {
1647 global GDB
1648 global INTERNAL_GDBFLAGS GDBFLAGS
1649 global gdb_spawn_id inferior_spawn_id
1650 global inotify_log_file
1651
1652 gdb_stop_suppressing_tests
1653
1654 if ![info exists gdb_spawn_id] {
1655 return
1656 }
1657
1658 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1659
1660 if {[info exists inotify_log_file] && [file exists $inotify_log_file]} {
1661 set fd [open $inotify_log_file]
1662 set data [read -nonewline $fd]
1663 close $fd
1664
1665 if {[string compare $data ""] != 0} {
1666 warning "parallel-unsafe file creations noticed"
1667
1668 # Clear the log.
1669 set fd [open $inotify_log_file w]
1670 close $fd
1671 }
1672 }
1673
1674 if { [is_remote host] && [board_info host exists fileid] } {
1675 send_gdb "quit\n"
1676 gdb_expect 10 {
1677 -re "y or n" {
1678 send_gdb "y\n" answer
1679 exp_continue
1680 }
1681 -re "DOSEXIT code" { }
1682 default { }
1683 }
1684 }
1685
1686 if ![is_remote host] {
1687 remote_close host
1688 }
1689 unset gdb_spawn_id
1690 unset inferior_spawn_id
1691 }
1692
1693 # Load a file into the debugger.
1694 # The return value is 0 for success, -1 for failure.
1695 #
1696 # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO
1697 # to one of these values:
1698 #
1699 # debug file was loaded successfully and has debug information
1700 # nodebug file was loaded successfully and has no debug information
1701 # lzma file was loaded, .gnu_debugdata found, but no LZMA support
1702 # compiled in
1703 # fail file was not loaded
1704 #
1705 # I tried returning this information as part of the return value,
1706 # but ran into a mess because of the many re-implementations of
1707 # gdb_load in config/*.exp.
1708 #
1709 # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use
1710 # this if they can get more information set.
1711
1712 proc gdb_file_cmd { arg } {
1713 global gdb_prompt
1714 global GDB
1715 global last_loaded_file
1716
1717 # Save this for the benefit of gdbserver-support.exp.
1718 set last_loaded_file $arg
1719
1720 # Set whether debug info was found.
1721 # Default to "fail".
1722 global gdb_file_cmd_debug_info
1723 set gdb_file_cmd_debug_info "fail"
1724
1725 if [is_remote host] {
1726 set arg [remote_download host $arg]
1727 if { $arg == "" } {
1728 perror "download failed"
1729 return -1
1730 }
1731 }
1732
1733 # The file command used to kill the remote target. For the benefit
1734 # of the testsuite, preserve this behavior. Mark as optional so it doesn't
1735 # get written to the stdin log.
1736 send_gdb "kill\n" optional
1737 gdb_expect 120 {
1738 -re "Kill the program being debugged. .y or n. $" {
1739 send_gdb "y\n" answer
1740 verbose "\t\tKilling previous program being debugged"
1741 exp_continue
1742 }
1743 -re "$gdb_prompt $" {
1744 # OK.
1745 }
1746 }
1747
1748 send_gdb "file $arg\n"
1749 gdb_expect 120 {
1750 -re "Reading symbols from.*LZMA support was disabled.*$gdb_prompt $" {
1751 verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available"
1752 set gdb_file_cmd_debug_info "lzma"
1753 return 0
1754 }
1755 -re "Reading symbols from.*no debugging symbols found.*$gdb_prompt $" {
1756 verbose "\t\tLoaded $arg into $GDB with no debugging symbols"
1757 set gdb_file_cmd_debug_info "nodebug"
1758 return 0
1759 }
1760 -re "Reading symbols from.*$gdb_prompt $" {
1761 verbose "\t\tLoaded $arg into $GDB"
1762 set gdb_file_cmd_debug_info "debug"
1763 return 0
1764 }
1765 -re "Load new symbol table from \".*\".*y or n. $" {
1766 send_gdb "y\n" answer
1767 gdb_expect 120 {
1768 -re "Reading symbols from.*$gdb_prompt $" {
1769 verbose "\t\tLoaded $arg with new symbol table into $GDB"
1770 set gdb_file_cmd_debug_info "debug"
1771 return 0
1772 }
1773 timeout {
1774 perror "Couldn't load $arg, other program already loaded (timeout)."
1775 return -1
1776 }
1777 eof {
1778 perror "Couldn't load $arg, other program already loaded (eof)."
1779 return -1
1780 }
1781 }
1782 }
1783 -re "No such file or directory.*$gdb_prompt $" {
1784 perror "($arg) No such file or directory"
1785 return -1
1786 }
1787 -re "A problem internal to GDB has been detected" {
1788 fail "($arg) (GDB internal error)"
1789 gdb_internal_error_resync
1790 return -1
1791 }
1792 -re "$gdb_prompt $" {
1793 perror "Couldn't load $arg into $GDB."
1794 return -1
1795 }
1796 timeout {
1797 perror "Couldn't load $arg into $GDB (timeout)."
1798 return -1
1799 }
1800 eof {
1801 # This is an attempt to detect a core dump, but seems not to
1802 # work. Perhaps we need to match .* followed by eof, in which
1803 # gdb_expect does not seem to have a way to do that.
1804 perror "Couldn't load $arg into $GDB (eof)."
1805 return -1
1806 }
1807 }
1808 }
1809
1810 # Default gdb_spawn procedure.
1811
1812 proc default_gdb_spawn { } {
1813 global use_gdb_stub
1814 global GDB
1815 global INTERNAL_GDBFLAGS GDBFLAGS
1816 global gdb_spawn_id
1817
1818 gdb_stop_suppressing_tests
1819
1820 # Set the default value, it may be overriden later by specific testfile.
1821 #
1822 # Use `set_board_info use_gdb_stub' for the board file to flag the inferior
1823 # is already started after connecting and run/attach are not supported.
1824 # This is used for the "remote" protocol. After GDB starts you should
1825 # check global $use_gdb_stub instead of the board as the testfile may force
1826 # a specific different target protocol itself.
1827 set use_gdb_stub [target_info exists use_gdb_stub]
1828
1829 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1830 gdb_write_cmd_file "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS"
1831
1832 if [info exists gdb_spawn_id] {
1833 return 0
1834 }
1835
1836 if ![is_remote host] {
1837 if { [which $GDB] == 0 } then {
1838 perror "$GDB does not exist."
1839 exit 1
1840 }
1841 }
1842 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"]
1843 if { $res < 0 || $res == "" } {
1844 perror "Spawning $GDB failed."
1845 return 1
1846 }
1847
1848 set gdb_spawn_id $res
1849 return 0
1850 }
1851
1852 # Default gdb_start procedure.
1853
1854 proc default_gdb_start { } {
1855 global gdb_prompt
1856 global gdb_spawn_id
1857 global inferior_spawn_id
1858
1859 if [info exists gdb_spawn_id] {
1860 return 0
1861 }
1862
1863 # Keep track of the number of times GDB has been launched.
1864 global gdb_instances
1865 incr gdb_instances
1866
1867 gdb_stdin_log_init
1868
1869 set res [gdb_spawn]
1870 if { $res != 0} {
1871 return $res
1872 }
1873
1874 # Default to assuming inferior I/O is done on GDB's terminal.
1875 if {![info exists inferior_spawn_id]} {
1876 set inferior_spawn_id $gdb_spawn_id
1877 }
1878
1879 # When running over NFS, particularly if running many simultaneous
1880 # tests on different hosts all using the same server, things can
1881 # get really slow. Give gdb at least 3 minutes to start up.
1882 gdb_expect 360 {
1883 -re "\[\r\n\]$gdb_prompt $" {
1884 verbose "GDB initialized."
1885 }
1886 -re "$gdb_prompt $" {
1887 perror "GDB never initialized."
1888 unset gdb_spawn_id
1889 return -1
1890 }
1891 timeout {
1892 perror "(timeout) GDB never initialized after 10 seconds."
1893 remote_close host
1894 unset gdb_spawn_id
1895 return -1
1896 }
1897 eof {
1898 perror "(eof) GDB never initialized."
1899 unset gdb_spawn_id
1900 return -1
1901 }
1902 }
1903
1904 # force the height to "unlimited", so no pagers get used
1905
1906 send_gdb "set height 0\n"
1907 gdb_expect 10 {
1908 -re "$gdb_prompt $" {
1909 verbose "Setting height to 0." 2
1910 }
1911 timeout {
1912 warning "Couldn't set the height to 0"
1913 }
1914 }
1915 # force the width to "unlimited", so no wraparound occurs
1916 send_gdb "set width 0\n"
1917 gdb_expect 10 {
1918 -re "$gdb_prompt $" {
1919 verbose "Setting width to 0." 2
1920 }
1921 timeout {
1922 warning "Couldn't set the width to 0."
1923 }
1924 }
1925
1926 gdb_debug_init
1927 return 0
1928 }
1929
1930 # Utility procedure to give user control of the gdb prompt in a script. It is
1931 # meant to be used for debugging test cases, and should not be left in the
1932 # test cases code.
1933
1934 proc gdb_interact { } {
1935 global gdb_spawn_id
1936 set spawn_id $gdb_spawn_id
1937
1938 send_user "+------------------------------------------+\n"
1939 send_user "| Script interrupted, you can now interact |\n"
1940 send_user "| with by gdb. Type >>> to continue. |\n"
1941 send_user "+------------------------------------------+\n"
1942
1943 interact {
1944 ">>>" return
1945 }
1946 }
1947
1948 # Examine the output of compilation to determine whether compilation
1949 # failed or not. If it failed determine whether it is due to missing
1950 # compiler or due to compiler error. Report pass, fail or unsupported
1951 # as appropriate
1952
1953 proc gdb_compile_test {src output} {
1954 if { $output == "" } {
1955 pass "compilation [file tail $src]"
1956 } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } {
1957 unsupported "compilation [file tail $src]"
1958 } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } {
1959 unsupported "compilation [file tail $src]"
1960 } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } {
1961 unsupported "compilation [file tail $src]"
1962 } else {
1963 verbose -log "compilation failed: $output" 2
1964 fail "compilation [file tail $src]"
1965 }
1966 }
1967
1968 # Return a 1 for configurations for which we don't even want to try to
1969 # test C++.
1970
1971 proc skip_cplus_tests {} {
1972 if { [istarget "h8300-*-*"] } {
1973 return 1
1974 }
1975
1976 # The C++ IO streams are too large for HC11/HC12 and are thus not
1977 # available. The gdb C++ tests use them and don't compile.
1978 if { [istarget "m6811-*-*"] } {
1979 return 1
1980 }
1981 if { [istarget "m6812-*-*"] } {
1982 return 1
1983 }
1984 return 0
1985 }
1986
1987 # Return a 1 for configurations for which don't have both C++ and the STL.
1988
1989 proc skip_stl_tests {} {
1990 # Symbian supports the C++ language, but the STL is missing
1991 # (both headers and libraries).
1992 if { [istarget "arm*-*-symbianelf*"] } {
1993 return 1
1994 }
1995
1996 return [skip_cplus_tests]
1997 }
1998
1999 # Return a 1 if I don't even want to try to test FORTRAN.
2000
2001 proc skip_fortran_tests {} {
2002 return 0
2003 }
2004
2005 # Return a 1 if I don't even want to try to test ada.
2006
2007 proc skip_ada_tests {} {
2008 return 0
2009 }
2010
2011 # Return a 1 if I don't even want to try to test GO.
2012
2013 proc skip_go_tests {} {
2014 return 0
2015 }
2016
2017 # Return a 1 if I don't even want to try to test D.
2018
2019 proc skip_d_tests {} {
2020 return 0
2021 }
2022
2023 # Return 1 to skip Rust tests, 0 to try them.
2024 proc skip_rust_tests {} {
2025 return [expr {![isnative]}]
2026 }
2027
2028 # Return a 1 for configurations that do not support Python scripting.
2029 # PROMPT_REGEXP is the expected prompt.
2030
2031 proc skip_python_tests_prompt { prompt_regexp } {
2032 global gdb_py_is_py3k
2033
2034 gdb_test_multiple "python print ('test')" "verify python support" \
2035 -prompt "$prompt_regexp" {
2036 -re "not supported.*$prompt_regexp" {
2037 unsupported "Python support is disabled."
2038 return 1
2039 }
2040 -re "$prompt_regexp" {}
2041 }
2042
2043 gdb_test_multiple "python print (sys.version_info\[0\])" "check if python 3" \
2044 -prompt "$prompt_regexp" {
2045 -re "3.*$prompt_regexp" {
2046 set gdb_py_is_py3k 1
2047 }
2048 -re ".*$prompt_regexp" {
2049 set gdb_py_is_py3k 0
2050 }
2051 }
2052
2053 return 0
2054 }
2055
2056 # Return a 1 for configurations that do not support Python scripting.
2057 # Note: This also sets various globals that specify which version of Python
2058 # is in use. See skip_python_tests_prompt.
2059
2060 proc skip_python_tests {} {
2061 global gdb_prompt
2062 return [skip_python_tests_prompt "$gdb_prompt $"]
2063 }
2064
2065 # Return a 1 if we should skip shared library tests.
2066
2067 proc skip_shlib_tests {} {
2068 # Run the shared library tests on native systems.
2069 if {[isnative]} {
2070 return 0
2071 }
2072
2073 # An abbreviated list of remote targets where we should be able to
2074 # run shared library tests.
2075 if {([istarget *-*-linux*]
2076 || [istarget *-*-*bsd*]
2077 || [istarget *-*-solaris2*]
2078 || [istarget arm*-*-symbianelf*]
2079 || [istarget *-*-mingw*]
2080 || [istarget *-*-cygwin*]
2081 || [istarget *-*-pe*])} {
2082 return 0
2083 }
2084
2085 return 1
2086 }
2087
2088 # Return 1 if we should skip tui related tests.
2089
2090 proc skip_tui_tests {} {
2091 global gdb_prompt
2092
2093 gdb_test_multiple "help layout" "verify tui support" {
2094 -re "Undefined command: \"layout\".*$gdb_prompt $" {
2095 return 1
2096 }
2097 -re "$gdb_prompt $" {
2098 }
2099 }
2100
2101 return 0
2102 }
2103
2104 # Test files shall make sure all the test result lines in gdb.sum are
2105 # unique in a test run, so that comparing the gdb.sum files of two
2106 # test runs gives correct results. Test files that exercise
2107 # variations of the same tests more than once, shall prefix the
2108 # different test invocations with different identifying strings in
2109 # order to make them unique.
2110 #
2111 # About test prefixes:
2112 #
2113 # $pf_prefix is the string that dejagnu prints after the result (FAIL,
2114 # PASS, etc.), and before the test message/name in gdb.sum. E.g., the
2115 # underlined substring in
2116 #
2117 # PASS: gdb.base/mytest.exp: some test
2118 # ^^^^^^^^^^^^^^^^^^^^
2119 #
2120 # is $pf_prefix.
2121 #
2122 # The easiest way to adjust the test prefix is to append a test
2123 # variation prefix to the $pf_prefix, using the with_test_prefix
2124 # procedure. E.g.,
2125 #
2126 # proc do_tests {} {
2127 # gdb_test ... ... "test foo"
2128 # gdb_test ... ... "test bar"
2129 #
2130 # with_test_prefix "subvariation a" {
2131 # gdb_test ... ... "test x"
2132 # }
2133 #
2134 # with_test_prefix "subvariation b" {
2135 # gdb_test ... ... "test x"
2136 # }
2137 # }
2138 #
2139 # with_test_prefix "variation1" {
2140 # ...do setup for variation 1...
2141 # do_tests
2142 # }
2143 #
2144 # with_test_prefix "variation2" {
2145 # ...do setup for variation 2...
2146 # do_tests
2147 # }
2148 #
2149 # Results in:
2150 #
2151 # PASS: gdb.base/mytest.exp: variation1: test foo
2152 # PASS: gdb.base/mytest.exp: variation1: test bar
2153 # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x
2154 # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x
2155 # PASS: gdb.base/mytest.exp: variation2: test foo
2156 # PASS: gdb.base/mytest.exp: variation2: test bar
2157 # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x
2158 # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x
2159 #
2160 # If for some reason more flexibility is necessary, one can also
2161 # manipulate the pf_prefix global directly, treating it as a string.
2162 # E.g.,
2163 #
2164 # global pf_prefix
2165 # set saved_pf_prefix
2166 # append pf_prefix "${foo}: bar"
2167 # ... actual tests ...
2168 # set pf_prefix $saved_pf_prefix
2169 #
2170
2171 # Run BODY in the context of the caller, with the current test prefix
2172 # (pf_prefix) appended with one space, then PREFIX, and then a colon.
2173 # Returns the result of BODY.
2174 #
2175 proc with_test_prefix { prefix body } {
2176 global pf_prefix
2177
2178 set saved $pf_prefix
2179 append pf_prefix " " $prefix ":"
2180 set code [catch {uplevel 1 $body} result]
2181 set pf_prefix $saved
2182
2183 if {$code == 1} {
2184 global errorInfo errorCode
2185 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2186 } else {
2187 return -code $code $result
2188 }
2189 }
2190
2191 # Wrapper for foreach that calls with_test_prefix on each iteration,
2192 # including the iterator's name and current value in the prefix.
2193
2194 proc foreach_with_prefix {var list body} {
2195 upvar 1 $var myvar
2196 foreach myvar $list {
2197 with_test_prefix "$var=$myvar" {
2198 set code [catch {uplevel 1 $body} result]
2199 }
2200
2201 if {$code == 1} {
2202 global errorInfo errorCode
2203 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2204 } elseif {$code == 3} {
2205 break
2206 } elseif {$code == 2} {
2207 return -code $code $result
2208 }
2209 }
2210 }
2211
2212 # Like TCL's native proc, but defines a procedure that wraps its body
2213 # within 'with_test_prefix "$proc_name" { ... }'.
2214 proc proc_with_prefix {name arguments body} {
2215 # Define the advertised proc.
2216 proc $name $arguments [list with_test_prefix $name $body]
2217 }
2218
2219
2220 # Run BODY in the context of the caller. After BODY is run, the variables
2221 # listed in VARS will be reset to the values they had before BODY was run.
2222 #
2223 # This is useful for providing a scope in which it is safe to temporarily
2224 # modify global variables, e.g.
2225 #
2226 # global INTERNAL_GDBFLAGS
2227 # global env
2228 #
2229 # set foo GDBHISTSIZE
2230 #
2231 # save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } {
2232 # append INTERNAL_GDBFLAGS " -nx"
2233 # unset -nocomplain env(GDBHISTSIZE)
2234 # gdb_start
2235 # gdb_test ...
2236 # }
2237 #
2238 # Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be
2239 # modified inside BODY, this proc guarantees that the modifications will be
2240 # undone after BODY finishes executing.
2241
2242 proc save_vars { vars body } {
2243 array set saved_scalars { }
2244 array set saved_arrays { }
2245 set unset_vars { }
2246
2247 foreach var $vars {
2248 # First evaluate VAR in the context of the caller in case the variable
2249 # name may be a not-yet-interpolated string like env($foo)
2250 set var [uplevel 1 list $var]
2251
2252 if [uplevel 1 [list info exists $var]] {
2253 if [uplevel 1 [list array exists $var]] {
2254 set saved_arrays($var) [uplevel 1 [list array get $var]]
2255 } else {
2256 set saved_scalars($var) [uplevel 1 [list set $var]]
2257 }
2258 } else {
2259 lappend unset_vars $var
2260 }
2261 }
2262
2263 set code [catch {uplevel 1 $body} result]
2264
2265 foreach {var value} [array get saved_scalars] {
2266 uplevel 1 [list set $var $value]
2267 }
2268
2269 foreach {var value} [array get saved_arrays] {
2270 uplevel 1 [list unset $var]
2271 uplevel 1 [list array set $var $value]
2272 }
2273
2274 foreach var $unset_vars {
2275 uplevel 1 [list unset -nocomplain $var]
2276 }
2277
2278 if {$code == 1} {
2279 global errorInfo errorCode
2280 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2281 } else {
2282 return -code $code $result
2283 }
2284 }
2285
2286 # Run tests in BODY with the current working directory (CWD) set to
2287 # DIR. When BODY is finished, restore the original CWD. Return the
2288 # result of BODY.
2289 #
2290 # This procedure doesn't check if DIR is a valid directory, so you
2291 # have to make sure of that.
2292
2293 proc with_cwd { dir body } {
2294 set saved_dir [pwd]
2295 verbose -log "Switching to directory $dir (saved CWD: $saved_dir)."
2296 cd $dir
2297
2298 set code [catch {uplevel 1 $body} result]
2299
2300 verbose -log "Switching back to $saved_dir."
2301 cd $saved_dir
2302
2303 if {$code == 1} {
2304 global errorInfo errorCode
2305 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2306 } else {
2307 return -code $code $result
2308 }
2309 }
2310
2311 # Run tests in BODY with GDB prompt and variable $gdb_prompt set to
2312 # PROMPT. When BODY is finished, restore GDB prompt and variable
2313 # $gdb_prompt.
2314 # Returns the result of BODY.
2315 #
2316 # Notes:
2317 #
2318 # 1) If you want to use, for example, "(foo)" as the prompt you must pass it
2319 # as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in
2320 # TCL). PROMPT is internally converted to a suitable regexp for matching.
2321 # We do the conversion from "(foo)" to "\(foo\)" here for a few reasons:
2322 # a) It's more intuitive for callers to pass the plain text form.
2323 # b) We need two forms of the prompt:
2324 # - a regexp to use in output matching,
2325 # - a value to pass to the "set prompt" command.
2326 # c) It's easier to convert the plain text form to its regexp form.
2327 #
2328 # 2) Don't add a trailing space, we do that here.
2329
2330 proc with_gdb_prompt { prompt body } {
2331 global gdb_prompt
2332
2333 # Convert "(foo)" to "\(foo\)".
2334 # We don't use string_to_regexp because while it works today it's not
2335 # clear it will work tomorrow: the value we need must work as both a
2336 # regexp *and* as the argument to the "set prompt" command, at least until
2337 # we start recording both forms separately instead of just $gdb_prompt.
2338 # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the
2339 # regexp form.
2340 regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt
2341
2342 set saved $gdb_prompt
2343
2344 verbose -log "Setting gdb prompt to \"$prompt \"."
2345 set gdb_prompt $prompt
2346 gdb_test_no_output "set prompt $prompt " ""
2347
2348 set code [catch {uplevel 1 $body} result]
2349
2350 verbose -log "Restoring gdb prompt to \"$saved \"."
2351 set gdb_prompt $saved
2352 gdb_test_no_output "set prompt $saved " ""
2353
2354 if {$code == 1} {
2355 global errorInfo errorCode
2356 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2357 } else {
2358 return -code $code $result
2359 }
2360 }
2361
2362 # Run tests in BODY with target-charset setting to TARGET_CHARSET. When
2363 # BODY is finished, restore target-charset.
2364
2365 proc with_target_charset { target_charset body } {
2366 global gdb_prompt
2367
2368 set saved ""
2369 gdb_test_multiple "show target-charset" "" {
2370 -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " {
2371 set saved $expect_out(1,string)
2372 }
2373 -re "The target character set is \"(.*)\".*$gdb_prompt " {
2374 set saved $expect_out(1,string)
2375 }
2376 -re ".*$gdb_prompt " {
2377 fail "get target-charset"
2378 }
2379 }
2380
2381 gdb_test_no_output "set target-charset $target_charset" ""
2382
2383 set code [catch {uplevel 1 $body} result]
2384
2385 gdb_test_no_output "set target-charset $saved" ""
2386
2387 if {$code == 1} {
2388 global errorInfo errorCode
2389 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2390 } else {
2391 return -code $code $result
2392 }
2393 }
2394
2395 # Switch the default spawn id to SPAWN_ID, so that gdb_test,
2396 # mi_gdb_test etc. default to using it.
2397
2398 proc switch_gdb_spawn_id {spawn_id} {
2399 global gdb_spawn_id
2400 global board board_info
2401
2402 set gdb_spawn_id $spawn_id
2403 set board [host_info name]
2404 set board_info($board,fileid) $spawn_id
2405 }
2406
2407 # Clear the default spawn id.
2408
2409 proc clear_gdb_spawn_id {} {
2410 global gdb_spawn_id
2411 global board board_info
2412
2413 unset -nocomplain gdb_spawn_id
2414 set board [host_info name]
2415 unset -nocomplain board_info($board,fileid)
2416 }
2417
2418 # Run BODY with SPAWN_ID as current spawn id.
2419
2420 proc with_spawn_id { spawn_id body } {
2421 global gdb_spawn_id
2422
2423 if [info exists gdb_spawn_id] {
2424 set saved_spawn_id $gdb_spawn_id
2425 }
2426
2427 switch_gdb_spawn_id $spawn_id
2428
2429 set code [catch {uplevel 1 $body} result]
2430
2431 if [info exists saved_spawn_id] {
2432 switch_gdb_spawn_id $saved_spawn_id
2433 } else {
2434 clear_gdb_spawn_id
2435 }
2436
2437 if {$code == 1} {
2438 global errorInfo errorCode
2439 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2440 } else {
2441 return -code $code $result
2442 }
2443 }
2444
2445 # Select the largest timeout from all the timeouts:
2446 # - the local "timeout" variable of the scope two levels above,
2447 # - the global "timeout" variable,
2448 # - the board variable "gdb,timeout".
2449
2450 proc get_largest_timeout {} {
2451 upvar #0 timeout gtimeout
2452 upvar 2 timeout timeout
2453
2454 set tmt 0
2455 if [info exists timeout] {
2456 set tmt $timeout
2457 }
2458 if { [info exists gtimeout] && $gtimeout > $tmt } {
2459 set tmt $gtimeout
2460 }
2461 if { [target_info exists gdb,timeout]
2462 && [target_info gdb,timeout] > $tmt } {
2463 set tmt [target_info gdb,timeout]
2464 }
2465 if { $tmt == 0 } {
2466 # Eeeeew.
2467 set tmt 60
2468 }
2469
2470 return $tmt
2471 }
2472
2473 # Run tests in BODY with timeout increased by factor of FACTOR. When
2474 # BODY is finished, restore timeout.
2475
2476 proc with_timeout_factor { factor body } {
2477 global timeout
2478
2479 set savedtimeout $timeout
2480
2481 set timeout [expr [get_largest_timeout] * $factor]
2482 set code [catch {uplevel 1 $body} result]
2483
2484 set timeout $savedtimeout
2485 if {$code == 1} {
2486 global errorInfo errorCode
2487 return -code $code -errorinfo $errorInfo -errorcode $errorCode $result
2488 } else {
2489 return -code $code $result
2490 }
2491 }
2492
2493 # Run BODY with timeout factor FACTOR if check-read1 is used.
2494
2495 proc with_read1_timeout_factor { factor body } {
2496 if { [info exists ::env(READ1)] == 1 && $::env(READ1) == 1 } {
2497 # Use timeout factor
2498 } else {
2499 # Reset timeout factor
2500 set factor 1
2501 }
2502 return [uplevel [list with_timeout_factor $factor $body]]
2503 }
2504
2505 # Return 1 if _Complex types are supported, otherwise, return 0.
2506
2507 gdb_caching_proc support_complex_tests {
2508
2509 if { [gdb_skip_float_test] } {
2510 # If floating point is not supported, _Complex is not
2511 # supported.
2512 return 0
2513 }
2514
2515 # Compile a test program containing _Complex types.
2516
2517 return [gdb_can_simple_compile complex {
2518 int main() {
2519 _Complex float cf;
2520 _Complex double cd;
2521 _Complex long double cld;
2522 return 0;
2523 }
2524 } executable]
2525 }
2526
2527 # Return 1 if compiling go is supported.
2528 gdb_caching_proc support_go_compile {
2529
2530 return [gdb_can_simple_compile go-hello {
2531 package main
2532 import "fmt"
2533 func main() {
2534 fmt.Println("hello world")
2535 }
2536 } executable go]
2537 }
2538
2539 # Return 1 if GDB can get a type for siginfo from the target, otherwise
2540 # return 0.
2541
2542 proc supports_get_siginfo_type {} {
2543 if { [istarget "*-*-linux*"] } {
2544 return 1
2545 } else {
2546 return 0
2547 }
2548 }
2549
2550 # Return 1 if the target supports hardware single stepping.
2551
2552 proc can_hardware_single_step {} {
2553
2554 if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"]
2555 || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"]
2556 || [istarget "nios2-*-*"] } {
2557 return 0
2558 }
2559
2560 return 1
2561 }
2562
2563 # Return 1 if target hardware or OS supports single stepping to signal
2564 # handler, otherwise, return 0.
2565
2566 proc can_single_step_to_signal_handler {} {
2567 # Targets don't have hardware single step. On these targets, when
2568 # a signal is delivered during software single step, gdb is unable
2569 # to determine the next instruction addresses, because start of signal
2570 # handler is one of them.
2571 return [can_hardware_single_step]
2572 }
2573
2574 # Return 1 if target supports process record, otherwise return 0.
2575
2576 proc supports_process_record {} {
2577
2578 if [target_info exists gdb,use_precord] {
2579 return [target_info gdb,use_precord]
2580 }
2581
2582 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2583 || [istarget "i\[34567\]86-*-linux*"]
2584 || [istarget "aarch64*-*-linux*"]
2585 || [istarget "powerpc*-*-linux*"]
2586 || [istarget "s390*-*-linux*"] } {
2587 return 1
2588 }
2589
2590 return 0
2591 }
2592
2593 # Return 1 if target supports reverse debugging, otherwise return 0.
2594
2595 proc supports_reverse {} {
2596
2597 if [target_info exists gdb,can_reverse] {
2598 return [target_info gdb,can_reverse]
2599 }
2600
2601 if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"]
2602 || [istarget "i\[34567\]86-*-linux*"]
2603 || [istarget "aarch64*-*-linux*"]
2604 || [istarget "powerpc*-*-linux*"]
2605 || [istarget "s390*-*-linux*"] } {
2606 return 1
2607 }
2608
2609 return 0
2610 }
2611
2612 # Return 1 if readline library is used.
2613
2614 proc readline_is_used { } {
2615 global gdb_prompt
2616
2617 gdb_test_multiple "show editing" "" {
2618 -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" {
2619 return 1
2620 }
2621 -re ".*$gdb_prompt $" {
2622 return 0
2623 }
2624 }
2625 }
2626
2627 # Return 1 if target is ELF.
2628 gdb_caching_proc is_elf_target {
2629 set me "is_elf_target"
2630
2631 set src { int foo () {return 0;} }
2632 if {![gdb_simple_compile elf_target $src]} {
2633 return 0
2634 }
2635
2636 set fp_obj [open $obj "r"]
2637 fconfigure $fp_obj -translation binary
2638 set data [read $fp_obj]
2639 close $fp_obj
2640
2641 file delete $obj
2642
2643 set ELFMAG "\u007FELF"
2644
2645 if {[string compare -length 4 $data $ELFMAG] != 0} {
2646 verbose "$me: returning 0" 2
2647 return 0
2648 }
2649
2650 verbose "$me: returning 1" 2
2651 return 1
2652 }
2653
2654 # Return 1 if the memory at address zero is readable.
2655
2656 gdb_caching_proc is_address_zero_readable {
2657 global gdb_prompt
2658
2659 set ret 0
2660 gdb_test_multiple "x 0" "" {
2661 -re "Cannot access memory at address 0x0.*$gdb_prompt $" {
2662 set ret 0
2663 }
2664 -re ".*$gdb_prompt $" {
2665 set ret 1
2666 }
2667 }
2668
2669 return $ret
2670 }
2671
2672 # Produce source file NAME and write SOURCES into it.
2673
2674 proc gdb_produce_source { name sources } {
2675 set index 0
2676 set f [open $name "w"]
2677
2678 puts $f $sources
2679 close $f
2680 }
2681
2682 # Return 1 if target is ILP32.
2683 # This cannot be decided simply from looking at the target string,
2684 # as it might depend on externally passed compiler options like -m64.
2685 gdb_caching_proc is_ilp32_target {
2686 return [gdb_can_simple_compile is_ilp32_target {
2687 int dummy[sizeof (int) == 4
2688 && sizeof (void *) == 4
2689 && sizeof (long) == 4 ? 1 : -1];
2690 }]
2691 }
2692
2693 # Return 1 if target is LP64.
2694 # This cannot be decided simply from looking at the target string,
2695 # as it might depend on externally passed compiler options like -m64.
2696 gdb_caching_proc is_lp64_target {
2697 return [gdb_can_simple_compile is_lp64_target {
2698 int dummy[sizeof (int) == 4
2699 && sizeof (void *) == 8
2700 && sizeof (long) == 8 ? 1 : -1];
2701 }]
2702 }
2703
2704 # Return 1 if target has 64 bit addresses.
2705 # This cannot be decided simply from looking at the target string,
2706 # as it might depend on externally passed compiler options like -m64.
2707 gdb_caching_proc is_64_target {
2708 return [gdb_can_simple_compile is_64_target {
2709 int function(void) { return 3; }
2710 int dummy[sizeof (&function) == 8 ? 1 : -1];
2711 }]
2712 }
2713
2714 # Return 1 if target has x86_64 registers - either amd64 or x32.
2715 # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined
2716 # just from the target string.
2717 gdb_caching_proc is_amd64_regs_target {
2718 if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} {
2719 return 0
2720 }
2721
2722 return [gdb_can_simple_compile is_amd64_regs_target {
2723 int main (void) {
2724 asm ("incq %rax");
2725 asm ("incq %r15");
2726
2727 return 0;
2728 }
2729 }]
2730 }
2731
2732 # Return 1 if this target is an x86 or x86-64 with -m32.
2733 proc is_x86_like_target {} {
2734 if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} {
2735 return 0
2736 }
2737 return [expr [is_ilp32_target] && ![is_amd64_regs_target]]
2738 }
2739
2740 # Return 1 if this target is an arm or aarch32 on aarch64.
2741
2742 gdb_caching_proc is_aarch32_target {
2743 if { [istarget "arm*-*-*"] } {
2744 return 1
2745 }
2746
2747 if { ![istarget "aarch64*-*-*"] } {
2748 return 0
2749 }
2750
2751 set list {}
2752 foreach reg \
2753 {r0 r1 r2 r3} {
2754 lappend list "\tmov $reg, $reg"
2755 }
2756
2757 return [gdb_can_simple_compile aarch32 [join $list \n]]
2758 }
2759
2760 # Return 1 if this target is an aarch64, either lp64 or ilp32.
2761
2762 proc is_aarch64_target {} {
2763 if { ![istarget "aarch64*-*-*"] } {
2764 return 0
2765 }
2766
2767 return [expr ![is_aarch32_target]]
2768 }
2769
2770 # Return 1 if displaced stepping is supported on target, otherwise, return 0.
2771 proc support_displaced_stepping {} {
2772
2773 if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"]
2774 || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"]
2775 || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"]
2776 || [istarget "aarch64*-*-linux*"] } {
2777 return 1
2778 }
2779
2780 return 0
2781 }
2782
2783 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2784 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2785
2786 gdb_caching_proc skip_altivec_tests {
2787 global srcdir subdir gdb_prompt inferior_exited_re
2788
2789 set me "skip_altivec_tests"
2790
2791 # Some simulators are known to not support VMX instructions.
2792 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2793 verbose "$me: target known to not support VMX, returning 1" 2
2794 return 1
2795 }
2796
2797 # Make sure we have a compiler that understands altivec.
2798 if [get_compiler_info] {
2799 warning "Could not get compiler info"
2800 return 1
2801 }
2802 if [test_compiler_info gcc*] {
2803 set compile_flags "additional_flags=-maltivec"
2804 } elseif [test_compiler_info xlc*] {
2805 set compile_flags "additional_flags=-qaltivec"
2806 } else {
2807 verbose "Could not compile with altivec support, returning 1" 2
2808 return 1
2809 }
2810
2811 # Compile a test program containing VMX instructions.
2812 set src {
2813 int main() {
2814 #ifdef __MACH__
2815 asm volatile ("vor v0,v0,v0");
2816 #else
2817 asm volatile ("vor 0,0,0");
2818 #endif
2819 return 0;
2820 }
2821 }
2822 if {![gdb_simple_compile $me $src executable $compile_flags]} {
2823 return 1
2824 }
2825
2826 # Compilation succeeded so now run it via gdb.
2827
2828 gdb_exit
2829 gdb_start
2830 gdb_reinitialize_dir $srcdir/$subdir
2831 gdb_load "$obj"
2832 gdb_run_cmd
2833 gdb_expect {
2834 -re ".*Illegal instruction.*${gdb_prompt} $" {
2835 verbose -log "\n$me altivec hardware not detected"
2836 set skip_vmx_tests 1
2837 }
2838 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2839 verbose -log "\n$me: altivec hardware detected"
2840 set skip_vmx_tests 0
2841 }
2842 default {
2843 warning "\n$me: default case taken"
2844 set skip_vmx_tests 1
2845 }
2846 }
2847 gdb_exit
2848 remote_file build delete $obj
2849
2850 verbose "$me: returning $skip_vmx_tests" 2
2851 return $skip_vmx_tests
2852 }
2853
2854 # Run a test on the target to see if it supports vmx hardware. Return 0 if so,
2855 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2856
2857 gdb_caching_proc skip_vsx_tests {
2858 global srcdir subdir gdb_prompt inferior_exited_re
2859
2860 set me "skip_vsx_tests"
2861
2862 # Some simulators are known to not support Altivec instructions, so
2863 # they won't support VSX instructions as well.
2864 if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } {
2865 verbose "$me: target known to not support VSX, returning 1" 2
2866 return 1
2867 }
2868
2869 # Make sure we have a compiler that understands altivec.
2870 if [get_compiler_info] {
2871 warning "Could not get compiler info"
2872 return 1
2873 }
2874 if [test_compiler_info gcc*] {
2875 set compile_flags "additional_flags=-mvsx"
2876 } elseif [test_compiler_info xlc*] {
2877 set compile_flags "additional_flags=-qasm=gcc"
2878 } else {
2879 verbose "Could not compile with vsx support, returning 1" 2
2880 return 1
2881 }
2882
2883 # Compile a test program containing VSX instructions.
2884 set src {
2885 int main() {
2886 double a[2] = { 1.0, 2.0 };
2887 #ifdef __MACH__
2888 asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a));
2889 #else
2890 asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a));
2891 #endif
2892 return 0;
2893 }
2894 }
2895 if {![gdb_simple_compile $me $src executable $compile_flags]} {
2896 return 1
2897 }
2898
2899 # No error message, compilation succeeded so now run it via gdb.
2900
2901 gdb_exit
2902 gdb_start
2903 gdb_reinitialize_dir $srcdir/$subdir
2904 gdb_load "$obj"
2905 gdb_run_cmd
2906 gdb_expect {
2907 -re ".*Illegal instruction.*${gdb_prompt} $" {
2908 verbose -log "\n$me VSX hardware not detected"
2909 set skip_vsx_tests 1
2910 }
2911 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2912 verbose -log "\n$me: VSX hardware detected"
2913 set skip_vsx_tests 0
2914 }
2915 default {
2916 warning "\n$me: default case taken"
2917 set skip_vsx_tests 1
2918 }
2919 }
2920 gdb_exit
2921 remote_file build delete $obj
2922
2923 verbose "$me: returning $skip_vsx_tests" 2
2924 return $skip_vsx_tests
2925 }
2926
2927 # Run a test on the target to see if it supports TSX hardware. Return 0 if so,
2928 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2929
2930 gdb_caching_proc skip_tsx_tests {
2931 global srcdir subdir gdb_prompt inferior_exited_re
2932
2933 set me "skip_tsx_tests"
2934
2935 # Compile a test program.
2936 set src {
2937 int main() {
2938 asm volatile ("xbegin .L0");
2939 asm volatile ("xend");
2940 asm volatile (".L0: nop");
2941 return 0;
2942 }
2943 }
2944 if {![gdb_simple_compile $me $src executable]} {
2945 return 1
2946 }
2947
2948 # No error message, compilation succeeded so now run it via gdb.
2949
2950 gdb_exit
2951 gdb_start
2952 gdb_reinitialize_dir $srcdir/$subdir
2953 gdb_load "$obj"
2954 gdb_run_cmd
2955 gdb_expect {
2956 -re ".*Illegal instruction.*${gdb_prompt} $" {
2957 verbose -log "$me: TSX hardware not detected."
2958 set skip_tsx_tests 1
2959 }
2960 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
2961 verbose -log "$me: TSX hardware detected."
2962 set skip_tsx_tests 0
2963 }
2964 default {
2965 warning "\n$me: default case taken."
2966 set skip_tsx_tests 1
2967 }
2968 }
2969 gdb_exit
2970 remote_file build delete $obj
2971
2972 verbose "$me: returning $skip_tsx_tests" 2
2973 return $skip_tsx_tests
2974 }
2975
2976 # Run a test on the target to see if it supports btrace hardware. Return 0 if so,
2977 # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite.
2978
2979 gdb_caching_proc skip_btrace_tests {
2980 global srcdir subdir gdb_prompt inferior_exited_re
2981
2982 set me "skip_btrace_tests"
2983 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
2984 verbose "$me: target does not support btrace, returning 1" 2
2985 return 1
2986 }
2987
2988 # Compile a test program.
2989 set src { int main() { return 0; } }
2990 if {![gdb_simple_compile $me $src executable]} {
2991 return 1
2992 }
2993
2994 # No error message, compilation succeeded so now run it via gdb.
2995
2996 gdb_exit
2997 gdb_start
2998 gdb_reinitialize_dir $srcdir/$subdir
2999 gdb_load $obj
3000 if ![runto_main] {
3001 return 1
3002 }
3003 # In case of an unexpected output, we return 2 as a fail value.
3004 set skip_btrace_tests 2
3005 gdb_test_multiple "record btrace" "check btrace support" {
3006 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3007 set skip_btrace_tests 1
3008 }
3009 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3010 set skip_btrace_tests 1
3011 }
3012 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3013 set skip_btrace_tests 1
3014 }
3015 -re "^record btrace\r\n$gdb_prompt $" {
3016 set skip_btrace_tests 0
3017 }
3018 }
3019 gdb_exit
3020 remote_file build delete $obj
3021
3022 verbose "$me: returning $skip_btrace_tests" 2
3023 return $skip_btrace_tests
3024 }
3025
3026 # Run a test on the target to see if it supports btrace pt hardware.
3027 # Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available'
3028 # from the GCC testsuite.
3029
3030 gdb_caching_proc skip_btrace_pt_tests {
3031 global srcdir subdir gdb_prompt inferior_exited_re
3032
3033 set me "skip_btrace_tests"
3034 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } {
3035 verbose "$me: target does not support btrace, returning 1" 2
3036 return 1
3037 }
3038
3039 # Compile a test program.
3040 set src { int main() { return 0; } }
3041 if {![gdb_simple_compile $me $src executable]} {
3042 return 1
3043 }
3044
3045 # No error message, compilation succeeded so now run it via gdb.
3046
3047 gdb_exit
3048 gdb_start
3049 gdb_reinitialize_dir $srcdir/$subdir
3050 gdb_load $obj
3051 if ![runto_main] {
3052 return 1
3053 }
3054 # In case of an unexpected output, we return 2 as a fail value.
3055 set skip_btrace_tests 2
3056 gdb_test_multiple "record btrace pt" "check btrace pt support" {
3057 -re "You can't do that when your target is.*\r\n$gdb_prompt $" {
3058 set skip_btrace_tests 1
3059 }
3060 -re "Target does not support branch tracing.*\r\n$gdb_prompt $" {
3061 set skip_btrace_tests 1
3062 }
3063 -re "Could not enable branch tracing.*\r\n$gdb_prompt $" {
3064 set skip_btrace_tests 1
3065 }
3066 -re "support was disabled at compile time.*\r\n$gdb_prompt $" {
3067 set skip_btrace_tests 1
3068 }
3069 -re "^record btrace pt\r\n$gdb_prompt $" {
3070 set skip_btrace_tests 0
3071 }
3072 }
3073 gdb_exit
3074 remote_file build delete $obj
3075
3076 verbose "$me: returning $skip_btrace_tests" 2
3077 return $skip_btrace_tests
3078 }
3079
3080 # Run a test on the target to see if it supports Aarch64 SVE hardware.
3081 # Return 0 if so, 1 if it does not. Note this causes a restart of GDB.
3082
3083 gdb_caching_proc skip_aarch64_sve_tests {
3084 global srcdir subdir gdb_prompt inferior_exited_re
3085
3086 set me "skip_aarch64_sve_tests"
3087
3088 if { ![is_aarch64_target]} {
3089 return 1
3090 }
3091
3092 set compile_flags "{additional_flags=-march=armv8-a+sve}"
3093
3094 # Compile a test program containing SVE instructions.
3095 set src {
3096 int main() {
3097 asm volatile ("ptrue p0.b");
3098 return 0;
3099 }
3100 }
3101 if {![gdb_simple_compile $me $src executable $compile_flags]} {
3102 return 1
3103 }
3104
3105 # Compilation succeeded so now run it via gdb.
3106 clean_restart $obj
3107 gdb_run_cmd
3108 gdb_expect {
3109 -re ".*Illegal instruction.*${gdb_prompt} $" {
3110 verbose -log "\n$me sve hardware not detected"
3111 set skip_sve_tests 1
3112 }
3113 -re ".*$inferior_exited_re normally.*${gdb_prompt} $" {
3114 verbose -log "\n$me: sve hardware detected"
3115 set skip_sve_tests 0
3116 }
3117 default {
3118 warning "\n$me: default case taken"
3119 set skip_sve_tests 1
3120 }
3121 }
3122 gdb_exit
3123 remote_file build delete $obj
3124
3125 verbose "$me: returning $skip_sve_tests" 2
3126 return $skip_sve_tests
3127 }
3128
3129
3130 # A helper that compiles a test case to see if __int128 is supported.
3131 proc gdb_int128_helper {lang} {
3132 return [gdb_can_simple_compile "i128-for-$lang" {
3133 __int128 x;
3134 int main() { return 0; }
3135 } executable $lang]
3136 }
3137
3138 # Return true if the C compiler understands the __int128 type.
3139 gdb_caching_proc has_int128_c {
3140 return [gdb_int128_helper c]
3141 }
3142
3143 # Return true if the C++ compiler understands the __int128 type.
3144 gdb_caching_proc has_int128_cxx {
3145 return [gdb_int128_helper c++]
3146 }
3147
3148 # Return true if the IFUNC feature is unsupported.
3149 gdb_caching_proc skip_ifunc_tests {
3150 if [gdb_can_simple_compile ifunc {
3151 extern void f_ ();
3152 typedef void F (void);
3153 F* g (void) { return &f_; }
3154 void f () __attribute__ ((ifunc ("g")));
3155 } object] {
3156 return 0
3157 } else {
3158 return 1
3159 }
3160 }
3161
3162 # Return whether we should skip tests for showing inlined functions in
3163 # backtraces. Requires get_compiler_info and get_debug_format.
3164
3165 proc skip_inline_frame_tests {} {
3166 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3167 if { ! [test_debug_format "DWARF 2"] } {
3168 return 1
3169 }
3170
3171 # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line.
3172 if { ([test_compiler_info "gcc-2-*"]
3173 || [test_compiler_info "gcc-3-*"]
3174 || [test_compiler_info "gcc-4-0-*"]) } {
3175 return 1
3176 }
3177
3178 return 0
3179 }
3180
3181 # Return whether we should skip tests for showing variables from
3182 # inlined functions. Requires get_compiler_info and get_debug_format.
3183
3184 proc skip_inline_var_tests {} {
3185 # GDB only recognizes inlining information in DWARF 2 (DWARF 3).
3186 if { ! [test_debug_format "DWARF 2"] } {
3187 return 1
3188 }
3189
3190 return 0
3191 }
3192
3193 # Return a 1 if we should skip tests that require hardware breakpoints
3194
3195 proc skip_hw_breakpoint_tests {} {
3196 # Skip tests if requested by the board (note that no_hardware_watchpoints
3197 # disables both watchpoints and breakpoints)
3198 if { [target_info exists gdb,no_hardware_watchpoints]} {
3199 return 1
3200 }
3201
3202 # These targets support hardware breakpoints natively
3203 if { [istarget "i?86-*-*"]
3204 || [istarget "x86_64-*-*"]
3205 || [istarget "ia64-*-*"]
3206 || [istarget "arm*-*-*"]
3207 || [istarget "aarch64*-*-*"]
3208 || [istarget "s390*-*-*"] } {
3209 return 0
3210 }
3211
3212 return 1
3213 }
3214
3215 # Return a 1 if we should skip tests that require hardware watchpoints
3216
3217 proc skip_hw_watchpoint_tests {} {
3218 # Skip tests if requested by the board
3219 if { [target_info exists gdb,no_hardware_watchpoints]} {
3220 return 1
3221 }
3222
3223 # These targets support hardware watchpoints natively
3224 if { [istarget "i?86-*-*"]
3225 || [istarget "x86_64-*-*"]
3226 || [istarget "ia64-*-*"]
3227 || [istarget "arm*-*-*"]
3228 || [istarget "aarch64*-*-*"]
3229 || [istarget "powerpc*-*-linux*"]
3230 || [istarget "s390*-*-*"] } {
3231 return 0
3232 }
3233
3234 return 1
3235 }
3236
3237 # Return a 1 if we should skip tests that require *multiple* hardware
3238 # watchpoints to be active at the same time
3239
3240 proc skip_hw_watchpoint_multi_tests {} {
3241 if { [skip_hw_watchpoint_tests] } {
3242 return 1
3243 }
3244
3245 # These targets support just a single hardware watchpoint
3246 if { [istarget "arm*-*-*"]
3247 || [istarget "powerpc*-*-linux*"] } {
3248 return 1
3249 }
3250
3251 return 0
3252 }
3253
3254 # Return a 1 if we should skip tests that require read/access watchpoints
3255
3256 proc skip_hw_watchpoint_access_tests {} {
3257 if { [skip_hw_watchpoint_tests] } {
3258 return 1
3259 }
3260
3261 # These targets support just write watchpoints
3262 if { [istarget "s390*-*-*"] } {
3263 return 1
3264 }
3265
3266 return 0
3267 }
3268
3269 # Return 1 if we should skip tests that require the runtime unwinder
3270 # hook. This must be invoked while gdb is running, after shared
3271 # libraries have been loaded. This is needed because otherwise a
3272 # shared libgcc won't be visible.
3273
3274 proc skip_unwinder_tests {} {
3275 global gdb_prompt
3276
3277 set ok 0
3278 gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" {
3279 -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3280 }
3281 -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" {
3282 set ok 1
3283 }
3284 -re "No symbol .* in current context.\r\n$gdb_prompt $" {
3285 }
3286 }
3287 if {!$ok} {
3288 gdb_test_multiple "info probe" "check for stap probe in unwinder" {
3289 -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" {
3290 set ok 1
3291 }
3292 -re "\r\n$gdb_prompt $" {
3293 }
3294 }
3295 }
3296 return $ok
3297 }
3298
3299 # Return 1 if we should skip tests that require the libstdc++ stap
3300 # probes. This must be invoked while gdb is running, after shared
3301 # libraries have been loaded. PROMPT_REGEXP is the expected prompt.
3302
3303 proc skip_libstdcxx_probe_tests_prompt { prompt_regexp } {
3304 set supported 0
3305 gdb_test_multiple "info probe" "check for stap probe in libstdc++" \
3306 -prompt "$prompt_regexp" {
3307 -re ".*libstdcxx.*catch.*\r\n$prompt_regexp" {
3308 set supported 1
3309 }
3310 -re "\r\n$prompt_regexp" {
3311 }
3312 }
3313 set skip [expr !$supported]
3314 return $skip
3315 }
3316
3317 # As skip_libstdcxx_probe_tests_prompt, with gdb_prompt.
3318
3319 proc skip_libstdcxx_probe_tests {} {
3320 global gdb_prompt
3321 return [skip_libstdcxx_probe_tests_prompt "$gdb_prompt $"]
3322 }
3323
3324 # Return 1 if we should skip tests of the "compile" feature.
3325 # This must be invoked after the inferior has been started.
3326
3327 proc skip_compile_feature_tests {} {
3328 global gdb_prompt
3329
3330 set result 0
3331 gdb_test_multiple "compile code -- ;" "check for working compile command" {
3332 "Could not load libcc1.*\r\n$gdb_prompt $" {
3333 set result 1
3334 }
3335 -re "Command not supported on this host\\..*\r\n$gdb_prompt $" {
3336 set result 1
3337 }
3338 -re "\r\n$gdb_prompt $" {
3339 }
3340 }
3341 return $result
3342 }
3343
3344 # Helper for gdb_is_target_* procs. TARGET_NAME is the name of the target
3345 # we're looking for (used to build the test name). TARGET_STACK_REGEXP
3346 # is a regexp that will match the output of "maint print target-stack" if
3347 # the target in question is currently pushed. PROMPT_REGEXP is a regexp
3348 # matching the expected prompt after the command output.
3349
3350 proc gdb_is_target_1 { target_name target_stack_regexp prompt_regexp } {
3351 set test "probe for target ${target_name}"
3352 gdb_test_multiple "maint print target-stack" $test \
3353 -prompt "$prompt_regexp" {
3354 -re "${target_stack_regexp}${prompt_regexp}" {
3355 pass $test
3356 return 1
3357 }
3358 -re "$prompt_regexp" {
3359 pass $test
3360 }
3361 }
3362 return 0
3363 }
3364
3365 # Helper for gdb_is_target_remote where the expected prompt is variable.
3366
3367 proc gdb_is_target_remote_prompt { prompt_regexp } {
3368 return [gdb_is_target_1 "remote" ".*emote serial target in gdb-specific protocol.*" $prompt_regexp]
3369 }
3370
3371 # Check whether we're testing with the remote or extended-remote
3372 # targets.
3373
3374 proc gdb_is_target_remote { } {
3375 global gdb_prompt
3376
3377 return [gdb_is_target_remote_prompt "$gdb_prompt $"]
3378 }
3379
3380 # Check whether we're testing with the native target.
3381
3382 proc gdb_is_target_native { } {
3383 global gdb_prompt
3384
3385 return [gdb_is_target_1 "native" ".*native \\(Native process\\).*" "$gdb_prompt $"]
3386 }
3387
3388 # Return the effective value of use_gdb_stub.
3389 #
3390 # If the use_gdb_stub global has been set (it is set when the gdb process is
3391 # spawned), return that. Otherwise, return the value of the use_gdb_stub
3392 # property from the board file.
3393 #
3394 # This is the preferred way of checking use_gdb_stub, since it allows to check
3395 # the value before the gdb has been spawned and it will return the correct value
3396 # even when it was overriden by the test.
3397
3398 proc use_gdb_stub {} {
3399 global use_gdb_stub
3400
3401 if [info exists use_gdb_stub] {
3402 return $use_gdb_stub
3403 }
3404
3405 return [target_info exists use_gdb_stub]
3406 }
3407
3408 # Return 1 if the current remote target is an instance of our GDBserver, 0
3409 # otherwise. Return -1 if there was an error and we can't tell.
3410
3411 gdb_caching_proc target_is_gdbserver {
3412 global gdb_prompt
3413
3414 set is_gdbserver -1
3415 set test "probing for GDBserver"
3416
3417 gdb_test_multiple "monitor help" $test {
3418 -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" {
3419 set is_gdbserver 1
3420 }
3421 -re "$gdb_prompt $" {
3422 set is_gdbserver 0
3423 }
3424 }
3425
3426 if { $is_gdbserver == -1 } {
3427 verbose -log "Unable to tell whether we are using GDBserver or not."
3428 }
3429
3430 return $is_gdbserver
3431 }
3432
3433 # N.B. compiler_info is intended to be local to this file.
3434 # Call test_compiler_info with no arguments to fetch its value.
3435 # Yes, this is counterintuitive when there's get_compiler_info,
3436 # but that's the current API.
3437 if [info exists compiler_info] {
3438 unset compiler_info
3439 }
3440
3441 set gcc_compiled 0
3442
3443 # Figure out what compiler I am using.
3444 # The result is cached so only the first invocation runs the compiler.
3445 #
3446 # ARG can be empty or "C++". If empty, "C" is assumed.
3447 #
3448 # There are several ways to do this, with various problems.
3449 #
3450 # [ gdb_compile -E $ifile -o $binfile.ci ]
3451 # source $binfile.ci
3452 #
3453 # Single Unix Spec v3 says that "-E -o ..." together are not
3454 # specified. And in fact, the native compiler on hp-ux 11 (among
3455 # others) does not work with "-E -o ...". Most targets used to do
3456 # this, and it mostly worked, because it works with gcc.
3457 #
3458 # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ]
3459 # source $binfile.ci
3460 #
3461 # This avoids the problem with -E and -o together. This almost works
3462 # if the build machine is the same as the host machine, which is
3463 # usually true of the targets which are not gcc. But this code does
3464 # not figure which compiler to call, and it always ends up using the C
3465 # compiler. Not good for setting hp_aCC_compiler. Target
3466 # hppa*-*-hpux* used to do this.
3467 #
3468 # [ gdb_compile -E $ifile > $binfile.ci ]
3469 # source $binfile.ci
3470 #
3471 # dejagnu target_compile says that it supports output redirection,
3472 # but the code is completely different from the normal path and I
3473 # don't want to sweep the mines from that path. So I didn't even try
3474 # this.
3475 #
3476 # set cppout [ gdb_compile $ifile "" preprocess $args quiet ]
3477 # eval $cppout
3478 #
3479 # I actually do this for all targets now. gdb_compile runs the right
3480 # compiler, and TCL captures the output, and I eval the output.
3481 #
3482 # Unfortunately, expect logs the output of the command as it goes by,
3483 # and dejagnu helpfully prints a second copy of it right afterwards.
3484 # So I turn off expect logging for a moment.
3485 #
3486 # [ gdb_compile $ifile $ciexe_file executable $args ]
3487 # [ remote_exec $ciexe_file ]
3488 # [ source $ci_file.out ]
3489 #
3490 # I could give up on -E and just do this.
3491 # I didn't get desperate enough to try this.
3492 #
3493 # -- chastain 2004-01-06
3494
3495 proc get_compiler_info {{arg ""}} {
3496 # For compiler.c and compiler.cc
3497 global srcdir
3498
3499 # I am going to play with the log to keep noise out.
3500 global outdir
3501 global tool
3502
3503 # These come from compiler.c or compiler.cc
3504 global compiler_info
3505
3506 # Legacy global data symbols.
3507 global gcc_compiled
3508
3509 if [info exists compiler_info] {
3510 # Already computed.
3511 return 0
3512 }
3513
3514 # Choose which file to preprocess.
3515 set ifile "${srcdir}/lib/compiler.c"
3516 if { $arg == "c++" } {
3517 set ifile "${srcdir}/lib/compiler.cc"
3518 }
3519
3520 # Run $ifile through the right preprocessor.
3521 # Toggle gdb.log to keep the compiler output out of the log.
3522 set saved_log [log_file -info]
3523 log_file
3524 if [is_remote host] {
3525 # We have to use -E and -o together, despite the comments
3526 # above, because of how DejaGnu handles remote host testing.
3527 set ppout "$outdir/compiler.i"
3528 gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet getting_compiler_info]
3529 set file [open $ppout r]
3530 set cppout [read $file]
3531 close $file
3532 } else {
3533 set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet getting_compiler_info] ]
3534 }
3535 eval log_file $saved_log
3536
3537 # Eval the output.
3538 set unknown 0
3539 foreach cppline [ split "$cppout" "\n" ] {
3540 if { [ regexp "^#" "$cppline" ] } {
3541 # line marker
3542 } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
3543 # blank line
3544 } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
3545 # eval this line
3546 verbose "get_compiler_info: $cppline" 2
3547 eval "$cppline"
3548 } else {
3549 # unknown line
3550 verbose -log "get_compiler_info: $cppline"
3551 set unknown 1
3552 }
3553 }
3554
3555 # Set to unknown if for some reason compiler_info didn't get defined.
3556 if ![info exists compiler_info] {
3557 verbose -log "get_compiler_info: compiler_info not provided"
3558 set compiler_info "unknown"
3559 }
3560 # Also set to unknown compiler if any diagnostics happened.
3561 if { $unknown } {
3562 verbose -log "get_compiler_info: got unexpected diagnostics"
3563 set compiler_info "unknown"
3564 }
3565
3566 # Set the legacy symbols.
3567 set gcc_compiled 0
3568 regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled
3569
3570 # Log what happened.
3571 verbose -log "get_compiler_info: $compiler_info"
3572
3573 # Most compilers will evaluate comparisons and other boolean
3574 # operations to 0 or 1.
3575 uplevel \#0 { set true 1 }
3576 uplevel \#0 { set false 0 }
3577
3578 return 0
3579 }
3580
3581 # Return the compiler_info string if no arg is provided.
3582 # Otherwise the argument is a glob-style expression to match against
3583 # compiler_info.
3584
3585 proc test_compiler_info { {compiler ""} } {
3586 global compiler_info
3587 get_compiler_info
3588
3589 # If no arg, return the compiler_info string.
3590 if [string match "" $compiler] {
3591 return $compiler_info
3592 }
3593
3594 return [string match $compiler $compiler_info]
3595 }
3596
3597 proc current_target_name { } {
3598 global target_info
3599 if [info exists target_info(target,name)] {
3600 set answer $target_info(target,name)
3601 } else {
3602 set answer ""
3603 }
3604 return $answer
3605 }
3606
3607 set gdb_wrapper_initialized 0
3608 set gdb_wrapper_target ""
3609
3610 proc gdb_wrapper_init { args } {
3611 global gdb_wrapper_initialized
3612 global gdb_wrapper_file
3613 global gdb_wrapper_flags
3614 global gdb_wrapper_target
3615
3616 # If the wrapper is initialized but the wrapper file cannot be
3617 # found anymore, the wrapper file must be built again.
3618 if { $gdb_wrapper_initialized == 1 && \
3619 [info exists gdb_wrapper_file] && \
3620 ![file exists $gdb_wrapper_file] } {
3621 verbose "reinitializing the wrapper"
3622 set gdb_wrapper_initialized 0
3623 }
3624
3625 if { $gdb_wrapper_initialized == 1 } { return; }
3626
3627 if {[target_info exists needs_status_wrapper] && \
3628 [target_info needs_status_wrapper] != "0"} {
3629 set result [build_wrapper [standard_output_file "testglue.o"]]
3630 if { $result != "" } {
3631 set gdb_wrapper_file [lindex $result 0]
3632 set gdb_wrapper_flags [lindex $result 1]
3633 } else {
3634 warning "Status wrapper failed to build."
3635 }
3636 }
3637 set gdb_wrapper_initialized 1
3638 set gdb_wrapper_target [current_target_name]
3639 }
3640
3641 # Determine options that we always want to pass to the compiler.
3642 gdb_caching_proc universal_compile_options {
3643 set me "universal_compile_options"
3644 set options {}
3645
3646 set src [standard_temp_file ccopts[pid].c]
3647 set obj [standard_temp_file ccopts[pid].o]
3648
3649 gdb_produce_source $src {
3650 int foo(void) { return 0; }
3651 }
3652
3653 # Try an option for disabling colored diagnostics. Some compilers
3654 # yield colored diagnostics by default (when run from a tty) unless
3655 # such an option is specified.
3656 set opt "additional_flags=-fdiagnostics-color=never"
3657 set lines [target_compile $src $obj object [list "quiet" $opt]]
3658 if [string match "" $lines] then {
3659 # Seems to have worked; use the option.
3660 lappend options $opt
3661 }
3662 file delete $src
3663 file delete $obj
3664
3665 verbose "$me: returning $options" 2
3666 return $options
3667 }
3668
3669 # Compile the code in $code to a file based on $name, using the flags
3670 # $compile_flag as well as debug, nowarning and quiet.
3671 # Return 1 if code can be compiled
3672 # Leave the file name of the resulting object in the upvar object.
3673
3674 proc gdb_simple_compile {name code {type object} {compile_flags {}} {object obj}} {
3675 upvar $object obj
3676
3677 switch -regexp -- $type {
3678 "executable" {
3679 set postfix "x"
3680 }
3681 "object" {
3682 set postfix "o"
3683 }
3684 "preprocess" {
3685 set postfix "i"
3686 }
3687 "assembly" {
3688 set postfix "s"
3689 }
3690 }
3691 set ext "c"
3692 foreach flag $compile_flags {
3693 if { "$flag" == "go" } {
3694 set ext "go"
3695 break
3696 }
3697 }
3698 set src [standard_temp_file $name-[pid].$ext]
3699 set obj [standard_temp_file $name-[pid].$postfix]
3700 set compile_flags [concat $compile_flags {debug nowarnings quiet}]
3701
3702 gdb_produce_source $src $code
3703
3704 verbose "$name: compiling testfile $src" 2
3705 set lines [gdb_compile $src $obj $type $compile_flags]
3706
3707 file delete $src
3708
3709 if ![string match "" $lines] then {
3710 verbose "$name: compilation failed, returning 0" 2
3711 return 0
3712 }
3713 return 1
3714 }
3715
3716 # Compile the code in $code to a file based on $name, using the flags
3717 # $compile_flag as well as debug, nowarning and quiet.
3718 # Return 1 if code can be compiled
3719 # Delete all created files and objects.
3720
3721 proc gdb_can_simple_compile {name code {type object} {compile_flags ""}} {
3722 set ret [gdb_simple_compile $name $code $type $compile_flags temp_obj]
3723 file delete $temp_obj
3724 return $ret
3725 }
3726
3727 # Some targets need to always link a special object in. Save its path here.
3728 global gdb_saved_set_unbuffered_mode_obj
3729 set gdb_saved_set_unbuffered_mode_obj ""
3730
3731 # Compile source files specified by SOURCE into a binary of type TYPE at path
3732 # DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type
3733 # parameter and most options are passed directly to it.
3734 #
3735 # The type can be one of the following:
3736 #
3737 # - object: Compile into an object file.
3738 # - executable: Compile and link into an executable.
3739 # - preprocess: Preprocess the source files.
3740 # - assembly: Generate assembly listing.
3741 #
3742 # The following options are understood and processed by gdb_compile:
3743 #
3744 # - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific
3745 # quirks to be able to use shared libraries.
3746 # - shlib_load: Link with appropriate libraries to allow the test to
3747 # dynamically load libraries at runtime. For example, on Linux, this adds
3748 # -ldl so that the test can use dlopen.
3749 # - nowarnings: Inhibit all compiler warnings.
3750 # - pie: Force creation of PIE executables.
3751 # - nopie: Prevent creation of PIE executables.
3752 #
3753 # And here are some of the not too obscure options understood by DejaGnu that
3754 # influence the compilation:
3755 #
3756 # - additional_flags=flag: Add FLAG to the compiler flags.
3757 # - libs=library: Add LIBRARY to the libraries passed to the linker. The
3758 # argument can be a file, in which case it's added to the sources, or a
3759 # linker flag.
3760 # - ldflags=flag: Add FLAG to the linker flags.
3761 # - incdir=path: Add PATH to the searched include directories.
3762 # - libdir=path: Add PATH to the linker searched directories.
3763 # - ada, c++, f77: Compile the file as Ada, C++ or Fortran.
3764 # - debug: Build with debug information.
3765 # - optimize: Build with optimization.
3766
3767 proc gdb_compile {source dest type options} {
3768 global GDB_TESTCASE_OPTIONS
3769 global gdb_wrapper_file
3770 global gdb_wrapper_flags
3771 global gdb_wrapper_initialized
3772 global srcdir
3773 global objdir
3774 global gdb_saved_set_unbuffered_mode_obj
3775
3776 set outdir [file dirname $dest]
3777
3778 # Add platform-specific options if a shared library was specified using
3779 # "shlib=librarypath" in OPTIONS.
3780 set new_options {}
3781 if {[lsearch -exact $options rust] != -1} {
3782 # -fdiagnostics-color is not a rustcc option.
3783 } else {
3784 set new_options [universal_compile_options]
3785 }
3786
3787 # Place (and look for) Fortran `.mod` files in the output
3788 # directory for this specific test.
3789 if {[lsearch -exact $options f77] != -1 \
3790 || [lsearch -exact $options f90] != -1 } {
3791 # Fortran compile.
3792 set mod_path [standard_output_file ""]
3793 lappend new_options "additional_flags=-J${mod_path}"
3794 }
3795
3796 set shlib_found 0
3797 set shlib_load 0
3798 set getting_compiler_info 0
3799 foreach opt $options {
3800 if {[regexp {^shlib=(.*)} $opt dummy_var shlib_name]
3801 && $type == "executable"} {
3802 if [test_compiler_info "xlc-*"] {
3803 # IBM xlc compiler doesn't accept shared library named other
3804 # than .so: use "-Wl," to bypass this
3805 lappend source "-Wl,$shlib_name"
3806 } elseif { ([istarget "*-*-mingw*"]
3807 || [istarget *-*-cygwin*]
3808 || [istarget *-*-pe*])} {
3809 lappend source "${shlib_name}.a"
3810 } else {
3811 lappend source $shlib_name
3812 }
3813 if { $shlib_found == 0 } {
3814 set shlib_found 1
3815 if { ([istarget "*-*-mingw*"]
3816 || [istarget *-*-cygwin*]) } {
3817 lappend new_options "additional_flags=-Wl,--enable-auto-import"
3818 }
3819 if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
3820 # Undo debian's change in the default.
3821 # Put it at the front to not override any user-provided
3822 # value, and to make sure it appears in front of all the
3823 # shlibs!
3824 lappend new_options "early_flags=-Wl,--no-as-needed"
3825 }
3826 }
3827 } elseif { $opt == "shlib_load" && $type == "executable" } {
3828 set shlib_load 1
3829 } elseif { $opt == "getting_compiler_info" } {
3830 # If this is set, calling test_compiler_info will cause recursion.
3831 set getting_compiler_info 1
3832 } else {
3833 lappend new_options $opt
3834 }
3835 }
3836
3837 # Ensure stack protector is disabled for GCC, as this causes problems with
3838 # DWARF line numbering.
3839 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88432
3840 # This option defaults to on for Debian/Ubuntu.
3841 if { $getting_compiler_info == 0
3842 && [test_compiler_info {gcc-*-*}]
3843 && !([test_compiler_info {gcc-[0-3]-*}]
3844 || [test_compiler_info {gcc-4-0-*}])
3845 && [lsearch -exact $options rust] == -1} {
3846 # Put it at the front to not override any user-provided value.
3847 lappend new_options "early_flags=-fno-stack-protector"
3848 }
3849
3850 # Because we link with libraries using their basename, we may need
3851 # (depending on the platform) to set a special rpath value, to allow
3852 # the executable to find the libraries it depends on.
3853 if { $shlib_load || $shlib_found } {
3854 if { ([istarget "*-*-mingw*"]
3855 || [istarget *-*-cygwin*]
3856 || [istarget *-*-pe*]) } {
3857 # Do not need anything.
3858 } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } {
3859 lappend new_options "ldflags=-Wl,-rpath,${outdir}"
3860 } elseif { [istarget arm*-*-symbianelf*] } {
3861 if { $shlib_load } {
3862 lappend new_options "libs=-ldl"
3863 }
3864 } else {
3865 if { $shlib_load } {
3866 lappend new_options "libs=-ldl"
3867 }
3868 lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN"
3869 }
3870 }
3871 set options $new_options
3872
3873 if [info exists GDB_TESTCASE_OPTIONS] {
3874 lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"
3875 }
3876 verbose "options are $options"
3877 verbose "source is $source $dest $type $options"
3878
3879 gdb_wrapper_init
3880
3881 if {[target_info exists needs_status_wrapper] && \
3882 [target_info needs_status_wrapper] != "0" && \
3883 [info exists gdb_wrapper_file]} {
3884 lappend options "libs=${gdb_wrapper_file}"
3885 lappend options "ldflags=${gdb_wrapper_flags}"
3886 }
3887
3888 # Replace the "nowarnings" option with the appropriate additional_flags
3889 # to disable compiler warnings.
3890 set nowarnings [lsearch -exact $options nowarnings]
3891 if {$nowarnings != -1} {
3892 if [target_info exists gdb,nowarnings_flag] {
3893 set flag "additional_flags=[target_info gdb,nowarnings_flag]"
3894 } else {
3895 set flag "additional_flags=-w"
3896 }
3897 set options [lreplace $options $nowarnings $nowarnings $flag]
3898 }
3899
3900 # Replace the "pie" option with the appropriate compiler and linker flags
3901 # to enable PIE executables.
3902 set pie [lsearch -exact $options pie]
3903 if {$pie != -1} {
3904 if [target_info exists gdb,pie_flag] {
3905 set flag "additional_flags=[target_info gdb,pie_flag]"
3906 } else {
3907 # For safety, use fPIE rather than fpie. On AArch64, m68k, PowerPC
3908 # and SPARC, fpie can cause compile errors due to the GOT exceeding
3909 # a maximum size. On other architectures the two flags are
3910 # identical (see the GCC manual). Note Debian9 and Ubuntu16.10
3911 # onwards default GCC to using fPIE. If you do require fpie, then
3912 # it can be set using the pie_flag.
3913 set flag "additional_flags=-fPIE"
3914 }
3915 set options [lreplace $options $pie $pie $flag]
3916
3917 if [target_info exists gdb,pie_ldflag] {
3918 set flag "ldflags=[target_info gdb,pie_ldflag]"
3919 } else {
3920 set flag "ldflags=-pie"
3921 }
3922 lappend options "$flag"
3923 }
3924
3925 # Replace the "nopie" option with the appropriate linker flag to disable
3926 # PIE executables. There are no compiler flags for this option.
3927 set nopie [lsearch -exact $options nopie]
3928 if {$nopie != -1} {
3929 if [target_info exists gdb,nopie_flag] {
3930 set flag "ldflags=[target_info gdb,nopie_flag]"
3931 } else {
3932 set flag "ldflags=-no-pie"
3933 }
3934 set options [lreplace $options $nopie $nopie $flag]
3935 }
3936
3937 if { $type == "executable" } {
3938 if { ([istarget "*-*-mingw*"]
3939 || [istarget "*-*-*djgpp"]
3940 || [istarget "*-*-cygwin*"])} {
3941 # Force output to unbuffered mode, by linking in an object file
3942 # with a global contructor that calls setvbuf.
3943 #
3944 # Compile the special object separately for two reasons:
3945 # 1) Insulate it from $options.
3946 # 2) Avoid compiling it for every gdb_compile invocation,
3947 # which is time consuming, especially if we're remote
3948 # host testing.
3949 #
3950 if { $gdb_saved_set_unbuffered_mode_obj == "" } {
3951 verbose "compiling gdb_saved_set_unbuffered_obj"
3952 set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c
3953 set unbuf_obj ${objdir}/set_unbuffered_mode.o
3954
3955 set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}]
3956 if { $result != "" } {
3957 return $result
3958 }
3959 if {[is_remote host]} {
3960 set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o
3961 } else {
3962 set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o
3963 }
3964 # Link a copy of the output object, because the
3965 # original may be automatically deleted.
3966 remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj
3967 } else {
3968 verbose "gdb_saved_set_unbuffered_obj already compiled"
3969 }
3970
3971 # Rely on the internal knowledge that the global ctors are ran in
3972 # reverse link order. In that case, we can use ldflags to
3973 # avoid copying the object file to the host multiple
3974 # times.
3975 # This object can only be added if standard libraries are
3976 # used. Thus, we need to disable it if -nostdlib option is used
3977 if {[lsearch -regexp $options "-nostdlib"] < 0 } {
3978 lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
3979 }
3980 }
3981 }
3982
3983 set result [target_compile $source $dest $type $options]
3984
3985 # Prune uninteresting compiler (and linker) output.
3986 regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result
3987
3988 regsub "\[\r\n\]*$" "$result" "" result
3989 regsub "^\[\r\n\]*" "$result" "" result
3990
3991 if { $type == "executable" && $result == "" \
3992 && ($nopie != -1 || $pie != -1) } {
3993 set is_pie [exec_is_pie "$dest"]
3994 if { $nopie != -1 && $is_pie == 1 } {
3995 set result "nopie failed to prevent PIE executable"
3996 } elseif { $pie != -1 && $is_pie == 0 } {
3997 set result "pie failed to generate PIE executable"
3998 }
3999 }
4000
4001 if {[lsearch $options quiet] < 0} {
4002 # We shall update this on a per language basis, to avoid
4003 # changing the entire testsuite in one go.
4004 if {[lsearch $options f77] >= 0} {
4005 gdb_compile_test $source $result
4006 } elseif { $result != "" } {
4007 clone_output "gdb compile failed, $result"
4008 }
4009 }
4010 return $result
4011 }
4012
4013
4014 # This is just like gdb_compile, above, except that it tries compiling
4015 # against several different thread libraries, to see which one this
4016 # system has.
4017 proc gdb_compile_pthreads {source dest type options} {
4018 if {$type != "executable"} {
4019 return [gdb_compile $source $dest $type $options]
4020 }
4021 set built_binfile 0
4022 set why_msg "unrecognized error"
4023 foreach lib {-lpthreads -lpthread -lthread ""} {
4024 # This kind of wipes out whatever libs the caller may have
4025 # set. Or maybe theirs will override ours. How infelicitous.
4026 set options_with_lib [concat $options [list libs=$lib quiet]]
4027 set ccout [gdb_compile $source $dest $type $options_with_lib]
4028 switch -regexp -- $ccout {
4029 ".*no posix threads support.*" {
4030 set why_msg "missing threads include file"
4031 break
4032 }
4033 ".*cannot open -lpthread.*" {
4034 set why_msg "missing runtime threads library"
4035 }
4036 ".*Can't find library for -lpthread.*" {
4037 set why_msg "missing runtime threads library"
4038 }
4039 {^$} {
4040 pass "successfully compiled posix threads test case"
4041 set built_binfile 1
4042 break
4043 }
4044 }
4045 }
4046 if {!$built_binfile} {
4047 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4048 return -1
4049 }
4050 }
4051
4052 # Build a shared library from SOURCES.
4053
4054 proc gdb_compile_shlib {sources dest options} {
4055 set obj_options $options
4056
4057 set info_options ""
4058 if { [lsearch -exact $options "c++"] >= 0 } {
4059 set info_options "c++"
4060 }
4061 if [get_compiler_info ${info_options}] {
4062 return -1
4063 }
4064
4065 switch -glob [test_compiler_info] {
4066 "xlc-*" {
4067 lappend obj_options "additional_flags=-qpic"
4068 }
4069 "clang-*" {
4070 if { !([istarget "*-*-cygwin*"]
4071 || [istarget "*-*-mingw*"]) } {
4072 lappend obj_options "additional_flags=-fpic"
4073 }
4074 }
4075 "gcc-*" {
4076 if { !([istarget "powerpc*-*-aix*"]
4077 || [istarget "rs6000*-*-aix*"]
4078 || [istarget "*-*-cygwin*"]
4079 || [istarget "*-*-mingw*"]
4080 || [istarget "*-*-pe*"]) } {
4081 lappend obj_options "additional_flags=-fpic"
4082 }
4083 }
4084 "icc-*" {
4085 lappend obj_options "additional_flags=-fpic"
4086 }
4087 default {
4088 # don't know what the compiler is...
4089 }
4090 }
4091
4092 set outdir [file dirname $dest]
4093 set objects ""
4094 foreach source $sources {
4095 set sourcebase [file tail $source]
4096 if {[file extension $source] == ".o"} {
4097 # Already a .o file.
4098 lappend objects $source
4099 } elseif {[gdb_compile $source "${outdir}/${sourcebase}.o" object \
4100 $obj_options] != ""} {
4101 return -1
4102 } else {
4103 lappend objects ${outdir}/${sourcebase}.o
4104 }
4105 }
4106
4107 set link_options $options
4108 if [test_compiler_info "xlc-*"] {
4109 lappend link_options "additional_flags=-qmkshrobj"
4110 } else {
4111 lappend link_options "additional_flags=-shared"
4112
4113 if { ([istarget "*-*-mingw*"]
4114 || [istarget *-*-cygwin*]
4115 || [istarget *-*-pe*]) } {
4116 if { [is_remote host] } {
4117 set name [file tail ${dest}]
4118 } else {
4119 set name ${dest}
4120 }
4121 lappend link_options "additional_flags=-Wl,--out-implib,${name}.a"
4122 } else {
4123 # Set the soname of the library. This causes the linker on ELF
4124 # systems to create the DT_NEEDED entry in the executable referring
4125 # to the soname of the library, and not its absolute path. This
4126 # (using the absolute path) would be problem when testing on a
4127 # remote target.
4128 #
4129 # In conjunction with setting the soname, we add the special
4130 # rpath=$ORIGIN value when building the executable, so that it's
4131 # able to find the library in its own directory.
4132 set destbase [file tail $dest]
4133 lappend link_options "additional_flags=-Wl,-soname,$destbase"
4134 }
4135 }
4136 if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} {
4137 return -1
4138 }
4139 if { [is_remote host]
4140 && ([istarget "*-*-mingw*"]
4141 || [istarget *-*-cygwin*]
4142 || [istarget *-*-pe*]) } {
4143 set dest_tail_name [file tail ${dest}]
4144 remote_upload host $dest_tail_name.a ${dest}.a
4145 remote_file host delete $dest_tail_name.a
4146 }
4147
4148 return ""
4149 }
4150
4151 # This is just like gdb_compile_shlib, above, except that it tries compiling
4152 # against several different thread libraries, to see which one this
4153 # system has.
4154 proc gdb_compile_shlib_pthreads {sources dest options} {
4155 set built_binfile 0
4156 set why_msg "unrecognized error"
4157 foreach lib {-lpthreads -lpthread -lthread ""} {
4158 # This kind of wipes out whatever libs the caller may have
4159 # set. Or maybe theirs will override ours. How infelicitous.
4160 set options_with_lib [concat $options [list libs=$lib quiet]]
4161 set ccout [gdb_compile_shlib $sources $dest $options_with_lib]
4162 switch -regexp -- $ccout {
4163 ".*no posix threads support.*" {
4164 set why_msg "missing threads include file"
4165 break
4166 }
4167 ".*cannot open -lpthread.*" {
4168 set why_msg "missing runtime threads library"
4169 }
4170 ".*Can't find library for -lpthread.*" {
4171 set why_msg "missing runtime threads library"
4172 }
4173 {^$} {
4174 pass "successfully compiled posix threads test case"
4175 set built_binfile 1
4176 break
4177 }
4178 }
4179 }
4180 if {!$built_binfile} {
4181 unsupported "couldn't compile $sources: ${why_msg}"
4182 return -1
4183 }
4184 }
4185
4186 # This is just like gdb_compile_pthreads, above, except that we always add the
4187 # objc library for compiling Objective-C programs
4188 proc gdb_compile_objc {source dest type options} {
4189 set built_binfile 0
4190 set why_msg "unrecognized error"
4191 foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} {
4192 # This kind of wipes out whatever libs the caller may have
4193 # set. Or maybe theirs will override ours. How infelicitous.
4194 if { $lib == "solaris" } {
4195 set lib "-lpthread -lposix4"
4196 }
4197 if { $lib != "-lobjc" } {
4198 set lib "-lobjc $lib"
4199 }
4200 set options_with_lib [concat $options [list libs=$lib quiet]]
4201 set ccout [gdb_compile $source $dest $type $options_with_lib]
4202 switch -regexp -- $ccout {
4203 ".*no posix threads support.*" {
4204 set why_msg "missing threads include file"
4205 break
4206 }
4207 ".*cannot open -lpthread.*" {
4208 set why_msg "missing runtime threads library"
4209 }
4210 ".*Can't find library for -lpthread.*" {
4211 set why_msg "missing runtime threads library"
4212 }
4213 {^$} {
4214 pass "successfully compiled objc with posix threads test case"
4215 set built_binfile 1
4216 break
4217 }
4218 }
4219 }
4220 if {!$built_binfile} {
4221 unsupported "couldn't compile [file tail $source]: ${why_msg}"
4222 return -1
4223 }
4224 }
4225
4226 # Build an OpenMP program from SOURCE. See prefatory comment for
4227 # gdb_compile, above, for discussion of the parameters to this proc.
4228
4229 proc gdb_compile_openmp {source dest type options} {
4230 lappend options "additional_flags=-fopenmp"
4231 return [gdb_compile $source $dest $type $options]
4232 }
4233
4234 # Send a command to GDB.
4235 # For options for TYPE see gdb_stdin_log_write
4236
4237 proc send_gdb { string {type standard}} {
4238 global suppress_flag
4239 if { $suppress_flag } {
4240 return "suppressed"
4241 }
4242 gdb_stdin_log_write $string $type
4243 return [remote_send host "$string"]
4244 }
4245
4246 # Send STRING to the inferior's terminal.
4247
4248 proc send_inferior { string } {
4249 global inferior_spawn_id
4250
4251 if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} {
4252 return "$errorInfo"
4253 } else {
4254 return ""
4255 }
4256 }
4257
4258 #
4259 #
4260
4261 proc gdb_expect { args } {
4262 if { [llength $args] == 2 && [lindex $args 0] != "-re" } {
4263 set atimeout [lindex $args 0]
4264 set expcode [list [lindex $args 1]]
4265 } else {
4266 set expcode $args
4267 }
4268
4269 # A timeout argument takes precedence, otherwise of all the timeouts
4270 # select the largest.
4271 if [info exists atimeout] {
4272 set tmt $atimeout
4273 } else {
4274 set tmt [get_largest_timeout]
4275 }
4276
4277 global suppress_flag
4278 global remote_suppress_flag
4279 if [info exists remote_suppress_flag] {
4280 set old_val $remote_suppress_flag
4281 }
4282 if [info exists suppress_flag] {
4283 if { $suppress_flag } {
4284 set remote_suppress_flag 1
4285 }
4286 }
4287 set code [catch \
4288 {uplevel remote_expect host $tmt $expcode} string]
4289 if [info exists old_val] {
4290 set remote_suppress_flag $old_val
4291 } else {
4292 if [info exists remote_suppress_flag] {
4293 unset remote_suppress_flag
4294 }
4295 }
4296
4297 if {$code == 1} {
4298 global errorInfo errorCode
4299
4300 return -code error -errorinfo $errorInfo -errorcode $errorCode $string
4301 } else {
4302 return -code $code $string
4303 }
4304 }
4305
4306 # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs
4307 #
4308 # Check for long sequence of output by parts.
4309 # TEST: is the test message to be printed with the test success/fail.
4310 # SENTINEL: Is the terminal pattern indicating that output has finished.
4311 # LIST: is the sequence of outputs to match.
4312 # If the sentinel is recognized early, it is considered an error.
4313 #
4314 # Returns:
4315 # 1 if the test failed,
4316 # 0 if the test passes,
4317 # -1 if there was an internal error.
4318
4319 proc gdb_expect_list {test sentinel list} {
4320 global gdb_prompt
4321 global suppress_flag
4322 set index 0
4323 set ok 1
4324 if { $suppress_flag } {
4325 set ok 0
4326 unresolved "${test}"
4327 }
4328 while { ${index} < [llength ${list}] } {
4329 set pattern [lindex ${list} ${index}]
4330 set index [expr ${index} + 1]
4331 verbose -log "gdb_expect_list pattern: /$pattern/" 2
4332 if { ${index} == [llength ${list}] } {
4333 if { ${ok} } {
4334 gdb_expect {
4335 -re "${pattern}${sentinel}" {
4336 # pass "${test}, pattern ${index} + sentinel"
4337 }
4338 -re "${sentinel}" {
4339 fail "${test} (pattern ${index} + sentinel)"
4340 set ok 0
4341 }
4342 -re ".*A problem internal to GDB has been detected" {
4343 fail "${test} (GDB internal error)"
4344 set ok 0
4345 gdb_internal_error_resync
4346 }
4347 timeout {
4348 fail "${test} (pattern ${index} + sentinel) (timeout)"
4349 set ok 0
4350 }
4351 }
4352 } else {
4353 # unresolved "${test}, pattern ${index} + sentinel"
4354 }
4355 } else {
4356 if { ${ok} } {
4357 gdb_expect {
4358 -re "${pattern}" {
4359 # pass "${test}, pattern ${index}"
4360 }
4361 -re "${sentinel}" {
4362 fail "${test} (pattern ${index})"
4363 set ok 0
4364 }
4365 -re ".*A problem internal to GDB has been detected" {
4366 fail "${test} (GDB internal error)"
4367 set ok 0
4368 gdb_internal_error_resync
4369 }
4370 timeout {
4371 fail "${test} (pattern ${index}) (timeout)"
4372 set ok 0
4373 }
4374 }
4375 } else {
4376 # unresolved "${test}, pattern ${index}"
4377 }
4378 }
4379 }
4380 if { ${ok} } {
4381 pass "${test}"
4382 return 0
4383 } else {
4384 return 1
4385 }
4386 }
4387
4388 #
4389 #
4390 proc gdb_suppress_entire_file { reason } {
4391 global suppress_flag
4392
4393 warning "$reason\n"
4394 set suppress_flag -1
4395 }
4396
4397 #
4398 # Set suppress_flag, which will cause all subsequent calls to send_gdb and
4399 # gdb_expect to fail immediately (until the next call to
4400 # gdb_stop_suppressing_tests).
4401 #
4402 proc gdb_suppress_tests { args } {
4403 global suppress_flag
4404
4405 return; # fnf - disable pending review of results where
4406 # testsuite ran better without this
4407 incr suppress_flag
4408
4409 if { $suppress_flag == 1 } {
4410 if { [llength $args] > 0 } {
4411 warning "[lindex $args 0]\n"
4412 } else {
4413 warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n"
4414 }
4415 }
4416 }
4417
4418 #
4419 # Clear suppress_flag.
4420 #
4421 proc gdb_stop_suppressing_tests { } {
4422 global suppress_flag
4423
4424 if [info exists suppress_flag] {
4425 if { $suppress_flag > 0 } {
4426 set suppress_flag 0
4427 clone_output "Tests restarted.\n"
4428 }
4429 } else {
4430 set suppress_flag 0
4431 }
4432 }
4433
4434 proc gdb_clear_suppressed { } {
4435 global suppress_flag
4436
4437 set suppress_flag 0
4438 }
4439
4440 # Spawn the gdb process.
4441 #
4442 # This doesn't expect any output or do any other initialization,
4443 # leaving those to the caller.
4444 #
4445 # Overridable function -- you can override this function in your
4446 # baseboard file.
4447
4448 proc gdb_spawn { } {
4449 default_gdb_spawn
4450 }
4451
4452 # Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
4453
4454 proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
4455 global GDBFLAGS
4456
4457 set saved_gdbflags $GDBFLAGS
4458
4459 if {$GDBFLAGS != ""} {
4460 append GDBFLAGS " "
4461 }
4462 append GDBFLAGS $cmdline_flags
4463
4464 set res [gdb_spawn]
4465
4466 set GDBFLAGS $saved_gdbflags
4467
4468 return $res
4469 }
4470
4471 # Start gdb running, wait for prompt, and disable the pagers.
4472
4473 # Overridable function -- you can override this function in your
4474 # baseboard file.
4475
4476 proc gdb_start { } {
4477 default_gdb_start
4478 }
4479
4480 proc gdb_exit { } {
4481 catch default_gdb_exit
4482 }
4483
4484 # Return true if we can spawn a program on the target and attach to
4485 # it.
4486
4487 proc can_spawn_for_attach { } {
4488 # We use exp_pid to get the inferior's pid, assuming that gives
4489 # back the pid of the program. On remote boards, that would give
4490 # us instead the PID of e.g., the ssh client, etc.
4491 if [is_remote target] then {
4492 return 0
4493 }
4494
4495 # The "attach" command doesn't make sense when the target is
4496 # stub-like, where GDB finds the program already started on
4497 # initial connection.
4498 if {[target_info exists use_gdb_stub]} {
4499 return 0
4500 }
4501
4502 # Assume yes.
4503 return 1
4504 }
4505
4506 # Kill a progress previously started with spawn_wait_for_attach, and
4507 # reap its wait status. PROC_SPAWN_ID is the spawn id associated with
4508 # the process.
4509
4510 proc kill_wait_spawned_process { proc_spawn_id } {
4511 set pid [exp_pid -i $proc_spawn_id]
4512
4513 verbose -log "killing ${pid}"
4514 remote_exec build "kill -9 ${pid}"
4515
4516 verbose -log "closing ${proc_spawn_id}"
4517 catch "close -i $proc_spawn_id"
4518 verbose -log "waiting for ${proc_spawn_id}"
4519
4520 # If somehow GDB ends up still attached to the process here, a
4521 # blocking wait hangs until gdb is killed (or until gdb / the
4522 # ptracer reaps the exit status too, but that won't happen because
4523 # something went wrong.) Passing -nowait makes expect tell Tcl to
4524 # wait for the PID in the background. That's fine because we
4525 # don't care about the exit status. */
4526 wait -nowait -i $proc_spawn_id
4527 }
4528
4529 # Returns the process id corresponding to the given spawn id.
4530
4531 proc spawn_id_get_pid { spawn_id } {
4532 set testpid [exp_pid -i $spawn_id]
4533
4534 if { [istarget "*-*-cygwin*"] } {
4535 # testpid is the Cygwin PID, GDB uses the Windows PID, which
4536 # might be different due to the way fork/exec works.
4537 set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
4538 }
4539
4540 return $testpid
4541 }
4542
4543 # Start a set of programs running and then wait for a bit, to be sure
4544 # that they can be attached to. Return a list of processes spawn IDs,
4545 # one element for each process spawned. It's a test error to call
4546 # this when [can_spawn_for_attach] is false.
4547
4548 proc spawn_wait_for_attach { executable_list } {
4549 set spawn_id_list {}
4550
4551 if ![can_spawn_for_attach] {
4552 # The caller should have checked can_spawn_for_attach itself
4553 # before getting here.
4554 error "can't spawn for attach with this target/board"
4555 }
4556
4557 foreach {executable} $executable_list {
4558 # Note we use Expect's spawn, not Tcl's exec, because with
4559 # spawn we control when to wait for/reap the process. That
4560 # allows killing the process by PID without being subject to
4561 # pid-reuse races.
4562 lappend spawn_id_list [remote_spawn target $executable]
4563 }
4564
4565 sleep 2
4566
4567 return $spawn_id_list
4568 }
4569
4570 #
4571 # gdb_load_cmd -- load a file into the debugger.
4572 # ARGS - additional args to load command.
4573 # return a -1 if anything goes wrong.
4574 #
4575 proc gdb_load_cmd { args } {
4576 global gdb_prompt
4577
4578 if [target_info exists gdb_load_timeout] {
4579 set loadtimeout [target_info gdb_load_timeout]
4580 } else {
4581 set loadtimeout 1600
4582 }
4583 send_gdb "load $args\n"
4584 verbose "Timeout is now $loadtimeout seconds" 2
4585 gdb_expect $loadtimeout {
4586 -re "Loading section\[^\r\]*\r\n" {
4587 exp_continue
4588 }
4589 -re "Start address\[\r\]*\r\n" {
4590 exp_continue
4591 }
4592 -re "Transfer rate\[\r\]*\r\n" {
4593 exp_continue
4594 }
4595 -re "Memory access error\[^\r\]*\r\n" {
4596 perror "Failed to load program"
4597 return -1
4598 }
4599 -re "$gdb_prompt $" {
4600 return 0
4601 }
4602 -re "(.*)\r\n$gdb_prompt " {
4603 perror "Unexpected reponse from 'load' -- $expect_out(1,string)"
4604 return -1
4605 }
4606 timeout {
4607 perror "Timed out trying to load $args."
4608 return -1
4609 }
4610 }
4611 return -1
4612 }
4613
4614 # Invoke "gcore". CORE is the name of the core file to write. TEST
4615 # is the name of the test case. This will return 1 if the core file
4616 # was created, 0 otherwise. If this fails to make a core file because
4617 # this configuration of gdb does not support making core files, it
4618 # will call "unsupported", not "fail". However, if this fails to make
4619 # a core file for some other reason, then it will call "fail".
4620
4621 proc gdb_gcore_cmd {core test} {
4622 global gdb_prompt
4623
4624 set result 0
4625 gdb_test_multiple "gcore $core" $test {
4626 -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" {
4627 pass $test
4628 set result 1
4629 }
4630 -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" {
4631 unsupported $test
4632 }
4633 }
4634
4635 return $result
4636 }
4637
4638 # Load core file CORE. TEST is the name of the test case.
4639 # This will record a pass/fail for loading the core file.
4640 # Returns:
4641 # 1 - core file is successfully loaded
4642 # 0 - core file loaded but has a non fatal error
4643 # -1 - core file failed to load
4644
4645 proc gdb_core_cmd { core test } {
4646 global gdb_prompt
4647
4648 gdb_test_multiple "core $core" "$test" {
4649 -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
4650 exp_continue
4651 }
4652 -re " is not a core dump:.*\r\n$gdb_prompt $" {
4653 fail "$test (bad file format)"
4654 return -1
4655 }
4656 -re -wrap "[string_to_regexp $core]: No such file or directory.*" {
4657 fail "$test (file not found)"
4658 return -1
4659 }
4660 -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
4661 fail "$test (incomplete note section)"
4662 return 0
4663 }
4664 -re "Core was generated by .*\r\n$gdb_prompt $" {
4665 pass "$test"
4666 return 1
4667 }
4668 -re ".*$gdb_prompt $" {
4669 fail "$test"
4670 return -1
4671 }
4672 timeout {
4673 fail "$test (timeout)"
4674 return -1
4675 }
4676 }
4677 fail "unsupported output from 'core' command"
4678 return -1
4679 }
4680
4681 # Return the filename to download to the target and load on the target
4682 # for this shared library. Normally just LIBNAME, unless shared libraries
4683 # for this target have separate link and load images.
4684
4685 proc shlib_target_file { libname } {
4686 return $libname
4687 }
4688
4689 # Return the filename GDB will load symbols from when debugging this
4690 # shared library. Normally just LIBNAME, unless shared libraries for
4691 # this target have separate link and load images.
4692
4693 proc shlib_symbol_file { libname } {
4694 return $libname
4695 }
4696
4697 # Return the filename to download to the target and load for this
4698 # executable. Normally just BINFILE unless it is renamed to something
4699 # else for this target.
4700
4701 proc exec_target_file { binfile } {
4702 return $binfile
4703 }
4704
4705 # Return the filename GDB will load symbols from when debugging this
4706 # executable. Normally just BINFILE unless executables for this target
4707 # have separate files for symbols.
4708
4709 proc exec_symbol_file { binfile } {
4710 return $binfile
4711 }
4712
4713 # Rename the executable file. Normally this is just BINFILE1 being renamed
4714 # to BINFILE2, but some targets require multiple binary files.
4715 proc gdb_rename_execfile { binfile1 binfile2 } {
4716 file rename -force [exec_target_file ${binfile1}] \
4717 [exec_target_file ${binfile2}]
4718 if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } {
4719 file rename -force [exec_symbol_file ${binfile1}] \
4720 [exec_symbol_file ${binfile2}]
4721 }
4722 }
4723
4724 # "Touch" the executable file to update the date. Normally this is just
4725 # BINFILE, but some targets require multiple files.
4726 proc gdb_touch_execfile { binfile } {
4727 set time [clock seconds]
4728 file mtime [exec_target_file ${binfile}] $time
4729 if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } {
4730 file mtime [exec_symbol_file ${binfile}] $time
4731 }
4732 }
4733
4734 # Like remote_download but provides a gdb-specific behavior.
4735 #
4736 # If the destination board is remote, the local file FROMFILE is transferred as
4737 # usual with remote_download to TOFILE on the remote board. The destination
4738 # filename is added to the CLEANFILES global, so it can be cleaned up at the
4739 # end of the test.
4740 #
4741 # If the destination board is local, the destination path TOFILE is passed
4742 # through standard_output_file, and FROMFILE is copied there.
4743 #
4744 # In both cases, if TOFILE is omitted, it defaults to the [file tail] of
4745 # FROMFILE.
4746
4747 proc gdb_remote_download {dest fromfile {tofile {}}} {
4748 # If TOFILE is not given, default to the same filename as FROMFILE.
4749 if {[string length $tofile] == 0} {
4750 set tofile [file tail $fromfile]
4751 }
4752
4753 if {[is_remote $dest]} {
4754 # When the DEST is remote, we simply send the file to DEST.
4755 global cleanfiles
4756
4757 set destname [remote_download $dest $fromfile $tofile]
4758 lappend cleanfiles $destname
4759
4760 return $destname
4761 } else {
4762 # When the DEST is local, we copy the file to the test directory (where
4763 # the executable is).
4764 #
4765 # Note that we pass TOFILE through standard_output_file, regardless of
4766 # whether it is absolute or relative, because we don't want the tests
4767 # to be able to write outside their standard output directory.
4768
4769 set tofile [standard_output_file $tofile]
4770
4771 file copy -force $fromfile $tofile
4772
4773 return $tofile
4774 }
4775 }
4776
4777 # gdb_load_shlib LIB...
4778 #
4779 # Copy the listed library to the target.
4780
4781 proc gdb_load_shlib { file } {
4782 global gdb_spawn_id
4783
4784 if ![info exists gdb_spawn_id] {
4785 perror "gdb_load_shlib: GDB is not running"
4786 }
4787
4788 set dest [gdb_remote_download target [shlib_target_file $file]]
4789
4790 if {[is_remote target]} {
4791 # If the target is remote, we need to tell gdb where to find the
4792 # libraries.
4793 #
4794 # We could set this even when not testing remotely, but a user
4795 # generally won't set it unless necessary. In order to make the tests
4796 # more like the real-life scenarios, we don't set it for local testing.
4797 gdb_test "set solib-search-path [file dirname $file]" "" ""
4798 }
4799
4800 return $dest
4801 }
4802
4803 #
4804 # gdb_load -- load a file into the debugger. Specifying no file
4805 # defaults to the executable currently being debugged.
4806 # The return value is 0 for success, -1 for failure.
4807 # Many files in config/*.exp override this procedure.
4808 #
4809 proc gdb_load { arg } {
4810 if { $arg != "" } {
4811 return [gdb_file_cmd $arg]
4812 }
4813 return 0
4814 }
4815
4816 # gdb_reload -- load a file into the target. Called before "running",
4817 # either the first time or after already starting the program once,
4818 # for remote targets. Most files that override gdb_load should now
4819 # override this instead.
4820
4821 proc gdb_reload { } {
4822 # For the benefit of existing configurations, default to gdb_load.
4823 # Specifying no file defaults to the executable currently being
4824 # debugged.
4825 return [gdb_load ""]
4826 }
4827
4828 proc gdb_continue { function } {
4829 global decimal
4830
4831 return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]
4832 }
4833
4834 proc default_gdb_init { test_file_name } {
4835 global gdb_wrapper_initialized
4836 global gdb_wrapper_target
4837 global gdb_test_file_name
4838 global cleanfiles
4839 global pf_prefix
4840
4841 set cleanfiles {}
4842
4843 gdb_clear_suppressed
4844
4845 set gdb_test_file_name [file rootname [file tail $test_file_name]]
4846
4847 # Make sure that the wrapper is rebuilt
4848 # with the appropriate multilib option.
4849 if { $gdb_wrapper_target != [current_target_name] } {
4850 set gdb_wrapper_initialized 0
4851 }
4852
4853 # Unlike most tests, we have a small number of tests that generate
4854 # a very large amount of output. We therefore increase the expect
4855 # buffer size to be able to contain the entire test output. This
4856 # is especially needed by gdb.base/info-macros.exp.
4857 match_max -d 65536
4858 # Also set this value for the currently running GDB.
4859 match_max [match_max -d]
4860
4861 # We want to add the name of the TCL testcase to the PASS/FAIL messages.
4862 set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:"
4863
4864 global gdb_prompt
4865 if [target_info exists gdb_prompt] {
4866 set gdb_prompt [target_info gdb_prompt]
4867 } else {
4868 set gdb_prompt "\\(gdb\\)"
4869 }
4870 global use_gdb_stub
4871 if [info exists use_gdb_stub] {
4872 unset use_gdb_stub
4873 }
4874 }
4875
4876 # Return a path using GDB_PARALLEL.
4877 # ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL".
4878 # GDB_PARALLEL must be defined, the caller must check.
4879 #
4880 # The default value for GDB_PARALLEL is, canonically, ".".
4881 # The catch is that tests don't expect an additional "./" in file paths so
4882 # omit any directory for the default case.
4883 # GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark
4884 # its special handling.
4885
4886 proc make_gdb_parallel_path { args } {
4887 global GDB_PARALLEL objdir
4888 set joiner [list "file" "join" $objdir]
4889 if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } {
4890 lappend joiner $GDB_PARALLEL
4891 }
4892 set joiner [concat $joiner $args]
4893 return [eval $joiner]
4894 }
4895
4896 # Turn BASENAME into a full file name in the standard output
4897 # directory. It is ok if BASENAME is the empty string; in this case
4898 # the directory is returned.
4899
4900 proc standard_output_file {basename} {
4901 global objdir subdir gdb_test_file_name
4902
4903 set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name]
4904 file mkdir $dir
4905 # If running on MinGW, replace /c/foo with c:/foo
4906 if { [ishost *-*-mingw*] } {
4907 set dir [exec sh -c "cd ${dir} && pwd -W"]
4908 }
4909 return [file join $dir $basename]
4910 }
4911
4912 # Turn BASENAME into a full file name in the standard output directory. If
4913 # GDB has been launched more than once then append the count, starting with
4914 # a ".1" postfix.
4915
4916 proc standard_output_file_with_gdb_instance {basename} {
4917 global gdb_instances
4918 set count [expr $gdb_instances - 1 ]
4919
4920 if {$count == 0} {
4921 return [standard_output_file $basename]
4922 }
4923 return [standard_output_file ${basename}.${count}]
4924 }
4925
4926 # Return the name of a file in our standard temporary directory.
4927
4928 proc standard_temp_file {basename} {
4929 # Since a particular runtest invocation is only executing a single test
4930 # file at any given time, we can use the runtest pid to build the
4931 # path of the temp directory.
4932 set dir [make_gdb_parallel_path temp [pid]]
4933 file mkdir $dir
4934 return [file join $dir $basename]
4935 }
4936
4937 # Rename file A to file B, if B does not already exists. Otherwise, leave B
4938 # as is and delete A. Return 1 if rename happened.
4939
4940 proc tentative_rename { a b } {
4941 global errorInfo errorCode
4942 set code [catch {file rename -- $a $b} result]
4943 if { $code == 1 && [lindex $errorCode 0] == "POSIX" \
4944 && [lindex $errorCode 1] == "EEXIST" } {
4945 file delete $a
4946 return 0
4947 }
4948 if {$code == 1} {
4949 return -code error -errorinfo $errorInfo -errorcode $errorCode $result
4950 } elseif {$code > 1} {
4951 return -code $code $result
4952 }
4953 return 1
4954 }
4955
4956 # Create a file with name FILENAME and contents TXT in the cache directory.
4957 # If EXECUTABLE, mark the new file for execution.
4958
4959 proc cached_file { filename txt {executable 0}} {
4960 set filename [make_gdb_parallel_path cache $filename]
4961
4962 if { [file exists $filename] } {
4963 return $filename
4964 }
4965
4966 set dir [file dirname $filename]
4967 file mkdir $dir
4968
4969 set tmp_filename $filename.[pid]
4970 set fd [open $tmp_filename w]
4971 puts $fd $txt
4972 close $fd
4973
4974 if { $executable } {
4975 exec chmod +x $tmp_filename
4976 }
4977 tentative_rename $tmp_filename $filename
4978
4979 return $filename
4980 }
4981
4982 # Set 'testfile', 'srcfile', and 'binfile'.
4983 #
4984 # ARGS is a list of source file specifications.
4985 # Without any arguments, the .exp file's base name is used to
4986 # compute the source file name. The ".c" extension is added in this case.
4987 # If ARGS is not empty, each entry is a source file specification.
4988 # If the specification starts with a ".", it is treated as a suffix
4989 # to append to the .exp file's base name.
4990 # If the specification is the empty string, it is treated as if it
4991 # were ".c".
4992 # Otherwise it is a file name.
4993 # The first file in the list is used to set the 'srcfile' global.
4994 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
4995 #
4996 # Most tests should call this without arguments.
4997 #
4998 # If a completely different binary file name is needed, then it
4999 # should be handled in the .exp file with a suitable comment.
5000
5001 proc standard_testfile {args} {
5002 global gdb_test_file_name
5003 global subdir
5004 global gdb_test_file_last_vars
5005
5006 # Outputs.
5007 global testfile binfile
5008
5009 set testfile $gdb_test_file_name
5010 set binfile [standard_output_file ${testfile}]
5011
5012 if {[llength $args] == 0} {
5013 set args .c
5014 }
5015
5016 # Unset our previous output variables.
5017 # This can help catch hidden bugs.
5018 if {[info exists gdb_test_file_last_vars]} {
5019 foreach varname $gdb_test_file_last_vars {
5020 global $varname
5021 catch {unset $varname}
5022 }
5023 }
5024 # 'executable' is often set by tests.
5025 set gdb_test_file_last_vars {executable}
5026
5027 set suffix ""
5028 foreach arg $args {
5029 set varname srcfile$suffix
5030 global $varname
5031
5032 # Handle an extension.
5033 if {$arg == ""} {
5034 set arg $testfile.c
5035 } elseif {[string range $arg 0 0] == "."} {
5036 set arg $testfile$arg
5037 }
5038
5039 set $varname $arg
5040 lappend gdb_test_file_last_vars $varname
5041
5042 if {$suffix == ""} {
5043 set suffix 2
5044 } else {
5045 incr suffix
5046 }
5047 }
5048 }
5049
5050 # The default timeout used when testing GDB commands. We want to use
5051 # the same timeout as the default dejagnu timeout, unless the user has
5052 # already provided a specific value (probably through a site.exp file).
5053 global gdb_test_timeout
5054 if ![info exists gdb_test_timeout] {
5055 set gdb_test_timeout $timeout
5056 }
5057
5058 # A list of global variables that GDB testcases should not use.
5059 # We try to prevent their use by monitoring write accesses and raising
5060 # an error when that happens.
5061 set banned_variables { bug_id prms_id }
5062
5063 # A list of procedures that GDB testcases should not use.
5064 # We try to prevent their use by monitoring invocations and raising
5065 # an error when that happens.
5066 set banned_procedures { strace }
5067
5068 # gdb_init is called by runtest at start, but also by several
5069 # tests directly; gdb_finish is only called from within runtest after
5070 # each test source execution.
5071 # Placing several traces by repetitive calls to gdb_init leads
5072 # to problems, as only one trace is removed in gdb_finish.
5073 # To overcome this possible problem, we add a variable that records
5074 # if the banned variables and procedures are already traced.
5075 set banned_traced 0
5076
5077 proc gdb_init { test_file_name } {
5078 # Reset the timeout value to the default. This way, any testcase
5079 # that changes the timeout value without resetting it cannot affect
5080 # the timeout used in subsequent testcases.
5081 global gdb_test_timeout
5082 global timeout
5083 set timeout $gdb_test_timeout
5084
5085 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
5086 && [target_info exists gdb_reverse_timeout] } {
5087 set timeout [target_info gdb_reverse_timeout]
5088 }
5089
5090 # If GDB_INOTIFY is given, check for writes to '.'. This is a
5091 # debugging tool to help confirm that the test suite is
5092 # parallel-safe. You need "inotifywait" from the
5093 # inotify-tools package to use this.
5094 global GDB_INOTIFY inotify_pid
5095 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
5096 global outdir tool inotify_log_file
5097
5098 set exclusions {outputs temp gdb[.](log|sum) cache}
5099 set exclusion_re ([join $exclusions |])
5100
5101 set inotify_log_file [standard_temp_file inotify.out]
5102 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
5103 --exclude $exclusion_re \
5104 |& tee -a $outdir/$tool.log $inotify_log_file &]
5105
5106 # Wait for the watches; hopefully this is long enough.
5107 sleep 2
5108
5109 # Clear the log so that we don't emit a warning the first time
5110 # we check it.
5111 set fd [open $inotify_log_file w]
5112 close $fd
5113 }
5114
5115 # Block writes to all banned variables, and invocation of all
5116 # banned procedures...
5117 global banned_variables
5118 global banned_procedures
5119 global banned_traced
5120 if (!$banned_traced) {
5121 foreach banned_var $banned_variables {
5122 global "$banned_var"
5123 trace add variable "$banned_var" write error
5124 }
5125 foreach banned_proc $banned_procedures {
5126 global "$banned_proc"
5127 trace add execution "$banned_proc" enter error
5128 }
5129 set banned_traced 1
5130 }
5131
5132 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
5133 # messages as expected.
5134 setenv LC_ALL C
5135 setenv LC_CTYPE C
5136 setenv LANG C
5137
5138 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
5139 # the test results. Even if /dev/null doesn't exist on the particular
5140 # platform, the readline library will use the default setting just by
5141 # failing to open the file. OTOH, opening /dev/null successfully will
5142 # also result in the default settings being used since nothing will be
5143 # read from this file.
5144 setenv INPUTRC "/dev/null"
5145
5146 # This disables style output, which would interfere with many
5147 # tests.
5148 setenv TERM "dumb"
5149
5150 # Ensure that GDBHISTFILE and GDBHISTSIZE are removed from the
5151 # environment, we don't want these modifications to the history
5152 # settings.
5153 unset -nocomplain ::env(GDBHISTFILE)
5154 unset -nocomplain ::env(GDBHISTSIZE)
5155
5156 # Initialize GDB's pty with a fixed size, to make sure we avoid pagination
5157 # during startup. See "man expect" for details about stty_init.
5158 global stty_init
5159 set stty_init "rows 25 cols 80"
5160
5161 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
5162 # grep. Clear GREP_OPTIONS to make the behavior predictable,
5163 # especially having color output turned on can cause tests to fail.
5164 setenv GREP_OPTIONS ""
5165
5166 # Clear $gdbserver_reconnect_p.
5167 global gdbserver_reconnect_p
5168 set gdbserver_reconnect_p 1
5169 unset gdbserver_reconnect_p
5170
5171 # Clear $last_loaded_file
5172 global last_loaded_file
5173 unset -nocomplain last_loaded_file
5174
5175 # Reset GDB number of instances
5176 global gdb_instances
5177 set gdb_instances 0
5178
5179 return [default_gdb_init $test_file_name]
5180 }
5181
5182 proc gdb_finish { } {
5183 global gdbserver_reconnect_p
5184 global gdb_prompt
5185 global cleanfiles
5186
5187 # Exit first, so that the files are no longer in use.
5188 gdb_exit
5189
5190 if { [llength $cleanfiles] > 0 } {
5191 eval remote_file target delete $cleanfiles
5192 set cleanfiles {}
5193 }
5194
5195 # Unblock write access to the banned variables. Dejagnu typically
5196 # resets some of them between testcases.
5197 global banned_variables
5198 global banned_procedures
5199 global banned_traced
5200 if ($banned_traced) {
5201 foreach banned_var $banned_variables {
5202 global "$banned_var"
5203 trace remove variable "$banned_var" write error
5204 }
5205 foreach banned_proc $banned_procedures {
5206 global "$banned_proc"
5207 trace remove execution "$banned_proc" enter error
5208 }
5209 set banned_traced 0
5210 }
5211 }
5212
5213 global debug_format
5214 set debug_format "unknown"
5215
5216 # Run the gdb command "info source" and extract the debugging format
5217 # information from the output and save it in debug_format.
5218
5219 proc get_debug_format { } {
5220 global gdb_prompt
5221 global expect_out
5222 global debug_format
5223
5224 set debug_format "unknown"
5225 send_gdb "info source\n"
5226 gdb_expect 10 {
5227 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
5228 set debug_format $expect_out(1,string)
5229 verbose "debug format is $debug_format"
5230 return 1
5231 }
5232 -re "No current source file.\r\n$gdb_prompt $" {
5233 perror "get_debug_format used when no current source file"
5234 return 0
5235 }
5236 -re "$gdb_prompt $" {
5237 warning "couldn't check debug format (no valid response)."
5238 return 1
5239 }
5240 timeout {
5241 warning "couldn't check debug format (timeout)."
5242 return 1
5243 }
5244 }
5245 }
5246
5247 # Return true if FORMAT matches the debug format the current test was
5248 # compiled with. FORMAT is a shell-style globbing pattern; it can use
5249 # `*', `[...]', and so on.
5250 #
5251 # This function depends on variables set by `get_debug_format', above.
5252
5253 proc test_debug_format {format} {
5254 global debug_format
5255
5256 return [expr [string match $format $debug_format] != 0]
5257 }
5258
5259 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
5260 # COFF, stabs, etc). If that format matches the format that the
5261 # current test was compiled with, then the next test is expected to
5262 # fail for any target. Returns 1 if the next test or set of tests is
5263 # expected to fail, 0 otherwise (or if it is unknown). Must have
5264 # previously called get_debug_format.
5265 proc setup_xfail_format { format } {
5266 set ret [test_debug_format $format]
5267
5268 if {$ret} then {
5269 setup_xfail "*-*-*"
5270 }
5271 return $ret
5272 }
5273
5274 # gdb_get_line_number TEXT [FILE]
5275 #
5276 # Search the source file FILE, and return the line number of the
5277 # first line containing TEXT. If no match is found, an error is thrown.
5278 #
5279 # TEXT is a string literal, not a regular expression.
5280 #
5281 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
5282 # specified, and does not start with "/", then it is assumed to be in
5283 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
5284 # by changing the callers and the interface at the same time.
5285 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
5286 # gdb.base/ena-dis-br.exp.
5287 #
5288 # Use this function to keep your test scripts independent of the
5289 # exact line numbering of the source file. Don't write:
5290 #
5291 # send_gdb "break 20"
5292 #
5293 # This means that if anyone ever edits your test's source file,
5294 # your test could break. Instead, put a comment like this on the
5295 # source file line you want to break at:
5296 #
5297 # /* breakpoint spot: frotz.exp: test name */
5298 #
5299 # and then write, in your test script (which we assume is named
5300 # frotz.exp):
5301 #
5302 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
5303 #
5304 # (Yes, Tcl knows how to handle the nested quotes and brackets.
5305 # Try this:
5306 # $ tclsh
5307 # % puts "foo [lindex "bar baz" 1]"
5308 # foo baz
5309 # %
5310 # Tcl is quite clever, for a little stringy language.)
5311 #
5312 # ===
5313 #
5314 # The previous implementation of this procedure used the gdb search command.
5315 # This version is different:
5316 #
5317 # . It works with MI, and it also works when gdb is not running.
5318 #
5319 # . It operates on the build machine, not the host machine.
5320 #
5321 # . For now, this implementation fakes a current directory of
5322 # $srcdir/$subdir to be compatible with the old implementation.
5323 # This will go away eventually and some callers will need to
5324 # be changed.
5325 #
5326 # . The TEXT argument is literal text and matches literally,
5327 # not a regular expression as it was before.
5328 #
5329 # . State changes in gdb, such as changing the current file
5330 # and setting $_, no longer happen.
5331 #
5332 # After a bit of time we can forget about the differences from the
5333 # old implementation.
5334 #
5335 # --chastain 2004-08-05
5336
5337 proc gdb_get_line_number { text { file "" } } {
5338 global srcdir
5339 global subdir
5340 global srcfile
5341
5342 if { "$file" == "" } then {
5343 set file "$srcfile"
5344 }
5345 if { ! [regexp "^/" "$file"] } then {
5346 set file "$srcdir/$subdir/$file"
5347 }
5348
5349 if { [ catch { set fd [open "$file"] } message ] } then {
5350 error "$message"
5351 }
5352
5353 set found -1
5354 for { set line 1 } { 1 } { incr line } {
5355 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
5356 error "$message"
5357 }
5358 if { $nchar < 0 } then {
5359 break
5360 }
5361 if { [string first "$text" "$body"] >= 0 } then {
5362 set found $line
5363 break
5364 }
5365 }
5366
5367 if { [ catch { close "$fd" } message ] } then {
5368 error "$message"
5369 }
5370
5371 if {$found == -1} {
5372 error "undefined tag \"$text\""
5373 }
5374
5375 return $found
5376 }
5377
5378 # Continue the program until it ends.
5379 #
5380 # MSSG is the error message that gets printed. If not given, a
5381 # default is used.
5382 # COMMAND is the command to invoke. If not given, "continue" is
5383 # used.
5384 # ALLOW_EXTRA is a flag indicating whether the test should expect
5385 # extra output between the "Continuing." line and the program
5386 # exiting. By default it is zero; if nonzero, any extra output
5387 # is accepted.
5388
5389 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
5390 global inferior_exited_re use_gdb_stub
5391
5392 if {$mssg == ""} {
5393 set text "continue until exit"
5394 } else {
5395 set text "continue until exit at $mssg"
5396 }
5397 if {$allow_extra} {
5398 set extra ".*"
5399 } else {
5400 set extra ""
5401 }
5402
5403 # By default, we don't rely on exit() behavior of remote stubs --
5404 # it's common for exit() to be implemented as a simple infinite
5405 # loop, or a forced crash/reset. For native targets, by default, we
5406 # assume process exit is reported as such. If a non-reliable target
5407 # is used, we set a breakpoint at exit, and continue to that.
5408 if { [target_info exists exit_is_reliable] } {
5409 set exit_is_reliable [target_info exit_is_reliable]
5410 } else {
5411 set exit_is_reliable [expr ! $use_gdb_stub]
5412 }
5413
5414 if { ! $exit_is_reliable } {
5415 if {![gdb_breakpoint "exit"]} {
5416 return 0
5417 }
5418 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
5419 $text
5420 } else {
5421 # Continue until we exit. Should not stop again.
5422 # Don't bother to check the output of the program, that may be
5423 # extremely tough for some remote systems.
5424 gdb_test $command \
5425 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
5426 $text
5427 }
5428 }
5429
5430 proc rerun_to_main {} {
5431 global gdb_prompt use_gdb_stub
5432
5433 if $use_gdb_stub {
5434 gdb_run_cmd
5435 gdb_expect {
5436 -re ".*Breakpoint .*main .*$gdb_prompt $"\
5437 {pass "rerun to main" ; return 0}
5438 -re "$gdb_prompt $"\
5439 {fail "rerun to main" ; return 0}
5440 timeout {fail "(timeout) rerun to main" ; return 0}
5441 }
5442 } else {
5443 send_gdb "run\n"
5444 gdb_expect {
5445 -re "The program .* has been started already.*y or n. $" {
5446 send_gdb "y\n" answer
5447 exp_continue
5448 }
5449 -re "Starting program.*$gdb_prompt $"\
5450 {pass "rerun to main" ; return 0}
5451 -re "$gdb_prompt $"\
5452 {fail "rerun to main" ; return 0}
5453 timeout {fail "(timeout) rerun to main" ; return 0}
5454 }
5455 }
5456 }
5457
5458 # Return true if EXECUTABLE contains a .gdb_index or .debug_names index section.
5459
5460 proc exec_has_index_section { executable } {
5461 set readelf_program [gdb_find_readelf]
5462 set res [catch {exec $readelf_program -S $executable \
5463 | grep -E "\.gdb_index|\.debug_names" }]
5464 if { $res == 0 } {
5465 return 1
5466 }
5467 return 0
5468 }
5469
5470 # Return list with major and minor version of readelf, or an empty list.
5471 gdb_caching_proc readelf_version {
5472 set readelf_program [gdb_find_readelf]
5473 set res [catch {exec $readelf_program --version} output]
5474 if { $res != 0 } {
5475 return [list]
5476 }
5477 set lines [split $output \n]
5478 set line [lindex $lines 0]
5479 set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \
5480 $line dummy major minor]
5481 if { $res != 1 } {
5482 return [list]
5483 }
5484 return [list $major $minor]
5485 }
5486
5487 # Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown.
5488 proc readelf_prints_pie { } {
5489 set version [readelf_version]
5490 if { [llength $version] == 0 } {
5491 return -1
5492 }
5493 set major [lindex $version 0]
5494 set minor [lindex $version 1]
5495 # It would be better to construct a PIE executable and test if the PIE
5496 # flag is printed by readelf, but we cannot reliably construct a PIE
5497 # executable if the multilib_flags dictate otherwise
5498 # (--target_board=unix/-no-pie/-fno-PIE).
5499 return [version_at_least $major $minor 2 26]
5500 }
5501
5502 # Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not,
5503 # and -1 if unknown.
5504
5505 proc exec_is_pie { executable } {
5506 set res [readelf_prints_pie]
5507 if { $res != 1 } {
5508 return -1
5509 }
5510 set readelf_program [gdb_find_readelf]
5511 set res [catch {exec $readelf_program -d $executable} output]
5512 if { $res != 0 } {
5513 return -1
5514 }
5515 set res [regexp -line {\(FLAGS_1\).*Flags:.* PIE($| )} $output]
5516 if { $res == 1 } {
5517 return 1
5518 }
5519 return 0
5520 }
5521
5522 # Return true if a test should be skipped due to lack of floating
5523 # point support or GDB can't fetch the contents from floating point
5524 # registers.
5525
5526 gdb_caching_proc gdb_skip_float_test {
5527 if [target_info exists gdb,skip_float_tests] {
5528 return 1
5529 }
5530
5531 # There is an ARM kernel ptrace bug that hardware VFP registers
5532 # are not updated after GDB ptrace set VFP registers. The bug
5533 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
5534 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
5535 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
5536 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
5537 # This kernel bug is detected by check how does GDB change the
5538 # program result by changing one VFP register.
5539 if { [istarget "arm*-*-linux*"] } {
5540
5541 set compile_flags {debug nowarnings }
5542
5543 # Set up, compile, and execute a test program having VFP
5544 # operations.
5545 set src [standard_temp_file arm_vfp[pid].c]
5546 set exe [standard_temp_file arm_vfp[pid].x]
5547
5548 gdb_produce_source $src {
5549 int main() {
5550 double d = 4.0;
5551 int ret;
5552
5553 asm ("vldr d0, [%0]" : : "r" (&d));
5554 asm ("vldr d1, [%0]" : : "r" (&d));
5555 asm (".global break_here\n"
5556 "break_here:");
5557 asm ("vcmp.f64 d0, d1\n"
5558 "vmrs APSR_nzcv, fpscr\n"
5559 "bne L_value_different\n"
5560 "movs %0, #0\n"
5561 "b L_end\n"
5562 "L_value_different:\n"
5563 "movs %0, #1\n"
5564 "L_end:\n" : "=r" (ret) :);
5565
5566 /* Return $d0 != $d1. */
5567 return ret;
5568 }
5569 }
5570
5571 verbose "compiling testfile $src" 2
5572 set lines [gdb_compile $src $exe executable $compile_flags]
5573 file delete $src
5574
5575 if ![string match "" $lines] then {
5576 verbose "testfile compilation failed, returning 1" 2
5577 return 0
5578 }
5579
5580 # No error message, compilation succeeded so now run it via gdb.
5581 # Run the test up to 5 times to detect whether ptrace can
5582 # correctly update VFP registers or not.
5583 set skip_vfp_test 0
5584 for {set i 0} {$i < 5} {incr i} {
5585 global gdb_prompt srcdir subdir
5586
5587 gdb_exit
5588 gdb_start
5589 gdb_reinitialize_dir $srcdir/$subdir
5590 gdb_load "$exe"
5591
5592 runto_main
5593 gdb_test "break *break_here"
5594 gdb_continue_to_breakpoint "break_here"
5595
5596 # Modify $d0 to a different value, so the exit code should
5597 # be 1.
5598 gdb_test "set \$d0 = 5.0"
5599
5600 set test "continue to exit"
5601 gdb_test_multiple "continue" "$test" {
5602 -re "exited with code 01.*$gdb_prompt $" {
5603 }
5604 -re "exited normally.*$gdb_prompt $" {
5605 # However, the exit code is 0. That means something
5606 # wrong in setting VFP registers.
5607 set skip_vfp_test 1
5608 break
5609 }
5610 }
5611 }
5612
5613 gdb_exit
5614 remote_file build delete $exe
5615
5616 return $skip_vfp_test
5617 }
5618 return 0
5619 }
5620
5621 # Print a message and return true if a test should be skipped
5622 # due to lack of stdio support.
5623
5624 proc gdb_skip_stdio_test { msg } {
5625 if [target_info exists gdb,noinferiorio] {
5626 verbose "Skipping test '$msg': no inferior i/o."
5627 return 1
5628 }
5629 return 0
5630 }
5631
5632 proc gdb_skip_bogus_test { msg } {
5633 return 0
5634 }
5635
5636 # Return true if a test should be skipped due to lack of XML support
5637 # in the host GDB.
5638 # NOTE: This must be called while gdb is *not* running.
5639
5640 gdb_caching_proc gdb_skip_xml_test {
5641 global gdb_spawn_id
5642 global gdb_prompt
5643 global srcdir
5644
5645 if { [info exists gdb_spawn_id] } {
5646 error "GDB must not be running in gdb_skip_xml_tests."
5647 }
5648
5649 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
5650
5651 gdb_start
5652 set xml_missing 0
5653 gdb_test_multiple "set tdesc filename $xml_file" "" {
5654 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
5655 set xml_missing 1
5656 }
5657 -re ".*$gdb_prompt $" { }
5658 }
5659 gdb_exit
5660 return $xml_missing
5661 }
5662
5663 # Return true if argv[0] is available.
5664
5665 gdb_caching_proc gdb_has_argv0 {
5666 set result 0
5667
5668 # Compile and execute a test program to check whether argv[0] is available.
5669 gdb_simple_compile has_argv0 {
5670 int main (int argc, char **argv) {
5671 return 0;
5672 }
5673 } executable
5674
5675
5676 # Helper proc.
5677 proc gdb_has_argv0_1 { exe } {
5678 global srcdir subdir
5679 global gdb_prompt hex
5680
5681 gdb_exit
5682 gdb_start
5683 gdb_reinitialize_dir $srcdir/$subdir
5684 gdb_load "$exe"
5685
5686 # Set breakpoint on main.
5687 gdb_test_multiple "break main" "break main" {
5688 -re "Breakpoint.*${gdb_prompt} $" {
5689 }
5690 -re "${gdb_prompt} $" {
5691 return 0
5692 }
5693 }
5694
5695 # Run to main.
5696 gdb_run_cmd
5697 gdb_test_multiple "" "run to main" {
5698 -re "Breakpoint.*${gdb_prompt} $" {
5699 }
5700 -re "${gdb_prompt} $" {
5701 return 0
5702 }
5703 }
5704
5705 set old_elements "200"
5706 set test "show print elements"
5707 gdb_test_multiple $test $test {
5708 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5709 set old_elements $expect_out(1,string)
5710 }
5711 }
5712 set old_repeats "200"
5713 set test "show print repeats"
5714 gdb_test_multiple $test $test {
5715 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5716 set old_repeats $expect_out(1,string)
5717 }
5718 }
5719 gdb_test_no_output "set print elements unlimited" ""
5720 gdb_test_no_output "set print repeats unlimited" ""
5721
5722 set retval 0
5723 # Check whether argc is 1.
5724 gdb_test_multiple "p argc" "p argc" {
5725 -re " = 1\r\n${gdb_prompt} $" {
5726
5727 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
5728 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
5729 set retval 1
5730 }
5731 -re "${gdb_prompt} $" {
5732 }
5733 }
5734 }
5735 -re "${gdb_prompt} $" {
5736 }
5737 }
5738
5739 gdb_test_no_output "set print elements $old_elements" ""
5740 gdb_test_no_output "set print repeats $old_repeats" ""
5741
5742 return $retval
5743 }
5744
5745 set result [gdb_has_argv0_1 $obj]
5746
5747 gdb_exit
5748 file delete $obj
5749
5750 if { !$result
5751 && ([istarget *-*-linux*]
5752 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
5753 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
5754 || [istarget *-*-openbsd*]
5755 || [istarget *-*-darwin*]
5756 || [istarget *-*-solaris*]
5757 || [istarget *-*-aix*]
5758 || [istarget *-*-gnu*]
5759 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
5760 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
5761 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
5762 || [istarget *-*-symbianelf*]
5763 || [istarget *-*-osf*]
5764 || [istarget *-*-dicos*]
5765 || [istarget *-*-nto*]
5766 || [istarget *-*-*vms*]
5767 || [istarget *-*-lynx*178]) } {
5768 fail "argv\[0\] should be available on this target"
5769 }
5770
5771 return $result
5772 }
5773
5774 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
5775 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
5776 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
5777 # the name of a debuginfo only file. This file will be stored in the same
5778 # subdirectory.
5779
5780 # Functions for separate debug info testing
5781
5782 # starting with an executable:
5783 # foo --> original executable
5784
5785 # at the end of the process we have:
5786 # foo.stripped --> foo w/o debug info
5787 # foo.debug --> foo's debug info
5788 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
5789
5790 # Fetch the build id from the file.
5791 # Returns "" if there is none.
5792
5793 proc get_build_id { filename } {
5794 if { ([istarget "*-*-mingw*"]
5795 || [istarget *-*-cygwin*]) } {
5796 set objdump_program [gdb_find_objdump]
5797 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
5798 verbose "result is $result"
5799 verbose "output is $output"
5800 if {$result == 1} {
5801 return ""
5802 }
5803 return $data
5804 } else {
5805 set tmp [standard_output_file "${filename}-tmp"]
5806 set objcopy_program [gdb_find_objcopy]
5807 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
5808 verbose "result is $result"
5809 verbose "output is $output"
5810 if {$result == 1} {
5811 return ""
5812 }
5813 set fi [open $tmp]
5814 fconfigure $fi -translation binary
5815 # Skip the NOTE header.
5816 read $fi 16
5817 set data [read $fi]
5818 close $fi
5819 file delete $tmp
5820 if ![string compare $data ""] then {
5821 return ""
5822 }
5823 # Convert it to hex.
5824 binary scan $data H* data
5825 return $data
5826 }
5827 }
5828
5829 # Return the build-id hex string (usually 160 bits as 40 hex characters)
5830 # converted to the form: .build-id/ab/cdef1234...89.debug
5831 # Return "" if no build-id found.
5832 proc build_id_debug_filename_get { filename } {
5833 set data [get_build_id $filename]
5834 if { $data == "" } {
5835 return ""
5836 }
5837 regsub {^..} $data {\0/} data
5838 return ".build-id/${data}.debug"
5839 }
5840
5841 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
5842 # list of optional flags. The only currently supported flag is no-main,
5843 # which removes the symbol entry for main from the separate debug file.
5844 #
5845 # Function returns zero on success. Function will return non-zero failure code
5846 # on some targets not supporting separate debug info (such as i386-msdos).
5847
5848 proc gdb_gnu_strip_debug { dest args } {
5849
5850 # Use the first separate debug info file location searched by GDB so the
5851 # run cannot be broken by some stale file searched with higher precedence.
5852 set debug_file "${dest}.debug"
5853
5854 set strip_to_file_program [transform strip]
5855 set objcopy_program [gdb_find_objcopy]
5856
5857 set debug_link [file tail $debug_file]
5858 set stripped_file "${dest}.stripped"
5859
5860 # Get rid of the debug info, and store result in stripped_file
5861 # something like gdb/testsuite/gdb.base/blah.stripped.
5862 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
5863 verbose "result is $result"
5864 verbose "output is $output"
5865 if {$result == 1} {
5866 return 1
5867 }
5868
5869 # Workaround PR binutils/10802:
5870 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5871 set perm [file attributes ${dest} -permissions]
5872 file attributes ${stripped_file} -permissions $perm
5873
5874 # Get rid of everything but the debug info, and store result in debug_file
5875 # This will be in the .debug subdirectory, see above.
5876 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
5877 verbose "result is $result"
5878 verbose "output is $output"
5879 if {$result == 1} {
5880 return 1
5881 }
5882
5883 # If no-main is passed, strip the symbol for main from the separate
5884 # file. This is to simulate the behavior of elfutils's eu-strip, which
5885 # leaves the symtab in the original file only. There's no way to get
5886 # objcopy or strip to remove the symbol table without also removing the
5887 # debugging sections, so this is as close as we can get.
5888 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
5889 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
5890 verbose "result is $result"
5891 verbose "output is $output"
5892 if {$result == 1} {
5893 return 1
5894 }
5895 file delete "${debug_file}"
5896 file rename "${debug_file}-tmp" "${debug_file}"
5897 }
5898
5899 # Link the two previous output files together, adding the .gnu_debuglink
5900 # section to the stripped_file, containing a pointer to the debug_file,
5901 # save the new file in dest.
5902 # This will be the regular executable filename, in the usual location.
5903 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
5904 verbose "result is $result"
5905 verbose "output is $output"
5906 if {$result == 1} {
5907 return 1
5908 }
5909
5910 # Workaround PR binutils/10802:
5911 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5912 set perm [file attributes ${stripped_file} -permissions]
5913 file attributes ${dest} -permissions $perm
5914
5915 return 0
5916 }
5917
5918 # Test the output of GDB_COMMAND matches the pattern obtained
5919 # by concatenating all elements of EXPECTED_LINES. This makes
5920 # it possible to split otherwise very long string into pieces.
5921 # If third argument TESTNAME is not empty, it's used as the name of the
5922 # test to be printed on pass/fail.
5923 proc help_test_raw { gdb_command expected_lines {testname {}} } {
5924 set expected_output [join $expected_lines ""]
5925 if {$testname != {}} {
5926 gdb_test "${gdb_command}" "${expected_output}" $testname
5927 return
5928 }
5929
5930 gdb_test "${gdb_command}" "${expected_output}"
5931 }
5932
5933 # A regexp that matches the end of help CLASS|PREFIX_COMMAND
5934 set help_list_trailer {
5935 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
5936 "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+"
5937 "Command name abbreviations are allowed if unambiguous\."
5938 }
5939
5940 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
5941 # are regular expressions that should match the beginning of output,
5942 # before the list of commands in that class.
5943 # LIST_OF_COMMANDS are regular expressions that should match the
5944 # list of commands in that class. If empty, the command list will be
5945 # matched automatically. The presence of standard epilogue will be tested
5946 # automatically.
5947 # If last argument TESTNAME is not empty, it's used as the name of the
5948 # test to be printed on pass/fail.
5949 # Notice that the '[' and ']' characters don't need to be escaped for strings
5950 # wrapped in {} braces.
5951 proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } {
5952 global help_list_trailer
5953 if {[llength $list_of_commands]>0} {
5954 set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"}
5955 set l_list_of_commands [concat $l_list_of_commands $list_of_commands]
5956 set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}]
5957 } else {
5958 set l_list_of_commands {"List of commands\:.*[\r\n]+"}
5959 }
5960 set l_stock_body {
5961 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
5962 }
5963 set l_entire_body [concat $expected_initial_lines $l_list_of_commands \
5964 $l_stock_body $help_list_trailer]
5965
5966 help_test_raw "help ${command_class}" $l_entire_body $testname
5967 }
5968
5969 # Like test_class_help but specialised to test "help user-defined".
5970 proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } {
5971 test_class_help "user-defined" {
5972 "User-defined commands\.[\r\n]+"
5973 "The commands in this class are those defined by the user\.[\r\n]+"
5974 "Use the \"define\" command to define a command\.[\r\n]+"
5975 } $list_of_commands $testname
5976 }
5977
5978
5979 # COMMAND_LIST should have either one element -- command to test, or
5980 # two elements -- abbreviated command to test, and full command the first
5981 # element is abbreviation of.
5982 # The command must be a prefix command. EXPECTED_INITIAL_LINES
5983 # are regular expressions that should match the beginning of output,
5984 # before the list of subcommands. The presence of
5985 # subcommand list and standard epilogue will be tested automatically.
5986 proc test_prefix_command_help { command_list expected_initial_lines args } {
5987 global help_list_trailer
5988 set command [lindex $command_list 0]
5989 if {[llength $command_list]>1} {
5990 set full_command [lindex $command_list 1]
5991 } else {
5992 set full_command $command
5993 }
5994 # Use 'list' and not just {} because we want variables to
5995 # be expanded in this list.
5996 set l_stock_body [list\
5997 "List of $full_command subcommands\:.*\[\r\n\]+"\
5998 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"]
5999 set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer]
6000 if {[llength $args]>0} {
6001 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
6002 } else {
6003 help_test_raw "help ${command}" $l_entire_body
6004 }
6005 }
6006
6007 # Build executable named EXECUTABLE from specifications that allow
6008 # different options to be passed to different sub-compilations.
6009 # TESTNAME is the name of the test; this is passed to 'untested' if
6010 # something fails.
6011 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
6012 # contains the option "pthreads", then gdb_compile_pthreads is used.
6013 # ARGS is a flat list of source specifications, of the form:
6014 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
6015 # Each SOURCE is compiled to an object file using its OPTIONS,
6016 # using gdb_compile.
6017 # Returns 0 on success, -1 on failure.
6018 proc build_executable_from_specs {testname executable options args} {
6019 global subdir
6020 global srcdir
6021
6022 set binfile [standard_output_file $executable]
6023
6024 set info_options ""
6025 if { [lsearch -exact $options "c++"] >= 0 } {
6026 set info_options "c++"
6027 }
6028 if [get_compiler_info ${info_options}] {
6029 return -1
6030 }
6031
6032 set func gdb_compile
6033 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads|openmp)$}]
6034 if {$func_index != -1} {
6035 set func "${func}_[lindex $options $func_index]"
6036 }
6037
6038 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
6039 # parameter. They also requires $sources while gdb_compile and
6040 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
6041 if [string match gdb_compile_shlib* $func] {
6042 set sources_path {}
6043 foreach {s local_options} $args {
6044 if { [regexp "^/" "$s"] } then {
6045 lappend sources_path "$s"
6046 } else {
6047 lappend sources_path "$srcdir/$subdir/$s"
6048 }
6049 }
6050 set ret [$func $sources_path "${binfile}" $options]
6051 } elseif {[lsearch -exact $options rust] != -1} {
6052 set sources_path {}
6053 foreach {s local_options} $args {
6054 if { [regexp "^/" "$s"] } then {
6055 lappend sources_path "$s"
6056 } else {
6057 lappend sources_path "$srcdir/$subdir/$s"
6058 }
6059 }
6060 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
6061 } else {
6062 set objects {}
6063 set i 0
6064 foreach {s local_options} $args {
6065 if { ! [regexp "^/" "$s"] } then {
6066 set s "$srcdir/$subdir/$s"
6067 }
6068 if { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } {
6069 untested $testname
6070 return -1
6071 }
6072 lappend objects "${binfile}${i}.o"
6073 incr i
6074 }
6075 set ret [$func $objects "${binfile}" executable $options]
6076 }
6077 if { $ret != "" } {
6078 untested $testname
6079 return -1
6080 }
6081
6082 return 0
6083 }
6084
6085 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
6086 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
6087 # to pass to untested, if something is wrong. OPTIONS are passed
6088 # to gdb_compile directly.
6089 proc build_executable { testname executable {sources ""} {options {debug}} } {
6090 if {[llength $sources]==0} {
6091 set sources ${executable}.c
6092 }
6093
6094 set arglist [list $testname $executable $options]
6095 foreach source $sources {
6096 lappend arglist $source $options
6097 }
6098
6099 return [eval build_executable_from_specs $arglist]
6100 }
6101
6102 # Starts fresh GDB binary and loads an optional executable into GDB.
6103 # Usage: clean_restart [executable]
6104 # EXECUTABLE is the basename of the binary.
6105 # Return -1 if starting gdb or loading the executable failed.
6106
6107 proc clean_restart { args } {
6108 global srcdir
6109 global subdir
6110 global errcnt
6111
6112 if { [llength $args] > 1 } {
6113 error "bad number of args: [llength $args]"
6114 }
6115
6116 gdb_exit
6117
6118 # We'd like to do:
6119 # if { [gdb_start] == -1 } {
6120 # return -1
6121 # }
6122 # but gdb_start is a ${tool}_start proc, which doesn't have a defined
6123 # return value. So instead, we test for errcnt.
6124 set saved_errcnt $errcnt
6125 gdb_start
6126 if { $errcnt > $saved_errcnt } {
6127 return -1
6128 }
6129
6130 gdb_reinitialize_dir $srcdir/$subdir
6131
6132 if { [llength $args] >= 1 } {
6133 set executable [lindex $args 0]
6134 set binfile [standard_output_file ${executable}]
6135 return [gdb_load ${binfile}]
6136 }
6137
6138 return 0
6139 }
6140
6141 # Prepares for testing by calling build_executable_full, then
6142 # clean_restart.
6143 # TESTNAME is the name of the test.
6144 # Each element in ARGS is a list of the form
6145 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
6146 # These are passed to build_executable_from_specs, which see.
6147 # The last EXECUTABLE is passed to clean_restart.
6148 # Returns 0 on success, non-zero on failure.
6149 proc prepare_for_testing_full {testname args} {
6150 foreach spec $args {
6151 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
6152 return -1
6153 }
6154 set executable [lindex $spec 0]
6155 }
6156 clean_restart $executable
6157 return 0
6158 }
6159
6160 # Prepares for testing, by calling build_executable, and then clean_restart.
6161 # Please refer to build_executable for parameter description.
6162 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
6163
6164 if {[build_executable $testname $executable $sources $options] == -1} {
6165 return -1
6166 }
6167 clean_restart $executable
6168
6169 return 0
6170 }
6171
6172 # Retrieve the value of EXP in the inferior, represented in format
6173 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
6174 # print fails. TEST is the test message to use. It can be omitted,
6175 # in which case a test message is built from EXP.
6176
6177 proc get_valueof { fmt exp default {test ""} } {
6178 global gdb_prompt
6179
6180 if {$test == "" } {
6181 set test "get valueof \"${exp}\""
6182 }
6183
6184 set val ${default}
6185 gdb_test_multiple "print${fmt} ${exp}" "$test" {
6186 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
6187 set val $expect_out(1,string)
6188 pass "$test"
6189 }
6190 timeout {
6191 fail "$test (timeout)"
6192 }
6193 }
6194 return ${val}
6195 }
6196
6197 # Retrieve the value of local var EXP in the inferior. DEFAULT is used as
6198 # fallback if print fails. TEST is the test message to use. It can be
6199 # omitted, in which case a test message is built from EXP.
6200
6201 proc get_local_valueof { exp default {test ""} } {
6202 global gdb_prompt
6203
6204 if {$test == "" } {
6205 set test "get local valueof \"${exp}\""
6206 }
6207
6208 set val ${default}
6209 gdb_test_multiple "info locals ${exp}" "$test" {
6210 -re "$exp = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
6211 set val $expect_out(1,string)
6212 pass "$test"
6213 }
6214 timeout {
6215 fail "$test (timeout)"
6216 }
6217 }
6218 return ${val}
6219 }
6220
6221 # Retrieve the value of EXP in the inferior, as a signed decimal value
6222 # (using "print /d"). DEFAULT is used as fallback if print fails.
6223 # TEST is the test message to use. It can be omitted, in which case
6224 # a test message is built from EXP.
6225
6226 proc get_integer_valueof { exp default {test ""} } {
6227 global gdb_prompt
6228
6229 if {$test == ""} {
6230 set test "get integer valueof \"${exp}\""
6231 }
6232
6233 set val ${default}
6234 gdb_test_multiple "print /d ${exp}" "$test" {
6235 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
6236 set val $expect_out(1,string)
6237 pass "$test"
6238 }
6239 timeout {
6240 fail "$test (timeout)"
6241 }
6242 }
6243 return ${val}
6244 }
6245
6246 # Retrieve the value of EXP in the inferior, as an hexadecimal value
6247 # (using "print /x"). DEFAULT is used as fallback if print fails.
6248 # TEST is the test message to use. It can be omitted, in which case
6249 # a test message is built from EXP.
6250
6251 proc get_hexadecimal_valueof { exp default {test ""} } {
6252 global gdb_prompt
6253
6254 if {$test == ""} {
6255 set test "get hexadecimal valueof \"${exp}\""
6256 }
6257
6258 set val ${default}
6259 gdb_test_multiple "print /x ${exp}" $test {
6260 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
6261 set val $expect_out(1,string)
6262 pass "$test"
6263 }
6264 }
6265 return ${val}
6266 }
6267
6268 # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
6269 # is used as fallback if print fails. TEST is the test message to use.
6270 # It can be omitted, in which case a test message is 'sizeof (TYPE)'.
6271
6272 proc get_sizeof { type default {test ""} } {
6273 return [get_integer_valueof "sizeof (${type})" $default $test]
6274 }
6275
6276 proc get_target_charset { } {
6277 global gdb_prompt
6278
6279 gdb_test_multiple "show target-charset" "" {
6280 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
6281 return $expect_out(1,string)
6282 }
6283 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
6284 return $expect_out(1,string)
6285 }
6286 }
6287
6288 # Pick a reasonable default.
6289 warning "Unable to read target-charset."
6290 return "UTF-8"
6291 }
6292
6293 # Get the address of VAR.
6294
6295 proc get_var_address { var } {
6296 global gdb_prompt hex
6297
6298 # Match output like:
6299 # $1 = (int *) 0x0
6300 # $5 = (int (*)()) 0
6301 # $6 = (int (*)()) 0x24 <function_bar>
6302
6303 gdb_test_multiple "print &${var}" "get address of ${var}" {
6304 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
6305 {
6306 pass "get address of ${var}"
6307 if { $expect_out(1,string) == "0" } {
6308 return "0x0"
6309 } else {
6310 return $expect_out(1,string)
6311 }
6312 }
6313 }
6314 return ""
6315 }
6316
6317 # Return the frame number for the currently selected frame
6318 proc get_current_frame_number {{test_name ""}} {
6319 global gdb_prompt
6320
6321 if { $test_name == "" } {
6322 set test_name "get current frame number"
6323 }
6324 set frame_num -1
6325 gdb_test_multiple "frame" $test_name {
6326 -re "#(\[0-9\]+) .*$gdb_prompt $" {
6327 set frame_num $expect_out(1,string)
6328 }
6329 }
6330 return $frame_num
6331 }
6332
6333 # Get the current value for remotetimeout and return it.
6334 proc get_remotetimeout { } {
6335 global gdb_prompt
6336 global decimal
6337
6338 gdb_test_multiple "show remotetimeout" "" {
6339 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
6340 return $expect_out(1,string)
6341 }
6342 }
6343
6344 # Pick the default that gdb uses
6345 warning "Unable to read remotetimeout"
6346 return 300
6347 }
6348
6349 # Set the remotetimeout to the specified timeout. Nothing is returned.
6350 proc set_remotetimeout { timeout } {
6351 global gdb_prompt
6352
6353 gdb_test_multiple "set remotetimeout $timeout" "" {
6354 -re "$gdb_prompt $" {
6355 verbose "Set remotetimeout to $timeout\n"
6356 }
6357 }
6358 }
6359
6360 # Get the target's current endianness and return it.
6361 proc get_endianness { } {
6362 global gdb_prompt
6363
6364 gdb_test_multiple "show endian" "determine endianness" {
6365 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
6366 # Pass silently.
6367 return $expect_out(1,string)
6368 }
6369 }
6370 return "little"
6371 }
6372
6373 # ROOT and FULL are file names. Returns the relative path from ROOT
6374 # to FULL. Note that FULL must be in a subdirectory of ROOT.
6375 # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
6376 # will return "ls".
6377
6378 proc relative_filename {root full} {
6379 set root_split [file split $root]
6380 set full_split [file split $full]
6381
6382 set len [llength $root_split]
6383
6384 if {[eval file join $root_split]
6385 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
6386 error "$full not a subdir of $root"
6387 }
6388
6389 return [eval file join [lrange $full_split $len end]]
6390 }
6391
6392 # If GDB_PARALLEL exists, then set up the parallel-mode directories.
6393 if {[info exists GDB_PARALLEL]} {
6394 if {[is_remote host]} {
6395 unset GDB_PARALLEL
6396 } else {
6397 file mkdir \
6398 [make_gdb_parallel_path outputs] \
6399 [make_gdb_parallel_path temp] \
6400 [make_gdb_parallel_path cache]
6401 }
6402 }
6403
6404 proc core_find {binfile {deletefiles {}} {arg ""}} {
6405 global objdir subdir
6406
6407 set destcore "$binfile.core"
6408 file delete $destcore
6409
6410 # Create a core file named "$destcore" rather than just "core", to
6411 # avoid problems with sys admin types that like to regularly prune all
6412 # files named "core" from the system.
6413 #
6414 # Arbitrarily try setting the core size limit to "unlimited" since
6415 # this does not hurt on systems where the command does not work and
6416 # allows us to generate a core on systems where it does.
6417 #
6418 # Some systems append "core" to the name of the program; others append
6419 # the name of the program to "core"; still others (like Linux, as of
6420 # May 2003) create cores named "core.PID". In the latter case, we
6421 # could have many core files lying around, and it may be difficult to
6422 # tell which one is ours, so let's run the program in a subdirectory.
6423 set found 0
6424 set coredir [standard_output_file coredir.[getpid]]
6425 file mkdir $coredir
6426 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
6427 # remote_exec host "${binfile}"
6428 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
6429 if [remote_file build exists $i] {
6430 remote_exec build "mv $i $destcore"
6431 set found 1
6432 }
6433 }
6434 # Check for "core.PID".
6435 if { $found == 0 } {
6436 set names [glob -nocomplain -directory $coredir core.*]
6437 if {[llength $names] == 1} {
6438 set corefile [file join $coredir [lindex $names 0]]
6439 remote_exec build "mv $corefile $destcore"
6440 set found 1
6441 }
6442 }
6443 if { $found == 0 } {
6444 # The braindamaged HPUX shell quits after the ulimit -c above
6445 # without executing ${binfile}. So we try again without the
6446 # ulimit here if we didn't find a core file above.
6447 # Oh, I should mention that any "braindamaged" non-Unix system has
6448 # the same problem. I like the cd bit too, it's really neat'n stuff.
6449 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
6450 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
6451 if [remote_file build exists $i] {
6452 remote_exec build "mv $i $destcore"
6453 set found 1
6454 }
6455 }
6456 }
6457
6458 # Try to clean up after ourselves.
6459 foreach deletefile $deletefiles {
6460 remote_file build delete [file join $coredir $deletefile]
6461 }
6462 remote_exec build "rmdir $coredir"
6463
6464 if { $found == 0 } {
6465 warning "can't generate a core file - core tests suppressed - check ulimit -c"
6466 return ""
6467 }
6468 return $destcore
6469 }
6470
6471 # gdb_target_symbol_prefix compiles a test program and then examines
6472 # the output from objdump to determine the prefix (such as underscore)
6473 # for linker symbol prefixes.
6474
6475 gdb_caching_proc gdb_target_symbol_prefix {
6476 # Compile a simple test program...
6477 set src { int main() { return 0; } }
6478 if {![gdb_simple_compile target_symbol_prefix $src executable]} {
6479 return 0
6480 }
6481
6482 set prefix ""
6483
6484 set objdump_program [gdb_find_objdump]
6485 set result [catch "exec $objdump_program --syms $obj" output]
6486
6487 if { $result == 0 \
6488 && ![regexp -lineanchor \
6489 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
6490 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
6491 }
6492
6493 file delete $obj
6494
6495 return $prefix
6496 }
6497
6498 # Return 1 if target supports scheduler locking, otherwise return 0.
6499
6500 gdb_caching_proc target_supports_scheduler_locking {
6501 global gdb_prompt
6502
6503 set me "gdb_target_supports_scheduler_locking"
6504
6505 set src { int main() { return 0; } }
6506 if {![gdb_simple_compile $me $src executable]} {
6507 return 0
6508 }
6509
6510 clean_restart $obj
6511 if ![runto_main] {
6512 return 0
6513 }
6514
6515 set supports_schedule_locking -1
6516 set current_schedule_locking_mode ""
6517
6518 set test "reading current scheduler-locking mode"
6519 gdb_test_multiple "show scheduler-locking" $test {
6520 -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" {
6521 set current_schedule_locking_mode $expect_out(1,string)
6522 }
6523 -re "$gdb_prompt $" {
6524 set supports_schedule_locking 0
6525 }
6526 timeout {
6527 set supports_schedule_locking 0
6528 }
6529 }
6530
6531 if { $supports_schedule_locking == -1 } {
6532 set test "checking for scheduler-locking support"
6533 gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test {
6534 -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" {
6535 set supports_schedule_locking 0
6536 }
6537 -re "$gdb_prompt $" {
6538 set supports_schedule_locking 1
6539 }
6540 timeout {
6541 set supports_schedule_locking 0
6542 }
6543 }
6544 }
6545
6546 if { $supports_schedule_locking == -1 } {
6547 set supports_schedule_locking 0
6548 }
6549
6550 gdb_exit
6551 remote_file build delete $obj
6552 verbose "$me: returning $supports_schedule_locking" 2
6553 return $supports_schedule_locking
6554 }
6555
6556 # Return 1 if compiler supports use of nested functions. Otherwise,
6557 # return 0.
6558
6559 gdb_caching_proc support_nested_function_tests {
6560 # Compile a test program containing a nested function
6561 return [gdb_can_simple_compile nested_func {
6562 int main () {
6563 int foo () {
6564 return 0;
6565 }
6566 return foo ();
6567 }
6568 } executable]
6569 }
6570
6571 # gdb_target_symbol returns the provided symbol with the correct prefix
6572 # prepended. (See gdb_target_symbol_prefix, above.)
6573
6574 proc gdb_target_symbol { symbol } {
6575 set prefix [gdb_target_symbol_prefix]
6576 return "${prefix}${symbol}"
6577 }
6578
6579 # gdb_target_symbol_prefix_flags_asm returns a string that can be
6580 # added to gdb_compile options to define the C-preprocessor macro
6581 # SYMBOL_PREFIX with a value that can be prepended to symbols
6582 # for targets which require a prefix, such as underscore.
6583 #
6584 # This version (_asm) defines the prefix without double quotes
6585 # surrounding the prefix. It is used to define the macro
6586 # SYMBOL_PREFIX for assembly language files. Another version, below,
6587 # is used for symbols in inline assembler in C/C++ files.
6588 #
6589 # The lack of quotes in this version (_asm) makes it possible to
6590 # define supporting macros in the .S file. (The version which
6591 # uses quotes for the prefix won't work for such files since it's
6592 # impossible to define a quote-stripping macro in C.)
6593 #
6594 # It's possible to use this version (_asm) for C/C++ source files too,
6595 # but a string is usually required in such files; providing a version
6596 # (no _asm) which encloses the prefix with double quotes makes it
6597 # somewhat easier to define the supporting macros in the test case.
6598
6599 proc gdb_target_symbol_prefix_flags_asm {} {
6600 set prefix [gdb_target_symbol_prefix]
6601 if {$prefix ne ""} {
6602 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
6603 } else {
6604 return "";
6605 }
6606 }
6607
6608 # gdb_target_symbol_prefix_flags returns the same string as
6609 # gdb_target_symbol_prefix_flags_asm, above, but with the prefix
6610 # enclosed in double quotes if there is a prefix.
6611 #
6612 # See the comment for gdb_target_symbol_prefix_flags_asm for an
6613 # extended discussion.
6614
6615 proc gdb_target_symbol_prefix_flags {} {
6616 set prefix [gdb_target_symbol_prefix]
6617 if {$prefix ne ""} {
6618 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
6619 } else {
6620 return "";
6621 }
6622 }
6623
6624 # A wrapper for 'remote_exec host' that passes or fails a test.
6625 # Returns 0 if all went well, nonzero on failure.
6626 # TEST is the name of the test, other arguments are as for remote_exec.
6627
6628 proc run_on_host { test program args } {
6629 verbose -log "run_on_host: $program $args"
6630 # remote_exec doesn't work properly if the output is set but the
6631 # input is the empty string -- so replace an empty input with
6632 # /dev/null.
6633 if {[llength $args] > 1 && [lindex $args 1] == ""} {
6634 set args [lreplace $args 1 1 "/dev/null"]
6635 }
6636 set result [eval remote_exec host [list $program] $args]
6637 verbose "result is $result"
6638 set status [lindex $result 0]
6639 set output [lindex $result 1]
6640 if {$status == 0} {
6641 pass $test
6642 return 0
6643 } else {
6644 verbose -log "run_on_host failed: $output"
6645 fail $test
6646 return -1
6647 }
6648 }
6649
6650 # Return non-zero if "board_info debug_flags" mentions Fission.
6651 # http://gcc.gnu.org/wiki/DebugFission
6652 # Fission doesn't support everything yet.
6653 # This supports working around bug 15954.
6654
6655 proc using_fission { } {
6656 set debug_flags [board_info [target_info name] debug_flags]
6657 return [regexp -- "-gsplit-dwarf" $debug_flags]
6658 }
6659
6660 # Search the caller's ARGS list and set variables according to the list of
6661 # valid options described by ARGSET.
6662 #
6663 # The first member of each one- or two-element list in ARGSET defines the
6664 # name of a variable that will be added to the caller's scope.
6665 #
6666 # If only one element is given to describe an option, it the value is
6667 # 0 if the option is not present in (the caller's) ARGS or 1 if
6668 # it is.
6669 #
6670 # If two elements are given, the second element is the default value of
6671 # the variable. This is then overwritten if the option exists in ARGS.
6672 #
6673 # Any parse_args elements in (the caller's) ARGS will be removed, leaving
6674 # any optional components.
6675
6676 # Example:
6677 # proc myproc {foo args} {
6678 # parse_args {{bar} {baz "abc"} {qux}}
6679 # # ...
6680 # }
6681 # myproc ABC -bar -baz DEF peanut butter
6682 # will define the following variables in myproc:
6683 # foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
6684 # args will be the list {peanut butter}
6685
6686 proc parse_args { argset } {
6687 upvar args args
6688
6689 foreach argument $argset {
6690 if {[llength $argument] == 1} {
6691 # No default specified, so we assume that we should set
6692 # the value to 1 if the arg is present and 0 if it's not.
6693 # It is assumed that no value is given with the argument.
6694 set result [lsearch -exact $args "-$argument"]
6695 if {$result != -1} then {
6696 uplevel 1 [list set $argument 1]
6697 set args [lreplace $args $result $result]
6698 } else {
6699 uplevel 1 [list set $argument 0]
6700 }
6701 } elseif {[llength $argument] == 2} {
6702 # There are two items in the argument. The second is a
6703 # default value to use if the item is not present.
6704 # Otherwise, the variable is set to whatever is provided
6705 # after the item in the args.
6706 set arg [lindex $argument 0]
6707 set result [lsearch -exact $args "-[lindex $arg 0]"]
6708 if {$result != -1} then {
6709 uplevel 1 [list set $arg [lindex $args [expr $result+1]]]
6710 set args [lreplace $args $result [expr $result+1]]
6711 } else {
6712 uplevel 1 [list set $arg [lindex $argument 1]]
6713 }
6714 } else {
6715 error "Badly formatted argument \"$argument\" in argument set"
6716 }
6717 }
6718
6719 # The remaining args should be checked to see that they match the
6720 # number of items expected to be passed into the procedure...
6721 }
6722
6723 # Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
6724 # return that string.
6725
6726 proc capture_command_output { command prefix } {
6727 global gdb_prompt
6728 global expect_out
6729
6730 set output_string ""
6731 gdb_test_multiple "$command" "capture_command_output for $command" {
6732 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
6733 set output_string $expect_out(1,string)
6734 }
6735 }
6736 return $output_string
6737 }
6738
6739 # A convenience function that joins all the arguments together, with a
6740 # regexp that matches exactly one end of line in between each argument.
6741 # This function is ideal to write the expected output of a GDB command
6742 # that generates more than a couple of lines, as this allows us to write
6743 # each line as a separate string, which is easier to read by a human
6744 # being.
6745
6746 proc multi_line { args } {
6747 return [join $args "\r\n"]
6748 }
6749
6750 # Similar to the above, but while multi_line is meant to be used to
6751 # match GDB output, this one is meant to be used to build strings to
6752 # send as GDB input.
6753
6754 proc multi_line_input { args } {
6755 return [join $args "\n"]
6756 }
6757
6758 # Return the version of the DejaGnu framework.
6759 #
6760 # The return value is a list containing the major, minor and patch version
6761 # numbers. If the version does not contain a minor or patch number, they will
6762 # be set to 0. For example:
6763 #
6764 # 1.6 -> {1 6 0}
6765 # 1.6.1 -> {1 6 1}
6766 # 2 -> {2 0 0}
6767
6768 proc dejagnu_version { } {
6769 # The frame_version variable is defined by DejaGnu, in runtest.exp.
6770 global frame_version
6771
6772 verbose -log "DejaGnu version: $frame_version"
6773 verbose -log "Expect version: [exp_version]"
6774 verbose -log "Tcl version: [info tclversion]"
6775
6776 set dg_ver [split $frame_version .]
6777
6778 while { [llength $dg_ver] < 3 } {
6779 lappend dg_ver 0
6780 }
6781
6782 return $dg_ver
6783 }
6784
6785 # Define user-defined command COMMAND using the COMMAND_LIST as the
6786 # command's definition. The terminating "end" is added automatically.
6787
6788 proc gdb_define_cmd {command command_list} {
6789 global gdb_prompt
6790
6791 set input [multi_line_input {*}$command_list "end"]
6792 set test "define $command"
6793
6794 gdb_test_multiple "define $command" $test {
6795 -re "End with" {
6796 gdb_test_multiple $input $test {
6797 -re "\r\n$gdb_prompt " {
6798 }
6799 }
6800 }
6801 }
6802 }
6803
6804 # Override the 'cd' builtin with a version that ensures that the
6805 # log file keeps pointing at the same file. We need this because
6806 # unfortunately the path to the log file is recorded using an
6807 # relative path name, and, we sometimes need to close/reopen the log
6808 # after changing the current directory. See get_compiler_info.
6809
6810 rename cd builtin_cd
6811
6812 proc cd { dir } {
6813
6814 # Get the existing log file flags.
6815 set log_file_info [log_file -info]
6816
6817 # Split the flags into args and file name.
6818 set log_file_flags ""
6819 set log_file_file ""
6820 foreach arg [ split "$log_file_info" " "] {
6821 if [string match "-*" $arg] {
6822 lappend log_file_flags $arg
6823 } else {
6824 lappend log_file_file $arg
6825 }
6826 }
6827
6828 # If there was an existing file, ensure it is an absolute path, and then
6829 # reset logging.
6830 if { $log_file_file != "" } {
6831 set log_file_file [file normalize $log_file_file]
6832 log_file
6833 log_file $log_file_flags "$log_file_file"
6834 }
6835
6836 # Call the builtin version of cd.
6837 builtin_cd $dir
6838 }
6839
6840 # Return a list of all languages supported by GDB, suitable for use in
6841 # 'set language NAME'. This doesn't include either the 'local' or
6842 # 'auto' keywords.
6843 proc gdb_supported_languages {} {
6844 return [list c objective-c c++ d go fortran modula-2 asm pascal \
6845 opencl rust minimal ada]
6846 }
6847
6848 # Check if debugging is enabled for gdb.
6849
6850 proc gdb_debug_enabled { } {
6851 global gdbdebug
6852
6853 # If not already read, get the debug setting from environment or board setting.
6854 if {![info exists gdbdebug]} {
6855 global env
6856 if [info exists env(GDB_DEBUG)] {
6857 set gdbdebug $env(GDB_DEBUG)
6858 } elseif [target_info exists gdb,debug] {
6859 set gdbdebug [target_info gdb,debug]
6860 } else {
6861 return 0
6862 }
6863 }
6864
6865 # Ensure it not empty.
6866 return [expr { $gdbdebug != "" }]
6867 }
6868
6869 # Turn on debugging if enabled, or reset if already on.
6870
6871 proc gdb_debug_init { } {
6872
6873 global gdb_prompt
6874
6875 if ![gdb_debug_enabled] {
6876 return;
6877 }
6878
6879 # First ensure logging is off.
6880 send_gdb "set logging off\n"
6881
6882 set debugfile [standard_output_file gdb.debug]
6883 send_gdb "set logging file $debugfile\n"
6884
6885 send_gdb "set logging debugredirect\n"
6886
6887 global gdbdebug
6888 foreach entry [split $gdbdebug ,] {
6889 send_gdb "set debug $entry 1\n"
6890 }
6891
6892 # Now that everything is set, enable logging.
6893 send_gdb "set logging on\n"
6894 gdb_expect 10 {
6895 -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {}
6896 timeout { warning "Couldn't set logging file" }
6897 }
6898 }
6899
6900 # Check if debugging is enabled for gdbserver.
6901
6902 proc gdbserver_debug_enabled { } {
6903 # Always disabled for GDB only setups.
6904 return 0
6905 }
6906
6907 # Open the file for logging gdb input
6908
6909 proc gdb_stdin_log_init { } {
6910 global in_file
6911
6912 if {[info exists in_file]} {
6913 # Close existing file.
6914 catch "close $in_file"
6915 }
6916
6917 set logfile [standard_output_file_with_gdb_instance gdb.in]
6918 set in_file [open $logfile w]
6919 }
6920
6921 # Write to the file for logging gdb input.
6922 # TYPE can be one of the following:
6923 # "standard" : Default. Standard message written to the log
6924 # "answer" : Answer to a question (eg "Y"). Not written the log.
6925 # "optional" : Optional message. Not written to the log.
6926
6927 proc gdb_stdin_log_write { message {type standard} } {
6928
6929 global in_file
6930 if {![info exists in_file]} {
6931 return
6932 }
6933
6934 # Check message types.
6935 switch -regexp -- $type {
6936 "answer" {
6937 return
6938 }
6939 "optional" {
6940 return
6941 }
6942 }
6943
6944 #Write to the log
6945 puts -nonewline $in_file "$message"
6946 }
6947
6948 # Write the command line used to invocate gdb to the cmd file.
6949
6950 proc gdb_write_cmd_file { cmdline } {
6951 set logfile [standard_output_file_with_gdb_instance gdb.cmd]
6952 set cmd_file [open $logfile w]
6953 puts $cmd_file $cmdline
6954 catch "close $cmd_file"
6955 }
6956
6957 # Compare contents of FILE to string STR. Pass with MSG if equal, otherwise
6958 # fail with MSG.
6959
6960 proc cmp_file_string { file str msg } {
6961 if { ![file exists $file]} {
6962 fail "$msg"
6963 return
6964 }
6965
6966 set caught_error [catch {
6967 set fp [open "$file" r]
6968 set file_contents [read $fp]
6969 close $fp
6970 } error_message]
6971 if { $caught_error } then {
6972 error "$error_message"
6973 fail "$msg"
6974 return
6975 }
6976
6977 if { $file_contents == $str } {
6978 pass "$msg"
6979 } else {
6980 fail "$msg"
6981 }
6982 }
6983
6984 # Does the compiler support CTF debug output using '-gt' compiler
6985 # flag? If not then we should skip these tests.
6986
6987 gdb_caching_proc skip_ctf_tests {
6988 return ![gdb_can_simple_compile ctfdebug {
6989 int main () {
6990 return 0;
6991 }
6992 } executable "additional_flags=-gt"]
6993 }
6994
6995 # Return 1 if compiler supports -gstatement-frontiers. Otherwise,
6996 # return 0.
6997
6998 gdb_caching_proc supports_statement_frontiers {
6999 return [gdb_can_simple_compile supports_statement_frontiers {
7000 int main () {
7001 return 0;
7002 }
7003 } executable "additional_flags=-gstatement-frontiers"]
7004 }
7005
7006 # Return 1 if symbols were read in using -readnow. Otherwise, return 0.
7007
7008 proc readnow { } {
7009 set cmd "maint print objfiles"
7010 gdb_test_multiple $cmd "" {
7011 -re -wrap "\r\n.gdb_index: faked for \"readnow\"\r\n.*" {
7012 return 1
7013 }
7014 -re -wrap "" {
7015 return 0
7016 }
7017 }
7018
7019 return 0
7020 }
7021
7022 # Return 1 if partial symbols are available. Otherwise, return 0.
7023
7024 proc psymtabs_p { } {
7025 global gdb_prompt
7026
7027 set cmd "maint info psymtab"
7028 gdb_test_multiple $cmd "" {
7029 -re "$cmd\r\n$gdb_prompt $" {
7030 return 0
7031 }
7032 -re -wrap "" {
7033 return 1
7034 }
7035 }
7036
7037 return 0
7038 }
7039
7040 # Verify that partial symtab expansion for $filename has state $readin.
7041
7042 proc verify_psymtab_expanded { filename readin } {
7043 global gdb_prompt
7044
7045 set cmd "maint info psymtab"
7046 set test "$cmd: $filename: $readin"
7047 set re [multi_line \
7048 " \{ psymtab \[^\r\n\]*$filename\[^\r\n\]*" \
7049 " readin $readin" \
7050 ".*"]
7051
7052 gdb_test_multiple $cmd $test {
7053 -re "$cmd\r\n$gdb_prompt $" {
7054 unsupported $gdb_test_name
7055 }
7056 -re -wrap $re {
7057 pass $gdb_test_name
7058 }
7059 }
7060 }
7061
7062 # Add a .gdb_index section to PROGRAM.
7063 # PROGRAM is assumed to be the output of standard_output_file.
7064 # Returns the 0 if there is a failure, otherwise 1.
7065
7066 proc add_gdb_index { program } {
7067 global srcdir GDB env BUILD_DATA_DIRECTORY
7068 set contrib_dir "$srcdir/../contrib"
7069 set env(GDB) "$GDB --data-directory=$BUILD_DATA_DIRECTORY"
7070 set result [catch "exec $contrib_dir/gdb-add-index.sh $program" output]
7071 if { $result != 0 } {
7072 verbose -log "result is $result"
7073 verbose -log "output is $output"
7074 return 0
7075 }
7076
7077 return 1
7078 }
7079
7080 # Add a .gdb_index section to PROGRAM, unless it alread has an index
7081 # (.gdb_index/.debug_names). Gdb doesn't support building an index from a
7082 # program already using one. Return 1 if a .gdb_index was added, return 0
7083 # if it already contained an index, and -1 if an error occurred.
7084
7085 proc ensure_gdb_index { binfile } {
7086 set testfile [file tail $binfile]
7087 set test "check if index present"
7088 gdb_test_multiple "mt print objfiles ${testfile}" $test {
7089 -re -wrap "gdb_index.*" {
7090 return 0
7091 }
7092 -re -wrap "debug_names.*" {
7093 return 0
7094 }
7095 -re -wrap "Psymtabs.*" {
7096 if { [add_gdb_index $binfile] != "1" } {
7097 return -1
7098 }
7099 return 1
7100 }
7101 }
7102 return -1
7103 }
7104
7105 # Always load compatibility stuff.
7106 load_lib future.exp
This page took 0.176987 seconds and 5 git commands to generate.