1 # Copyright 2012-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 standard_testfile jit-reader-host.c
18 if { (![istarget x86_64-*-*] && ![istarget i?86-*-*]) || ![is_lp64_target] } {
22 if {[skip_shlib_tests]} {
30 if {[get_compiler_info]} {
31 untested "could not get compiler info"
36 # Increase this to see more detail.
39 set jit_host_src $srcfile
40 set jit_host_bin $binfile
42 # We inject the complete path to jit-reader.h into the source file
43 # lest we end up (incorrectly) building against a system-installed
45 set jit_reader_header [standard_output_file "../../../../../gdb/jit-reader.h"]
46 set jit_reader_flag "-DJIT_READER_H=\"$jit_reader_header\""
48 if { [gdb_compile "${srcdir}/${subdir}/${jit_host_src}" "${jit_host_bin}" \
49 executable [list debug additional_flags=$jit_reader_flag]] != "" } {
50 untested "failed to compile"
54 set jit_reader jit-reader
55 set jit_reader_src ${jit_reader}.c
56 set jit_reader_bin [standard_output_file ${jit_reader}.so]
58 if { [gdb_compile_shlib "${srcdir}/${subdir}/${jit_reader_src}" "${jit_reader_bin}" \
59 [list debug additional_flags=$jit_reader_flag]] != "" } {
60 untested "failed to compile"
64 # Test "info registers" in the current frame, expecting RSP's value to
67 proc info_registers_current_frame {sp} {
72 set neg_decimal "-?$decimal"
76 "rax $hex +$neg_decimal" \
77 "rbx $hex +$neg_decimal" \
78 "rcx $hex +$neg_decimal" \
79 "rdx $hex +$neg_decimal" \
80 "rsi $hex +$neg_decimal" \
81 "rdi $hex +$neg_decimal" \
84 "r8 $hex +$neg_decimal" \
85 "r9 $hex +$neg_decimal" \
86 "r10 $hex +$neg_decimal" \
87 "r11 $hex +$neg_decimal" \
88 "r12 $hex +$neg_decimal" \
89 "r13 $hex +$neg_decimal" \
90 "r14 $hex +$neg_decimal" \
91 "r15 $hex +$neg_decimal" \
92 "rip $hex +$hex$any" \
93 "eflags $hex +\\\[$any\\\]" \
94 "cs $hex +$neg_decimal" \
95 "ss $hex +$neg_decimal" \
96 "ds $hex +$neg_decimal" \
97 "es $hex +$neg_decimal" \
98 "fs $hex +$neg_decimal" \
99 "gs $hex +$neg_decimal" \
102 # There may be more registers.
105 gdb_test "info registers" $expected
108 proc jit_reader_test {} {
110 global jit_reader_bin
116 clean_restart $jit_host_bin
117 gdb_load_shlib $jit_reader_bin
119 if {$test_verbose > 0} {
120 gdb_test_no_output "set debug jit 1"
123 gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"
125 gdb_test "" "Program received signal SIGTRAP, .*" "expect SIGTRAP"
127 # Test the JIT reader unwinder.
128 with_test_prefix "with jit-reader" {
130 with_test_prefix "before mangling" {
133 "#0 ${any} in jit_function_stack_mangle ${any}" \
134 "#1 ${any} in main ${any}" \
138 set sp_before_mangling \
139 [get_hexadecimal_valueof "\$sp" 0 "get sp"]
141 gdb_test "up" "#1 $any in main $any\r\n$any function_stack_mangle $any" \
145 [get_hexadecimal_valueof "\$sp" 0 "get caller sp"]
148 # Step over the instruction that mangles the stack pointer.
149 # While that confuses GDB's built-in unwinder, the JIT
150 # reader's unwinder understands the mangling and should thus
151 # be able to unwind at that location.
152 with_test_prefix "after mangling" {
153 gdb_test "si" "in jit_function_stack_mangle .*" "step over stack mangling"
155 set sp_after_mangling \
156 [get_hexadecimal_valueof "\$sp" 0 "get sp"]
158 gdb_assert {$sp_before_mangling != $sp_after_mangling} \
161 # Check that the jit unwinder manages to backtrace through
162 # the mangled stack pointer.
165 "#0 ${any} in jit_function_stack_mangle ${any}" \
166 "#1 ${any} in main ${any}" \
170 with_test_prefix "current frame" {
171 info_registers_current_frame $sp_after_mangling
173 gdb_test "info frame" \
174 "Stack level 0, frame at $sp_before_mangling.*in jit_function_stack_mangle.*"
177 with_test_prefix "caller frame" {
178 gdb_test "up" "#1 $any in main $any\r\n$any function_stack_mangle $any" \
181 # Since the JIT unwinder only provides RIP/RSP/RBP,
182 # all other registers should show as "<not saved>".
193 "rsp $caller_sp +$caller_sp" \
202 "rip $hex +$hex $any" \
203 "eflags <not saved>" \
212 # There may be more registers.
215 gdb_test "info registers" $expected
217 # Make sure that "info frame" doesn't crash.
218 gdb_test "info frame" "Stack level 1, .*in main.*"
220 # ... and that neither does printing a pseudo
222 gdb_test "print /x \$ebp" " = $hex" "print pseudo register"
224 # There's no way for the JIT reader API to support
225 # modifyiable values.
226 gdb_test "print \$rbp = -1" \
227 "Attempt to assign to an unmodifiable value\." \
228 "cannot assign to register"
233 # Now unload the jit reader, and ensure that backtracing really
234 # doesn't work without it.
235 with_test_prefix "without jit-reader" {
236 gdb_test_no_output "jit-reader-unload ${jit_reader_bin}" \
239 # Check that we're no longer using the JIT unwinder, and that
240 # the built-in unwinder cannot backtrace through the mangled
244 "Backtrace stopped: Cannot access memory at address $sp_after_mangling" \
248 gdb_test "info frame" "Cannot access memory at address.*" \
249 "info frame shows error"
250 info_registers_current_frame $sp_after_mangling
251 gdb_test "up" "Initial frame selected; you cannot go up\\." \
255 with_test_prefix "with jit-reader again" {
256 gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"
258 # Check that the jit unwinder manages to backtrace through
259 # the mangled stack pointer.
262 "#0 ${any} in jit_function_stack_mangle ${any}" \
263 "#1 ${any} in main ${any}" \