]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-value.exp
gdb/python: remove Python 2 support
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-value.exp
1 # Copyright (C) 2008-2022 Free Software Foundation, Inc.
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
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 set has_argv0 [gdb_has_argv0]
24
25 # Build inferior to language specification.
26 # LANG is one of "c" or "c++".
27 proc build_inferior {exefile lang} {
28 global srcdir subdir srcfile testfile hex
29
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"] != "" } {
42 untested "failed to compile in $lang mode"
43 return -1
44 }
45 return 0
46 }
47
48 proc test_value_creation {} {
49 global gdb_prompt
50
51 gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1
52 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1
53 gdb_py_test_silent_cmd "python i = gdb.Value (3,None)" "create integer value, with None type" 1
54
55 gdb_py_test_silent_cmd "python l = gdb.Value(0xffffffff12345678)" "create large unsigned 64-bit value" 1
56 gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python"
57
58 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1
59 gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1
60 gdb_test "python print (a)" "\"string test\"" "print 8-bit string"
61 gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string"
62
63 # Test address attribute is None in a non-addressable value
64 gdb_test "python print ('result = %s' % i.address)" "= None" "test address attribute in non-addressable value"
65 }
66
67 # Check that we can call gdb.Value.__init__ to change a value.
68 proc test_value_reinit {} {
69 gdb_py_test_silent_cmd "python v = gdb.Value (3)" \
70 "create initial integer value" 1
71 gdb_test "python print(v)" "3" \
72 "check initial value contents"
73 gdb_py_test_silent_cmd "python v.__init__(5)" \
74 "call gdb.Value.__init__ manually" 1
75 gdb_test "python print(v)" "5" \
76 "check new value contents"
77 }
78
79 proc test_value_numeric_ops {} {
80 global gdb_prompt
81
82 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
83 gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
84 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0
85 gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0
86 gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values"
87 gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result"
88
89 gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values"
90 gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values"
91 gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values"
92 gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values"
93 gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values"
94 gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values"
95 gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values"
96 gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values"
97 # Remainder of float is implemented in Python but not in GDB's value system.
98
99 gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value"
100 gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value"
101
102 gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value"
103 gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value"
104 gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value"
105 gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value"
106 gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value"
107 gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value"
108
109 # Test gdb.Value mixed with Python types.
110
111 gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer"
112 gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result"
113 gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float"
114
115 gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value"
116 gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value"
117
118 # Conversion test.
119 gdb_test "print evalue" " = TWO"
120 gdb_test_no_output "python evalue = gdb.history (0)"
121 gdb_test "python print (int (evalue))" "2"
122
123 # Test pointer arithmethic
124
125 # First, obtain the pointers
126 gdb_test "print (void *) 2" ".*" ""
127 gdb_test_no_output "python a = gdb.history (0)" ""
128 gdb_test "print (void *) 5" ".*" ""
129 gdb_test_no_output "python b = gdb.history (0)" ""
130
131 gdb_test "python print(int(b))" "5" "convert pointer to int"
132
133 gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer"
134 gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value"
135 gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values"
136
137 gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \
138 "result = r" "use value as string index"
139 gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \
140 "result = 1" "use value as tuple index"
141 gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
142 "result = 1" "use value as array index"
143
144 gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \
145 "f+" "int conversion respect type sign"
146
147 # Test some invalid operations.
148
149 gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {
150 -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"}
151 -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"}
152 -re "$gdb_prompt $" {fail "catch error in python type conversion"}
153 }
154
155 gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" {
156 -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"}
157 -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"}
158 -re "$gdb_prompt $" {fail "catch throw of GDB error"}
159 }
160 }
161
162 proc test_value_boolean {} {
163 # First, define a useful function to test booleans.
164 gdb_test_multiline "define function to test booleans" \
165 "python" "" \
166 "def test_bool (val):" "" \
167 " if val:" "" \
168 " print ('yay')" "" \
169 " else:" "" \
170 " print ('nay')" "" \
171 "end" ""
172
173 gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression"
174
175 gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression"
176
177 gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression"
178
179 gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression"
180
181 gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true float value in expression"
182
183 gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false float value in expression"
184 }
185
186 proc test_value_compare {} {
187 gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal"
188 gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less"
189 gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater"
190 gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None"
191
192 gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal"
193 gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less"
194 gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater"
195 gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None"
196
197 gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values"
198 gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values"
199 gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value"
200 gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value"
201 gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None"
202
203 gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false"
204 gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true"
205 gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None"
206
207 gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal"
208 gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less"
209 gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater"
210 gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None"
211
212 gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal"
213 gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less"
214 gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater"
215 gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None"
216 }
217
218 proc test_value_in_inferior {} {
219 global gdb_prompt
220 global testfile
221
222 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
223 gdb_continue_to_breakpoint "break to inspect struct and union"
224
225 # Just get inferior variable s in the value history, available to python.
226 gdb_test "print s" " = {a = 3, b = 5}" ""
227
228 gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value s from history" 1
229
230 gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name"
231
232 # Test dereferencing the argv pointer
233
234 # Just get inferior variable argv the value history, available to python.
235 gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" ""
236
237 gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0
238 gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1
239
240 # Check that the dereferenced value is sane
241 global has_argv0
242 set test "verify dereferenced value"
243 if { $has_argv0 } {
244 gdb_test_no_output "set print elements unlimited" ""
245 gdb_test_no_output "set print repeats unlimited" ""
246 gdb_test "python print (arg0)" "0x.*$testfile\"" $test
247 } else {
248 unsupported $test
249 }
250
251 # Smoke-test is_optimized_out attribute
252 gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "test is_optimized_out attribute"
253
254 # Test address attribute
255 gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "test address attribute"
256
257 # Test displaying a variable that is temporarily at a bad address.
258 # But if we can examine what's at memory address 0, then we'll also be
259 # able to display it without error. Don't run the test in that case.
260 set can_read_0 [is_address_zero_readable]
261
262 # Test memory error.
263 set test "parse_and_eval with memory error"
264 if {$can_read_0} {
265 untested $test
266 } else {
267 gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
268 }
269
270 # Test Python lazy value handling
271 set test "memory error and lazy values"
272 if {$can_read_0} {
273 untested $test
274 } else {
275 gdb_test "python inval = gdb.parse_and_eval('*(int*)0')"
276 gdb_test "python print (inval.is_lazy)" "True"
277 gdb_test "python inval2 = inval+1" \
278 "gdb.MemoryError: Cannot access memory at address 0x0.*" \
279 "$test, first test"
280 gdb_test "python inval.fetch_lazy ()" \
281 "gdb.MemoryError: Cannot access memory at address 0x0.*" \
282 "$test, second test"
283 }
284 set argc_value [get_integer_valueof "argc" 0]
285 gdb_test "python argc_lazy = gdb.parse_and_eval('argc')"
286 gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')"
287 gdb_test "python argc_notlazy.fetch_lazy()"
288 gdb_test "python print (argc_lazy.is_lazy)" "True" \
289 "python print (argc_lazy.is_lazy) the first time"
290 gdb_test "python print (argc_notlazy.is_lazy)" "False"
291 gdb_test "print argc" " = $argc_value" "sanity check argc"
292 gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue" \
293 "python print (argc_lazy.is_lazy) the second time"
294 gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc"
295 gdb_test "python print (argc_notlazy)" "\r\n$argc_value"
296 gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]"
297 gdb_test "python print (argc_lazy.is_lazy)" "False"
298
299 # Test string fetches, both partial and whole.
300 gdb_test "print st" "\"divide et impera\""
301 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value st from history" 1
302 gdb_test "python print (st.string ())" "divide et impera" "Test string with no length"
303 gdb_test "python print (st.string (length = -1))" "divide et impera" "test string (length = -1) is all of the string"
304 gdb_test "python print (st.string (length = 6))" "divide"
305 gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "test string (length = 0) is empty"
306 gdb_test "python print (len(st.string (length = 0)))" "0" "test length is 0"
307
308 # We choose Ada here to test a language where c_style_arrays is
309 # false.
310 gdb_test "set lang ada" \
311 "Warning: the current language does not match this frame."
312 gdb_test "python print (st.string ())" "divide et impera" \
313 "Test string with no length in ada"
314 gdb_test_no_output "set lang auto"
315
316 # Fetch a string that has embedded nulls.
317 gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*"
318 gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value nullst from history" 1
319 gdb_test "python print (nullst.string ())" "divide" "test string to first null"
320 # Python cannot print strings that contain the null (\0) character.
321 # For the purposes of this test, use repr()
322 gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1
323 gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'"
324
325 # Test fetching a string longer than its declared (in C) size.
326 # PR 16286
327 gdb_py_test_silent_cmd "python xstr = gdb.parse_and_eval('xstr')" "get xstr" 1
328 gdb_test "python print(xstr\['text'\].string (length = xstr\['length'\]))" "x{100}" \
329 "read string beyond declared size"
330
331 # However it shouldn't be possible to fetch past the end of a
332 # non-memory value.
333 gdb_py_test_silent_cmd "python str = '\"str\"'" "set up str variable" 1
334 gdb_test "python print (gdb.parse_and_eval (str).string (length = 10))" \
335 "gdb.error: Attempt to take address of value not located in memory.\r\nError while executing Python code."
336 }
337
338 proc test_inferior_function_call {} {
339 global gdb_prompt hex decimal
340
341 # Correct inferior call without arguments.
342 gdb_test "p/x fp1" " = $hex.*"
343 gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value fp1 from history" 1
344 gdb_test "python fp1 = fp1.dereference()" ""
345 gdb_test "python result = fp1()" ""
346 gdb_test "python print (result)" "void"
347
348 # Correct inferior call with arguments.
349 gdb_test "p/x fp2" " = $hex.*" \
350 "print fp2 to place it into history"
351 gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value fp2 from history" 1
352 gdb_test "python fp2 = fp2.dereference()" ""
353 gdb_test "python result2 = fp2(10,20)" ""
354 gdb_test "python print (result2)" "30"
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 i 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 "print fp2 again to place it into history"
364 gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value fp3 from history" 1
365 gdb_test "python fp3 = fp3.dereference()" ""
366 gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*"
367 }
368
369 # A few objfile tests.
370 proc test_objfiles {} {
371 gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'py-value' in file.filename:\n ok=True\nprint (ok)\nend" "True" \
372 "py-value in file.filename"
373
374 gdb_test "python print (gdb.objfiles()\[0\].pretty_printers)" "\\\[\\\]"
375
376 gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \
377 "pretty_printers attribute must be a list.*Error while executing Python code."
378 }
379
380 proc test_value_after_death {} {
381 # Construct a type while the inferior is still running.
382 gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \
383 "create PTR type" 1
384
385 # Kill the inferior and remove the symbols.
386 gdb_test "kill" "" "kill the inferior" \
387 "Kill the program being debugged. .y or n. $" \
388 "y"
389 gdb_test "file" "" "discard the symbols" \
390 "Discard symbol table from.*y or n. $" \
391 "y"
392
393 # Now create a value using that type. Relies on arg0, created by
394 # test_value_in_inferior.
395 gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \
396 "cast arg0 to PTR" 1
397
398 # Make sure the type is deleted.
399 gdb_py_test_silent_cmd "python ptrtype = None" \
400 "delete PTR type" 1
401
402 # Now see if the value's type is still valid.
403 gdb_test "python print (castval.type)" "PTR ." \
404 "print value's type"
405 }
406
407 # Regression test for invalid subscript operations. The bug was that
408 # the type of the value was not being checked before allowing a
409 # subscript operation to proceed.
410
411 proc test_subscript_regression {exefile lang} {
412 # Start with a fresh gdb.
413 clean_restart ${exefile}
414
415 if ![runto_main ] then {
416 perror "couldn't run to breakpoint"
417 return
418 }
419
420 if {$lang == "c++"} {
421 gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"]
422 gdb_continue_to_breakpoint "break to inspect pointer by reference"
423
424 gdb_py_test_silent_cmd "print rptr_int" \
425 "Obtain address" 1
426 gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \
427 "Obtains value from GDB" 1
428 gdb_test "python print (rptr\[0\])" "2" "check pointer passed as reference"
429
430 # Just the most basic test of dynamic_cast -- it is checked in
431 # the C++ tests.
432 gdb_test "python print (bool(gdb.parse_and_eval('base').dynamic_cast(gdb.lookup_type('Derived').pointer())))" \
433 True
434
435 # Likewise.
436 gdb_test "python print (gdb.parse_and_eval('base').dynamic_type)" \
437 "Derived \[*\]"
438 gdb_test "python print (gdb.parse_and_eval('base_ref').dynamic_type)" \
439 "Derived \[&\]"
440 # A static type case.
441 gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \
442 "int"
443 }
444
445 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
446 gdb_continue_to_breakpoint \
447 "break to inspect struct and union for subscript regression test"
448
449 gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \
450 "Create value intv for subscript test" 1
451 gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \
452 "Create value stringv for subscript test" 1
453
454 # Try to access an int with a subscript. This should fail.
455 gdb_test "python print (intv)" "1" "baseline print of an int Python value"
456 gdb_test "python print (intv\[0\])" "gdb.error: Cannot subscript requested type.*" \
457 "Attempt to access an integer with a subscript"
458
459 # Try to access a string with a subscript. This should pass.
460 gdb_test "python print (stringv)" "foo." "baseline print of a string Python value"
461 gdb_test "python print (stringv\[0\])" "f." "attempt to access a string with a subscript"
462
463 # Try to access an int array via a pointer with a subscript. This should pass.
464 gdb_py_test_silent_cmd "print p" "Build pointer to array" 1
465 gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "fetch pointer" 0
466 gdb_test "python print (pointer\[0\])" "1" "access array via pointer with int subscript"
467 gdb_test "python print (pointer\[intv\])" "2" "access array via pointer with value subscript"
468
469 # Try to access a single dimension array with a subscript to the
470 # result. This should fail.
471 gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error: Cannot subscript requested type.*" \
472 "Attempt to access a single dimension array with a two subscripts"
473
474 # Lastly, test subscript access to an array with multiple
475 # dimensions. This should pass.
476 gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1
477 gdb_py_test_silent_cmd "python marray = gdb.history(0)" "fetch marray" 0
478 gdb_test "python print (marray\[1\]\[2\])" "o." "test multiple subscript"
479 }
480
481 # A few tests of gdb.parse_and_eval.
482 proc test_parse_and_eval {} {
483 gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \
484 "parse_and_eval constant test"
485 gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \
486 "parse_and_eval simple expression test"
487 gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \
488 ".(type|class) 'gdb.Value'."\
489 "parse_and_eval type test"
490 }
491
492 # Test that values are hashable.
493 proc test_value_hash {} {
494 gdb_test_multiline "Simple Python value dictionary" \
495 "python" "" \
496 "one = gdb.Value(1)" "" \
497 "two = gdb.Value(2)" "" \
498 "three = gdb.Value(3)" "" \
499 "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \
500 "end"
501 gdb_test "python print (vdict\[one\])" "one str" "test dictionary hash for one"
502 gdb_test "python print (vdict\[two\])" "two str" "test dictionary hash for two"
503 gdb_test "python print (vdict\[three\])" "three str" "test dictionary hash for three"
504 gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash"
505 }
506
507 proc test_float_conversion {} {
508 gdb_test "python print(int(gdb.Value(0)))" "0"
509 gdb_test "python print(int(gdb.Value(2.5)))" "2"
510 gdb_test "python print(float(gdb.Value(2.5)))" "2\\.5"
511 gdb_test "python print(float(gdb.Value(0)))" "0\\.0"
512 }
513
514 # Setup some Python variables:
515 # tp : a gdb.Type for 'int',
516 # size_a : the size of array 'a' from the inferior,
517 # size_a0 : the size of array element 'a[0] from the inferior,
518 # addr : the address of 'a[0]' from the inferior,
519 # b : a buffer containing the full contents of array 'a' from the
520 # inferior.
521 proc prepare_type_and_buffer {} {
522 gdb_py_test_silent_cmd "python tp=gdb.lookup_type('int')" "look up int type" 0
523 gdb_py_test_silent_cmd "python size_a=gdb.parse_and_eval('sizeof(a)')" \
524 "find size of a" 0
525 gdb_py_test_silent_cmd "python size_a0=gdb.parse_and_eval('sizeof(a\[0\])')" \
526 "find size of element of a" 0
527 gdb_py_test_silent_cmd "python addr=gdb.parse_and_eval('&a')" \
528 "find address of a" 0
529 gdb_py_test_silent_cmd "python b=gdb.selected_inferior().read_memory(addr,size_a)" \
530 "read buffer from memory" 0
531 }
532
533 proc test_value_from_buffer {} {
534 global gdb_prompt
535
536 prepare_type_and_buffer
537 gdb_test "python v=gdb.Value(b,tp); print(v)" "1" \
538 "construct value from buffer"
539 gdb_test "python v=gdb.Value(b\[size_a0:\],tp); print(v)" "2" \
540 "convert 2nd elem of buffer to value"
541 gdb_test "python v=gdb.Value(b\[2*size_a0:\],tp); print(v)" "3" \
542 "convert 3rd elem of buffer to value"
543 gdb_test "python v=gdb.Value(b\[2*size_a0+1:\],tp); print(v)" \
544 "ValueError: Size of type is larger than that of buffer object\..*" \
545 "attempt to convert smaller buffer than size of type"
546 gdb_py_test_silent_cmd "python atp=tp.array(2) ; print(atp)" \
547 "make array type" 0
548 gdb_py_test_silent_cmd "python va=gdb.Value(b,atp)" \
549 "construct array value from buffer" 0
550 gdb_test "python print(va)" "\\{1, 2, 3\\}" "print array value"
551 gdb_test "python print(va\[0\])" "1" "print first array element"
552 gdb_test "python print(va\[1\])" "2" "print second array element"
553 gdb_test "python print(va\[2\])" "3" "print third array element"
554 gdb_test "python print(va\[3\])" "gdb\.error: no such vector element.*" \
555 "print out of bounds array element"
556 gdb_py_test_silent_cmd "python atpbig=tp.array(3)" "make bigger array type" 0
557 gdb_test "python vabig=gdb.Value(b,atpbig)" \
558 "ValueError: Size of type is larger than that of buffer object\..*" \
559 "attempt to construct large value with small buffer"
560 gdb_test "python v=gdb.Value(2048,tp)" \
561 "TypeError: Object must support the python buffer protocol\..*" \
562 "attempt to construct value from buffer with non-buffer object"
563 gdb_test "python v=gdb.Value(b,'int'); print(v)" \
564 "TypeError: type argument must be a gdb\.Type\..*" \
565 "attempt to construct value with string as type"
566 }
567
568 # Test the gdb.add_history API.
569 proc test_add_to_history {} {
570 # Add a gdb.Value to the value history list.
571 gdb_test_no_output "python idx = gdb.add_history(gdb.Value(42))" \
572 "add value 42 to the history list"
573 gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \
574 " = 42" "print value 42 from the history list"
575 set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 42"]
576 gdb_test "print \$${idx}" " = 42"
577
578 # Add something to the history list that can be converted into a
579 # gdb.Value.
580 gdb_test_no_output "python idx = gdb.add_history(84)" \
581 "add value to 84 to the history list"
582 gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \
583 " = 84" "print value 84 from the history list"
584 set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 84"]
585 gdb_test "print \$${idx}" " = 84"
586
587 # Try adding something that can't be converted to a gdb.Value,
588 # this should give an error.
589 gdb_test "python idx = gdb.add_history(gdb.GdbError(\"an error\"))" \
590 "TypeError: Could not convert Python object: .*"
591 }
592
593 # Check we can create sub-classes of gdb.Value.
594 proc test_value_sub_classes {} {
595 prepare_type_and_buffer
596
597 gdb_test_multiline "Create sub-class of gdb.Value" \
598 "python" "" \
599 "class MyValue(gdb.Value):" "" \
600 " def __init__(self,val,type=None):" "" \
601 " gdb.Value.__init__(self,val,type)" "" \
602 " print(\"In MyValue.__init__\")" "" \
603 "end"
604
605 gdb_test "python obj = MyValue (123)" "In MyValue.__init__" \
606 "create instance of MyValue"
607 gdb_test "python print(obj)" "123" \
608 "check printing of MyValue"
609
610 gdb_test "python obj = MyValue(b\[size_a0:\],tp)" "In MyValue.__init__" \
611 "convert 2nd elem of buffer to a MyValue"
612 gdb_test "python print(obj)" "2" \
613 "check printing of MyValue when initiaized with a type"
614 }
615
616 # Test the history count. This must be the first thing called after
617 # starting GDB as it depends on there being nothing in the value
618 # history.
619 proc test_history_count {} {
620 for { set i 0 } { $i < 5 } { incr i } {
621 gdb_test "python print('history count is %d' % gdb.history_count())" \
622 "history count is $i" "history count is $i"
623 gdb_test "print $i" " = $i"
624 }
625 }
626
627 # Build C version of executable. C++ is built later.
628 if { [build_inferior "${binfile}" "c"] < 0 } {
629 return -1
630 }
631
632 # Start with a fresh gdb.
633 clean_restart ${binfile}
634
635 # Skip all tests if Python scripting is not enabled.
636 if { [skip_python_tests] } { continue }
637
638 test_history_count
639 test_value_creation
640 test_value_reinit
641 test_value_numeric_ops
642 test_value_boolean
643 test_value_compare
644 test_objfiles
645 test_parse_and_eval
646 test_value_hash
647 test_float_conversion
648 test_add_to_history
649
650 # The following tests require execution.
651
652 if ![runto_main] then {
653 return 0
654 }
655
656 test_value_in_inferior
657 test_value_from_buffer
658 test_value_sub_classes
659 test_inferior_function_call
660 test_value_after_death
661
662 # Test either C or C++ values.
663
664 test_subscript_regression "${binfile}" "c"
665
666 if ![skip_cplus_tests] {
667 if { [build_inferior "${binfile}-cxx" "c++"] < 0 } {
668 return -1
669 }
670 with_test_prefix "c++" {
671 test_subscript_regression "${binfile}-cxx" "c++"
672 }
673 }