xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.btrace/tailcall.exp (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1# This testcase is part of GDB, the GNU debugger.
2#
3# Copyright 2013-2023 Free Software Foundation, Inc.
4#
5# Contributed by Intel Corp. <markus.t.metzger@intel.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20if { [skip_btrace_tests] } {
21    unsupported "target does not support record-btrace"
22    return -1
23}
24
25# This test requires the compiler to generate a tail call.  To guarantee that
26# we always get one, we use an assembly source file.
27#
28# We use different assembly sources based on the target architecture.
29#
30# Luckily, they are similar enough that a single test script can handle
31# both.
32set opts {}
33if [info exists COMPILE] {
34    # make check RUNTESTFLAGS="gdb.btrace/tailcall.exp COMPILE=1"
35    standard_testfile tailcall.c
36    lappend opts debug optimize=-O2
37} elseif {[istarget "i?86-*-*"] || [istarget "x86_64-*-*"]} {
38	if {[is_amd64_regs_target]} {
39		standard_testfile x86_64-tailcall.S
40	} else {
41		standard_testfile i686-tailcall.S
42	}
43} else {
44    unsupported "target architecture not supported"
45    return -1
46}
47
48if [prepare_for_testing "failed to prepare" $testfile $srcfile $opts] {
49    return -1
50}
51if ![runto_main] {
52    return -1
53}
54
55# we want to see the full trace for this test
56gdb_test_no_output "set record function-call-history-size 0"
57
58# trace the call to foo
59gdb_test_no_output "record btrace"
60gdb_test "next 2"
61
62# show the flat branch trace
63gdb_test "record function-call-history 1" [multi_line \
64  "1\tmain" \
65  "2\tfoo" \
66  "3\tbar" \
67  "4\tmain" \
68  ] "flat"
69
70# show the branch trace with calls indented
71gdb_test "record function-call-history /c 1" [multi_line \
72  "1\tmain" \
73  "2\t  foo" \
74  "3\t    bar" \
75  "4\tmain" \
76  ] "indented"
77
78# go into bar
79gdb_test "record goto 4" ".*bar \\(\\) at .*tailcall.c:24\r\n.*"
80
81# check the backtrace
82gdb_test "backtrace" [multi_line \
83  "#0.*bar \\(\\) at tailcall.c:24" \
84  "#1.*foo \\(\\) at tailcall.c:29" \
85  "#2.*main \\(\\) at tailcall.c:37" \
86  "Backtrace stopped: not enough registers or memory available to unwind further" \
87  ]
88
89# walk the backtrace
90gdb_test "up" "#1\[^\r\n\]*foo \\(\\) at tailcall.c:29\r\n.*" "up to foo"
91gdb_test "up" "#2\[^\r\n\]*main \\(\\) at tailcall.c:37\r\n.*" "up to main"
92gdb_test "down" "#1\[^\r\n\]*foo \\(\\) at tailcall.c:29\r\n.*" "down to foo"
93
94# test stepping into and out of tailcalls.
95gdb_test "finish" "\[^\r\n\]*main \\(\\) at tailcall.c:38\r\n.*" \
96    "finish.1"
97gdb_test "reverse-step" "\[^\r\n\]*bar \\(\\) at tailcall.c:24\r\n.*" \
98    "reverse-step.1"
99gdb_test "reverse-finish" "\[^\r\n\]*foo \\(\\) at tailcall.c:29\r\n.*" \
100    "reverse-finish.1"
101gdb_test "reverse-step" "\[^\r\n\]*main \\(\\) at tailcall.c:37\r\n.*" \
102    "reverse-step.2"
103gdb_test "next" "\[^\r\n\]*38.*" \
104    "next.1"
105gdb_test "reverse-next" "\[^\r\n\]*main \\(\\) at tailcall.c:37\r\n.*" \
106    "reverse-next.1"
107gdb_test "step" "\[^\r\n\]*foo \\(\\) at tailcall.c:29\r\n.*" \
108    "step.1"
109gdb_test "finish" "\[^\r\n\]*main \\(\\) at tailcall.c:38\r\n.*" \
110    "finish.2"
111gdb_test "reverse-step" "\[^\r\n\]*bar \\(\\) at tailcall.c:24\r\n.*" \
112    "reverse-step.3"
113gdb_test "finish" "\[^\r\n\]*main \\(\\) at tailcall.c:38\r\n.*" \
114    "finish.3"
115