Commit | Line | Data |
---|---|---|
42a4f53d | 1 | # Copyright (C) 2010-2019 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 | ||
b4a58790 TT |
21 | standard_testfile |
22 | ||
5b362f04 | 23 | if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} { |
f3e9a817 PM |
24 | return -1 |
25 | } | |
26 | ||
f3e9a817 PM |
27 | # Skip all tests if Python scripting is not enabled. |
28 | if { [skip_python_tests] } { continue } | |
29 | ||
6e6fbe60 DE |
30 | # Test looking up a global symbol before we runto_main as this is the |
31 | # point where we don't have a current frame, and we don't want to | |
32 | # require one. | |
33 | gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1 | |
cdc7edd7 LM |
34 | gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function" |
35 | gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")" | |
6e6fbe60 | 36 | |
9325cb04 | 37 | gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \ |
f0823d2c TT |
38 | "print value of main" |
39 | ||
64e7d9dd | 40 | set qq_line [gdb_get_line_number "line of qq"] |
9325cb04 | 41 | gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \ |
64e7d9dd TT |
42 | "print line number of qq" |
43 | ||
9325cb04 | 44 | gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \ |
f0823d2c TT |
45 | "print value of qq" |
46 | ||
9325cb04 | 47 | gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \ |
09fa21bd | 48 | "False" \ |
f0823d2c TT |
49 | "print whether qq needs a frame" |
50 | ||
2906593f CB |
51 | set rr_line [gdb_get_line_number "line of rr"] |
52 | gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \ | |
53 | "lookup_global_symbol for static var" | |
54 | ||
55 | gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \ | |
56 | "print line number of rr" | |
57 | ||
58 | gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \ | |
59 | "print value of rr" | |
60 | ||
61 | gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \ | |
62 | "False" \ | |
63 | "print whether rr needs a frame" | |
64 | ||
65 | gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \ | |
66 | "True" "lookup_static_symbol for nonexistent var" | |
67 | ||
68 | gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \ | |
69 | "True" "lookup_static_symbol for global var" | |
f0823d2c | 70 | |
f3e9a817 | 71 | if ![runto_main] then { |
bc6c7af4 | 72 | fail "can't run to main" |
f3e9a817 PM |
73 | return 0 |
74 | } | |
75 | ||
76 | global hex decimal | |
77 | ||
78 | gdb_breakpoint [gdb_get_line_number "Block break here."] | |
79 | gdb_continue_to_breakpoint "Block break here." | |
80 | gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0 | |
81 | gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0 | |
82 | ||
83 | # Test is_argument attribute. | |
985c818c | 84 | gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0 |
cdc7edd7 LM |
85 | gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable" |
86 | gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant" | |
87 | gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument" | |
88 | gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function" | |
f3e9a817 PM |
89 | |
90 | # Test is_function attribute. | |
985c818c | 91 | gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0 |
cdc7edd7 LM |
92 | gdb_test "python print (func.is_variable)" "False" "test func.is_variable" |
93 | gdb_test "python print (func.is_constant)" "False" "test func.is_constant" | |
94 | gdb_test "python print (func.is_argument)" "False" "test func.is_argument" | |
95 | gdb_test "python print (func.is_function)" "True" "test func.is_function" | |
985c818c DE |
96 | |
97 | # Test attributes of func. | |
cdc7edd7 LM |
98 | gdb_test "python print (func.name)" "func" "test func.name" |
99 | gdb_test "python print (func.print_name)" "func" "test func.print_name" | |
100 | gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name" | |
101 | gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class" | |
f3e9a817 PM |
102 | |
103 | gdb_breakpoint [gdb_get_line_number "Break at end."] | |
985c818c | 104 | gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*" |
f3e9a817 PM |
105 | gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0 |
106 | ||
107 | # Test is_variable attribute. | |
108 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 | |
cdc7edd7 LM |
109 | gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable" |
110 | gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant" | |
111 | gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument" | |
112 | gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function" | |
985c818c DE |
113 | |
114 | # Test attributes of a. | |
cdc7edd7 | 115 | gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class" |
f3e9a817 | 116 | |
9325cb04 | 117 | gdb_test "python print (a\[0\].value())" \ |
f0823d2c TT |
118 | "symbol requires a frame to compute its value.*"\ |
119 | "try to print value of a without a frame" | |
9325cb04 | 120 | gdb_test "python print (a\[0\].value(frame))" "0" \ |
f0823d2c | 121 | "print value of a" |
9325cb04 | 122 | gdb_test "python print (a\[0\].needs_frame)" "True" \ |
f0823d2c TT |
123 | "print whether a needs a frame" |
124 | ||
f3e9a817 | 125 | # Test is_constant attribute |
985c818c | 126 | gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0 |
cdc7edd7 LM |
127 | gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable" |
128 | gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant" | |
129 | gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument" | |
130 | gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function" | |
985c818c DE |
131 | |
132 | # Test attributes of t. | |
cdc7edd7 | 133 | gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class" |
457e09f0 DE |
134 | |
135 | # Test type attribute. | |
cdc7edd7 | 136 | gdb_test "python print (t\[0\].type)" "enum tag" "get type" |
457e09f0 DE |
137 | |
138 | # Test symtab attribute. | |
65d7b369 YQ |
139 | if { [is_remote host] } { |
140 | set py_symbol_c [string_to_regexp $srcfile] | |
141 | } else { | |
142 | set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}] | |
143 | } | |
cdc7edd7 | 144 | gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab" |
f3e9a817 PM |
145 | |
146 | # C++ tests | |
147 | # Recompile binary. | |
f873dd7a | 148 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}-cxx" executable "debug c++"] != "" } { |
84c93cd5 | 149 | untested "failed to compile in C++ mode" |
f873dd7a DE |
150 | return -1 |
151 | } | |
f3e9a817 PM |
152 | |
153 | # Start with a fresh gdb. | |
154 | gdb_exit | |
155 | gdb_start | |
156 | gdb_reinitialize_dir $srcdir/$subdir | |
db8e4570 | 157 | gdb_load ${binfile}-cxx |
f3e9a817 | 158 | |
2906593f CB |
159 | gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \ |
160 | "True" "anon is None" | |
161 | gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \ | |
162 | "10" "print value of anon" | |
163 | ||
f3e9a817 | 164 | if ![runto_main] then { |
bc6c7af4 | 165 | fail "can't run to main" |
f3e9a817 PM |
166 | return 0 |
167 | } | |
168 | ||
169 | gdb_breakpoint [gdb_get_line_number "Break in class."] | |
170 | gdb_continue_to_breakpoint "Break in class." | |
171 | ||
985c818c DE |
172 | gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0 |
173 | gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0 | |
174 | ||
175 | gdb_test "python print (cplusfunc.is_variable)" \ | |
176 | "False" "Test cplusfunc.is_variable" | |
177 | gdb_test "python print (cplusfunc.is_constant)" \ | |
178 | "False" "Test cplusfunc.is_constant" | |
179 | gdb_test "python print (cplusfunc.is_argument)" \ | |
180 | "False" "Test cplusfunc.is_argument" | |
181 | gdb_test "python print (cplusfunc.is_function)" \ | |
182 | "True" "Test cplusfunc.is_function" | |
183 | ||
cdc7edd7 LM |
184 | gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name" |
185 | gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name" | |
186 | gdb_test "python print (cplusfunc.linkage_name)" "SimpleClass::valueofi().*" "test method.linkage_name" | |
187 | gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class" | |
29703da4 PM |
188 | |
189 | # Test is_valid when the objfile is unloaded. This must be the last | |
190 | # test as it unloads the object file in GDB. | |
191 | # Start with a fresh gdb. | |
192 | clean_restart ${testfile} | |
193 | if ![runto_main] then { | |
bc6c7af4 | 194 | fail "cannot run to main." |
29703da4 PM |
195 | return 0 |
196 | } | |
985c818c | 197 | |
29703da4 | 198 | gdb_breakpoint [gdb_get_line_number "Break at end."] |
985c818c | 199 | gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*" |
29703da4 | 200 | gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0 |
cdc7edd7 | 201 | gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity" |
29703da4 PM |
202 | delete_breakpoints |
203 | gdb_unload | |
cdc7edd7 | 204 | gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity" |
9f058c10 | 205 | gdb_test_no_output "python a = None" "test symbol destructor" |