Modify gdb.base/commands.exp to test multi breakpoints command clearing.
[external/binutils.git] / gdb / testsuite / gdb.base / commands.exp
1 #   Copyright 1988-2018 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 #
17 # test special commands (if, while, etc)
18 #
19
20 standard_testfile
21
22 if { [prepare_for_testing "failed to prepare" commands run.c {debug additional_flags=-DFAKEARGV}] } {
23     return -1
24 }
25
26 # Run to FUNCTION.  If that fails, issue a FAIL and make the caller
27 # return.
28
29 proc runto_or_return {function} {
30     if { ![runto factorial] } {
31         fail "cannot run to $function"
32         return -code return
33     }
34 }
35
36 proc_with_prefix gdbvar_simple_if_test {} {
37     global valnum_re
38
39     gdb_test_no_output "set \$foo = 0" "set foo"
40     # All this test should do is print 0xdeadbeef once.
41     gdb_test \
42         [multi_line_input \
43              {if $foo == 1} \
44              {  p/x 0xfeedface} \
45              {else} \
46              {  p/x 0xdeadbeef} \
47              {end}] \
48         "$valnum_re = 0xdeadbeef" \
49         "#1"
50
51     # All this test should do is print 0xfeedface once.
52     gdb_test \
53         [multi_line_input \
54              {if $foo == 0} \
55              {  p/x 0xfeedface} \
56              {else} \
57              {  p/x 0xdeadbeef} \
58              {end}] \
59         "$valnum_re = 0xfeedface" \
60         "#2"
61 }
62
63 proc_with_prefix gdbvar_simple_while_test {} {
64     global valnum_re
65
66     gdb_test_no_output "set \$foo = 5" "set foo"
67     # This test should print 0xfeedface five times.
68     gdb_test \
69         [multi_line_input \
70              {while $foo > 0} \
71              {  p/x 0xfeedface} \
72              {  set $foo -= 1} \
73              {end}] \
74         [multi_line \
75              "$valnum_re = 0xfeedface" \
76              "$valnum_re = 0xfeedface" \
77              "$valnum_re = 0xfeedface" \
78              "$valnum_re = 0xfeedface" \
79              "$valnum_re = 0xfeedface"] \
80         "#1"
81 }
82
83 proc_with_prefix gdbvar_complex_if_while_test {} {
84     global valnum_re
85
86     gdb_test_no_output "set \$foo = 4" "set foo"
87     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
88     gdb_test \
89         [multi_line_input \
90              {while $foo > 0} \
91              {  set $foo -= 1} \
92              {  if ($foo % 2) == 1} \
93              {    p/x 0xdeadbeef} \
94              {  else} \
95              {    p/x 0xfeedface} \
96              {  end} \
97              {end}] \
98         [multi_line \
99              "$valnum_re = 0xdeadbeef" \
100              "$valnum_re = 0xfeedface" \
101              "$valnum_re = 0xdeadbeef" \
102              "$valnum_re = 0xfeedface"] \
103         "#1"
104 }
105
106 proc_with_prefix progvar_simple_if_test {} {
107     global valnum_re
108
109     runto_or_return factorial
110
111     # Don't depend upon argument passing, since most simulators don't
112     # currently support it.  Bash value variable to be what we want.
113     gdb_test "p value=5" " = 5" "set value to 5"
114     # All this test should do is print 0xdeadbeef once.
115     gdb_test \
116         [multi_line_input \
117              {if value == 1} \
118              {  p/x 0xfeedface} \
119              {else} \
120              {  p/x 0xdeadbeef} \
121              {end}] \
122         "$valnum_re = 0xdeadbeef" \
123         "#1"
124
125     # All this test should do is print 0xfeedface once.
126     gdb_test \
127         [multi_line_input \
128              {if value == 5} \
129              {  p/x 0xfeedface} \
130              {else} \
131              {  p/x 0xdeadbeef} \
132              {end}] \
133         "$valnum_re = 0xfeedface" \
134         "#2"
135 }
136
137 proc_with_prefix progvar_simple_while_test {} {
138     global valnum_re
139
140     runto_or_return factorial
141
142     # Don't depend upon argument passing, since most simulators don't
143     # currently support it.  Bash value variable to be what we want.
144     gdb_test "p value=5" " = 5" "set value to 5"
145     # This test should print 0xfeedface five times.
146     gdb_test \
147         [multi_line_input \
148              {while value > 0} \
149              {  p/x 0xfeedface} \
150              {  set value -= 1} \
151              {end}] \
152         [multi_line \
153              "$valnum_re = 0xfeedface" \
154              "$valnum_re = 0xfeedface" \
155              "$valnum_re = 0xfeedface" \
156              "$valnum_re = 0xfeedface" \
157              "$valnum_re = 0xfeedface"] \
158         "#1"
159 }
160
161 proc_with_prefix progvar_complex_if_while_test {} {
162     global valnum_re
163
164     runto_or_return factorial
165
166     # Don't depend upon argument passing, since most simulators don't
167     # currently support it.  Bash value variable to be what we want.
168     gdb_test "p value=4" " = 4" "set value to 4"
169     # This test should alternate between 0xdeadbeef and 0xfeedface two
170     # times.
171     gdb_test \
172         [multi_line_input \
173              {while value > 0} \
174              {  set value -= 1} \
175              {  if (value % 2) == 1} \
176              {    p/x 0xdeadbeef} \
177              {  else} \
178              {    p/x 0xfeedface} \
179              {  end} \
180              {end}] \
181         [multi_line \
182              "$valnum_re = 0xdeadbeef" \
183              "$valnum_re = 0xfeedface" \
184              "$valnum_re = 0xdeadbeef" \
185              "$valnum_re = 0xfeedface"] \
186         "#1"
187 }
188
189 proc_with_prefix if_while_breakpoint_command_test {} {
190     global valnum_re
191
192     runto_or_return factorial
193
194     # Don't depend upon argument passing, since most simulators don't
195     # currently support it.  Bash value variable to be what we want.
196     gdb_test "p value=5" " = 5" "set value to 5"
197     delete_breakpoints
198     gdb_test "break factorial" "Breakpoint.*at.*" "break factorial"
199
200     gdb_test_multiple "commands" "commands" {
201         -re "End with" {
202             pass "commands"
203         }
204     }
205
206     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
207     gdb_test \
208         [multi_line_input \
209              {while value > 0} \
210              {  set value -= 1} \
211              {  if (value % 2) == 1} \
212              {    p/x 0xdeadbeef} \
213              {  else} \
214              {    p/x 0xfeedface} \
215              {  end} \
216              {end} \
217              {end}] \
218         "" \
219         "commands part 2"
220     gdb_test \
221         "continue" \
222         [multi_line \
223              "$valnum_re = 0xdeadbeef" \
224              "$valnum_re = 0xfeedface" \
225              "$valnum_re = 0xdeadbeef" \
226              "$valnum_re = 0xfeedface"] \
227         "#1"
228     gdb_test "info break" "while.*set.*if.*p/x.*else.*p/x.*end.*"
229 }
230
231 # Test that we can run the inferior from breakpoint commands.
232 #
233 # The expected behavior is that all commands after the first "step"
234 # shall be ignored.  See the gdb manual, "Break Commands",
235 # subsection "Breakpoint command lists".
236
237 proc_with_prefix infrun_breakpoint_command_test {} {
238     runto_or_return factorial
239
240     # Don't depend upon argument passing, since most simulators don't
241     # currently support it.  Bash value variable to be what we want.
242     gdb_test "p value=6" " = 6" "set value to 6"
243     delete_breakpoints
244     gdb_test "break factorial if value == 5" "Breakpoint.*at.*"
245
246 # infrun_breakpoint_command_test - This test was broken into two parts 
247 # to get around a synchronization problem in expect.
248 # part1: issue the gdb command "commands"
249 # part2: send the list of commands
250
251     set test "commands #1"
252     gdb_test_multiple "commands" $test {
253         -re "End with" {
254             pass $test
255         }
256     }
257     gdb_test "step\nstep\nstep\nstep\nend" "" \
258         "commands #2"
259
260     gdb_test "continue" \
261         "Continuing.*.*.*Breakpoint \[0-9\]*, factorial \\(value=5\\).*at.*\[0-9\]*\[      \]*if \\(value > 1\\) \{.*\[0-9\]*\[      \]*value \\*= factorial \\(value - 1\\);.*"
262 }
263
264 proc_with_prefix breakpoint_command_test {} {
265     runto_or_return factorial
266
267     # Don't depend upon argument passing, since most simulators don't
268     # currently support it.  Bash value variable to be what we want.
269     gdb_test "p value=6" " = 6" "set value to 6"
270     delete_breakpoints
271     gdb_test "break factorial" "Breakpoint.*at.*"
272     gdb_test \
273         [multi_line_input \
274              {commands} \
275              {  printf "Now the value is %d\n", value} \
276              {end}] \
277         "End with.*" \
278         "commands"
279     gdb_test "continue" \
280             "Breakpoint \[0-9\]*, factorial.*Now the value is 5"
281     gdb_test "print value" " = 5"
282 }
283
284 # Test clearing the commands of several breakpoints with one single "end".
285 proc_with_prefix breakpoint_clear_command_test {} {
286     runto_or_return factorial
287
288     set any "\[^\r\n\]*"
289     delete_breakpoints
290     gdb_test "break factorial" "Breakpoint.*at.*"
291     gdb_test_no_output "set \$bpnumfactorial = \$bpnum"
292     gdb_test "break main" "Breakpoint.*at.*"
293     gdb_test_no_output "set \$bpnummain = \$bpnum"
294
295     gdb_test \
296         [multi_line_input \
297              {commands $bpnumfactorial $bpnummain} \
298              {  print 1234321} \
299              {end}] \
300         "End with.*" \
301         "set commands of two breakpoints to print 1234321"
302     gdb_test "info breakpoints" \
303         [multi_line \
304              "${any}What${any}" \
305              "${any}in factorial${any}" \
306              "${any}print 1234321${any}" \
307              "${any}in main${any}" \
308              "${any}print 1234321${any}" \
309             ] \
310         "print 1234321 command present in the two breakpoints"
311     gdb_test \
312         [multi_line_input \
313              {commands $bpnumfactorial $bpnummain} \
314              {end}] \
315         "End with.*" \
316         "clear the command list of the two breakpoints"
317     gdb_test "info breakpoints" \
318         [multi_line \
319              "${any}What${any}" \
320              "${any}in factorial${any}" \
321              "${any}in main${any}" \
322             ] \
323         "print 1234321 command is not present anymore in the two breakpoints"
324     }
325
326 # Test a simple user defined command (with arguments)
327 proc_with_prefix user_defined_command_test {} {
328     global valnum_re
329
330     gdb_test_no_output "set \$foo = 4" "set foo"
331
332     gdb_test_multiple "define mycommand" "define mycommand" {
333         -re "End with"  {
334             pass "define mycommand"
335         }
336     }
337
338     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
339     gdb_test \
340         [multi_line_input \
341              {while $arg0 > 0} \
342              {  set $arg0 -= 1} \
343              {  if ($arg0 % 2) == 1} \
344              {    p/x 0xdeadbeef} \
345              {  else} \
346              {    p/x 0xfeedface} \
347              {  end} \
348              {end} \
349              {end}] \
350         "" \
351         "enter commands"
352
353     global decimal
354     set valnum_re "\\\$$decimal"
355
356     gdb_test \
357         {mycommand $foo} \
358         [multi_line \
359              "$valnum_re = 0xdeadbeef" \
360              "$valnum_re = 0xfeedface" \
361              "$valnum_re = 0xdeadbeef" \
362              "$valnum_re = 0xfeedface"] \
363         "execute user-defined command"
364    gdb_test "show user mycommand" \
365         "  while \\\$arg0.*set.*    if \\\(\\\$arg0.*p/x.*    else\[^\n\].*p/x.*    end\[^\n\].*  end\[^\n\].*" \
366            "display user command"
367
368     # Create and test a user-defined command with an empty body.
369     gdb_test_multiple "define myemptycommand" "define myemptycommand" {
370         -re "End with"  {
371             pass "define myemptycommand"
372         }
373     }
374     gdb_test "end" \
375         "" \
376         "end definition of user-defined command with empty body"
377
378     gdb_test_no_output "myemptycommand" \
379         "execute user-defined empty command"
380
381     gdb_test "show user" \
382         "User command \"myemptycommand.*" \
383         "display empty command in command list"
384
385     gdb_test "show user myemptycommand" \
386         "User command \"myemptycommand.*" \
387         "display user-defined empty command"
388 }
389
390 # Test that the case with which the command was defined is preserved.
391
392 proc_with_prefix user_defined_command_case_sensitivity {} {
393     # Define a first command with mixed case name.
394     set test "define Homer-Simpson"
395     gdb_test_multiple $test $test {
396             -re "End with"  {
397                 pass $test
398             }
399         }
400
401     gdb_test "print 123\nend" "" "enter commands 1"
402
403     # Define a second command, same name but different case.
404     set test "define HomeR-SimpsoN"
405     gdb_test_multiple $test $test {
406             -re "End with"  {
407                 pass $test
408             }
409         }
410
411     gdb_test "print 456\nend" "" "enter commands 2"
412
413     gdb_test "Homer-Simpson" " = 123" "execute command Homer-Simpson"
414     gdb_test "HomeR-SimpsoN" " = 456" "execute command HomeR-SimpsoN"
415     gdb_test "HOMER-SIMPSON" "Undefined command.*" "try to call in upper case"
416     gdb_test "homer-simpson" "Undefined command.*" "try to call in lower case"
417 }
418
419 # Test that "eval" in a user-defined command expands $argc/$argN.
420
421 proc_with_prefix user_defined_command_args_eval {} {
422     gdb_test_multiple "define command_args_eval" \
423         "define command_args_eval" {
424             -re "End with"  {
425                 pass "define"
426             }
427         }
428
429     # Make a command that constructs references to $argc and $argN via
430     # eval.
431     gdb_test \
432         [multi_line \
433              {eval "printf \"argc = %%d,\", $arg%c", 'c'} \
434              {set $i = 0} \
435              {while $i < $argc} \
436              {  eval "printf \" %%d\", $arg%d", $i} \
437              {  set $i = $i + 1} \
438              {end} \
439              {printf "\n"} \
440              {end}] \
441         "" \
442         "enter commands"
443
444     gdb_test "command_args_eval 1 2 3" "argc = 3, 1 2 3" "execute command"
445 }
446
447 # Test that the $argc/$argN variables are pushed on/popped from the
448 # args stack correctly when a user-defined command calls another
449 # user-defined command (or in this case, recurses).
450
451 proc_with_prefix user_defined_command_args_stack_test {} {
452     gdb_test_multiple "define args_stack_command" \
453         "define args_stack_command" {
454             -re "End with"  {
455                 pass "define"
456             }
457         }
458
459     # Make a command that refers to $argc/$argN before and after
460     # recursing.  Also, vary the number of arguments passed to each
461     # recursion point.
462     gdb_test \
463         [multi_line \
464              {printf "before, argc = %d,", $argc} \
465              {set $i = 0} \
466              {while $i < $argc} \
467              {  eval "printf \" %%d\", $arg%d", $i} \
468              {  set $i = $i + 1} \
469              {end} \
470              {printf "\n"} \
471              {} \
472              {} \
473              {if $argc == 3} \
474              {  args_stack_command 21 22} \
475              {end} \
476              {if $argc == 2} \
477              {  args_stack_command 11} \
478              {end} \
479              {} \
480              {} \
481              {printf "after, argc = %d,", $argc} \
482              {set $i = 0} \
483              {while $i < $argc} \
484              {  eval "printf \" %%d\", $arg%d", $i} \
485              {  set $i = $i + 1} \
486              {end} \
487              {printf "\n"} \
488              {end}] \
489         "" \
490         "enter commands"
491
492     set expected \
493         [multi_line \
494              "before, argc = 3, 31 32 33" \
495              "before, argc = 2, 21 22" \
496              "before, argc = 1, 11" \
497              "after, argc = 1, 11" \
498              "after, argc = 2, 21 22" \
499              "after, argc = 3, 31 32 33"]
500     gdb_test "args_stack_command 31 32 33" $expected "execute command"
501 }
502
503 # Test a simple user defined command with many arguments.  GDB <= 7.12
504 # used to have a hard coded limit of 10 arguments.
505
506 proc_with_prefix user_defined_command_manyargs_test {} {
507     set test "define command"
508     gdb_test_multiple "define manyargs" $test {
509         -re "End with"  {
510             pass $test
511         }
512     }
513
514     # Define a function that doubles its arguments.
515     gdb_test \
516         [multi_line \
517              {printf "nargs=%d:", $argc} \
518              {set $i = 0} \
519              {while $i < $argc} \
520              {  eval "printf \" %%d\", 2 * $arg%d\n", $i} \
521              {  set $i = $i + 1} \
522              {end} \
523              {printf "\n"} \
524              {end}] \
525         "" \
526         "enter commands"
527
528     # Some random number of arguments, as long as higher than 10.
529     set nargs 100
530
531     set cmd "manyargs"
532     for {set i 1} {$i <= $nargs} {incr i} {
533         append cmd " $i"
534     }
535
536     set expected "nargs=$nargs:"
537     for {set i 1} {$i <= $nargs} {incr i} {
538         append expected " " [expr 2 * $i]
539     }
540
541     gdb_test $cmd $expected "execute command"
542 }
543
544 proc_with_prefix watchpoint_command_test {} {
545     global gdb_prompt
546
547     # Disable hardware watchpoints if necessary.
548     if [target_info exists gdb,no_hardware_watchpoints] {
549         gdb_test_no_output "set can-use-hw-watchpoints 0" ""
550     }
551
552     runto_or_return factorial
553
554     delete_breakpoints
555
556     # Verify that we can create a watchpoint, and give it a commands
557     # list that continues the inferior.  We set the watchpoint on a
558     # local variable, too, so that it self-deletes when the watched
559     # data goes out of scope.
560     #
561     # What should happen is: Each time the watchpoint triggers, it
562     # continues the inferior.  Eventually, the watchpoint will self-
563     # delete, when the watched variable is out of scope.  But by that
564     # time, the inferior should have exited.  GDB shouldn't crash or
565     # anything untoward as a result of this.
566     #
567     set wp_id -1
568
569     gdb_test_multiple "watch local_var" "watch local_var" {
570         -re "\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
571             set wp_id $expect_out(1,string)
572             pass "watch local_var"
573         }
574     }
575
576     if {$wp_id == -1} {return}
577
578     gdb_test_multiple "commands $wp_id" "begin commands on watch" {
579         -re "Type commands for breakpoint.*, one per line.*>$" {
580             pass "begin commands on watch"
581         }
582     }
583     # See the 'No symbol "value...' fail below.  This command will
584     # fail if it's executed in the wrong frame.  If adjusting the
585     # test, make sure this property holds.
586     gdb_test_multiple "print value" "add print command to watch" {
587         -re ">$" {
588             pass "add print command to watch"
589         }
590     }
591     gdb_test_multiple "continue" "add continue command to watch" {
592         -re ">$" {
593             pass "add continue command to watch"
594         }
595     }
596     gdb_test "end" \
597         "" \
598         "end commands on watch"
599
600     set test "continue with watch"
601     set lno_1 [gdb_get_line_number "commands.exp: hw local_var out of scope" "run.c"]
602     set lno_2 [gdb_get_line_number "commands.exp: local_var out of scope"    "run.c"]
603     gdb_test_multiple "continue" "$test" {
604         -re "No symbol \"value\" in current context.\r\n$gdb_prompt $" {
605             # Happens if GDB actually runs the watchpoints commands,
606             # even though the watchpoint was deleted for not being in
607             # scope.
608             fail $test
609         }
610         -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 $" {
611             pass $test
612         }
613    }
614 }
615
616 proc_with_prefix test_command_prompt_position {} {
617     global gdb_prompt
618     global valnum_re
619
620     runto_or_return factorial
621
622     # Don't depend upon argument passing, since most simulators don't
623     # currently support it.  Bash value variable to be what we want.
624     delete_breakpoints
625     gdb_test "break factorial" "Breakpoint.*at.*"
626     gdb_test "p value=5" ".*" "set value to 5"
627     # All this test should do is print 0xdeadbeef once.
628     gdb_test \
629         [multi_line_input \
630              {if value == 1} \
631              {  p/x 0xfeedface} \
632              {else} \
633              {  p/x 0xdeadbeef} \
634              {end}] \
635         "$valnum_re = 0xdeadbeef" \
636         "if test"
637
638     # Now let's test for the correct position of the '>' in gdb's
639     # prompt for commands.  It should be at the beginning of the line,
640     # and not after one space.
641
642     set test "> OK"
643     gdb_test_multiple "commands" $test {
644         -re "Type commands.*End with.*\[\r\n\]>$" {
645             gdb_test_multiple "printf \"Now the value is %d\\n\", value" $test {
646                 -re "^printf.*value\r\n>$" {
647                     gdb_test_multiple "end" $test {
648                         -re "^end\r\n$gdb_prompt $" { 
649                             pass $test
650                         }
651                     }
652                 }
653             }
654         }
655     }
656 }
657
658
659
660 proc_with_prefix deprecated_command_test {} {
661     gdb_test "maintenance deprecate blah" "Can't find command.*" \
662           "tried to deprecate non-existing command"
663
664     gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /1/"
665     gdb_test "p 5" \
666             "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
667             "p deprecated warning, with replacement"
668     gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /1/"
669
670     gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /2/"
671     gdb_test_no_output "maintenance deprecate print \"new_print\"" 
672     gdb_test "p 5" \
673             "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
674             "both alias and command are deprecated"
675     gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /2/"
676
677     gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
678             "deprecate long command /1/"
679     gdb_test "set remote memory-read-packet-size" \
680             "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
681             "long command deprecated /1/"
682
683     gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size" \
684             "deprecate long command /2/"
685     gdb_test "set remote memory-read-packet-size" \
686             "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
687             "long command deprecated with no alternative /2/"
688
689     gdb_test "maintenance deprecate" \
690             "\"maintenance deprecate\".*" \
691             "deprecate with no arguments"
692 }
693
694 proc_with_prefix bp_deleted_in_command_test {} {
695     global gdb_prompt
696
697     delete_breakpoints
698
699     # Create a breakpoint, and associate a command-list to it, with
700     # one command that deletes this breakpoint.
701     gdb_test "break factorial" \
702              "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\."
703     
704     gdb_test_multiple "commands" "begin commands" {
705       -re "Type commands for breakpoint.*>$" {
706           pass "begin commands"
707       }
708     }
709     gdb_test_multiple "silent" "add silent command" {
710         -re ">$" {
711             pass "add silent command"
712         }
713     }
714     gdb_test_multiple "clear factorial" "add clear command" {
715         -re ">$" {
716             pass "add clear command"
717         }
718     }
719     gdb_test_multiple "printf \"factorial command-list executed\\n\"" \
720         "add printf command" {
721         -re ">$" {
722             pass "add printf command"
723         }
724     }
725     gdb_test_multiple "cont" "add cont command" {
726         -re ">$" {
727             pass "add cont command"
728         }
729     }
730     gdb_test "end" \
731         "" \
732         "end commands"
733
734     gdb_run_cmd
735     gdb_test "" "factorial command-list executed.*" "run factorial until breakpoint"
736 }
737
738 proc_with_prefix temporary_breakpoint_commands {} {
739     delete_breakpoints
740
741     # Create a temporary breakpoint, and associate a commands list to it.
742     # This test will verify that this commands list is executed when the
743     # breakpoint is hit.
744     gdb_test "tbreak factorial" \
745             "Temporary breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\." \
746             "breakpoint"
747
748     gdb_test_multiple "commands" \
749         "begin commands in bp_deleted_in_command_test" {
750             -re "Type commands for breakpoint.*>$" {
751                 pass "begin commands"
752             }
753         }
754     gdb_test_multiple "silent" "add silent tbreak command" {
755         -re ">$" {
756             pass "add silent tbreak command"
757         }
758     }
759     gdb_test_multiple "printf \"factorial tbreak commands executed\\n\"" \
760         "add printf tbreak command" {
761             -re ">$" {
762                 pass "add printf tbreak command"
763             }
764         }
765     gdb_test_multiple "cont" "add cont tbreak command" {
766         -re ">$" {
767             pass "add cont tbreak command"
768         }
769     }
770     gdb_test "end" \
771         "" \
772         "end tbreak commands"
773
774     gdb_run_cmd
775     gdb_test "" "factorial tbreak commands executed.*" \
776         "run factorial until temporary breakpoint"
777 }
778
779 # Test that GDB can handle $arg0 outside of user functions without
780 # crashing.
781 proc_with_prefix stray_arg0_test { } {
782     global valnum_re
783
784     gdb_test "print \$arg0" \
785         "$valnum_re = void" \
786         "#1"
787
788     gdb_test "if 1 == 1\nprint \$arg0\nend" \
789         "$valnum_re = void" \
790         "#2"
791
792     gdb_test "print \$arg0 = 1" \
793         "$valnum_re = 1" \
794         "#3"
795
796     gdb_test "print \$arg0" \
797         "$valnum_re = 1" \
798         "#4"
799 }
800
801 # Test that GDB is able to source a file with an indented comment.
802 proc_with_prefix source_file_with_indented_comment {} {
803     set file1 [standard_output_file file1]
804
805     set fd [open "$file1" w]
806     puts $fd \
807 {define my_fun
808     #indented comment
809 end
810 echo Done!\n}
811     close $fd
812
813     gdb_test "source $file1" "Done!" "source file"
814 }
815
816 # Test that GDB can handle arguments when sourcing files recursively.
817 # If the arguments are overwritten with ####### then the test has failed.
818 proc_with_prefix recursive_source_test {} {
819     set file1 [standard_output_file file1]
820     set file2 [standard_output_file file2]
821     set file3 [standard_output_file file3]
822
823     set fd [open "$file1" w]
824     puts $fd \
825 "source $file2
826 abcdef qwerty"
827     close $fd
828
829     set fd [open "$file2" w]
830     puts $fd \
831 "define abcdef
832   echo 1: <<<\$arg0>>>\\n
833   source $file3
834   echo 2: <<<\$arg0>>>\\n
835 end"
836     close $fd
837
838     set fd [open "$file3" w]
839     puts $fd \
840 "echo in file3\\n
841 #################################################################"
842     close $fd
843
844     gdb_test "source $file1" \
845         "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
846         "source file"
847
848     file delete $file1
849     file delete $file2
850     file delete $file3
851 }
852
853 proc gdb_test_no_prompt { command result msg } {
854     set msg "$command - $msg"
855     set result "^[string_to_regexp $command]\r\n$result$"
856     gdb_test_multiple $command $msg {
857         -re "$result" {
858             pass $msg
859             return 1
860         }
861         -re "\r\n *>$" {
862             fail $msg
863             return 0
864         }
865     }
866     return 0
867 }
868
869 proc_with_prefix if_commands_test {} {
870     global gdb_prompt
871
872     gdb_test_no_output "set \$tem = 1" "set \$tem"
873
874     set test "if_commands_test 1"
875     gdb_test_no_prompt "if \$tem == 2" { >} $test
876     gdb_test_no_prompt "break main" { >} $test
877     gdb_test_no_prompt "else" { >} $test
878     gdb_test_no_prompt "break factorial" { >} $test
879     gdb_test_no_prompt "commands" {  >} $test
880     gdb_test_no_prompt "silent" {  >} $test
881     gdb_test_no_prompt "set \$tem = 3" {  >} $test
882     gdb_test_no_prompt "continue" {  >} $test
883     gdb_test_multiple "end" "first end - $test" {
884         -re " >\$" {
885             pass "first end - $test"
886         }
887         -re "\r\n>\$" {
888             fail "first end - $test"
889         }
890     }
891     gdb_test_multiple "end" "second end - $test" {
892         -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
893             pass "second end - $test"
894         }
895         -re "Undefined command: \"silent\".*$gdb_prompt $" {
896             fail "second end - $test"
897         }
898     }
899
900     set test "if_commands_test 2"
901     gdb_test_no_prompt "if \$tem == 1" { >} $test
902     gdb_test_no_prompt "break main" { >} $test
903     gdb_test_no_prompt "else" { >} $test
904     gdb_test_no_prompt "break factorial" { >} $test
905     gdb_test_no_prompt "commands" {  >} $test
906     gdb_test_no_prompt "silent" {  >} $test
907     gdb_test_no_prompt "set \$tem = 3" {  >} $test
908     gdb_test_no_prompt "continue" {  >} $test
909     gdb_test_multiple "end" "first end - $test" {
910         -re " >\$" {
911             pass "first end - $test"
912         }
913         -re "\r\n>\$" {
914             fail "first end - $test"
915         }
916     }
917     gdb_test_multiple "end" "second end - $test" {
918         -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
919             pass "second end - $test"
920         }
921     }
922 }
923
924 # Verify an error during "commands" commands execution will prevent any other
925 # "commands" from other breakpoints at the same location to be executed.
926
927 proc_with_prefix error_clears_commands_left {} {
928     set test "hook-stop 1"
929     gdb_test_multiple {define hook-stop} $test {
930         -re "End with a line saying just \"end\"\\.\r\n>$" {
931             pass $test
932         }
933     }
934     set test "hook-stop 1a"
935     gdb_test_multiple {echo hook-stop1\n} $test {
936         -re "\r\n>$" {
937             pass $test
938         }
939     }
940     gdb_test_no_output "end" "hook-stop 1b"
941
942     delete_breakpoints
943     gdb_breakpoint "main"
944
945     set test "main commands 1"
946     gdb_test_multiple {commands $bpnum} $test {
947         -re "End with a line saying just \"end\"\\.\r\n>$" {
948             pass $test
949         }
950     }
951     set test "main commands 1a"
952     gdb_test_multiple {echo cmd1\n} $test {
953         -re "\r\n>$" {
954             pass $test
955         }
956     }
957     set test "main commands 1b"
958     gdb_test_multiple {errorcommandxy\n} $test {
959         -re "\r\n>$" {
960             pass $test
961         }
962     }
963     gdb_test_no_output "end" "main commands 1c"
964
965     gdb_breakpoint "main"
966     set test "main commands 2"
967     gdb_test_multiple {commands $bpnum} $test {
968         -re "End with a line saying just \"end\"\\.\r\n>$" {
969             pass $test
970         }
971     }
972     set test "main commands 2a"
973     gdb_test_multiple {echo cmd2\n} $test {
974         -re "\r\n>$" {
975             pass $test
976         }
977     }
978     set test "main commands 2b"
979     gdb_test_multiple {errorcommandyz\n} $test {
980         -re "\r\n>$" {
981             pass $test
982         }
983     }
984     gdb_test_no_output "end" "main commands 2c"
985
986     gdb_run_cmd
987     gdb_test \
988         "" \
989         [multi_line \
990              "hook-stop1" \
991              ".*" \
992              "cmd1" \
993              "Undefined command: \"errorcommandxy\"\\.  Try \"help\"\\."] \
994         "cmd1 error"
995
996     gdb_test {echo idle\n} "\r\nidle" "no cmd2"
997 }
998
999 proc_with_prefix redefine_hook_test {} {
1000     gdb_test \
1001         [multi_line_input \
1002              "define one"\
1003              "end"] \
1004         "" \
1005         "define one"
1006
1007     gdb_test \
1008         [multi_line_input \
1009              "define hook-one" \
1010              "echo hibob\\n" \
1011              "end"] \
1012         "" \
1013         "define hook-one"
1014
1015     set test "redefine one"
1016     gdb_test_multiple "define one" $test {
1017         -re "Redefine command .one.. .y or n. $" {
1018             send_gdb "y\n"
1019             exp_continue
1020         }
1021
1022         -re "End with"  {
1023             pass $test
1024         }
1025     }
1026
1027     gdb_test "end" "" "enter commands for one redefinition"
1028
1029     gdb_test "one" "hibob" "execute one command"
1030 }
1031
1032 proc_with_prefix redefine_backtrace_test {} {
1033     gdb_test_multiple "define backtrace" "define backtrace" {
1034         -re "Really redefine built-in command \"backtrace\"\\? \\(y or n\\) $"  {
1035             pass "define backtrace"
1036         }
1037     }
1038
1039     gdb_test_multiple "y" "expect response to define backtrace" {
1040         -re "End with a line saying just \"end\"\\.\r\n>$"  {
1041             pass "expect response to define backtrace"
1042         }
1043     }
1044
1045     gdb_test \
1046         [multi_line_input \
1047              "echo hibob\\n" \
1048              "end"] \
1049         "" \
1050         "enter commands"
1051
1052     gdb_test "backtrace" "hibob" "execute backtrace command"
1053     gdb_test "bt" "hibob" "execute bt command"
1054 }
1055
1056 # Test using "if" and "while" without args when building a command list.
1057
1058 proc define_if_without_arg_test {} {
1059     foreach cmd {if while define} {
1060         set test "define some_command_$cmd"
1061         gdb_test_multiple $test $test {
1062             -re "End with"  {
1063                 pass $test
1064             }
1065         }
1066
1067         gdb_test "$cmd" "$cmd command requires an argument." "type $cmd without args"
1068     }
1069 }
1070
1071 # Test the loop_break command.
1072
1073 proc_with_prefix loop_break_test {} {
1074     gdb_test_no_output "set \$a = 0" "initialize \$a"
1075     gdb_test_no_output "set \$total = 0" "initialize \$total"
1076
1077     gdb_test \
1078         [multi_line_input \
1079              "while \$a < 5" \
1080              "  if \$a == 4" \
1081              "    loop_break" \
1082              "  end" \
1083              "  set \$b = 0" \
1084              "  while \$b < 5" \
1085              "    if \$b == 2" \
1086              "      loop_break" \
1087              "    end" \
1088              "    set \$total = \$total + 1" \
1089              "    set \$b = \$b + 1" \
1090              "  end" \
1091              "  set \$a = \$a + 1" \
1092              "end"] \
1093         "" \
1094         "run while loop"
1095
1096     gdb_test "print \$a" " = 4" "validate \$a"
1097     gdb_test "print \$b" " = 2" "validate \$b"
1098     gdb_test "print \$total" " = 8" "validate \$total"
1099 }
1100
1101 # Test the loop_continue command.
1102
1103 proc_with_prefix loop_continue_test {} {
1104     gdb_test_no_output "set \$a = 0" "initialize \$a"
1105     gdb_test_no_output "set \$total = 0" "initialize \$total"
1106
1107     gdb_test \
1108         [multi_line_input \
1109              "while \$a < 5" \
1110              "  set \$a = \$a + 1" \
1111              "  set \$b = 0" \
1112              "  if \$a == 4" \
1113              "    loop_continue" \
1114              "  end" \
1115              "  while \$b < 5" \
1116              "    set \$b = \$b + 1" \
1117              "    if \$b == 2" \
1118              "      loop_continue" \
1119              "    end" \
1120              "    set \$total = \$total + 1" \
1121              "  end" \
1122              "end"] \
1123         "" \
1124         "run while loop"
1125
1126     gdb_test "print \$a" " = 5" "validate \$a"
1127     gdb_test "print \$b" " = 5" "validate \$b"
1128     gdb_test "print \$total" " = 16" "validate \$total"
1129 }
1130
1131 # Test an input line split with a continuation character (backslash)
1132 # while entering a multi-line command (in a secondary prompt).
1133
1134 proc_with_prefix backslash_in_multi_line_command_test {} {
1135     set dg_ver [dejagnu_version]
1136     set dg_major [lindex $dg_ver 0]
1137     set dg_minor [lindex $dg_ver 1]
1138
1139     # With older versions of DejaGnu, the "\\\n" we send gets replaced with a
1140     # space, thus breaking the test.  Just skip it in that case.
1141     if { $dg_major == 1 && $dg_minor < 5 } {
1142         untested "dejagnu version is too old"
1143         return
1144     }
1145
1146     gdb_breakpoint "main"
1147
1148     gdb_test_multiple "commands" "commands" {
1149         -re "End with a line saying just \"end\"\\.\r\n>$" {
1150             pass "commands"
1151         }
1152     }
1153
1154     set test "input line split with backslash"
1155     send_gdb "print \\\nargc\n"
1156     gdb_test_multiple "" $test {
1157         -re "^print \\\\\r\nargc\r\n>$" {
1158             pass $test
1159         }
1160     }
1161
1162     gdb_test_no_output "end"
1163
1164     # Input any command, just to be sure the readline state is sane.
1165     # In PR 21218, this would trigger the infamous:
1166     # readline: readline_callback_read_char() called with no handler!
1167     gdb_test "print 1" "" "run command"
1168 }
1169
1170 gdbvar_simple_if_test
1171 gdbvar_simple_while_test
1172 gdbvar_complex_if_while_test
1173 progvar_simple_if_test
1174 progvar_simple_while_test
1175 progvar_complex_if_while_test
1176 if_while_breakpoint_command_test
1177 infrun_breakpoint_command_test
1178 breakpoint_command_test
1179 breakpoint_clear_command_test
1180 user_defined_command_test
1181 user_defined_command_case_sensitivity
1182 user_defined_command_args_eval
1183 user_defined_command_args_stack_test
1184 user_defined_command_manyargs_test
1185 watchpoint_command_test
1186 test_command_prompt_position
1187 deprecated_command_test
1188 bp_deleted_in_command_test
1189 temporary_breakpoint_commands
1190 stray_arg0_test
1191 source_file_with_indented_comment
1192 recursive_source_test
1193 if_commands_test
1194 error_clears_commands_left
1195 redefine_hook_test
1196 backslash_in_multi_line_command_test
1197 define_if_without_arg_test
1198 loop_break_test
1199 loop_continue_test
1200 # This one should come last, as it redefines "backtrace".
1201 redefine_backtrace_test