]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/python-value.exp
* gdb.ada/uninitialized_vars: New test program.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / python-value.exp
CommitLineData
0fb0cc75 1# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
a08702d6
TJB
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
19if $tracelevel then {
20 strace $tracelevel
21}
22
23set testfile "python-value"
24set srcfile ${testfile}.c
25set binfile ${objdir}/${subdir}/${testfile}
26if { [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.
35proc 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.
51proc 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
60proc 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
75proc test_value_numeric_ops {} {
76 global gdb_prompt
77
78 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
79 gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
80 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0
81 gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0
82 gdb_test "python print 'result = ' + str(i+j)" " = 7" "add two integer values"
83 gdb_test "python print (i+j).__class__" "<type 'gdb.Value'>" "verify type of integer add result"
84
85 gdb_test "python print 'result = ' + str(f+g)" " = 3.75" "add two double values"
86 gdb_test "python print 'result = ' + str(i-j)" " = 3" "subtract two integer values"
87 gdb_test "python print 'result = ' + str(f-g)" " = -1.25" "subtract two double values"
88 gdb_test "python print 'result = ' + str(i*j)" " = 10" "multiply two integer values"
89 gdb_test "python print 'result = ' + str(f*g)" " = 3.125" "multiply two double values"
90 gdb_test "python print 'result = ' + str(i/j)" " = 2" "divide two integer values"
91 gdb_test "python print 'result = ' + str(f/g)" " = 0.5" "divide two double values"
92 gdb_test "python print 'result = ' + str(i%j)" " = 1" "take remainder of two integer values"
93 # Remainder of float is implemented in Python but not in GDB's value system.
94
95 gdb_test "python print 'result = ' + str(i**j)" " = 25" "integer value raised to the power of another integer value"
96 gdb_test "python print 'result = ' + str(g**j)" " = 6.25" "double value raised to the power of integer value"
97
98 gdb_test "python print 'result = ' + str(-i)" " = -5" "negated integer value"
99 gdb_test "python print 'result = ' + str(+i)" " = 5" "positive integer value"
100 gdb_test "python print 'result = ' + str(-f)" " = -1.25" "negated double value"
101 gdb_test "python print 'result = ' + str(+f)" " = 1.25" "positive double value"
102 gdb_test "python print 'result = ' + str(abs(j-i))" " = 3" "absolute of integer value"
103 gdb_test "python print 'result = ' + str(abs(f-g))" " = 1.25" "absolute of double value"
104
105 # Test gdb.Value mixed with Python types.
106
107 gdb_test "python print 'result = ' + str(i-1)" " = 4" "subtract integer value from python integer"
108 gdb_test "python print (i-1).__class__" "<type 'gdb.Value'>" "verify type of mixed integer subtraction result"
109 gdb_test "python print 'result = ' + str(f+1.5)" " = 2.75" "add double value with python float"
110
111 gdb_test "python print 'result = ' + str(1-i)" " = -4" "subtract python integer from integer value"
112 gdb_test "python print 'result = ' + str(1.5+f)" " = 2.75" "add python float with double value"
113
114 # Test pointer arithmethic
115
116 # First, obtain the pointers
117 gdb_test "print (void *) 2" "" ""
118 gdb_test "python a = gdb.get_value_from_history (0)" "" ""
119 gdb_test "print (void *) 5" "" ""
120 gdb_test "python b = gdb.get_value_from_history (0)" "" ""
121
122 gdb_test "python print 'result = ' + str(a+5)" " = 0x7" "add pointer value with python integer"
123 gdb_test "python print 'result = ' + str(b-2)" " = 0x3" "subtract python integer from pointer value"
124 gdb_test "python print 'result = ' + str(b-a)" " = 3" "subtract two pointer values"
125
126 # Test some invalid operations.
127
128 gdb_test_multiple "python print 'result = ' + str(i+'foo')" "catch error in python type conversion" {
129 -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"}
130 -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"}
131 -re "$gdb_prompt $" {fail "catch error in python type conversion"}
132 }
133
134 gdb_test_multiple "python print 'result = ' + str(i+gdb.Value('foo'))" "catch throw of GDB error" {
135 -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"}
136 -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"}
137 -re "$gdb_prompt $" {fail "catch throw of GDB error"}
138 }
139}
140
141proc test_value_boolean {} {
142 # First, define a useful function to test booleans.
143 gdb_py_test_multiple "define function to test booleans" \
144 "python" "" \
145 "def test_bool (val):" "" \
146 " if val:" "" \
147 " print 'yay'" "" \
148 " else:" "" \
149 " print 'nay'" "" \
150 "end" ""
151
152 gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression"
153
154 gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression"
155
156 gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression"
157
158 gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression"
159
160 gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression"
161
162 gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression"
163}
164
165proc test_value_compare {} {
166 gdb_test "py print gdb.Value (1) < gdb.Value (1)" "False" "less than, equal"
167 gdb_test "py print gdb.Value (1) < gdb.Value (2)" "True" "less than, less"
168 gdb_test "py print gdb.Value (2) < gdb.Value (1)" "False" "less than, greater"
169 gdb_test "py print gdb.Value (2) < None" "False" "less than, None"
170
171 gdb_test "py print gdb.Value (1) <= gdb.Value (1)" "True" "less or equal, equal"
172 gdb_test "py print gdb.Value (1) <= gdb.Value (2)" "True" "less or equal, less"
173 gdb_test "py print gdb.Value (2) <= gdb.Value (1)" "False" "less or equal, greater"
174 gdb_test "py print gdb.Value (2) <= None" "False" "less or equal, None"
175
176 gdb_test "py print gdb.Value (1) == gdb.Value (1)" "True" "equality of gdb.Values"
177 gdb_test "py print gdb.Value (1) == gdb.Value (2)" "False" "inequality of gdb.Values"
178 gdb_test "py print gdb.Value (1) == 1.0" "True" "equality of gdb.Value with Python value"
179 gdb_test "py print gdb.Value (1) == 2" "False" "inequality of gdb.Value with Python value"
180 gdb_test "py print gdb.Value (1) == None" "False" "inequality of gdb.Value with None"
181
182 gdb_test "py print gdb.Value (1) != gdb.Value (1)" "False" "inequality, false"
183 gdb_test "py print gdb.Value (1) != gdb.Value (2)" "True" "inequality, true"
184 gdb_test "py print gdb.Value (1) != None" "True" "inequality, None"
185
186 gdb_test "py print gdb.Value (1) > gdb.Value (1)" "False" "greater than, equal"
187 gdb_test "py print gdb.Value (1) > gdb.Value (2)" "False" "greater than, less"
188 gdb_test "py print gdb.Value (2) > gdb.Value (1)" "True" "greater than, greater"
189 gdb_test "py print gdb.Value (2) > None" "True" "greater than, None"
190
191 gdb_test "py print gdb.Value (1) >= gdb.Value (1)" "True" "greater or equal, equal"
192 gdb_test "py print gdb.Value (1) >= gdb.Value (2)" "False" "greater or equal, less"
193 gdb_test "py print gdb.Value (2) >= gdb.Value (1)" "True" "greater or equal, greater"
194 gdb_test "py print gdb.Value (2) >= None" "True" "greater or equal, None"
195}
196
197proc test_value_in_inferior {} {
198 global gdb_prompt
199 global testfile
200
201 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
a08702d6
TJB
202
203 gdb_continue_to_breakpoint "break to inspect struct and union"
204
205 # Just get inferior variable s in the value history, available to python.
206 gdb_test "print s" " = {a = 3, b = 5}" ""
207
208 gdb_py_test_silent_cmd "python s = gdb.get_value_from_history (0)" "get value from history" 1
209
210 gdb_test "python print 'result = ' + str(s\['a'\])" " = 3" "access element inside struct using 8-bit string name"
211 gdb_test "python print 'result = ' + str(s\[u'a'\])" " = 3" "access element inside struct using unicode name"
212
213 # Test dereferencing the argv pointer
214
215 # Just get inferior variable argv the value history, available to python.
216 gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" ""
217
218 gdb_py_test_silent_cmd "python argv = gdb.get_value_from_history (0)" "" 0
219 gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1
220
221 # Check that the dereferenced value is sane
222 gdb_test "python print arg0" "0x.*$testfile\"" "verify dereferenced value"
223}
224
225
226# Start with a fresh gdb.
227
228gdb_exit
229gdb_start
230gdb_reinitialize_dir $srcdir/$subdir
231gdb_load ${binfile}
232
233gdb_test_multiple "python print 'hello, world!'" "verify python support" {
234 -re "not supported.*$gdb_prompt $" {
235 unsupported "python support is disabled"
236 return -1
237 }
238 -re "$gdb_prompt $" {}
239}
240
241test_value_creation
242test_value_numeric_ops
243test_value_boolean
244test_value_compare
adc13a14
PA
245
246# The following tests require execution.
247
248if ![runto_main] then {
249 fail "Can't run to main"
250 return 0
251}
252
a08702d6 253test_value_in_inferior