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