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