1 # Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
21 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
25 proc test_convenience_variables {} {
28 gdb_test_no_output "set \$foo = 101" \
29 "Set a new convenience variable"
31 gdb_test "print \$foo" " = 101" \
32 "Print contents of new convenience variable"
34 gdb_test_no_output "set \$foo = 301" \
35 "Set convenience variable to a new value"
37 gdb_test "print \$foo" " = 301" \
38 "Print new contents of convenience variable"
40 gdb_test_no_output "set \$_ = 11" \
41 "Set convenience variable \$_"
43 gdb_test "print \$_" " = 11" \
44 "Print contents of convenience variable \$_"
46 gdb_test "print \$foo + 10" " = 311" \
47 "Use convenience variable in arithmetic expression"
49 gdb_test "print (\$foo = 32) + 4" " = 36" \
50 "Use convenience variable assignment in arithmetic expression"
52 gdb_test "print \$bar" " = void" \
53 "Print contents of uninitialized convenience variable"
56 gdb_test "print \$str = \"some string\"" \
57 " = \"some string\"" \
58 "Set convenience variable to string value"
59 gdb_test "print \$str = \"some other string\"" \
60 " = \"some other string\"" \
61 "Change convenience variable to different string value"
63 gdb_test "print \$arr = {1, 2, 3}" \
65 "Set convenience variable to array value"
66 gdb_test "print \$arr = {0, 1, 2, 3}" \
67 " = \\{0, 1, 2, 3\\}" \
68 "Set convenience variable to different array value"
70 gdb_test "print \$ptr = (void *) 7" \
71 " = \\(void \\*\\) 0x7" \
72 "set convenience variable to pointer value"
73 # This used to "succeed".
74 gdb_test "print \$ptr + 23 = 97" \
75 "Left operand of assignment is not an lvalue." \
76 "reject invalid assignment"
79 proc test_convenience_functions {} {
80 gdb_test "print \$_isvoid" " = <internal function _isvoid>" \
81 "Print internal function \$_isvoid"
83 gdb_test "print \$isvoid_foo" " = void" \
84 "Print void convenience variable"
86 gdb_test "print \$_isvoid (\$isvoid_foo)" " = 1" \
87 "Check whether void convenience variable is void"
89 gdb_test_no_output "set \$isvoid_foo = 1" \
90 "Set void convenience variable to 1"
92 gdb_test "print \$_isvoid (\$isvoid_foo)" " = 0" \
93 "Check whether non-void convenience variable is void"
95 # For the next test, we need the inferior to be running.
96 if { ![runto_main] } {
100 gdb_test "print \$_isvoid (foo_void ())" " = 1" \
101 "Check whether void function is void"
103 gdb_test "print \$_isvoid (foo_int ())" " = 0" \
104 "Check whether non-void function is void"
107 proc test_value_history {} {
110 gdb_test "print 101" "\\\$1 = 101" \
111 "Set value-history\[1\] using \$1"
113 gdb_test "print 102" "\\\$2 = 102" \
114 "Set value-history\[2\] using \$2"
116 gdb_test "print 103" "\\\$3 = 103" \
117 "Set value-history\[3\] using \$3"
119 gdb_test "print \$\$" "\\\$4 = 102" \
120 "Print value-history\[MAX-1\] using inplicit index \$\$"
122 gdb_test "print \$\$" "\\\$5 = 103" \
123 "Print value-history\[MAX-1\] again using implicit index \$\$"
125 gdb_test "print \$" "\\\$6 = 103" \
126 "Print value-history\[MAX\] using implicit index \$"
128 gdb_test "print \$\$2" "\\\$7 = 102" \
129 "Print value-history\[MAX-2\] using explicit index \$\$2"
131 gdb_test "print \$0" "\\\$8 = 102" \
132 "Print value-history\[MAX\] using explicit index \$0"
134 gdb_test "print 108" "\\\$9 = 108"
136 gdb_test "print \$\$0" "\\\$10 = 108" \
137 "Print value-history\[MAX\] using explicit index \$\$0"
139 gdb_test "print \$1" "\\\$11 = 101" \
140 "Print value-history\[1\] using explicit index \$1"
142 gdb_test "print \$2" "\\\$12 = 102" \
143 "Print value-history\[2\] using explicit index \$2"
145 gdb_test "print \$3" "\\\$13 = 103" \
146 "Print value-history\[3\] using explicit index \$3"
148 gdb_test "print \$-3" "\\\$14 = 100" \
149 "Print (value-history\[MAX\] - 3) using implicit index \$"
151 gdb_test "print \$1 + 3" "\\\$15 = 104" \
152 "Use value-history element in arithmetic expression"
155 proc test_with_program {} {
157 gdb_test_no_output "set \$prog_var = p" \
158 "Set a new convenience variable to a program variable"
159 gdb_test "print /x \$prog_var" " = $hex" \
160 "Print contents of new convenience variable of program variable"
162 # Test $_thread/$_inferior convenience variables in various commands.
163 gdb_test "info threads \$_thread" "\\* 1 .* main.*"
164 gdb_test "info inferiors \$_inferior" "\\* 1 .* process.*"
165 gdb_test "break foo_int thread \$_thread" "Breakpoint \[0-9\]*.*"
168 gdb_test_no_output "set print sevenbit-strings"
171 test_convenience_variables
172 test_convenience_functions