259b89b803d187079daa2d433cb92d6837f95dc4
[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 a simple user defined command (with arguments)
285 proc_with_prefix user_defined_command_test {} {
286     global valnum_re
287
288     gdb_test_no_output "set \$foo = 4" "set foo"
289
290     gdb_test_multiple "define mycommand" "define mycommand" {
291         -re "End with"  {
292             pass "define mycommand"
293         }
294     }
295
296     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
297     gdb_test \
298         [multi_line_input \
299              {while $arg0 > 0} \
300              {  set $arg0 -= 1} \
301              {  if ($arg0 % 2) == 1} \
302              {    p/x 0xdeadbeef} \
303              {  else} \
304              {    p/x 0xfeedface} \
305              {  end} \
306              {end} \
307              {end}] \
308         "" \
309         "enter commands"
310
311     global decimal
312     set valnum_re "\\\$$decimal"
313
314     gdb_test \
315         {mycommand $foo} \
316         [multi_line \
317              "$valnum_re = 0xdeadbeef" \
318              "$valnum_re = 0xfeedface" \
319              "$valnum_re = 0xdeadbeef" \
320              "$valnum_re = 0xfeedface"] \
321         "execute user-defined command"
322    gdb_test "show user mycommand" \
323         "  while \\\$arg0.*set.*    if \\\(\\\$arg0.*p/x.*    else\[^\n\].*p/x.*    end\[^\n\].*  end\[^\n\].*" \
324            "display user command"
325
326     # Create and test a user-defined command with an empty body.
327     gdb_test_multiple "define myemptycommand" "define myemptycommand" {
328         -re "End with"  {
329             pass "define myemptycommand"
330         }
331     }
332     gdb_test "end" \
333         "" \
334         "end definition of user-defined command with empty body"
335
336     gdb_test_no_output "myemptycommand" \
337         "execute user-defined empty command"
338
339     gdb_test "show user" \
340         "User command \"myemptycommand.*" \
341         "display empty command in command list"
342
343     gdb_test "show user myemptycommand" \
344         "User command \"myemptycommand.*" \
345         "display user-defined empty command"
346 }
347
348 # Test that the case with which the command was defined is preserved.
349
350 proc_with_prefix user_defined_command_case_sensitivity {} {
351     # Define a first command with mixed case name.
352     set test "define Homer-Simpson"
353     gdb_test_multiple $test $test {
354             -re "End with"  {
355                 pass $test
356             }
357         }
358
359     gdb_test "print 123\nend" "" "enter commands 1"
360
361     # Define a second command, same name but different case.
362     set test "define HomeR-SimpsoN"
363     gdb_test_multiple $test $test {
364             -re "End with"  {
365                 pass $test
366             }
367         }
368
369     gdb_test "print 456\nend" "" "enter commands 2"
370
371     gdb_test "Homer-Simpson" " = 123" "execute command"
372     gdb_test "HomeR-SimpsoN" " = 456" "execute command"
373     gdb_test "HOMER-SIMPSON" "Undefined command.*" "try to call in upper case"
374     gdb_test "homer-simpson" "Undefined command.*" "try to call in lower case"
375 }
376
377 # Test that "eval" in a user-defined command expands $argc/$argN.
378
379 proc_with_prefix user_defined_command_args_eval {} {
380     gdb_test_multiple "define command_args_eval" \
381         "define command_args_eval" {
382             -re "End with"  {
383                 pass "define"
384             }
385         }
386
387     # Make a command that constructs references to $argc and $argN via
388     # eval.
389     gdb_test \
390         [multi_line \
391              {eval "printf \"argc = %%d,\", $arg%c", 'c'} \
392              {set $i = 0} \
393              {while $i < $argc} \
394              {  eval "printf \" %%d\", $arg%d", $i} \
395              {  set $i = $i + 1} \
396              {end} \
397              {printf "\n"} \
398              {end}] \
399         "" \
400         "enter commands"
401
402     gdb_test "command_args_eval 1 2 3" "argc = 3, 1 2 3" "execute command"
403 }
404
405 # Test that the $argc/$argN variables are pushed on/popped from the
406 # args stack correctly when a user-defined command calls another
407 # user-defined command (or in this case, recurses).
408
409 proc_with_prefix user_defined_command_args_stack_test {} {
410     gdb_test_multiple "define args_stack_command" \
411         "define args_stack_command" {
412             -re "End with"  {
413                 pass "define"
414             }
415         }
416
417     # Make a command that refers to $argc/$argN before and after
418     # recursing.  Also, vary the number of arguments passed to each
419     # recursion point.
420     gdb_test \
421         [multi_line \
422              {printf "before, argc = %d,", $argc} \
423              {set $i = 0} \
424              {while $i < $argc} \
425              {  eval "printf \" %%d\", $arg%d", $i} \
426              {  set $i = $i + 1} \
427              {end} \
428              {printf "\n"} \
429              {} \
430              {} \
431              {if $argc == 3} \
432              {  args_stack_command 21 22} \
433              {end} \
434              {if $argc == 2} \
435              {  args_stack_command 11} \
436              {end} \
437              {} \
438              {} \
439              {printf "after, argc = %d,", $argc} \
440              {set $i = 0} \
441              {while $i < $argc} \
442              {  eval "printf \" %%d\", $arg%d", $i} \
443              {  set $i = $i + 1} \
444              {end} \
445              {printf "\n"} \
446              {end}] \
447         "" \
448         "enter commands"
449
450     set expected \
451         [multi_line \
452              "before, argc = 3, 31 32 33" \
453              "before, argc = 2, 21 22" \
454              "before, argc = 1, 11" \
455              "after, argc = 1, 11" \
456              "after, argc = 2, 21 22" \
457              "after, argc = 3, 31 32 33"]
458     gdb_test "args_stack_command 31 32 33" $expected "execute command"
459 }
460
461 # Test a simple user defined command with many arguments.  GDB <= 7.12
462 # used to have a hard coded limit of 10 arguments.
463
464 proc_with_prefix user_defined_command_manyargs_test {} {
465     set test "define command"
466     gdb_test_multiple "define manyargs" $test {
467         -re "End with"  {
468             pass $test
469         }
470     }
471
472     # Define a function that doubles its arguments.
473     gdb_test \
474         [multi_line \
475              {printf "nargs=%d:", $argc} \
476              {set $i = 0} \
477              {while $i < $argc} \
478              {  eval "printf \" %%d\", 2 * $arg%d\n", $i} \
479              {  set $i = $i + 1} \
480              {end} \
481              {printf "\n"} \
482              {end}] \
483         "" \
484         "enter commands"
485
486     # Some random number of arguments, as long as higher than 10.
487     set nargs 100
488
489     set cmd "manyargs"
490     for {set i 1} {$i <= $nargs} {incr i} {
491         append cmd " $i"
492     }
493
494     set expected "nargs=$nargs:"
495     for {set i 1} {$i <= $nargs} {incr i} {
496         append expected " " [expr 2 * $i]
497     }
498
499     gdb_test $cmd $expected "execute command"
500 }
501
502 proc_with_prefix watchpoint_command_test {} {
503     global gdb_prompt
504
505     # Disable hardware watchpoints if necessary.
506     if [target_info exists gdb,no_hardware_watchpoints] {
507         gdb_test_no_output "set can-use-hw-watchpoints 0" ""
508     }
509
510     runto_or_return factorial
511
512     delete_breakpoints
513
514     # Verify that we can create a watchpoint, and give it a commands
515     # list that continues the inferior.  We set the watchpoint on a
516     # local variable, too, so that it self-deletes when the watched
517     # data goes out of scope.
518     #
519     # What should happen is: Each time the watchpoint triggers, it
520     # continues the inferior.  Eventually, the watchpoint will self-
521     # delete, when the watched variable is out of scope.  But by that
522     # time, the inferior should have exited.  GDB shouldn't crash or
523     # anything untoward as a result of this.
524     #
525     set wp_id -1
526
527     gdb_test_multiple "watch local_var" "watch local_var" {
528         -re "\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
529             set wp_id $expect_out(1,string)
530             pass "watch local_var"
531         }
532     }
533
534     if {$wp_id == -1} {return}
535
536     gdb_test_multiple "commands $wp_id" "begin commands on watch" {
537         -re "Type commands for breakpoint.*, one per line.*>$" {
538             pass "begin commands on watch"
539         }
540     }
541     # See the 'No symbol "value...' fail below.  This command will
542     # fail if it's executed in the wrong frame.  If adjusting the
543     # test, make sure this property holds.
544     gdb_test_multiple "print value" "add print command to watch" {
545         -re ">$" {
546             pass "add print command to watch"
547         }
548     }
549     gdb_test_multiple "continue" "add continue command to watch" {
550         -re ">$" {
551             pass "add continue command to watch"
552         }
553     }
554     gdb_test "end" \
555         "" \
556         "end commands on watch"
557
558     set test "continue with watch"
559     set lno_1 [gdb_get_line_number "commands.exp: hw local_var out of scope" "run.c"]
560     set lno_2 [gdb_get_line_number "commands.exp: local_var out of scope"    "run.c"]
561     gdb_test_multiple "continue" "$test" {
562         -re "No symbol \"value\" in current context.\r\n$gdb_prompt $" {
563             # Happens if GDB actually runs the watchpoints commands,
564             # even though the watchpoint was deleted for not being in
565             # scope.
566             fail $test
567         }
568         -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 $" {
569             pass $test
570         }
571    }
572 }
573
574 proc_with_prefix test_command_prompt_position {} {
575     global gdb_prompt
576     global valnum_re
577
578     runto_or_return factorial
579
580     # Don't depend upon argument passing, since most simulators don't
581     # currently support it.  Bash value variable to be what we want.
582     delete_breakpoints
583     gdb_test "break factorial" "Breakpoint.*at.*"
584     gdb_test "p value=5" ".*" "set value to 5"
585     # All this test should do is print 0xdeadbeef once.
586     gdb_test \
587         [multi_line_input \
588              {if value == 1} \
589              {  p/x 0xfeedface} \
590              {else} \
591              {  p/x 0xdeadbeef} \
592              {end}] \
593         "$valnum_re = 0xdeadbeef" \
594         "if test"
595
596     # Now let's test for the correct position of the '>' in gdb's
597     # prompt for commands.  It should be at the beginning of the line,
598     # and not after one space.
599
600     set test "> OK"
601     gdb_test_multiple "commands" $test {
602         -re "Type commands.*End with.*\[\r\n\]>$" {
603             gdb_test_multiple "printf \"Now the value is %d\\n\", value" $test {
604                 -re "^printf.*value\r\n>$" {
605                     gdb_test_multiple "end" $test {
606                         -re "^end\r\n$gdb_prompt $" { 
607                             pass $test
608                         }
609                     }
610                 }
611             }
612         }
613     }
614 }
615
616
617
618 proc_with_prefix deprecated_command_test {} {
619     gdb_test "maintenance deprecate blah" "Can't find command.*" \
620           "tried to deprecate non-existing command"
621
622     gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /1/"
623     gdb_test "p 5" \
624             "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
625             "p deprecated warning, with replacement"
626     gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /1/"
627
628     gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /2/"
629     gdb_test_no_output "maintenance deprecate print \"new_print\"" 
630     gdb_test "p 5" \
631             "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
632             "both alias and command are deprecated"
633     gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /2/"
634
635     gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
636             "deprecate long command /1/"
637     gdb_test "set remote memory-read-packet-size" \
638             "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
639             "long command deprecated /1/"
640
641     gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size" \
642             "deprecate long command /2/"
643     gdb_test "set remote memory-read-packet-size" \
644             "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
645             "long command deprecated with no alternative /2/"
646
647     gdb_test "maintenance deprecate" \
648             "\"maintenance deprecate\".*" \
649             "deprecate with no arguments"
650 }
651
652 proc_with_prefix bp_deleted_in_command_test {} {
653     global gdb_prompt
654
655     delete_breakpoints
656
657     # Create a breakpoint, and associate a command-list to it, with
658     # one command that deletes this breakpoint.
659     gdb_test "break factorial" \
660              "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\."
661     
662     gdb_test_multiple "commands" "begin commands" {
663       -re "Type commands for breakpoint.*>$" {
664           pass "begin commands"
665       }
666     }
667     gdb_test_multiple "silent" "add silent command" {
668         -re ">$" {
669             pass "add silent command"
670         }
671     }
672     gdb_test_multiple "clear factorial" "add clear command" {
673         -re ">$" {
674             pass "add clear command"
675         }
676     }
677     gdb_test_multiple "printf \"factorial command-list executed\\n\"" \
678         "add printf command" {
679         -re ">$" {
680             pass "add printf command"
681         }
682     }
683     gdb_test_multiple "cont" "add cont command" {
684         -re ">$" {
685             pass "add cont command"
686         }
687     }
688     gdb_test "end" \
689         "" \
690         "end commands"
691
692     gdb_run_cmd
693     gdb_test "" "factorial command-list executed.*" "run factorial until breakpoint"
694 }
695
696 proc_with_prefix temporary_breakpoint_commands {} {
697     delete_breakpoints
698
699     # Create a temporary breakpoint, and associate a commands list to it.
700     # This test will verify that this commands list is executed when the
701     # breakpoint is hit.
702     gdb_test "tbreak factorial" \
703             "Temporary breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\." \
704             "breakpoint"
705
706     gdb_test_multiple "commands" \
707         "begin commands in bp_deleted_in_command_test" {
708             -re "Type commands for breakpoint.*>$" {
709                 pass "begin commands"
710             }
711         }
712     gdb_test_multiple "silent" "add silent tbreak command" {
713         -re ">$" {
714             pass "add silent tbreak command"
715         }
716     }
717     gdb_test_multiple "printf \"factorial tbreak commands executed\\n\"" \
718         "add printf tbreak command" {
719             -re ">$" {
720                 pass "add printf tbreak command"
721             }
722         }
723     gdb_test_multiple "cont" "add cont tbreak command" {
724         -re ">$" {
725             pass "add cont tbreak command"
726         }
727     }
728     gdb_test "end" \
729         "" \
730         "end tbreak commands"
731
732     gdb_run_cmd
733     gdb_test "" "factorial tbreak commands executed.*" \
734         "run factorial until temporary breakpoint"
735 }
736
737 # Test that GDB can handle $arg0 outside of user functions without
738 # crashing.
739 proc_with_prefix stray_arg0_test { } {
740     global valnum_re
741
742     gdb_test "print \$arg0" \
743         "$valnum_re = void" \
744         "#1"
745
746     gdb_test "if 1 == 1\nprint \$arg0\nend" \
747         "$valnum_re = void" \
748         "#2"
749
750     gdb_test "print \$arg0 = 1" \
751         "$valnum_re = 1" \
752         "#3"
753
754     gdb_test "print \$arg0" \
755         "$valnum_re = 1" \
756         "#4"
757 }
758
759 # Test that GDB is able to source a file with an indented comment.
760 proc_with_prefix source_file_with_indented_comment {} {
761     set file1 [standard_output_file file1]
762
763     set fd [open "$file1" w]
764     puts $fd \
765 {define my_fun
766     #indented comment
767 end
768 echo Done!\n}
769     close $fd
770
771     gdb_test "source $file1" "Done!" "source file"
772 }
773
774 # Test that GDB can handle arguments when sourcing files recursively.
775 # If the arguments are overwritten with ####### then the test has failed.
776 proc_with_prefix recursive_source_test {} {
777     set file1 [standard_output_file file1]
778     set file2 [standard_output_file file2]
779     set file3 [standard_output_file file3]
780
781     set fd [open "$file1" w]
782     puts $fd \
783 "source $file2
784 abcdef qwerty"
785     close $fd
786
787     set fd [open "$file2" w]
788     puts $fd \
789 "define abcdef
790   echo 1: <<<\$arg0>>>\\n
791   source $file3
792   echo 2: <<<\$arg0>>>\\n
793 end"
794     close $fd
795
796     set fd [open "$file3" w]
797     puts $fd \
798 "echo in file3\\n
799 #################################################################"
800     close $fd
801
802     gdb_test "source $file1" \
803         "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
804         "source file"
805
806     file delete $file1
807     file delete $file2
808     file delete $file3
809 }
810
811 proc gdb_test_no_prompt { command result msg } {
812     set msg "$command - $msg"
813     set result "^[string_to_regexp $command]\r\n$result$"
814     gdb_test_multiple $command $msg {
815         -re "$result" {
816             pass $msg
817             return 1
818         }
819         -re "\r\n *>$" {
820             fail $msg
821             return 0
822         }
823     }
824     return 0
825 }
826
827 proc_with_prefix if_commands_test {} {
828     global gdb_prompt
829
830     gdb_test_no_output "set \$tem = 1" "set \$tem"
831
832     set test "if_commands_test 1"
833     gdb_test_no_prompt "if \$tem == 2" { >} $test
834     gdb_test_no_prompt "break main" { >} $test
835     gdb_test_no_prompt "else" { >} $test
836     gdb_test_no_prompt "break factorial" { >} $test
837     gdb_test_no_prompt "commands" {  >} $test
838     gdb_test_no_prompt "silent" {  >} $test
839     gdb_test_no_prompt "set \$tem = 3" {  >} $test
840     gdb_test_no_prompt "continue" {  >} $test
841     gdb_test_multiple "end" "first end - $test" {
842         -re " >\$" {
843             pass "first end - $test"
844         }
845         -re "\r\n>\$" {
846             fail "first end - $test"
847         }
848     }
849     gdb_test_multiple "end" "second end - $test" {
850         -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
851             pass "second end - $test"
852         }
853         -re "Undefined command: \"silent\".*$gdb_prompt $" {
854             fail "second end - $test"
855         }
856     }
857
858     set test "if_commands_test 2"
859     gdb_test_no_prompt "if \$tem == 1" { >} $test
860     gdb_test_no_prompt "break main" { >} $test
861     gdb_test_no_prompt "else" { >} $test
862     gdb_test_no_prompt "break factorial" { >} $test
863     gdb_test_no_prompt "commands" {  >} $test
864     gdb_test_no_prompt "silent" {  >} $test
865     gdb_test_no_prompt "set \$tem = 3" {  >} $test
866     gdb_test_no_prompt "continue" {  >} $test
867     gdb_test_multiple "end" "first end - $test" {
868         -re " >\$" {
869             pass "first end - $test"
870         }
871         -re "\r\n>\$" {
872             fail "first end - $test"
873         }
874     }
875     gdb_test_multiple "end" "second end - $test" {
876         -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
877             pass "second end - $test"
878         }
879     }
880 }
881
882 # Verify an error during "commands" commands execution will prevent any other
883 # "commands" from other breakpoints at the same location to be executed.
884
885 proc_with_prefix error_clears_commands_left {} {
886     set test "hook-stop 1"
887     gdb_test_multiple {define hook-stop} $test {
888         -re "End with a line saying just \"end\"\\.\r\n>$" {
889             pass $test
890         }
891     }
892     set test "hook-stop 1a"
893     gdb_test_multiple {echo hook-stop1\n} $test {
894         -re "\r\n>$" {
895             pass $test
896         }
897     }
898     gdb_test_no_output "end" "hook-stop 1b"
899
900     delete_breakpoints
901     gdb_breakpoint "main"
902
903     set test "main commands 1"
904     gdb_test_multiple {commands $bpnum} $test {
905         -re "End with a line saying just \"end\"\\.\r\n>$" {
906             pass $test
907         }
908     }
909     set test "main commands 1a"
910     gdb_test_multiple {echo cmd1\n} $test {
911         -re "\r\n>$" {
912             pass $test
913         }
914     }
915     set test "main commands 1b"
916     gdb_test_multiple {errorcommandxy\n} $test {
917         -re "\r\n>$" {
918             pass $test
919         }
920     }
921     gdb_test_no_output "end" "main commands 1c"
922
923     gdb_breakpoint "main"
924     set test "main commands 2"
925     gdb_test_multiple {commands $bpnum} $test {
926         -re "End with a line saying just \"end\"\\.\r\n>$" {
927             pass $test
928         }
929     }
930     set test "main commands 2a"
931     gdb_test_multiple {echo cmd2\n} $test {
932         -re "\r\n>$" {
933             pass $test
934         }
935     }
936     set test "main commands 2b"
937     gdb_test_multiple {errorcommandyz\n} $test {
938         -re "\r\n>$" {
939             pass $test
940         }
941     }
942     gdb_test_no_output "end" "main commands 2c"
943
944     gdb_run_cmd
945     gdb_test \
946         "" \
947         [multi_line \
948              "hook-stop1" \
949              ".*" \
950              "cmd1" \
951              "Undefined command: \"errorcommandxy\"\\.  Try \"help\"\\."] \
952         "cmd1 error"
953
954     gdb_test {echo idle\n} "\r\nidle" "no cmd2"
955 }
956
957 proc_with_prefix redefine_hook_test {} {
958     gdb_test \
959         [multi_line_input \
960              "define one"\
961              "end"] \
962         "" \
963         "define one"
964
965     gdb_test \
966         [multi_line_input \
967              "define hook-one" \
968              "echo hibob\\n" \
969              "end"] \
970         "" \
971         "define hook-one"
972
973     set test "redefine one"
974     gdb_test_multiple "define one" $test {
975         -re "Redefine command .one.. .y or n. $" {
976             send_gdb "y\n"
977             exp_continue
978         }
979
980         -re "End with"  {
981             pass $test
982         }
983     }
984
985     gdb_test "end" "" "enter commands for one redefinition"
986
987     gdb_test "one" "hibob" "execute one command"
988 }
989
990 proc_with_prefix redefine_backtrace_test {} {
991     gdb_test_multiple "define backtrace" "define backtrace" {
992         -re "Really redefine built-in command \"backtrace\"\\? \\(y or n\\) $"  {
993             pass "define backtrace"
994         }
995     }
996
997     gdb_test_multiple "y" "expect response to define backtrace" {
998         -re "End with a line saying just \"end\"\\.\r\n>$"  {
999             pass "expect response to define backtrace"
1000         }
1001     }
1002
1003     gdb_test \
1004         [multi_line_input \
1005              "echo hibob\\n" \
1006              "end"] \
1007         "" \
1008         "enter commands"
1009
1010     gdb_test "backtrace" "hibob" "execute backtrace command"
1011     gdb_test "bt" "hibob" "execute bt command"
1012 }
1013
1014 # Test using "if" and "while" without args when building a command list.
1015
1016 proc define_if_without_arg_test {} {
1017     foreach cmd {if while define} {
1018         set test "define some_command_$cmd"
1019         gdb_test_multiple $test $test {
1020             -re "End with"  {
1021                 pass $test
1022             }
1023         }
1024
1025         gdb_test "$cmd" "$cmd command requires an argument." "type $cmd without args"
1026     }
1027 }
1028
1029 # Test the loop_break command.
1030
1031 proc_with_prefix loop_break_test {} {
1032     gdb_test_no_output "set \$a = 0" "initialize \$a"
1033     gdb_test_no_output "set \$total = 0" "initialize \$total"
1034
1035     gdb_test \
1036         [multi_line_input \
1037              "while \$a < 5" \
1038              "  if \$a == 4" \
1039              "    loop_break" \
1040              "  end" \
1041              "  set \$b = 0" \
1042              "  while \$b < 5" \
1043              "    if \$b == 2" \
1044              "      loop_break" \
1045              "    end" \
1046              "    set \$total = \$total + 1" \
1047              "    set \$b = \$b + 1" \
1048              "  end" \
1049              "  set \$a = \$a + 1" \
1050              "end"] \
1051         "" \
1052         "run while loop"
1053
1054     gdb_test "print \$a" " = 4" "validate \$a"
1055     gdb_test "print \$b" " = 2" "validate \$b"
1056     gdb_test "print \$total" " = 8" "validate \$total"
1057 }
1058
1059 # Test the loop_continue command.
1060
1061 proc_with_prefix loop_continue_test {} {
1062     gdb_test_no_output "set \$a = 0" "initialize \$a"
1063     gdb_test_no_output "set \$total = 0" "initialize \$total"
1064
1065     gdb_test \
1066         [multi_line_input \
1067              "while \$a < 5" \
1068              "  set \$a = \$a + 1" \
1069              "  set \$b = 0" \
1070              "  if \$a == 4" \
1071              "    loop_continue" \
1072              "  end" \
1073              "  while \$b < 5" \
1074              "    set \$b = \$b + 1" \
1075              "    if \$b == 2" \
1076              "      loop_continue" \
1077              "    end" \
1078              "    set \$total = \$total + 1" \
1079              "  end" \
1080              "end"] \
1081         "" \
1082         "run while loop"
1083
1084     gdb_test "print \$a" " = 5" "validate \$a"
1085     gdb_test "print \$b" " = 5" "validate \$b"
1086     gdb_test "print \$total" " = 16" "validate \$total"
1087 }
1088
1089 # Test an input line split with a continuation character (backslash)
1090 # while entering a multi-line command (in a secondary prompt).
1091
1092 proc_with_prefix backslash_in_multi_line_command_test {} {
1093     set dg_ver [dejagnu_version]
1094     set dg_major [lindex $dg_ver 0]
1095     set dg_minor [lindex $dg_ver 1]
1096
1097     # With older versions of DejaGnu, the "\\\n" we send gets replaced with a
1098     # space, thus breaking the test.  Just skip it in that case.
1099     if { $dg_major == 1 && $dg_minor < 5 } {
1100         untested "dejagnu version is too old"
1101         return
1102     }
1103
1104     gdb_breakpoint "main"
1105
1106     gdb_test_multiple "commands" "commands" {
1107         -re "End with a line saying just \"end\"\\.\r\n>$" {
1108             pass "commands"
1109         }
1110     }
1111
1112     set test "input line split with backslash"
1113     send_gdb "print \\\nargc\n"
1114     gdb_test_multiple "" $test {
1115         -re "^print \\\\\r\nargc\r\n>$" {
1116             pass $test
1117         }
1118     }
1119
1120     gdb_test_no_output "end"
1121
1122     # Input any command, just to be sure the readline state is sane.
1123     # In PR 21218, this would trigger the infamous:
1124     # readline: readline_callback_read_char() called with no handler!
1125     gdb_test "print 1" "" "run command"
1126 }
1127
1128 gdbvar_simple_if_test
1129 gdbvar_simple_while_test
1130 gdbvar_complex_if_while_test
1131 progvar_simple_if_test
1132 progvar_simple_while_test
1133 progvar_complex_if_while_test
1134 if_while_breakpoint_command_test
1135 infrun_breakpoint_command_test
1136 breakpoint_command_test
1137 user_defined_command_test
1138 user_defined_command_case_sensitivity
1139 user_defined_command_args_eval
1140 user_defined_command_args_stack_test
1141 user_defined_command_manyargs_test
1142 watchpoint_command_test
1143 test_command_prompt_position
1144 deprecated_command_test
1145 bp_deleted_in_command_test
1146 temporary_breakpoint_commands
1147 stray_arg0_test
1148 source_file_with_indented_comment
1149 recursive_source_test
1150 if_commands_test
1151 error_clears_commands_left
1152 redefine_hook_test
1153 backslash_in_multi_line_command_test
1154 define_if_without_arg_test
1155 loop_break_test
1156 loop_continue_test
1157 # This one should come last, as it redefines "backtrace".
1158 redefine_backtrace_test