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