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