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