1# Copyright 2014-2020 Free Software Foundation, Inc. 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 16load_lib compile-support.exp 17 18standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c 19 20get_compiler_info 21set options {} 22if { [test_compiler_info gcc*] || [test_compiler_info clang*] } { 23 lappend options additional_flags=-g3 24 lappend options additional_flags=-std=gnu++11 25 lappend options c++ 26} 27 28if { ![istarget x86_64-*-* ] || ![is_lp64_target] } { 29 verbose "Skipping x86_64 LOC_CONST test." 30 set srcfile3 "" 31} 32 33set srcfilesoptions [list ${srcfile} ${options}] 34if { $srcfile3 != "" } { 35 lappend srcfilesoptions $srcfile3 {} 36} 37set srcfile4options "nodebug c++" 38lappend srcfilesoptions $srcfile4 $srcfile4options 39if { [eval build_executable_from_specs ${testfile}.exp $testfile {$options} ${srcfilesoptions}] } { 40 return -1 41} 42 43clean_restart ${testfile} 44 45# 46# FIXME: Right now, for C++ we just duplicate the C tests, but force 47# the language to C++ 48# 49gdb_test_no_output "set language c++" \ 50 "Set language to C++" 51 52if ![runto_main] { 53 return -1 54} 55 56if {[skip_compile_feature_tests]} { 57 untested "compile command not supported (could not find libcc1 shared library?)" 58 return -1 59} 60 61# 62# Test delimiter for code, and arguments. 63# 64 65 66gdb_test_no_output "compile code globalvar = SOME_MACRO;" \ 67 "set variable from macro" 68gdb_test "p globalvar" " = 23" "expect 23" 69 70gdb_test_no_output "compile code globalvar = ARG_MACRO(0, 0);" \ 71 "set variable from function-like macro" 72gdb_test "p globalvar" " = -1" "expect -1" 73 74gdb_test_no_output "compile code globalvar = 42;" "set variable" 75gdb_test "p globalvar" " = 42" "expect 42" 76 77gdb_test_no_output "compile code globalvar *= 2;" "modify variable" 78gdb_test "p globalvar" " = 84" "expect 84" 79 80gdb_test_no_output "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \ 81 "use external source file" 82gdb_test "p globalvar" " = 7" "expect 7" 83 84gdb_test_no_output "compile code func_static (2);" "call static function" 85gdb_test "p globalvar" " = 9" "expect 9" 86gdb_test_no_output "compile code func_global (1);" "call global function" 87gdb_test "p globalvar" " = 8" "expect 8" 88 89gdb_test_no_output \ 90 "compile code globalvar = (sizeof (ulonger) == sizeof (long))" \ 91 "compute size of ulonger" 92gdb_test "p globalvar" " = 1" "check size of ulonger" 93gdb_test_no_output \ 94 "compile code globalvar = (sizeof (longer) == sizeof (long))" \ 95 "compute size of longer" 96gdb_test "p globalvar" " = 1" "check size of longer" 97gdb_test_no_output "compile code globalvar = MINUS_1" 98gdb_test "p globalvar" " = -1" "check MINUS_1" 99 100gdb_test_no_output "compile code globalvar = static_local" 101gdb_test "p globalvar" " = 77000" "check static_local" 102 103gdb_test_no_output \ 104 "compile code static int staticvar = 5; intptr = &staticvar" \ 105 "do not keep jit in memory" 106gdb_test "p *intptr" "Cannot access memory at address 0x\[0-9a-f\]+" \ 107 "expect 5" 108 109gdb_test "compile code func_doesnotexist ();" "error: \'func_doesnotexist\' was not declared in this scope.*" 110 111gdb_test "compile code *(volatile int *) 0 = 0;" \ 112 "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.*" \ 113 "compile code segfault first" 114gdb_test "bt" \ 115 "\r\n#0 \[^\r\n\]* in _gdb_expr \[^\r\n\]*\r\n#1 <function called from gdb>\r\n.*" 116 117set test "p/x \$pc" 118set infcall_pc 0 119gdb_test_multiple $test $test { 120 -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" { 121 set infcall_pc $expect_out(1,string) 122 pass $test 123 } 124} 125 126gdb_test "info sym $infcall_pc" "\r\n_gdb_expr.*" "info sym found" 127gdb_test "return" "\r\n#0 main .*" "return" \ 128 "Make _gdb_expr\\(__gdb_regs\\*\\) return now\\? \\(y or n\\) " "y" 129gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found" 130 131gdb_test_no_output "set unwindonsignal on" 132gdb_test "compile code *(volatile int *) 0 = 0;" \ 133 "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.*" \ 134 "compile code segfault second" 135 136gdb_breakpoint [gdb_get_line_number "break-here"] 137gdb_continue_to_breakpoint "break-here" ".* break-here .*" 138 139# C++ Specific tests. 140## Public methods and members 141 142gdb_test "print foovar.public_var" "42" \ 143 "Test compile code foovar.public_var = 42 setting." 144gdb_test_no_output "compile code foovar.public_var = 43;" \ 145 "set foobar.public_var to 43" 146gdb_test "print foovar.public_var" "43" \ 147 "Test compile code foovar.public_var = 43 setting." 148gdb_test "print foovar.public_method ()" "43" \ 149 "Test compile code foovar.public_method = 43 setting." 150 151## Private methods and members 152gdb_test_no_output "compile code foovar.set_private_var (84);" \ 153 "Call class function to set private_var" 154gdb_test "print foovar.private_var" "84" \ 155 "Test compile code foovar.set_private_var = 84 setting." 156gdb_test_no_output "compile code foovar.private_var = 85" \ 157 "Directly set a private member in GDB compile5" 158gdb_test "print foovar.private_var" "85" \ 159 "Test compile code foovar.set_private_var = 85 setting." 160 161## Simple inheritance 162CompileExpression::new "var" 163CompileExpression::test "class Baz: public Foo {public: int z = 12;}; Baz bazvar; bazvar.z = 24; var = bazvar.z" 24 -explicit 164## Multiple inheritance 165CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return 42;}}; MI MIVar; var = MIVar.pure_virt();" 42 -explicit 166CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return Base::return_value() + 42;}}; MI MIVar; var = MIVar.pure_virt();" 43 -explicit 167CompileExpression::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 168 169gdb_test "p localvar" " = 50" "expect localvar 50" 170 171gdb_test_no_output "compile code localvar = 12;" "set localvar" 172gdb_test "p localvar" " = 12" "expect 12" 173 174gdb_test_no_output "compile code localvar *= 2;" "modify localvar" 175gdb_test "p localvar" " = 24" "expect 24" 176 177gdb_test_no_output "compile code localvar = shadowed" \ 178 "test shadowing" 179gdb_test "p localvar" " = 52" "expect 52" 180 181gdb_test_no_output "compile code localvar = externed" 182gdb_test "p localvar" " = 7" "test extern in inner scope" 183 184gdb_test_no_output "compile code vla\[2\] = 7" 185gdb_test "p vla\[2\]" " = 7" 186gdb_test_no_output \ 187 "compile code localvar = (sizeof (vla) == bound * sizeof (vla\[0\]))" 188gdb_test "p localvar" " = 1" 189 190# 191# Test setting fields and also many different types. 192# 193 194gdb_test_no_output "compile code struct_object.selffield = (struct_type*)&struct_object" 195gdb_test "print struct_object.selffield == &struct_object" " = true" 196 197gdb_test_no_output "compile code struct_object.charfield = 1" 198gdb_test "print struct_object.charfield" " = 1 '\\\\001'" 199gdb_test_no_output "compile code struct_object.ucharfield = 1" 200gdb_test "print struct_object.ucharfield" " = 1 '\\\\001'" 201 202foreach {field value} { 203 shortfield -5 204 ushortfield 5 205 intfield -7 206 uintfield 7 207 bitfield 2 208 longfield -9 209 ulongfield 9 210 enumfield ONE 211 floatfield 1 212 doublefield 2 213} { 214 gdb_test_no_output "compile code struct_object.$field = $value" 215 gdb_test "print struct_object.$field" " = $value" 216} 217 218gdb_test_no_output "compile code struct_object.arrayfield\[2\] = 7" 219gdb_test "print struct_object.arrayfield" \ 220 " = \\{0, 0, 7, 0, 0\\}" 221 222gdb_test_no_output "compile code struct_object.complexfield = 7 + 5i" 223gdb_test "print struct_object.complexfield" " = 7 \\+ 5i" 224 225gdb_test_no_output "compile code struct_object.boolfield = 1" 226gdb_test "print struct_object.boolfield" " = true" 227 228gdb_test_no_output "compile code struct_object.vectorfield\[2\] = 7" 229gdb_test "print struct_object.vectorfield" \ 230 " = \\{0, 0, 7, 0\\}" 231 232gdb_test_no_output "compile code union_object.typedeffield = 7" 233gdb_test "print union_object.typedeffield" " = 7" 234gdb_test "print union_object.intfield" " = 7" 235 236 237# LOC_UNRESOLVED tests. 238 239gdb_test "print unresolved" " = 20" 240gdb_test "compile code globalvar = unresolved;" 241gdb_test "print globalvar" " = 20" "print unresolved value" 242 243# Test shadowing with global and static variables. 244 245gdb_test_no_output "compile code globalshadow += 1;" 246gdb_test "print globalshadow" " = 101" 247gdb_test_no_output "compile code extern int globalshadow; globalshadow += 5;" 248gdb_test "print 'compile-cplus.c'::globalshadow" " = 15" 249gdb_test "print globalshadow" " = 101" "print globalshadow second time" 250gdb_test_no_output "compile code staticshadow += 2;" 251gdb_test "print staticshadow" " = 202" 252# "extern int staticshadow;" cannot access static variable. 253 254# Raw code cannot refer to locals. 255# As it references global variable we need the #pragma. 256# For #pragma we need multiline input. 257gdb_test_multiple "compile code -r" "compile code -r multiline 1" { -re "\r\n>$" {} } 258gdb_test_multiple "void _gdb_expr(void) {" "compile code -r multiline 2" { -re "\r\n>$" {} } 259gdb_test_multiple "#pragma GCC push_user_expression" "compile code -r multiline 3" { -re "\r\n>$" {} } 260gdb_test_multiple " globalshadow = 77000;" "compile code -r multiline 4" { -re "\r\n>$" {} } 261gdb_test_multiple "#pragma GCC pop_user_expression" "compile code -r multiline 5" { -re "\r\n>$" {} } 262gdb_test_multiple "}" "compile code -r multiline 6" { -re "\r\n>$" {} } 263gdb_test_no_output "end" "compile code -r multiline 7" 264gdb_test "print 'compile-cplus.c'::globalshadow" " = 77000" \ 265 "check globalshadow with -r" 266 267# Test GOT vs. resolving jit function pointers. 268 269gdb_test_no_output "compile -raw -- extern \"C\" void abort(); int func(){return 21;} void _gdb_expr(){int (*funcp)()=func; if (funcp()!=21) abort();}" \ 270 "pointer to jit function" 271 272# 273# Test the case where the registers structure would not normally have 274# any fields. 275# 276 277gdb_breakpoint [gdb_get_line_number "no_args_or_locals breakpoint"] 278gdb_continue_to_breakpoint "no_args_or_locals" 279 280gdb_test_no_output "compile code globalvar = 77;" "set variable to 77" 281gdb_test "p globalvar" " = 77" "expect 77" 282 283 284# Test reference to minimal_symbol, not (full) symbol. 285 286setup_kfail compile/23585 *-*-* 287gdb_test_no_output "compile code globalvar = func_nodebug (75);" \ 288 "call func_nodebug" 289 290setup_kfail compile/23585 *-*-* 291gdb_test "p globalvar" " = -75" "expect -75" 292 293setup_kfail compile/23585 *-*-* 294gdb_test_no_output \ 295 "compile code int (*funcp) (int) = (int(*)(int))func_nodebug; globalvar = funcp (76);" \ 296 "call func_nodebug indirectly" 297setup_kfail compile/23585 *-*-* 298gdb_test "p globalvar" " = -76" "expect -76" 299 300 301# Test compiled module memory protection. 302 303gdb_test_no_output "set debug compile on" 304gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \ 305 "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.*" 306gdb_test_no_output "set debug compile off" 307 308 309# 310# Some simple coverage tests. 311# 312 313gdb_test "show debug compile" "Compile debugging is .*" 314gdb_test "show compile-args" \ 315 "Compile command command-line arguments are .*" 316gdb_test "compile code -z" "Unknown argument.*" 317 318gdb_test "set lang rust" \ 319 "Warning: the current language does not match this frame." 320gdb_test "compile code globalvar" "No compiler support for language rust\." 321gdb_test_no_output "set lang auto" 322 323gdb_test_no_output "compile code union union_type newdecl_u" 324gdb_test_no_output "compile code struct struct_type newdecl_s" 325gdb_test_no_output "compile code inttypedef newdecl_i" 326 327gdb_test "compile file" \ 328 "You must provide a filename for this command.*" \ 329 "Test compile file without a filename" 330gdb_test "compile file -r" \ 331 "You must provide a filename with the raw option set.*" \ 332 "Test compile file and raw option without a filename" 333gdb_test "compile file -z" \ 334 "Unknown argument.*" \ 335 "Test compile file with unknown argument" 336 337 338# LOC_CONST tests. 339 340if { $srcfile3 != "" } { 341 gdb_test "p constvar" " = 3" 342 gdb_test "info addr constvar" {Symbol "constvar" is constant\.} 343 344 gdb_test_no_output "compile code globalvar = constvar;" 345 gdb_test "print globalvar" " = 3" "print constvar value" 346} else { 347 untested "print constvar value" 348} 349