[gdb/testsuite] Run read1 timeout tests with with_read1_timeout_factor
[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 return [file join $dir $basename]
4728 }
4729
4730 # Turn BASENAME into a full file name in the standard output directory. If
4731 # GDB has been launched more than once then append the count, starting with
4732 # a ".1" postfix.
4733
4734 proc standard_output_file_with_gdb_instance {basename} {
4735 global gdb_instances
4736 set count [expr $gdb_instances - 1 ]
4737
4738 if {$count == 0} {
4739 return [standard_output_file $basename]
4740 }
4741 return [standard_output_file ${basename}.${count}]
4742 }
4743
4744 # Return the name of a file in our standard temporary directory.
4745
4746 proc standard_temp_file {basename} {
4747 # Since a particular runtest invocation is only executing a single test
4748 # file at any given time, we can use the runtest pid to build the
4749 # path of the temp directory.
4750 set dir [make_gdb_parallel_path temp [pid]]
4751 file mkdir $dir
4752 return [file join $dir $basename]
4753 }
4754
4755 # Set 'testfile', 'srcfile', and 'binfile'.
4756 #
4757 # ARGS is a list of source file specifications.
4758 # Without any arguments, the .exp file's base name is used to
4759 # compute the source file name. The ".c" extension is added in this case.
4760 # If ARGS is not empty, each entry is a source file specification.
4761 # If the specification starts with a ".", it is treated as a suffix
4762 # to append to the .exp file's base name.
4763 # If the specification is the empty string, it is treated as if it
4764 # were ".c".
4765 # Otherwise it is a file name.
4766 # The first file in the list is used to set the 'srcfile' global.
4767 # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc.
4768 #
4769 # Most tests should call this without arguments.
4770 #
4771 # If a completely different binary file name is needed, then it
4772 # should be handled in the .exp file with a suitable comment.
4773
4774 proc standard_testfile {args} {
4775 global gdb_test_file_name
4776 global subdir
4777 global gdb_test_file_last_vars
4778
4779 # Outputs.
4780 global testfile binfile
4781
4782 set testfile $gdb_test_file_name
4783 set binfile [standard_output_file ${testfile}]
4784
4785 if {[llength $args] == 0} {
4786 set args .c
4787 }
4788
4789 # Unset our previous output variables.
4790 # This can help catch hidden bugs.
4791 if {[info exists gdb_test_file_last_vars]} {
4792 foreach varname $gdb_test_file_last_vars {
4793 global $varname
4794 catch {unset $varname}
4795 }
4796 }
4797 # 'executable' is often set by tests.
4798 set gdb_test_file_last_vars {executable}
4799
4800 set suffix ""
4801 foreach arg $args {
4802 set varname srcfile$suffix
4803 global $varname
4804
4805 # Handle an extension.
4806 if {$arg == ""} {
4807 set arg $testfile.c
4808 } elseif {[string range $arg 0 0] == "."} {
4809 set arg $testfile$arg
4810 }
4811
4812 set $varname $arg
4813 lappend gdb_test_file_last_vars $varname
4814
4815 if {$suffix == ""} {
4816 set suffix 2
4817 } else {
4818 incr suffix
4819 }
4820 }
4821 }
4822
4823 # The default timeout used when testing GDB commands. We want to use
4824 # the same timeout as the default dejagnu timeout, unless the user has
4825 # already provided a specific value (probably through a site.exp file).
4826 global gdb_test_timeout
4827 if ![info exists gdb_test_timeout] {
4828 set gdb_test_timeout $timeout
4829 }
4830
4831 # A list of global variables that GDB testcases should not use.
4832 # We try to prevent their use by monitoring write accesses and raising
4833 # an error when that happens.
4834 set banned_variables { bug_id prms_id }
4835
4836 # A list of procedures that GDB testcases should not use.
4837 # We try to prevent their use by monitoring invocations and raising
4838 # an error when that happens.
4839 set banned_procedures { strace }
4840
4841 # gdb_init is called by runtest at start, but also by several
4842 # tests directly; gdb_finish is only called from within runtest after
4843 # each test source execution.
4844 # Placing several traces by repetitive calls to gdb_init leads
4845 # to problems, as only one trace is removed in gdb_finish.
4846 # To overcome this possible problem, we add a variable that records
4847 # if the banned variables and procedures are already traced.
4848 set banned_traced 0
4849
4850 proc gdb_init { test_file_name } {
4851 # Reset the timeout value to the default. This way, any testcase
4852 # that changes the timeout value without resetting it cannot affect
4853 # the timeout used in subsequent testcases.
4854 global gdb_test_timeout
4855 global timeout
4856 set timeout $gdb_test_timeout
4857
4858 if { [regexp ".*gdb\.reverse\/.*" $test_file_name]
4859 && [target_info exists gdb_reverse_timeout] } {
4860 set timeout [target_info gdb_reverse_timeout]
4861 }
4862
4863 # If GDB_INOTIFY is given, check for writes to '.'. This is a
4864 # debugging tool to help confirm that the test suite is
4865 # parallel-safe. You need "inotifywait" from the
4866 # inotify-tools package to use this.
4867 global GDB_INOTIFY inotify_pid
4868 if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} {
4869 global outdir tool inotify_log_file
4870
4871 set exclusions {outputs temp gdb[.](log|sum) cache}
4872 set exclusion_re ([join $exclusions |])
4873
4874 set inotify_log_file [standard_temp_file inotify.out]
4875 set inotify_pid [exec inotifywait -r -m -e move,create,delete . \
4876 --exclude $exclusion_re \
4877 |& tee -a $outdir/$tool.log $inotify_log_file &]
4878
4879 # Wait for the watches; hopefully this is long enough.
4880 sleep 2
4881
4882 # Clear the log so that we don't emit a warning the first time
4883 # we check it.
4884 set fd [open $inotify_log_file w]
4885 close $fd
4886 }
4887
4888 # Block writes to all banned variables, and invocation of all
4889 # banned procedures...
4890 global banned_variables
4891 global banned_procedures
4892 global banned_traced
4893 if (!$banned_traced) {
4894 foreach banned_var $banned_variables {
4895 global "$banned_var"
4896 trace add variable "$banned_var" write error
4897 }
4898 foreach banned_proc $banned_procedures {
4899 global "$banned_proc"
4900 trace add execution "$banned_proc" enter error
4901 }
4902 set banned_traced 1
4903 }
4904
4905 # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same
4906 # messages as expected.
4907 setenv LC_ALL C
4908 setenv LC_CTYPE C
4909 setenv LANG C
4910
4911 # Don't let a .inputrc file or an existing setting of INPUTRC mess up
4912 # the test results. Even if /dev/null doesn't exist on the particular
4913 # platform, the readline library will use the default setting just by
4914 # failing to open the file. OTOH, opening /dev/null successfully will
4915 # also result in the default settings being used since nothing will be
4916 # read from this file.
4917 setenv INPUTRC "/dev/null"
4918
4919 # This disables style output, which would interfere with many
4920 # tests.
4921 setenv TERM "dumb"
4922
4923 # Initialize GDB's pty with a fixed size, to make sure we avoid pagination
4924 # during startup. See "man expect" for details about stty_init.
4925 global stty_init
4926 set stty_init "rows 25 cols 80"
4927
4928 # Some tests (for example gdb.base/maint.exp) shell out from gdb to use
4929 # grep. Clear GREP_OPTIONS to make the behavior predictable,
4930 # especially having color output turned on can cause tests to fail.
4931 setenv GREP_OPTIONS ""
4932
4933 # Clear $gdbserver_reconnect_p.
4934 global gdbserver_reconnect_p
4935 set gdbserver_reconnect_p 1
4936 unset gdbserver_reconnect_p
4937
4938 # Reset GDB number of instances
4939 global gdb_instances
4940 set gdb_instances 0
4941
4942 return [default_gdb_init $test_file_name]
4943 }
4944
4945 proc gdb_finish { } {
4946 global gdbserver_reconnect_p
4947 global gdb_prompt
4948 global cleanfiles
4949
4950 # Exit first, so that the files are no longer in use.
4951 gdb_exit
4952
4953 if { [llength $cleanfiles] > 0 } {
4954 eval remote_file target delete $cleanfiles
4955 set cleanfiles {}
4956 }
4957
4958 # Unblock write access to the banned variables. Dejagnu typically
4959 # resets some of them between testcases.
4960 global banned_variables
4961 global banned_procedures
4962 global banned_traced
4963 if ($banned_traced) {
4964 foreach banned_var $banned_variables {
4965 global "$banned_var"
4966 trace remove variable "$banned_var" write error
4967 }
4968 foreach banned_proc $banned_procedures {
4969 global "$banned_proc"
4970 trace remove execution "$banned_proc" enter error
4971 }
4972 set banned_traced 0
4973 }
4974 }
4975
4976 global debug_format
4977 set debug_format "unknown"
4978
4979 # Run the gdb command "info source" and extract the debugging format
4980 # information from the output and save it in debug_format.
4981
4982 proc get_debug_format { } {
4983 global gdb_prompt
4984 global verbose
4985 global expect_out
4986 global debug_format
4987
4988 set debug_format "unknown"
4989 send_gdb "info source\n"
4990 gdb_expect 10 {
4991 -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" {
4992 set debug_format $expect_out(1,string)
4993 verbose "debug format is $debug_format"
4994 return 1
4995 }
4996 -re "No current source file.\r\n$gdb_prompt $" {
4997 perror "get_debug_format used when no current source file"
4998 return 0
4999 }
5000 -re "$gdb_prompt $" {
5001 warning "couldn't check debug format (no valid response)."
5002 return 1
5003 }
5004 timeout {
5005 warning "couldn't check debug format (timeout)."
5006 return 1
5007 }
5008 }
5009 }
5010
5011 # Return true if FORMAT matches the debug format the current test was
5012 # compiled with. FORMAT is a shell-style globbing pattern; it can use
5013 # `*', `[...]', and so on.
5014 #
5015 # This function depends on variables set by `get_debug_format', above.
5016
5017 proc test_debug_format {format} {
5018 global debug_format
5019
5020 return [expr [string match $format $debug_format] != 0]
5021 }
5022
5023 # Like setup_xfail, but takes the name of a debug format (DWARF 1,
5024 # COFF, stabs, etc). If that format matches the format that the
5025 # current test was compiled with, then the next test is expected to
5026 # fail for any target. Returns 1 if the next test or set of tests is
5027 # expected to fail, 0 otherwise (or if it is unknown). Must have
5028 # previously called get_debug_format.
5029 proc setup_xfail_format { format } {
5030 set ret [test_debug_format $format]
5031
5032 if {$ret} then {
5033 setup_xfail "*-*-*"
5034 }
5035 return $ret
5036 }
5037
5038 # gdb_get_line_number TEXT [FILE]
5039 #
5040 # Search the source file FILE, and return the line number of the
5041 # first line containing TEXT. If no match is found, an error is thrown.
5042 #
5043 # TEXT is a string literal, not a regular expression.
5044 #
5045 # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is
5046 # specified, and does not start with "/", then it is assumed to be in
5047 # "$srcdir/$subdir". This is awkward, and can be fixed in the future,
5048 # by changing the callers and the interface at the same time.
5049 # In particular: gdb.base/break.exp, gdb.base/condbreak.exp,
5050 # gdb.base/ena-dis-br.exp.
5051 #
5052 # Use this function to keep your test scripts independent of the
5053 # exact line numbering of the source file. Don't write:
5054 #
5055 # send_gdb "break 20"
5056 #
5057 # This means that if anyone ever edits your test's source file,
5058 # your test could break. Instead, put a comment like this on the
5059 # source file line you want to break at:
5060 #
5061 # /* breakpoint spot: frotz.exp: test name */
5062 #
5063 # and then write, in your test script (which we assume is named
5064 # frotz.exp):
5065 #
5066 # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n"
5067 #
5068 # (Yes, Tcl knows how to handle the nested quotes and brackets.
5069 # Try this:
5070 # $ tclsh
5071 # % puts "foo [lindex "bar baz" 1]"
5072 # foo baz
5073 # %
5074 # Tcl is quite clever, for a little stringy language.)
5075 #
5076 # ===
5077 #
5078 # The previous implementation of this procedure used the gdb search command.
5079 # This version is different:
5080 #
5081 # . It works with MI, and it also works when gdb is not running.
5082 #
5083 # . It operates on the build machine, not the host machine.
5084 #
5085 # . For now, this implementation fakes a current directory of
5086 # $srcdir/$subdir to be compatible with the old implementation.
5087 # This will go away eventually and some callers will need to
5088 # be changed.
5089 #
5090 # . The TEXT argument is literal text and matches literally,
5091 # not a regular expression as it was before.
5092 #
5093 # . State changes in gdb, such as changing the current file
5094 # and setting $_, no longer happen.
5095 #
5096 # After a bit of time we can forget about the differences from the
5097 # old implementation.
5098 #
5099 # --chastain 2004-08-05
5100
5101 proc gdb_get_line_number { text { file "" } } {
5102 global srcdir
5103 global subdir
5104 global srcfile
5105
5106 if { "$file" == "" } then {
5107 set file "$srcfile"
5108 }
5109 if { ! [regexp "^/" "$file"] } then {
5110 set file "$srcdir/$subdir/$file"
5111 }
5112
5113 if { [ catch { set fd [open "$file"] } message ] } then {
5114 error "$message"
5115 }
5116
5117 set found -1
5118 for { set line 1 } { 1 } { incr line } {
5119 if { [ catch { set nchar [gets "$fd" body] } message ] } then {
5120 error "$message"
5121 }
5122 if { $nchar < 0 } then {
5123 break
5124 }
5125 if { [string first "$text" "$body"] >= 0 } then {
5126 set found $line
5127 break
5128 }
5129 }
5130
5131 if { [ catch { close "$fd" } message ] } then {
5132 error "$message"
5133 }
5134
5135 if {$found == -1} {
5136 error "undefined tag \"$text\""
5137 }
5138
5139 return $found
5140 }
5141
5142 # Continue the program until it ends.
5143 #
5144 # MSSG is the error message that gets printed. If not given, a
5145 # default is used.
5146 # COMMAND is the command to invoke. If not given, "continue" is
5147 # used.
5148 # ALLOW_EXTRA is a flag indicating whether the test should expect
5149 # extra output between the "Continuing." line and the program
5150 # exiting. By default it is zero; if nonzero, any extra output
5151 # is accepted.
5152
5153 proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} {
5154 global inferior_exited_re use_gdb_stub
5155
5156 if {$mssg == ""} {
5157 set text "continue until exit"
5158 } else {
5159 set text "continue until exit at $mssg"
5160 }
5161 if {$allow_extra} {
5162 set extra ".*"
5163 } else {
5164 set extra ""
5165 }
5166
5167 # By default, we don't rely on exit() behavior of remote stubs --
5168 # it's common for exit() to be implemented as a simple infinite
5169 # loop, or a forced crash/reset. For native targets, by default, we
5170 # assume process exit is reported as such. If a non-reliable target
5171 # is used, we set a breakpoint at exit, and continue to that.
5172 if { [target_info exists exit_is_reliable] } {
5173 set exit_is_reliable [target_info exit_is_reliable]
5174 } else {
5175 set exit_is_reliable [expr ! $use_gdb_stub]
5176 }
5177
5178 if { ! $exit_is_reliable } {
5179 if {![gdb_breakpoint "exit"]} {
5180 return 0
5181 }
5182 gdb_test $command "Continuing..*Breakpoint .*exit.*" \
5183 $text
5184 } else {
5185 # Continue until we exit. Should not stop again.
5186 # Don't bother to check the output of the program, that may be
5187 # extremely tough for some remote systems.
5188 gdb_test $command \
5189 "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\
5190 $text
5191 }
5192 }
5193
5194 proc rerun_to_main {} {
5195 global gdb_prompt use_gdb_stub
5196
5197 if $use_gdb_stub {
5198 gdb_run_cmd
5199 gdb_expect {
5200 -re ".*Breakpoint .*main .*$gdb_prompt $"\
5201 {pass "rerun to main" ; return 0}
5202 -re "$gdb_prompt $"\
5203 {fail "rerun to main" ; return 0}
5204 timeout {fail "(timeout) rerun to main" ; return 0}
5205 }
5206 } else {
5207 send_gdb "run\n"
5208 gdb_expect {
5209 -re "The program .* has been started already.*y or n. $" {
5210 send_gdb "y\n" answer
5211 exp_continue
5212 }
5213 -re "Starting program.*$gdb_prompt $"\
5214 {pass "rerun to main" ; return 0}
5215 -re "$gdb_prompt $"\
5216 {fail "rerun to main" ; return 0}
5217 timeout {fail "(timeout) rerun to main" ; return 0}
5218 }
5219 }
5220 }
5221
5222 # Return true if EXECUTABLE contains a .gdb_index or .debug_names index section.
5223
5224 proc exec_has_index_section { executable } {
5225 set readelf_program [gdb_find_readelf]
5226 set res [catch {exec $readelf_program -S $executable \
5227 | grep -E "\.gdb_index|\.debug_names" }]
5228 if { $res == 0 } {
5229 return 1
5230 }
5231 return 0
5232 }
5233
5234 # Return list with major and minor version of readelf, or an empty list.
5235 gdb_caching_proc readelf_version {
5236 set readelf_program [gdb_find_readelf]
5237 set res [catch {exec $readelf_program --version} output]
5238 if { $res != 0 } {
5239 return [list]
5240 }
5241 set lines [split $output \n]
5242 set line [lindex $lines 0]
5243 set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \
5244 $line dummy major minor]
5245 if { $res != 1 } {
5246 return [list]
5247 }
5248 return [list $major $minor]
5249 }
5250
5251 # Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown.
5252 proc readelf_prints_pie { } {
5253 set version [readelf_version]
5254 if { [llength $version] == 0 } {
5255 return -1
5256 }
5257 set major [lindex $version 0]
5258 set minor [lindex $version 1]
5259 # It would be better to construct a PIE executable and test if the PIE
5260 # flag is printed by readelf, but we cannot reliably construct a PIE
5261 # executable if the multilib_flags dictate otherwise
5262 # (--target_board=unix/-no-pie/-fno-PIE).
5263 return [version_at_least $major $minor 2 26]
5264 }
5265
5266 # Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not,
5267 # and -1 if unknown.
5268
5269 proc exec_is_pie { executable } {
5270 set res [readelf_prints_pie]
5271 if { $res != 1 } {
5272 return -1
5273 }
5274 set readelf_program [gdb_find_readelf]
5275 set res [catch {exec $readelf_program -d $executable} output]
5276 if { $res != 0 } {
5277 return -1
5278 }
5279 set res [regexp -line {\(FLAGS_1\).*Flags:.* PIE($| )} $output]
5280 if { $res == 1 } {
5281 return 1
5282 }
5283 return 0
5284 }
5285
5286 # Return true if a test should be skipped due to lack of floating
5287 # point support or GDB can't fetch the contents from floating point
5288 # registers.
5289
5290 gdb_caching_proc gdb_skip_float_test {
5291 if [target_info exists gdb,skip_float_tests] {
5292 return 1
5293 }
5294
5295 # There is an ARM kernel ptrace bug that hardware VFP registers
5296 # are not updated after GDB ptrace set VFP registers. The bug
5297 # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f
5298 # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf
5299 # in May 2016. In other words, kernels older than 4.6.3, 4.4.14,
5300 # 4.1.27, 3.18.36, and 3.14.73 have this bug.
5301 # This kernel bug is detected by check how does GDB change the
5302 # program result by changing one VFP register.
5303 if { [istarget "arm*-*-linux*"] } {
5304
5305 set compile_flags {debug nowarnings }
5306
5307 # Set up, compile, and execute a test program having VFP
5308 # operations.
5309 set src [standard_temp_file arm_vfp[pid].c]
5310 set exe [standard_temp_file arm_vfp[pid].x]
5311
5312 gdb_produce_source $src {
5313 int main() {
5314 double d = 4.0;
5315 int ret;
5316
5317 asm ("vldr d0, [%0]" : : "r" (&d));
5318 asm ("vldr d1, [%0]" : : "r" (&d));
5319 asm (".global break_here\n"
5320 "break_here:");
5321 asm ("vcmp.f64 d0, d1\n"
5322 "vmrs APSR_nzcv, fpscr\n"
5323 "bne L_value_different\n"
5324 "movs %0, #0\n"
5325 "b L_end\n"
5326 "L_value_different:\n"
5327 "movs %0, #1\n"
5328 "L_end:\n" : "=r" (ret) :);
5329
5330 /* Return $d0 != $d1. */
5331 return ret;
5332 }
5333 }
5334
5335 verbose "compiling testfile $src" 2
5336 set lines [gdb_compile $src $exe executable $compile_flags]
5337 file delete $src
5338
5339 if ![string match "" $lines] then {
5340 verbose "testfile compilation failed, returning 1" 2
5341 return 0
5342 }
5343
5344 # No error message, compilation succeeded so now run it via gdb.
5345 # Run the test up to 5 times to detect whether ptrace can
5346 # correctly update VFP registers or not.
5347 set skip_vfp_test 0
5348 for {set i 0} {$i < 5} {incr i} {
5349 global gdb_prompt srcdir subdir
5350
5351 gdb_exit
5352 gdb_start
5353 gdb_reinitialize_dir $srcdir/$subdir
5354 gdb_load "$exe"
5355
5356 runto_main
5357 gdb_test "break *break_here"
5358 gdb_continue_to_breakpoint "break_here"
5359
5360 # Modify $d0 to a different value, so the exit code should
5361 # be 1.
5362 gdb_test "set \$d0 = 5.0"
5363
5364 set test "continue to exit"
5365 gdb_test_multiple "continue" "$test" {
5366 -re "exited with code 01.*$gdb_prompt $" {
5367 }
5368 -re "exited normally.*$gdb_prompt $" {
5369 # However, the exit code is 0. That means something
5370 # wrong in setting VFP registers.
5371 set skip_vfp_test 1
5372 break
5373 }
5374 }
5375 }
5376
5377 gdb_exit
5378 remote_file build delete $exe
5379
5380 return $skip_vfp_test
5381 }
5382 return 0
5383 }
5384
5385 # Print a message and return true if a test should be skipped
5386 # due to lack of stdio support.
5387
5388 proc gdb_skip_stdio_test { msg } {
5389 if [target_info exists gdb,noinferiorio] {
5390 verbose "Skipping test '$msg': no inferior i/o."
5391 return 1
5392 }
5393 return 0
5394 }
5395
5396 proc gdb_skip_bogus_test { msg } {
5397 return 0
5398 }
5399
5400 # Return true if a test should be skipped due to lack of XML support
5401 # in the host GDB.
5402 # NOTE: This must be called while gdb is *not* running.
5403
5404 gdb_caching_proc gdb_skip_xml_test {
5405 global gdb_spawn_id
5406 global gdb_prompt
5407 global srcdir
5408
5409 if { [info exists gdb_spawn_id] } {
5410 error "GDB must not be running in gdb_skip_xml_tests."
5411 }
5412
5413 set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
5414
5415 gdb_start
5416 set xml_missing 0
5417 gdb_test_multiple "set tdesc filename $xml_file" "" {
5418 -re ".*XML support was disabled at compile time.*$gdb_prompt $" {
5419 set xml_missing 1
5420 }
5421 -re ".*$gdb_prompt $" { }
5422 }
5423 gdb_exit
5424 return $xml_missing
5425 }
5426
5427 # Return true if argv[0] is available.
5428
5429 gdb_caching_proc gdb_has_argv0 {
5430 set result 0
5431
5432 # Compile and execute a test program to check whether argv[0] is available.
5433 gdb_simple_compile has_argv0 {
5434 int main (int argc, char **argv) {
5435 return 0;
5436 }
5437 } executable
5438
5439
5440 # Helper proc.
5441 proc gdb_has_argv0_1 { exe } {
5442 global srcdir subdir
5443 global gdb_prompt hex
5444
5445 gdb_exit
5446 gdb_start
5447 gdb_reinitialize_dir $srcdir/$subdir
5448 gdb_load "$exe"
5449
5450 # Set breakpoint on main.
5451 gdb_test_multiple "break main" "break main" {
5452 -re "Breakpoint.*${gdb_prompt} $" {
5453 }
5454 -re "${gdb_prompt} $" {
5455 return 0
5456 }
5457 }
5458
5459 # Run to main.
5460 gdb_run_cmd
5461 gdb_test_multiple "" "run to main" {
5462 -re "Breakpoint.*${gdb_prompt} $" {
5463 }
5464 -re "${gdb_prompt} $" {
5465 return 0
5466 }
5467 }
5468
5469 set old_elements "200"
5470 set test "show print elements"
5471 gdb_test_multiple $test $test {
5472 -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5473 set old_elements $expect_out(1,string)
5474 }
5475 }
5476 set old_repeats "200"
5477 set test "show print repeats"
5478 gdb_test_multiple $test $test {
5479 -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" {
5480 set old_repeats $expect_out(1,string)
5481 }
5482 }
5483 gdb_test_no_output "set print elements unlimited" ""
5484 gdb_test_no_output "set print repeats unlimited" ""
5485
5486 set retval 0
5487 # Check whether argc is 1.
5488 gdb_test_multiple "p argc" "p argc" {
5489 -re " = 1\r\n${gdb_prompt} $" {
5490
5491 gdb_test_multiple "p argv\[0\]" "p argv\[0\]" {
5492 -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" {
5493 set retval 1
5494 }
5495 -re "${gdb_prompt} $" {
5496 }
5497 }
5498 }
5499 -re "${gdb_prompt} $" {
5500 }
5501 }
5502
5503 gdb_test_no_output "set print elements $old_elements" ""
5504 gdb_test_no_output "set print repeats $old_repeats" ""
5505
5506 return $retval
5507 }
5508
5509 set result [gdb_has_argv0_1 $obj]
5510
5511 gdb_exit
5512 file delete $obj
5513
5514 if { !$result
5515 && ([istarget *-*-linux*]
5516 || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*]
5517 || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*]
5518 || [istarget *-*-openbsd*]
5519 || [istarget *-*-darwin*]
5520 || [istarget *-*-solaris*]
5521 || [istarget *-*-aix*]
5522 || [istarget *-*-gnu*]
5523 || [istarget *-*-cygwin*] || [istarget *-*-mingw32*]
5524 || [istarget *-*-*djgpp*] || [istarget *-*-go32*]
5525 || [istarget *-wince-pe] || [istarget *-*-mingw32ce*]
5526 || [istarget *-*-symbianelf*]
5527 || [istarget *-*-osf*]
5528 || [istarget *-*-dicos*]
5529 || [istarget *-*-nto*]
5530 || [istarget *-*-*vms*]
5531 || [istarget *-*-lynx*178]) } {
5532 fail "argv\[0\] should be available on this target"
5533 }
5534
5535 return $result
5536 }
5537
5538 # Note: the procedure gdb_gnu_strip_debug will produce an executable called
5539 # ${binfile}.dbglnk, which is just like the executable ($binfile) but without
5540 # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains
5541 # the name of a debuginfo only file. This file will be stored in the same
5542 # subdirectory.
5543
5544 # Functions for separate debug info testing
5545
5546 # starting with an executable:
5547 # foo --> original executable
5548
5549 # at the end of the process we have:
5550 # foo.stripped --> foo w/o debug info
5551 # foo.debug --> foo's debug info
5552 # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug.
5553
5554 # Fetch the build id from the file.
5555 # Returns "" if there is none.
5556
5557 proc get_build_id { filename } {
5558 if { ([istarget "*-*-mingw*"]
5559 || [istarget *-*-cygwin*]) } {
5560 set objdump_program [gdb_find_objdump]
5561 set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output]
5562 verbose "result is $result"
5563 verbose "output is $output"
5564 if {$result == 1} {
5565 return ""
5566 }
5567 return $data
5568 } else {
5569 set tmp [standard_output_file "${filename}-tmp"]
5570 set objcopy_program [gdb_find_objcopy]
5571 set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output]
5572 verbose "result is $result"
5573 verbose "output is $output"
5574 if {$result == 1} {
5575 return ""
5576 }
5577 set fi [open $tmp]
5578 fconfigure $fi -translation binary
5579 # Skip the NOTE header.
5580 read $fi 16
5581 set data [read $fi]
5582 close $fi
5583 file delete $tmp
5584 if ![string compare $data ""] then {
5585 return ""
5586 }
5587 # Convert it to hex.
5588 binary scan $data H* data
5589 return $data
5590 }
5591 }
5592
5593 # Return the build-id hex string (usually 160 bits as 40 hex characters)
5594 # converted to the form: .build-id/ab/cdef1234...89.debug
5595 # Return "" if no build-id found.
5596 proc build_id_debug_filename_get { filename } {
5597 set data [get_build_id $filename]
5598 if { $data == "" } {
5599 return ""
5600 }
5601 regsub {^..} $data {\0/} data
5602 return ".build-id/${data}.debug"
5603 }
5604
5605 # Create stripped files for DEST, replacing it. If ARGS is passed, it is a
5606 # list of optional flags. The only currently supported flag is no-main,
5607 # which removes the symbol entry for main from the separate debug file.
5608 #
5609 # Function returns zero on success. Function will return non-zero failure code
5610 # on some targets not supporting separate debug info (such as i386-msdos).
5611
5612 proc gdb_gnu_strip_debug { dest args } {
5613
5614 # Use the first separate debug info file location searched by GDB so the
5615 # run cannot be broken by some stale file searched with higher precedence.
5616 set debug_file "${dest}.debug"
5617
5618 set strip_to_file_program [transform strip]
5619 set objcopy_program [gdb_find_objcopy]
5620
5621 set debug_link [file tail $debug_file]
5622 set stripped_file "${dest}.stripped"
5623
5624 # Get rid of the debug info, and store result in stripped_file
5625 # something like gdb/testsuite/gdb.base/blah.stripped.
5626 set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output]
5627 verbose "result is $result"
5628 verbose "output is $output"
5629 if {$result == 1} {
5630 return 1
5631 }
5632
5633 # Workaround PR binutils/10802:
5634 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5635 set perm [file attributes ${dest} -permissions]
5636 file attributes ${stripped_file} -permissions $perm
5637
5638 # Get rid of everything but the debug info, and store result in debug_file
5639 # This will be in the .debug subdirectory, see above.
5640 set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output]
5641 verbose "result is $result"
5642 verbose "output is $output"
5643 if {$result == 1} {
5644 return 1
5645 }
5646
5647 # If no-main is passed, strip the symbol for main from the separate
5648 # file. This is to simulate the behavior of elfutils's eu-strip, which
5649 # leaves the symtab in the original file only. There's no way to get
5650 # objcopy or strip to remove the symbol table without also removing the
5651 # debugging sections, so this is as close as we can get.
5652 if { [llength $args] == 1 && [lindex $args 0] == "no-main" } {
5653 set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output]
5654 verbose "result is $result"
5655 verbose "output is $output"
5656 if {$result == 1} {
5657 return 1
5658 }
5659 file delete "${debug_file}"
5660 file rename "${debug_file}-tmp" "${debug_file}"
5661 }
5662
5663 # Link the two previous output files together, adding the .gnu_debuglink
5664 # section to the stripped_file, containing a pointer to the debug_file,
5665 # save the new file in dest.
5666 # This will be the regular executable filename, in the usual location.
5667 set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output]
5668 verbose "result is $result"
5669 verbose "output is $output"
5670 if {$result == 1} {
5671 return 1
5672 }
5673
5674 # Workaround PR binutils/10802:
5675 # Preserve the 'x' bit also for PIEs (Position Independent Executables).
5676 set perm [file attributes ${stripped_file} -permissions]
5677 file attributes ${dest} -permissions $perm
5678
5679 return 0
5680 }
5681
5682 # Test the output of GDB_COMMAND matches the pattern obtained
5683 # by concatenating all elements of EXPECTED_LINES. This makes
5684 # it possible to split otherwise very long string into pieces.
5685 # If third argument TESTNAME is not empty, it's used as the name of the
5686 # test to be printed on pass/fail.
5687 proc help_test_raw { gdb_command expected_lines {testname {}} } {
5688 if {$testname == {}} {
5689 set message $gdb_command
5690 } else {
5691 set message $testname
5692 }
5693 set expected_output [join $expected_lines ""]
5694 gdb_test "${gdb_command}" "${expected_output}" $message
5695 }
5696
5697 # A regexp that matches the end of help CLASS|PREFIX_COMMAND
5698 set help_list_trailer {
5699 "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+"
5700 "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+"
5701 "Command name abbreviations are allowed if unambiguous\."
5702 }
5703
5704 # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES
5705 # are regular expressions that should match the beginning of output,
5706 # before the list of commands in that class.
5707 # LIST_OF_COMMANDS are regular expressions that should match the
5708 # list of commands in that class. If empty, the command list will be
5709 # matched automatically. The presence of standard epilogue will be tested
5710 # automatically.
5711 # If last argument TESTNAME is not empty, it's used as the name of the
5712 # test to be printed on pass/fail.
5713 # Notice that the '[' and ']' characters don't need to be escaped for strings
5714 # wrapped in {} braces.
5715 proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } {
5716 global help_list_trailer
5717 if {[llength $list_of_commands]>0} {
5718 set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"}
5719 set l_list_of_commands [concat $l_list_of_commands $list_of_commands]
5720 set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}]
5721 } else {
5722 set l_list_of_commands {"List of commands\:.*[\r\n]+"}
5723 }
5724 set l_stock_body {
5725 "Type \"help\" followed by command name for full documentation\.[\r\n]+"
5726 }
5727 set l_entire_body [concat $expected_initial_lines $l_list_of_commands \
5728 $l_stock_body $help_list_trailer]
5729
5730 help_test_raw "help ${command_class}" $l_entire_body $testname
5731 }
5732
5733 # Like test_class_help but specialised to test "help user-defined".
5734 proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } {
5735 test_class_help "user-defined" {
5736 "User-defined commands\.[\r\n]+"
5737 "The commands in this class are those defined by the user\.[\r\n]+"
5738 "Use the \"define\" command to define a command\.[\r\n]+"
5739 } $list_of_commands $testname
5740 }
5741
5742
5743 # COMMAND_LIST should have either one element -- command to test, or
5744 # two elements -- abbreviated command to test, and full command the first
5745 # element is abbreviation of.
5746 # The command must be a prefix command. EXPECTED_INITIAL_LINES
5747 # are regular expressions that should match the beginning of output,
5748 # before the list of subcommands. The presence of
5749 # subcommand list and standard epilogue will be tested automatically.
5750 proc test_prefix_command_help { command_list expected_initial_lines args } {
5751 global help_list_trailer
5752 set command [lindex $command_list 0]
5753 if {[llength $command_list]>1} {
5754 set full_command [lindex $command_list 1]
5755 } else {
5756 set full_command $command
5757 }
5758 # Use 'list' and not just {} because we want variables to
5759 # be expanded in this list.
5760 set l_stock_body [list\
5761 "List of $full_command subcommands\:.*\[\r\n\]+"\
5762 "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"]
5763 set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer]
5764 if {[llength $args]>0} {
5765 help_test_raw "help ${command}" $l_entire_body [lindex $args 0]
5766 } else {
5767 help_test_raw "help ${command}" $l_entire_body
5768 }
5769 }
5770
5771 # Build executable named EXECUTABLE from specifications that allow
5772 # different options to be passed to different sub-compilations.
5773 # TESTNAME is the name of the test; this is passed to 'untested' if
5774 # something fails.
5775 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
5776 # contains the option "pthreads", then gdb_compile_pthreads is used.
5777 # ARGS is a flat list of source specifications, of the form:
5778 # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... }
5779 # Each SOURCE is compiled to an object file using its OPTIONS,
5780 # using gdb_compile.
5781 # Returns 0 on success, -1 on failure.
5782 proc build_executable_from_specs {testname executable options args} {
5783 global subdir
5784 global srcdir
5785
5786 set binfile [standard_output_file $executable]
5787
5788 set info_options ""
5789 if { [lsearch -exact $options "c++"] >= 0 } {
5790 set info_options "c++"
5791 }
5792 if [get_compiler_info ${info_options}] {
5793 return -1
5794 }
5795
5796 set func gdb_compile
5797 set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads)$}]
5798 if {$func_index != -1} {
5799 set func "${func}_[lindex $options $func_index]"
5800 }
5801
5802 # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd
5803 # parameter. They also requires $sources while gdb_compile and
5804 # gdb_compile_pthreads require $objects. Moreover they ignore any options.
5805 if [string match gdb_compile_shlib* $func] {
5806 set sources_path {}
5807 foreach {s local_options} $args {
5808 if { [regexp "^/" "$s"] } then {
5809 lappend sources_path "$s"
5810 } else {
5811 lappend sources_path "$srcdir/$subdir/$s"
5812 }
5813 }
5814 set ret [$func $sources_path "${binfile}" $options]
5815 } elseif {[lsearch -exact $options rust] != -1} {
5816 set sources_path {}
5817 foreach {s local_options} $args {
5818 if { [regexp "^/" "$s"] } then {
5819 lappend sources_path "$s"
5820 } else {
5821 lappend sources_path "$srcdir/$subdir/$s"
5822 }
5823 }
5824 set ret [gdb_compile_rust $sources_path "${binfile}" $options]
5825 } else {
5826 set objects {}
5827 set i 0
5828 foreach {s local_options} $args {
5829 if { ! [regexp "^/" "$s"] } then {
5830 set s "$srcdir/$subdir/$s"
5831 }
5832 if { [gdb_compile "${s}" "${binfile}${i}.o" object $local_options] != "" } {
5833 untested $testname
5834 return -1
5835 }
5836 lappend objects "${binfile}${i}.o"
5837 incr i
5838 }
5839 set ret [$func $objects "${binfile}" executable $options]
5840 }
5841 if { $ret != "" } {
5842 untested $testname
5843 return -1
5844 }
5845
5846 return 0
5847 }
5848
5849 # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not
5850 # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test
5851 # to pass to untested, if something is wrong. OPTIONS are passed
5852 # to gdb_compile directly.
5853 proc build_executable { testname executable {sources ""} {options {debug}} } {
5854 if {[llength $sources]==0} {
5855 set sources ${executable}.c
5856 }
5857
5858 set arglist [list $testname $executable $options]
5859 foreach source $sources {
5860 lappend arglist $source $options
5861 }
5862
5863 return [eval build_executable_from_specs $arglist]
5864 }
5865
5866 # Starts fresh GDB binary and loads an optional executable into GDB.
5867 # Usage: clean_restart [executable]
5868 # EXECUTABLE is the basename of the binary.
5869
5870 proc clean_restart { args } {
5871 global srcdir
5872 global subdir
5873
5874 if { [llength $args] > 1 } {
5875 error "bad number of args: [llength $args]"
5876 }
5877
5878 gdb_exit
5879 gdb_start
5880 gdb_reinitialize_dir $srcdir/$subdir
5881
5882 if { [llength $args] >= 1 } {
5883 set executable [lindex $args 0]
5884 set binfile [standard_output_file ${executable}]
5885 gdb_load ${binfile}
5886 }
5887 }
5888
5889 # Prepares for testing by calling build_executable_full, then
5890 # clean_restart.
5891 # TESTNAME is the name of the test.
5892 # Each element in ARGS is a list of the form
5893 # { EXECUTABLE OPTIONS SOURCE_SPEC... }
5894 # These are passed to build_executable_from_specs, which see.
5895 # The last EXECUTABLE is passed to clean_restart.
5896 # Returns 0 on success, non-zero on failure.
5897 proc prepare_for_testing_full {testname args} {
5898 foreach spec $args {
5899 if {[eval build_executable_from_specs [list $testname] $spec] == -1} {
5900 return -1
5901 }
5902 set executable [lindex $spec 0]
5903 }
5904 clean_restart $executable
5905 return 0
5906 }
5907
5908 # Prepares for testing, by calling build_executable, and then clean_restart.
5909 # Please refer to build_executable for parameter description.
5910 proc prepare_for_testing { testname executable {sources ""} {options {debug}}} {
5911
5912 if {[build_executable $testname $executable $sources $options] == -1} {
5913 return -1
5914 }
5915 clean_restart $executable
5916
5917 return 0
5918 }
5919
5920 # Retrieve the value of EXP in the inferior, represented in format
5921 # specified in FMT (using "printFMT"). DEFAULT is used as fallback if
5922 # print fails. TEST is the test message to use. It can be omitted,
5923 # in which case a test message is built from EXP.
5924
5925 proc get_valueof { fmt exp default {test ""} } {
5926 global gdb_prompt
5927
5928 if {$test == "" } {
5929 set test "get valueof \"${exp}\""
5930 }
5931
5932 set val ${default}
5933 gdb_test_multiple "print${fmt} ${exp}" "$test" {
5934 -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" {
5935 set val $expect_out(1,string)
5936 pass "$test"
5937 }
5938 timeout {
5939 fail "$test (timeout)"
5940 }
5941 }
5942 return ${val}
5943 }
5944
5945 # Retrieve the value of EXP in the inferior, as a signed decimal value
5946 # (using "print /d"). DEFAULT is used as fallback if print fails.
5947 # TEST is the test message to use. It can be omitted, in which case
5948 # a test message is built from EXP.
5949
5950 proc get_integer_valueof { exp default {test ""} } {
5951 global gdb_prompt
5952
5953 if {$test == ""} {
5954 set test "get integer valueof \"${exp}\""
5955 }
5956
5957 set val ${default}
5958 gdb_test_multiple "print /d ${exp}" "$test" {
5959 -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
5960 set val $expect_out(1,string)
5961 pass "$test"
5962 }
5963 timeout {
5964 fail "$test (timeout)"
5965 }
5966 }
5967 return ${val}
5968 }
5969
5970 # Retrieve the value of EXP in the inferior, as an hexadecimal value
5971 # (using "print /x"). DEFAULT is used as fallback if print fails.
5972 # TEST is the test message to use. It can be omitted, in which case
5973 # a test message is built from EXP.
5974
5975 proc get_hexadecimal_valueof { exp default {test ""} } {
5976 global gdb_prompt
5977
5978 if {$test == ""} {
5979 set test "get hexadecimal valueof \"${exp}\""
5980 }
5981
5982 set val ${default}
5983 gdb_test_multiple "print /x ${exp}" $test {
5984 -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" {
5985 set val $expect_out(1,string)
5986 pass "$test"
5987 }
5988 }
5989 return ${val}
5990 }
5991
5992 # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT
5993 # is used as fallback if print fails. TEST is the test message to use.
5994 # It can be omitted, in which case a test message is 'sizeof (TYPE)'.
5995
5996 proc get_sizeof { type default {test ""} } {
5997 return [get_integer_valueof "sizeof (${type})" $default $test]
5998 }
5999
6000 proc get_target_charset { } {
6001 global gdb_prompt
6002
6003 gdb_test_multiple "show target-charset" "" {
6004 -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
6005 return $expect_out(1,string)
6006 }
6007 -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
6008 return $expect_out(1,string)
6009 }
6010 }
6011
6012 # Pick a reasonable default.
6013 warning "Unable to read target-charset."
6014 return "UTF-8"
6015 }
6016
6017 # Get the address of VAR.
6018
6019 proc get_var_address { var } {
6020 global gdb_prompt hex
6021
6022 # Match output like:
6023 # $1 = (int *) 0x0
6024 # $5 = (int (*)()) 0
6025 # $6 = (int (*)()) 0x24 <function_bar>
6026
6027 gdb_test_multiple "print &${var}" "get address of ${var}" {
6028 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
6029 {
6030 pass "get address of ${var}"
6031 if { $expect_out(1,string) == "0" } {
6032 return "0x0"
6033 } else {
6034 return $expect_out(1,string)
6035 }
6036 }
6037 }
6038 return ""
6039 }
6040
6041 # Return the frame number for the currently selected frame
6042 proc get_current_frame_number {{test_name ""}} {
6043 global gdb_prompt
6044
6045 if { $test_name == "" } {
6046 set test_name "get current frame number"
6047 }
6048 set frame_num -1
6049 gdb_test_multiple "frame" $test_name {
6050 -re "#(\[0-9\]+) .*$gdb_prompt $" {
6051 set frame_num $expect_out(1,string)
6052 }
6053 }
6054 return $frame_num
6055 }
6056
6057 # Get the current value for remotetimeout and return it.
6058 proc get_remotetimeout { } {
6059 global gdb_prompt
6060 global decimal
6061
6062 gdb_test_multiple "show remotetimeout" "" {
6063 -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" {
6064 return $expect_out(1,string)
6065 }
6066 }
6067
6068 # Pick the default that gdb uses
6069 warning "Unable to read remotetimeout"
6070 return 300
6071 }
6072
6073 # Set the remotetimeout to the specified timeout. Nothing is returned.
6074 proc set_remotetimeout { timeout } {
6075 global gdb_prompt
6076
6077 gdb_test_multiple "set remotetimeout $timeout" "" {
6078 -re "$gdb_prompt $" {
6079 verbose "Set remotetimeout to $timeout\n"
6080 }
6081 }
6082 }
6083
6084 # Get the target's current endianness and return it.
6085 proc get_endianness { } {
6086 global gdb_prompt
6087
6088 gdb_test_multiple "show endian" "determine endianness" {
6089 -re ".* (little|big) endian.*\r\n$gdb_prompt $" {
6090 # Pass silently.
6091 return $expect_out(1,string)
6092 }
6093 }
6094 return "little"
6095 }
6096
6097 # ROOT and FULL are file names. Returns the relative path from ROOT
6098 # to FULL. Note that FULL must be in a subdirectory of ROOT.
6099 # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this
6100 # will return "ls".
6101
6102 proc relative_filename {root full} {
6103 set root_split [file split $root]
6104 set full_split [file split $full]
6105
6106 set len [llength $root_split]
6107
6108 if {[eval file join $root_split]
6109 != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} {
6110 error "$full not a subdir of $root"
6111 }
6112
6113 return [eval file join [lrange $full_split $len end]]
6114 }
6115
6116 # If GDB_PARALLEL exists, then set up the parallel-mode directories.
6117 if {[info exists GDB_PARALLEL]} {
6118 if {[is_remote host]} {
6119 unset GDB_PARALLEL
6120 } else {
6121 file mkdir \
6122 [make_gdb_parallel_path outputs] \
6123 [make_gdb_parallel_path temp] \
6124 [make_gdb_parallel_path cache]
6125 }
6126 }
6127
6128 proc core_find {binfile {deletefiles {}} {arg ""}} {
6129 global objdir subdir
6130
6131 set destcore "$binfile.core"
6132 file delete $destcore
6133
6134 # Create a core file named "$destcore" rather than just "core", to
6135 # avoid problems with sys admin types that like to regularly prune all
6136 # files named "core" from the system.
6137 #
6138 # Arbitrarily try setting the core size limit to "unlimited" since
6139 # this does not hurt on systems where the command does not work and
6140 # allows us to generate a core on systems where it does.
6141 #
6142 # Some systems append "core" to the name of the program; others append
6143 # the name of the program to "core"; still others (like Linux, as of
6144 # May 2003) create cores named "core.PID". In the latter case, we
6145 # could have many core files lying around, and it may be difficult to
6146 # tell which one is ours, so let's run the program in a subdirectory.
6147 set found 0
6148 set coredir [standard_output_file coredir.[getpid]]
6149 file mkdir $coredir
6150 catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\""
6151 # remote_exec host "${binfile}"
6152 foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" {
6153 if [remote_file build exists $i] {
6154 remote_exec build "mv $i $destcore"
6155 set found 1
6156 }
6157 }
6158 # Check for "core.PID".
6159 if { $found == 0 } {
6160 set names [glob -nocomplain -directory $coredir core.*]
6161 if {[llength $names] == 1} {
6162 set corefile [file join $coredir [lindex $names 0]]
6163 remote_exec build "mv $corefile $destcore"
6164 set found 1
6165 }
6166 }
6167 if { $found == 0 } {
6168 # The braindamaged HPUX shell quits after the ulimit -c above
6169 # without executing ${binfile}. So we try again without the
6170 # ulimit here if we didn't find a core file above.
6171 # Oh, I should mention that any "braindamaged" non-Unix system has
6172 # the same problem. I like the cd bit too, it's really neat'n stuff.
6173 catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\""
6174 foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" {
6175 if [remote_file build exists $i] {
6176 remote_exec build "mv $i $destcore"
6177 set found 1
6178 }
6179 }
6180 }
6181
6182 # Try to clean up after ourselves.
6183 foreach deletefile $deletefiles {
6184 remote_file build delete [file join $coredir $deletefile]
6185 }
6186 remote_exec build "rmdir $coredir"
6187
6188 if { $found == 0 } {
6189 warning "can't generate a core file - core tests suppressed - check ulimit -c"
6190 return ""
6191 }
6192 return $destcore
6193 }
6194
6195 # gdb_target_symbol_prefix compiles a test program and then examines
6196 # the output from objdump to determine the prefix (such as underscore)
6197 # for linker symbol prefixes.
6198
6199 gdb_caching_proc gdb_target_symbol_prefix {
6200 # Compile a simple test program...
6201 set src { int main() { return 0; } }
6202 if {![gdb_simple_compile target_symbol_prefix $src executable]} {
6203 return 0
6204 }
6205
6206 set prefix ""
6207
6208 set objdump_program [gdb_find_objdump]
6209 set result [catch "exec $objdump_program --syms $obj" output]
6210
6211 if { $result == 0 \
6212 && ![regexp -lineanchor \
6213 { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } {
6214 verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2
6215 }
6216
6217 file delete $obj
6218
6219 return $prefix
6220 }
6221
6222 # Return 1 if target supports scheduler locking, otherwise return 0.
6223
6224 gdb_caching_proc target_supports_scheduler_locking {
6225 global gdb_prompt
6226
6227 set me "gdb_target_supports_scheduler_locking"
6228
6229 set src { int main() { return 0; } }
6230 if {![gdb_simple_compile $me $src executable]} {
6231 return 0
6232 }
6233
6234 clean_restart $obj
6235 if ![runto_main] {
6236 return 0
6237 }
6238
6239 set supports_schedule_locking -1
6240 set current_schedule_locking_mode ""
6241
6242 set test "reading current scheduler-locking mode"
6243 gdb_test_multiple "show scheduler-locking" $test {
6244 -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" {
6245 set current_schedule_locking_mode $expect_out(1,string)
6246 }
6247 -re "$gdb_prompt $" {
6248 set supports_schedule_locking 0
6249 }
6250 timeout {
6251 set supports_schedule_locking 0
6252 }
6253 }
6254
6255 if { $supports_schedule_locking == -1 } {
6256 set test "checking for scheduler-locking support"
6257 gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test {
6258 -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" {
6259 set supports_schedule_locking 0
6260 }
6261 -re "$gdb_prompt $" {
6262 set supports_schedule_locking 1
6263 }
6264 timeout {
6265 set supports_schedule_locking 0
6266 }
6267 }
6268 }
6269
6270 if { $supports_schedule_locking == -1 } {
6271 set supports_schedule_locking 0
6272 }
6273
6274 gdb_exit
6275 remote_file build delete $obj
6276 verbose "$me: returning $supports_schedule_locking" 2
6277 return $supports_schedule_locking
6278 }
6279
6280 # gdb_target_symbol returns the provided symbol with the correct prefix
6281 # prepended. (See gdb_target_symbol_prefix, above.)
6282
6283 proc gdb_target_symbol { symbol } {
6284 set prefix [gdb_target_symbol_prefix]
6285 return "${prefix}${symbol}"
6286 }
6287
6288 # gdb_target_symbol_prefix_flags_asm returns a string that can be
6289 # added to gdb_compile options to define the C-preprocessor macro
6290 # SYMBOL_PREFIX with a value that can be prepended to symbols
6291 # for targets which require a prefix, such as underscore.
6292 #
6293 # This version (_asm) defines the prefix without double quotes
6294 # surrounding the prefix. It is used to define the macro
6295 # SYMBOL_PREFIX for assembly language files. Another version, below,
6296 # is used for symbols in inline assembler in C/C++ files.
6297 #
6298 # The lack of quotes in this version (_asm) makes it possible to
6299 # define supporting macros in the .S file. (The version which
6300 # uses quotes for the prefix won't work for such files since it's
6301 # impossible to define a quote-stripping macro in C.)
6302 #
6303 # It's possible to use this version (_asm) for C/C++ source files too,
6304 # but a string is usually required in such files; providing a version
6305 # (no _asm) which encloses the prefix with double quotes makes it
6306 # somewhat easier to define the supporting macros in the test case.
6307
6308 proc gdb_target_symbol_prefix_flags_asm {} {
6309 set prefix [gdb_target_symbol_prefix]
6310 if {$prefix ne ""} {
6311 return "additional_flags=-DSYMBOL_PREFIX=$prefix"
6312 } else {
6313 return "";
6314 }
6315 }
6316
6317 # gdb_target_symbol_prefix_flags returns the same string as
6318 # gdb_target_symbol_prefix_flags_asm, above, but with the prefix
6319 # enclosed in double quotes if there is a prefix.
6320 #
6321 # See the comment for gdb_target_symbol_prefix_flags_asm for an
6322 # extended discussion.
6323
6324 proc gdb_target_symbol_prefix_flags {} {
6325 set prefix [gdb_target_symbol_prefix]
6326 if {$prefix ne ""} {
6327 return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
6328 } else {
6329 return "";
6330 }
6331 }
6332
6333 # A wrapper for 'remote_exec host' that passes or fails a test.
6334 # Returns 0 if all went well, nonzero on failure.
6335 # TEST is the name of the test, other arguments are as for remote_exec.
6336
6337 proc run_on_host { test program args } {
6338 verbose -log "run_on_host: $program $args"
6339 # remote_exec doesn't work properly if the output is set but the
6340 # input is the empty string -- so replace an empty input with
6341 # /dev/null.
6342 if {[llength $args] > 1 && [lindex $args 1] == ""} {
6343 set args [lreplace $args 1 1 "/dev/null"]
6344 }
6345 set result [eval remote_exec host [list $program] $args]
6346 verbose "result is $result"
6347 set status [lindex $result 0]
6348 set output [lindex $result 1]
6349 if {$status == 0} {
6350 pass $test
6351 return 0
6352 } else {
6353 verbose -log "run_on_host failed: $output"
6354 fail $test
6355 return -1
6356 }
6357 }
6358
6359 # Return non-zero if "board_info debug_flags" mentions Fission.
6360 # http://gcc.gnu.org/wiki/DebugFission
6361 # Fission doesn't support everything yet.
6362 # This supports working around bug 15954.
6363
6364 proc using_fission { } {
6365 set debug_flags [board_info [target_info name] debug_flags]
6366 return [regexp -- "-gsplit-dwarf" $debug_flags]
6367 }
6368
6369 # Search the caller's ARGS list and set variables according to the list of
6370 # valid options described by ARGSET.
6371 #
6372 # The first member of each one- or two-element list in ARGSET defines the
6373 # name of a variable that will be added to the caller's scope.
6374 #
6375 # If only one element is given to describe an option, it the value is
6376 # 0 if the option is not present in (the caller's) ARGS or 1 if
6377 # it is.
6378 #
6379 # If two elements are given, the second element is the default value of
6380 # the variable. This is then overwritten if the option exists in ARGS.
6381 #
6382 # Any parse_args elements in (the caller's) ARGS will be removed, leaving
6383 # any optional components.
6384
6385 # Example:
6386 # proc myproc {foo args} {
6387 # parse_args {{bar} {baz "abc"} {qux}}
6388 # # ...
6389 # }
6390 # myproc ABC -bar -baz DEF peanut butter
6391 # will define the following variables in myproc:
6392 # foo (=ABC), bar (=1), baz (=DEF), and qux (=0)
6393 # args will be the list {peanut butter}
6394
6395 proc parse_args { argset } {
6396 upvar args args
6397
6398 foreach argument $argset {
6399 if {[llength $argument] == 1} {
6400 # No default specified, so we assume that we should set
6401 # the value to 1 if the arg is present and 0 if it's not.
6402 # It is assumed that no value is given with the argument.
6403 set result [lsearch -exact $args "-$argument"]
6404 if {$result != -1} then {
6405 uplevel 1 [list set $argument 1]
6406 set args [lreplace $args $result $result]
6407 } else {
6408 uplevel 1 [list set $argument 0]
6409 }
6410 } elseif {[llength $argument] == 2} {
6411 # There are two items in the argument. The second is a
6412 # default value to use if the item is not present.
6413 # Otherwise, the variable is set to whatever is provided
6414 # after the item in the args.
6415 set arg [lindex $argument 0]
6416 set result [lsearch -exact $args "-[lindex $arg 0]"]
6417 if {$result != -1} then {
6418 uplevel 1 [list set $arg [lindex $args [expr $result+1]]]
6419 set args [lreplace $args $result [expr $result+1]]
6420 } else {
6421 uplevel 1 [list set $arg [lindex $argument 1]]
6422 }
6423 } else {
6424 error "Badly formatted argument \"$argument\" in argument set"
6425 }
6426 }
6427
6428 # The remaining args should be checked to see that they match the
6429 # number of items expected to be passed into the procedure...
6430 }
6431
6432 # Capture the output of COMMAND in a string ignoring PREFIX (a regexp);
6433 # return that string.
6434
6435 proc capture_command_output { command prefix } {
6436 global gdb_prompt
6437 global expect_out
6438
6439 set output_string ""
6440 gdb_test_multiple "$command" "capture_command_output for $command" {
6441 -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
6442 set output_string $expect_out(1,string)
6443 }
6444 }
6445 return $output_string
6446 }
6447
6448 # A convenience function that joins all the arguments together, with a
6449 # regexp that matches exactly one end of line in between each argument.
6450 # This function is ideal to write the expected output of a GDB command
6451 # that generates more than a couple of lines, as this allows us to write
6452 # each line as a separate string, which is easier to read by a human
6453 # being.
6454
6455 proc multi_line { args } {
6456 return [join $args "\r\n"]
6457 }
6458
6459 # Similar to the above, but while multi_line is meant to be used to
6460 # match GDB output, this one is meant to be used to build strings to
6461 # send as GDB input.
6462
6463 proc multi_line_input { args } {
6464 return [join $args "\n"]
6465 }
6466
6467 # Return the version of the DejaGnu framework.
6468 #
6469 # The return value is a list containing the major, minor and patch version
6470 # numbers. If the version does not contain a minor or patch number, they will
6471 # be set to 0. For example:
6472 #
6473 # 1.6 -> {1 6 0}
6474 # 1.6.1 -> {1 6 1}
6475 # 2 -> {2 0 0}
6476
6477 proc dejagnu_version { } {
6478 # The frame_version variable is defined by DejaGnu, in runtest.exp.
6479 global frame_version
6480
6481 verbose -log "DejaGnu version: $frame_version"
6482 verbose -log "Expect version: [exp_version]"
6483 verbose -log "Tcl version: [info tclversion]"
6484
6485 set dg_ver [split $frame_version .]
6486
6487 while { [llength $dg_ver] < 3 } {
6488 lappend dg_ver 0
6489 }
6490
6491 return $dg_ver
6492 }
6493
6494 # Define user-defined command COMMAND using the COMMAND_LIST as the
6495 # command's definition. The terminating "end" is added automatically.
6496
6497 proc gdb_define_cmd {command command_list} {
6498 global gdb_prompt
6499
6500 set input [multi_line_input {*}$command_list "end"]
6501 set test "define $command"
6502
6503 gdb_test_multiple "define $command" $test {
6504 -re "End with" {
6505 gdb_test_multiple $input $test {
6506 -re "\r\n$gdb_prompt " {
6507 }
6508 }
6509 }
6510 }
6511 }
6512
6513 # Override the 'cd' builtin with a version that ensures that the
6514 # log file keeps pointing at the same file. We need this because
6515 # unfortunately the path to the log file is recorded using an
6516 # relative path name, and, we sometimes need to close/reopen the log
6517 # after changing the current directory. See get_compiler_info.
6518
6519 rename cd builtin_cd
6520
6521 proc cd { dir } {
6522
6523 # Get the existing log file flags.
6524 set log_file_info [log_file -info]
6525
6526 # Split the flags into args and file name.
6527 set log_file_flags ""
6528 set log_file_file ""
6529 foreach arg [ split "$log_file_info" " "] {
6530 if [string match "-*" $arg] {
6531 lappend log_file_flags $arg
6532 } else {
6533 lappend log_file_file $arg
6534 }
6535 }
6536
6537 # If there was an existing file, ensure it is an absolute path, and then
6538 # reset logging.
6539 if { $log_file_file != "" } {
6540 set log_file_file [file normalize $log_file_file]
6541 log_file
6542 log_file $log_file_flags "$log_file_file"
6543 }
6544
6545 # Call the builtin version of cd.
6546 builtin_cd $dir
6547 }
6548
6549 # Return a list of all languages supported by GDB, suitable for use in
6550 # 'set language NAME'. This doesn't include either the 'local' or
6551 # 'auto' keywords.
6552 proc gdb_supported_languages {} {
6553 return [list c objective-c c++ d go fortran modula-2 asm pascal \
6554 opencl rust minimal ada]
6555 }
6556
6557 # Check if debugging is enabled for gdb.
6558
6559 proc gdb_debug_enabled { } {
6560 global gdbdebug
6561
6562 # If not already read, get the debug setting from environment or board setting.
6563 if {![info exists gdbdebug]} {
6564 global env
6565 if [info exists env(GDB_DEBUG)] {
6566 set gdbdebug $env(GDB_DEBUG)
6567 } elseif [target_info exists gdb,debug] {
6568 set gdbdebug [target_info gdb,debug]
6569 } else {
6570 return 0
6571 }
6572 }
6573
6574 # Ensure it not empty.
6575 return [expr { $gdbdebug != "" }]
6576 }
6577
6578 # Turn on debugging if enabled, or reset if already on.
6579
6580 proc gdb_debug_init { } {
6581
6582 global gdb_prompt
6583
6584 if ![gdb_debug_enabled] {
6585 return;
6586 }
6587
6588 # First ensure logging is off.
6589 send_gdb "set logging off\n"
6590
6591 set debugfile [standard_output_file gdb.debug]
6592 send_gdb "set logging file $debugfile\n"
6593
6594 send_gdb "set logging debugredirect\n"
6595
6596 global gdbdebug
6597 foreach entry [split $gdbdebug ,] {
6598 send_gdb "set debug $entry 1\n"
6599 }
6600
6601 # Now that everything is set, enable logging.
6602 send_gdb "set logging on\n"
6603 gdb_expect 10 {
6604 -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {}
6605 timeout { warning "Couldn't set logging file" }
6606 }
6607 }
6608
6609 # Check if debugging is enabled for gdbserver.
6610
6611 proc gdbserver_debug_enabled { } {
6612 # Always disabled for GDB only setups.
6613 return 0
6614 }
6615
6616 # Open the file for logging gdb input
6617
6618 proc gdb_stdin_log_init { } {
6619 global in_file
6620
6621 if {[info exists in_file]} {
6622 # Close existing file.
6623 catch "close $in_file"
6624 }
6625
6626 set logfile [standard_output_file_with_gdb_instance gdb.in]
6627 set in_file [open $logfile w]
6628 }
6629
6630 # Write to the file for logging gdb input.
6631 # TYPE can be one of the following:
6632 # "standard" : Default. Standard message written to the log
6633 # "answer" : Answer to a question (eg "Y"). Not written the log.
6634 # "optional" : Optional message. Not written to the log.
6635
6636 proc gdb_stdin_log_write { message {type standard} } {
6637
6638 global in_file
6639 if {![info exists in_file]} {
6640 return
6641 }
6642
6643 # Check message types.
6644 switch -regexp -- $type {
6645 "answer" {
6646 return
6647 }
6648 "optional" {
6649 return
6650 }
6651 }
6652
6653 #Write to the log
6654 puts -nonewline $in_file "$message"
6655 }
6656
6657 # Write the command line used to invocate gdb to the cmd file.
6658
6659 proc gdb_write_cmd_file { cmdline } {
6660 set logfile [standard_output_file_with_gdb_instance gdb.cmd]
6661 set cmd_file [open $logfile w]
6662 puts $cmd_file $cmdline
6663 catch "close $cmd_file"
6664 }
6665
6666 # Always load compatibility stuff.
6667 load_lib future.exp
This page took 0.201198 seconds and 5 git commands to generate.