1 # Copyright 1988, 1990, 1991, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
2 # 2001, 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@prep.ai.mit.edu
26 # test special commands (if, while, etc)
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/commands
34 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-DFAKEARGV}] != "" } {
42 gdb_reinitialize_dir $srcdir/$subdir
45 proc gdbvar_simple_if_test {} {
48 gdb_test "set \$foo = 0" "" "set foo in gdbvar_simple_if_test"
49 # All this test should do is print 0xdeadbeef once.
50 gdb_test "if \$foo == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
51 "\\\$\[0-9\]* = 0xdeadbeef" "gdbvar_simple_if_test #1"
52 # All this test should do is print 0xfeedface once.
53 gdb_test "if \$foo == 0\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
54 "\\\$\[0-9\]* = 0xfeedface" "gdbvar_simple_if_test #2"
57 proc gdbvar_simple_while_test {} {
60 gdb_test "set \$foo = 5" "" "set foo in gdbvar_simple_while_test"
61 # This test should print 0xfeedface five times.
62 gdb_test "while \$foo > 0\np/x 0xfeedface\nset \$foo -= 1\nend" \
63 "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
64 "gdbvar_simple_while_test #1"
67 proc gdbvar_complex_if_while_test {} {
70 gdb_test "set \$foo = 4" "" "set foo in gdbvar complex_if_while_test"
71 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
72 gdb_test "while \$foo > 0\nset \$foo -= 1\nif \(\$foo % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
73 "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
74 "gdbvar_complex_if_while_test #1"
77 proc progvar_simple_if_test {} {
80 if [target_info exists noargs] {
81 verbose "Skipping progvar_simple_if_test because of noargs."
85 if { ![runto factorial] } then { gdb_suppress_tests; }
86 # Don't depend upon argument passing, since most simulators don't
87 # currently support it. Bash value variable to be what we want.
88 gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test #1"
89 # All this test should do is print 0xdeadbeef once.
90 gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
91 "\\\$\[0-9\]* = 0xdeadbeef" \
92 "progvar_simple_if_test #1"
93 # All this test should do is print 0xfeedface once.
94 gdb_test "if value == 5\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
95 "\\\$\[0-9\]* = 0xfeedface" \
96 "progvar_simple_if_test #2"
97 gdb_stop_suppressing_tests;
100 proc progvar_simple_while_test {} {
103 if [target_info exists noargs] {
104 verbose "Skipping progvar_simple_while_test because of noargs."
108 gdb_test "set args 5" "" "set args in progvar_simple_while_test"
109 if { ![runto factorial] } then { gdb_suppress_tests }
110 # Don't depend upon argument passing, since most simulators don't
111 # currently support it. Bash value variable to be what we want.
112 gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test #2"
113 # This test should print 0xfeedface five times.
114 gdb_test "while value > 0\np/x 0xfeedface\nset value -= 1\nend" \
115 "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
116 "progvar_simple_while_test #1"
117 gdb_stop_suppressing_tests;
120 proc progvar_complex_if_while_test {} {
123 if [target_info exists noargs] {
124 verbose "Skipping progvar_simple_if_while_test because of noargs."
128 gdb_test "set args 4" "" "set args in progvar_complex_if_while_test"
129 if { ![runto factorial] } then { gdb_suppress_tests }
130 # Don't depend upon argument passing, since most simulators don't
131 # currently support it. Bash value variable to be what we want.
132 gdb_test "p value=4" "" "set value to 4 in progvar_simple_if_test"
133 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
134 gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
135 "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
136 "progvar_complex_if_while_test #1"
137 gdb_stop_suppressing_tests;
140 proc if_while_breakpoint_command_test {} {
141 if [target_info exists noargs] {
142 verbose "Skipping if_while_breakpoint_command_test because of noargs."
146 gdb_test "set args 5" "" "set args in if_while_breakpoint_command_test"
147 if { ![runto factorial] } then { gdb_suppress_tests }
148 # Don't depend upon argument passing, since most simulators don't
149 # currently support it. Bash value variable to be what we want.
150 gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test"
152 gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #1"
154 send_gdb "commands\n"
157 pass "commands in if_while_breakpoint_command_test"
160 fail "(timeout or eof) commands in if_while_breakpoint_command_test"
163 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
164 gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
166 "commands part 2 in if_while_breakpoint_command_test"
167 gdb_test "continue" \
168 "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
169 "if_while_breakpoint_command_test #1"
170 gdb_test "info break" \
171 "while.*set.*if.*p/x.*else.*p/x.*end.*" \
172 "info break in if_while_breakpoint_command_test"
173 gdb_stop_suppressing_tests;
176 # Test that we can run the inferior from breakpoint commands.
178 # The expected behavior is that all commands after the first "step"
179 # shall be ignored. See the gdb manual, "Break Commands",
180 # subsection "Breakpoint command lists".
182 proc infrun_breakpoint_command_test {} {
183 if [target_info exists noargs] {
184 verbose "Skipping infrun_breakpoint_command_test because of noargs."
188 gdb_test "set args 6" "" "set args in infrun_breakpoint_command_test"
189 if { ![runto factorial] } then { gdb_suppress_tests }
190 # Don't depend upon argument passing, since most simulators don't
191 # currently support it. Bash value variable to be what we want.
192 gdb_test "p value=6" "" "set value to 6 in progvar_simple_if_test #1"
194 gdb_test "break factorial if value == 5" "Breakpoint.*at.*"
196 # infrun_breakpoint_command_test - This test was broken into two parts
197 # to get around a synchronization problem in expect.
198 # part1: issue the gdb command "commands"
199 # part2: send the list of commands
200 send_gdb "commands\n"
203 pass "commands in infrun_breakpoint_command_test #1"
206 fail "(timeout or eof) commands in infrun_breakpoint_command_test"
209 gdb_test "step\nstep\nstep\nstep\nend" "" \
210 "commands in infrun_breakpoint_command_test #2"
212 gdb_test "continue" \
213 "Continuing.*.*.*Breakpoint \[0-9\]*, factorial \\(value=5\\).*at.*\[0-9\]*\[ \]*if \\(value > 1\\) \{.*\[0-9\]*\[ \]*value \\*= factorial \\(value - 1\\);.*" \
214 "continue in infrun_breakpoint_command_test"
216 gdb_stop_suppressing_tests;
219 proc breakpoint_command_test {} {
220 if [target_info exists noargs] {
221 verbose "Skipping breakpoint_command_test because of noargs."
225 gdb_test "set args 6" "" "set args in breakpoint_command_test"
226 if { ![runto factorial] } then { gdb_suppress_tests; }
227 # Don't depend upon argument passing, since most simulators don't
228 # currently support it. Bash value variable to be what we want.
229 gdb_test "p value=6" "" "set value to 6 in progvar_simple_if_test #2"
231 gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #2"
232 gdb_test "commands\nprintf \"Now the value is %d\\n\", value\nend" \
233 "End with.*" "commands in breakpoint_command_test"
234 gdb_test "continue" \
235 "Breakpoint \[0-9\]*, factorial.*Now the value is 5" \
236 "continue in breakpoint_command_test"
237 gdb_test "print value" " = 5" "print value in breakpoint_command_test"
238 gdb_stop_suppressing_tests;
241 # Test a simple user defined command (with arguments)
242 proc user_defined_command_test {} {
245 gdb_test "set \$foo = 4" "" "set foo in user_defined_command_test"
247 send_gdb "define mycommand\n"
250 pass "define mycommand in user_defined_command_test"
253 fail "(timeout or eof) define mycommand in user_defined_command_test"
256 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
257 gdb_test "while \$arg0 > 0\nset \$arg0 -= 1\nif \(\$arg0 % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
259 "enter commands in user_defined_command_test"
261 gdb_test "mycommand \$foo" \
262 "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
263 "execute user defined command in user_defined_command_test"
264 gdb_test "show user mycommand" \
265 " while \\\$arg0.*set.* if \\\(\\\$arg0.*p/x.* else\[^\n\].*p/x.* end\[^\n\].* end\[^\n\].*" \
266 "display user command in user_defined_command_test"
269 proc watchpoint_command_test {} {
273 if [target_info exists noargs] {
274 verbose "Skipping watchpoint_command_test because of noargs."
278 gdb_test "set args 6" "" "set args in watchpoint_command_test"
279 if { ![runto factorial] } then { return }
282 # Verify that we can create a watchpoint, and give it a commands
283 # list that continues the inferior. We set the watchpoint on a
284 # local variable, too, so that it self-deletes when the watched
285 # data goes out of scope.
287 # What should happen is: Each time the watchpoint triggers, it
288 # continues the inferior. Eventually, the watchpoint will self-
289 # delete, when the watched variable is out of scope. But by that
290 # time, the inferior should have exited. GDB shouldn't crash or
291 # anything untoward as a result of this.
295 send_gdb "watch local_var\n"
297 -re ".*\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
298 set wp_id $expect_out(1,string)
299 pass "watch local_var"
302 {fail "watch local_var"}
303 timeout {fail "(timeout) watch local_var"}
306 if {$wp_id == -1} {return}
308 send_gdb "commands $wp_id\n"
310 -re "Type commands for when breakpoint $wp_id is hit, one per line.*>" {
311 pass "begin commands on watch"
313 -re "$gdb_prompt $" {fail "begin commands on watch"}
314 timeout {fail "(timeout) begin commands on watch"}
316 send_gdb "print value\n"
318 -re ">" {pass "add print command to watch"}
319 -re "$gdb_prompt $" {fail "add print command to watch"}
320 timeout {fail "(timeout) add print command to watch"}
322 send_gdb "continue\n"
324 -re ">" {pass "add continue command to watch"}
325 -re "$gdb_prompt $" {fail "add continue command to watch"}
326 timeout {fail "(timeout) add continue command to watch"}
330 -re "$gdb_prompt $" {pass "end commands on watch"}
331 timeout {fail "(timeout) end commands on watch"}
333 send_gdb "continue\n"
335 -re "Continuing.*\[Ww\]atchpoint $wp_id deleted because the program has left the block in.*which its expression is valid.*run.c:(57|82).*$gdb_prompt $" {
336 pass "continue with watch"
338 -re "$gdb_prompt $" {fail "continue with watch"}
339 timeout {fail "(timeout) continue with watch"}
343 proc test_command_prompt_position {} {
346 if [target_info exists noargs] {
347 verbose "Skipping test_command_prompt_position because of noargs."
351 if { ![runto factorial] } then { gdb_suppress_tests; }
352 # Don't depend upon argument passing, since most simulators don't
353 # currently support it. Bash value variable to be what we want.
355 gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #3"
356 gdb_test "p value=5" "" "set value to 5 in test_command_prompt_position"
357 # All this test should do is print 0xdeadbeef once.
358 gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
359 "\\\$\[0-9\]* = 0xdeadbeef" \
360 "if test in test_command_prompt_position"
362 # Now let's test for the correct position of the '>' in gdb's
363 # prompt for commands. It should be at the beginning of the line,
364 # and not after one space.
366 send_gdb "commands\n"
368 -re "Type commands.*End with.*\[\r\n\]>$" {
369 send_gdb "printf \"Now the value is %d\\n\", value\n"
371 -re "^printf.*value\r\n>$" {
374 -re "^end\r\n$gdb_prompt $" {
375 pass "> OK in test_command_prompt_position"
377 -re ".*$gdb_prompt $" {
378 fail "some other message in test_command_prompt_position"
381 fail "(timeout) 1 in test_command_prompt_position"
385 -re "^ >$" { fail "> not OK in test_command_prompt_position" }
386 -re ".*$gdb_prompt $" {
387 fail "wrong message in test_command_prompt_position"
390 fail "(timeout) 2 in test_command_prompt_position "
394 -re "Type commands.*End with.*\[\r\n\] >$" {
395 fail "prompt not OK in test_command_prompt_position"
397 -re ".*$gdb_prompt $" {
398 fail "commands in test_command_prompt_position"
400 timeout { fail "(timeout) 3 commands in test_command_prompt_position" }
403 gdb_stop_suppressing_tests;
408 proc deprecated_command_test {} {
409 gdb_test "maintenance deprecate blah" "Can't find command.*" \
410 "tried to deprecate non-existing command"
412 gdb_test "maintenance deprecate p \"new_p\"" "" "maintenance deprecate p \"new_p\" /1/"
414 "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
415 "p deprecated warning, with replacement"
416 gdb_test "p 5" ".\[0-9\]* = 5.*" "Deprecated warning goes away /1/"
418 gdb_test "maintenance deprecate p \"new_p\"" "" "maintenance deprecate p \"new_p\" /2/"
419 gdb_test "maintenance deprecate print \"new_print\"" ""
421 "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
422 "both alias and command are deprecated"
423 gdb_test "p 5" ".\[0-9\]* = 5.*" "Deprecated warning goes away /2/"
425 gdb_test "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
427 "deprecate long command /1/"
428 gdb_test "set remote memory-read-packet-size" \
429 "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
430 "long command deprecated /1/"
432 gdb_test "maintenance deprecate set remote memory-read-packet-size" \
434 "deprecate long command /2/"
435 gdb_test "set remote memory-read-packet-size" \
436 "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
437 "long command deprecated with no alternative /2/"
439 gdb_test "maintenance deprecate" \
440 "\"maintenance deprecate\".*" \
441 "deprecate with no arguments"
444 proc bp_deleted_in_command_test {} {
447 if [target_info exists noargs] {
448 verbose "Skipping bp_deleted_in_command_test because of noargs."
452 gdb_test "set args 1" "" "set args in bp_deleted_in_command_test"
455 # Create a breakpoint, and associate a command-list to it, with
456 # one command that deletes this breakpoint.
457 gdb_test "break factorial" \
458 "Breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\." \
459 "breakpoint in bp_deleted_in_command_test"
461 send_gdb "commands\n"
463 -re "Type commands for when breakpoint .* is hit, one per line.*>" {
464 pass "begin commands in bp_deleted_in_command_test"
466 -re "$gdb_prompt $" {fail "begin commands in bp_deleted_in_command_test"}
467 timeout {fail "(timeout) begin commands bp_deleted_in_command_test"}
471 -re ">" {pass "add silent command"}
472 -re "$gdb_prompt $" {fail "add silent command"}
473 timeout {fail "(timeout) add silent command"}
475 send_gdb "clear factorial\n"
477 -re ">" {pass "add clear command"}
478 -re "$gdb_prompt $" {fail "add clear command"}
479 timeout {fail "(timeout) add clear command"} }
480 send_gdb "printf \"factorial command-list executed\\n\"\n"
482 -re ">" {pass "add printf command"}
483 -re "$gdb_prompt $" {fail "add printf command"}
484 timeout {fail "(timeout) add printf command"}
488 -re ">" {pass "add cont command"}
489 -re "$gdb_prompt $" {fail "add cont command"}
490 timeout {fail "(timeout) add cont command"} }
493 -re "$gdb_prompt $" {pass "end commands"}
494 timeout {fail "(timeout) end commands"}
499 -re ".*factorial command-list executed.*1.*$gdb_prompt $" {
500 pass "run factorial until breakpoint"
502 -re ".*$gdb_prompt $" {
503 fail "run factorial until breakpoint"
505 default { fail "(timeout) run factorial until breakpoint" }
506 timeout { fail "(timeout) run factorial until breakpoint" }
510 proc temporary_breakpoint_commands {} {
513 if [target_info exists noargs] {
514 verbose "Skipping temporary_breakpoint_commands because of noargs."
518 gdb_test "set args 1" "" "set args in temporary_breakpoint_commands"
521 # Create a temporary breakpoint, and associate a commands list to it.
522 # This test will verify that this commands list is executed when the
524 gdb_test "tbreak factorial" \
525 "Breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\." \
526 "breakpoint in temporary_breakpoint_commands"
528 send_gdb "commands\n"
530 -re "Type commands for when breakpoint .* is hit, one per line.*>" {
531 pass "begin commands in bp_deleted_in_command_test"
533 -re "$gdb_prompt $" {fail "begin commands in bp_deleted_in_command_test"}
534 timeout {fail "(timeout) begin commands bp_deleted_in_command_test"}
538 -re ">" {pass "add silent tbreak command"}
539 -re "$gdb_prompt $" {fail "add silent tbreak command"}
540 timeout {fail "(timeout) add silent tbreak command"}
542 send_gdb "printf \"factorial tbreak commands executed\\n\"\n"
544 -re ">" {pass "add printf tbreak command"}
545 -re "$gdb_prompt $" {fail "add printf tbreak command"}
546 timeout {fail "(timeout) add printf tbreak command"}
550 -re ">" {pass "add cont tbreak command"}
551 -re "$gdb_prompt $" {fail "add cont tbreak command"}
552 timeout {fail "(timeout) add cont tbreak command"} }
555 -re "$gdb_prompt $" {pass "end tbreak commands"}
556 timeout {fail "(timeout) end tbreak commands"}
561 -re ".*factorial tbreak commands executed.*1.*$gdb_prompt $" {
562 pass "run factorial until temporary breakpoint"
564 timeout { fail "(timeout) run factorial until temporary breakpoint" }
568 # Test that GDB can handle $arg0 outside of user functions without
570 proc stray_arg0_test { } {
571 gdb_test "print \$arg0" \
572 "\\\$\[0-9\]* = void" \
575 gdb_test "if 1 == 1\nprint \$arg0\nend" \
576 "\\\$\[0-9\]* = void" \
579 gdb_test "print \$arg0 = 1" \
583 gdb_test "print \$arg0" \
588 # Test that GDB can handle arguments when sourcing files recursively.
589 # If the arguments are overwritten with ####### then the test has failed.
590 proc recursive_source_test {} {
591 set fd [open "file1" w]
597 set fd [open "file2" w]
600 echo 1: <<<$arg0>>>\n
602 echo 2: <<<$arg0>>>\n
606 set fd [open "file3" w]
609 #################################################################"
612 gdb_test "source file1" \
613 "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
614 "recursive source test"
621 gdbvar_simple_if_test
622 gdbvar_simple_while_test
623 gdbvar_complex_if_while_test
624 progvar_simple_if_test
625 progvar_simple_while_test
626 progvar_complex_if_while_test
627 if_while_breakpoint_command_test
628 infrun_breakpoint_command_test
629 breakpoint_command_test
630 user_defined_command_test
631 watchpoint_command_test
632 test_command_prompt_position
633 deprecated_command_test
634 bp_deleted_in_command_test
635 temporary_breakpoint_commands
637 recursive_source_test