aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/unwind-on-each-insn.exp
blob: fc48bf5c0618fdec0a0bc6b7b6b366d0736240f4 (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
# Copyright 2022-2023 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/>.  */

# Single step through a simple (empty) function that was compiled
# without DWARF debug information.
#
# At each instruction check that the frame-id, and frame base address,
# are calculated correctly.
#
# Additionally, check we can correctly unwind to the previous frame,
# and that the previous stack-pointer value, and frame base address
# value, can be calculated correctly.

standard_testfile .c -foo.c

set debug_flags {debug}
set nodebug_flags {nodebug}

# Make sure that we don't use .eh_frame info, by not generating it,
# using -fno-asynchronous-unwind-tables, if supported.
if { [gdb_can_simple_compile fno-asynchronous-unwind-tables \
	  { void foo () { } } object -fno-asynchronous-unwind-tables] } {
    lappend nodebug_flags additional_flags=-fno-asynchronous-unwind-tables
}

if {[prepare_for_testing_full "failed to prepare" \
	 [list ${testfile} $debug_flags \
	      $srcfile $debug_flags $srcfile2 $nodebug_flags]]} {
    return -1
}

if {![runto_main]} {
    return 0
}

# Return a two element list, the first element is the stack-pointer
# value (from the $sp register), and the second element is the frame
# base address (from the 'info frame' output).
proc get_sp_and_fba { testname } {
    with_test_prefix "get \$sp and frame base $testname" {
	set sp [get_hexadecimal_valueof "\$sp" "*UNKNOWN*"]

	set fba ""
	gdb_test_multiple "info frame" "" {
	    -re -wrap ".*Stack level ${::decimal}, frame at ($::hex):.*" {
		set fba $expect_out(1,string)
	    }
	}

	return [list $sp $fba]
    }
}

# Return the frame-id of the current frame, collected using the 'maint
# print frame-id' command.
proc get_fid { } {
    set fid ""
    gdb_test_multiple "maint print frame-id" "" {
	-re -wrap ".*frame-id for frame #${::decimal}: (.*)" {
	    set fid $expect_out(1,string)
	}
    }
    return $fid
}

# Record the current stack-pointer, and the frame base address.
lassign [get_sp_and_fba "in main"] main_sp main_fba
set main_fid [get_fid]

# Now enter the foo function.
gdb_breakpoint "*foo"
gdb_continue_to_breakpoint "enter foo"

# Record the current stack-pointer, and the frame base address.
lassign [get_sp_and_fba "in foo"] foo_sp foo_fba
set foo_fid [get_fid]

for { set i_count 1 } { true } { incr i_count } {
    with_test_prefix "instruction ${i_count}" {

	# The current stack-pointer value can legitimately change
	# throughout the lifetime of a function, so we don't check the
	# current stack-pointer value.  But the frame base address
	# should not change, so we do check for that.
	lassign [get_sp_and_fba "for foo"] sp_value fba_value
	gdb_assert { $fba_value == $foo_fba }

	# The frame-id should never change within a function, so check
	# that now.
	set fid [get_fid]
	gdb_assert { [string equal $fid $foo_fid] } \
	    "check frame-id matches"

	# Check that the previous frame is 'main'.
	gdb_test "bt 2" "\r\n#1\\s+\[^\r\n\]+ in main \\(\\) .*"

	# Move up the stack (to main).
	gdb_test "up" \
	    "\r\n#1\\s+\[^\r\n\]+ in main \\(\\) .*"

	# Check we can unwind the stack-pointer and the frame base
	# address correctly.
	lassign [get_sp_and_fba "for main"] sp_value fba_value
	gdb_assert { $sp_value == $main_sp }
	gdb_assert { $fba_value == $main_fba }

	# Check we have a consistent value for main's frame-id.
	set fid [get_fid]
	gdb_assert { [string equal $fid $main_fid] }

	# Move back to the inner most frame.
	gdb_test "frame 0" ".*"

	if { $i_count > 100 } {
	    # We expect a handful of instructions, if we reach 100,
	    # something is going wrong.  Avoid an infinite loop.
	    fail "exceeded max number of instructions"
	    break
	}

	set in_foo 0
	gdb_test_multiple "stepi" "" {
	    -re -wrap "$hex in foo \\(\\)" {
		set in_foo 1
	    }
	    -re -wrap "" {}
	}

	if { ! $in_foo } {
	    break
	}
    }
}