]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/commands.exp
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / commands.exp
CommitLineData
e2882c85 1# Copyright 1988-2018 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
64f367a2 198 gdb_test "break factorial" "Breakpoint.*at.*" "break factorial"
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
284# Test a simple user defined command (with arguments)
64f367a2 285proc_with_prefix user_defined_command_test {} {
fad0c9fb 286 global valnum_re
c906108c 287
64f367a2 288 gdb_test_no_output "set \$foo = 4" "set foo"
c906108c 289
64f367a2
PA
290 gdb_test_multiple "define mycommand" "define mycommand" {
291 -re "End with" {
292 pass "define mycommand"
c906108c 293 }
64f367a2 294 }
ad3986f0 295
c906108c 296 # This test should alternate between 0xdeadbeef and 0xfeedface two times.
fad0c9fb
PA
297 gdb_test \
298 [multi_line_input \
299 {while $arg0 > 0} \
300 { set $arg0 -= 1} \
301 { if ($arg0 % 2) == 1} \
302 { p/x 0xdeadbeef} \
303 { else} \
304 { p/x 0xfeedface} \
305 { end} \
306 {end} \
307 {end}] \
308 "" \
309 "enter commands"
310
311 global decimal
312 set valnum_re "\\\$$decimal"
313
314 gdb_test \
315 {mycommand $foo} \
316 [multi_line \
317 "$valnum_re = 0xdeadbeef" \
318 "$valnum_re = 0xfeedface" \
319 "$valnum_re = 0xdeadbeef" \
320 "$valnum_re = 0xfeedface"] \
321 "execute user-defined command"
42f5c13f 322 gdb_test "show user mycommand" \
e6ccd35f 323 " while \\\$arg0.*set.* if \\\(\\\$arg0.*p/x.* else\[^\n\].*p/x.* end\[^\n\].* end\[^\n\].*" \
64f367a2 324 "display user command"
a9f116cb
GKB
325
326 # Create and test a user-defined command with an empty body.
64f367a2
PA
327 gdb_test_multiple "define myemptycommand" "define myemptycommand" {
328 -re "End with" {
329 pass "define myemptycommand"
a9f116cb 330 }
64f367a2 331 }
a9f116cb
GKB
332 gdb_test "end" \
333 "" \
334 "end definition of user-defined command with empty body"
335
336 gdb_test_no_output "myemptycommand" \
64f367a2 337 "execute user-defined empty command"
a9f116cb
GKB
338
339 gdb_test "show user" \
340 "User command \"myemptycommand.*" \
64f367a2 341 "display empty command in command list"
a9f116cb
GKB
342
343 gdb_test "show user myemptycommand" \
344 "User command \"myemptycommand.*" \
64f367a2 345 "display user-defined empty command"
c906108c
SS
346}
347
fd437cbc
SM
348# Test that the case with which the command was defined is preserved.
349
350proc_with_prefix user_defined_command_case_sensitivity {} {
351 # Define a first command with mixed case name.
352 set test "define Homer-Simpson"
353 gdb_test_multiple $test $test {
354 -re "End with" {
355 pass $test
356 }
357 }
358
359 gdb_test "print 123\nend" "" "enter commands 1"
360
361 # Define a second command, same name but different case.
362 set test "define HomeR-SimpsoN"
363 gdb_test_multiple $test $test {
364 -re "End with" {
365 pass $test
366 }
367 }
368
369 gdb_test "print 456\nend" "" "enter commands 2"
370
371 gdb_test "Homer-Simpson" " = 123" "execute command"
372 gdb_test "HomeR-SimpsoN" " = 456" "execute command"
373 gdb_test "HOMER-SIMPSON" "Undefined command.*" "try to call in upper case"
374 gdb_test "homer-simpson" "Undefined command.*" "try to call in lower case"
375}
376
01770bbd
PA
377# Test that "eval" in a user-defined command expands $argc/$argN.
378
379proc_with_prefix user_defined_command_args_eval {} {
01770bbd
PA
380 gdb_test_multiple "define command_args_eval" \
381 "define command_args_eval" {
382 -re "End with" {
383 pass "define"
384 }
385 }
386
387 # Make a command that constructs references to $argc and $argN via
388 # eval.
389 gdb_test \
390 [multi_line \
391 {eval "printf \"argc = %%d,\", $arg%c", 'c'} \
392 {set $i = 0} \
393 {while $i < $argc} \
394 { eval "printf \" %%d\", $arg%d", $i} \
395 { set $i = $i + 1} \
396 {end} \
397 {printf "\n"} \
398 {end}] \
399 "" \
400 "enter commands"
401
402 gdb_test "command_args_eval 1 2 3" "argc = 3, 1 2 3" "execute command"
403}
404
ec835369
PA
405# Test that the $argc/$argN variables are pushed on/popped from the
406# args stack correctly when a user-defined command calls another
407# user-defined command (or in this case, recurses).
408
409proc_with_prefix user_defined_command_args_stack_test {} {
ec835369
PA
410 gdb_test_multiple "define args_stack_command" \
411 "define args_stack_command" {
412 -re "End with" {
413 pass "define"
414 }
415 }
416
417 # Make a command that refers to $argc/$argN before and after
418 # recursing. Also, vary the number of arguments passed to each
419 # recursion point.
420 gdb_test \
421 [multi_line \
422 {printf "before, argc = %d,", $argc} \
423 {set $i = 0} \
424 {while $i < $argc} \
425 { eval "printf \" %%d\", $arg%d", $i} \
426 { set $i = $i + 1} \
427 {end} \
428 {printf "\n"} \
429 {} \
430 {} \
431 {if $argc == 3} \
432 { args_stack_command 21 22} \
433 {end} \
434 {if $argc == 2} \
435 { args_stack_command 11} \
436 {end} \
437 {} \
438 {} \
439 {printf "after, argc = %d,", $argc} \
440 {set $i = 0} \
441 {while $i < $argc} \
442 { eval "printf \" %%d\", $arg%d", $i} \
443 { set $i = $i + 1} \
444 {end} \
445 {printf "\n"} \
446 {end}] \
447 "" \
448 "enter commands"
449
450 set expected \
451 [multi_line \
452 "before, argc = 3, 31 32 33" \
453 "before, argc = 2, 21 22" \
454 "before, argc = 1, 11" \
455 "after, argc = 1, 11" \
456 "after, argc = 2, 21 22" \
457 "after, argc = 3, 31 32 33"]
458 gdb_test "args_stack_command 31 32 33" $expected "execute command"
459}
460
df3ee9ca
PA
461# Test a simple user defined command with many arguments. GDB <= 7.12
462# used to have a hard coded limit of 10 arguments.
463
464proc_with_prefix user_defined_command_manyargs_test {} {
df3ee9ca
PA
465 set test "define command"
466 gdb_test_multiple "define manyargs" $test {
467 -re "End with" {
468 pass $test
469 }
470 }
471
472 # Define a function that doubles its arguments.
473 gdb_test \
474 [multi_line \
475 {printf "nargs=%d:", $argc} \
476 {set $i = 0} \
477 {while $i < $argc} \
478 { eval "printf \" %%d\", 2 * $arg%d\n", $i} \
479 { set $i = $i + 1} \
480 {end} \
481 {printf "\n"} \
482 {end}] \
483 "" \
484 "enter commands"
485
486 # Some random number of arguments, as long as higher than 10.
487 set nargs 100
488
489 set cmd "manyargs"
490 for {set i 1} {$i <= $nargs} {incr i} {
491 append cmd " $i"
492 }
493
494 set expected "nargs=$nargs:"
495 for {set i 1} {$i <= $nargs} {incr i} {
496 append expected " " [expr 2 * $i]
497 }
498
499 gdb_test $cmd $expected "execute command"
500}
501
64f367a2 502proc_with_prefix watchpoint_command_test {} {
085dd6e6
JM
503 global gdb_prompt
504
bd5ddfe8
DJ
505 # Disable hardware watchpoints if necessary.
506 if [target_info exists gdb,no_hardware_watchpoints] {
35ec993f 507 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
bd5ddfe8
DJ
508 }
509
fad0c9fb
PA
510 runto_or_return factorial
511
085dd6e6
JM
512 delete_breakpoints
513
514 # Verify that we can create a watchpoint, and give it a commands
515 # list that continues the inferior. We set the watchpoint on a
516 # local variable, too, so that it self-deletes when the watched
517 # data goes out of scope.
518 #
519 # What should happen is: Each time the watchpoint triggers, it
520 # continues the inferior. Eventually, the watchpoint will self-
521 # delete, when the watched variable is out of scope. But by that
522 # time, the inferior should have exited. GDB shouldn't crash or
523 # anything untoward as a result of this.
524 #
525 set wp_id -1
526
ad3986f0
MS
527 gdb_test_multiple "watch local_var" "watch local_var" {
528 -re "\[Ww\]atchpoint (\[0-9\]*): local_var.*$gdb_prompt $" {
085dd6e6
JM
529 set wp_id $expect_out(1,string)
530 pass "watch local_var"
531 }
085dd6e6 532 }
7a292a7a 533
085dd6e6
JM
534 if {$wp_id == -1} {return}
535
3f9e0d32 536 gdb_test_multiple "commands $wp_id" "begin commands on watch" {
ad3986f0
MS
537 -re "Type commands for breakpoint.*, one per line.*>$" {
538 pass "begin commands on watch"
539 }
085dd6e6 540 }
cdac0397
PA
541 # See the 'No symbol "value...' fail below. This command will
542 # fail if it's executed in the wrong frame. If adjusting the
543 # test, make sure this property holds.
ad3986f0
MS
544 gdb_test_multiple "print value" "add print command to watch" {
545 -re ">$" {
546 pass "add print command to watch"
547 }
085dd6e6 548 }
ad3986f0
MS
549 gdb_test_multiple "continue" "add continue command to watch" {
550 -re ">$" {
551 pass "add continue command to watch"
42f5c13f 552 }
085dd6e6 553 }
ad3986f0
MS
554 gdb_test "end" \
555 "" \
556 "end commands on watch"
557
cdac0397 558 set test "continue with watch"
95e4302a
JM
559 set lno_1 [gdb_get_line_number "commands.exp: hw local_var out of scope" "run.c"]
560 set lno_2 [gdb_get_line_number "commands.exp: local_var out of scope" "run.c"]
cdac0397
PA
561 gdb_test_multiple "continue" "$test" {
562 -re "No symbol \"value\" in current context.\r\n$gdb_prompt $" {
563 # Happens if GDB actually runs the watchpoints commands,
564 # even though the watchpoint was deleted for not being in
565 # scope.
566 fail $test
567 }
95e4302a 568 -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
569 pass $test
570 }
571 }
085dd6e6 572}
7a292a7a 573
64f367a2 574proc_with_prefix test_command_prompt_position {} {
7a292a7a 575 global gdb_prompt
fad0c9fb
PA
576 global valnum_re
577
578 runto_or_return factorial
7a292a7a 579
42f5c13f
MS
580 # Don't depend upon argument passing, since most simulators don't
581 # currently support it. Bash value variable to be what we want.
7a292a7a 582 delete_breakpoints
64f367a2
PA
583 gdb_test "break factorial" "Breakpoint.*at.*"
584 gdb_test "p value=5" ".*" "set value to 5"
7a292a7a 585 # All this test should do is print 0xdeadbeef once.
fad0c9fb
PA
586 gdb_test \
587 [multi_line_input \
588 {if value == 1} \
589 { p/x 0xfeedface} \
590 {else} \
591 { p/x 0xdeadbeef} \
592 {end}] \
593 "$valnum_re = 0xdeadbeef" \
594 "if test"
595
42f5c13f
MS
596 # Now let's test for the correct position of the '>' in gdb's
597 # prompt for commands. It should be at the beginning of the line,
598 # and not after one space.
7a292a7a 599
fad0c9fb
PA
600 set test "> OK"
601 gdb_test_multiple "commands" $test {
602 -re "Type commands.*End with.*\[\r\n\]>$" {
603 gdb_test_multiple "printf \"Now the value is %d\\n\", value" $test {
42f5c13f 604 -re "^printf.*value\r\n>$" {
fad0c9fb 605 gdb_test_multiple "end" $test {
42f5c13f 606 -re "^end\r\n$gdb_prompt $" {
fad0c9fb 607 pass $test
42f5c13f
MS
608 }
609 }
610 }
42f5c13f
MS
611 }
612 }
42f5c13f 613 }
7a292a7a
SS
614}
615
616
003ba290 617
64f367a2 618proc_with_prefix deprecated_command_test {} {
003ba290 619 gdb_test "maintenance deprecate blah" "Can't find command.*" \
7dbd117d 620 "tried to deprecate non-existing command"
003ba290 621
27d3a1a2 622 gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /1/"
42f5c13f
MS
623 gdb_test "p 5" \
624 "Warning: 'p', an alias for the command 'print' is deprecated.*Use 'new_p'.*" \
625 "p deprecated warning, with replacement"
cdc7edd7 626 gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /1/"
003ba290 627
27d3a1a2
MS
628 gdb_test_no_output "maintenance deprecate p \"new_p\"" "maintenance deprecate p \"new_p\" /2/"
629 gdb_test_no_output "maintenance deprecate print \"new_print\""
42f5c13f
MS
630 gdb_test "p 5" \
631 "Warning: command 'print' \\(p\\) is deprecated.*Use 'new_print'.*" \
632 "both alias and command are deprecated"
cdc7edd7 633 gdb_test "p 5" ".\[0-9\]* = 5.*" "deprecated warning goes away /2/"
003ba290 634
27d3a1a2 635 gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size \"srm\" " \
7dbd117d 636 "deprecate long command /1/"
42f5c13f
MS
637 gdb_test "set remote memory-read-packet-size" \
638 "Warning: command 'set remote memory-read-packet-size' is deprecated.*Use 'srm'.*" \
7dbd117d 639 "long command deprecated /1/"
42f5c13f 640
27d3a1a2 641 gdb_test_no_output "maintenance deprecate set remote memory-read-packet-size" \
7dbd117d 642 "deprecate long command /2/"
42f5c13f
MS
643 gdb_test "set remote memory-read-packet-size" \
644 "Warning: command 'set remote memory-read-packet-size' is deprecated.*No alternative known.*" \
7dbd117d 645 "long command deprecated with no alternative /2/"
42f5c13f
MS
646
647 gdb_test "maintenance deprecate" \
648 "\"maintenance deprecate\".*" \
649 "deprecate with no arguments"
003ba290
FN
650}
651
64f367a2 652proc_with_prefix bp_deleted_in_command_test {} {
c2b8ed2c 653 global gdb_prompt
c9d37158 654
c2b8ed2c
MS
655 delete_breakpoints
656
657 # Create a breakpoint, and associate a command-list to it, with
658 # one command that deletes this breakpoint.
659 gdb_test "break factorial" \
64f367a2 660 "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\."
c2b8ed2c 661
64f367a2 662 gdb_test_multiple "commands" "begin commands" {
ad3986f0 663 -re "Type commands for breakpoint.*>$" {
64f367a2 664 pass "begin commands"
c2b8ed2c 665 }
c2b8ed2c 666 }
ad3986f0
MS
667 gdb_test_multiple "silent" "add silent command" {
668 -re ">$" {
669 pass "add silent command"
670 }
c2b8ed2c 671 }
ad3986f0
MS
672 gdb_test_multiple "clear factorial" "add clear command" {
673 -re ">$" {
674 pass "add clear command"
675 }
c2b8ed2c 676 }
ad3986f0
MS
677 gdb_test_multiple "printf \"factorial command-list executed\\n\"" \
678 "add printf command" {
679 -re ">$" {
680 pass "add printf command"
681 }
c2b8ed2c 682 }
ad3986f0
MS
683 gdb_test_multiple "cont" "add cont command" {
684 -re ">$" {
685 pass "add cont command"
686 }
687 }
688 gdb_test "end" \
689 "" \
690 "end commands"
003ba290 691
c2b8ed2c 692 gdb_run_cmd
fa43b1d7 693 gdb_test "" "factorial command-list executed.*" "run factorial until breakpoint"
c2b8ed2c
MS
694}
695
64f367a2 696proc_with_prefix temporary_breakpoint_commands {} {
c2b8ed2c
MS
697 delete_breakpoints
698
699 # Create a temporary breakpoint, and associate a commands list to it.
700 # This test will verify that this commands list is executed when the
701 # breakpoint is hit.
702 gdb_test "tbreak factorial" \
42c0c4f1 703 "Temporary breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\." \
64f367a2
PA
704 "breakpoint"
705
ad3986f0
MS
706 gdb_test_multiple "commands" \
707 "begin commands in bp_deleted_in_command_test" {
708 -re "Type commands for breakpoint.*>$" {
64f367a2 709 pass "begin commands"
ad3986f0
MS
710 }
711 }
712 gdb_test_multiple "silent" "add silent tbreak command" {
713 -re ">$" {
714 pass "add silent tbreak command"
c2b8ed2c 715 }
c2b8ed2c 716 }
38979823 717 gdb_test_multiple "printf \"factorial tbreak commands executed\\n\"" \
ad3986f0
MS
718 "add printf tbreak command" {
719 -re ">$" {
720 pass "add printf tbreak command"
721 }
722 }
723 gdb_test_multiple "cont" "add cont tbreak command" {
724 -re ">$" {
725 pass "add cont tbreak command"
726 }
727 }
728 gdb_test "end" \
729 "" \
730 "end tbreak commands"
c2b8ed2c
MS
731
732 gdb_run_cmd
fa43b1d7
PA
733 gdb_test "" "factorial tbreak commands executed.*" \
734 "run factorial until temporary breakpoint"
c2b8ed2c 735}
61d9b92f
DJ
736
737# Test that GDB can handle $arg0 outside of user functions without
738# crashing.
64f367a2 739proc_with_prefix stray_arg0_test { } {
fad0c9fb
PA
740 global valnum_re
741
61d9b92f 742 gdb_test "print \$arg0" \
fad0c9fb 743 "$valnum_re = void" \
64f367a2 744 "#1"
61d9b92f
DJ
745
746 gdb_test "if 1 == 1\nprint \$arg0\nend" \
fad0c9fb 747 "$valnum_re = void" \
64f367a2 748 "#2"
61d9b92f
DJ
749
750 gdb_test "print \$arg0 = 1" \
fad0c9fb 751 "$valnum_re = 1" \
64f367a2 752 "#3"
61d9b92f
DJ
753
754 gdb_test "print \$arg0" \
fad0c9fb 755 "$valnum_re = 1" \
64f367a2 756 "#4"
61d9b92f 757}
e28493f2 758
02e7ef19 759# Test that GDB is able to source a file with an indented comment.
64f367a2 760proc_with_prefix source_file_with_indented_comment {} {
f76495c8
TT
761 set file1 [standard_output_file file1]
762
763 set fd [open "$file1" w]
02e7ef19
JB
764 puts $fd \
765{define my_fun
766 #indented comment
767end
768echo Done!\n}
769 close $fd
770
64f367a2 771 gdb_test "source $file1" "Done!" "source file"
02e7ef19
JB
772}
773
e28493f2
AS
774# Test that GDB can handle arguments when sourcing files recursively.
775# If the arguments are overwritten with ####### then the test has failed.
64f367a2 776proc_with_prefix recursive_source_test {} {
f76495c8
TT
777 set file1 [standard_output_file file1]
778 set file2 [standard_output_file file2]
779 set file3 [standard_output_file file3]
780
781 set fd [open "$file1" w]
e28493f2 782 puts $fd \
f76495c8
TT
783"source $file2
784abcdef qwerty"
e28493f2
AS
785 close $fd
786
f76495c8 787 set fd [open "$file2" w]
e28493f2 788 puts $fd \
f76495c8
TT
789"define abcdef
790 echo 1: <<<\$arg0>>>\\n
791 source $file3
792 echo 2: <<<\$arg0>>>\\n
793end"
e28493f2
AS
794 close $fd
795
f76495c8 796 set fd [open "$file3" w]
e28493f2
AS
797 puts $fd \
798"echo in file3\\n
799#################################################################"
800 close $fd
801
f76495c8 802 gdb_test "source $file1" \
e28493f2 803 "1: <<<qwerty>>>\[\r\n]+in file3\[\r\n]+2: <<<qwerty>>>" \
64f367a2 804 "source file"
e28493f2 805
f76495c8
TT
806 file delete $file1
807 file delete $file2
808 file delete $file3
e28493f2
AS
809}
810
704a4f78 811proc gdb_test_no_prompt { command result msg } {
704a4f78
DJ
812 set msg "$command - $msg"
813 set result "^[string_to_regexp $command]\r\n$result$"
814 gdb_test_multiple $command $msg {
815 -re "$result" {
816 pass $msg
817 return 1
818 }
819 -re "\r\n *>$" {
820 fail $msg
821 return 0
822 }
823 }
824 return 0
825}
826
64f367a2 827proc_with_prefix if_commands_test {} {
704a4f78
DJ
828 global gdb_prompt
829
64f367a2 830 gdb_test_no_output "set \$tem = 1" "set \$tem"
704a4f78
DJ
831
832 set test "if_commands_test 1"
833 gdb_test_no_prompt "if \$tem == 2" { >} $test
834 gdb_test_no_prompt "break main" { >} $test
835 gdb_test_no_prompt "else" { >} $test
836 gdb_test_no_prompt "break factorial" { >} $test
837 gdb_test_no_prompt "commands" { >} $test
838 gdb_test_no_prompt "silent" { >} $test
839 gdb_test_no_prompt "set \$tem = 3" { >} $test
840 gdb_test_no_prompt "continue" { >} $test
841 gdb_test_multiple "end" "first end - $test" {
842 -re " >\$" {
843 pass "first end - $test"
844 }
845 -re "\r\n>\$" {
846 fail "first end - $test"
847 }
848 }
849 gdb_test_multiple "end" "second end - $test" {
42c0c4f1 850 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
704a4f78
DJ
851 pass "second end - $test"
852 }
853 -re "Undefined command: \"silent\".*$gdb_prompt $" {
854 fail "second end - $test"
855 }
856 }
857
858 set test "if_commands_test 2"
859 gdb_test_no_prompt "if \$tem == 1" { >} $test
860 gdb_test_no_prompt "break main" { >} $test
861 gdb_test_no_prompt "else" { >} $test
862 gdb_test_no_prompt "break factorial" { >} $test
863 gdb_test_no_prompt "commands" { >} $test
864 gdb_test_no_prompt "silent" { >} $test
865 gdb_test_no_prompt "set \$tem = 3" { >} $test
866 gdb_test_no_prompt "continue" { >} $test
867 gdb_test_multiple "end" "first end - $test" {
868 -re " >\$" {
869 pass "first end - $test"
870 }
871 -re "\r\n>\$" {
872 fail "first end - $test"
873 }
874 }
875 gdb_test_multiple "end" "second end - $test" {
42c0c4f1 876 -re "Breakpoint \[0-9\]+ at .*: file .*run.c, line \[0-9\]+\.\r\n$gdb_prompt $" {
704a4f78
DJ
877 pass "second end - $test"
878 }
879 }
880}
881
353d1d73
JK
882# Verify an error during "commands" commands execution will prevent any other
883# "commands" from other breakpoints at the same location to be executed.
884
64f367a2 885proc_with_prefix error_clears_commands_left {} {
353d1d73
JK
886 set test "hook-stop 1"
887 gdb_test_multiple {define hook-stop} $test {
888 -re "End with a line saying just \"end\"\\.\r\n>$" {
889 pass $test
890 }
891 }
892 set test "hook-stop 1a"
893 gdb_test_multiple {echo hook-stop1\n} $test {
894 -re "\r\n>$" {
895 pass $test
896 }
897 }
898 gdb_test_no_output "end" "hook-stop 1b"
899
900 delete_breakpoints
901 gdb_breakpoint "main"
902
903 set test "main commands 1"
904 gdb_test_multiple {commands $bpnum} $test {
905 -re "End with a line saying just \"end\"\\.\r\n>$" {
906 pass $test
907 }
908 }
909 set test "main commands 1a"
910 gdb_test_multiple {echo cmd1\n} $test {
911 -re "\r\n>$" {
912 pass $test
913 }
914 }
915 set test "main commands 1b"
916 gdb_test_multiple {errorcommandxy\n} $test {
917 -re "\r\n>$" {
918 pass $test
919 }
920 }
921 gdb_test_no_output "end" "main commands 1c"
922
923 gdb_breakpoint "main"
924 set test "main commands 2"
925 gdb_test_multiple {commands $bpnum} $test {
926 -re "End with a line saying just \"end\"\\.\r\n>$" {
927 pass $test
928 }
929 }
930 set test "main commands 2a"
931 gdb_test_multiple {echo cmd2\n} $test {
932 -re "\r\n>$" {
933 pass $test
934 }
935 }
936 set test "main commands 2b"
937 gdb_test_multiple {errorcommandyz\n} $test {
938 -re "\r\n>$" {
939 pass $test
940 }
941 }
942 gdb_test_no_output "end" "main commands 2c"
943
944 gdb_run_cmd
fad0c9fb
PA
945 gdb_test \
946 "" \
947 [multi_line \
948 "hook-stop1" \
949 ".*" \
950 "cmd1" \
951 "Undefined command: \"errorcommandxy\"\\. Try \"help\"\\."] \
952 "cmd1 error"
353d1d73
JK
953
954 gdb_test {echo idle\n} "\r\nidle" "no cmd2"
955}
956
64f367a2 957proc_with_prefix redefine_hook_test {} {
fad0c9fb
PA
958 gdb_test \
959 [multi_line_input \
960 "define one"\
961 "end"] \
962 "" \
963 "define one"
fad6eecd 964
fad0c9fb
PA
965 gdb_test \
966 [multi_line_input \
967 "define hook-one" \
968 "echo hibob\\n" \
969 "end"] \
970 "" \
971 "define hook-one"
fad6eecd 972
64f367a2
PA
973 set test "redefine one"
974 gdb_test_multiple "define one" $test {
fad6eecd
TT
975 -re "Redefine command .one.. .y or n. $" {
976 send_gdb "y\n"
977 exp_continue
978 }
979
980 -re "End with" {
64f367a2 981 pass $test
fad6eecd
TT
982 }
983 }
984
fad0c9fb 985 gdb_test "end" "" "enter commands for one redefinition"
fad6eecd 986
fad0c9fb 987 gdb_test "one" "hibob" "execute one command"
fad6eecd
TT
988}
989
64f367a2 990proc_with_prefix redefine_backtrace_test {} {
b05dcbb7 991 gdb_test_multiple "define backtrace" "define backtrace" {
d26ccb4f
JK
992 -re "Really redefine built-in command \"backtrace\"\\? \\(y or n\\) $" {
993 pass "define backtrace"
b05dcbb7 994 }
d26ccb4f 995 }
b05dcbb7 996
d26ccb4f
JK
997 gdb_test_multiple "y" "expect response to define backtrace" {
998 -re "End with a line saying just \"end\"\\.\r\n>$" {
999 pass "expect response to define backtrace"
b05dcbb7
TT
1000 }
1001 }
d26ccb4f 1002
fad0c9fb
PA
1003 gdb_test \
1004 [multi_line_input \
1005 "echo hibob\\n" \
1006 "end"] \
1007 "" \
1008 "enter commands"
b05dcbb7 1009
fad0c9fb
PA
1010 gdb_test "backtrace" "hibob" "execute backtrace command"
1011 gdb_test "bt" "hibob" "execute bt command"
b05dcbb7
TT
1012}
1013
80a65e9b
SM
1014# Test using "if" and "while" without args when building a command list.
1015
1016proc define_if_without_arg_test {} {
1017 foreach cmd {if while} {
1018 set test "define some_command_$cmd"
1019 gdb_test_multiple $test $test {
1020 -re "End with" {
1021 pass $test
1022 }
1023 }
1024
1025 gdb_test "$cmd" "if/while commands require arguments." "type $cmd without args"
1026 }
1027}
1028
9521ecda
SM
1029# Test the loop_break command.
1030
1031proc_with_prefix loop_break_test {} {
1032 gdb_test_no_output "set \$a = 0" "initialize \$a"
34d16ea2 1033 gdb_test_no_output "set \$total = 0" "initialize \$total"
9521ecda
SM
1034
1035 gdb_test \
34d16ea2
SM
1036 [multi_line_input \
1037 "while \$a < 5" \
1038 " if \$a == 4" \
1039 " loop_break" \
1040 " end" \
1041 " set \$b = 0" \
1042 " while \$b < 5" \
1043 " if \$b == 2" \
1044 " loop_break" \
1045 " end" \
1046 " set \$total = \$total + 1" \
1047 " set \$b = \$b + 1" \
1048 " end" \
1049 " set \$a = \$a + 1" \
1050 "end"] \
1051 "" \
1052 "run while loop"
1053
1054 gdb_test "print \$a" " = 4" "validate \$a"
1055 gdb_test "print \$b" " = 2" "validate \$b"
1056 gdb_test "print \$total" " = 8" "validate \$total"
9521ecda
SM
1057}
1058
1059# Test the loop_continue command.
1060
1061proc_with_prefix loop_continue_test {} {
1062 gdb_test_no_output "set \$a = 0" "initialize \$a"
34d16ea2 1063 gdb_test_no_output "set \$total = 0" "initialize \$total"
9521ecda
SM
1064
1065 gdb_test \
34d16ea2
SM
1066 [multi_line_input \
1067 "while \$a < 5" \
1068 " set \$a = \$a + 1" \
1069 " set \$b = 0" \
1070 " if \$a == 4" \
1071 " loop_continue" \
1072 " end" \
1073 " while \$b < 5" \
1074 " set \$b = \$b + 1" \
1075 " if \$b == 2" \
1076 " loop_continue" \
1077 " end" \
1078 " set \$total = \$total + 1" \
1079 " end" \
1080 "end"] \
1081 "" \
1082 "run while loop"
9521ecda
SM
1083
1084 gdb_test "print \$a" " = 5" "validate \$a"
34d16ea2
SM
1085 gdb_test "print \$b" " = 5" "validate \$b"
1086 gdb_test "print \$total" " = 16" "validate \$total"
9521ecda
SM
1087}
1088
6e5d74e7
PA
1089# Test an input line split with a continuation character (backslash)
1090# while entering a multi-line command (in a secondary prompt).
1091
1092proc_with_prefix backslash_in_multi_line_command_test {} {
7978d7c3
SM
1093 set dg_ver [dejagnu_version]
1094 set dg_major [lindex $dg_ver 0]
1095 set dg_minor [lindex $dg_ver 1]
1096
1097 # With older versions of DejaGnu, the "\\\n" we send gets replaced with a
1098 # space, thus breaking the test. Just skip it in that case.
1099 if { $dg_major == 1 && $dg_minor < 5 } {
1100 untested "dejagnu version is too old"
1101 return
1102 }
1103
6e5d74e7
PA
1104 gdb_breakpoint "main"
1105
1106 gdb_test_multiple "commands" "commands" {
1107 -re "End with a line saying just \"end\"\\.\r\n>$" {
1108 pass "commands"
1109 }
1110 }
1111
1112 set test "input line split with backslash"
1113 send_gdb "print \\\nargc\n"
1114 gdb_test_multiple "" $test {
1115 -re "^print \\\\\r\nargc\r\n>$" {
1116 pass $test
1117 }
1118 }
1119
1120 gdb_test_no_output "end"
1121
1122 # Input any command, just to be sure the readline state is sane.
1123 # In PR 21218, this would trigger the infamous:
1124 # readline: readline_callback_read_char() called with no handler!
1125 gdb_test "print 1" "" "run command"
1126}
1127
c906108c
SS
1128gdbvar_simple_if_test
1129gdbvar_simple_while_test
1130gdbvar_complex_if_while_test
1131progvar_simple_if_test
1132progvar_simple_while_test
1133progvar_complex_if_while_test
1134if_while_breakpoint_command_test
1135infrun_breakpoint_command_test
1136breakpoint_command_test
1137user_defined_command_test
fd437cbc 1138user_defined_command_case_sensitivity
01770bbd 1139user_defined_command_args_eval
ec835369 1140user_defined_command_args_stack_test
df3ee9ca 1141user_defined_command_manyargs_test
085dd6e6 1142watchpoint_command_test
7a292a7a 1143test_command_prompt_position
003ba290 1144deprecated_command_test
c2b8ed2c
MS
1145bp_deleted_in_command_test
1146temporary_breakpoint_commands
61d9b92f 1147stray_arg0_test
02e7ef19 1148source_file_with_indented_comment
e28493f2 1149recursive_source_test
704a4f78 1150if_commands_test
353d1d73 1151error_clears_commands_left
fad6eecd 1152redefine_hook_test
6e5d74e7 1153backslash_in_multi_line_command_test
80a65e9b 1154define_if_without_arg_test
9521ecda
SM
1155loop_break_test
1156loop_continue_test
b05dcbb7
TT
1157# This one should come last, as it redefines "backtrace".
1158redefine_backtrace_test