1 # Copyright 2012-2024 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 # Optionally test a Python API here as well.
17 load_lib gdb-python.exp
19 standard_testfile jit-reader-host.c
21 require {is_any_target "i?86-*-*" "x86_64-*-*"} is_lp64_target
23 require allow_shlib_tests isnative
25 # Increase this to see more detail.
28 set jit_host_src $srcfile
29 set jit_host_bin $binfile
31 # We inject the complete path to jit-reader.h into the source file
32 # lest we end up (incorrectly) building against a system-installed
34 set jit_reader_header [standard_output_file "../../../../../gdb/jit-reader.h"]
35 set jit_reader_flag "-DJIT_READER_H=\"$jit_reader_header\""
37 if { [gdb_compile "${srcdir}/${subdir}/${jit_host_src}" "${jit_host_bin}" \
38 executable [list debug additional_flags=$jit_reader_flag]] != "" } {
39 untested "failed to compile"
43 set jit_reader jit-reader
44 set jit_reader_src ${jit_reader}.c
45 set jit_reader_bin [standard_output_file ${jit_reader}.so]
47 if { [gdb_compile_shlib "${srcdir}/${subdir}/${jit_reader_src}" "${jit_reader_bin}" \
48 [list debug additional_flags=$jit_reader_flag]] != "" } {
49 untested "failed to compile"
53 # Test "info registers" in the current frame, expecting RSP's value to
56 proc info_registers_current_frame {sp} {
61 set neg_decimal "-?$decimal"
65 "rax $hex +$neg_decimal" \
66 "rbx $hex +$neg_decimal" \
67 "rcx $hex +$neg_decimal" \
68 "rdx $hex +$neg_decimal" \
69 "rsi $hex +$neg_decimal" \
70 "rdi $hex +$neg_decimal" \
73 "r8 $hex +$neg_decimal" \
74 "r9 $hex +$neg_decimal" \
75 "r10 $hex +$neg_decimal" \
76 "r11 $hex +$neg_decimal" \
77 "r12 $hex +$neg_decimal" \
78 "r13 $hex +$neg_decimal" \
79 "r14 $hex +$neg_decimal" \
80 "r15 $hex +$neg_decimal" \
81 "rip $hex +$hex$any" \
82 "eflags $hex +\\\[$any\\\]" \
83 "cs $hex +$neg_decimal" \
84 "ss $hex +$neg_decimal" \
85 "ds $hex +$neg_decimal" \
86 "es $hex +$neg_decimal" \
87 "fs $hex +$neg_decimal" \
88 "gs $hex +$neg_decimal" \
91 # There may be more registers.
94 gdb_test "info registers" $expected
97 proc jit_reader_test {} {
105 clean_restart $jit_host_bin
106 gdb_load_shlib $jit_reader_bin
108 if {$test_verbose > 0} {
109 gdb_test_no_output "set debug jit 1"
112 gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"
114 gdb_test "" "Program received signal SIGTRAP, .*" "expect SIGTRAP"
116 # Test the JIT reader unwinder.
117 with_test_prefix "with jit-reader" {
119 with_test_prefix "before mangling" {
122 "#0 ${any} in jit_function_stack_mangle ${any}" \
123 "#1 ${any} in main ${any}" \
127 set sp_before_mangling \
128 [get_hexadecimal_valueof "\$sp" 0 "get sp"]
130 gdb_test "up" "#1 $any in main $any\r\n$any function_stack_mangle $any" \
134 [get_hexadecimal_valueof "\$sp" 0 "get caller sp"]
137 # Step over the instruction that mangles the stack pointer.
138 # While that confuses GDB's built-in unwinder, the JIT
139 # reader's unwinder understands the mangling and should thus
140 # be able to unwind at that location.
141 with_test_prefix "after mangling" {
142 gdb_test "si" "in jit_function_stack_mangle .*" "step over stack mangling"
144 set sp_after_mangling \
145 [get_hexadecimal_valueof "\$sp" 0 "get sp"]
147 gdb_assert {$sp_before_mangling != $sp_after_mangling} \
150 # Check that the jit unwinder manages to backtrace through
151 # the mangled stack pointer.
154 "#0 ${any} in jit_function_stack_mangle ${any}" \
155 "#1 ${any} in main ${any}" \
159 with_test_prefix "current frame" {
160 info_registers_current_frame $sp_after_mangling
162 gdb_test "info frame" \
163 "Stack level 0, frame at $sp_before_mangling.*in jit_function_stack_mangle.*"
166 with_test_prefix "caller frame" {
167 gdb_test "up" "#1 $any in main $any\r\n$any function_stack_mangle $any" \
170 # Since the JIT unwinder only provides RIP/RSP/RBP,
171 # all other registers should show as "<not saved>".
182 "rsp $caller_sp +$caller_sp" \
191 "rip $hex +$hex $any" \
192 "eflags <not saved>" \
201 # There may be more registers.
204 gdb_test "info registers" $expected
206 # Make sure that "info frame" doesn't crash.
207 gdb_test "info frame" "Stack level 1, .*in main.*"
209 # ... and that neither does printing a pseudo
211 gdb_test "print /x \$ebp" " = $hex" "print pseudo register"
213 # There's no way for the JIT reader API to support
214 # modifyiable values.
215 gdb_test "print \$rbp = -1" \
216 "Attempt to assign to an unmodifiable value\." \
217 "cannot assign to register"
220 if { [allow_python_tests] } {
221 gdb_test "python print(gdb.objfiles())" \
222 "$any<gdb.Objfile filename=<< JIT compiled code at $hex >>>$any" \
223 "python gdb.Objfile.__repr__ ()"
225 gdb_test "python print(list(map(lambda objf : objf.filename, gdb.objfiles())))" \
226 "$any'<< JIT compiled code at $hex >>'$any" \
227 "python gdb.Objfile.filename"
229 gdb_test "python print( \[o for o in gdb.objfiles() if o.filename.startswith('<< JIT compiled code')\]\[0\].build_id )" \
231 "python gdb.Objfile.build_id"
236 # Now unload the jit reader, and ensure that backtracing really
237 # doesn't work without it.
238 with_test_prefix "without jit-reader" {
239 gdb_test_no_output "jit-reader-unload ${jit_reader_bin}" \
242 # Check that we're no longer using the JIT unwinder, and that
243 # the built-in unwinder cannot backtrace through the mangled
246 "Backtrace stopped: Cannot access memory at address $sp_after_mangling" \
249 gdb_test "info frame" "Cannot access memory at address.*" \
250 "info frame shows error"
251 info_registers_current_frame $sp_after_mangling
252 gdb_test "up" "Initial frame selected; you cannot go up\\." \
256 with_test_prefix "with jit-reader again" {
257 gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"
259 # Check that the jit unwinder manages to backtrace through
260 # the mangled stack pointer.
263 "#0 ${any} in jit_function_stack_mangle ${any}" \
264 "#1 ${any} in main ${any}" \
268 if {[allow_python_tests]} {
269 gdb_test "python print(any(\[not x.is_file for x in gdb.objfiles()\]))" \
271 "at least one non-file objfile"
272 gdb_test "python print(any(\[x.is_file for x in gdb.objfiles()\]))" \
274 "at least one file-based objfile"
277 with_test_prefix "test dwarf unwinder" {
278 # Check that the DWARF unwinder does not crash in presence of
281 gdb_breakpoint "*function_add" temporary
282 gdb_test "cont" ".*Temporary breakpoint ${any} in jit_function_add .*"
285 "#0 ${any} in jit_function_add ${any}" \
286 "#1 ${any} in main ${any}" \