1 # Copyright 2014-2022 Free Software Foundation, Inc.
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.
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.
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/>.
16 load_lib compile-support.exp
18 standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c
21 if { [test_compiler_info gcc*] || [test_compiler_info clang*] } {
22 lappend options additional_flags=-g3
23 lappend options additional_flags=-std=gnu++11
27 if { ![istarget x86_64-*-* ] || ![is_lp64_target] } {
28 verbose "Skipping x86_64 LOC_CONST test."
32 set srcfilesoptions [list ${srcfile} ${options}]
33 if { $srcfile3 != "" } {
34 lappend srcfilesoptions $srcfile3 {}
36 set srcfile4options "nodebug c++"
37 lappend srcfilesoptions $srcfile4 $srcfile4options
38 if { [eval build_executable_from_specs ${testfile}.exp $testfile {$options} ${srcfilesoptions}] } {
42 clean_restart ${testfile}
45 # FIXME: Right now, for C++ we just duplicate the C tests, but force
48 gdb_test_no_output "set language c++" \
55 gdb_breakpoint [gdb_get_line_number "break-here"]
56 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
58 if {[skip_compile_feature_untested "var = 0"]} {
63 # Test delimiter for code, and arguments.
67 gdb_test_no_output "compile code globalvar = SOME_MACRO;" \
68 "set variable from macro"
69 gdb_test "p globalvar" " = 23" "expect 23"
71 gdb_test_no_output "compile code globalvar = ARG_MACRO(0, 0);" \
72 "set variable from function-like macro"
73 gdb_test "p globalvar" " = -1" "expect -1"
75 gdb_test_no_output "compile code globalvar = 42;" "set variable"
76 gdb_test "p globalvar" " = 42" "expect 42"
78 gdb_test_no_output "compile code globalvar *= 2;" "modify variable"
79 gdb_test "p globalvar" " = 84" "expect 84"
81 gdb_test_no_output "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \
82 "use external source file"
83 gdb_test "p globalvar" " = 7" "expect 7"
85 gdb_test_no_output "compile code func_static (2);" "call static function"
86 gdb_test "p globalvar" " = 9" "expect 9"
87 gdb_test_no_output "compile code func_global (1);" "call global function"
88 gdb_test "p globalvar" " = 8" "expect 8"
91 "compile code globalvar = (sizeof (ulonger) == sizeof (long))" \
92 "compute size of ulonger"
93 gdb_test "p globalvar" " = 1" "check size of ulonger"
95 "compile code globalvar = (sizeof (longer) == sizeof (long))" \
96 "compute size of longer"
97 gdb_test "p globalvar" " = 1" "check size of longer"
98 gdb_test_no_output "compile code globalvar = MINUS_1"
99 gdb_test "p globalvar" " = -1" "check MINUS_1"
101 gdb_test_no_output "compile code globalvar = static_local"
102 gdb_test "p globalvar" " = 77000" "check static_local"
105 "compile code static int staticvar = 5; intptr = &staticvar" \
106 "do not keep jit in memory"
107 gdb_test "p *intptr" "Cannot access memory at address 0x\[0-9a-f\]+" \
110 gdb_test "compile code func_doesnotexist ();" "error: \'func_doesnotexist\' was not declared in this scope.*"
112 gdb_test "compile code *(volatile int *) 0 = 0;" \
113 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB remains in the frame where the signal was received\\.\r\n.*" \
114 "compile code segfault first"
116 "\r\n#0 \[^\r\n\]* in _gdb_expr \[^\r\n\]*\r\n#1 <function called from gdb>\r\n.*"
120 gdb_test_multiple $test $test {
121 -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
122 set infcall_pc $expect_out(1,string)
127 gdb_test "info sym $infcall_pc" "\r\n_gdb_expr.*" "info sym found"
128 gdb_test "return" "\r\n#0 main .*" "return" \
129 "Make _gdb_expr\\(__gdb_regs\\*\\) return now\\? \\(y or n\\) " "y"
130 gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found"
132 gdb_test_no_output "set unwindonsignal on"
133 gdb_test "compile code *(volatile int *) 0 = 0;" \
134 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*" \
135 "compile code segfault second"
137 # C++ Specific tests.
138 ## Public methods and members
140 gdb_test "print foovar.public_var" "42" \
141 "Test compile code foovar.public_var = 42 setting."
142 gdb_test_no_output "compile code foovar.public_var = 43;" \
143 "set foobar.public_var to 43"
144 gdb_test "print foovar.public_var" "43" \
145 "Test compile code foovar.public_var = 43 setting."
146 gdb_test "print foovar.public_method ()" "43" \
147 "Test compile code foovar.public_method = 43 setting."
149 ## Private methods and members
150 gdb_test_no_output "compile code foovar.set_private_var (84);" \
151 "Call class function to set private_var"
152 gdb_test "print foovar.private_var" "84" \
153 "Test compile code foovar.set_private_var = 84 setting."
154 gdb_test_no_output "compile code foovar.private_var = 85" \
155 "Directly set a private member in GDB compile5"
156 gdb_test "print foovar.private_var" "85" \
157 "Test compile code foovar.set_private_var = 85 setting."
159 ## Simple inheritance
160 CompileExpression::new "var"
161 CompileExpression::test "class Baz: public Foo {public: int z = 12;}; Baz bazvar; bazvar.z = 24; var = bazvar.z" 24 -explicit
162 ## Multiple inheritance
163 CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return 42;}}; MI MIVar; var = MIVar.pure_virt();" 42 -explicit
164 CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return Base::return_value() + 42;}}; MI MIVar; var = MIVar.pure_virt();" 43 -explicit
165 CompileExpression::test "class Base3 {public: int z = 99;}; class MI: public Base, public Base3 {int pure_virt () {return Base3::z + 42;}}; MI MIVar; var = MIVar.pure_virt();" 141 -explicit
167 gdb_test "p localvar" " = 50" "expect localvar 50"
169 gdb_test_no_output "compile code localvar = 12;" "set localvar"
170 gdb_test "p localvar" " = 12" "expect 12"
172 gdb_test_no_output "compile code localvar *= 2;" "modify localvar"
173 gdb_test "p localvar" " = 24" "expect 24"
175 gdb_test_no_output "compile code localvar = shadowed" \
177 gdb_test "p localvar" " = 52" "expect 52"
179 gdb_test_no_output "compile code localvar = externed"
180 gdb_test "p localvar" " = 7" "test extern in inner scope"
182 gdb_test_no_output "compile code vla\[2\] = 7"
183 gdb_test "p vla\[2\]" " = 7"
185 "compile code localvar = (sizeof (vla) == bound * sizeof (vla\[0\]))"
186 gdb_test "p localvar" " = 1"
189 # Test setting fields and also many different types.
192 gdb_test_no_output "compile code struct_object.selffield = (struct_type*)&struct_object"
193 gdb_test "print struct_object.selffield == &struct_object" " = true"
195 gdb_test_no_output "compile code struct_object.charfield = 1"
196 gdb_test "print struct_object.charfield" " = 1 '\\\\001'"
197 gdb_test_no_output "compile code struct_object.ucharfield = 1"
198 gdb_test "print struct_object.ucharfield" " = 1 '\\\\001'"
200 foreach {field value} {
212 gdb_test_no_output "compile code struct_object.$field = $value"
213 gdb_test "print struct_object.$field" " = $value"
216 gdb_test_no_output "compile code struct_object.arrayfield\[2\] = 7"
217 gdb_test "print struct_object.arrayfield" \
218 " = \\{0, 0, 7, 0, 0\\}"
220 gdb_test_no_output "compile code struct_object.complexfield = 7 + 5i"
221 gdb_test "print struct_object.complexfield" " = 7 \\+ 5i"
223 gdb_test_no_output "compile code struct_object.boolfield = 1"
224 gdb_test "print struct_object.boolfield" " = true"
226 gdb_test_no_output "compile code struct_object.vectorfield\[2\] = 7"
227 gdb_test "print struct_object.vectorfield" \
228 " = \\{0, 0, 7, 0\\}"
230 gdb_test_no_output "compile code union_object.typedeffield = 7"
231 gdb_test "print union_object.typedeffield" " = 7"
232 gdb_test "print union_object.intfield" " = 7"
235 # LOC_UNRESOLVED tests.
237 gdb_test "print unresolved" " = 20"
238 gdb_test "compile code globalvar = unresolved;"
239 gdb_test "print globalvar" " = 20" "print unresolved value"
241 # Test shadowing with global and static variables.
243 gdb_test_no_output "compile code globalshadow += 1;"
244 gdb_test "print globalshadow" " = 101"
245 gdb_test_no_output "compile code extern int globalshadow; globalshadow += 5;"
246 gdb_test "print 'compile-cplus.c'::globalshadow" " = 15"
247 gdb_test "print globalshadow" " = 101" "print globalshadow second time"
248 gdb_test_no_output "compile code staticshadow += 2;"
249 gdb_test "print staticshadow" " = 202"
250 # "extern int staticshadow;" cannot access static variable.
252 # Raw code cannot refer to locals.
253 # As it references global variable we need the #pragma.
254 # For #pragma we need multiline input.
255 gdb_test_multiple "compile code -r" "compile code -r multiline 1" { -re "\r\n>$" {} }
256 gdb_test_multiple "void _gdb_expr(void) {" "compile code -r multiline 2" { -re "\r\n>$" {} }
257 gdb_test_multiple "#pragma GCC push_user_expression" "compile code -r multiline 3" { -re "\r\n>$" {} }
258 gdb_test_multiple " globalshadow = 77000;" "compile code -r multiline 4" { -re "\r\n>$" {} }
259 gdb_test_multiple "#pragma GCC pop_user_expression" "compile code -r multiline 5" { -re "\r\n>$" {} }
260 gdb_test_multiple "}" "compile code -r multiline 6" { -re "\r\n>$" {} }
261 gdb_test_no_output "end" "compile code -r multiline 7"
262 gdb_test "print 'compile-cplus.c'::globalshadow" " = 77000" \
263 "check globalshadow with -r"
265 # Test GOT vs. resolving jit function pointers.
267 gdb_test_no_output "compile -raw -- extern \"C\" void abort(); int func(){return 21;} void _gdb_expr(){int (*funcp)()=func; if (funcp()!=21) abort();}" \
268 "pointer to jit function"
271 # Test the case where the registers structure would not normally have
275 gdb_breakpoint [gdb_get_line_number "no_args_or_locals breakpoint"]
276 gdb_continue_to_breakpoint "no_args_or_locals"
278 gdb_test_no_output "compile code globalvar = 77;" "set variable to 77"
279 gdb_test "p globalvar" " = 77" "expect 77"
282 # Test reference to minimal_symbol, not (full) symbol.
284 setup_kfail compile/23585 *-*-*
285 gdb_test_no_output "compile code globalvar = func_nodebug (75);" \
288 setup_kfail compile/23585 *-*-*
289 gdb_test "p globalvar" " = -75" "expect -75"
291 setup_kfail compile/23585 *-*-*
293 "compile code int (*funcp) (int) = (int(*)(int))func_nodebug; globalvar = funcp (76);" \
294 "call func_nodebug indirectly"
295 setup_kfail compile/23585 *-*-*
296 gdb_test "p globalvar" " = -76" "expect -76"
299 # Test compiled module memory protection.
301 gdb_test_no_output "set debug compile on"
302 gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \
303 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*"
304 gdb_test_no_output "set debug compile off"
308 # Some simple coverage tests.
311 gdb_test "show debug compile" "Compile debugging is .*"
312 gdb_test "show compile-args" \
313 "Compile command command-line arguments are .*"
314 gdb_test "compile code -z" "Unrecognized option at: -z"
316 gdb_test "set lang rust" \
317 "Warning: the current language does not match this frame."
318 gdb_test "compile code globalvar" "No compiler support for language rust\."
319 gdb_test_no_output "set lang auto"
321 gdb_test_no_output "compile code union union_type newdecl_u"
322 gdb_test_no_output "compile code struct struct_type newdecl_s"
323 gdb_test_no_output "compile code inttypedef newdecl_i"
325 gdb_test "compile file" \
326 "You must provide a filename for this command.*" \
327 "Test compile file without a filename"
328 gdb_test "compile file -r" \
329 "You must provide a filename for this command.*" \
330 "Test compile file and raw option without a filename"
331 gdb_test "compile file -z" \
332 "Unrecognized option at: -z" \
333 "test compile file with unknown option"
337 if { $srcfile3 != "" } {
338 gdb_test "p constvar" " = 3"
339 gdb_test "info addr constvar" {Symbol "constvar" is constant\.}
341 gdb_test_no_output "compile code globalvar = constvar;"
342 gdb_test "print globalvar" " = 3" "print constvar value"
344 untested "print constvar value"