2010-01-13 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-value.exp
1 # Copyright (C) 2008, 2009, 2010 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 if $tracelevel then {
20 strace $tracelevel
21 }
22
23 set testfile "py-value"
24 set srcfile ${testfile}.c
25 set binfile ${objdir}/${subdir}/${testfile}
26 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
27 untested "Couldn't compile ${srcfile}"
28 return -1
29 }
30
31 # Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}...
32 # Run a test named NAME, consisting of multiple lines of input.
33 # After each input line INPUT, search for result line RESULT.
34 # Succeed if all results are seen; fail otherwise.
35 proc gdb_py_test_multiple {name args} {
36 global gdb_prompt
37 foreach {input result} $args {
38 if {[gdb_test_multiple $input "$name - $input" {
39 -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
40 pass "$name - $input"
41 }
42 }]} {
43 return 1
44 }
45 }
46 return 0
47 }
48
49 # Run a command in GDB, and report a failure if a Python exception is thrown.
50 # If report_pass is true, report a pass if no exception is thrown.
51 proc gdb_py_test_silent_cmd {cmd name report_pass} {
52 global gdb_prompt
53
54 gdb_test_multiple $cmd $name {
55 -re "Traceback.*$gdb_prompt $" { fail $name }
56 -re "$gdb_prompt $" { if $report_pass { pass $name } }
57 }
58 }
59
60 proc test_value_creation {} {
61 global gdb_prompt
62
63 gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1
64 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1
65 gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1
66 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1
67 gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1
68 gdb_test "python print a" "\"string test\"" "print 8-bit string"
69 gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of 8-bit string"
70 gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1
71 gdb_test "python print a" "\"unicode test\"" "print Unicode string"
72 gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of unicode string"
73
74 # Test address attribute is None in a non-addressable value
75 gdb_test "python print 'result =', i.address" "= None" "Test address attribute in non-addressable value"
76 }
77
78 proc test_value_numeric_ops {} {
79 global gdb_prompt
80
81 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
82 gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
83 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0
84 gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0
85 gdb_test "python print 'result = ' + str(i+j)" " = 7" "add two integer values"
86 gdb_test "python print (i+j).__class__" "<type 'gdb.Value'>" "verify type of integer add result"
87
88 gdb_test "python print 'result = ' + str(f+g)" " = 3.75" "add two double values"
89 gdb_test "python print 'result = ' + str(i-j)" " = 3" "subtract two integer values"
90 gdb_test "python print 'result = ' + str(f-g)" " = -1.25" "subtract two double values"
91 gdb_test "python print 'result = ' + str(i*j)" " = 10" "multiply two integer values"
92 gdb_test "python print 'result = ' + str(f*g)" " = 3.125" "multiply two double values"
93 gdb_test "python print 'result = ' + str(i/j)" " = 2" "divide two integer values"
94 gdb_test "python print 'result = ' + str(f/g)" " = 0.5" "divide two double values"
95 gdb_test "python print 'result = ' + str(i%j)" " = 1" "take remainder of two integer values"
96 # Remainder of float is implemented in Python but not in GDB's value system.
97
98 gdb_test "python print 'result = ' + str(i**j)" " = 25" "integer value raised to the power of another integer value"
99 gdb_test "python print 'result = ' + str(g**j)" " = 6.25" "double value raised to the power of integer value"
100
101 gdb_test "python print 'result = ' + str(-i)" " = -5" "negated integer value"
102 gdb_test "python print 'result = ' + str(+i)" " = 5" "positive integer value"
103 gdb_test "python print 'result = ' + str(-f)" " = -1.25" "negated double value"
104 gdb_test "python print 'result = ' + str(+f)" " = 1.25" "positive double value"
105 gdb_test "python print 'result = ' + str(abs(j-i))" " = 3" "absolute of integer value"
106 gdb_test "python print 'result = ' + str(abs(f-g))" " = 1.25" "absolute of double value"
107
108 # Test gdb.Value mixed with Python types.
109
110 gdb_test "python print 'result = ' + str(i-1)" " = 4" "subtract integer value from python integer"
111 gdb_test "python print (i-1).__class__" "<type 'gdb.Value'>" "verify type of mixed integer subtraction result"
112 gdb_test "python print 'result = ' + str(f+1.5)" " = 2.75" "add double value with python float"
113
114 gdb_test "python print 'result = ' + str(1-i)" " = -4" "subtract python integer from integer value"
115 gdb_test "python print 'result = ' + str(1.5+f)" " = 2.75" "add python float with double value"
116
117 # Conversion test.
118 gdb_test "print evalue" " = TWO"
119 gdb_test "python evalue = gdb.history (0)" ""
120 gdb_test "python print int (evalue)" "2"
121
122 # Test pointer arithmethic
123
124 # First, obtain the pointers
125 gdb_test "print (void *) 2" "" ""
126 gdb_test "python a = gdb.history (0)" "" ""
127 gdb_test "print (void *) 5" "" ""
128 gdb_test "python b = gdb.history (0)" "" ""
129
130 gdb_test "python print 'result = ' + str(a+5)" " = 0x7" "add pointer value with python integer"
131 gdb_test "python print 'result = ' + str(b-2)" " = 0x3" "subtract python integer from pointer value"
132 gdb_test "python print 'result = ' + str(b-a)" " = 3" "subtract two pointer values"
133
134 # Test some invalid operations.
135
136 gdb_test_multiple "python print 'result = ' + str(i+'foo')" "catch error in python type conversion" {
137 -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"}
138 -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"}
139 -re "$gdb_prompt $" {fail "catch error in python type conversion"}
140 }
141
142 gdb_test_multiple "python print 'result = ' + str(i+gdb.Value('foo'))" "catch throw of GDB error" {
143 -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"}
144 -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"}
145 -re "$gdb_prompt $" {fail "catch throw of GDB error"}
146 }
147 }
148
149 proc test_value_boolean {} {
150 # First, define a useful function to test booleans.
151 gdb_py_test_multiple "define function to test booleans" \
152 "python" "" \
153 "def test_bool (val):" "" \
154 " if val:" "" \
155 " print 'yay'" "" \
156 " else:" "" \
157 " print 'nay'" "" \
158 "end" ""
159
160 gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression"
161
162 gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression"
163
164 gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression"
165
166 gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression"
167
168 gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression"
169
170 gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression"
171 }
172
173 proc test_value_compare {} {
174 gdb_test "py print gdb.Value (1) < gdb.Value (1)" "False" "less than, equal"
175 gdb_test "py print gdb.Value (1) < gdb.Value (2)" "True" "less than, less"
176 gdb_test "py print gdb.Value (2) < gdb.Value (1)" "False" "less than, greater"
177 gdb_test "py print gdb.Value (2) < None" "False" "less than, None"
178
179 gdb_test "py print gdb.Value (1) <= gdb.Value (1)" "True" "less or equal, equal"
180 gdb_test "py print gdb.Value (1) <= gdb.Value (2)" "True" "less or equal, less"
181 gdb_test "py print gdb.Value (2) <= gdb.Value (1)" "False" "less or equal, greater"
182 gdb_test "py print gdb.Value (2) <= None" "False" "less or equal, None"
183
184 gdb_test "py print gdb.Value (1) == gdb.Value (1)" "True" "equality of gdb.Values"
185 gdb_test "py print gdb.Value (1) == gdb.Value (2)" "False" "inequality of gdb.Values"
186 gdb_test "py print gdb.Value (1) == 1.0" "True" "equality of gdb.Value with Python value"
187 gdb_test "py print gdb.Value (1) == 2" "False" "inequality of gdb.Value with Python value"
188 gdb_test "py print gdb.Value (1) == None" "False" "inequality of gdb.Value with None"
189
190 gdb_test "py print gdb.Value (1) != gdb.Value (1)" "False" "inequality, false"
191 gdb_test "py print gdb.Value (1) != gdb.Value (2)" "True" "inequality, true"
192 gdb_test "py print gdb.Value (1) != None" "True" "inequality, None"
193
194 gdb_test "py print gdb.Value (1) > gdb.Value (1)" "False" "greater than, equal"
195 gdb_test "py print gdb.Value (1) > gdb.Value (2)" "False" "greater than, less"
196 gdb_test "py print gdb.Value (2) > gdb.Value (1)" "True" "greater than, greater"
197 gdb_test "py print gdb.Value (2) > None" "True" "greater than, None"
198
199 gdb_test "py print gdb.Value (1) >= gdb.Value (1)" "True" "greater or equal, equal"
200 gdb_test "py print gdb.Value (1) >= gdb.Value (2)" "False" "greater or equal, less"
201 gdb_test "py print gdb.Value (2) >= gdb.Value (1)" "True" "greater or equal, greater"
202 gdb_test "py print gdb.Value (2) >= None" "True" "greater or equal, None"
203 }
204
205 proc test_value_in_inferior {} {
206 global gdb_prompt
207 global testfile
208
209 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
210
211 gdb_continue_to_breakpoint "break to inspect struct and union"
212
213 # Just get inferior variable s in the value history, available to python.
214 gdb_test "print s" " = {a = 3, b = 5}" ""
215
216 gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1
217
218 gdb_test "python print 'result = ' + str(s\['a'\])" " = 3" "access element inside struct using 8-bit string name"
219 gdb_test "python print 'result = ' + str(s\[u'a'\])" " = 3" "access element inside struct using unicode name"
220
221 # Test dereferencing the argv pointer
222
223 # Just get inferior variable argv the value history, available to python.
224 gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" ""
225
226 gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0
227 gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1
228
229 # Check that the dereferenced value is sane
230 gdb_test "python print arg0" "0x.*$testfile\"" "verify dereferenced value"
231
232 # Smoke-test is_optimized_out attribute
233 gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute"
234
235 # Test address attribute
236 gdb_test "python print 'result =', arg0.address" "= 0x\[\[:xdigit:\]\]+" "Test address attribute"
237
238 # Test string fetches, both partial and whole.
239 gdb_test "print st" "\"divide et impera\""
240 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
241 gdb_test "python print st.string ()" "divide et impera" "Test string with no length"
242 gdb_test "python print st.string (length = -1)" "divide et impera" "Test string (length = -1) is all of the string"
243 gdb_test "python print st.string (length = 6)" "divide"
244 gdb_test "python print \"---\"+st.string (length = 0)+\"---\"" "------" "Test string (length = 0) is empty"
245 gdb_test "python print len(st.string (length = 0))" "0" "Test length is 0"
246
247
248 # Fetch a string that has embedded nulls.
249 gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*"
250 gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value from history" 1
251 gdb_test "python print nullst.string ()" "divide" "Test string to first null"
252 # Python cannot print strings that contain the null (\0) character.
253 # For the purposes of this test, use repr()
254 gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1
255 gdb_test "python print repr(nullst)" "u'divide\\\\x00et'"
256 }
257
258 proc test_lazy_strings {} {
259
260 global hex
261
262 gdb_test "print sptr" "\"pointer\""
263 gdb_py_test_silent_cmd "python sptr = gdb.history (0)" "Get value from history" 1
264
265 gdb_py_test_silent_cmd "python lstr = sptr.lazy_string()" "Aquire lazy string" 1
266 gdb_test "python print lstr.type" "const char \*." "Test type name equality"
267 gdb_test "python print sptr.type" "const char \*." "Test type name equality"
268 }
269
270
271 # A few objfile tests.
272 proc test_objfiles {} {
273 gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'py-value' in file.filename:\n ok=True\nprint ok\nend" "True"
274
275 gdb_test "python print gdb.objfiles()\[0\].pretty_printers" "\\\[\\\]"
276
277 gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \
278 "pretty_printers attribute must be a list.*Error while executing Python code."
279 }
280
281 proc test_value_after_death {} {
282 # Construct a type while the inferior is still running.
283 gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \
284 "create PTR type" 1
285
286 # Kill the inferior and remove the symbols.
287 gdb_test "kill" "" "kill the inferior" \
288 "Kill the program being debugged. .y or n. $" \
289 "y"
290 gdb_test "file" "" "Discard the symbols" \
291 "Discard symbol table from.*y or n. $" \
292 "y"
293
294 # Now create a value using that type. Relies on arg0, created by
295 # test_value_in_inferior.
296 gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \
297 "cast arg0 to PTR" 1
298
299 # Make sure the type is deleted.
300 gdb_py_test_silent_cmd "python ptrtype = None" \
301 "delete PTR type" 1
302
303 # Now see if the value's type is still valid.
304 gdb_test "python print castval.type" "PTR ." \
305 "print value's type"
306 }
307
308 # Regression test for invalid subscript operations. The bug was that
309 # the type of the value was not being checked before allowing a
310 # subscript operation to proceed.
311
312 proc test_subscript_regression {lang} {
313
314 global srcdir subdir srcfile binfile testfile hex
315 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug $lang"] != "" } {
316 untested "Couldn't compile ${srcfile} in $lang mode"
317 return -1
318 }
319
320 # Start with a fresh gdb.
321 gdb_exit
322 gdb_start
323 gdb_reinitialize_dir $srcdir/$subdir
324 gdb_load ${binfile}
325
326 if ![runto_main ] then {
327 perror "couldn't run to breakpoint"
328 return
329 }
330
331 if {$lang == "c++"} {
332 gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"]
333 gdb_continue_to_breakpoint "break to inspect pointer by reference"
334
335 gdb_py_test_silent_cmd "print rptr_int" \
336 "Obtain address" 1
337 gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \
338 "Obtains value from GDB" 1
339 gdb_test "python print rptr\[0\]" "2" "Check pointer passed as reference"
340 }
341
342 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
343 gdb_continue_to_breakpoint "break to inspect struct and union"
344
345 gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \
346 "Create a value for subscript test" 1
347 gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \
348 "Create a value for subscript test" 1
349
350 # Try to access an int with a subscript. This should fail.
351 gdb_test "python print intv" "1" "Baseline print of a Python value"
352 gdb_test "python print intv\[0\]" "RuntimeError: Cannot subscript requested type.*" \
353 "Attempt to access an integer with a subscript"
354
355 # Try to access a string with a subscript. This should pass.
356 gdb_test "python print stringv" "foo." "Baseline print of a Python value"
357 gdb_test "python print stringv\[0\]" "f." "Attempt to access a string with a subscript"
358
359 # Try to access an int array via a pointer with a subscript. This should pass.
360 gdb_py_test_silent_cmd "print p" "Build pointer to array" 1
361 gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "" 1
362 gdb_test "python print pointer\[0\]" "1" "Access array via pointer with int subscript"
363 gdb_test "python print pointer\[intv\]" "2" "Access array via pointer with value subscript"
364
365 # Try to access a single dimension array with a subscript to the
366 # result. This should fail.
367 gdb_test "python print pointer\[intv\]\[0\]" "RuntimeError: Cannot subscript requested type.*" \
368 "Attempt to access an integer with a subscript"
369
370 # Lastly, test subscript access to an array with multiple
371 # dimensions. This should pass.
372 gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1
373 gdb_py_test_silent_cmd "python marray = gdb.history(0)" "" 1
374 gdb_test "python print marray\[1\]\[2\]" "o." "Test multiple subscript"
375 }
376
377 # A few tests of gdb.parse_and_eval.
378 proc test_parse_and_eval {} {
379 gdb_test "python print gdb.parse_and_eval ('23')" "23" \
380 "parse_and_eval constant test"
381 gdb_test "python print gdb.parse_and_eval ('5 + 7')" "12" \
382 "parse_and_eval simple expression test"
383 gdb_test "python print type(gdb.parse_and_eval ('5 + 7'))" \
384 ".type 'gdb.Value'."\
385 "parse_and_eval type test"
386 }
387
388 # Start with a fresh gdb.
389
390 gdb_exit
391 gdb_start
392 gdb_reinitialize_dir $srcdir/$subdir
393 gdb_load ${binfile}
394
395 gdb_test_multiple "python print 'hello, world!'" "verify python support" {
396 -re "not supported.*$gdb_prompt $" {
397 unsupported "python support is disabled"
398 return -1
399 }
400 -re "$gdb_prompt $" {}
401 }
402
403 test_value_creation
404 test_value_numeric_ops
405 test_value_boolean
406 test_value_compare
407 test_objfiles
408 test_parse_and_eval
409
410 # The following tests require execution.
411
412 if ![runto_main] then {
413 fail "Can't run to main"
414 return 0
415 }
416
417 test_value_in_inferior
418 test_lazy_strings
419 test_value_after_death
420
421 # The following test recompiles the binary to test either C or C++
422 # values.
423 test_subscript_regression "c++"
424 test_subscript_regression "c"
This page took 0.044178 seconds and 5 git commands to generate.