old-cross-binutils/gdb/testsuite/gdb.python/python-value.exp
Tom Tromey 2c74e83381 gdb
2009-05-27  Tom Tromey  <tromey@redhat.com>
	    Thiago Jung Bauermann  <bauerman@br.ibm.com>
	    Phil Muldoon  <pmuldoon@redhat.com>
	    Paul Pluzhnikov  <ppluzhnikov@google.com>

	* python/python.c (_initialize_python): Call
	gdbpy_initialize_types.
	(GdbMethods): Add "lookup_type".
	* python/python-value.c (value_object) <type>: New field.
	(valpy_dealloc): Decref type.
	(valpy_new): Initialize type.
	(valpy_get_type): New function.
	(value_to_value_object): Initialize type.
	(valpy_cast): New function.
	(value_object_getset): Add "type".
	(value_object_methods): Add "cast".
	* python/python-internal.h (type_to_type_object): Declare.
	(type_object_to_type): Likewise.
	(gdbpy_initialize_types): Likewise.
	(gdbpy_lookup_type): Declare.
	* Makefile.in (SUBDIR_PYTHON_OBS): Add python-type.o.
	(SUBDIR_PYTHON_SRCS): Add python-type.c.
	(python-type.o): New target.
	* python/python-type.c: New file.

gdb/doc

2009-05-27  Thiago Jung Bauermann  <bauerman@br.ibm.com>
	    Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Types In Python): New node.
	(Values From Inferior): "type" is now an attribute.
	(Python API): Update.


gdb/testsuite

2009-05-27  Thiago Jung Bauermann  <bauerman@br.ibm.com>
	    Tom Tromey  <tromey@redhat.com>
	    Pedro Alves  <pedro@codesourcery.com>
	    Paul Pluzhnikov  <ppluzhnikov@google.com>

	* gdb.python/python-template.exp: New file.
	* gdb.python/python-template.cc: New file.
	* gdb.python/python.exp (gdb_py_test_multiple): Add two objfile
	tests.
	* gdb.python/python-value.exp (py_objfile_tests): New proc.
	Call it.
	(test_value_after_death): New proc.
	* gdb.python/python-value.c (PTR): New typedef.
	(main): New variable 'x'.
2009-05-28 00:47:20 +00:00

305 lines
13 KiB
Text

# 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"
# Test address attribute is None in a non-addressable value
gdb_test "python print 'result =', i.address" "= None" "Test address attribute in non-addressable value"
}
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"
# Test address attribute
gdb_test "python print 'result =', arg0.address" "= 0x\[\[:xdigit:\]\]+" "Test address attribute"
}
# A few objfile tests.
proc test_objfiles {} {
gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'python-value' in file.filename:\n ok=True\nprint ok\nend" "True"
gdb_test "python print gdb.objfiles()\[0\].pretty_printers" "\\\[\\\]"
gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \
"pretty_printers attribute must be a list.*Error while executing Python code."
}
proc test_value_after_death {} {
# Construct a type while the inferior is still running.
gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \
"create PTR type" 1
# Kill the inferior and remove the symbols.
gdb_test "kill" "" "kill the inferior" \
"Kill the program being debugged. .y or n. $" \
"y"
gdb_test "file" "" "Discard the symbols" \
"Discard symbol table from.*y or n. $" \
"y"
# Now create a value using that type. Relies on arg0, created by
# test_value_in_inferior.
gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \
"cast arg0 to PTR" 1
# Make sure the type is deleted.
gdb_py_test_silent_cmd "python ptrtype = None" \
"delete PTR type" 1
# Now see if the value's type is still valid.
gdb_test "python print castval.type" "PTR ." \
"print value's type"
}
# 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
test_objfiles
# The following tests require execution.
if ![runto_main] then {
fail "Can't run to main"
return 0
}
test_value_in_inferior
test_value_after_death