5395f8cbbd1d7a66e0ca10f10b0e910a8e41d2f3
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-value.exp
1 # Copyright (C) 2008-2016 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 "couldn't compile ${srcfile} in $lang mode"
43 return -1
44 }
45 return 0
46 }
47
48 proc test_value_creation {} {
49 global gdb_prompt
50 global gdb_py_is_py3k
51
52 gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1
53 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1
54 if { $gdb_py_is_py3k == 0 } {
55 gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1
56 }
57
58 gdb_py_test_silent_cmd "python l = gdb.Value(0xffffffff12345678)" "create large unsigned 64-bit value" 1
59 if { $gdb_py_is_py3k == 0 } {
60 gdb_test "python print long(l)" "18446744069720004216" "large unsigned 64-bit int conversion to python"
61 } else {
62 gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python"
63 }
64
65 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1
66 gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1
67 gdb_test "python print (a)" "\"string test\"" "print 8-bit string"
68 gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string"
69
70 if { $gdb_py_is_py3k == 0 } {
71 gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1
72 gdb_test "python print (a)" "\"unicode test\"" "print Unicode string"
73 gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of unicode string"
74 }
75
76 # Test address attribute is None in a non-addressable value
77 gdb_test "python print ('result = %s' % i.address)" "= None" "Test address attribute in non-addressable value"
78 }
79
80 proc test_value_numeric_ops {} {
81 global gdb_prompt
82
83 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
84 gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
85 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0
86 gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0
87 gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values"
88 gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result"
89
90 gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values"
91 gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values"
92 gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values"
93 gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values"
94 gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values"
95 gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values"
96 gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values"
97 gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values"
98 # Remainder of float is implemented in Python but not in GDB's value system.
99
100 gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value"
101 gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value"
102
103 gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value"
104 gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value"
105 gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value"
106 gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value"
107 gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value"
108 gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value"
109
110 # Test gdb.Value mixed with Python types.
111
112 gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer"
113 gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result"
114 gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float"
115
116 gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value"
117 gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value"
118
119 # Conversion test.
120 gdb_test "print evalue" " = TWO"
121 gdb_test_no_output "python evalue = gdb.history (0)"
122 gdb_test "python print (int (evalue))" "2"
123
124 # Test pointer arithmethic
125
126 # First, obtain the pointers
127 gdb_test "print (void *) 2" ".*" ""
128 gdb_test_no_output "python a = gdb.history (0)" ""
129 gdb_test "print (void *) 5" ".*" ""
130 gdb_test_no_output "python b = gdb.history (0)" ""
131
132 gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer"
133 gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value"
134 gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values"
135
136 gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \
137 "result = r" "use value as string index"
138 gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \
139 "result = 1" "use value as tuple index"
140 gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
141 "result = 1" "use value as array index"
142
143 # Test some invalid operations.
144
145 gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {
146 -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"}
147 -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"}
148 -re "$gdb_prompt $" {fail "catch error in python type conversion"}
149 }
150
151 gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" {
152 -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"}
153 -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"}
154 -re "$gdb_prompt $" {fail "catch throw of GDB error"}
155 }
156 }
157
158 proc test_value_boolean {} {
159 # First, define a useful function to test booleans.
160 gdb_py_test_multiple "define function to test booleans" \
161 "python" "" \
162 "def test_bool (val):" "" \
163 " if val:" "" \
164 " print ('yay')" "" \
165 " else:" "" \
166 " print ('nay')" "" \
167 "end" ""
168
169 gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression"
170
171 gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression"
172
173 gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression"
174
175 gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression"
176
177 gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression"
178
179 gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression"
180 }
181
182 proc test_value_compare {} {
183 gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal"
184 gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less"
185 gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater"
186 gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None"
187
188 gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal"
189 gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less"
190 gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater"
191 gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None"
192
193 gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values"
194 gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values"
195 gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value"
196 gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value"
197 gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None"
198
199 gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false"
200 gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true"
201 gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None"
202
203 gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal"
204 gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less"
205 gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater"
206 gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None"
207
208 gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal"
209 gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less"
210 gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater"
211 gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None"
212 }
213
214 proc test_value_in_inferior {} {
215 global gdb_prompt
216 global testfile
217 global gdb_py_is_py3k
218
219 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
220
221 gdb_continue_to_breakpoint "break to inspect struct and union"
222
223 # Just get inferior variable s in the value history, available to python.
224 gdb_test "print s" " = {a = 3, b = 5}" ""
225
226 gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1
227
228 gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name"
229 if { $gdb_py_is_py3k == 0 } {
230 gdb_test "python print ('result = ' + str(s\[u'a'\]))" " = 3" "access element inside struct using unicode name"
231 }
232
233 # Test dereferencing the argv pointer
234
235 # Just get inferior variable argv the value history, available to python.
236 gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" ""
237
238 gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0
239 gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1
240
241 # Check that the dereferenced value is sane
242 global has_argv0
243 set test "verify dereferenced value"
244 if { $has_argv0 } {
245 gdb_test_no_output "set print elements unlimited" ""
246 gdb_test_no_output "set print repeats unlimited" ""
247 gdb_test "python print (arg0)" "0x.*$testfile\"" $test
248 } else {
249 unsupported $test
250 }
251
252 # Smoke-test is_optimized_out attribute
253 gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "Test is_optimized_out attribute"
254
255 # Test address attribute
256 gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "Test address attribute"
257
258 # Test displaying a variable that is temporarily at a bad address.
259 # But if we can examine what's at memory address 0, then we'll also be
260 # able to display it without error. Don't run the test in that case.
261 set can_read_0 [is_address_zero_readable]
262
263 # Test memory error.
264 set test "parse_and_eval with memory error"
265 if {$can_read_0} {
266 untested $test
267 } else {
268 gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
269 }
270
271 # Test Python lazy value handling
272 set test "memory error and lazy values"
273 if {$can_read_0} {
274 untested $test
275 } else {
276 gdb_test "python inval = gdb.parse_and_eval('*(int*)0')"
277 gdb_test "python print (inval.is_lazy)" "True"
278 gdb_test "python inval2 = inval+1" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
279 gdb_test "python inval.fetch_lazy ()" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
280 }
281 set argc_value [get_integer_valueof "argc" 0]
282 gdb_test "python argc_lazy = gdb.parse_and_eval('argc')"
283 gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')"
284 gdb_test "python argc_notlazy.fetch_lazy()"
285 gdb_test "python print (argc_lazy.is_lazy)" "True"
286 gdb_test "python print (argc_notlazy.is_lazy)" "False"
287 gdb_test "print argc" " = $argc_value" "sanity check argc"
288 gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue"
289 gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc"
290 gdb_test "python print (argc_notlazy)" "\r\n$argc_value"
291 gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]"
292 gdb_test "python print (argc_lazy.is_lazy)" "False"
293
294 # Test string fetches, both partial and whole.
295 gdb_test "print st" "\"divide et impera\""
296 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
297 gdb_test "python print (st.string ())" "divide et impera" "Test string with no length"
298 gdb_test "python print (st.string (length = -1))" "divide et impera" "Test string (length = -1) is all of the string"
299 gdb_test "python print (st.string (length = 6))" "divide"
300 gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "Test string (length = 0) is empty"
301 gdb_test "python print (len(st.string (length = 0)))" "0" "Test length is 0"
302
303
304 # Fetch a string that has embedded nulls.
305 gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*"
306 gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value from history" 1
307 gdb_test "python print (nullst.string ())" "divide" "Test string to first null"
308 # Python cannot print strings that contain the null (\0) character.
309 # For the purposes of this test, use repr()
310 gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1
311 gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'"
312
313 # Test fetching a string longer than its declared (in C) size.
314 # PR 16286
315 gdb_py_test_silent_cmd "python xstr = gdb.parse_and_eval('xstr')" "get xstr" 1
316 gdb_test "python print(xstr\['text'\].string (length = xstr\['length'\]))" "x{100}" \
317 "read string beyond declared size"
318 }
319
320 proc test_lazy_strings {} {
321
322 global hex
323
324 gdb_test "print sptr" "\"pointer\""
325 gdb_py_test_silent_cmd "python sptr = gdb.history (0)" "Get value from history" 1
326
327 gdb_py_test_silent_cmd "python lstr = sptr.lazy_string()" "Aquire lazy string" 1
328 gdb_test "python print (lstr.type)" "const char \*." "Test lazy-string type name equality"
329 gdb_test "python print (sptr.type)" "const char \*." "Test string type name equality"
330
331 # Prevent symbol on address 0x0 being printed.
332 gdb_test_no_output "set print symbol off"
333 gdb_test "print sn" "0x0"
334
335 gdb_py_test_silent_cmd "python snptr = gdb.history (0)" "Get value from history" 1
336 gdb_test "python snstr = snptr.lazy_string(length=5)" ".*Cannot create a lazy string with address.*" "Test lazy string"
337 gdb_py_test_silent_cmd "python snstr = snptr.lazy_string(length=0)" "Succesfully create a lazy string" 1
338 gdb_test "python print (snstr.length)" "0" "Test lazy string length"
339 gdb_test "python print (snstr.address)" "0" "Test lazy string address"
340 }
341
342
343 proc test_inferior_function_call {} {
344 global gdb_prompt hex decimal
345
346 # Correct inferior call without arguments.
347 gdb_test "p/x fp1" " = $hex.*"
348 gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value from history" 1
349 gdb_test "python fp1 = fp1.dereference()" ""
350 gdb_test "python result = fp1()" ""
351 gdb_test "python print (result)" "void"
352
353 # Correct inferior call with arguments.
354 gdb_test "p/x fp2" " = $hex.*"
355 gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value from history" 1
356 gdb_test "python fp2 = fp2.dereference()" ""
357 gdb_test "python result2 = fp2(10,20)" ""
358 gdb_test "python print (result2)" "30"
359
360 # Incorrect to call an int value.
361 gdb_test "p i" " = $decimal.*"
362 gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value from history" 1
363 gdb_test "python result3 = i()" ".*Value is not callable.*"
364
365 # Incorrect number of arguments.
366 gdb_test "p/x fp2" " = $hex.*"
367 gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value 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 ] then {
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 "break to inspect struct and union"
450
451 gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \
452 "Create a value for subscript test" 1
453 gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \
454 "Create a value for subscript test" 1
455
456 # Try to access an int with a subscript. This should fail.
457 gdb_test "python print (intv)" "1" "Baseline print of an int Python value"
458 gdb_test "python print (intv\[0\])" "gdb.error: Cannot subscript requested type.*" \
459 "Attempt to access an integer with a subscript"
460
461 # Try to access a string with a subscript. This should pass.
462 gdb_test "python print (stringv)" "foo." "Baseline print of a string Python value"
463 gdb_test "python print (stringv\[0\])" "f." "Attempt to access a string with a subscript"
464
465 # Try to access an int array via a pointer with a subscript. This should pass.
466 gdb_py_test_silent_cmd "print p" "Build pointer to array" 1
467 gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "" 1
468 gdb_test "python print (pointer\[0\])" "1" "Access array via pointer with int subscript"
469 gdb_test "python print (pointer\[intv\])" "2" "Access array via pointer with value subscript"
470
471 # Try to access a single dimension array with a subscript to the
472 # result. This should fail.
473 gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error: Cannot subscript requested type.*" \
474 "Attempt to access an integer with a subscript"
475
476 # Lastly, test subscript access to an array with multiple
477 # dimensions. This should pass.
478 gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1
479 gdb_py_test_silent_cmd "python marray = gdb.history(0)" "" 1
480 gdb_test "python print (marray\[1\]\[2\])" "o." "Test multiple subscript"
481 }
482
483 # A few tests of gdb.parse_and_eval.
484 proc test_parse_and_eval {} {
485 gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \
486 "parse_and_eval constant test"
487 gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \
488 "parse_and_eval simple expression test"
489 gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \
490 ".(type|class) 'gdb.Value'."\
491 "parse_and_eval type test"
492 }
493
494 # Test that values are hashable.
495 proc test_value_hash {} {
496 gdb_py_test_multiple "Simple Python value dictionary" \
497 "python" "" \
498 "one = gdb.Value(1)" "" \
499 "two = gdb.Value(2)" "" \
500 "three = gdb.Value(3)" "" \
501 "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \
502 "end"
503 gdb_test "python print (vdict\[one\])" "one str" "Test dictionary hash"
504 gdb_test "python print (vdict\[two\])" "two str" "Test dictionary hash"
505 gdb_test "python print (vdict\[three\])" "three str" "Test dictionary hash"
506 gdb_test "python print (one.__hash__() == hash(one))" "True" "Test inbuilt hash"
507 }
508
509 # Build C version of executable. C++ is built later.
510 if { [build_inferior "${binfile}" "c"] < 0 } {
511 return -1
512 }
513
514 # Start with a fresh gdb.
515 clean_restart ${binfile}
516
517 # Skip all tests if Python scripting is not enabled.
518 if { [skip_python_tests] } { continue }
519
520 test_value_creation
521 test_value_numeric_ops
522 test_value_boolean
523 test_value_compare
524 test_objfiles
525 test_parse_and_eval
526 test_value_hash
527
528 # The following tests require execution.
529
530 if ![runto_main] then {
531 fail "can't run to main"
532 return 0
533 }
534
535 test_value_in_inferior
536 test_inferior_function_call
537 test_lazy_strings
538 test_value_after_death
539
540 # Test either C or C++ values.
541
542 test_subscript_regression "${binfile}" "c"
543
544 if ![skip_cplus_tests] {
545 if { [build_inferior "${binfile}-cxx" "c++"] < 0 } {
546 return -1
547 }
548 with_test_prefix "c++" {
549 test_subscript_regression "${binfile}-cxx" "c++"
550 }
551 }
This page took 0.051113 seconds and 3 git commands to generate.