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