1# Copyright 2010-2024 Free Software Foundation, Inc.
2
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.
7#
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.
12#
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/>.
15
16# Test single stepping over Thumb-2 IT blocks.
17
18require {istarget arm*-*eabi*}
19
20standard_testfile .S
21
22if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
23    untested "failed to compile"
24    return -1
25}
26
27clean_restart $binfile
28
29if {![runto_main]} {
30    return -1
31}
32
33# Make sure that the compiler options allow Thumb-2.
34gdb_test_multiple "list" "list main" {
35    -re ".*@ No Thumb-2.*$gdb_prompt $" {
36          pass "list main"
37          untested "skipping tests due to lack of Thumb-2"
38          return -1
39    }
40    -re ".*@ Thumb-2 OK.*$gdb_prompt $" {
41          pass "list main"
42    }
43}
44
45proc test_it_block { func } {
46    global gdb_prompt
47    global software_step
48
49    if { ! [gdb_breakpoint "*${func}"] } {
50          unresolved "$func, IT block tests"
51          return
52    }
53
54    gdb_test "call (int) ${func}()" "Breakpoint.*@ Setup.*" "$func, call"
55
56    set expected 0
57    set reached 0
58    set steps 0
59    set ok 1
60    while { $ok } {
61          set ok 0
62          set msg "$func, stepi $steps"
63          gdb_test_multiple "stepi" "$msg" {
64              -re ".*@ Setup.*$gdb_prompt $" {
65                    pass "$msg"
66                    set ok 1
67              }
68              -re ".*@ IT instruction, Expected == (\[0-9\]*)\r\n$gdb_prompt $" {
69                    set expected $expect_out(1,string)
70                    pass "$msg"
71                    set ok 1
72              }
73              -re ".*@ Reached.*$gdb_prompt $" {
74                    incr reached
75                    pass "$msg"
76                    set ok 1
77                    if { [regexp {@ Reached, Set ([^\r\n]*)\r\n} $expect_out(0,string) dummy change] } {
78                        gdb_test "set $change" "" "$func, set $change"
79                    }
80              }
81              -re ".*@ Not reached.*$gdb_prompt $" {
82                    # An instruction in an IT block whose predicate is false when
83                    # we reach it.  If using software single step, we should not
84                    # stop here.
85                    if { $software_step } {
86                        fail "$msg"
87                    } else {
88                        pass "$msg"
89                        set ok 1
90                    }
91              }
92              -re ".*@ Never reached.*$gdb_prompt $" {
93                    # An instruction that should be branched over.
94                    fail "$msg"
95              }
96              -re ".*@ Done.*$gdb_prompt $" {
97                    pass "$msg"
98                    if { $reached == $expected } {
99                        pass "$func, correct instructions reached"
100                    } else {
101                        fail "$func, correct instructions reached"
102                    }
103                    if { [regexp {@ Done, Check ([^\r\n]*)\r\n} $expect_out(0,string) dummy check] } {
104                        gdb_test "print $check" ".* = 1" "$func, $check"
105                    }
106              }
107          }
108          if { ! $ok } {
109              break
110          }
111          incr steps
112          continue
113    }
114
115    gdb_test "continue" "" "$func, continue"
116    return
117}
118
119proc test_it_break { ndx } {
120    global software_step
121
122    set line [gdb_get_line_number "@ Break ${ndx}"]
123
124    if { ! [gdb_breakpoint "${line}"] } {
125          unresolved "continue to breakpoint: test ${ndx}"
126          return
127    }
128
129    if { $software_step } {
130          gdb_continue_to_breakpoint "test ${ndx}" ".*@ Location ${ndx}.*"
131    } else {
132          gdb_continue_to_breakpoint "test ${ndx}" ".*@ Break ${ndx}.*"
133    }
134}
135
136# If we are using software single-stepping in GDB, then GDB will not
137# stop at conditional instructions with a false predicate during stepi.
138# If we are using a simulator or debug interface with hardware single
139# step, then GDB will stop at such instructions.
140if { [istarget arm*-linux*] } {
141    set software_step 1
142} else {
143    set software_step 0
144}
145
146for { set i 1 } { $i <= 8 } { incr i } {
147    test_it_block it_${i}
148}
149
150gdb_breakpoint "*it_breakpoints"
151gdb_test "call (int) it_breakpoints()" "Breakpoint.*"
152for { set i 1 } { $i <= 7 } { incr i } {
153    test_it_break ${i}
154}
155