2009-12-08 Phil Muldoon <pmuldoon@redhat.com>
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
1 # Copyright (C) 2009 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 # of exposing types to Python.
18
19 if $tracelevel then {
20 strace $tracelevel
21 }
22
23 set testfile "py-type"
24 set srcfile ${testfile}.c
25 set binfile ${objdir}/${subdir}/${testfile}
26
27 # Build inferior to language specification.
28 proc build_inferior {lang} {
29 global srcdir subdir srcfile binfile testfile hex
30
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug $lang"] != "" } {
32 untested "Couldn't compile ${srcfile} in $lang mode"
33 return -1
34 }
35 }
36
37 # Restart GDB, set breakpoint and run to that breakpoint.
38 proc restart_gdb {bp} {
39 global srcdir subdir srcfile binfile testfile hex
40
41 gdb_exit
42 gdb_start
43 gdb_reinitialize_dir $srcdir/$subdir
44 gdb_load ${binfile}
45
46 if ![runto_main ] then {
47 perror "couldn't run to breakpoint"
48 return
49 }
50
51 gdb_breakpoint [gdb_get_line_number $bp]
52 gdb_continue_to_breakpoint $bp
53 }
54
55
56 # Run a command in GDB, and report a failure if a Python exception is thrown.
57 # If report_pass is true, report a pass if no exception is thrown.
58 proc gdb_py_test_silent_cmd {cmd name report_pass} {
59 global gdb_prompt
60
61 gdb_test_multiple $cmd $name {
62 -re "Traceback.*$gdb_prompt $" { fail $name }
63 -re "$gdb_prompt $" { if $report_pass { pass $name } }
64 }
65 }
66
67 proc test_fields {lang} {
68 global gdb_prompt
69
70 if {$lang == "c++"} {
71 # Test usage with a class
72 gdb_py_test_silent_cmd "print c" "print value" 1
73 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1
74 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1
75 gdb_test "python print len(fields)" "2" "Check number of fields"
76 gdb_test "python print fields\[0\].name" "c" "Check class field c name"
77 gdb_test "python print fields\[1\].name" "d" "Check class field d name"
78 }
79
80 # Test normal fields usage in structs.
81 gdb_py_test_silent_cmd "print st" "print value" 1
82 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
83 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1
84 gdb_test "python print len(fields)" "2" "Check number of fields"
85 gdb_test "python print fields\[0\].name" "a" "Check structure field a name"
86 gdb_test "python print fields\[1\].name" "b" "Check structure field b name"
87
88 # Test regression PR python/10805
89 gdb_py_test_silent_cmd "print ar" "print value" 1
90 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
91 gdb_test "python fields = ar.type.fields()"
92 gdb_test "python print len(fields)" "1" "Check the number of fields"
93 gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
94 }
95
96 proc test_base_class {} {
97 gdb_py_test_silent_cmd "print d" "print value" 1
98 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1
99 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
100 gdb_test "python print len(fields)" "3" "Check the number of fields"
101 gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
102 gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
103 }
104
105 proc test_range {} {
106
107 # Test a valid range request.
108 gdb_py_test_silent_cmd "print ar" "print value" 1
109 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
110 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
111 gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
112 gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
113
114 # Test a range request on a ranged type.
115 gdb_py_test_silent_cmd "print ar" "print value" 1
116 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
117 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
118 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
119 gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
120
121 # Test where a range does not exist.
122 gdb_py_test_silent_cmd "print st" "print value" 1
123 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
124 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
125 }
126
127
128 # Perform C Tests.
129 build_inferior "c"
130 restart_gdb "break to inspect struct and array."
131 test_fields "c"
132
133 # Perform C++ Tests.
134 build_inferior "c++"
135 restart_gdb "break to inspect struct and array."
136 test_fields "c++"
137 test_base_class
138 test_range