OSDN Git Service

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