]>
| Commit | Line | Data |
|---|---|---|
| 30243af8 | 1 | # Copyright (C) 2010-2025 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 | ||
| d82e5429 | 21 | require allow_python_tests |
| 79749205 | 22 | |
| 1c7d9f96 | 23 | standard_testfile py-symbol.c py-symbol-2.c py-symbol-3.c |
| b4a58790 | 24 | |
| 09ff83af AB |
25 | set opts { debug additional_flags=-DUSE_TWO_FILES } |
| 26 | if {[prepare_for_testing "failed to prepare" $testfile \ | |
| 1c7d9f96 | 27 | [list $srcfile $srcfile2 $srcfile3] $opts]} { |
| f3e9a817 PM |
28 | return -1 |
| 29 | } | |
| 30 | ||
| 99cc6b2a TV |
31 | set readnow_p [readnow] |
| 32 | ||
| 086baaf1 AB |
33 | # Check that we find all static symbols before the inferior has |
| 34 | # started, at which point some of the symtabs might not have been | |
| 35 | # expanded. | |
| 36 | gdb_test "python print (len (gdb.lookup_static_symbols ('rr')))" \ | |
| 37 | "2" "print (len (gdb.lookup_static_symbols ('rr')))" | |
| 38 | ||
| 1c7d9f96 TT |
39 | # This test does not make sense when readnow is in effect. |
| 40 | if {!$readnow_p} { | |
| 41 | # Make sure that the global symbol's symtab was not expanded. | |
| 42 | gdb_test_no_output "pipe maint info symtab | grep \"name.*py-symbol-3.c\"" \ | |
| 43 | "global rr symtab was not expanded" | |
| 44 | } | |
| 45 | ||
| 086baaf1 | 46 | # Restart so we don't have expanded symtabs after the previous test. |
| c1e9f145 | 47 | clean_restart ${::testfile} |
| 086baaf1 | 48 | |
| 6e6fbe60 DE |
49 | # Test looking up a global symbol before we runto_main as this is the |
| 50 | # point where we don't have a current frame, and we don't want to | |
| 51 | # require one. | |
| 52 | gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1 | |
| bb2bd584 MBB |
53 | gdb_test "python print (repr (main_func))" "<gdb.Symbol print_name=main>" \ |
| 54 | "test main_func.__repr__" | |
| cdc7edd7 LM |
55 | gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function" |
| 56 | gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")" | |
| 6e6fbe60 | 57 | |
| 9325cb04 | 58 | gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \ |
| f0823d2c TT |
59 | "print value of main" |
| 60 | ||
| 64e7d9dd | 61 | set qq_line [gdb_get_line_number "line of qq"] |
| 9325cb04 | 62 | gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \ |
| 64e7d9dd TT |
63 | "print line number of qq" |
| 64 | ||
| 9325cb04 | 65 | gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \ |
| f0823d2c TT |
66 | "print value of qq" |
| 67 | ||
| 9325cb04 | 68 | gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \ |
| 09fa21bd | 69 | "False" \ |
| f0823d2c TT |
70 | "print whether qq needs a frame" |
| 71 | ||
| 09ff83af | 72 | # Similarly, test looking up a static symbol before we runto_main. |
| 1c7d9f96 | 73 | gdb_test "python print (gdb.lookup_global_symbol ('qqrr') is None)" "True" \ |
| 2906593f CB |
74 | "lookup_global_symbol for static var" |
| 75 | ||
| d0aa7406 SM |
76 | # Either "rr" static symbol is a valid result. |
| 77 | set rr_line_1 [gdb_get_line_number "line of rr"] | |
| 78 | set rr_line_2 [gdb_get_line_number "line of other rr" py-symbol-2.c] | |
| 79 | gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "($rr_line_1|$rr_line_2)" | |
| 80 | gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "(42|99)" | |
| 2906593f CB |
81 | |
| 82 | gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \ | |
| 83 | "False" \ | |
| 84 | "print whether rr needs a frame" | |
| 85 | ||
| 86 | gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \ | |
| 87 | "True" "lookup_static_symbol for nonexistent var" | |
| 88 | ||
| 89 | gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \ | |
| 90 | "True" "lookup_static_symbol for global var" | |
| f0823d2c | 91 | |
| b0e16ca5 | 92 | if {![runto_main]} { |
| f3e9a817 PM |
93 | return 0 |
| 94 | } | |
| 95 | ||
| 96 | global hex decimal | |
| 97 | ||
| 98 | gdb_breakpoint [gdb_get_line_number "Block break here."] | |
| 99 | gdb_continue_to_breakpoint "Block break here." | |
| 100 | gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0 | |
| 101 | gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0 | |
| 102 | ||
| 103 | # Test is_argument attribute. | |
| 985c818c | 104 | gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0 |
| cdc7edd7 LM |
105 | gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable" |
| 106 | gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant" | |
| 107 | gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument" | |
| 108 | gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function" | |
| f3e9a817 PM |
109 | |
| 110 | # Test is_function attribute. | |
| 985c818c | 111 | gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0 |
| cdc7edd7 LM |
112 | gdb_test "python print (func.is_variable)" "False" "test func.is_variable" |
| 113 | gdb_test "python print (func.is_constant)" "False" "test func.is_constant" | |
| 114 | gdb_test "python print (func.is_argument)" "False" "test func.is_argument" | |
| 115 | gdb_test "python print (func.is_function)" "True" "test func.is_function" | |
| 985c818c DE |
116 | |
| 117 | # Test attributes of func. | |
| cdc7edd7 LM |
118 | gdb_test "python print (func.name)" "func" "test func.name" |
| 119 | gdb_test "python print (func.print_name)" "func" "test func.print_name" | |
| 120 | gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name" | |
| 121 | gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class" | |
| 30243af8 | 122 | gdb_test "python print (func.domain == gdb.SYMBOL_FUNCTION_DOMAIN)" "True" "test func.domain" |
| f3e9a817 | 123 | |
| 09ff83af AB |
124 | # Stop in a second file and ensure we find its local static symbol. |
| 125 | gdb_breakpoint "function_in_other_file" | |
| 126 | gdb_continue_to_breakpoint "function_in_other_file" | |
| 127 | gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \ | |
| 128 | "print value of rr from other file" | |
| 27c6f127 TT |
129 | # GDB doesn't really guarantee the order of these, so sort the values. |
| 130 | gdb_test_no_output "python rrs = gdb.lookup_static_symbols ('rr')" \ | |
| 131 | "fetch all rr symbols, from the other file" | |
| 132 | gdb_test "python print (sorted(\[int(x.value()) for x in rrs\]))" \ | |
| 133 | "\\\[42, 99\\\]" \ | |
| 134 | "print values of all 'rr' symbols, from the other file" | |
| 09ff83af AB |
135 | |
| 136 | # Now continue back to the first source file. | |
| 137 | set linenum [gdb_get_line_number "Break at end."] | |
| 138 | gdb_breakpoint "$srcfile:$linenum" | |
| 985c818c | 139 | gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*" |
| f3e9a817 PM |
140 | gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0 |
| 141 | ||
| 09ff83af AB |
142 | # Check that we find the static sybol local to this file over the |
| 143 | # static symbol from the second source file. | |
| 144 | gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \ | |
| 145 | "print value of rr from main file" | |
| 27c6f127 TT |
146 | # This should be consistent with the first file. |
| 147 | gdb_test_no_output "python rrs = gdb.lookup_static_symbols ('rr')" \ | |
| 148 | "fetch all rr symbols, from the main file" | |
| 149 | gdb_test "python print (sorted(\[int(x.value()) for x in rrs\]))" \ | |
| 150 | "\\\[42, 99\\\]" \ | |
| 151 | "print values of all 'rr' symbols, from the main file" | |
| 09ff83af | 152 | |
| f3e9a817 PM |
153 | # Test is_variable attribute. |
| 154 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 | |
| cdc7edd7 LM |
155 | gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable" |
| 156 | gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant" | |
| 157 | gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument" | |
| 158 | gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function" | |
| 985c818c DE |
159 | |
| 160 | # Test attributes of a. | |
| cdc7edd7 | 161 | gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class" |
| f3e9a817 | 162 | |
| 9325cb04 | 163 | gdb_test "python print (a\[0\].value())" \ |
| f0823d2c TT |
164 | "symbol requires a frame to compute its value.*"\ |
| 165 | "try to print value of a without a frame" | |
| 9325cb04 | 166 | gdb_test "python print (a\[0\].value(frame))" "0" \ |
| f0823d2c | 167 | "print value of a" |
| 9325cb04 | 168 | gdb_test "python print (a\[0\].needs_frame)" "True" \ |
| f0823d2c TT |
169 | "print whether a needs a frame" |
| 170 | ||
| f3e9a817 | 171 | # Test is_constant attribute |
| 985c818c | 172 | gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0 |
| cdc7edd7 LM |
173 | gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable" |
| 174 | gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant" | |
| 175 | gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument" | |
| 176 | gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function" | |
| 985c818c DE |
177 | |
| 178 | # Test attributes of t. | |
| cdc7edd7 | 179 | gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class" |
| 457e09f0 DE |
180 | |
| 181 | # Test type attribute. | |
| cdc7edd7 | 182 | gdb_test "python print (t\[0\].type)" "enum tag" "get type" |
| 457e09f0 DE |
183 | |
| 184 | # Test symtab attribute. | |
| 65d7b369 YQ |
185 | if { [is_remote host] } { |
| 186 | set py_symbol_c [string_to_regexp $srcfile] | |
| 187 | } else { | |
| 188 | set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}] | |
| 189 | } | |
| cdc7edd7 | 190 | gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab" |
| f3e9a817 PM |
191 | |
| 192 | # C++ tests | |
| 193 | # Recompile binary. | |
| 09ff83af | 194 | lappend opts c++ |
| c1e9f145 TV |
195 | set testfile $testfile-cxx |
| 196 | set binfile [standard_output_file $testfile] | |
| 197 | if { [prepare_for_testing "failed to prepare" $testfile \ | |
| 198 | [list $srcfile $srcfile2] $opts] } { | |
| f873dd7a DE |
199 | return -1 |
| 200 | } | |
| f3e9a817 | 201 | |
| 2906593f CB |
202 | gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \ |
| 203 | "True" "anon is None" | |
| 204 | gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \ | |
| 205 | "10" "print value of anon" | |
| 206 | ||
| b0e16ca5 | 207 | if {![runto_main]} { |
| f3e9a817 PM |
208 | return 0 |
| 209 | } | |
| 210 | ||
| 211 | gdb_breakpoint [gdb_get_line_number "Break in class."] | |
| 212 | gdb_continue_to_breakpoint "Break in class." | |
| 213 | ||
| 985c818c DE |
214 | gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0 |
| 215 | gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0 | |
| 216 | ||
| 217 | gdb_test "python print (cplusfunc.is_variable)" \ | |
| 218 | "False" "Test cplusfunc.is_variable" | |
| 219 | gdb_test "python print (cplusfunc.is_constant)" \ | |
| 220 | "False" "Test cplusfunc.is_constant" | |
| 221 | gdb_test "python print (cplusfunc.is_argument)" \ | |
| 222 | "False" "Test cplusfunc.is_argument" | |
| 223 | gdb_test "python print (cplusfunc.is_function)" \ | |
| 224 | "True" "Test cplusfunc.is_function" | |
| 225 | ||
| cdc7edd7 LM |
226 | gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name" |
| 227 | gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name" | |
| bcfe6157 | 228 | gdb_test "python print (cplusfunc.linkage_name)" "_ZN11SimpleClass8valueofiEv.*" "test method.linkage_name" |
| cdc7edd7 | 229 | gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class" |
| 29703da4 PM |
230 | |
| 231 | # Test is_valid when the objfile is unloaded. This must be the last | |
| 232 | # test as it unloads the object file in GDB. | |
| 233 | # Start with a fresh gdb. | |
| c1e9f145 | 234 | clean_restart ${::testfile} |
| b0e16ca5 | 235 | if {![runto_main]} { |
| 29703da4 PM |
236 | return 0 |
| 237 | } | |
| 985c818c | 238 | |
| 29703da4 | 239 | gdb_breakpoint [gdb_get_line_number "Break at end."] |
| 985c818c | 240 | gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*" |
| 29703da4 | 241 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 |
| cdc7edd7 | 242 | gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity" |
| 29703da4 PM |
243 | delete_breakpoints |
| 244 | gdb_unload | |
| cdc7edd7 | 245 | gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity" |
| 9f058c10 | 246 | gdb_test_no_output "python a = None" "test symbol destructor" |
| 6ac5237c HD |
247 | |
| 248 | # Test gdb.Symbol domain categories | |
| ad4bd975 | 249 | gdb_test "python print (gdb.SYMBOL_UNDEF_DOMAIN)" \ |
| 6ac5237c | 250 | "0" "test gdb.SYMBOL_UNDEF_DOMAIN" |
| ad4bd975 | 251 | gdb_test "python print (gdb.SYMBOL_VAR_DOMAIN)" \ |
| 6ac5237c | 252 | "1" "test gdb.SYMBOL_VAR_DOMAIN" |
| ad4bd975 | 253 | gdb_test "python print (gdb.SYMBOL_STRUCT_DOMAIN)" \ |
| 6ac5237c | 254 | "2" "test gdb.SYMBOL_STRUCT_DOMAIN" |
| ad4bd975 | 255 | gdb_test "python print (gdb.SYMBOL_MODULE_DOMAIN)" \ |
| 6ac5237c | 256 | "3" "test gdb.SYMBOL_MODULE_DOMAIN" |
| ad4bd975 | 257 | gdb_test "python print (gdb.SYMBOL_LABEL_DOMAIN)" \ |
| 6ac5237c | 258 | "4" "test gdb.SYMBOL_LABEL_DOMAIN" |
| ad4bd975 | 259 | gdb_test "python print (gdb.SYMBOL_COMMON_BLOCK_DOMAIN)" \ |
| 6ac5237c HD |
260 | "5" "test gdb.SYMBOL_COMMON_BLOCK_DOMAIN" |
| 261 | ||
| 262 | # Test gdb.Symbol address class categories | |
| ad4bd975 | 263 | gdb_test "python print (gdb.SYMBOL_LOC_UNDEF)" \ |
| 6ac5237c | 264 | "0" "test gdb.SYMBOL_LOC_UNDEF" |
| ad4bd975 | 265 | gdb_test "python print (gdb.SYMBOL_LOC_CONST)" \ |
| 6ac5237c | 266 | "1" "test gdb.SYMBOL_LOC_CONST" |
| ad4bd975 | 267 | gdb_test "python print (gdb.SYMBOL_LOC_STATIC)" \ |
| 6ac5237c | 268 | "2" "test gdb.SYMBOL_LOC_STATIC" |
| ad4bd975 | 269 | gdb_test "python print (gdb.SYMBOL_LOC_REGISTER)" \ |
| 6ac5237c | 270 | "3" "test gdb.SYMBOL_LOC_REGISTER" |
| ad4bd975 | 271 | gdb_test "python print (gdb.SYMBOL_LOC_ARG)" \ |
| 6ac5237c | 272 | "4" "test gdb.SYMBOL_LOC_ARG" |
| ad4bd975 | 273 | gdb_test "python print (gdb.SYMBOL_LOC_REF_ARG)" \ |
| 6ac5237c | 274 | "5" "test gdb.SYMBOL_LOC_REF_ARG" |
| ad4bd975 | 275 | gdb_test "python print (gdb.SYMBOL_LOC_REGPARM_ADDR)" \ |
| 6ac5237c | 276 | "6" "test gdb.SYMBOL_LOC_REGPARM_ADDR" |
| ad4bd975 | 277 | gdb_test "python print (gdb.SYMBOL_LOC_LOCAL)" \ |
| 6ac5237c | 278 | "7" "test gdb.SYMBOL_LOC_LOCAL" |
| ad4bd975 | 279 | gdb_test "python print (gdb.SYMBOL_LOC_TYPEDEF)" \ |
| 6ac5237c | 280 | "8" "test gdb.SYMBOL_LOC_TYPEDEF" |
| ad4bd975 | 281 | gdb_test "python print (gdb.SYMBOL_LOC_LABEL)" \ |
| 6ac5237c | 282 | "9" "test gdb.SYMBOL_LOC_LABEL" |
| ad4bd975 | 283 | gdb_test "python print (gdb.SYMBOL_LOC_BLOCK)" \ |
| 6ac5237c | 284 | "10" "test gdb.SYMBOL_LOC_BLOCK" |
| ad4bd975 | 285 | gdb_test "python print (gdb.SYMBOL_LOC_CONST_BYTES)" \ |
| 6ac5237c | 286 | "11" "test gdb.SYMBOL_LOC_CONST_BYTES" |
| ad4bd975 | 287 | gdb_test "python print (gdb.SYMBOL_LOC_UNRESOLVED)" \ |
| 6ac5237c | 288 | "12" "test gdb.SYMBOL_LOC_UNRESOLVED" |
| ad4bd975 | 289 | gdb_test "python print (gdb.SYMBOL_LOC_OPTIMIZED_OUT)" \ |
| 6ac5237c | 290 | "13" "test gdb.SYMBOL_LOC_OPTIMIZED_OUT" |
| ad4bd975 | 291 | gdb_test "python print (gdb.SYMBOL_LOC_COMPUTED)" \ |
| 6ac5237c | 292 | "14" "test gdb.SYMBOL_LOC_COMPUTED" |
| ad4bd975 | 293 | gdb_test "python print (gdb.SYMBOL_LOC_COMMON_BLOCK)" \ |
| 6ac5237c | 294 | "15" "test gdb.SYMBOL_LOC_COMMON_BLOCK" |