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