xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/jit-elf-so.exp (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
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# The same tests as in jit.exp, but loading JITer itself from a shared
17# library.
18
19if {[skip_shlib_tests]} {
20    untested "skipping shared library tests"
21    return -1
22}
23
24load_lib jit-elf-helpers.exp
25
26# Increase this to see more detail.
27set test_verbose 0
28
29# The "real" main of this test, which loads jit-elf-main
30# as a shared library.
31set main_loader_basename jit-elf-dlmain
32set main_loader_srcfile ${srcdir}/${subdir}/${main_loader_basename}.c
33set main_loader_binfile [standard_output_file ${main_loader_basename}]
34
35# The main code that loads and registers JIT objects.
36set main_solib_basename jit-elf-main
37set main_solib_srcfile ${srcdir}/${subdir}/${main_solib_basename}.c
38set main_solib_binfile [standard_output_file ${main_solib_basename}.so]
39
40# The shared library that gets loaded as JIT objects.
41set jit_solib_basename jit-elf-solib
42set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
43
44# Compile the testcase shared library loader.
45#
46# OPTIONS is passed to gdb_compile when compiling the binary.
47#
48# On success, return 0.
49# On failure, return -1.
50proc compile_jit_dlmain {options} {
51    global main_loader_srcfile main_loader_binfile main_loader_basename
52    set options [concat $options debug]
53
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"
57	return -1
58    }
59
60    return 0
61}
62
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.
66#
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`
70# must match.
71proc one_jit_test {solib_binfiles_target match_str} {
72    set count [llength $solib_binfiles_target]
73    with_test_prefix "one_jit_test-$count" {
74	global test_verbose
75	global main_loader_binfile main_loader_srcfile
76	global main_solib_binfile main_solib_binfile_target main_solib_srcfile
77
78	clean_restart $main_loader_binfile
79	gdb_locate_shlib $main_solib_binfile
80
81	# This is just to help debugging when things fail
82	if {$test_verbose > 0} {
83	    gdb_test "set debug jit 1"
84	}
85
86	if { ![runto_main] } {
87	    return
88	}
89
90	gdb_breakpoint [gdb_get_line_number "break here before-dlopen" \
91			    $main_loader_srcfile]
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"
95
96	gdb_breakpoint [gdb_get_line_number "break here after-dlopen" \
97			$main_loader_srcfile]
98	gdb_continue_to_breakpoint "break here after-dlopen"
99
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"
103
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}\"" \
111		"forging argv\[$i\]"
112	}
113
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"
117
118	gdb_test "info function jit_function" "$match_str"
119
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"
124	}
125
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"
129
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"
134    }
135}
136
137# Compile the main code (which loads the JIT objects) as a shared library.
138if { [compile_jit_elf_main_as_so $main_solib_srcfile $main_solib_binfile \
139	{additional_flags="-DMAIN=jit_dl_main"}] < 0 } {
140    return
141}
142
143set main_solib_binfile_target \
144    [gdb_download_shlib $main_solib_binfile]
145
146# Compile the "real" main for this test.
147if { [compile_jit_dlmain {shlib_load}] < 0 } {
148    return
149}
150
151# Compile two shared libraries to use as JIT objects.
152set jit_solibs_target [compile_and_download_n_jit_so \
153		      $jit_solib_basename $jit_solib_srcfile 2]
154if { $jit_solibs_target == -1 } {
155    return
156}
157
158one_jit_test [lindex $jit_solibs_target 0] "${hex}  jit_function_0001"
159one_jit_test $jit_solibs_target "${hex}  jit_function_0001\[\r\n\]+${hex}  jit_function_0002"
160
161foreach 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 }]]
165    gdb_test \
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]"
169}
170