aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/jit-reader.exp
blob: 83e403653d69ac7ef87635f8ff5231ea60998fd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2012-2021 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

standard_testfile jit-reader-host.c

if { (![istarget x86_64-*-*] && ![istarget i?86-*-*]) || ![is_lp64_target] } {
    return -1;
}

if {[skip_shlib_tests]} {
    return -1
}

if { ![isnative] } {
    return -1
}

if {[get_compiler_info]} {
    untested "could not get compiler info"
    return 1
}


# Increase this to see more detail.
set test_verbose 0

set jit_host_src $srcfile
set jit_host_bin $binfile

# We inject the complete path to jit-reader.h into the source file
# lest we end up (incorrectly) building against a system-installed
# version.
set jit_reader_header [standard_output_file "../../../../../gdb/jit-reader.h"]
set jit_reader_flag "-DJIT_READER_H=\"$jit_reader_header\""

if  { [gdb_compile "${srcdir}/${subdir}/${jit_host_src}" "${jit_host_bin}" \
       executable  [list debug additional_flags=$jit_reader_flag]] != "" } {
    untested "failed to compile"
    return -1
}

set jit_reader jit-reader
set jit_reader_src ${jit_reader}.c
set jit_reader_bin [standard_output_file ${jit_reader}.so]

if { [gdb_compile_shlib "${srcdir}/${subdir}/${jit_reader_src}" "${jit_reader_bin}" \
	  [list debug additional_flags=$jit_reader_flag]] != "" } {
    untested "failed to compile"
    return -1
}

# Test "info registers" in the current frame, expecting RSP's value to
# be SP.

proc info_registers_current_frame {sp} {
    global hex decimal

    set any "\[^\r\n\]*"

    set neg_decimal "-?$decimal"

    set expected \
	[multi_line \
	     "rax            $hex +$neg_decimal" \
	     "rbx            $hex +$neg_decimal" \
	     "rcx            $hex +$neg_decimal" \
	     "rdx            $hex +$neg_decimal" \
	     "rsi            $hex +$neg_decimal" \
	     "rdi            $hex +$neg_decimal" \
	     "rbp            $hex +$hex" \
	     "rsp            $sp +$sp" \
	     "r8             $hex +$neg_decimal" \
	     "r9             $hex +$neg_decimal" \
	     "r10            $hex +$neg_decimal" \
	     "r11            $hex +$neg_decimal" \
	     "r12            $hex +$neg_decimal" \
	     "r13            $hex +$neg_decimal" \
	     "r14            $hex +$neg_decimal" \
	     "r15            $hex +$neg_decimal" \
	     "rip            $hex +$hex$any" \
	     "eflags         $hex +\\\[$any\\\]" \
	     "cs             $hex +$neg_decimal" \
	     "ss             $hex +$neg_decimal" \
	     "ds             $hex +$neg_decimal" \
	     "es             $hex +$neg_decimal" \
	     "fs             $hex +$neg_decimal" \
	     "gs             $hex +$neg_decimal" \
	    ]

    # There may be more registers.
    append expected ".*"

    gdb_test "info registers" $expected
}

