Home | History | Annotate | Line # | Download | only in gdb.base
jit-elf.exp revision 1.1.1.1.2.1
      1  1.1.1.1.2.1  perseant # Copyright 2011-2023 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 load_lib jit-elf-helpers.exp
     22          1.1  christos 
     23          1.1  christos # Increase this to see more detail.
     24          1.1  christos set test_verbose 0
     25          1.1  christos 
     26          1.1  christos # The main code that loads and registers JIT objects.
     27          1.1  christos set main_basename "jit-elf-main"
     28          1.1  christos set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
     29          1.1  christos set main_binfile [standard_output_file ${main_basename}]
     30          1.1  christos 
     31          1.1  christos # The shared library that gets loaded as JIT objects.
     32          1.1  christos set jit_solib_basename jit-elf-solib
     33          1.1  christos set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
     34          1.1  christos 
     35          1.1  christos # Detach, restart GDB, and re-attach to the program.
     36  1.1.1.1.2.1  perseant # Return 0 if attach failed, otherwise return 1.
     37          1.1  christos proc clean_reattach {} {
     38          1.1  christos     global decimal gdb_prompt
     39          1.1  christos     global main_binfile main_srcfile
     40          1.1  christos 
     41          1.1  christos     # Get PID of test program.
     42          1.1  christos     set testpid -1
     43          1.1  christos     set test "get inferior process ID"
     44          1.1  christos     gdb_test_multiple "p mypid" $test {
     45          1.1  christos 	-re ".* = ($decimal).*$gdb_prompt $" {
     46          1.1  christos 	    set testpid $expect_out(1,string)
     47          1.1  christos 	    pass $test
     48          1.1  christos 	}
     49          1.1  christos     }
     50          1.1  christos 
     51          1.1  christos     gdb_test_no_output "set var wait_for_gdb = 1"
     52          1.1  christos     gdb_test "detach" "Detaching from .*"
     53          1.1  christos 
     54          1.1  christos     clean_restart ${main_binfile}
     55          1.1  christos 
     56  1.1.1.1.2.1  perseant     if { ![gdb_attach $testpid \
     57  1.1.1.1.2.1  perseant 	      -pattern "main.*at .*$::main_basename.c:.*"] } {
     58  1.1.1.1.2.1  perseant 	return 0
     59          1.1  christos     }
     60          1.1  christos 
     61          1.1  christos     gdb_test_no_output "set var wait_for_gdb = 0"
     62  1.1.1.1.2.1  perseant     return 1
     63          1.1  christos }
     64          1.1  christos 
     65          1.1  christos # Continue to LOCATION in the program.  If REATTACH, detach and
     66          1.1  christos # re-attach to the program from scratch.
     67  1.1.1.1.2.1  perseant # Return 0 if clean_reattach failed, otherwise return 1.
     68          1.1  christos proc continue_to_test_location {location reattach} {
     69          1.1  christos     global main_srcfile
     70          1.1  christos 
     71          1.1  christos     gdb_breakpoint [gdb_get_line_number $location $main_srcfile]
     72          1.1  christos     gdb_continue_to_breakpoint $location
     73          1.1  christos     if {$reattach} {
     74          1.1  christos 	with_test_prefix "$location" {
     75  1.1.1.1.2.1  perseant 	    if { ![clean_reattach] } {
     76  1.1.1.1.2.1  perseant 		return 0
     77  1.1.1.1.2.1  perseant 	    }
     78          1.1  christos 	}
     79          1.1  christos     }
     80  1.1.1.1.2.1  perseant     return 1
     81          1.1  christos }
     82          1.1  christos 
     83          1.1  christos proc one_jit_test {jit_solibs_target match_str reattach} {
     84          1.1  christos     set count [llength $jit_solibs_target]
     85          1.1  christos 
     86          1.1  christos     with_test_prefix "one_jit_test-$count" {
     87          1.1  christos 	global test_verbose
     88          1.1  christos 	global main_binfile main_srcfile
     89          1.1  christos 
     90          1.1  christos 	clean_restart ${main_binfile}
     91          1.1  christos 
     92          1.1  christos 	# This is just to help debugging when things fail
     93          1.1  christos 	if {$test_verbose > 0} {
     94          1.1  christos 	    gdb_test "set debug jit 1"
     95          1.1  christos 	}
     96          1.1  christos 
     97          1.1  christos 	if { ![runto_main] } {
     98          1.1  christos 	    return
     99          1.1  christos 	}
    100          1.1  christos 
    101          1.1  christos 	# Poke desired values directly into inferior instead of using "set args"
    102          1.1  christos 	# because "set args" does not work under gdbserver.
    103          1.1  christos 	incr count
    104          1.1  christos 	gdb_test_no_output "set var argc=$count" "forging argc"
    105          1.1  christos 	gdb_test_no_output "set var argv=fake_argv" "forging argv"
    106          1.1  christos 	for {set i 1} {$i < $count} {incr i} {
    107          1.1  christos 	    set jit_solib_target [lindex $jit_solibs_target [expr $i-1]]
    108          1.1  christos 	    gdb_test_no_output "set var argv\[$i\]=\"${jit_solib_target}\"" \
    109          1.1  christos 		"forging argv\[$i\]"
    110          1.1  christos 	}
    111          1.1  christos 
    112          1.1  christos 	gdb_breakpoint [gdb_get_line_number "break here 0" $main_srcfile]
    113          1.1  christos 	gdb_continue_to_breakpoint "break here 0"
    114          1.1  christos 
    115          1.1  christos 
    116  1.1.1.1.2.1  perseant 	if { ![continue_to_test_location "break here 1" $reattach] } {
    117  1.1.1.1.2.1  perseant 	    return
    118  1.1.1.1.2.1  perseant 	}
    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.1.1.2.1  perseant 	if { ![continue_to_test_location "break here 2" $reattach] } {
    129  1.1.1.1.2.1  perseant 	    return
    130  1.1.1.1.2.1  perseant 	}
    131          1.1  christos 
    132          1.1  christos 	# All jit librares must have been unregistered
    133          1.1  christos 	gdb_test "info function jit_function" \
    134          1.1  christos 	    "All functions matching regular expression \"jit_function\":"
    135          1.1  christos     }
    136          1.1  christos }
    137          1.1  christos 
    138          1.1  christos # Compile two shared libraries to use as JIT objects.
    139          1.1  christos set jit_solibs_target [compile_and_download_n_jit_so \
    140          1.1  christos 		      $jit_solib_basename $jit_solib_srcfile 2]
    141          1.1  christos if { $jit_solibs_target == -1 } {
    142          1.1  christos     return
    143          1.1  christos }
    144          1.1  christos 
    145          1.1  christos # Compile the main code (which loads the JIT objects).
    146          1.1  christos if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] == 0 } {
    147          1.1  christos     one_jit_test [lindex $jit_solibs_target 0] "${hex}  jit_function_0001" 0
    148          1.1  christos     one_jit_test $jit_solibs_target "${hex}  jit_function_0001\[\r\n\]+${hex}  jit_function_0002" 0
    149          1.1  christos }
    150          1.1  christos 
    151          1.1  christos # Test attaching to an inferior with some JIT libraries already
    152          1.1  christos # registered.  We reuse the normal test, and detach/reattach at
    153          1.1  christos # specific interesting points.
    154          1.1  christos if {[can_spawn_for_attach]} {
    155          1.1  christos     if { [compile_jit_main ${main_srcfile} "${main_binfile}-attach" \
    156          1.1  christos 	    {additional_flags=-DATTACH=1}] == 0 } {
    157          1.1  christos 	with_test_prefix attach {
    158          1.1  christos 	    one_jit_test $jit_solibs_target "${hex}  jit_function_0001\[\r\n\]+${hex}  jit_function_0002" 1
    159          1.1  christos 	}
    160          1.1  christos     }
    161          1.1  christos }
    162          1.1  christos 
    163  1.1.1.1.2.1  perseant if { [compile_jit_main ${main_srcfile} "${main_binfile}-pie" pie] == 0 } {
    164          1.1  christos     with_test_prefix PIE {
    165          1.1  christos 	one_jit_test [lindex $jit_solibs_target 0] "${hex}  jit_function_0001" 0
    166          1.1  christos     }
    167          1.1  christos }
    168