Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-symbol.exp
1 # Copyright (C) 2010-2020 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 load_lib gdb-python.exp
20
21 standard_testfile py-symbol.c py-symbol-2.c
22
23 set opts { debug additional_flags=-DUSE_TWO_FILES }
24 if {[prepare_for_testing "failed to prepare" $testfile \
25 [list $srcfile $srcfile2] $opts]} {
26 return -1
27 }
28
29 # Skip all tests if Python scripting is not enabled.
30 if { [skip_python_tests] } { continue }
31
32 # Check that we find all static symbols before the inferior has
33 # started, at which point some of the symtabs might not have been
34 # expanded.
35 gdb_test "python print (len (gdb.lookup_static_symbols ('rr')))" \
36 "2" "print (len (gdb.lookup_static_symbols ('rr')))"
37
38 # Restart so we don't have expanded symtabs after the previous test.
39 clean_restart ${binfile}
40
41 # Test looking up a global symbol before we runto_main as this is the
42 # point where we don't have a current frame, and we don't want to
43 # require one.
44 gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
45 gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
46 gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
47
48 gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
49 "print value of main"
50
51 set qq_line [gdb_get_line_number "line of qq"]
52 gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
53 "print line number of qq"
54
55 gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
56 "print value of qq"
57
58 gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
59 "False" \
60 "print whether qq needs a frame"
61
62 # Similarly, test looking up a static symbol before we runto_main.
63 set rr_line [gdb_get_line_number "line of rr"]
64 gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
65 "lookup_global_symbol for static var"
66
67 gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
68 "print line number of rr"
69
70 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
71 "print value of rr"
72
73 gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
74 "False" \
75 "print whether rr needs a frame"
76
77 gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
78 "True" "lookup_static_symbol for nonexistent var"
79
80 gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
81 "True" "lookup_static_symbol for global var"
82
83 if ![runto_main] then {
84 fail "can't run to main"
85 return 0
86 }
87
88 global hex decimal
89
90 gdb_breakpoint [gdb_get_line_number "Block break here."]
91 gdb_continue_to_breakpoint "Block break here."
92 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
93 gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
94
95 # Test is_argument attribute.
96 gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
97 gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
98 gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
99 gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
100 gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
101
102 # Test is_function attribute.
103 gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
104 gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
105 gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
106 gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
107 gdb_test "python print (func.is_function)" "True" "test func.is_function"
108
109 # Test attributes of func.
110 gdb_test "python print (func.name)" "func" "test func.name"
111 gdb_test "python print (func.print_name)" "func" "test func.print_name"
112 gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
113 gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
114
115 # Stop in a second file and ensure we find its local static symbol.
116 gdb_breakpoint "function_in_other_file"
117 gdb_continue_to_breakpoint "function_in_other_file"
118 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \
119 "print value of rr from other file"
120 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
121 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the other file"
122 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
123 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the other file"
124
125 # Now continue back to the first source file.
126 set linenum [gdb_get_line_number "Break at end."]
127 gdb_breakpoint "$srcfile:$linenum"
128 gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
129 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
130
131 # Check that we find the static sybol local to this file over the
132 # static symbol from the second source file.
133 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
134 "print value of rr from main file"
135 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
136 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the main file"
137 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
138 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the main file"
139
140 # Test is_variable attribute.
141 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
142 gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
143 gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
144 gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
145 gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
146
147 # Test attributes of a.
148 gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
149
150 gdb_test "python print (a\[0\].value())" \
151 "symbol requires a frame to compute its value.*"\
152 "try to print value of a without a frame"
153 gdb_test "python print (a\[0\].value(frame))" "0" \
154 "print value of a"
155 gdb_test "python print (a\[0\].needs_frame)" "True" \
156 "print whether a needs a frame"
157
158 # Test is_constant attribute
159 gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
160 gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
161 gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
162 gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
163 gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
164
165 # Test attributes of t.
166 gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
167
168 # Test type attribute.
169 gdb_test "python print (t\[0\].type)" "enum tag" "get type"
170
171 # Test symtab attribute.
172 if { [is_remote host] } {
173 set py_symbol_c [string_to_regexp $srcfile]
174 } else {
175 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
176 }
177 gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
178
179 # C++ tests
180 # Recompile binary.
181 lappend opts c++
182 if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \
183 [list $srcfile $srcfile2] $opts]} {
184 return -1
185 }
186
187 gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
188 "True" "anon is None"
189 gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
190 "10" "print value of anon"
191
192 if ![runto_main] then {
193 fail "can't run to main"
194 return 0
195 }
196
197 gdb_breakpoint [gdb_get_line_number "Break in class."]
198 gdb_continue_to_breakpoint "Break in class."
199
200 gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
201 gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
202
203 gdb_test "python print (cplusfunc.is_variable)" \
204 "False" "Test cplusfunc.is_variable"
205 gdb_test "python print (cplusfunc.is_constant)" \
206 "False" "Test cplusfunc.is_constant"
207 gdb_test "python print (cplusfunc.is_argument)" \
208 "False" "Test cplusfunc.is_argument"
209 gdb_test "python print (cplusfunc.is_function)" \
210 "True" "Test cplusfunc.is_function"
211
212 gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
213 gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
214 gdb_test "python print (cplusfunc.linkage_name)" "SimpleClass::valueofi().*" "test method.linkage_name"
215 gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
216
217 # Test is_valid when the objfile is unloaded. This must be the last
218 # test as it unloads the object file in GDB.
219 # Start with a fresh gdb.
220 clean_restart ${binfile}
221 if ![runto_main] then {
222 fail "cannot run to main."
223 return 0
224 }
225
226 gdb_breakpoint [gdb_get_line_number "Break at end."]
227 gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
228 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
229 gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
230 delete_breakpoints
231 gdb_unload
232 gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
233 gdb_test_no_output "python a = None" "test symbol destructor"
This page took 0.033237 seconds and 4 git commands to generate.