GDB: trad-frame: Store length of value_bytes in trad_frame_saved_reg
[binutils-gdb.git] / gdb / testsuite / gdb.base / stap-probe.exp
blob40e8c5e06f38e5bf4d8cba1e526b5ba9f0579b50
1 # Copyright (C) 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 standard_testfile
18 # Count the number of probes of TYPE (either 'stap' or 'dtrace'),
19 # from provider matching PROVIDER, with a name matching NAME, and from
20 # an objec file matching OBJECT.
22 # The OBJECT is optional, in which case all objects will be matched.
24 # If any error condition is detected, then perror is called, and -1
25 # returned.
27 # Otherwise, returns an integer, 0 or greater.
28 proc gdb_count_probes { type provider name { object "" }} {
29 set cmd "info probes ${type} ${provider} ${name}"
30 if { $object != "" } {
31 set cmd "$cmd ${object}"
34 set probe_count 0
35 set no_probes_line false
36 gdb_test_multiple $cmd "" {
37 -re "^$cmd\r\n" {
38 exp_continue
40 -re "^Type\\s+Provider\\s+Name\\s+Where\\s+Semaphore\\s+Object\\s*\r\n" {
41 exp_continue
43 -re "^\\s*\r\n" {
44 exp_continue
46 -re "^stap\[^\r\n\]+\r\n" {
47 incr probe_count
48 exp_continue
50 -re "^dtrace\[^\r\n\]+\r\n" {
51 incr probe_count
52 exp_continue
54 -re "^No probes matched\\.\r\n" {
55 set no_probes_line true
56 exp_continue
58 -re "^$::gdb_prompt $" {
59 pass $gdb_test_name
63 if { [expr $no_probes_line && $probe_count > 0] \
64 || [expr !$no_probes_line && $probe_count == 0] } {
65 perror "Mismatch between no probes found line, and probes count"
66 return -1
69 return $probe_count
72 proc check_for_usable_xmm0_probe { binfile } {
73 set readelf_program [gdb_find_readelf]
74 set binfile [standard_output_file $binfile]
75 set command "exec $readelf_program -n $binfile"
76 verbose -log "command is $command"
77 set result [catch $command output]
78 verbose -log "result is $result"
79 verbose -log "output is $output"
81 # We don't actually check RESULT. Sometimes readelf gives
82 # warnings about gaps in some of the notes data. This is
83 # unrelated to the staps probes, but still causes readelf to exit
84 # with non-zero status.
86 # Instead, just check the output. If readelf failed to run then
87 # the output will be empty, and the following regexps will fail to
88 # match.
90 # First, look for the xmmreg probe, and if we find it, grab the
91 # argument string.
92 if ![regexp {\n\s+Provider: test\n\s+Name: xmmreg\n[^\n]+\n\s+Arguments: ([^\n]+)\n} $output ignore arguments] {
93 verbose -log "APB: Couldn't find probe at all"
94 return false
97 verbose -log "APB: Matched on '$ignore'"
98 verbose -log "APB: arguments: '$arguments'"
100 # Check the the argument string mentions xmm0.
101 if ![regexp {@%?xmm0} $arguments] {
102 verbose -log "APB: Prove doesn't use xmm0 register"
103 return false
106 # Success! We have a probe that uses xmm0 for an argument.
107 return true
110 # Run the tests. We run the tests two different ways: once with a
111 # plain probe, and once with a probe that has an associated semaphore.
112 # This returns -1 on failure to compile or start, 0 otherwise.
113 proc stap_test {exec_name {args ""}} {
114 global testfile hex srcfile
116 set flags {}
117 lappend flags debug
118 lappend_include_file flags $::srcdir/lib/attributes.h
119 if { $args != "" } {
120 set flags [concat $flags $args]
123 if {[prepare_for_testing "failed to prepare" ${exec_name} $srcfile \
124 $flags]} {
125 return -1
128 set semaphore_addr_var ""
129 if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
130 gdb_test_no_output "set breakpoint always-inserted on"
131 set semaphore_addr_var \
132 [get_hexadecimal_valueof "&relocation_marker" "0"\
133 "get original address of relocation_marker"]
136 if ![runto_main] {
137 return -1
140 gdb_test "print \$_probe_argc" "No probe at PC $hex" \
141 "check argument not at probe point"
143 if {[string first "-DUSE_SEMAPHORES" $args] != -1} {
144 gdb_test_lines "info probes stap" "" \
145 "test *user *$hex *$hex .*"
146 } else {
147 gdb_test_lines "info probes stap" "" \
148 "test *user *$hex .*"
151 if {[runto "-pstap test:user"]} {
152 pass "run to -pstap test:user"
153 } else {
154 fail "run to -pstap test:user"
157 if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
158 set updated_semaphore_addr_var \
159 [get_hexadecimal_valueof "&relocation_marker" \
160 "0" "get revised relocation_marker address"]
161 set relocation_base \
162 [expr $updated_semaphore_addr_var - $semaphore_addr_var]
163 if {$relocation_base != 0} {
164 # Checks that GDB doesn't mistakenly relocate and write to null
165 # semaphore addresses. If it were to relocate a zero-valued
166 # semaphore address and increment the value at that address, we
167 # would expect to see "\200ELF" here instead.
168 gdb_test "p (*(char*) $relocation_base)@4" \
169 " = \"\\\\177ELF\"" \
170 "null semaphore relocation"
174 # Test probe arguments.
175 gdb_test "print \$_probe_argc" " = 1" \
176 "print \$_probe_argc for probe user"
177 gdb_test "print \$_probe_arg0 == x" " = 1" \
178 "check \$_probe_arg0 for probe user"
179 gdb_test "print \$_probe_arg1" \
180 "Invalid probe argument 1 -- probe has 1 arguments available" \
181 "check \$_probe_arg1 for probe user"
183 # Set a breakpoint with multiple probe locations.
184 gdb_test "break -pstap test:two" \
185 "Breakpoint \[0-9\]+ at $hex.*2 locations.*" \
186 "set multi-location probe breakpoint (probe two)"
188 # Reinit GDB, set a breakpoint on probe m4.
189 delete_breakpoints
190 if {[runto "-pstap test:m4"]} {
191 pass "run to -pstap test:m4"
192 } else {
193 fail "run to -pstap test:m4"
196 # Testing probe arguments.
197 gdb_test "print \$_probe_argc" " = 3" \
198 "print \$_probe_argc for probe m4"
199 gdb_test "print \$_probe_arg0" " = 42" \
200 "check \$_probe_arg0 for probe m4"
201 gdb_test "print (const char *) \$_probe_arg1" \
202 " = $hex .This is a test message.*" \
203 "check \$_probe_arg1 for probe m4"
204 gdb_test "print \$_probe_arg2 == v" " = 1" \
205 "check \$_probe_arg2 for probe m4"
207 # Reinit GDB, set a breakpoint on probe ps.
208 delete_breakpoints
209 if {[runto "-pstap test:ps"]} {
210 pass "run to -pstap test:ps"
211 } else {
212 fail "run to -pstap test:ps"
215 gdb_test "print \$_probe_argc" " = 3" \
216 "print \$_probe_argc for probe ps"
217 gdb_test "print (const char *) \$_probe_arg1" \
218 " = $hex .This is another test message.*" \
219 "print \$_probe_arg1 for probe ps"
221 # Check the probe is using the xmm0 register.
222 if [check_for_usable_xmm0_probe $exec_name] {
224 delete_breakpoints
225 if {[runto "-pstap test:xmmreg"]} {
226 pass "run to -pstap test:xmmreg"
227 } else {
228 fail "run to -pstap test:xmmreg"
231 gdb_test "print \$_probe_argc" " = 1" \
232 "print \$_probe_argc for probe xmmreg"
233 gdb_test "print/x \$_probe_arg0" " = 0x1234" \
234 "check \$_probe_arg0 for probe xmmreg"
235 } else {
236 unsupported "print probe argument from \$xmm0 register"
239 return 0
242 proc stap_test_no_debuginfo {exec_name {args ""}} {
243 global hex
245 set flags {}
246 lappend flags nodebug
247 lappend flags optimize=-O2
248 lappend_include_file flags $::srcdir/lib/attributes.h
249 if { $args != "" } {
250 set flags [concat $flags $args]
253 if {[prepare_for_testing "failed to prepare" ${exec_name} $::srcfile \
254 $flags]} {
255 return -1
258 if {[runto "-pstap test:user"]} {
259 pass "run to -pstap test:user"
260 } else {
261 fail "run to -pstap test:user"
264 # Test probe arguments.
265 gdb_test "print \$_probe_argc" " = 1" \
266 "print \$_probe_argc for probe user"
267 gdb_test "print \$_probe_arg0 == 23" " = 1" \
268 "check \$_probe_arg0 for probe user"
269 gdb_test "print \$_probe_arg1" \
270 "Invalid probe argument 1 -- probe has 1 arguments available" \
271 "check \$_probe_arg1 for probe user"
273 # Set a breakpoint with multiple probe locations.
274 # In this scenario, we may expect more than 2 locations because of
275 # the optimizations (inlining, loop unrolling, etc).
276 gdb_test "break -pstap test:two" \
277 "Breakpoint .* at $hex.*\[0-9\]+ locations.*" \
278 "set multi-location probe breakpoint (probe two)"
280 # Reinit GDB, set a breakpoint on probe m4.
281 delete_breakpoints
282 if {[runto "-pstap test:m4"]} {
283 pass "run to -pstap test:m4"
284 } else {
285 fail "run to -pstap test:m4"
288 # Testing probe arguments.
289 gdb_test "print \$_probe_argc" " = 3" \
290 "print \$_probe_argc for probe m4"
291 gdb_test "print \$_probe_arg0" " = 42" \
292 "check \$_probe_arg0 for probe m4"
293 gdb_test "print (const char *) \$_probe_arg1" \
294 " = $hex .This is a test message.*" \
295 "check \$_probe_arg1 for probe m4"
296 gdb_test "print \$_probe_arg2 == 0" " = 1" \
297 "check \$_probe_arg2 for probe m4"
299 # Reinit GDB, set a breakpoint on probe ps.
300 delete_breakpoints
301 if {[runto "-pstap test:ps"]} {
302 pass "run to -pstap test:ps"
303 } else {
304 fail "run to -pstap test:ps"
307 gdb_test "print \$_probe_argc" " = 3" \
308 "print \$_probe_argc for probe ps"
309 gdb_test "print (const char *) \$_probe_arg1" \
310 " = $hex .This is another test message.*" \
311 "print \$_probe_arg1 for probe ps"
313 # Reinit GDB, set a breakpoint on probe ps.
314 if { [gdb_count_probes stap test xmmreg] > 0 } {
315 delete_breakpoints
316 if {[runto "-pstap test:xmmreg"]} {
317 pass "run to -pstap test:xmmreg"
318 } else {
319 fail "run to -pstap test:xmmreg"
322 gdb_test "print \$_probe_argc" " = 1" \
323 "print \$_probe_argc for probe xmmreg"
324 gdb_test "print/x \$_probe_arg0" " = 0x1234" \
325 "check \$_probe_arg0 for probe xmmreg"
328 return 0
331 with_test_prefix "without semaphore, not optimized" {
332 if {[stap_test "stap-probe-nosem-noopt"] == -1} {
333 untested "stap probe test failed"
334 return -1
337 foreach_with_prefix pie { "nopie" "pie" } {
338 stap_test "stap-probe-nosem-noopt-$pie" $pie
342 with_test_prefix "with semaphore, not optimized" {
343 stap_test "stap-probe-sem-noopt" additional_flags=-DUSE_SEMAPHORES
346 with_test_prefix "without semaphore, optimized" {
347 stap_test_no_debuginfo "stap-probe-nosem-opt"
350 with_test_prefix "with semaphore, optimized" {
351 stap_test_no_debuginfo "stap-probe-sem-opt" additional_flags=-DUSE_SEMAPHORES