1 # Copyright 2011-2020 Free Software Foundation, Inc.
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.
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.
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/>.
16 if {[skip_shlib_tests]} {
17 untested "skipping shared library tests"
21 if {[get_compiler_info]} {
22 untested "could not get compiler info"
26 load_lib jit-elf-helpers.exp
28 # The main code that loads and registers JIT objects.
29 set main_basename "jit-elf-main"
30 set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
31 set main_binfile [standard_output_file ${main_basename}]
33 # The shared library that gets loaded as JIT objects.
34 set jit_solib_basename jit-elf-solib
35 set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
37 # Detach, restart GDB, and re-attach to the program.
38 proc clean_reattach {} {
39 global decimal gdb_prompt
40 global main_binfile main_srcfile
42 # Get PID of test program.
44 set test "get inferior process ID"
45 gdb_test_multiple "p mypid" $test {
46 -re ".* = ($decimal).*$gdb_prompt $" {
47 set testpid $expect_out(1,string)
52 gdb_test_no_output "set var wait_for_gdb = 1"
53 gdb_test "detach" "Detaching from .*"
55 clean_restart ${main_binfile}
58 gdb_test_multiple "attach $testpid" "$test" {
59 -re "Attaching to program.*.*main.*at .*$main_srcfile:.*$gdb_prompt $" {
64 gdb_test_no_output "set var wait_for_gdb = 0"
67 # Continue to LOCATION in the program. If REATTACH, detach and
68 # re-attach to the program from scratch.
69 proc continue_to_test_location {location reattach} {
72 gdb_breakpoint [gdb_get_line_number $location $main_srcfile]
73 gdb_continue_to_breakpoint $location
75 with_test_prefix "$location" {
81 proc one_jit_test {jit_solibs_target match_str reattach} {
82 set count [llength $jit_solibs_target]
84 with_test_prefix "one_jit_test-$count" {
86 global main_binfile main_srcfile
88 clean_restart ${main_binfile}
90 # This is just to help debugging when things fail
92 gdb_test "set debug jit 1"
95 if { ![runto_main] } {
96 fail "can't run to main"
100 # Poke desired values directly into inferior instead of using "set args"
101 # because "set args" does not work under gdbserver.
103 gdb_test_no_output "set var argc=$count" "forging argc"
104 gdb_test_no_output "set var argv=fake_argv" "forging argv"
105 for {set i 1} {$i < $count} {incr i} {
106 set jit_solib_target [lindex $jit_solibs_target [expr $i-1]]
107 gdb_test_no_output "set var argv\[$i\]=\"${jit_solib_target}\"" \
111 gdb_breakpoint [gdb_get_line_number "break here 0" $main_srcfile]
112 gdb_continue_to_breakpoint "break here 0"
115 continue_to_test_location "break here 1" $reattach
117 gdb_test "info function ^jit_function" "$match_str"
119 # This is just to help debugging when things fail
121 gdb_test "maintenance print objfiles"
122 gdb_test "maintenance info break"
125 continue_to_test_location "break here 2" $reattach
127 # All jit librares must have been unregistered
128 gdb_test "info function jit_function" \
129 "All functions matching regular expression \"jit_function\":"
133 # Compile two shared libraries to use as JIT objects.
134 set jit_solibs_target [compile_and_download_n_jit_so \
135 $jit_solib_basename $jit_solib_srcfile 2]
136 if { $jit_solibs_target == -1 } {
140 # Compile the main code (which loads the JIT objects).
141 if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] == 0 } {
142 one_jit_test [lindex $jit_solibs_target 0] "${hex} jit_function_0001" 0
143 one_jit_test $jit_solibs_target "${hex} jit_function_0001\[\r\n\]+${hex} jit_function_0002" 0
146 # Test attaching to an inferior with some JIT libraries already
147 # registered. We reuse the normal test, and detach/reattach at
148 # specific interesting points.
149 if {[can_spawn_for_attach]} {
150 if { [compile_jit_main ${main_srcfile} "${main_binfile}-attach" \
151 {additional_flags=-DATTACH=1}] == 0 } {
152 with_test_prefix attach {
153 one_jit_test $jit_solibs_target "${hex} jit_function_0001\[\r\n\]+${hex} jit_function_0002" 1
158 if { [compile_jit_main ${main_srcfile} "${main_binfile}-pie" \
159 {additional_flags=-fPIE ldflags=-pie}] == 0 } {
160 with_test_prefix PIE {
161 one_jit_test [lindex $jit_solibs_target 0] "${hex} jit_function_0001" 0