1 # Copyright 1988-2016 Free Software Foundation, Inc.
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.
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.
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/>.
17 # test special commands (if, while, etc)
22 if { [prepare_for_testing commands.exp commands run.c {debug additional_flags=-DFAKEARGV}] } {
26 # Run to FUNCTION. If that fails, issue a FAIL and make the caller
29 proc runto_or_return {function} {
30 if { ![runto factorial] } {
31 fail "cannot run to $function"
36 proc_with_prefix gdbvar_simple_if_test {} {
40 gdb_test_no_output "set \$foo = 0" "set foo"
41 # All this test should do is print 0xdeadbeef once.
49 "$valnum_re = 0xdeadbeef" \
52 # All this test should do is print 0xfeedface once.
60 "$valnum_re = 0xfeedface" \
64 proc_with_prefix gdbvar_simple_while_test {} {
68 gdb_test_no_output "set \$foo = 5" "set foo"
69 # This test should print 0xfeedface five times.
77 "$valnum_re = 0xfeedface" \
78 "$valnum_re = 0xfeedface" \
79 "$valnum_re = 0xfeedface" \
80 "$valnum_re = 0xfeedface" \
81 "$valnum_re = 0xfeedface"] \
85 proc_with_prefix gdbvar_complex_if_while_test {} {
89 gdb_test_no_output "set \$foo = 4" "set foo"
90 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
95 { if ($foo % 2) == 1} \
102 "$valnum_re = 0xdeadbeef" \
103 "$valnum_re = 0xfeedface" \
104 "$valnum_re = 0xdeadbeef" \
105 "$valnum_re = 0xfeedface"] \
109 proc_with_prefix progvar_simple_if_test {} {
113 runto_or_return factorial
115 # Don't depend upon argument passing, since most simulators don't
116 # currently support it. Bash value variable to be what we want.
117 gdb_test "p value=5" " = 5" "set value to 5"
118 # All this test should do is print 0xdeadbeef once.
126 "$valnum_re = 0xdeadbeef" \
129 # All this test should do is print 0xfeedface once.
137 "$valnum_re = 0xfeedface" \
141 proc_with_prefix progvar_simple_while_test {} {
145 runto_or_return factorial
147 # Don't depend upon argument passing, since most simulators don't
148 # currently support it. Bash value variable to be what we want.
149 gdb_test "p value=5" " = 5" "set value to 5"
150 # This test should print 0xfeedface five times.
158 "$valnum_re = 0xfeedface" \
159 "$valnum_re = 0xfeedface" \
160 "$valnum_re = 0xfeedface" \
161 "$valnum_re = 0xfeedface" \
162 "$valnum_re = 0xfeedface"] \
166 proc_with_prefix progvar_complex_if_while_test {} {
170 runto_or_return factorial
172 # Don't depend upon argument passing, since most simulators don't
173 # currently support it. Bash value variable to be what we want.
174 gdb_test "p value=4" " = 4" "set value to 4"
175 # This test should alternate between 0xdeadbeef and 0xfeedface two
181 { if (value % 2) == 1} \
188 "$valnum_re = 0xdeadbeef" \
189 "$valnum_re = 0xfeedface" \
190 "$valnum_re = 0xdeadbeef" \
191 "$valnum_re = 0xfeedface"] \
195 proc_with_prefix if_while_breakpoint_command_test {} {
198 runto_or_return factorial
200 # Don't depend upon argument passing, since most simulators don't
201 # currently support it. Bash value variable to be what we want.
202 gdb_test "p value=5" " = 5" "set value to 5"
204 gdb_test "break factorial" "Breakpoint.*at.*" "break factorial"
206 gdb_test_multiple "commands" "commands" {
212 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
217 { if (value % 2) == 1} \
229 "$valnum_re = 0xdeadbeef" \
230 "$valnum_re = 0xfeedface" \
231 "$valnum_re = 0xdeadbeef" \
232 "$valnum_re = 0xfeedface"] \
234 gdb_test "info break" "while.*set.*if.*p/x.*else.*p/x.*end.*"
237 # Test that we can run the inferior from breakpoint commands.
239 # The expected behavior is that all commands after the first "step"
240 # shall be ignored. See the gdb manual, "Break Commands",
241 # subsection "Breakpoint command lists".
243 proc_with_prefix infrun_breakpoint_command_test {} {
244 runto_or_return factorial
246 # Don't depend upon argument passing, since most simulators don't
247 # currently support it. Bash value variable to be what we want.
248 gdb_test "p value=6" " = 6" "set value to 6"
250 gdb_test "break factorial if value == 5" "Breakpoint.*at.*"
252 # infrun_breakpoint_command_test - This test was broken into two parts
253 # to get around a synchronization problem in expect.
254 # part1: issue the gdb command "commands"
255 # part2: send the list of commands
257 set test "commands #1"
258 gdb_test_multiple "commands" $test {
263 gdb_test "step\nstep\nstep\nstep\nend" "" \
266 gdb_test "continue" \
267 "Continuing.*.*.*Breakpoint \[0-9\]*, factorial \\(value=5\\).*at.*\[0-9\]*\[ \]*if \\(value > 1\\) \{.*\[0-9\]*\[ \]*value \\*= factorial \\(value - 1\\);.*"
270 proc_with_prefix breakpoint_command_test {} {
271 runto_or_return factorial
273 # Don't depend upon argument passing, since most simulators don't
274 # currently support it. Bash value variable to be what we want.
275 gdb_test "p value=6" " = 6" "set value to 6"
277 gdb_test "break factorial" "Breakpoint.*at.*"
281 { printf "Now the value is %d\n", value} \
285 gdb_test "continue" \
286 "Breakpoint \[0-9\]*, factorial.*Now the value is 5"
287 gdb_test "print value" " = 5"
290 # Test a simple user defined command (with arguments)
291 proc_with_prefix user_defined_command_test {} {
295 gdb_test_no_output "set \$foo = 4" "set foo"
297 gdb_test_multiple "define mycommand" "define mycommand" {
299 pass "define mycommand"
303 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
308 { if ($arg0 % 2) == 1} \
319 set valnum_re "\\\$$decimal"
324 "$valnum_re = 0xdeadbeef" \
325 "$valnum_re = 0xfeedface" \
326 "$valnum_re = 0xdeadbeef" \
327 "$valnum_re = 0xfeedface"] \
328 "execute user-defined command"
329 gdb_test "show user mycommand" \
330 " while \\\$arg0.*set.* if \\\(\\\$arg0.*p/x.* else\[^\n\].*p/x.* end\[^\n\].* end\[^\n\].*" \
331 "display user command"
333 # Create and test a user-defined command with an empty body.
334 gdb_test_multiple "define myemptycommand" "define myemptycommand" {
336 pass "define myemptycommand"
341 "end definition of user-defined command with empty body"
343 gdb_test_no_output "myemptycommand" \
344 "execute user-defined empty command"
346 gdb_test "show user" \
347 "User command \"myemptycommand.*" \
348 "display empty command in command list"
350 gdb_test "show user myemptycommand" \
351 "User command \"myemptycommand.*" \
352 "display user-defined empty command"
355 # Test that "eval" in a user-defined command expands $argc/$argN.
357 proc_with_prefix user_defined_command_args_eval {} {
360 gdb_test_multiple "define command_args_eval" \
361 "define command_args_eval" {
367 # Make a command that constructs references to $argc and $argN via
371 {eval "printf \"argc = %%d,\", $arg%c", 'c'} \
374 { eval "printf \" %%d\", $arg%d", $i} \
382 gdb_test "command_args_eval 1 2 3" "argc = 3, 1 2 3" "execute command"
385 # Test that the $argc/$argN variables are pushed on/popped from the
386 # args stack correctly when a user-defined command calls another
387 # user-defined command (or in this case, recurses).
389 proc_with_prefix user_defined_command_args_stack_test {} {
392 gdb_test_multiple "define args_stack_command" \
393 "define args_stack_command" {
399 # Make a command that refers to $argc/$argN before and after
400 # recursing. Also, vary the number of arguments passed to each
404 {printf "before, argc = %d,", $argc} \
407 { eval "printf \" %%d\", $arg%d", $i} \
414 { args_stack_command 21 22} \
417 { args_stack_command 11} \
421 {printf "after, argc = %d,", $argc} \
424 { eval "printf \" %%d\", $arg%d", $i} \
434 "before, argc = 3, 31 32 33" \
435 "before, argc = 2, 21 22" \
436 "before, argc = 1, 11" \
437 "after, argc = 1, 11" \
438 "after, argc = 2, 21 22" \
439 "after, argc = 3, 31 32 33"]
440 gdb_test "args_stack_command 31 32 33" $expected "execute command"
443 proc_with_prefix watchpoint_command_test {} {
446 # Disable hardware watchpoints if necessary.
447 if [target_info exists gdb,no_hardware_watchpoints] {
448 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
451 runto_or_return factorial
455 # Verify that we can create a watchpoint, and give it a commands
456 # list that continues the inferior. We set the watchpoint on a
457 # local variable, too, so that it self-deletes when the watched
458 # data goes out of scope.
460 # What should happen is: Each time the watchpoint triggers, it
461 # continues the inferior. Eventually, the watchpoint will self-
462 # delete, when the watched variable is out of scope. But by that
463 # time, the inferior should have exited. GDB shouldn't crash or
464 # anything untoward as a result of this.
468 gdb_test_multiple "watch local_var" "watch local_var" {
469 -re "\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
470 set wp_id $expect_out(1,string)
471 pass "watch local_var"
475 if {$wp_id == -1} {return}
477 gdb_test_multiple "commands $wp_id" "begin commands on watch" {
478 -re "Type commands for breakpoint.*, one per line.*>$" {
479 pass "begin commands on watch"
482 # See the 'No symbol "value...' fail below. This command will
483 # fail if it's executed in the wrong frame. If adjusting the
484 # test, make sure this property holds.
485 gdb_test_multiple "print value" "add print command to watch" {
487 pass "add print command to watch"
490 gdb_test_multiple "continue" "add continue command to watch" {
492 pass "add continue command to watch"
497 "end commands on watch"
499 set test "continue with watch"
500 set lno_1 [gdb_get_line_number "commands.exp: hw local_var out of scope" "run.c"]
501 set lno_2 [gdb_get_line_number "commands.exp: local_var out of scope" "run.c"]
502 gdb_test_multiple "continue" "$test" {
503 -re "No symbol \"value\" in current context.\r\n$gdb_prompt $" {
504 # Happens if GDB actually runs the watchpoints commands,
505 # even though the watchpoint was deleted for not being in
509 -re "Continuing.*\[Ww\]atchpoint $wp_id deleted because the program has left the block in.*which its expression is valid.*run.c:($lno_1|$lno_2).*$gdb_prompt $" {
515 proc_with_prefix test_command_prompt_position {} {
519 runto_or_return factorial
521 # Don't depend upon argument passing, since most simulators don't
522 # currently support it. Bash value variable to be what we want.
524 gdb_test "break factorial" "Breakpoint.*at.*"
525 gdb_test "p value=5" ".*" "set value to 5"
526 # All this test should do is print 0xdeadbeef once.
534 "$valnum_re = 0xdeadbeef" \
537 # Now let's test for the correct position of the '>' in gdb's
538 # prompt for commands. It should be at the beginning of the line,
539 # and not after one space.
542 gdb_test_multiple "commands" $test {
543 -re "Type commands.*End with.*\[\r\n\]>$" {
544 gdb_test_multiple "printf \"Now the value is %d\\n\", value" $test {
545 -re "^printf.*value\r\n>$" {
546 gdb_test_multiple "end" $test {
547 -re "^end\r\n$gdb_prompt $" {
559 proc_with_prefix deprecated_command_test {} {
560 gdb_test "maintenance deprecate blah" "Can't find command.*" \
561 "tried to deprecate non-existing command"
563 gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /1/"
565 "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
566 "p deprecated warning, with replacement"
567 gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /1/"
569 gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /2/"
570 gdb_test_no_output "maintenance deprecate print \"new_print\""
572 "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
573 "both alias and command are deprecated"
574 gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /2/"
576 gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
577 "deprecate long command /1/"
578 gdb_test "set remote memory-read-packet-size" \
579 "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
580 "long command deprecated /1/"
582 gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size" \
583 "deprecate long command /2/"
584 gdb_test "set remote memory-read-packet-size" \
585 "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
586 "long command deprecated with no alternative /2/"
588 gdb_test "maintenance deprecate" \
589 "\"maintenance deprecate\".*" \
590 "deprecate with no arguments"
593 proc_with_prefix bp_deleted_in_command_test {} {
598 # Create a breakpoint, and associate a command-list to it, with
599 # one command that deletes this breakpoint.
600 gdb_test "break factorial" \
601 "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\."
603 gdb_test_multiple "commands" "begin commands" {
604 -re "Type commands for breakpoint.*>$" {
605 pass "begin commands"
608 gdb_test_multiple "silent" "add silent command" {
610 pass "add silent command"
613 gdb_test_multiple "clear factorial" "add clear command" {
615 pass "add clear command"
618 gdb_test_multiple "printf \"factorial command-list executed\\n\"" \
619 "add printf command" {
621 pass "add printf command"
624 gdb_test_multiple "cont" "add cont command" {
626 pass "add cont command"
634 gdb_test "" "factorial command-list executed.*" "run factorial until breakpoint"
637 proc_with_prefix temporary_breakpoint_commands {} {
642 # Create a temporary breakpoint, and associate a commands list to it.
643 # This test will verify that this commands list is executed when the
645 gdb_test "tbreak factorial" \
646 "Temporary breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\." \
649 gdb_test_multiple "commands" \
650 "begin commands in bp_deleted_in_command_test" {
651 -re "Type commands for breakpoint.*>$" {
652 pass "begin commands"
655 gdb_test_multiple "silent" "add silent tbreak command" {
657 pass "add silent tbreak command"
660 gdb_test_multiple "printf \"factorial tbreak commands executed\\n\"" \
661 "add printf tbreak command" {
663 pass "add printf tbreak command"
666 gdb_test_multiple "cont" "add cont tbreak command" {
668 pass "add cont tbreak command"
673 "end tbreak commands"
676 gdb_test "" "factorial tbreak commands executed.*" \
677 "run factorial until temporary breakpoint"
680 # Test that GDB can handle $arg0 outside of user functions without
682 proc_with_prefix stray_arg0_test { } {
685 gdb_test "print \$arg0" \
686 "$valnum_re = void" \
689 gdb_test "if 1 == 1\nprint \$arg0\nend" \
690 "$valnum_re = void" \
693 gdb_test "print \$arg0 = 1" \
697 gdb_test "print \$arg0" \
702 # Test that GDB is able to source a file with an indented comment.
703 proc_with_prefix source_file_with_indented_comment {} {
704 set file1 [standard_output_file file1]
706 set fd [open "$file1" w]
714 gdb_test "source $file1" "Done!" "source file"
717 # Test that GDB can handle arguments when sourcing files recursively.
718 # If the arguments are overwritten with ####### then the test has failed.
719 proc_with_prefix recursive_source_test {} {
720 set file1 [standard_output_file file1]
721 set file2 [standard_output_file file2]
722 set file3 [standard_output_file file3]
724 set fd [open "$file1" w]
730 set fd [open "$file2" w]
733 echo 1: <<<\$arg0>>>\\n
735 echo 2: <<<\$arg0>>>\\n
739 set fd [open "$file3" w]
742 #################################################################"
745 gdb_test "source $file1" \
746 "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
754 proc gdb_test_no_prompt { command result msg } {
757 set msg "$command - $msg"
758 set result "^[string_to_regexp $command]\r\n$result$"
759 gdb_test_multiple $command $msg {
772 proc_with_prefix if_commands_test {} {
775 gdb_test_no_output "set \$tem = 1" "set \$tem"
777 set test "if_commands_test 1"
778 gdb_test_no_prompt "if \$tem == 2" { >} $test
779 gdb_test_no_prompt "break main" { >} $test
780 gdb_test_no_prompt "else" { >} $test
781 gdb_test_no_prompt "break factorial" { >} $test
782 gdb_test_no_prompt "commands" { >} $test
783 gdb_test_no_prompt "silent" { >} $test
784 gdb_test_no_prompt "set \$tem = 3" { >} $test
785 gdb_test_no_prompt "continue" { >} $test
786 gdb_test_multiple "end" "first end - $test" {
788 pass "first end - $test"
791 fail "first end - $test"
794 gdb_test_multiple "end" "second end - $test" {
795 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
796 pass "second end - $test"
798 -re "Undefined command: \"silent\".*$gdb_prompt $" {
799 fail "second end - $test"
803 set test "if_commands_test 2"
804 gdb_test_no_prompt "if \$tem == 1" { >} $test
805 gdb_test_no_prompt "break main" { >} $test
806 gdb_test_no_prompt "else" { >} $test
807 gdb_test_no_prompt "break factorial" { >} $test
808 gdb_test_no_prompt "commands" { >} $test
809 gdb_test_no_prompt "silent" { >} $test
810 gdb_test_no_prompt "set \$tem = 3" { >} $test
811 gdb_test_no_prompt "continue" { >} $test
812 gdb_test_multiple "end" "first end - $test" {
814 pass "first end - $test"
817 fail "first end - $test"
820 gdb_test_multiple "end" "second end - $test" {
821 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
822 pass "second end - $test"
827 # Verify an error during "commands" commands execution will prevent any other
828 # "commands" from other breakpoints at the same location to be executed.
830 proc_with_prefix error_clears_commands_left {} {
831 set test "hook-stop 1"
832 gdb_test_multiple {define hook-stop} $test {
833 -re "End with a line saying just \"end\"\\.\r\n>$" {
837 set test "hook-stop 1a"
838 gdb_test_multiple {echo hook-stop1\n} $test {
843 gdb_test_no_output "end" "hook-stop 1b"
846 gdb_breakpoint "main"
848 set test "main commands 1"
849 gdb_test_multiple {commands $bpnum} $test {
850 -re "End with a line saying just \"end\"\\.\r\n>$" {
854 set test "main commands 1a"
855 gdb_test_multiple {echo cmd1\n} $test {
860 set test "main commands 1b"
861 gdb_test_multiple {errorcommandxy\n} $test {
866 gdb_test_no_output "end" "main commands 1c"
868 gdb_breakpoint "main"
869 set test "main commands 2"
870 gdb_test_multiple {commands $bpnum} $test {
871 -re "End with a line saying just \"end\"\\.\r\n>$" {
875 set test "main commands 2a"
876 gdb_test_multiple {echo cmd2\n} $test {
881 set test "main commands 2b"
882 gdb_test_multiple {errorcommandyz\n} $test {
887 gdb_test_no_output "end" "main commands 2c"
896 "Undefined command: \"errorcommandxy\"\\. Try \"help\"\\."] \
899 gdb_test {echo idle\n} "\r\nidle" "no cmd2"
902 proc_with_prefix redefine_hook_test {} {
920 set test "redefine one"
921 gdb_test_multiple "define one" $test {
922 -re "Redefine command .one.. .y or n. $" {
932 gdb_test "end" "" "enter commands for one redefinition"
934 gdb_test "one" "hibob" "execute one command"
937 proc_with_prefix redefine_backtrace_test {} {
940 gdb_test_multiple "define backtrace" "define backtrace" {
941 -re "Really redefine built-in command \"backtrace\"\\? \\(y or n\\) $" {
942 pass "define backtrace"
946 gdb_test_multiple "y" "expect response to define backtrace" {
947 -re "End with a line saying just \"end\"\\.\r\n>$" {
948 pass "expect response to define backtrace"
959 gdb_test "backtrace" "hibob" "execute backtrace command"
960 gdb_test "bt" "hibob" "execute bt command"
963 gdbvar_simple_if_test
964 gdbvar_simple_while_test
965 gdbvar_complex_if_while_test
966 progvar_simple_if_test
967 progvar_simple_while_test
968 progvar_complex_if_while_test
969 if_while_breakpoint_command_test
970 infrun_breakpoint_command_test
971 breakpoint_command_test
972 user_defined_command_test
973 user_defined_command_args_eval
974 user_defined_command_args_stack_test
975 watchpoint_command_test
976 test_command_prompt_position
977 deprecated_command_test
978 bp_deleted_in_command_test
979 temporary_breakpoint_commands
981 source_file_with_indented_comment
982 recursive_source_test
984 error_clears_commands_left
986 # This one should come last, as it redefines "backtrace".
987 redefine_backtrace_test