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