restore.exp revision 1.9
1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 1998-2020 Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Test GDB's ability to restore saved registers from stack frames
19# when using the `return' command.
20#
21# This file was written by Jim Blandy <jimb@cygnus.com>, with
22# fragments borrowed from return.exp.
23
24standard_testfile
25set executable $testfile
26
27if { [prepare_for_testing "failed to prepare" $executable $srcfile] } {
28    return -1
29}
30
31proc restore_tests { } {
32    global gdb_prompt
33
34    if { ! [ runto driver ] } {
35	return 0
36    }
37
38    set limit 5
39
40    # For each caller function,
41    #   call each of the callee functions,
42    #   force a return from the callee, and
43    #   make sure that the local variables still have the right values.
44
45    for {set c 1} {$c <= $limit} {incr c} {
46
47        # Set a breakpoint at the next caller function.
48        gdb_test "tbreak caller$c" "Temporary breakpoint.*\[0-9\]*\\."
49
50	# Continue to the next caller function.
51	gdb_test "continue" " caller$c prologue .*" "run to caller$c"
52
53	# Do each callee function.
54        for {set e 1} {$e <= $limit} {incr e} {
55
56            gdb_test "tbreak callee$e" "Temporary breakpoint.*\[0-9\]*\\." \
57		"caller$c calls callee$e; tbreak callee"
58
59  	    gdb_test "continue" " callee$e prologue .*/" \
60		"caller$c calls callee$e; continue to callee"
61
62            # Do a forced return from the callee.
63	    set test "caller$c calls callee$e; return callee now"
64
65	    gdb_test "return 0" \
66		" caller$c .*" \
67		"$test" \
68		"Make .* return now.*y or n. $" \
69		"y"
70
71            # Check that the values of the local variables are what
72	    # they should be.
73            for {set var 1} {$var <= $c} {incr var} {
74                set expected [expr 0x7eeb + $var]
75		set test "caller$c calls callee$e; return restored l$var to $expected"
76		set pass_pattern " = $expected"
77		set unsupported_pattern " = <optimized out>"
78	        gdb_test_multiple "print l$var" $test {
79		    -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
80			pass $test
81		    }
82		    -re "\[\r\n\]*(?:$unsupported_pattern)\[\r\n\]+$gdb_prompt $" {
83			unsupported $test
84		    }
85		}
86            }
87        }
88    }
89
90    gdb_continue_to_end "" continue 1
91}
92
93set prev_timeout $timeout
94set timeout 30
95restore_tests
96set timeout $prev_timeout
97
98# Test PR cli/23785
99clean_restart $binfile
100if { ![runto_main] } {
101    return -1
102}
103gdb_test "restore non-existent-file binary" \
104    "Failed to open non-existent-file: .*"
105