Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright (C) 2010-2020 Free Software Foundation, Inc. |
f3e9a817 PM |
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 | ||
a2c09bd0 DE |
19 | load_lib gdb-python.exp |
20 | ||
09ff83af | 21 | standard_testfile py-symbol.c py-symbol-2.c |
b4a58790 | 22 | |
09ff83af AB |
23 | set opts { debug additional_flags=-DUSE_TWO_FILES } |
24 | if {[prepare_for_testing "failed to prepare" $testfile \ | |
25 | [list $srcfile $srcfile2] $opts]} { | |
f3e9a817 PM |
26 | return -1 |
27 | } | |
28 | ||
f3e9a817 PM |
29 | # Skip all tests if Python scripting is not enabled. |
30 | if { [skip_python_tests] } { continue } | |
31 | ||
086baaf1 AB |
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 | ||
6e6fbe60 DE |
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 | |
cdc7edd7 LM |
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\")" | |
6e6fbe60 | 47 | |
9325cb04 | 48 | gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \ |
f0823d2c TT |
49 | "print value of main" |
50 | ||
64e7d9dd | 51 | set qq_line [gdb_get_line_number "line of qq"] |
9325cb04 | 52 | gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \ |
64e7d9dd TT |
53 | "print line number of qq" |
54 | ||
9325cb04 | 55 | gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \ |
f0823d2c TT |
56 | "print value of qq" |
57 | ||
9325cb04 | 58 | gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \ |
09fa21bd | 59 | "False" \ |
f0823d2c TT |
60 | "print whether qq needs a frame" |
61 | ||
09ff83af | 62 | # Similarly, test looking up a static symbol before we runto_main. |
2906593f CB |
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" | |
f0823d2c | 82 | |
f3e9a817 | 83 | if ![runto_main] then { |
bc6c7af4 | 84 | fail "can't run to main" |
f3e9a817 PM |
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. | |
985c818c | 96 | gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0 |
cdc7edd7 LM |
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" | |
f3e9a817 PM |
101 | |
102 | # Test is_function attribute. | |
985c818c | 103 | gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0 |
cdc7edd7 LM |
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" | |
985c818c DE |
108 | |
109 | # Test attributes of func. | |
cdc7edd7 LM |
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" | |
f3e9a817 | 114 | |
09ff83af AB |
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" | |
086baaf1 AB |
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" | |
09ff83af AB |
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" | |
985c818c | 128 | gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*" |
f3e9a817 PM |
129 | gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0 |
130 | ||
09ff83af AB |
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" | |
086baaf1 AB |
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" | |
09ff83af | 139 | |
f3e9a817 PM |
140 | # Test is_variable attribute. |
141 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 | |
cdc7edd7 LM |
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" | |
985c818c DE |
146 | |
147 | # Test attributes of a. | |
cdc7edd7 | 148 | gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class" |
f3e9a817 | 149 | |
9325cb04 | 150 | gdb_test "python print (a\[0\].value())" \ |
f0823d2c TT |
151 | "symbol requires a frame to compute its value.*"\ |
152 | "try to print value of a without a frame" | |
9325cb04 | 153 | gdb_test "python print (a\[0\].value(frame))" "0" \ |
f0823d2c | 154 | "print value of a" |
9325cb04 | 155 | gdb_test "python print (a\[0\].needs_frame)" "True" \ |
f0823d2c TT |
156 | "print whether a needs a frame" |
157 | ||
f3e9a817 | 158 | # Test is_constant attribute |
985c818c | 159 | gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0 |
cdc7edd7 LM |
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" | |
985c818c DE |
164 | |
165 | # Test attributes of t. | |
cdc7edd7 | 166 | gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class" |
457e09f0 DE |
167 | |
168 | # Test type attribute. | |
cdc7edd7 | 169 | gdb_test "python print (t\[0\].type)" "enum tag" "get type" |
457e09f0 DE |
170 | |
171 | # Test symtab attribute. | |
65d7b369 YQ |
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 | } | |
cdc7edd7 | 177 | gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab" |
f3e9a817 PM |
178 | |
179 | # C++ tests | |
180 | # Recompile binary. | |
09ff83af AB |
181 | lappend opts c++ |
182 | if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \ | |
183 | [list $srcfile $srcfile2] $opts]} { | |
f873dd7a DE |
184 | return -1 |
185 | } | |
f3e9a817 | 186 | |
2906593f CB |
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 | ||
f3e9a817 | 192 | if ![runto_main] then { |
bc6c7af4 | 193 | fail "can't run to main" |
f3e9a817 PM |
194 | return 0 |
195 | } | |
196 | ||
197 | gdb_breakpoint [gdb_get_line_number "Break in class."] | |
198 | gdb_continue_to_breakpoint "Break in class." | |
199 | ||
985c818c DE |
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 | ||
cdc7edd7 LM |
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" | |
bcfe6157 | 214 | gdb_test "python print (cplusfunc.linkage_name)" "_ZN11SimpleClass8valueofiEv.*" "test method.linkage_name" |
cdc7edd7 | 215 | gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class" |
29703da4 PM |
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. | |
09ff83af | 220 | clean_restart ${binfile} |
29703da4 | 221 | if ![runto_main] then { |
bc6c7af4 | 222 | fail "cannot run to main." |
29703da4 PM |
223 | return 0 |
224 | } | |
985c818c | 225 | |
29703da4 | 226 | gdb_breakpoint [gdb_get_line_number "Break at end."] |
985c818c | 227 | gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*" |
29703da4 | 228 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 |
cdc7edd7 | 229 | gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity" |
29703da4 PM |
230 | delete_breakpoints |
231 | gdb_unload | |
cdc7edd7 | 232 | gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity" |
9f058c10 | 233 | gdb_test_no_output "python a = None" "test symbol destructor" |