1 # Copyright 2011-2023 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 # The same tests as in jit.exp, but loading JITer itself from a shared
19 if {[skip_shlib_tests]} {
20 untested "skipping shared library tests"
24 load_lib jit-elf-helpers.exp
26 # Increase this to see more detail.
29 # The "real" main of this test, which loads jit-elf-main
30 # as a shared library.
31 set main_loader_basename jit-elf-dlmain
32 set main_loader_srcfile ${srcdir}/${subdir}/${main_loader_basename}.c
33 set main_loader_binfile [standard_output_file ${main_loader_basename}]
35 # The main code that loads and registers JIT objects.
36 set main_solib_basename jit-elf-main
37 set main_solib_srcfile ${srcdir}/${subdir}/${main_solib_basename}.c
38 set main_solib_binfile [standard_output_file ${main_solib_basename}.so]
40 # The shared library that gets loaded as JIT objects.
41 set jit_solib_basename jit-elf-solib
42 set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
44 # Compile the testcase shared library loader.
46 # OPTIONS is passed to gdb_compile when compiling the binary.
48 # On success, return 0.
49 # On failure, return -1.
50 proc compile_jit_dlmain {options} {
51 global main_loader_srcfile main_loader_binfile main_loader_basename
52 set options [concat $options debug]
54 if { [gdb_compile ${main_loader_srcfile} ${main_loader_binfile} \
55 executable $options] != "" } {
56 untested "failed to compile ${main_loader_basename}.c as an executable"
63 # Run $main_loader_binfile and load $main_solib_binfile in
64 # GDB. Check jit-related debug output and matches `info function`
65 # output for a jit loaded function using MATCH_STR.
67 # SOLIB_BINFILES_TARGETS is a list of shared libraries to pass
68 # as arguments when running $main_loader_binfile.
69 # MATCH_STR is a regular expression that output of `info function`
71 proc one_jit_test {solib_binfiles_target match_str} {
72 set count [llength $solib_binfiles_target]
73 with_test_prefix "one_jit_test-$count" {
75 global main_loader_binfile main_loader_srcfile
76 global main_solib_binfile main_solib_binfile_target main_solib_srcfile
78 clean_restart $main_loader_binfile
79 gdb_locate_shlib $main_solib_binfile
81 # This is just to help debugging when things fail
82 if {$test_verbose > 0} {
83 gdb_test "set debug jit 1"
86 if { ![runto_main] } {
90 gdb_breakpoint [gdb_get_line_number "break here before-dlopen" \
92 gdb_continue_to_breakpoint "break here before-dlopen"
93 gdb_test_no_output "set var jit_libname = \"$main_solib_binfile_target\"" \
94 "setting library name"
96 gdb_breakpoint [gdb_get_line_number "break here after-dlopen" \
98 gdb_continue_to_breakpoint "break here after-dlopen"
100 set line [gdb_get_line_number {break here 0} $main_solib_srcfile]
101 gdb_breakpoint "$main_solib_srcfile:$line"
102 gdb_continue_to_breakpoint "break here 0"
104 # Poke desired values directly into inferior instead of using "set args"
105 # because "set args" does not work under gdbserver.
106 gdb_test_no_output "set var argc=[expr $count + 1]" "forging argc"
107 gdb_test_no_output "set var argv=fake_argv" "forging argv"
108 for {set i 1} {$i <= $count} {incr i} {
109 set binfile_target [lindex $solib_binfiles_target [expr $i-1]]
110 gdb_test_no_output "set var argv\[$i\]=\"${binfile_target}\"" \
114 set line [gdb_get_line_number {break here 1} $main_solib_srcfile]
115 gdb_breakpoint "$main_solib_srcfile:$line"
116 gdb_continue_to_breakpoint "break here 1"
118 gdb_test "info function jit_function" "$match_str"
120 # This is just to help debugging when things fail
121 if {$test_verbose > 0} {
122 gdb_test "maintenance print objfiles"
123 gdb_test "maintenance info break"
126 set line [gdb_get_line_number {break here 2} $main_solib_srcfile]
127 gdb_breakpoint "$main_solib_srcfile:$line"
128 gdb_continue_to_breakpoint "break here 2"
130 # All jit librares must have been unregistered
131 gdb_test "info function jit_function" \
132 "All functions matching regular expression \"jit_function\":" \
133 "info function jit_function after unregistration"
137 # Compile the main code (which loads the JIT objects) as a shared library.
138 if { [compile_jit_elf_main_as_so $main_solib_srcfile $main_solib_binfile \
139 {additional_flags="-DMAIN=jit_dl_main"}] < 0 } {
143 set main_solib_binfile_target \
144 [gdb_download_shlib $main_solib_binfile]
146 # Compile the "real" main for this test.
147 if { [compile_jit_dlmain {shlib_load}] < 0 } {
151 # Compile two shared libraries to use as JIT objects.
152 set jit_solibs_target [compile_and_download_n_jit_so \
153 $jit_solib_basename $jit_solib_srcfile 2]
154 if { $jit_solibs_target == -1 } {
158 one_jit_test [lindex $jit_solibs_target 0] "${hex} jit_function_0001"
159 one_jit_test $jit_solibs_target "${hex} jit_function_0001\[\r\n\]+${hex} jit_function_0002"
161 foreach solib $jit_solibs_target {
162 # We don't intend to load the .so as a JIT debuginfo reader, but we
163 # need some handy file name for a completion test.
164 set input [string range $solib 0 [expr { [string length $solib] - 2 }]]
166 "complete jit-reader-load [standard_output_file $input]" \
167 "jit-reader-load \[^\r\n\]*$solib" \
168 "test jit-reader-load filename completion [file tail $solib]"