OSDN Git Service

Test indented comment in file being sourced.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / testsuite / gdb.base / commands.exp
1 #   Copyright 1988, 1990, 1991, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
2 #   2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010
3 #   Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 if $tracelevel then {
19     strace $tracelevel
20 }
21
22 #
23 # test special commands (if, while, etc)
24 #
25 set prms_id 0
26 set bug_id 0
27
28 if { [prepare_for_testing commands.exp commands run.c {debug additional_flags=-DFAKEARGV}] } {
29     return -1
30 }
31
32 proc gdbvar_simple_if_test {} {
33     global gdb_prompt
34
35     gdb_test "set \$foo = 0" "" "set foo in gdbvar_simple_if_test"
36     # All this test should do is print 0xdeadbeef once.
37     gdb_test "if \$foo == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
38             "\\\$\[0-9\]* = 0xdeadbeef" "gdbvar_simple_if_test #1"
39     # All this test should do is print 0xfeedface once.
40     gdb_test "if \$foo == 0\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
41             "\\\$\[0-9\]* = 0xfeedface" "gdbvar_simple_if_test #2"
42 }
43
44 proc gdbvar_simple_while_test {} {
45     global gdb_prompt
46
47     gdb_test "set \$foo = 5" "" "set foo in gdbvar_simple_while_test"
48     # This test should print 0xfeedface five times.
49     gdb_test "while \$foo > 0\np/x 0xfeedface\nset \$foo -= 1\nend" \
50             "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
51             "gdbvar_simple_while_test #1"
52 }
53
54 proc gdbvar_complex_if_while_test {} {
55     global gdb_prompt
56
57     gdb_test "set \$foo = 4" "" "set foo in gdbvar complex_if_while_test"
58     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
59     gdb_test "while \$foo > 0\nset \$foo -= 1\nif \(\$foo % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
60             "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
61             "gdbvar_complex_if_while_test #1"
62 }
63
64 proc progvar_simple_if_test {} {
65     global gdb_prompt
66
67     if [target_info exists noargs] { 
68         verbose "Skipping progvar_simple_if_test because of noargs."
69         return
70     }
71
72     if { ![runto factorial] } then { gdb_suppress_tests; }
73     # Don't depend upon argument passing, since most simulators don't
74     # currently support it.  Bash value variable to be what we want.
75     gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test #1"
76     # All this test should do is print 0xdeadbeef once.
77     gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
78             "\\\$\[0-9\]* = 0xdeadbeef" \
79             "progvar_simple_if_test #1"
80     # All this test should do is print 0xfeedface once.
81     gdb_test "if value == 5\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
82             "\\\$\[0-9\]* = 0xfeedface" \
83             "progvar_simple_if_test #2"
84     gdb_stop_suppressing_tests;
85 }
86
87 proc progvar_simple_while_test {} {
88     global gdb_prompt
89
90     if [target_info exists noargs] { 
91         verbose "Skipping progvar_simple_while_test because of noargs."
92         return
93     }
94
95     gdb_test "set args 5" "" "set args in progvar_simple_while_test"
96     if { ![runto factorial] } then { gdb_suppress_tests }
97     # Don't depend upon argument passing, since most simulators don't
98     # currently support it.  Bash value variable to be what we want.
99     gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test #2"
100     # This test should print 0xfeedface five times.
101     gdb_test "while value > 0\np/x 0xfeedface\nset value -= 1\nend" \
102             "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
103             "progvar_simple_while_test #1"
104     gdb_stop_suppressing_tests;
105 }
106
107 proc progvar_complex_if_while_test {} {
108     global gdb_prompt
109
110     if [target_info exists noargs] { 
111         verbose "Skipping progvar_simple_if_while_test because of noargs."
112         return
113     }
114
115     gdb_test "set args 4" "" "set args in progvar_complex_if_while_test"
116     if { ![runto factorial] } then { gdb_suppress_tests }
117     # Don't depend upon argument passing, since most simulators don't
118     # currently support it.  Bash value variable to be what we want.
119     gdb_test "p value=4" "" "set value to 4 in progvar_simple_if_test"
120     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
121     gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
122             "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
123             "progvar_complex_if_while_test #1"
124     gdb_stop_suppressing_tests;
125 }
126
127 proc if_while_breakpoint_command_test {} {
128     if [target_info exists noargs] { 
129         verbose "Skipping if_while_breakpoint_command_test because of noargs."
130         return
131     }
132
133     gdb_test "set args 5" "" "set args in if_while_breakpoint_command_test"
134     if { ![runto factorial] } then { gdb_suppress_tests }
135     # Don't depend upon argument passing, since most simulators don't
136     # currently support it.  Bash value variable to be what we want.
137     gdb_test "p value=5" "" "set value to 5 in progvar_simple_if_test"
138     delete_breakpoints
139     gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #1"
140
141     send_gdb "commands\n"
142     gdb_expect {
143         -re "End with" {
144             pass "commands in if_while_breakpoint_command_test"
145         }
146         default {
147             fail "(timeout or eof) commands in if_while_breakpoint_command_test"
148         }
149     }
150     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
151     gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
152             "" \
153             "commands part 2 in if_while_breakpoint_command_test"
154     gdb_test "continue" \
155             "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
156             "if_while_breakpoint_command_test #1"
157    gdb_test "info break" \
158            "while.*set.*if.*p/x.*else.*p/x.*end.*" \
159            "info break in if_while_breakpoint_command_test"
160     gdb_stop_suppressing_tests;
161 }
162
163 # Test that we can run the inferior from breakpoint commands.
164 #
165 # The expected behavior is that all commands after the first "step"
166 # shall be ignored.  See the gdb manual, "Break Commands",
167 # subsection "Breakpoint command lists".
168
169 proc infrun_breakpoint_command_test {} {
170     if [target_info exists noargs] { 
171         verbose "Skipping infrun_breakpoint_command_test because of noargs."
172         return
173     }
174
175     gdb_test "set args 6" "" "set args in infrun_breakpoint_command_test"
176     if { ![runto factorial] } then { gdb_suppress_tests }
177     # Don't depend upon argument passing, since most simulators don't
178     # currently support it.  Bash value variable to be what we want.
179     gdb_test "p value=6" "" "set value to 6 in progvar_simple_if_test #1"
180     delete_breakpoints
181     gdb_test "break factorial if value == 5" "Breakpoint.*at.*"
182
183 # infrun_breakpoint_command_test - This test was broken into two parts 
184 # to get around a synchronization problem in expect.
185 # part1: issue the gdb command "commands"
186 # part2: send the list of commands
187     send_gdb "commands\n"
188     gdb_expect {
189         -re "End with" {
190             pass "commands in infrun_breakpoint_command_test #1"
191         }
192         default {
193             fail "(timeout or eof) commands in infrun_breakpoint_command_test"
194         }
195     }
196     gdb_test "step\nstep\nstep\nstep\nend" "" \
197         "commands in infrun_breakpoint_command_test #2"
198
199         gdb_test "continue" \
200                 "Continuing.*.*.*Breakpoint \[0-9\]*, factorial \\(value=5\\).*at.*\[0-9\]*\[      \]*if \\(value > 1\\) \{.*\[0-9\]*\[      \]*value \\*= factorial \\(value - 1\\);.*" \
201                 "continue in infrun_breakpoint_command_test"
202
203     gdb_stop_suppressing_tests;
204 }
205
206 proc breakpoint_command_test {} {
207     if [target_info exists noargs] { 
208         verbose "Skipping breakpoint_command_test because of noargs."
209         return
210     }
211
212     gdb_test "set args 6" "" "set args in breakpoint_command_test"
213     if { ![runto factorial] } then { gdb_suppress_tests; }
214     # Don't depend upon argument passing, since most simulators don't
215     # currently support it.  Bash value variable to be what we want.
216     gdb_test "p value=6" "" "set value to 6 in progvar_simple_if_test #2"
217     delete_breakpoints
218     gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #2"
219     gdb_test "commands\nprintf \"Now the value is %d\\n\", value\nend" \
220         "End with.*" "commands in breakpoint_command_test"
221     gdb_test "continue" \
222             "Breakpoint \[0-9\]*, factorial.*Now the value is 5" \
223         "continue in breakpoint_command_test"
224     gdb_test "print value" " = 5" "print value in breakpoint_command_test"
225     gdb_stop_suppressing_tests;
226 }
227
228 # Test a simple user defined command (with arguments)
229 proc user_defined_command_test {} {
230     global gdb_prompt
231
232     gdb_test "set \$foo = 4" "" "set foo in user_defined_command_test"
233
234     send_gdb "define mycommand\n"
235     gdb_expect {
236         -re "End with"  {
237             pass "define mycommand in user_defined_command_test"
238         }
239         default {
240             fail "(timeout or eof) define mycommand in user_defined_command_test"
241         }
242     }
243     # This test should alternate between 0xdeadbeef and 0xfeedface two times.
244     gdb_test "while \$arg0 > 0\nset \$arg0 -= 1\nif \(\$arg0 % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
245             "" \
246             "enter commands in user_defined_command_test"
247
248     gdb_test "mycommand \$foo" \
249             "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
250             "execute user defined command in user_defined_command_test"
251    gdb_test "show user mycommand" \
252         "  while \\\$arg0.*set.*    if \\\(\\\$arg0.*p/x.*    else\[^\n\].*p/x.*    end\[^\n\].*  end\[^\n\].*" \
253            "display user command in user_defined_command_test"
254 }
255
256 proc watchpoint_command_test {} {
257     global noargs
258     global gdb_prompt
259
260     if [target_info exists noargs] { 
261         verbose "Skipping watchpoint_command_test because of noargs."
262         return
263     }
264
265     # Disable hardware watchpoints if necessary.
266     if [target_info exists gdb,no_hardware_watchpoints] {
267         gdb_test "set can-use-hw-watchpoints 0" "" ""
268     }
269
270     gdb_test "set args 6" "" "set args in watchpoint_command_test"
271     if { ![runto factorial] } then { return }
272     delete_breakpoints
273
274     # Verify that we can create a watchpoint, and give it a commands
275     # list that continues the inferior.  We set the watchpoint on a
276     # local variable, too, so that it self-deletes when the watched
277     # data goes out of scope.
278     #
279     # What should happen is: Each time the watchpoint triggers, it
280     # continues the inferior.  Eventually, the watchpoint will self-
281     # delete, when the watched variable is out of scope.  But by that
282     # time, the inferior should have exited.  GDB shouldn't crash or
283     # anything untoward as a result of this.
284     #
285     set wp_id -1
286
287     send_gdb "watch local_var\n"
288     gdb_expect {
289         -re ".*\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
290             set wp_id $expect_out(1,string)
291             pass "watch local_var"
292         }
293         -re "$gdb_prompt $"\
294             {fail "watch local_var"}
295         timeout {fail "(timeout) watch local_var"}
296     }
297
298     if {$wp_id == -1} {return}
299
300     send_gdb "commands $wp_id\n"
301     gdb_expect {
302       -re "Type commands for when breakpoint $wp_id is hit, one per line.*>" {
303           pass "begin commands on watch"
304       }
305       -re "$gdb_prompt $" {fail "begin commands on watch"}
306       timeout             {fail "(timeout) begin commands on watch"}
307     }
308     send_gdb "print value\n"
309     gdb_expect {
310         -re ">"               {pass "add print command to watch"}
311         -re "$gdb_prompt $"   {fail "add print command to watch"}
312         timeout               {fail "(timeout) add print command to watch"}
313     }
314     send_gdb "continue\n"
315     gdb_expect {
316         -re ">"               {pass "add continue command to watch"}
317         -re "$gdb_prompt $"   {fail "add continue command to watch"}
318         timeout               {fail "(timeout) add continue command to watch"}
319     }
320     send_gdb "end\n"
321     gdb_expect {
322         -re "$gdb_prompt $"   {pass "end commands on watch"}
323         timeout               {fail "(timeout) end commands on watch"}
324     }
325     send_gdb "continue\n"
326     gdb_expect {
327         -re "Continuing.*\[Ww\]atchpoint $wp_id deleted because the program has left the block in.*which its expression is valid.*run.c:(57|82).*$gdb_prompt $" {
328             pass "continue with watch"
329         }
330         -re "$gdb_prompt $"   {fail "continue with watch"}
331         timeout               {fail "(timeout) continue with watch"}
332     }
333 }
334
335 proc test_command_prompt_position {} {
336     global gdb_prompt
337
338     if [target_info exists noargs] { 
339         verbose "Skipping test_command_prompt_position because of noargs."
340         return
341     }
342
343     if { ![runto factorial] } then { gdb_suppress_tests; }
344     # Don't depend upon argument passing, since most simulators don't
345     # currently support it.  Bash value variable to be what we want.
346     delete_breakpoints
347     gdb_test "break factorial" "Breakpoint.*at.*" "break factorial #3"
348     gdb_test "p value=5" "" "set value to 5 in test_command_prompt_position"
349     # All this test should do is print 0xdeadbeef once.
350     gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
351             "\\\$\[0-9\]* = 0xdeadbeef" \
352             "if test in test_command_prompt_position"
353     
354     # Now let's test for the correct position of the '>' in gdb's
355     # prompt for commands.  It should be at the beginning of the line,
356     # and not after one space.
357
358     send_gdb "commands\n"
359     gdb_expect {
360         -re "Type commands.*End with.*\[\r\n\]>$" { 
361             send_gdb "printf \"Now the value is %d\\n\", value\n"
362             gdb_expect {
363                 -re "^printf.*value\r\n>$" {
364                     send_gdb "end\n"
365                     gdb_expect {
366                         -re "^end\r\n$gdb_prompt $" { 
367                             pass "> OK in test_command_prompt_position" 
368                         }
369                         -re ".*$gdb_prompt $" { 
370                             fail "some other message in test_command_prompt_position" 
371                         }
372                         timeout  { 
373                             fail "(timeout) 1 in test_command_prompt_position"
374                         }
375                     }
376                 }
377                 -re "^ >$" { fail "> not OK in test_command_prompt_position" }
378                 -re ".*$gdb_prompt $"   { 
379                     fail "wrong message in test_command_prompt_position" 
380                 }
381                 timeout    { 
382                     fail "(timeout) 2 in test_command_prompt_position " 
383                 }
384             }
385         }
386         -re "Type commands.*End with.*\[\r\n\] >$" { 
387             fail "prompt not OK in test_command_prompt_position" 
388         }
389         -re ".*$gdb_prompt $" { 
390             fail "commands in test_command_prompt_position" 
391         }
392         timeout { fail "(timeout) 3 commands in test_command_prompt_position" }
393     }
394
395     gdb_stop_suppressing_tests;
396 }
397
398
399
400 proc deprecated_command_test {} {
401     gdb_test "maintenance deprecate blah" "Can't find command.*" \
402           "tried to deprecate non-existing command"
403
404     gdb_test "maintenance deprecate p \"new_p\"" "" "maintenance deprecate p \"new_p\" /1/"
405     gdb_test "p 5" \
406             "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
407             "p deprecated warning, with replacement"
408     gdb_test "p 5" ".\[0-9\]* = 5.*" "Deprecated warning goes away /1/"
409
410     gdb_test "maintenance deprecate p \"new_p\"" "" "maintenance deprecate p \"new_p\" /2/"
411     gdb_test "maintenance deprecate print \"new_print\"" ""
412     gdb_test "p 5" \
413             "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
414             "both alias and command are deprecated"
415     gdb_test "p 5" ".\[0-9\]* = 5.*" "Deprecated warning goes away /2/"
416
417     gdb_test "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
418             "" \
419             "deprecate long command /1/"
420     gdb_test "set remote memory-read-packet-size" \
421             "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
422             "long command deprecated /1/"
423
424     gdb_test "maintenance deprecate set remote memory-read-packet-size" \
425             "" \
426             "deprecate long command /2/"
427     gdb_test "set remote memory-read-packet-size" \
428             "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
429             "long command deprecated with no alternative /2/"
430
431     gdb_test "maintenance deprecate" \
432             "\"maintenance deprecate\".*" \
433             "deprecate with no arguments"
434 }
435
436 proc bp_deleted_in_command_test {} {
437     global gdb_prompt
438     
439     if [target_info exists noargs] { 
440         verbose "Skipping bp_deleted_in_command_test because of noargs."
441         return
442     }
443
444     gdb_test "set args 1" "" "set args in bp_deleted_in_command_test"
445     delete_breakpoints
446
447     # Create a breakpoint, and associate a command-list to it, with
448     # one command that deletes this breakpoint.
449     gdb_test "break factorial" \
450              "Breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\." \
451              "breakpoint in bp_deleted_in_command_test"
452     
453     send_gdb "commands\n"
454     gdb_expect {
455       -re "Type commands for when breakpoint .* is hit, one per line.*>" {
456           pass "begin commands in bp_deleted_in_command_test"
457       }
458       -re "$gdb_prompt $" {fail "begin commands in bp_deleted_in_command_test"}
459       timeout             {fail "(timeout) begin commands bp_deleted_in_command_test"}
460     }
461     send_gdb "silent\n"
462     gdb_expect {
463         -re ">"               {pass "add silent command"}
464         -re "$gdb_prompt $"   {fail "add silent command"}
465         timeout               {fail "(timeout) add silent command"}
466     }
467     send_gdb "clear factorial\n"
468     gdb_expect {
469         -re ">"               {pass "add clear command"}
470         -re "$gdb_prompt $"   {fail "add clear command"}
471         timeout               {fail "(timeout) add clear command"} }
472     send_gdb "printf \"factorial command-list executed\\n\"\n"
473     gdb_expect {
474         -re ">"               {pass "add printf command"}
475         -re "$gdb_prompt $"   {fail "add printf command"}
476         timeout               {fail "(timeout) add printf command"}
477     }
478     send_gdb "cont\n"
479     gdb_expect {
480         -re ">"               {pass "add cont command"}
481         -re "$gdb_prompt $"   {fail "add cont command"}
482         timeout               {fail "(timeout) add cont command"} }
483     send_gdb "end\n"
484     gdb_expect {
485         -re "$gdb_prompt $"   {pass "end commands"}
486         timeout               {fail "(timeout) end commands"}
487     }
488
489     gdb_run_cmd
490     gdb_expect {
491         -re ".*factorial command-list executed.*1.*$gdb_prompt $" {
492             pass "run factorial until breakpoint"
493         }
494         -re ".*$gdb_prompt $" {
495             fail "run factorial until breakpoint"
496         }
497         default { fail "(timeout) run factorial until breakpoint" }
498         timeout { fail "(timeout) run factorial until breakpoint" }
499     }
500 }
501
502 proc temporary_breakpoint_commands {} {
503     global gdb_prompt
504     
505     if [target_info exists noargs] { 
506         verbose "Skipping temporary_breakpoint_commands because of noargs."
507         return
508     }
509
510     gdb_test "set args 1" "" "set args in temporary_breakpoint_commands"
511     delete_breakpoints
512
513     # Create a temporary breakpoint, and associate a commands list to it.
514     # This test will verify that this commands list is executed when the
515     # breakpoint is hit.
516     gdb_test "tbreak factorial" \
517             "Temporary breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\." \
518             "breakpoint in temporary_breakpoint_commands"
519     
520     send_gdb "commands\n"
521     gdb_expect {
522         -re "Type commands for when breakpoint .* is hit, one per line.*>" {
523             pass "begin commands in bp_deleted_in_command_test"
524         }
525         -re "$gdb_prompt $" {fail "begin commands in bp_deleted_in_command_test"}
526         timeout             {fail "(timeout) begin commands bp_deleted_in_command_test"}
527     }
528     send_gdb "silent\n"
529     gdb_expect {
530         -re ">"               {pass "add silent tbreak command"}
531         -re "$gdb_prompt $"   {fail "add silent tbreak command"}
532         timeout               {fail "(timeout) add silent tbreak command"}
533      }
534     send_gdb "printf \"factorial tbreak commands executed\\n\"\n"
535     gdb_expect {
536         -re ">"               {pass "add printf tbreak command"}
537         -re "$gdb_prompt $"   {fail "add printf tbreak command"}
538         timeout               {fail "(timeout) add printf tbreak command"}
539      }
540     send_gdb "cont\n"
541     gdb_expect {
542         -re ">"               {pass "add cont tbreak command"}
543         -re "$gdb_prompt $"   {fail "add cont tbreak command"}
544         timeout               {fail "(timeout) add cont tbreak command"} }
545     send_gdb "end\n"
546     gdb_expect {
547         -re "$gdb_prompt $"   {pass "end tbreak commands"}
548         timeout               {fail "(timeout) end tbreak commands"}
549      }
550
551     gdb_run_cmd
552     gdb_expect {
553         -re ".*factorial tbreak commands executed.*1.*$gdb_prompt $" {
554             pass "run factorial until temporary breakpoint"
555         }
556         timeout { fail "(timeout) run factorial until temporary breakpoint" }
557     }
558 }
559
560 # Test that GDB can handle $arg0 outside of user functions without
561 # crashing.
562 proc stray_arg0_test { } {
563     gdb_test "print \$arg0" \
564         "\\\$\[0-9\]* = void" \
565         "stray_arg0_test #1"
566
567     gdb_test "if 1 == 1\nprint \$arg0\nend" \
568         "\\\$\[0-9\]* = void" \
569         "stray_arg0_test #2"
570
571     gdb_test "print \$arg0 = 1" \
572         "\\\$\[0-9\]* = 1" \
573         "stray_arg0_test #3"
574
575     gdb_test "print \$arg0" \
576         "\\\$\[0-9\]* = 1" \
577         "stray_arg0_test #4"
578 }
579
580 # Test that GDB is able to source a file with an indented comment.
581 proc source_file_with_indented_comment {} {
582     set fd [open "file1" w]
583     puts $fd \
584 {define my_fun
585     #indented comment
586 end
587 echo Done!\n}
588     close $fd
589
590     gdb_test "source file1" "Done!" "source file with indented comment"
591 }
592
593 # Test that GDB can handle arguments when sourcing files recursively.
594 # If the arguments are overwritten with ####### then the test has failed.
595 proc recursive_source_test {} {
596     set fd [open "file1" w]
597     puts $fd \
598 {source file2
599 abcdef qwerty}
600     close $fd
601
602     set fd [open "file2" w]
603     puts $fd \
604 {define abcdef
605   echo 1: <<<$arg0>>>\n
606   source file3
607   echo 2: <<<$arg0>>>\n
608 end}
609     close $fd
610
611     set fd [open "file3" w]
612     puts $fd \
613 "echo in file3\\n
614 #################################################################"
615     close $fd
616
617     gdb_test "source file1" \
618         "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
619         "recursive source test"
620
621     file delete file1
622     file delete file2
623     file delete file3
624 }
625
626 proc gdb_test_no_prompt { command result msg } {
627     global gdb_prompt
628
629     set msg "$command - $msg"
630     set result "^[string_to_regexp $command]\r\n$result$"
631     gdb_test_multiple $command $msg {
632         -re "$result" {
633             pass $msg
634             return 1
635         }
636         -re "\r\n *>$" {
637             fail $msg
638             return 0
639         }
640     }
641     return 0
642 }
643
644 proc if_commands_test {} {
645     global gdb_prompt
646
647     gdb_test "set \$tem = 1" "" "set \$tem in if_commands_test"
648
649     set test "if_commands_test 1"
650     gdb_test_no_prompt "if \$tem == 2" { >} $test
651     gdb_test_no_prompt "break main" { >} $test
652     gdb_test_no_prompt "else" { >} $test
653     gdb_test_no_prompt "break factorial" { >} $test
654     gdb_test_no_prompt "commands" {  >} $test
655     gdb_test_no_prompt "silent" {  >} $test
656     gdb_test_no_prompt "set \$tem = 3" {  >} $test
657     gdb_test_no_prompt "continue" {  >} $test
658     gdb_test_multiple "end" "first end - $test" {
659         -re " >\$" {
660             pass "first end - $test"
661         }
662         -re "\r\n>\$" {
663             fail "first end - $test"
664         }
665     }
666     gdb_test_multiple "end" "second end - $test" {
667         -re "Breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
668             pass "second end - $test"
669         }
670         -re "Undefined command: \"silent\".*$gdb_prompt $" {
671             fail "second end - $test"
672         }
673     }
674
675     set test "if_commands_test 2"
676     gdb_test_no_prompt "if \$tem == 1" { >} $test
677     gdb_test_no_prompt "break main" { >} $test
678     gdb_test_no_prompt "else" { >} $test
679     gdb_test_no_prompt "break factorial" { >} $test
680     gdb_test_no_prompt "commands" {  >} $test
681     gdb_test_no_prompt "silent" {  >} $test
682     gdb_test_no_prompt "set \$tem = 3" {  >} $test
683     gdb_test_no_prompt "continue" {  >} $test
684     gdb_test_multiple "end" "first end - $test" {
685         -re " >\$" {
686             pass "first end - $test"
687         }
688         -re "\r\n>\$" {
689             fail "first end - $test"
690         }
691     }
692     gdb_test_multiple "end" "second end - $test" {
693         -re "Breakpoint \[0-9\]+ at .*: file .*/run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
694             pass "second end - $test"
695         }
696     }
697 }
698
699 proc redefine_hook_test {} {
700     global gdb_prompt
701
702     gdb_test "define one\nend" \
703       "" \
704       "define one"
705
706     gdb_test "define hook-one\necho hibob\\n\nend" \
707       "" \
708       "define hook-one"
709
710     gdb_test_multiple "define one" "redefine one" {
711         -re "Redefine command .one.. .y or n. $" {
712             send_gdb "y\n"
713             exp_continue
714         }
715
716         -re "End with"  {
717             pass "define one in redefine_hook_test"
718         }
719         default {
720             fail "(timeout or eof) define one in redefine_hook_test"
721         }
722     }
723
724     gdb_test "end" \
725             "" \
726             "enter commands for one redefinition in redefine_hook_test"
727
728     gdb_test "one" \
729             "hibob" \
730             "execute one command in redefine_hook_test"
731 }
732
733 proc redefine_backtrace_test {} {
734     global gdb_prompt
735
736     gdb_test_multiple "define backtrace" "define backtrace" {
737         -re "Really redefine built-in.*$" {
738             send_gdb "y\n"
739             exp_continue
740         }
741
742         -re "End with"  {
743             pass "define backtrace in redefine_backtrace_test"
744         }
745         default {
746             fail "(timeout or eof) define backtrace in redefine_backtrace_test"
747         }
748     }
749     gdb_test "echo hibob\\n\nend" \
750             "" \
751             "enter commands in redefine_backtrace_test"
752
753     gdb_test "backtrace" \
754             "hibob" \
755             "execute backtrace command in redefine_backtrace_test"
756     gdb_test "bt" \
757             "hibob" \
758             "execute bt command in redefine_backtrace_test"
759 }
760
761 gdbvar_simple_if_test
762 gdbvar_simple_while_test
763 gdbvar_complex_if_while_test
764 progvar_simple_if_test
765 progvar_simple_while_test
766 progvar_complex_if_while_test
767 if_while_breakpoint_command_test
768 infrun_breakpoint_command_test
769 breakpoint_command_test
770 user_defined_command_test
771 watchpoint_command_test
772 test_command_prompt_position
773 deprecated_command_test
774 bp_deleted_in_command_test
775 temporary_breakpoint_commands
776 stray_arg0_test
777 source_file_with_indented_comment
778 recursive_source_test
779 if_commands_test
780 redefine_hook_test
781 # This one should come last, as it redefines "backtrace".
782 redefine_backtrace_test