1 # Copyright 2014-2024 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
20 require is_c_compiler_gcc
23 if { [test_compiler_info gcc*] || [test_compiler_info clang*] } {
24 lappend options additional_flags=-g3
25 lappend options additional_flags=-std=gnu++11
29 if { ![is_x86_64_m64_target] } {
30 verbose "Skipping x86_64 LOC_CONST test."
34 set srcfilesoptions [list ${srcfile} ${options}]
35 if { $srcfile3 != "" } {
36 lappend srcfilesoptions $srcfile3 {}
38 set srcfile4options "nodebug c++"
39 lappend srcfilesoptions $srcfile4 $srcfile4options
40 if { [eval build_executable_from_specs ${testfile}.exp $testfile {$options} ${srcfilesoptions}] } {
44 clean_restart ${testfile}
47 # FIXME: Right now, for C++ we just duplicate the C tests, but force
50 gdb_test_no_output "set language c++" \
57 gdb_breakpoint [gdb_get_line_number "break-here"]
58 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
60 if {[skip_compile_feature_untested "var = 0"]} {
65 # Test delimiter for code, and arguments.
69 gdb_test_no_output "compile code globalvar = SOME_MACRO;" \
70 "set variable from macro"
71 gdb_test "p globalvar" " = 23" "expect 23"
73 gdb_test_no_output "compile code globalvar = ARG_MACRO(0, 0);" \
74 "set variable from function-like macro"
75 gdb_test "p globalvar" " = -1" "expect -1"
77 gdb_test_no_output "compile code globalvar = 42;" "set variable"
78 gdb_test "p globalvar" " = 42" "expect 42"
80 gdb_test_no_output "compile code globalvar *= 2;" "modify variable"
81 gdb_test "p globalvar" " = 84" "expect 84"
83 gdb_test_no_output "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \
84 "use external source file"
85 gdb_test "p globalvar" " = 7" "expect 7"
87 gdb_test_no_output "compile code func_static (2);" "call static function"
88 gdb_test "p globalvar" " = 9" "expect 9"
89 gdb_test_no_output "compile code func_global (1);" "call global function"
90 gdb_test "p globalvar" " = 8" "expect 8"
93 "compile code globalvar = (sizeof (ulonger) == sizeof (long))" \
94 "compute size of ulonger"
95 gdb_test "p globalvar" " = 1" "check size of ulonger"
97 "compile code globalvar = (sizeof (longer) == sizeof (long))" \
98 "compute size of longer"
99 gdb_test "p globalvar" " = 1" "check size of longer"
100 gdb_test_no_output "compile code globalvar = MINUS_1"
101 gdb_test "p globalvar" " = -1" "check MINUS_1"
103 gdb_test_no_output "compile code globalvar = static_local"
104 gdb_test "p globalvar" " = 77000" "check static_local"
107 "compile code static int staticvar = 5; intptr = &staticvar" \
108 "do not keep jit in memory"
109 gdb_test "p *intptr" "Cannot access memory at address 0x\[0-9a-f\]+" \
112 gdb_test "compile code func_doesnotexist ();" "error: \'func_doesnotexist\' was not declared in this scope.*"
114 gdb_test "compile code *(volatile int *) 0 = 0;" \
115 "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.*" \
116 "compile code segfault first"
118 "\r\n#0 \[^\r\n\]*_gdb_expr \[^\r\n\]*\r\n#1 <function called from gdb>.*"
122 gdb_test_multiple $test $test {
123 -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
124 set infcall_pc $expect_out(1,string)
129 gdb_test "info sym $infcall_pc" "\r\n_gdb_expr.*" "info sym found"
130 gdb_test "return" "\r\n#0 main .*" "return" \
131 "Make _gdb_expr\\(__gdb_regs\\*\\) return now\\? \\(y or n\\) " "y"
132 gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found"
134 gdb_test_no_output "set unwind-on-signal on"
135 gdb_test "compile code *(volatile int *) 0 = 0;" \
137 "The program being debugged received signal SIGSEGV, Segmentation fault" \
138 "while in a function called from GDB\\. GDB has restored the context" \
139 "to what it was before the call\\. To change this behavior use" \
140 "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \
141 "the function \\(_gdb_expr\\(__gdb_regs\\*\\)\\) will be abandoned\\."] \
142 "compile code segfault second"
144 # C++ Specific tests.
145 ## Public methods and members
147 gdb_test "print foovar.public_var" "42" \
148 "Test compile code foovar.public_var = 42 setting."
149 gdb_test_no_output "compile code foovar.public_var = 43;" \
150 "set foobar.public_var to 43"
151 gdb_test "print foovar.public_var" "43" \
152 "Test compile code foovar.public_var = 43 setting."
153 gdb_test "print foovar.public_method ()" "43" \
154 "Test compile code foovar.public_method = 43 setting."
156 ## Private methods and members
157 gdb_test_no_output "compile code foovar.set_private_var (84);" \
158 "Call class function to set private_var"
159 gdb_test "print foovar.private_var" "84" \
160 "Test compile code foovar.set_private_var = 84 setting."
161 gdb_test_no_output "compile code foovar.private_var = 85" \
162 "Directly set a private member in GDB compile5"
163 gdb_test "print foovar.private_var" "85" \
164 "Test compile code foovar.set_private_var = 85 setting."
166 ## Simple inheritance
167 CompileExpression::new "var"
168 CompileExpression::test "class Baz: public Foo {public: int z = 12;}; Baz bazvar; bazvar.z = 24; var = bazvar.z" 24 -explicit
169 ## Multiple inheritance
170 CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return 42;}}; MI MIVar; var = MIVar.pure_virt();" 42 -explicit
171 CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return Base::return_value() + 42;}}; MI MIVar; var = MIVar.pure_virt();" 43 -explicit
172 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
174 gdb_test "p localvar" " = 50" "expect localvar 50"
176 gdb_test_no_output "compile code localvar = 12;" "set localvar"
177 gdb_test "p localvar" " = 12" "expect 12"
179 gdb_test_no_output "compile code localvar *= 2;" "modify localvar"
180 gdb_test "p localvar" " = 24" "expect 24"
182 gdb_test_no_output "compile code localvar = shadowed" \
184 gdb_test "p localvar" " = 52" "expect 52"
186 gdb_test_no_output "compile code localvar = externed"
187 gdb_test "p localvar" " = 7" "test extern in inner scope"
189 gdb_test_no_output "compile code vla\[2\] = 7"
190 gdb_test "p vla\[2\]" " = 7"
192 "compile code localvar = (sizeof (vla) == bound * sizeof (vla\[0\]))"
193 gdb_test "p localvar" " = 1"
196 # Test setting fields and also many different types.
199 gdb_test_no_output "compile code struct_object.selffield = (struct_type*)&struct_object"
200 gdb_test "print struct_object.selffield == &struct_object" " = true"
202 gdb_test_no_output "compile code struct_object.charfield = 1"
203 gdb_test "print struct_object.charfield" " = 1 '\\\\001'"
204 gdb_test_no_output "compile code struct_object.ucharfield = 1"
205 gdb_test "print struct_object.ucharfield" " = 1 '\\\\001'"
207 foreach {field value} {
219 gdb_test_no_output "compile code struct_object.$field = $value"
220 gdb_test "print struct_object.$field" " = $value"
223 gdb_test_no_output "compile code struct_object.arrayfield\[2\] = 7"
224 gdb_test "print struct_object.arrayfield" \
225 " = \\{0, 0, 7, 0, 0\\}"
227 gdb_test_no_output "compile code struct_object.complexfield = 7 + 5i"
228 gdb_test "print struct_object.complexfield" " = 7 \\+ 5i"
230 gdb_test_no_output "compile code struct_object.boolfield = 1"
231 gdb_test "print struct_object.boolfield" " = true"
233 gdb_test_no_output "compile code struct_object.vectorfield\[2\] = 7"
234 gdb_test "print struct_object.vectorfield" \
235 " = \\{0, 0, 7, 0\\}"
237 gdb_test_no_output "compile code union_object.typedeffield = 7"
238 gdb_test "print union_object.typedeffield" " = 7"
239 gdb_test "print union_object.intfield" " = 7"
242 # LOC_UNRESOLVED tests.
244 gdb_test "print unresolved" " = 20"
245 gdb_test "compile code globalvar = unresolved;"
246 gdb_test "print globalvar" " = 20" "print unresolved value"
248 # Test shadowing with global and static variables.
250 gdb_test_no_output "compile code globalshadow += 1;"
251 gdb_test "print globalshadow" " = 101"
252 setup_kfail {no bug filed} *-*-*
253 gdb_test_no_output "compile code extern int globalshadow; globalshadow += 5;"
254 setup_kfail {fails due to previous test} *-*-*
255 gdb_test "print 'compile-cplus.c'::globalshadow" " = 15"
256 gdb_test "print globalshadow" " = 101" "print globalshadow second time"
257 gdb_test_no_output "compile code staticshadow += 2;"
258 gdb_test "print staticshadow" " = 202"
259 # "extern int staticshadow;" cannot access static variable.
261 # Raw code cannot refer to locals.
262 # As it references global variable we need the #pragma.
263 # For #pragma we need multiline input.
264 gdb_test_multiple "compile code -r" "compile code -r multiline 1" { -re "\r\n>$" {} }
265 gdb_test_multiple "void _gdb_expr(void) {" "compile code -r multiline 2" { -re "\r\n>$" {} }
266 gdb_test_multiple "#pragma GCC push_user_expression" "compile code -r multiline 3" { -re "\r\n>$" {} }
267 gdb_test_multiple " globalshadow = 77000;" "compile code -r multiline 4" { -re "\r\n>$" {} }
268 gdb_test_multiple "#pragma GCC pop_user_expression" "compile code -r multiline 5" { -re "\r\n>$" {} }
269 gdb_test_multiple "}" "compile code -r multiline 6" { -re "\r\n>$" {} }
270 gdb_test_no_output "end" "compile code -r multiline 7"
271 gdb_test "print 'compile-cplus.c'::globalshadow" " = 77000" \
272 "check globalshadow with -r"
274 # Test GOT vs. resolving jit function pointers.
276 gdb_test_no_output "compile -raw -- extern \"C\" void abort(); int func(){return 21;} void _gdb_expr(){int (*funcp)()=func; if (funcp()!=21) abort();}" \
277 "pointer to jit function"
280 # Test the case where the registers structure would not normally have
284 gdb_breakpoint [gdb_get_line_number "no_args_or_locals breakpoint"]
285 gdb_continue_to_breakpoint "no_args_or_locals"
287 gdb_test_no_output "compile code globalvar = 77;" "set variable to 77"
288 gdb_test "p globalvar" " = 77" "expect 77"
291 # Test reference to minimal_symbol, not (full) symbol.
293 setup_kfail compile/23585 *-*-*
294 gdb_test_no_output "compile code globalvar = func_nodebug (75);" \
297 setup_kfail compile/23585 *-*-*
298 gdb_test "p globalvar" " = -75" "expect -75"
300 setup_kfail compile/23585 *-*-*
302 "compile code int (*funcp) (int) = (int(*)(int))func_nodebug; globalvar = funcp (76);" \
303 "call func_nodebug indirectly"
304 setup_kfail compile/23585 *-*-*
305 gdb_test "p globalvar" " = -76" "expect -76"
308 # Test compiled module memory protection.
310 gdb_test_no_output "set debug compile on"
311 gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \
313 "The program being debugged received signal SIGSEGV, Segmentation fault" \
314 "while in a function called from GDB\\. GDB has restored the context" \
315 "to what it was before the call\\. To change this behavior use" \
316 "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \
317 "the function \\(_gdb_expr\\(__gdb_regs\\*\\)\\) will be abandoned\\."]
318 gdb_test_no_output "set debug compile off"
322 # Some simple coverage tests.
325 gdb_test "show debug compile" "Compile debugging is .*"
326 gdb_test "show compile-args" \
327 "Compile command command-line arguments are .*"
328 gdb_test "compile code -z" "Unrecognized option at: -z"
330 gdb_test "set lang rust" \
331 "Warning: the current language does not match this frame."
332 gdb_test "compile code globalvar" "No compiler support for language rust\."
333 gdb_test_no_output "set lang auto"
335 gdb_test_no_output "compile code union union_type newdecl_u"
336 gdb_test_no_output "compile code struct struct_type newdecl_s"
337 gdb_test_no_output "compile code inttypedef newdecl_i"
339 gdb_test "compile file" \
340 "You must provide a filename for this command.*" \
341 "Test compile file without a filename"
342 gdb_test "compile file -r" \
343 "You must provide a filename for this command.*" \
344 "Test compile file and raw option without a filename"
345 gdb_test "compile file -z" \
346 "Unrecognized option at: -z" \
347 "test compile file with unknown option"
351 if { $srcfile3 != "" } {
352 gdb_test "p constvar" " = 3"
353 gdb_test "info addr constvar" {Symbol "constvar" is constant\.}
355 gdb_test_no_output "compile code globalvar = constvar;"
356 gdb_test "print globalvar" " = 3" "print constvar value"
358 untested "print constvar value"