proc jit_reader_test {} {
    global jit_host_bin
    global jit_reader_bin
    global test_verbose
    global hex decimal

    set any "\[^\r\n\]*"

    clean_restart $jit_host_bin
    gdb_load_shlib $jit_reader_bin

    if {$test_verbose > 0} {
	gdb_test_no_output "set debug jit 1"
    }

    gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"
    gdb_run_cmd
    gdb_test "" "Program received signal SIGTRAP, .*" "expect SIGTRAP"

    # Test the JIT reader unwinder.
    with_test_prefix "with jit-reader" {

	with_test_prefix "before mangling" {
	    gdb_test "bt" \
		[multi_line \
		     "#0 ${any} in jit_function_stack_mangle ${any}" \
		     "#1 ${any} in main ${any}" \
		    ] \
		"bt works"

	    set sp_before_mangling \
		[get_hexadecimal_valueof "\$sp" 0 "get sp"]

	    gdb_test "up" "#1  $any in main $any\r\n$any  function_stack_mangle $any" \
		"move up to caller"

	    set caller_sp \
		[get_hexadecimal_valueof "\$sp" 0 "get caller sp"]
	}

	# Step over the instruction that mangles the stack pointer.
	# While that confuses GDB's built-in unwinder, the JIT
	# reader's unwinder understands the mangling and should thus
	# be able to unwind at that location.
	with_test_prefix "after mangling" {
	    gdb_test "si" "in jit_function_stack_mangle .*" "step over stack mangling"

	    set sp_after_mangling \
		[get_hexadecimal_valueof "\$sp" 0 "get sp"]

	    gdb_assert {$sp_before_mangling != $sp_after_mangling} \
		"sp is mangled"

	    # Check that the jit unwinder manages to backtrace through
	    # the mangled stack pointer.
	    gdb_test "bt" \
		[multi_line \
		     "#0 ${any} in jit_function_stack_mangle ${any}" \
		     "#1 ${any} in main ${any}" \
		    ] \
		"bt works"

	    with_test_prefix "current frame" {
		info_registers_current_frame $sp_after_mangling

		gdb_test "info frame" \
		    "Stack level 0, frame at $sp_before_mangling.*in jit_function_stack_mangle.*"
	    }

	    with_test_prefix "caller frame" {
		gdb_test "up" "#1  $any in main $any\r\n$any  function_stack_mangle $any" \
		    "up to caller"

		# Since the JIT unwinder only provides RIP/RSP/RBP,
		# all other registers should show as "<not saved>".

		set expected \
		    [multi_line \
			 "rax            <not saved>" \
			 "rbx            <not saved>" \
			 "rcx            <not saved>" \
			 "rdx            <not saved>" \
			 "rsi            <not saved>" \
			 "rdi            <not saved>" \
			 "rbp            $hex +$hex" \
			 "rsp            $caller_sp +$caller_sp" \
			 "r8             <not saved>" \
			 "r9             <not saved>" \
			 "r10            <not saved>" \
			 "r11            <not saved>" \
			 "r12            <not saved>" \
			 "r13            <not saved>" \
			 "r14            <not saved>" \
			 "r15            <not saved>" \
			 "rip            $hex +$hex $any" \
			 "eflags         <not saved>" \
			 "cs             <not saved>" \
			 "ss             <not saved>" \
			 "ds             <not saved>" \
			 "es             <not saved>" \
			 "fs             <not saved>" \
			 "gs             <not saved>" \
			]

		# There may be more registers.
		append expected ".*"

		gdb_test "info registers" $expected

		# Make sure that "info frame" doesn't crash.
		gdb_test "info frame" "Stack level 1, .*in main.*"

		# ... and that neither does printing a pseudo
		# register.
		gdb_test "print /x \$ebp" " = $hex" "print pseudo register"

		# There's no way for the JIT reader API to support
		# modifyiable values.
		gdb_test "print \$rbp = -1" \
		    "Attempt to assign to an unmodifiable value\." \
		    "cannot assign to register"
	    }
	}
    }

    # Now unload the jit reader, and ensure that backtracing really
    # doesn't work without it.
    with_test_prefix "without jit-reader" {
	gdb_test_no_output "jit-reader-unload ${jit_reader_bin}" \
	    "jit-reader-unload"

	# Check that we're no longer using the JIT unwinder, and that
	# the built-in unwinder cannot backtrace through the mangled
	# stack pointer.
	gdb_test "bt" \
	    "Backtrace stopped: Cannot access memory at address $sp_after_mangling" \
	    "bt shows error"

	gdb_test "info frame" "Cannot access memory at address.*" \
	    "info frame shows error"
	info_registers_current_frame $sp_after_mangling
	gdb_test "up" "Initial frame selected; you cannot go up\\." \
	    "cannot go up"
    }

    with_test_prefix "with jit-reader again" {
	gdb_test_no_output "jit-reader-load ${jit_reader_bin}" "jit-reader-load"

	# Check that the jit unwinder manages to backtrace through
	# the mangled stack pointer.
	gdb_test "bt" \
	    [multi_line \
		 "#0 ${any} in jit_function_stack_mangle ${any}" \
		 "#1 ${any} in main ${any}" \
		]
    }
}

jit_reader_test