Home | History | Annotate | Line # | Download | only in gdb.base
jit-elf.exp revision 1.1
      1  1.1  christos # Copyright 2011-2020 Free Software Foundation, Inc.
      2  1.1  christos 
      3  1.1  christos # This program is free software; you can redistribute it and/or modify
      4  1.1  christos # it under the terms of the GNU General Public License as published by
      5  1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6  1.1  christos # (at your option) any later version.
      7  1.1  christos #
      8  1.1  christos # This program is distributed in the hope that it will be useful,
      9  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1  christos # GNU General Public License for more details.
     12  1.1  christos #
     13  1.1  christos # You should have received a copy of the GNU General Public License
     14  1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  1.1  christos 
     16  1.1  christos if {[skip_shlib_tests]} {
     17  1.1  christos     untested "skipping shared library tests"
     18  1.1  christos     return -1
     19  1.1  christos }
     20  1.1  christos 
     21  1.1  christos if {[get_compiler_info]} {
     22  1.1  christos     untested "could not get compiler info"
     23  1.1  christos     return 1
     24  1.1  christos }
     25  1.1  christos 
     26  1.1  christos load_lib jit-elf-helpers.exp
     27  1.1  christos 
     28  1.1  christos # Increase this to see more detail.
     29  1.1  christos set test_verbose 0
     30  1.1  christos 
     31  1.1  christos # The main code that loads and registers JIT objects.
     32  1.1  christos set main_basename "jit-elf-main"
     33  1.1  christos set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
     34  1.1  christos set main_binfile [standard_output_file ${main_basename}]
     35  1.1  christos 
     36  1.1  christos # The shared library that gets loaded as JIT objects.
     37  1.1  christos set jit_solib_basename jit-elf-solib
     38  1.1  christos set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
     39  1.1  christos 
     40  1.1  christos # Detach, restart GDB, and re-attach to the program.
     41  1.1  christos proc clean_reattach {} {
     42  1.1  christos     global decimal gdb_prompt
     43  1.1  christos     global main_binfile main_srcfile
     44  1.1  christos 
     45  1.1  christos     # Get PID of test program.
     46  1.1  christos     set testpid -1
     47  1.1  christos     set test "get inferior process ID"
     48  1.1  christos     gdb_test_multiple "p mypid" $test {
     49  1.1  christos 	-re ".* = ($decimal).*$gdb_prompt $" {
     50  1.1  christos 	    set testpid $expect_out(1,string)
     51  1.1  christos 	    pass $test
     52  1.1  christos 	}
     53  1.1  christos     }
     54  1.1  christos 
     55  1.1  christos     gdb_test_no_output "set var wait_for_gdb = 1"
     56  1.1  christos     gdb_test "detach" "Detaching from .*"
     57  1.1  christos 
     58  1.1  christos     clean_restart ${main_binfile}
     59  1.1  christos 
     60  1.1  christos     set test "attach"
     61  1.1  christos     gdb_test_multiple "attach $testpid" "$test" {
     62  1.1  christos 	-re "Attaching to program.*.*main.*at .*$main_srcfile:.*$gdb_prompt $" {
     63  1.1  christos 	    pass "$test"
     64  1.1  christos 	}
     65  1.1  christos     }
     66  1.1  christos 
     67  1.1  christos     gdb_test_no_output "set var wait_for_gdb = 0"
     68  1.1  christos }
     69  1.1  christos 
     70  1.1  christos # Continue to LOCATION in the program.  If REATTACH, detach and
     71  1.1  christos # re-attach to the program from scratch.
     72  1.1  christos proc continue_to_test_location {location reattach} {
     73  1.1  christos     global main_srcfile
     74  1.1  christos 
     75  1.1  christos     gdb_breakpoint [gdb_get_line_number $location $main_srcfile]
     76  1.1  christos     gdb_continue_to_breakpoint $location
     77  1.1  christos     if {$reattach} {
     78  1.1  christos 	with_test_prefix "$location" {
     79  1.1  christos 	    clean_reattach
     80  1.1  christos 	}
     81  1.1  christos     }
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos proc one_jit_test {jit_solibs_target match_str reattach} {
     85  1.1  christos     set count [llength $jit_solibs_target]
     86  1.1  christos 
     87  1.1  christos     with_test_prefix "one_jit_test-$count" {
     88  1.1  christos 	global test_verbose
     89  1.1  christos 	global main_binfile main_srcfile
     90  1.1  christos 
     91  1.1  christos 	clean_restart ${main_binfile}
     92  1.1  christos 
     93  1.1  christos 	# This is just to help debugging when things fail
     94  1.1  christos 	if {$test_verbose > 0} {
     95  1.1  christos 	    gdb_test "set debug jit 1"
     96  1.1  christos 	}
     97  1.1  christos 
     98  1.1  christos 	if { ![runto_main] } {
     99  1.1  christos 	    fail "can't run to main"
    100  1.1  christos 	    return
    101  1.1  christos 	}
    102  1.1  christos 
    103  1.1  christos 	# Poke desired values directly into inferior instead of using "set args"
    104  1.1  christos 	# because "set args" does not work under gdbserver.
    105  1.1  christos 	incr count
    106  1.1  christos 	gdb_test_no_output "set var argc=$count" "forging argc"
    107  1.1  christos 	gdb_test_no_output "set var argv=fake_argv" "forging argv"
    108  1.1  christos 	for {set i 1} {$i < $count} {incr i} {
    109  1.1  christos 	    set jit_solib_target [lindex $jit_solibs_target [expr $i-1]]
    110  1.1  christos 	    gdb_test_no_output "set var argv\[$i\]=\"${jit_solib_target}\"" \
    111  1.1  christos 		"forging argv\[$i\]"
    112  1.1  christos 	}
    113  1.1  christos 
    114  1.1  christos 	gdb_breakpoint [gdb_get_line_number "break here 0" $main_srcfile]
    115  1.1  christos 	gdb_continue_to_breakpoint "break here 0"
    116  1.1  christos 
    117  1.1  christos 
    118  1.1  christos 	continue_to_test_location "break here 1" $reattach
    119  1.1  christos 
    120  1.1  christos 	gdb_test "info function ^jit_function" "$match_str"
    121  1.1  christos 
    122  1.1  christos 	# This is just to help debugging when things fail
    123  1.1  christos 	if {$test_verbose > 0} {
    124  1.1  christos 	    gdb_test "maintenance print objfiles"
    125  1.1  christos 	    gdb_test "maintenance info break"
    126  1.1  christos 	}
    127  1.1  christos 
    128  1.1  christos 	continue_to_test_location "break here 2" $reattach
    129  1.1  christos 
    130  1.1  christos 	# All jit librares must have been unregistered
    131  1.1  christos 	gdb_test "info function jit_function" \
    132  1.1  christos 	    "All functions matching regular expression \"jit_function\":"
    133  1.1  christos     }
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos # Compile two shared libraries to use as JIT objects.
    137  1.1  christos set jit_solibs_target [compile_and_download_n_jit_so \
    138  1.1  christos 		      $jit_solib_basename $jit_solib_srcfile 2]
    139  1.1  christos if { $jit_solibs_target == -1 } {
    140  1.1  christos     return
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos # Compile the main code (which loads the JIT objects).
    144  1.1  christos if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] == 0 } {
    145  1.1  christos     one_jit_test [lindex $jit_solibs_target 0] "${hex}  jit_function_0001" 0
    146  1.1  christos     one_jit_test $jit_solibs_target "${hex}  jit_function_0001\[\r\n\]+${hex}  jit_function_0002" 0
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos # Test attaching to an inferior with some JIT libraries already
    150  1.1  christos # registered.  We reuse the normal test, and detach/reattach at
    151  1.1  christos # specific interesting points.
    152  1.1  christos if {[can_spawn_for_attach]} {
    153  1.1  christos     if { [compile_jit_main ${main_srcfile} "${main_binfile}-attach" \
    154  1.1  christos 	    {additional_flags=-DATTACH=1}] == 0 } {
    155  1.1  christos 	with_test_prefix attach {
    156  1.1  christos 	    one_jit_test $jit_solibs_target "${hex}  jit_function_0001\[\r\n\]+${hex}  jit_function_0002" 1
    157  1.1  christos 	}
    158  1.1  christos     }
    159  1.1  christos }
    160  1.1  christos 
    161  1.1  christos if { [compile_jit_main ${main_srcfile} "${main_binfile}-pie" \
    162  1.1  christos 	  {additional_flags=-fPIE ldflags=-pie}] == 0 } {
    163  1.1  christos     with_test_prefix PIE {
    164  1.1  christos 	one_jit_test [lindex $jit_solibs_target 0] "${hex}  jit_function_0001" 0
    165  1.1  christos     }
    166  1.1  christos }
    167