gdb: Allow GDB to _not_ load a previous command history
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / commands.exp
CommitLineData
b811d2c2 1# Copyright 1988-2020 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"
292 gdb_test "break main" "Breakpoint.*at.*"
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
MS
665 gdb_test "p 5" \
666 "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
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"
003ba290
FN
692}
693
64f367a2 694proc_with_prefix bp_deleted_in_command_test {} {
c2b8ed2c 695 global gdb_prompt
c9d37158 696
c2b8ed2c
MS
697 delete_breakpoints
698
699 # Create a breakpoint, and associate a command-list to it, with
700 # one command that deletes this breakpoint.
701 gdb_test "break factorial" \
64f367a2 702 "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\."
c2b8ed2c 703
64f367a2 704 gdb_test_multiple "commands" "begin commands" {
ad3986f0 705 -re "Type commands for breakpoint.*>$" {
64f367a2 706 pass "begin commands"
c2b8ed2c 707 }
c2b8ed2c 708 }
ad3986f0
MS
709 gdb_test_multiple "silent" "add silent command" {
710 -re ">$" {
711 pass "add silent command"
712 }
c2b8ed2c 713 }
ad3986f0
MS
714 gdb_test_multiple "clear factorial" "add clear command" {
715 -re ">$" {
716 pass "add clear command"
717 }
c2b8ed2c 718 }
ad3986f0
MS
719 gdb_test_multiple "printf \"factorial command-list executed\\n\"" \
720 "add printf command" {
721 -re ">$" {
722 pass "add printf command"
723 }
c2b8ed2c 724 }
ad3986f0
MS
725 gdb_test_multiple "cont" "add cont command" {
726 -re ">$" {
727 pass "add cont command"
728 }
729 }
730 gdb_test "end" \
731 "" \
732 "end commands"
003ba290 733
c2b8ed2c 734 gdb_run_cmd
fa43b1d7 735 gdb_test "" "factorial command-list executed.*" "run factorial until breakpoint"
c2b8ed2c
MS
736}
737
64f367a2 738proc_with_prefix temporary_breakpoint_commands {} {
c2b8ed2c
MS
739 delete_breakpoints
740
741 # Create a temporary breakpoint, and associate a commands list to it.
742 # This test will verify that this commands list is executed when the
743 # breakpoint is hit.
744 gdb_test "tbreak factorial" \
42c0c4f1 745 "Temporary breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\." \
64f367a2
PA
746 "breakpoint"
747
ad3986f0
MS
748 gdb_test_multiple "commands" \
749 "begin commands in bp_deleted_in_command_test" {
750 -re "Type commands for breakpoint.*>$" {
64f367a2 751 pass "begin commands"
ad3986f0
MS
752 }
753 }
754 gdb_test_multiple "silent" "add silent tbreak command" {
755 -re ">$" {
756 pass "add silent tbreak command"
c2b8ed2c 757 }
c2b8ed2c 758 }
38979823 759 gdb_test_multiple "printf \"factorial tbreak commands executed\\n\"" \
ad3986f0
MS
760 "add printf tbreak command" {
761 -re ">$" {
762 pass "add printf tbreak command"
763 }
764 }
765 gdb_test_multiple "cont" "add cont tbreak command" {
766 -re ">$" {
767 pass "add cont tbreak command"
768 }
769 }
770 gdb_test "end" \
771 "" \
772 "end tbreak commands"
c2b8ed2c
MS
773
774 gdb_run_cmd
fa43b1d7
PA
775 gdb_test "" "factorial tbreak commands executed.*" \
776 "run factorial until temporary breakpoint"
c2b8ed2c 777}
61d9b92f
DJ
778
779# Test that GDB can handle $arg0 outside of user functions without
780# crashing.
64f367a2 781proc_with_prefix stray_arg0_test { } {
fad0c9fb
PA
782 global valnum_re
783
61d9b92f 784 gdb_test "print \$arg0" \
fad0c9fb 785 "$valnum_re = void" \
64f367a2 786 "#1"
61d9b92f
DJ
787
788 gdb_test "if 1 == 1\nprint \$arg0\nend" \
fad0c9fb 789 "$valnum_re = void" \
64f367a2 790 "#2"
61d9b92f
DJ
791
792 gdb_test "print \$arg0 = 1" \
fad0c9fb 793 "$valnum_re = 1" \
64f367a2 794 "#3"
61d9b92f
DJ
795
796 gdb_test "print \$arg0" \
fad0c9fb 797 "$valnum_re = 1" \
64f367a2 798 "#4"
61d9b92f 799}
e28493f2 800
02e7ef19 801# Test that GDB is able to source a file with an indented comment.
64f367a2 802proc_with_prefix source_file_with_indented_comment {} {
f76495c8
TT
803 set file1 [standard_output_file file1]
804
805 set fd [open "$file1" w]
02e7ef19
JB
806 puts $fd \
807{define my_fun
808 #indented comment
809end
810echo Done!\n}
811 close $fd
812
64f367a2 813 gdb_test "source $file1" "Done!" "source file"
02e7ef19
JB
814}
815
e28493f2
AS
816# Test that GDB can handle arguments when sourcing files recursively.
817# If the arguments are overwritten with ####### then the test has failed.
64f367a2 818proc_with_prefix recursive_source_test {} {
f76495c8
TT
819 set file1 [standard_output_file file1]
820 set file2 [standard_output_file file2]
821 set file3 [standard_output_file file3]
822
823 set fd [open "$file1" w]
e28493f2 824 puts $fd \
f76495c8
TT
825"source $file2
826abcdef qwerty"
e28493f2
AS
827 close $fd
828
f76495c8 829 set fd [open "$file2" w]
e28493f2 830 puts $fd \
f76495c8
TT
831"define abcdef
832 echo 1: <<<\$arg0>>>\\n
833 source $file3
834 echo 2: <<<\$arg0>>>\\n
835end"
e28493f2
AS
836 close $fd
837
f76495c8 838 set fd [open "$file3" w]
e28493f2
AS
839 puts $fd \
840"echo in file3\\n
841#################################################################"
842 close $fd
843
f76495c8 844 gdb_test "source $file1" \
e28493f2 845 "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
64f367a2 846 "source file"
e28493f2 847
f76495c8
TT
848 file delete $file1
849 file delete $file2
850 file delete $file3
e28493f2
AS
851}
852
704a4f78 853proc gdb_test_no_prompt { command result msg } {
704a4f78
DJ
854 set msg "$command - $msg"
855 set result "^[string_to_regexp $command]\r\n$result$"
856 gdb_test_multiple $command $msg {
857 -re "$result" {
858 pass $msg
859 return 1
860 }
861 -re "\r\n *>$" {
862 fail $msg
863 return 0
864 }
865 }
866 return 0
867}
868
64f367a2 869proc_with_prefix if_commands_test {} {
704a4f78
DJ
870 global gdb_prompt
871
64f367a2 872 gdb_test_no_output "set \$tem = 1" "set \$tem"
704a4f78
DJ
873
874 set test "if_commands_test 1"
875 gdb_test_no_prompt "if \$tem == 2" { >} $test
876 gdb_test_no_prompt "break main" { >} $test
877 gdb_test_no_prompt "else" { >} $test
878 gdb_test_no_prompt "break factorial" { >} $test
879 gdb_test_no_prompt "commands" { >} $test
880 gdb_test_no_prompt "silent" { >} $test
881 gdb_test_no_prompt "set \$tem = 3" { >} $test
882 gdb_test_no_prompt "continue" { >} $test
883 gdb_test_multiple "end" "first end - $test" {
884 -re " >\$" {
885 pass "first end - $test"
886 }
887 -re "\r\n>\$" {
888 fail "first end - $test"
889 }
890 }
891 gdb_test_multiple "end" "second end - $test" {
42c0c4f1 892 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
704a4f78
DJ
893 pass "second end - $test"
894 }
895 -re "Undefined command: \"silent\".*$gdb_prompt $" {
896 fail "second end - $test"
897 }
898 }
899
900 set test "if_commands_test 2"
901 gdb_test_no_prompt "if \$tem == 1" { >} $test
902 gdb_test_no_prompt "break main" { >} $test
903 gdb_test_no_prompt "else" { >} $test
904 gdb_test_no_prompt "break factorial" { >} $test
905 gdb_test_no_prompt "commands" { >} $test
906 gdb_test_no_prompt "silent" { >} $test
907 gdb_test_no_prompt "set \$tem = 3" { >} $test
908 gdb_test_no_prompt "continue" { >} $test
909 gdb_test_multiple "end" "first end - $test" {
910 -re " >\$" {
911 pass "first end - $test"
912 }
913 -re "\r\n>\$" {
914 fail "first end - $test"
915 }
916 }
917 gdb_test_multiple "end" "second end - $test" {
42c0c4f1 918 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
704a4f78
DJ
919 pass "second end - $test"
920 }
921 }
922}
923
353d1d73
JK
924# Verify an error during "commands" commands execution will prevent any other
925# "commands" from other breakpoints at the same location to be executed.
926
64f367a2 927proc_with_prefix error_clears_commands_left {} {
353d1d73
JK
928 set test "hook-stop 1"
929 gdb_test_multiple {define hook-stop} $test {
930 -re "End with a line saying just \"end\"\\.\r\n>$" {
931 pass $test
932 }
933 }
934 set test "hook-stop 1a"
935 gdb_test_multiple {echo hook-stop1\n} $test {
936 -re "\r\n>$" {
937 pass $test
938 }
939 }
940 gdb_test_no_output "end" "hook-stop 1b"
941
942 delete_breakpoints
943 gdb_breakpoint "main"
944
945 set test "main commands 1"
946 gdb_test_multiple {commands $bpnum} $test {
947 -re "End with a line saying just \"end\"\\.\r\n>$" {
948 pass $test
949 }
950 }
951 set test "main commands 1a"
952 gdb_test_multiple {echo cmd1\n} $test {
953 -re "\r\n>$" {
954 pass $test
955 }
956 }
957 set test "main commands 1b"
958 gdb_test_multiple {errorcommandxy\n} $test {
959 -re "\r\n>$" {
960 pass $test
961 }
962 }
963 gdb_test_no_output "end" "main commands 1c"
964
965 gdb_breakpoint "main"
966 set test "main commands 2"
967 gdb_test_multiple {commands $bpnum} $test {
968 -re "End with a line saying just \"end\"\\.\r\n>$" {
969 pass $test
970 }
971 }
972 set test "main commands 2a"
973 gdb_test_multiple {echo cmd2\n} $test {
974 -re "\r\n>$" {
975 pass $test
976 }
977 }
978 set test "main commands 2b"
979 gdb_test_multiple {errorcommandyz\n} $test {
980 -re "\r\n>$" {
981 pass $test
982 }
983 }
984 gdb_test_no_output "end" "main commands 2c"
985
986 gdb_run_cmd
fad0c9fb
PA
987 gdb_test \
988 "" \
989 [multi_line \
990 "hook-stop1" \
991 ".*" \
992 "cmd1" \
993 "Undefined command: \"errorcommandxy\"\\. Try \"help\"\\."] \
994 "cmd1 error"
353d1d73
JK
995
996 gdb_test {echo idle\n} "\r\nidle" "no cmd2"
997}
998
64f367a2 999proc_with_prefix redefine_hook_test {} {
fad0c9fb
PA
1000 gdb_test \
1001 [multi_line_input \
1002 "define one"\
1003 "end"] \
1004 "" \
1005 "define one"
fad6eecd 1006
fad0c9fb
PA
1007 gdb_test \
1008 [multi_line_input \
1009 "define hook-one" \
1010 "echo hibob\\n" \
1011 "end"] \
1012 "" \
1013 "define hook-one"
fad6eecd 1014
64f367a2
PA
1015 set test "redefine one"
1016 gdb_test_multiple "define one" $test {
fad6eecd
TT
1017 -re "Redefine command .one.. .y or n. $" {
1018 send_gdb "y\n"
1019 exp_continue
1020 }
1021
1022 -re "End with" {
64f367a2 1023 pass $test
fad6eecd
TT
1024 }
1025 }
1026
fad0c9fb 1027 gdb_test "end" "" "enter commands for one redefinition"
fad6eecd 1028
fad0c9fb 1029 gdb_test "one" "hibob" "execute one command"
fad6eecd
TT
1030}
1031
64f367a2 1032proc_with_prefix redefine_backtrace_test {} {
b05dcbb7 1033 gdb_test_multiple "define backtrace" "define backtrace" {
d26ccb4f
JK
1034 -re "Really redefine built-in command \"backtrace\"\\? \\(y or n\\) $" {
1035 pass "define backtrace"
b05dcbb7 1036 }
d26ccb4f 1037 }
b05dcbb7 1038
d26ccb4f
JK
1039 gdb_test_multiple "y" "expect response to define backtrace" {
1040 -re "End with a line saying just \"end\"\\.\r\n>$" {
1041 pass "expect response to define backtrace"
b05dcbb7
TT
1042 }
1043 }
d26ccb4f 1044
fad0c9fb
PA
1045 gdb_test \
1046 [multi_line_input \
1047 "echo hibob\\n" \
1048 "end"] \
1049 "" \
1050 "enter commands"
b05dcbb7 1051
fad0c9fb
PA
1052 gdb_test "backtrace" "hibob" "execute backtrace command"
1053 gdb_test "bt" "hibob" "execute bt command"
b05dcbb7
TT
1054}
1055
80a65e9b
SM
1056# Test using "if" and "while" without args when building a command list.
1057
1058proc define_if_without_arg_test {} {
7a2c85f2 1059 foreach cmd {if while define} {
80a65e9b
SM
1060 set test "define some_command_$cmd"
1061 gdb_test_multiple $test $test {
1062 -re "End with" {
1063 pass $test
1064 }
1065 }
1066
7a2c85f2 1067 gdb_test "$cmd" "$cmd command requires an argument." "type $cmd without args"
80a65e9b
SM
1068 }
1069}
1070
9521ecda
SM
1071# Test the loop_break command.
1072
1073proc_with_prefix loop_break_test {} {
1074 gdb_test_no_output "set \$a = 0" "initialize \$a"
34d16ea2 1075 gdb_test_no_output "set \$total = 0" "initialize \$total"
9521ecda
SM
1076
1077 gdb_test \
34d16ea2
SM
1078 [multi_line_input \
1079 "while \$a < 5" \
1080 " if \$a == 4" \
1081 " loop_break" \
1082 " end" \
1083 " set \$b = 0" \
1084 " while \$b < 5" \
1085 " if \$b == 2" \
1086 " loop_break" \
1087 " end" \
1088 " set \$total = \$total + 1" \
1089 " set \$b = \$b + 1" \
1090 " end" \
1091 " set \$a = \$a + 1" \
1092 "end"] \
1093 "" \
1094 "run while loop"
1095
1096 gdb_test "print \$a" " = 4" "validate \$a"
1097 gdb_test "print \$b" " = 2" "validate \$b"
1098 gdb_test "print \$total" " = 8" "validate \$total"
9521ecda
SM
1099}
1100
1101# Test the loop_continue command.
1102
1103proc_with_prefix loop_continue_test {} {
1104 gdb_test_no_output "set \$a = 0" "initialize \$a"
34d16ea2 1105 gdb_test_no_output "set \$total = 0" "initialize \$total"
9521ecda
SM
1106
1107 gdb_test \
34d16ea2
SM
1108 [multi_line_input \
1109 "while \$a < 5" \
1110 " set \$a = \$a + 1" \
1111 " set \$b = 0" \
1112 " if \$a == 4" \
1113 " loop_continue" \
1114 " end" \
1115 " while \$b < 5" \
1116 " set \$b = \$b + 1" \
1117 " if \$b == 2" \
1118 " loop_continue" \
1119 " end" \
1120 " set \$total = \$total + 1" \
1121 " end" \
1122 "end"] \
1123 "" \
1124 "run while loop"
9521ecda
SM
1125
1126 gdb_test "print \$a" " = 5" "validate \$a"
34d16ea2
SM
1127 gdb_test "print \$b" " = 5" "validate \$b"
1128 gdb_test "print \$total" " = 16" "validate \$total"
9521ecda
SM
1129}
1130
6e5d74e7
PA
1131# Test an input line split with a continuation character (backslash)
1132# while entering a multi-line command (in a secondary prompt).
1133
1134proc_with_prefix backslash_in_multi_line_command_test {} {
7978d7c3
SM
1135 set dg_ver [dejagnu_version]
1136 set dg_major [lindex $dg_ver 0]
1137 set dg_minor [lindex $dg_ver 1]
1138
1139 # With older versions of DejaGnu, the "\\\n" we send gets replaced with a
1140 # space, thus breaking the test. Just skip it in that case.
1141 if { $dg_major == 1 && $dg_minor < 5 } {
1142 untested "dejagnu version is too old"
1143 return
1144 }
1145
6e5d74e7
PA
1146 gdb_breakpoint "main"
1147
1148 gdb_test_multiple "commands" "commands" {
1149 -re "End with a line saying just \"end\"\\.\r\n>$" {
1150 pass "commands"
1151 }
1152 }
1153
1154 set test "input line split with backslash"
1155 send_gdb "print \\\nargc\n"
1156 gdb_test_multiple "" $test {
1157 -re "^print \\\\\r\nargc\r\n>$" {
1158 pass $test
1159 }
1160 }
1161
1162 gdb_test_no_output "end"
1163
1164 # Input any command, just to be sure the readline state is sane.
1165 # In PR 21218, this would trigger the infamous:
1166 # readline: readline_callback_read_char() called with no handler!
1167 gdb_test "print 1" "" "run command"
1168}
1169
c906108c
SS
1170gdbvar_simple_if_test
1171gdbvar_simple_while_test
1172gdbvar_complex_if_while_test
1173progvar_simple_if_test
1174progvar_simple_while_test
1175progvar_complex_if_while_test
1176if_while_breakpoint_command_test
1177infrun_breakpoint_command_test
1178breakpoint_command_test
ead9aa39 1179breakpoint_clear_command_test
c906108c 1180user_defined_command_test
fd437cbc 1181user_defined_command_case_sensitivity
01770bbd 1182user_defined_command_args_eval
ec835369 1183user_defined_command_args_stack_test
df3ee9ca 1184user_defined_command_manyargs_test
085dd6e6 1185watchpoint_command_test
7a292a7a 1186test_command_prompt_position
003ba290 1187deprecated_command_test
c2b8ed2c
MS
1188bp_deleted_in_command_test
1189temporary_breakpoint_commands
61d9b92f 1190stray_arg0_test
02e7ef19 1191source_file_with_indented_comment
e28493f2 1192recursive_source_test
704a4f78 1193if_commands_test
353d1d73 1194error_clears_commands_left
fad6eecd 1195redefine_hook_test
6e5d74e7 1196backslash_in_multi_line_command_test
80a65e9b 1197define_if_without_arg_test
9521ecda
SM
1198loop_break_test
1199loop_continue_test
b05dcbb7
TT
1200# This one should come last, as it redefines "backtrace".
1201redefine_backtrace_test
This page took 2.178446 seconds and 4 git commands to generate.