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