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