1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This file is part of the GDB testsuite. It tests the mechanism
# exposing values to Python.
if $tracelevel then {
strace $tracelevel
}
set testfile "python-value"
set srcfile ${testfile}.c
set binfile ${objdir}/${subdir}/${testfile}
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
untested "Couldn't compile ${srcfile}"
return -1
}
# Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}...
# Run a test named NAME, consisting of multiple lines of input.
# After each input line INPUT, search for result line RESULT.
# Succeed if all results are seen; fail otherwise.
proc gdb_py_test_multiple {name args} {
global gdb_prompt
foreach {input result} $args {
if {[gdb_test_multiple $input "$name - $input" {
-re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" {
pass "$name - $input"
}
}]} {
return 1
}
}
return 0
}
# Run a command in GDB, and report a failure if a Python exception is thrown.
# If report_pass is true, report a pass if no exception is thrown.
proc gdb_py_test_silent_cmd {cmd name report_pass} {
global gdb_prompt
gdb_test_multiple $cmd $name {
-re "Traceback.*$gdb_prompt $" { fail $name }
-re "$gdb_prompt $" { if $report_pass { pass $name } }
}
}
proc test_value_creation {} {
global gdb_prompt
gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1
gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1
gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1
gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1
gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1
gdb_test "python print a" "\"string test\"" "print 8-bit string"
gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of 8-bit string"
gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1
gdb_test "python print a" "\"unicode test\"" "print Unicode string"
gdb_test "python print a.__class__" "<type 'gdb.Value'>" "verify type of unicode string"
}
proc test_value_numeric_ops {} {
global gdb_prompt
gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0
gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0
gdb_test "python print 'result = ' + str(i+j)" " = 7" "add two integer values"
gdb_test "python print (i+j).__class__" "<type 'gdb.Value'>" "verify type of integer add result"
gdb_test "python print 'result = ' + str(f+g)" " = 3.75" "add two double values"
gdb_test "python print 'result = ' + str(i-j)" " = 3" "subtract two integer values"
gdb_test "python print 'result = ' + str(f-g)" " = -1.25" "subtract two double values"
gdb_test "python print 'result = ' + str(i*j)" " = 10" "multiply two integer values"
gdb_test "python print 'result = ' + str(f*g)" " = 3.125" "multiply two double values"
gdb_test "python print 'result = ' + str(i/j)" " = 2" "divide two integer values"
gdb_test "python print 'result = ' + str(f/g)" " = 0.5" "divide two double values"
gdb_test "python print 'result = ' + str(i%j)" " = 1" "take remainder of two integer values"
# Remainder of float is implemented in Python but not in GDB's value system.
gdb_test "python print 'result = ' + str(i**j)" " = 25" "integer value raised to the power of another integer value"
gdb_test "python print 'result = ' + str(g**j)" " = 6.25" "double value raised to the power of integer value"
gdb_test "python print 'result = ' + str(-i)" " = -5" "negated integer value"
gdb_test "python print 'result = ' + str(+i)" " = 5" "positive integer value"
gdb_test "python print 'result = ' + str(-f)" " = -1.25" "negated double value"
gdb_test "python print 'result = ' + str(+f)" " = 1.25" "positive double value"
gdb_test "python print 'result = ' + str(abs(j-i))" " = 3" "absolute of integer value"
gdb_test "python print 'result = ' + str(abs(f-g))" " = 1.25" "absolute of double value"
# Test gdb.Value mixed with Python types.
gdb_test "python print 'result = ' + str(i-1)" " = 4" "subtract integer value from python integer"
gdb_test "python print (i-1).__class__" "<type 'gdb.Value'>" "verify type of mixed integer subtraction result"
gdb_test "python print 'result = ' + str(f+1.5)" " = 2.75" "add double value with python float"
gdb_test "python print 'result = ' + str(1-i)" " = -4" "subtract python integer from integer value"
gdb_test "python print 'result = ' + str(1.5+f)" " = 2.75" "add python float with double value"
# Conversion test.
gdb_test "print evalue" " = TWO"
gdb_test "python evalue = gdb.history (0)" ""
gdb_test "python print int (evalue)" "2"
# Test pointer arithmethic
# First, obtain the pointers
gdb_test "print (void *) 2" "" ""
gdb_test "python a = gdb.history (0)" "" ""
gdb_test "print (void *) 5" "" ""
gdb_test "python b = gdb.history (0)" "" ""
gdb_test "python print 'result = ' + str(a+5)" " = 0x7" "add pointer value with python integer"
gdb_test "python print 'result = ' + str(b-2)" " = 0x3" "subtract python integer from pointer value"
gdb_test "python print 'result = ' + str(b-a)" " = 3" "subtract two pointer values"
# Test some invalid operations.
gdb_test_multiple "python print 'result = ' + str(i+'foo')" "catch error in python type conversion" {
-re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"}
-re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"}
-re "$gdb_prompt $" {fail "catch error in python type conversion"}
}
gdb_test_multiple "python print 'result = ' + str(i+gdb.Value('foo'))" "catch throw of GDB error" {
-re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"}
-re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"}
-re "$gdb_prompt $" {fail "catch throw of GDB error"}
}
}
proc test_value_boolean {} {
# First, define a useful function to test booleans.
gdb_py_test_multiple "define function to test booleans" \
"python" "" \
"def test_bool (val):" "" \
" if val:" "" \
" print 'yay'" "" \
" else:" "" \
" print 'nay'" "" \
"end" ""
gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression"
gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression"
gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression"
gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression"
gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression"
gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression"
}
proc test_value_compare {} {
gdb_test "py print gdb.Value (1) < gdb.Value (1)" "False" "less than, equal"
gdb_test "py print gdb.Value (1) < gdb.Value (2)" "True" "less than, less"
gdb_test "py print gdb.Value (2) < gdb.Value (1)" "False" "less than, greater"
gdb_test "py print gdb.Value (2) < None" "False" "less than, None"
gdb_test "py print gdb.Value (1) <= gdb.Value (1)" "True" "less or equal, equal"
gdb_test "py print gdb.Value (1) <= gdb.Value (2)" "True" "less or equal, less"
gdb_test "py print gdb.Value (2) <= gdb.Value (1)" "False" "less or equal, greater"
gdb_test "py print gdb.Value (2) <= None" "False" "less or equal, None"
gdb_test "py print gdb.Value (1) == gdb.Value (1)" "True" "equality of gdb.Values"
gdb_test "py print gdb.Value (1) == gdb.Value (2)" "False" "inequality of gdb.Values"
gdb_test "py print gdb.Value (1) == 1.0" "True" "equality of gdb.Value with Python value"
gdb_test "py print gdb.Value (1) == 2" "False" "inequality of gdb.Value with Python value"
gdb_test "py print gdb.Value (1) == None" "False" "inequality of gdb.Value with None"
gdb_test "py print gdb.Value (1) != gdb.Value (1)" "False" "inequality, false"
gdb_test "py print gdb.Value (1) != gdb.Value (2)" "True" "inequality, true"
gdb_test "py print gdb.Value (1) != None" "True" "inequality, None"
gdb_test "py print gdb.Value (1) > gdb.Value (1)" "False" "greater than, equal"
gdb_test "py print gdb.Value (1) > gdb.Value (2)" "False" "greater than, less"
gdb_test "py print gdb.Value (2) > gdb.Value (1)" "True" "greater than, greater"
gdb_test "py print gdb.Value (2) > None" "True" "greater than, None"
gdb_test "py print gdb.Value (1) >= gdb.Value (1)" "True" "greater or equal, equal"
gdb_test "py print gdb.Value (1) >= gdb.Value (2)" "False" "greater or equal, less"
gdb_test "py print gdb.Value (2) >= gdb.Value (1)" "True" "greater or equal, greater"
gdb_test "py print gdb.Value (2) >= None" "True" "greater or equal, None"
}
proc test_value_in_inferior {} {
global gdb_prompt
global testfile
gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
gdb_continue_to_breakpoint "break to inspect struct and union"
# Just get inferior variable s in the value history, available to python.
gdb_test "print s" " = {a = 3, b = 5}" ""
gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1
gdb_test "python print 'result = ' + str(s\['a'\])" " = 3" "access element inside struct using 8-bit string name"
gdb_test "python print 'result = ' + str(s\[u'a'\])" " = 3" "access element inside struct using unicode name"
# Test dereferencing the argv pointer
# Just get inferior variable argv the value history, available to python.
gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" ""
gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0
gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1
# Check that the dereferenced value is sane
gdb_test "python print arg0" "0x.*$testfile\"" "verify dereferenced value"
# Smoke-test is_optimized_out attribute
gdb_test "python print 'result =', arg0.is_optimized_out" "= False" "Test is_optimized_out attribute"
}
# Start with a fresh gdb.
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}
gdb_test_multiple "python print 'hello, world!'" "verify python support" {
-re "not supported.*$gdb_prompt $" {
unsupported "python support is disabled"
return -1
}
-re "$gdb_prompt $" {}
}
test_value_creation
test_value_numeric_ops
test_value_boolean
test_value_compare
# The following tests require execution.
if ![runto_main] then {
fail "Can't run to main"
return 0
}
test_value_in_inferior
|