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