Commit | Line | Data |
---|---|---|
618f726f | 1 | # Copyright (C) 2008-2016 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"] != "" } { | |
db8e4570 UW |
42 | untested "Couldn't compile ${srcfile} in $lang mode" |
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 | |
59 | gdb_test "python print long(l)" "18446744069720004216" "large unsigned 64-bit int conversion to python" | |
60 | ||
a08702d6 TJB |
61 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1 |
62 | gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1 | |
9325cb04 PK |
63 | gdb_test "python print (a)" "\"string test\"" "print 8-bit string" |
64 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string" | |
33fa2c6e | 65 | |
9325cb04 PK |
66 | if { $gdb_py_is_py3k == 0 } { |
67 | gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1 | |
68 | gdb_test "python print (a)" "\"unicode test\"" "print Unicode string" | |
69 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of unicode string" | |
70 | } | |
c0c6f777 TJB |
71 | |
72 | # Test address attribute is None in a non-addressable value | |
9325cb04 | 73 | gdb_test "python print ('result = %s' % i.address)" "= None" "Test address attribute in non-addressable value" |
a08702d6 TJB |
74 | } |
75 | ||
76 | proc test_value_numeric_ops {} { | |
77 | global gdb_prompt | |
78 | ||
79 | gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0 | |
80 | gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0 | |
81 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0 | |
82 | gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0 | |
9325cb04 PK |
83 | gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values" |
84 | gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result" | |
85 | ||
86 | gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values" | |
87 | gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values" | |
88 | gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values" | |
89 | gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values" | |
90 | gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values" | |
91 | gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values" | |
92 | gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" | |
93 | gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" | |
a08702d6 TJB |
94 | # Remainder of float is implemented in Python but not in GDB's value system. |
95 | ||
9325cb04 PK |
96 | gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value" |
97 | gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value" | |
a08702d6 | 98 | |
9325cb04 PK |
99 | gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value" |
100 | gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value" | |
101 | gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value" | |
102 | gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value" | |
103 | gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value" | |
104 | gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value" | |
a08702d6 TJB |
105 | |
106 | # Test gdb.Value mixed with Python types. | |
107 | ||
9325cb04 PK |
108 | gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer" |
109 | gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result" | |
110 | gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float" | |
a08702d6 | 111 | |
9325cb04 PK |
112 | gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value" |
113 | gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value" | |
a08702d6 | 114 | |
08c637de TJB |
115 | # Conversion test. |
116 | gdb_test "print evalue" " = TWO" | |
89493308 | 117 | gdb_test_no_output "python evalue = gdb.history (0)" |
9325cb04 | 118 | gdb_test "python print (int (evalue))" "2" |
08c637de | 119 | |
a08702d6 TJB |
120 | # Test pointer arithmethic |
121 | ||
122 | # First, obtain the pointers | |
de7ff789 | 123 | gdb_test "print (void *) 2" ".*" "" |
35ec993f | 124 | gdb_test_no_output "python a = gdb.history (0)" "" |
de7ff789 | 125 | gdb_test "print (void *) 5" ".*" "" |
35ec993f | 126 | gdb_test_no_output "python b = gdb.history (0)" "" |
a08702d6 | 127 | |
9325cb04 PK |
128 | gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer" |
129 | gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value" | |
130 | gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values" | |
a08702d6 | 131 | |
ddae9462 TT |
132 | gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \ |
133 | "result = r" "use value as string index" | |
134 | gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \ | |
135 | "result = 1" "use value as tuple index" | |
136 | gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \ | |
137 | "result = 1" "use value as array index" | |
138 | ||
a08702d6 TJB |
139 | # Test some invalid operations. |
140 | ||
9325cb04 | 141 | gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" { |
a08702d6 TJB |
142 | -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"} |
143 | -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} | |
144 | -re "$gdb_prompt $" {fail "catch error in python type conversion"} | |
145 | } | |
146 | ||
9325cb04 | 147 | gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" { |
a08702d6 TJB |
148 | -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"} |
149 | -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"} | |
150 | -re "$gdb_prompt $" {fail "catch throw of GDB error"} | |
151 | } | |
152 | } | |
153 | ||
154 | proc test_value_boolean {} { | |
155 | # First, define a useful function to test booleans. | |
156 | gdb_py_test_multiple "define function to test booleans" \ | |
157 | "python" "" \ | |
158 | "def test_bool (val):" "" \ | |
159 | " if val:" "" \ | |
9325cb04 | 160 | " print ('yay')" "" \ |
a08702d6 | 161 | " else:" "" \ |
9325cb04 | 162 | " print ('nay')" "" \ |
a08702d6 TJB |
163 | "end" "" |
164 | ||
165 | gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression" | |
166 | ||
167 | gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression" | |
168 | ||
169 | gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression" | |
170 | ||
171 | gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression" | |
172 | ||
173 | gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression" | |
174 | ||
175 | gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression" | |
176 | } | |
177 | ||
178 | proc test_value_compare {} { | |
9325cb04 PK |
179 | gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal" |
180 | gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less" | |
181 | gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater" | |
182 | gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None" | |
183 | ||
184 | gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal" | |
185 | gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less" | |
186 | gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater" | |
187 | gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None" | |
188 | ||
189 | gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values" | |
190 | gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values" | |
191 | gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value" | |
192 | gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value" | |
193 | gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None" | |
194 | ||
195 | gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false" | |
196 | gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true" | |
197 | gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None" | |
198 | ||
199 | gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal" | |
200 | gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less" | |
201 | gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater" | |
202 | gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None" | |
203 | ||
204 | gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal" | |
205 | gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less" | |
206 | gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater" | |
207 | gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None" | |
a08702d6 TJB |
208 | } |
209 | ||
210 | proc test_value_in_inferior {} { | |
211 | global gdb_prompt | |
212 | global testfile | |
9325cb04 | 213 | global gdb_py_is_py3k |
a08702d6 TJB |
214 | |
215 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] | |
a08702d6 TJB |
216 | |
217 | gdb_continue_to_breakpoint "break to inspect struct and union" | |
218 | ||
219 | # Just get inferior variable s in the value history, available to python. | |
220 | gdb_test "print s" " = {a = 3, b = 5}" "" | |
221 | ||
08c637de | 222 | gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1 |
a08702d6 | 223 | |
9325cb04 PK |
224 | gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name" |
225 | if { $gdb_py_is_py3k == 0 } { | |
226 | gdb_test "python print ('result = ' + str(s\[u'a'\]))" " = 3" "access element inside struct using unicode name" | |
227 | } | |
a08702d6 TJB |
228 | |
229 | # Test dereferencing the argv pointer | |
230 | ||
231 | # Just get inferior variable argv the value history, available to python. | |
232 | gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" "" | |
233 | ||
08c637de | 234 | gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0 |
a08702d6 TJB |
235 | gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1 |
236 | ||
237 | # Check that the dereferenced value is sane | |
673dc4a0 YQ |
238 | global has_argv0 |
239 | set test "verify dereferenced value" | |
240 | if { $has_argv0 } { | |
c0ecb95f JK |
241 | gdb_test_no_output "set print elements unlimited" "" |
242 | gdb_test_no_output "set print repeats unlimited" "" | |
673dc4a0 YQ |
243 | gdb_test "python print (arg0)" "0x.*$testfile\"" $test |
244 | } else { | |
245 | unsupported $test | |
70362913 | 246 | } |
def2b000 TJB |
247 | |
248 | # Smoke-test is_optimized_out attribute | |
9325cb04 | 249 | gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "Test is_optimized_out attribute" |
c0c6f777 TJB |
250 | |
251 | # Test address attribute | |
9325cb04 | 252 | gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "Test address attribute" |
fbb8f299 | 253 | |
46f886f1 JK |
254 | # Test displaying a variable that is temporarily at a bad address. |
255 | # But if we can examine what's at memory address 0, then we'll also be | |
256 | # able to display it without error. Don't run the test in that case. | |
20c6f1e1 | 257 | set can_read_0 [is_address_zero_readable] |
46f886f1 | 258 | |
621c8364 | 259 | # Test memory error. |
46f886f1 JK |
260 | set test "parse_and_eval with memory error" |
261 | if {$can_read_0} { | |
262 | untested $test | |
263 | } else { | |
9325cb04 | 264 | gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
46f886f1 | 265 | } |
621c8364 | 266 | |
54d8a644 PK |
267 | # Test Python lazy value handling |
268 | set test "memory error and lazy values" | |
aa2071bd PK |
269 | if {$can_read_0} { |
270 | untested $test | |
271 | } else { | |
54d8a644 | 272 | gdb_test "python inval = gdb.parse_and_eval('*(int*)0')" |
9325cb04 | 273 | gdb_test "python print (inval.is_lazy)" "True" |
54d8a644 PK |
274 | gdb_test "python inval2 = inval+1" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
275 | gdb_test "python inval.fetch_lazy ()" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test | |
aa2071bd | 276 | } |
4dac951e | 277 | set argc_value [get_integer_valueof "argc" 0] |
aa2071bd | 278 | gdb_test "python argc_lazy = gdb.parse_and_eval('argc')" |
54d8a644 PK |
279 | gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')" |
280 | gdb_test "python argc_notlazy.fetch_lazy()" | |
9325cb04 PK |
281 | gdb_test "python print (argc_lazy.is_lazy)" "True" |
282 | gdb_test "python print (argc_notlazy.is_lazy)" "False" | |
4dac951e | 283 | gdb_test "print argc" " = $argc_value" "sanity check argc" |
9325cb04 | 284 | gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue" |
4dac951e LM |
285 | gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc" |
286 | gdb_test "python print (argc_notlazy)" "\r\n$argc_value" | |
287 | gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]" | |
9325cb04 | 288 | gdb_test "python print (argc_lazy.is_lazy)" "False" |
aa2071bd | 289 | |
fbb8f299 PM |
290 | # Test string fetches, both partial and whole. |
291 | gdb_test "print st" "\"divide et impera\"" | |
292 | gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 | |
9325cb04 PK |
293 | gdb_test "python print (st.string ())" "divide et impera" "Test string with no length" |
294 | gdb_test "python print (st.string (length = -1))" "divide et impera" "Test string (length = -1) is all of the string" | |
295 | gdb_test "python print (st.string (length = 6))" "divide" | |
296 | gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "Test string (length = 0) is empty" | |
297 | gdb_test "python print (len(st.string (length = 0)))" "0" "Test length is 0" | |
fbb8f299 PM |
298 | |
299 | ||
300 | # Fetch a string that has embedded nulls. | |
301 | gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*" | |
302 | gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value from history" 1 | |
9325cb04 | 303 | gdb_test "python print (nullst.string ())" "divide" "Test string to first null" |
fbb8f299 PM |
304 | # Python cannot print strings that contain the null (\0) character. |
305 | # For the purposes of this test, use repr() | |
306 | gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1 | |
9325cb04 | 307 | gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'" |
0987cf35 DE |
308 | |
309 | # Test fetching a string longer than its declared (in C) size. | |
310 | # PR 16286 | |
311 | gdb_py_test_silent_cmd "python xstr = gdb.parse_and_eval('xstr')" "get xstr" 1 | |
d7fc3181 | 312 | gdb_test "python print(xstr\['text'\].string (length = xstr\['length'\]))" "x{100}" \ |
0987cf35 | 313 | "read string beyond declared size" |
a08702d6 TJB |
314 | } |
315 | ||
be759fcf PM |
316 | proc test_lazy_strings {} { |
317 | ||
318 | global hex | |
319 | ||
320 | gdb_test "print sptr" "\"pointer\"" | |
321 | gdb_py_test_silent_cmd "python sptr = gdb.history (0)" "Get value from history" 1 | |
322 | ||
323 | gdb_py_test_silent_cmd "python lstr = sptr.lazy_string()" "Aquire lazy string" 1 | |
35720eaa DE |
324 | gdb_test "python print (lstr.type)" "const char \*." "Test lazy-string type name equality" |
325 | gdb_test "python print (sptr.type)" "const char \*." "Test string type name equality" | |
3881fb67 YQ |
326 | |
327 | # Prevent symbol on address 0x0 being printed. | |
328 | gdb_test_no_output "set print symbol off" | |
fff5cc64 | 329 | gdb_test "print sn" "0x0" |
3881fb67 | 330 | |
fff5cc64 PM |
331 | gdb_py_test_silent_cmd "python snptr = gdb.history (0)" "Get value from history" 1 |
332 | gdb_test "python snstr = snptr.lazy_string(length=5)" ".*Cannot create a lazy string with address.*" "Test lazy string" | |
333 | gdb_py_test_silent_cmd "python snstr = snptr.lazy_string(length=0)" "Succesfully create a lazy string" 1 | |
9325cb04 PK |
334 | gdb_test "python print (snstr.length)" "0" "Test lazy string length" |
335 | gdb_test "python print (snstr.address)" "0" "Test lazy string address" | |
be759fcf PM |
336 | } |
337 | ||
338 | ||
5374244e PM |
339 | proc test_inferior_function_call {} { |
340 | global gdb_prompt hex decimal | |
341 | ||
342 | # Correct inferior call without arguments. | |
343 | gdb_test "p/x fp1" " = $hex.*" | |
344 | gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value from history" 1 | |
345 | gdb_test "python fp1 = fp1.dereference()" "" | |
346 | gdb_test "python result = fp1()" "" | |
9325cb04 | 347 | gdb_test "python print (result)" "void" |
5374244e PM |
348 | |
349 | # Correct inferior call with arguments. | |
350 | gdb_test "p/x fp2" " = $hex.*" | |
351 | gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value from history" 1 | |
352 | gdb_test "python fp2 = fp2.dereference()" "" | |
353 | gdb_test "python result2 = fp2(10,20)" "" | |
9325cb04 | 354 | gdb_test "python print (result2)" "30" |
5374244e PM |
355 | |
356 | # Incorrect to call an int value. | |
357 | gdb_test "p i" " = $decimal.*" | |
358 | gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value from history" 1 | |
359 | gdb_test "python result3 = i()" ".*Value is not callable.*" | |
360 | ||
361 | # Incorrect number of arguments. | |
362 | gdb_test "p/x fp2" " = $hex.*" | |
363 | gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value from history" 1 | |
364 | gdb_test "python fp3 = fp3.dereference()" "" | |
365 | gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*" | |
366 | } | |
367 | ||
89c73ade TT |
368 | # A few objfile tests. |
369 | proc test_objfiles {} { | |
9325cb04 | 370 | 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 | 371 | "py-value in file.filename" |
89c73ade | 372 | |
9325cb04 | 373 | gdb_test "python print (gdb.objfiles()\[0\].pretty_printers)" "\\\[\\\]" |
89c73ade TT |
374 | |
375 | gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \ | |
376 | "pretty_printers attribute must be a list.*Error while executing Python code." | |
377 | } | |
a08702d6 | 378 | |
2c74e833 TT |
379 | proc test_value_after_death {} { |
380 | # Construct a type while the inferior is still running. | |
381 | gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \ | |
382 | "create PTR type" 1 | |
383 | ||
384 | # Kill the inferior and remove the symbols. | |
385 | gdb_test "kill" "" "kill the inferior" \ | |
386 | "Kill the program being debugged. .y or n. $" \ | |
387 | "y" | |
388 | gdb_test "file" "" "Discard the symbols" \ | |
389 | "Discard symbol table from.*y or n. $" \ | |
390 | "y" | |
391 | ||
392 | # Now create a value using that type. Relies on arg0, created by | |
393 | # test_value_in_inferior. | |
394 | gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \ | |
395 | "cast arg0 to PTR" 1 | |
396 | ||
397 | # Make sure the type is deleted. | |
398 | gdb_py_test_silent_cmd "python ptrtype = None" \ | |
399 | "delete PTR type" 1 | |
400 | ||
401 | # Now see if the value's type is still valid. | |
9325cb04 | 402 | gdb_test "python print (castval.type)" "PTR ." \ |
2c74e833 TT |
403 | "print value's type" |
404 | } | |
405 | ||
2e4d963f PM |
406 | # Regression test for invalid subscript operations. The bug was that |
407 | # the type of the value was not being checked before allowing a | |
408 | # subscript operation to proceed. | |
409 | ||
db8e4570 | 410 | proc test_subscript_regression {exefile lang} { |
2e4d963f | 411 | # Start with a fresh gdb. |
d54b30bb | 412 | clean_restart ${exefile} |
2e4d963f PM |
413 | |
414 | if ![runto_main ] then { | |
415 | perror "couldn't run to breakpoint" | |
416 | return | |
417 | } | |
418 | ||
419 | if {$lang == "c++"} { | |
420 | gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"] | |
421 | gdb_continue_to_breakpoint "break to inspect pointer by reference" | |
422 | ||
423 | gdb_py_test_silent_cmd "print rptr_int" \ | |
424 | "Obtain address" 1 | |
425 | gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \ | |
426 | "Obtains value from GDB" 1 | |
9325cb04 | 427 | gdb_test "python print (rptr\[0\])" "2" "Check pointer passed as reference" |
f9ffd4bb TT |
428 | |
429 | # Just the most basic test of dynamic_cast -- it is checked in | |
430 | # the C++ tests. | |
9325cb04 | 431 | gdb_test "python print (bool(gdb.parse_and_eval('base').dynamic_cast(gdb.lookup_type('Derived').pointer())))" \ |
f9ffd4bb | 432 | True |
03f17ccf TT |
433 | |
434 | # Likewise. | |
9325cb04 | 435 | gdb_test "python print (gdb.parse_and_eval('base').dynamic_type)" \ |
03f17ccf | 436 | "Derived \[*\]" |
7af389b8 SC |
437 | gdb_test "python print (gdb.parse_and_eval('base_ref').dynamic_type)" \ |
438 | "Derived \[&\]" | |
03f17ccf | 439 | # A static type case. |
9325cb04 | 440 | gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \ |
03f17ccf | 441 | "int" |
2e4d963f PM |
442 | } |
443 | ||
444 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] | |
445 | gdb_continue_to_breakpoint "break to inspect struct and union" | |
446 | ||
447 | gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \ | |
448 | "Create a value for subscript test" 1 | |
449 | gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \ | |
450 | "Create a value for subscript test" 1 | |
451 | ||
452 | # Try to access an int with a subscript. This should fail. | |
35720eaa | 453 | gdb_test "python print (intv)" "1" "Baseline print of an int Python value" |
9325cb04 | 454 | gdb_test "python print (intv\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
2e4d963f PM |
455 | "Attempt to access an integer with a subscript" |
456 | ||
457 | # Try to access a string with a subscript. This should pass. | |
35720eaa | 458 | gdb_test "python print (stringv)" "foo." "Baseline print of a string Python value" |
9325cb04 | 459 | gdb_test "python print (stringv\[0\])" "f." "Attempt to access a string with a subscript" |
2e4d963f PM |
460 | |
461 | # Try to access an int array via a pointer with a subscript. This should pass. | |
462 | gdb_py_test_silent_cmd "print p" "Build pointer to array" 1 | |
463 | gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "" 1 | |
9325cb04 PK |
464 | gdb_test "python print (pointer\[0\])" "1" "Access array via pointer with int subscript" |
465 | gdb_test "python print (pointer\[intv\])" "2" "Access array via pointer with value subscript" | |
2e4d963f PM |
466 | |
467 | # Try to access a single dimension array with a subscript to the | |
468 | # result. This should fail. | |
9325cb04 | 469 | gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
2e4d963f PM |
470 | "Attempt to access an integer with a subscript" |
471 | ||
472 | # Lastly, test subscript access to an array with multiple | |
473 | # dimensions. This should pass. | |
474 | gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1 | |
475 | gdb_py_test_silent_cmd "python marray = gdb.history(0)" "" 1 | |
9325cb04 | 476 | gdb_test "python print (marray\[1\]\[2\])" "o." "Test multiple subscript" |
2e4d963f PM |
477 | } |
478 | ||
57a1d736 TT |
479 | # A few tests of gdb.parse_and_eval. |
480 | proc test_parse_and_eval {} { | |
9325cb04 | 481 | gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \ |
57a1d736 | 482 | "parse_and_eval constant test" |
9325cb04 | 483 | gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \ |
57a1d736 | 484 | "parse_and_eval simple expression test" |
9325cb04 PK |
485 | gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \ |
486 | ".(type|class) 'gdb.Value'."\ | |
57a1d736 TT |
487 | "parse_and_eval type test" |
488 | } | |
489 | ||
88d4aea7 PM |
490 | # Test that values are hashable. |
491 | proc test_value_hash {} { | |
492 | gdb_py_test_multiple "Simple Python value dictionary" \ | |
493 | "python" "" \ | |
494 | "one = gdb.Value(1)" "" \ | |
495 | "two = gdb.Value(2)" "" \ | |
496 | "three = gdb.Value(3)" "" \ | |
497 | "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \ | |
498 | "end" | |
9325cb04 PK |
499 | gdb_test "python print (vdict\[one\])" "one str" "Test dictionary hash" |
500 | gdb_test "python print (vdict\[two\])" "two str" "Test dictionary hash" | |
501 | gdb_test "python print (vdict\[three\])" "three str" "Test dictionary hash" | |
502 | gdb_test "python print (one.__hash__() == hash(one))" "True" "Test inbuilt hash" | |
88d4aea7 PM |
503 | } |
504 | ||
ac52f9a2 DE |
505 | # Build C version of executable. C++ is built later. |
506 | if { [build_inferior "${binfile}" "c"] < 0 } { | |
507 | return -1 | |
508 | } | |
88d4aea7 | 509 | |
a08702d6 | 510 | # Start with a fresh gdb. |
d54b30bb | 511 | clean_restart ${binfile} |
a08702d6 | 512 | |
f6bbabf0 PM |
513 | # Skip all tests if Python scripting is not enabled. |
514 | if { [skip_python_tests] } { continue } | |
a08702d6 TJB |
515 | |
516 | test_value_creation | |
517 | test_value_numeric_ops | |
518 | test_value_boolean | |
519 | test_value_compare | |
89c73ade | 520 | test_objfiles |
57a1d736 | 521 | test_parse_and_eval |
88d4aea7 | 522 | test_value_hash |
adc13a14 PA |
523 | |
524 | # The following tests require execution. | |
525 | ||
526 | if ![runto_main] then { | |
527 | fail "Can't run to main" | |
528 | return 0 | |
529 | } | |
530 | ||
a08702d6 | 531 | test_value_in_inferior |
5374244e | 532 | test_inferior_function_call |
be759fcf | 533 | test_lazy_strings |
2c74e833 | 534 | test_value_after_death |
2e4d963f | 535 | |
db8e4570 | 536 | # Test either C or C++ values. |
ac52f9a2 | 537 | |
db8e4570 | 538 | test_subscript_regression "${binfile}" "c" |
ac52f9a2 DE |
539 | |
540 | if ![skip_cplus_tests] { | |
541 | if { [build_inferior "${binfile}-cxx" "c++"] < 0 } { | |
542 | return -1 | |
543 | } | |
35720eaa DE |
544 | with_test_prefix "c++" { |
545 | test_subscript_regression "${binfile}-cxx" "c++" | |
546 | } | |
ac52f9a2 | 547 | } |