Automatic date update in version.in
[binutils-gdb.git] / gdb / testsuite / gdb.trace / signal.exp
blob6f0e520413c7b426c1dbfcf077c676f5158ae0e7
1 #   Copyright 2016-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 # This is to test whether GDBserver or other remote stubs deliver signal
17 # to the inferior while step over thread.  The program signal.c sends
18 # signal SIGABRT to itself via kill syscall.  The test sets tracepoints
19 # syscall instruction and the next one, and it is quite likely that
20 # GDBserver gets the signal when it steps over thread and does the
21 # collection.  If GDBserver doesn't deliver signal in thread step over,
22 # one collection is got for one tracepoint hit.  Otherwise, there may
23 # be two collections for one tracepoint hit, because tracepoint is
24 # collected once before step over, the program goes into signal handler
25 # (because signal is delivered in step over), and program goes back
26 # to the tracepoint again (one more collection) after returns from
27 # signal handler.
29 load_lib "trace-support.exp"
31 require {!target_info exists gdb,nosignals}
33 standard_testfile
35 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
36     return -1
39 if ![runto_main] {
40     return -1
43 if ![gdb_target_supports_trace] {
44     unsupported "target does not support trace"
45     return -1
48 # Step 1, find the syscall instruction address.
50 set syscall_insn ""
52 # Define the syscall instruction for each target.
54 if { [istarget "i\[34567\]86-*-linux*"] || [istarget "x86_64-*-linux*"] } {
55     set syscall_insn "\[ \t\](int|syscall|sysenter)"
56 } elseif { [istarget "aarch64*-*-linux*"] || [istarget "arm*-*-linux*"] } {
57     set syscall_insn "\[ \t\](swi|svc)\[ \t\]"
58 } else {
59     unsupported "unknown syscall instruction"
60     return -1
63 # Start with a fresh gdb.
64 clean_restart ${testfile}
65 if ![runto_main] {
66     return -1
69 gdb_breakpoint "kill" qualified
70 gdb_test "handle SIGABRT nostop noprint pass" ".*" "pass SIGABRT"
72 # Hit the breakpoint on $syscall for the first time.  In this time,
73 # we will let PLT resolution done, and the number single steps we will
74 # do later will be reduced.
75 gdb_test "continue" "Continuing\\..*Breakpoint $decimal, (.* in |__libc_|)kill \\(\\).*" \
76     "continue to kill, 1st time"
78 # Hit the breakpoint on $syscall for the second time.  In this time,
79 # the address of syscall insn and next insn of syscall are recorded.
80 gdb_test "continue" "Continuing\\..*Breakpoint $decimal, (.* in |__libc_|)kill \\(\\).*" \
81     "continue to kill, 2nd time"
83 gdb_test "display/i \$pc" ".*"
85 # Single step until we see a syscall insn or we reach the
86 # upper bound of loop iterations.
87 set msg "find syscall insn in kill"
88 set steps 0
89 set max_steps 1000
90 gdb_test_multiple "stepi" $msg {
91     -re ".*$syscall_insn.*$gdb_prompt $" {
92         pass $msg
93     }
94     -re "x/i .*=>.*\r\n$gdb_prompt $" {
95         incr steps
96         if {$steps == $max_steps} {
97             fail $msg
98         } else {
99             send_gdb "stepi\n"
100             exp_continue
101         }
102     }
105 if {$steps == $max_steps} {
106     return
109 # Remove the display
110 gdb_test_no_output "delete display 1"
112 set syscall_insn_addr [get_hexadecimal_valueof "\$pc" "0"]
113 set syscall_insn_next 0
114 set test "x/2i \$pc"
115 gdb_test_multiple $test $test {
116     -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
117         set syscall_insn_next $expect_out(1,string)
118     }
121 delete_breakpoints
122 gdb_breakpoint "start" qualified
123 gdb_continue_to_breakpoint "continue to start"
125 with_test_prefix "counter is zero" {
126     gdb_assert { 0 == [get_integer_valueof "counter" "1"] } "assert check"
129 delete_breakpoints
131 # Step 2, set tracepoints on syscall instruction and the next one.
132 # It is more likely to get signal on these two places when GDBserver
133 # is doing step-over.
134 gdb_test "trace *$syscall_insn_addr" "Tracepoint $decimal at .*" \
135     "tracepoint on syscall instruction"
136 set tpnum [get_integer_valueof "\$bpnum" 0]
137 gdb_test "trace *$syscall_insn_next" "Tracepoint $decimal at .*" \
138     "tracepoint on instruction following syscall instruction"
140 gdb_breakpoint "end" qualified
142 gdb_test_no_output "tstart"
143 gdb_test "continue" ".*Breakpoint.* end .*at.*$srcfile.*" \
144     "continue to end"
145 gdb_test_no_output "tstop"
147 set iterations [get_integer_valueof "iterations" "0"]
149 with_test_prefix "iterations equals to counter" {
150     gdb_assert { $iterations == [get_integer_valueof "counter" "0"] } \
151         "assert check"
154 # Record the hit times of each tracepoint in this array.
155 array set tracepoint_hits { }
156 for { set i $tpnum } { $i < [expr $tpnum + 2] } { incr i } {
157   set tracepoint_hits($i) 0
160 while { 1 } {
161    set test "tfind"
162    set idx 0
163    gdb_test_multiple $test $test {
164         -re "Found trace frame $decimal, tracepoint ($decimal).*\r\n$gdb_prompt $" {
165             set idx [expr $expect_out(1,string)]
166             incr tracepoint_hits($idx)
167         }
168        -re "Target failed to find requested trace frame\..*\r\n$gdb_prompt $" {
169            set idx 0
170        }
171    }
172    if {$idx == 0} {
173         break
174    }
177 # Step 3, check the number of collections on each tracepoint.
179 for { set i $tpnum } { $i < [expr $tpnum + 2] } { incr i } {
181     if { $tracepoint_hits($i) == $iterations } {
182         pass "tracepoint $i hit $iterations times"
183     } elseif { $tracepoint_hits($i) > $iterations } {
184         # GDBserver deliver the signal while stepping over tracepoint,
185         # so it is possible that a tracepoint is collected twice.
186         pass "tracepoint $i hit $iterations times (spurious collection)"
187     } else {
188         fail "tracepoint $i hit $iterations times"
189     }