1# Copyright 2018-2023 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
16standard_testfile
17
18if { ![istarget "x86*"] } {
19    return
20}
21
22set cflags "-mindirect-branch=thunk -mfunction-return=thunk"
23
24if { [gcc_major_version] >= 8 } {
25    append cflags " -fcf-protection=none"
26}
27if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
28        [list debug "additional_flags=$cflags"]] } {
29    return -1
30}
31
32if { ![runto_main] } {
33    return -1
34}
35
36# Do repeated instruction steps in order to reach TARGET from CURRENT
37#
38#  CURRENT is a string matching the current location
39#  TARGET  is a string matching the target location
40#  TEST    is the test name
41#
42# The function issues repeated "stepi" commands as long as the location
43# matches CURRENT up to a maximum of 100 steps.
44#
45# TEST passes if the resulting location matches TARGET and fails
46# otherwise.
47#
48proc stepi_until { current target test } {
49    global gdb_prompt
50
51    set count 0
52    gdb_test_multiple "stepi" "$test" {
53        -re "$current.*$gdb_prompt $" {
54            incr count
55            if { $count < 100 } {
56                send_gdb "stepi\n"
57                exp_continue
58            } else {
59                fail "$test"
60            }
61        }
62        -re "$target.*$gdb_prompt $" {
63            pass "$test"
64        }
65    }
66}
67
68# Normal stepping steps through all thunks.
69gdb_test "step" "thrice\.2.*" "step into thrice"
70gdb_test "next" "thrice\.3.*" "step through thunks and over inc"
71gdb_test "step" "inc\.2.*" "step through call thunk into inc"
72gdb_test "step" "inc\.3.*" "step inside inc"
73gdb_test "step" "thrice\.4.*" "step through return thunk back into thrice"
74
75set alphanum_re "\[a-zA-Z0-9\]"
76set pic_thunk_re  "__$alphanum_re*\\.get_pc_thunk\\.$alphanum_re* \\(\\)"
77
78# We can use instruction stepping to step into thunks.
79stepi_until "thrice" "indirect_thunk" "stepi into call thunk"
80stepi_until "indirect_thunk" "inc." "stepi out of call thunk into inc"
81stepi_until "(inc|$pic_thunk_re)" "return_thunk" "stepi into return thunk"
82stepi_until "return_thunk" "thrice" \
83    "stepi out of return thunk back into thrice"
84