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