Commit | Line | Data |
---|---|---|
42a4f53d | 1 | # Copyright (C) 2008-2019 Free Software Foundation, Inc. |
a08702d6 TJB |
2 | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | # This file is part of the GDB testsuite. It tests the mechanism | |
17 | # exposing values to Python. | |
18 | ||
a2c09bd0 DE |
19 | load_lib gdb-python.exp |
20 | ||
b4a58790 | 21 | standard_testfile |
db8e4570 | 22 | |
673dc4a0 YQ |
23 | set has_argv0 [gdb_has_argv0] |
24 | ||
db8e4570 | 25 | # Build inferior to language specification. |
ac52f9a2 | 26 | # LANG is one of "c" or "c++". |
db8e4570 UW |
27 | proc build_inferior {exefile lang} { |
28 | global srcdir subdir srcfile testfile hex | |
29 | ||
ac52f9a2 DE |
30 | # Use different names for .o files based on the language. |
31 | # For Fission, the debug info goes in foo.dwo and we don't want, | |
32 | # for example, a C++ compile to clobber the dwo of a C compile. | |
33 | # ref: http://gcc.gnu.org/wiki/DebugFission | |
34 | switch ${lang} { | |
35 | "c" { set filename ${testfile}.o } | |
36 | "c++" { set filename ${testfile}-cxx.o } | |
37 | } | |
38 | set objfile [standard_output_file $filename] | |
39 | ||
40 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objfile}" object "debug $lang"] != "" | |
41 | || [gdb_compile "${objfile}" "${exefile}" executable "debug $lang"] != "" } { | |
84c93cd5 | 42 | untested "failed to compile in $lang mode" |
db8e4570 UW |
43 | return -1 |
44 | } | |
ac52f9a2 | 45 | return 0 |
a08702d6 TJB |
46 | } |
47 | ||
a08702d6 TJB |
48 | proc test_value_creation {} { |
49 | global gdb_prompt | |
9325cb04 | 50 | global gdb_py_is_py3k |
a08702d6 TJB |
51 | |
52 | gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1 | |
53 | gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1 | |
9325cb04 PK |
54 | if { $gdb_py_is_py3k == 0 } { |
55 | gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1 | |
56 | } | |
33fa2c6e DE |
57 | |
58 | gdb_py_test_silent_cmd "python l = gdb.Value(0xffffffff12345678)" "create large unsigned 64-bit value" 1 | |
7353f247 TT |
59 | if { $gdb_py_is_py3k == 0 } { |
60 | gdb_test "python print long(l)" "18446744069720004216" "large unsigned 64-bit int conversion to python" | |
61 | } else { | |
62 | gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python" | |
63 | } | |
33fa2c6e | 64 | |
a08702d6 TJB |
65 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1 |
66 | gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1 | |
9325cb04 PK |
67 | gdb_test "python print (a)" "\"string test\"" "print 8-bit string" |
68 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string" | |
33fa2c6e | 69 | |
9325cb04 PK |
70 | if { $gdb_py_is_py3k == 0 } { |
71 | gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1 | |
72 | gdb_test "python print (a)" "\"unicode test\"" "print Unicode string" | |
73 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of unicode string" | |
74 | } | |
c0c6f777 TJB |
75 | |
76 | # Test address attribute is None in a non-addressable value | |
cdc7edd7 | 77 | gdb_test "python print ('result = %s' % i.address)" "= None" "test address attribute in non-addressable value" |
a08702d6 TJB |
78 | } |
79 | ||
80 | proc test_value_numeric_ops {} { | |
81 | global gdb_prompt | |
1c1e54f6 | 82 | global gdb_py_is_py3k |
a08702d6 TJB |
83 | |
84 | gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0 | |
85 | gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0 | |
86 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0 | |
87 | gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0 | |
9325cb04 PK |
88 | gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values" |
89 | gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result" | |
90 | ||
91 | gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values" | |
92 | gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values" | |
93 | gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values" | |
94 | gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values" | |
95 | gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values" | |
96 | gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values" | |
97 | gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" | |
98 | gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" | |
a08702d6 TJB |
99 | # Remainder of float is implemented in Python but not in GDB's value system. |
100 | ||
9325cb04 PK |
101 | gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value" |
102 | gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value" | |
a08702d6 | 103 | |
9325cb04 PK |
104 | gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value" |
105 | gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value" | |
106 | gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value" | |
107 | gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value" | |
108 | gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value" | |
109 | gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value" | |
a08702d6 TJB |
110 | |
111 | # Test gdb.Value mixed with Python types. | |
112 | ||
9325cb04 PK |
113 | gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer" |
114 | gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result" | |
115 | gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float" | |
a08702d6 | 116 | |
9325cb04 PK |
117 | gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value" |
118 | gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value" | |
a08702d6 | 119 | |
08c637de TJB |
120 | # Conversion test. |
121 | gdb_test "print evalue" " = TWO" | |
89493308 | 122 | gdb_test_no_output "python evalue = gdb.history (0)" |
9325cb04 | 123 | gdb_test "python print (int (evalue))" "2" |
08c637de | 124 | |
a08702d6 TJB |
125 | # Test pointer arithmethic |
126 | ||
127 | # First, obtain the pointers | |
de7ff789 | 128 | gdb_test "print (void *) 2" ".*" "" |
35ec993f | 129 | gdb_test_no_output "python a = gdb.history (0)" "" |
de7ff789 | 130 | gdb_test "print (void *) 5" ".*" "" |
35ec993f | 131 | gdb_test_no_output "python b = gdb.history (0)" "" |
a08702d6 | 132 | |
f5769a2c TT |
133 | gdb_test "python print(int(b))" "5" "convert pointer to int" |
134 | if {!$gdb_py_is_py3k} { | |
135 | gdb_test "python print(long(b))" "5" "convert pointer to long" | |
136 | } | |
137 | ||
9325cb04 PK |
138 | gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer" |
139 | gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value" | |
140 | gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values" | |
a08702d6 | 141 | |
ddae9462 TT |
142 | gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \ |
143 | "result = r" "use value as string index" | |
144 | gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \ | |
145 | "result = 1" "use value as tuple index" | |
f5769a2c TT |
146 | if {!$gdb_py_is_py3k} { |
147 | gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \ | |
148 | "result = 1" "use value as array index" | |
149 | } | |
ddae9462 | 150 | |
1c1e54f6 TT |
151 | gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \ |
152 | "f+" "int conversion respect type sign" | |
153 | if {!$gdb_py_is_py3k} { | |
154 | gdb_test "python print('%x' % long(gdb.parse_and_eval('-1ull')))" \ | |
155 | "f+" "long conversion respect type sign" | |
156 | } | |
157 | ||
a08702d6 TJB |
158 | # Test some invalid operations. |
159 | ||
9325cb04 | 160 | gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" { |
a08702d6 TJB |
161 | -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"} |
162 | -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} | |
163 | -re "$gdb_prompt $" {fail "catch error in python type conversion"} | |
164 | } | |
165 | ||
9325cb04 | 166 | gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" { |
a08702d6 TJB |
167 | -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"} |
168 | -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"} | |
169 | -re "$gdb_prompt $" {fail "catch throw of GDB error"} | |
170 | } | |
171 | } | |
172 | ||
173 | proc test_value_boolean {} { | |
174 | # First, define a useful function to test booleans. | |
175 | gdb_py_test_multiple "define function to test booleans" \ | |
176 | "python" "" \ | |
177 | "def test_bool (val):" "" \ | |
178 | " if val:" "" \ | |
9325cb04 | 179 | " print ('yay')" "" \ |
a08702d6 | 180 | " else:" "" \ |
9325cb04 | 181 | " print ('nay')" "" \ |
a08702d6 TJB |
182 | "end" "" |
183 | ||
184 | gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression" | |
185 | ||
186 | gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression" | |
187 | ||
188 | gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression" | |
189 | ||
190 | gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression" | |
191 | ||
192 | gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression" | |
193 | ||
194 | gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression" | |
195 | } | |
196 | ||
197 | proc test_value_compare {} { | |
9325cb04 PK |
198 | gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal" |
199 | gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less" | |
200 | gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater" | |
201 | gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None" | |
202 | ||
203 | gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal" | |
204 | gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less" | |
205 | gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater" | |
206 | gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None" | |
207 | ||
208 | gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values" | |
209 | gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values" | |
210 | gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value" | |
211 | gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value" | |
212 | gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None" | |
213 | ||
214 | gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false" | |
215 | gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true" | |
216 | gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None" | |
217 | ||
218 | gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal" | |
219 | gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less" | |
220 | gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater" | |
221 | gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None" | |
222 | ||
223 | gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal" | |
224 | gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less" | |
225 | gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater" | |
226 | gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None" | |
a08702d6 TJB |
227 | } |
228 | ||
229 | proc test_value_in_inferior {} { | |
230 | global gdb_prompt | |
231 | global testfile | |
9325cb04 | 232 | global gdb_py_is_py3k |
a08702d6 TJB |
233 | |
234 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] | |
a08702d6 TJB |
235 | |
236 | gdb_continue_to_breakpoint "break to inspect struct and union" | |
237 | ||
238 | # Just get inferior variable s in the value history, available to python. | |
239 | gdb_test "print s" " = {a = 3, b = 5}" "" | |
240 | ||
08c637de | 241 | gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1 |
a08702d6 | 242 | |
9325cb04 PK |
243 | gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name" |
244 | if { $gdb_py_is_py3k == 0 } { | |
245 | gdb_test "python print ('result = ' + str(s\[u'a'\]))" " = 3" "access element inside struct using unicode name" | |
246 | } | |
a08702d6 TJB |
247 | |
248 | # Test dereferencing the argv pointer | |
249 | ||
250 | # Just get inferior variable argv the value history, available to python. | |
251 | gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" "" | |
252 | ||
08c637de | 253 | gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0 |
a08702d6 TJB |
254 | gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1 |
255 | ||
256 | # Check that the dereferenced value is sane | |
673dc4a0 YQ |
257 | global has_argv0 |
258 | set test "verify dereferenced value" | |
259 | if { $has_argv0 } { | |
c0ecb95f JK |
260 | gdb_test_no_output "set print elements unlimited" "" |
261 | gdb_test_no_output "set print repeats unlimited" "" | |
673dc4a0 YQ |
262 | gdb_test "python print (arg0)" "0x.*$testfile\"" $test |
263 | } else { | |
264 | unsupported $test | |
70362913 | 265 | } |
def2b000 TJB |
266 | |
267 | # Smoke-test is_optimized_out attribute | |
cdc7edd7 | 268 | gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "test is_optimized_out attribute" |
c0c6f777 TJB |
269 | |
270 | # Test address attribute | |
cdc7edd7 | 271 | gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "test address attribute" |
fbb8f299 | 272 | |
46f886f1 JK |
273 | # Test displaying a variable that is temporarily at a bad address. |
274 | # But if we can examine what's at memory address 0, then we'll also be | |
275 | # able to display it without error. Don't run the test in that case. | |
20c6f1e1 | 276 | set can_read_0 [is_address_zero_readable] |
46f886f1 | 277 | |
621c8364 | 278 | # Test memory error. |
46f886f1 JK |
279 | set test "parse_and_eval with memory error" |
280 | if {$can_read_0} { | |
281 | untested $test | |
282 | } else { | |
9325cb04 | 283 | gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
46f886f1 | 284 | } |
621c8364 | 285 | |
54d8a644 PK |
286 | # Test Python lazy value handling |
287 | set test "memory error and lazy values" | |
aa2071bd PK |
288 | if {$can_read_0} { |
289 | untested $test | |
290 | } else { | |
54d8a644 | 291 | gdb_test "python inval = gdb.parse_and_eval('*(int*)0')" |
9325cb04 | 292 | gdb_test "python print (inval.is_lazy)" "True" |
54d8a644 PK |
293 | gdb_test "python inval2 = inval+1" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
294 | gdb_test "python inval.fetch_lazy ()" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test | |
aa2071bd | 295 | } |
4dac951e | 296 | set argc_value [get_integer_valueof "argc" 0] |
aa2071bd | 297 | gdb_test "python argc_lazy = gdb.parse_and_eval('argc')" |
54d8a644 PK |
298 | gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')" |
299 | gdb_test "python argc_notlazy.fetch_lazy()" | |
9325cb04 PK |
300 | gdb_test "python print (argc_lazy.is_lazy)" "True" |
301 | gdb_test "python print (argc_notlazy.is_lazy)" "False" | |
4dac951e | 302 | gdb_test "print argc" " = $argc_value" "sanity check argc" |
9325cb04 | 303 | gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue" |
4dac951e LM |
304 | gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc" |
305 | gdb_test "python print (argc_notlazy)" "\r\n$argc_value" | |
306 | gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]" | |
9325cb04 | 307 | gdb_test "python print (argc_lazy.is_lazy)" "False" |
aa2071bd | 308 | |
fbb8f299 PM |
309 | # Test string fetches, both partial and whole. |
310 | gdb_test "print st" "\"divide et impera\"" | |
311 | gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 | |
9325cb04 | 312 | gdb_test "python print (st.string ())" "divide et impera" "Test string with no length" |
cdc7edd7 | 313 | gdb_test "python print (st.string (length = -1))" "divide et impera" "test string (length = -1) is all of the string" |
9325cb04 | 314 | gdb_test "python print (st.string (length = 6))" "divide" |
cdc7edd7 LM |
315 | gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "test string (length = 0) is empty" |
316 | gdb_test "python print (len(st.string (length = 0)))" "0" "test length is 0" | |
fbb8f299 PM |
317 | |
318 | ||
319 | # Fetch a string that has embedded nulls. | |
320 | gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*" | |
321 | gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value from history" 1 | |
cdc7edd7 | 322 | gdb_test "python print (nullst.string ())" "divide" "test string to first null" |
fbb8f299 PM |
323 | # Python cannot print strings that contain the null (\0) character. |
324 | # For the purposes of this test, use repr() | |
325 | gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1 | |
9325cb04 | 326 | gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'" |
0987cf35 DE |
327 | |
328 | # Test fetching a string longer than its declared (in C) size. | |
329 | # PR 16286 | |
330 | gdb_py_test_silent_cmd "python xstr = gdb.parse_and_eval('xstr')" "get xstr" 1 | |
d7fc3181 | 331 | gdb_test "python print(xstr\['text'\].string (length = xstr\['length'\]))" "x{100}" \ |
0987cf35 | 332 | "read string beyond declared size" |
a08702d6 TJB |
333 | } |
334 | ||
5374244e PM |
335 | proc test_inferior_function_call {} { |
336 | global gdb_prompt hex decimal | |
337 | ||
338 | # Correct inferior call without arguments. | |
339 | gdb_test "p/x fp1" " = $hex.*" | |
340 | gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value from history" 1 | |
341 | gdb_test "python fp1 = fp1.dereference()" "" | |
342 | gdb_test "python result = fp1()" "" | |
9325cb04 | 343 | gdb_test "python print (result)" "void" |
5374244e PM |
344 | |
345 | # Correct inferior call with arguments. | |
346 | gdb_test "p/x fp2" " = $hex.*" | |
347 | gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value from history" 1 | |
348 | gdb_test "python fp2 = fp2.dereference()" "" | |
349 | gdb_test "python result2 = fp2(10,20)" "" | |
9325cb04 | 350 | gdb_test "python print (result2)" "30" |
5374244e PM |
351 | |
352 | # Incorrect to call an int value. | |
353 | gdb_test "p i" " = $decimal.*" | |
354 | gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value from history" 1 | |
355 | gdb_test "python result3 = i()" ".*Value is not callable.*" | |
356 | ||
357 | # Incorrect number of arguments. | |
358 | gdb_test "p/x fp2" " = $hex.*" | |
359 | gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value from history" 1 | |
360 | gdb_test "python fp3 = fp3.dereference()" "" | |
361 | gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*" | |
362 | } | |
363 | ||
89c73ade TT |
364 | # A few objfile tests. |
365 | proc test_objfiles {} { | |
9325cb04 | 366 | gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'py-value' in file.filename:\n ok=True\nprint (ok)\nend" "True" \ |
90556b8c | 367 | "py-value in file.filename" |
89c73ade | 368 | |
9325cb04 | 369 | gdb_test "python print (gdb.objfiles()\[0\].pretty_printers)" "\\\[\\\]" |
89c73ade TT |
370 | |
371 | gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \ | |
372 | "pretty_printers attribute must be a list.*Error while executing Python code." | |
373 | } | |
a08702d6 | 374 | |
2c74e833 TT |
375 | proc test_value_after_death {} { |
376 | # Construct a type while the inferior is still running. | |
377 | gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \ | |
378 | "create PTR type" 1 | |
379 | ||
380 | # Kill the inferior and remove the symbols. | |
381 | gdb_test "kill" "" "kill the inferior" \ | |
382 | "Kill the program being debugged. .y or n. $" \ | |
383 | "y" | |
cdc7edd7 | 384 | gdb_test "file" "" "discard the symbols" \ |
2c74e833 TT |
385 | "Discard symbol table from.*y or n. $" \ |
386 | "y" | |
387 | ||
388 | # Now create a value using that type. Relies on arg0, created by | |
389 | # test_value_in_inferior. | |
390 | gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \ | |
391 | "cast arg0 to PTR" 1 | |
392 | ||
393 | # Make sure the type is deleted. | |
394 | gdb_py_test_silent_cmd "python ptrtype = None" \ | |
395 | "delete PTR type" 1 | |
396 | ||
397 | # Now see if the value's type is still valid. | |
9325cb04 | 398 | gdb_test "python print (castval.type)" "PTR ." \ |
2c74e833 TT |
399 | "print value's type" |
400 | } | |
401 | ||
2e4d963f PM |
402 | # Regression test for invalid subscript operations. The bug was that |
403 | # the type of the value was not being checked before allowing a | |
404 | # subscript operation to proceed. | |
405 | ||
db8e4570 | 406 | proc test_subscript_regression {exefile lang} { |
2e4d963f | 407 | # Start with a fresh gdb. |
d54b30bb | 408 | clean_restart ${exefile} |
2e4d963f PM |
409 | |
410 | if ![runto_main ] then { | |
411 | perror "couldn't run to breakpoint" | |
412 | return | |
413 | } | |
414 | ||
415 | if {$lang == "c++"} { | |
416 | gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"] | |
417 | gdb_continue_to_breakpoint "break to inspect pointer by reference" | |
418 | ||
419 | gdb_py_test_silent_cmd "print rptr_int" \ | |
420 | "Obtain address" 1 | |
421 | gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \ | |
422 | "Obtains value from GDB" 1 | |
cdc7edd7 | 423 | gdb_test "python print (rptr\[0\])" "2" "check pointer passed as reference" |
f9ffd4bb TT |
424 | |
425 | # Just the most basic test of dynamic_cast -- it is checked in | |
426 | # the C++ tests. | |
9325cb04 | 427 | gdb_test "python print (bool(gdb.parse_and_eval('base').dynamic_cast(gdb.lookup_type('Derived').pointer())))" \ |
f9ffd4bb | 428 | True |
03f17ccf TT |
429 | |
430 | # Likewise. | |
9325cb04 | 431 | gdb_test "python print (gdb.parse_and_eval('base').dynamic_type)" \ |
03f17ccf | 432 | "Derived \[*\]" |
7af389b8 SC |
433 | gdb_test "python print (gdb.parse_and_eval('base_ref').dynamic_type)" \ |
434 | "Derived \[&\]" | |
03f17ccf | 435 | # A static type case. |
9325cb04 | 436 | gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \ |
03f17ccf | 437 | "int" |
2e4d963f PM |
438 | } |
439 | ||
440 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] | |
441 | gdb_continue_to_breakpoint "break to inspect struct and union" | |
442 | ||
443 | gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \ | |
444 | "Create a value for subscript test" 1 | |
445 | gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \ | |
446 | "Create a value for subscript test" 1 | |
447 | ||
448 | # Try to access an int with a subscript. This should fail. | |
cdc7edd7 | 449 | gdb_test "python print (intv)" "1" "baseline print of an int Python value" |
9325cb04 | 450 | gdb_test "python print (intv\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
2e4d963f PM |
451 | "Attempt to access an integer with a subscript" |
452 | ||
453 | # Try to access a string with a subscript. This should pass. | |
cdc7edd7 LM |
454 | gdb_test "python print (stringv)" "foo." "baseline print of a string Python value" |
455 | gdb_test "python print (stringv\[0\])" "f." "attempt to access a string with a subscript" | |
2e4d963f PM |
456 | |
457 | # Try to access an int array via a pointer with a subscript. This should pass. | |
458 | gdb_py_test_silent_cmd "print p" "Build pointer to array" 1 | |
459 | gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "" 1 | |
cdc7edd7 LM |
460 | gdb_test "python print (pointer\[0\])" "1" "access array via pointer with int subscript" |
461 | gdb_test "python print (pointer\[intv\])" "2" "access array via pointer with value subscript" | |
2e4d963f PM |
462 | |
463 | # Try to access a single dimension array with a subscript to the | |
464 | # result. This should fail. | |
9325cb04 | 465 | gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
2e4d963f PM |
466 | "Attempt to access an integer with a subscript" |
467 | ||
468 | # Lastly, test subscript access to an array with multiple | |
469 | # dimensions. This should pass. | |
470 | gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1 | |
471 | gdb_py_test_silent_cmd "python marray = gdb.history(0)" "" 1 | |
cdc7edd7 | 472 | gdb_test "python print (marray\[1\]\[2\])" "o." "test multiple subscript" |
2e4d963f PM |
473 | } |
474 | ||
57a1d736 TT |
475 | # A few tests of gdb.parse_and_eval. |
476 | proc test_parse_and_eval {} { | |
9325cb04 | 477 | gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \ |
57a1d736 | 478 | "parse_and_eval constant test" |
9325cb04 | 479 | gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \ |
57a1d736 | 480 | "parse_and_eval simple expression test" |
9325cb04 PK |
481 | gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \ |
482 | ".(type|class) 'gdb.Value'."\ | |
57a1d736 TT |
483 | "parse_and_eval type test" |
484 | } | |
485 | ||
88d4aea7 PM |
486 | # Test that values are hashable. |
487 | proc test_value_hash {} { | |
488 | gdb_py_test_multiple "Simple Python value dictionary" \ | |
489 | "python" "" \ | |
490 | "one = gdb.Value(1)" "" \ | |
491 | "two = gdb.Value(2)" "" \ | |
492 | "three = gdb.Value(3)" "" \ | |
493 | "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \ | |
494 | "end" | |
cdc7edd7 LM |
495 | gdb_test "python print (vdict\[one\])" "one str" "test dictionary hash" |
496 | gdb_test "python print (vdict\[two\])" "two str" "test dictionary hash" | |
497 | gdb_test "python print (vdict\[three\])" "three str" "test dictionary hash" | |
498 | gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash" | |
88d4aea7 PM |
499 | } |
500 | ||
fb4fa946 TT |
501 | proc test_float_conversion {} { |
502 | global gdb_py_is_py3k | |
503 | gdb_test "python print(int(gdb.Value(0)))" "0" | |
504 | gdb_test "python print(int(gdb.Value(2.5)))" "2" | |
505 | if {!$gdb_py_is_py3k} { | |
506 | gdb_test "python print(long(gdb.Value(0)))" "0" | |
507 | gdb_test "python print(long(gdb.Value(2.5)))" "2" | |
508 | } | |
509 | gdb_test "python print(float(gdb.Value(2.5)))" "2\\.5" | |
510 | gdb_test "python print(float(gdb.Value(0)))" "0\\.0" | |
511 | } | |
512 | ||
bc2a507e KB |
513 | proc test_value_from_buffer {} { |
514 | global gdb_prompt | |
515 | global gdb_py_is_py3k | |
516 | ||
517 | gdb_py_test_silent_cmd "python tp=gdb.lookup_type('int')" "look up int type" 0 | |
518 | gdb_py_test_silent_cmd "python size_a=gdb.parse_and_eval('sizeof(a)')" \ | |
519 | "find size of a" 0 | |
520 | gdb_py_test_silent_cmd "python size_a0=gdb.parse_and_eval('sizeof(a\[0\])')" \ | |
521 | "find size of element of a" 0 | |
522 | gdb_py_test_silent_cmd "python addr=gdb.parse_and_eval('&a')" \ | |
523 | "find address of a" 0 | |
524 | gdb_py_test_silent_cmd "python b=gdb.selected_inferior().read_memory(addr,size_a)" \ | |
525 | "read buffer from memory" 1 | |
526 | gdb_test "python v=gdb.Value(b,tp); print(v)" "1" \ | |
527 | "construct value from buffer" | |
528 | gdb_test "python v=gdb.Value(b\[size_a0:\],tp); print(v)" "2" \ | |
529 | "convert 2nd elem of buffer to value" | |
530 | gdb_test "python v=gdb.Value(b\[2*size_a0:\],tp); print(v)" "3" \ | |
531 | "convert 3rd elem of buffer to value" | |
532 | gdb_test "python v=gdb.Value(b\[2*size_a0+1:\],tp); print(v)" \ | |
533 | "TypeError: Size of type is larger than that of buffer object\..*" \ | |
534 | "attempt to convert smaller buffer than size of type" | |
535 | gdb_py_test_silent_cmd "python atp=tp.array(2) ; print(atp)" \ | |
536 | "make array type" 0 | |
537 | gdb_py_test_silent_cmd "python va=gdb.Value(b,atp)" \ | |
538 | "construct array value from buffer" 0 | |
539 | gdb_test "python print(va)" "\\{1, 2, 3\\}" "print array value" | |
540 | gdb_test "python print(va\[0\])" "1" "print first array element" | |
541 | gdb_test "python print(va\[1\])" "2" "print second array element" | |
542 | gdb_test "python print(va\[2\])" "3" "print third array element" | |
543 | gdb_test "python print(va\[3\])" "gdb\.error: no such vector element.*" \ | |
544 | "print out of bounds array element" | |
545 | gdb_py_test_silent_cmd "python atpbig=tp.array(3)" "make bigger array type" 0 | |
546 | gdb_test "python vabig=gdb.Value(b,atpbig)" \ | |
547 | "ValueError: Size of type is larger than that of buffer object\..*" \ | |
548 | "attempt to construct large value with small buffer" | |
549 | gdb_test "python v=gdb.Value(2048,tp)" \ | |
550 | "TypeError: Object must support the python buffer protocol\..*" \ | |
551 | "attempt to construct value from buffer with non-buffer object" | |
552 | gdb_test "python v=gdb.Value(b,'int'); print(v)" \ | |
553 | "TypeError: type argument must be a gdb\.Type\..*" \ | |
554 | "attempt to construct value with string as type" | |
555 | } | |
556 | ||
ac52f9a2 DE |
557 | # Build C version of executable. C++ is built later. |
558 | if { [build_inferior "${binfile}" "c"] < 0 } { | |
559 | return -1 | |
560 | } | |
88d4aea7 | 561 | |
a08702d6 | 562 | # Start with a fresh gdb. |
d54b30bb | 563 | clean_restart ${binfile} |
a08702d6 | 564 | |
f6bbabf0 PM |
565 | # Skip all tests if Python scripting is not enabled. |
566 | if { [skip_python_tests] } { continue } | |
a08702d6 TJB |
567 | |
568 | test_value_creation | |
569 | test_value_numeric_ops | |
570 | test_value_boolean | |
571 | test_value_compare | |
89c73ade | 572 | test_objfiles |
57a1d736 | 573 | test_parse_and_eval |
88d4aea7 | 574 | test_value_hash |
fb4fa946 | 575 | test_float_conversion |
adc13a14 PA |
576 | |
577 | # The following tests require execution. | |
578 | ||
579 | if ![runto_main] then { | |
bc6c7af4 | 580 | fail "can't run to main" |
adc13a14 PA |
581 | return 0 |
582 | } | |
583 | ||
a08702d6 | 584 | test_value_in_inferior |
bc2a507e | 585 | test_value_from_buffer |
5374244e | 586 | test_inferior_function_call |
2c74e833 | 587 | test_value_after_death |
2e4d963f | 588 | |
db8e4570 | 589 | # Test either C or C++ values. |
ac52f9a2 | 590 | |
db8e4570 | 591 | test_subscript_regression "${binfile}" "c" |
ac52f9a2 DE |
592 | |
593 | if ![skip_cplus_tests] { | |
594 | if { [build_inferior "${binfile}-cxx" "c++"] < 0 } { | |
595 | return -1 | |
596 | } | |
35720eaa DE |
597 | with_test_prefix "c++" { |
598 | test_subscript_regression "${binfile}-cxx" "c++" | |
599 | } | |
ac52f9a2 | 600 | } |