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