1# Copyright 2013-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/>.
15load_lib dwarf.exp
16
17# This test can only be run on targets which support DWARF-2 and use gas.
18if {![dwarf2_support]} {
19    return 0
20}
21
22standard_testfile .c entry-values-dw.S
23
24if  {[gdb_compile ${srcdir}/${subdir}/${srcfile} ${binfile}1.o \
25	  object {nodebug}] != ""} {
26    return -1
27}
28
29if  {[gdb_compile [list ${binfile}1.o] \
30	  "${binfile}1" executable {}] != ""} {
31    return -1
32}
33
34# Start GDB and load executable file, compute the offset of the
35# instruction in bar returned from foo.  It is needed in the Dwarf
36# Assembler.
37
38gdb_exit
39gdb_start
40gdb_reinitialize_dir $srcdir/$subdir
41gdb_load ${binfile}1
42
43set returned_from_foo ""
44
45if { [istarget "arm*-*-*"] || [istarget "aarch64*-*-*"] } {
46    set call_insn "bl"
47} elseif { [istarget "s390*-*-*"] } {
48    set call_insn "brasl"
49} elseif { [istarget "powerpc*-*-*"] } {
50    set call_insn "bl"
51} elseif { [istarget "mips*-*-*"] } {
52    # Skip the delay slot after the instruction used to make a call
53    # (which can be a jump or a branch) if it has one.
54    #
55    #  JUMP (or BRANCH) foo
56    #  insn1
57    #  insn2
58    #
59    # Most MIPS instructions used to make calls have a delay slot.
60    # These include JAL, JALS, JALX, JALR, JALRS, BAL and BALS.
61    # In this case the program continues from `insn2' when `foo'
62    # returns.  The only exception is JALRC, in which case execution
63    # resumes from `insn1' instead.
64    set call_insn {jalrc|[jb]al[sxr]*[ \t][^\r\n]+\r\n}
65} else {
66    set call_insn "call"
67}
68
69# Calculate the offset of the instruction in bar returned from foo.
70set test "disassemble bar"
71gdb_test_multiple $test $test {
72    -re ".*$hex <\\+$decimal>:\[ \t\]+\\y$call_insn\\y\[^\r\n\]+\r\n\[ \]+$hex <\\+($decimal)>:.*$gdb_prompt $" {
73	set returned_from_foo $expect_out(1,string)
74    }
75    -re ".*$gdb_prompt $" {
76	fail $test
77    }
78}
79
80if { [string equal $returned_from_foo ""] } {
81    fail "find the call or branch instruction offset in bar"
82    # The following test makes no sense if the offset is unknown.  We need
83    # to update the pattern above to match call or branch instruction for
84    # the target architecture.
85    return -1
86}
87
88gdb_exit
89
90# Make some DWARF for the test.
91set asm_file [standard_output_file $srcfile2]
92Dwarf::assemble $asm_file {
93    declare_labels int_label foo_label
94    global returned_from_foo
95    global srcdir subdir srcfile
96
97    set bar_result [function_range bar ${srcdir}/${subdir}/${srcfile}]
98    set bar_start [lindex $bar_result 0]
99    set bar_length [lindex $bar_result 1]
100
101    cu {} {
102	compile_unit {{language @DW_LANG_C}} {
103	    int_label: base_type {
104		{name int}
105		{encoding @DW_ATE_signed}
106		{byte_size 4 DW_FORM_sdata}
107	    }
108
109	    foo_label: subprogram {
110		{decl_file 0 udata}
111		{MACRO_AT_func { foo }}
112	    } {
113		formal_parameter {
114		    {type :$int_label}
115		    {name i}
116		    {location {DW_OP_reg0} SPECIAL_expr}
117		}
118		formal_parameter {
119		    {type :$int_label}
120		    {name j}
121		    {location {DW_OP_reg1} SPECIAL_expr}
122		}
123	    }
124
125	    subprogram {
126		{name bar}
127		{decl_file 0 udata}
128		{low_pc $bar_start addr}
129		{high_pc "$bar_start + $bar_length" addr}
130		{GNU_all_call_sites 1 sdata}
131	    } {
132		formal_parameter {
133		    {type :$int_label}
134		    {name i}
135		}
136
137		GNU_call_site {
138		    {low_pc "$bar_start + $returned_from_foo" addr}
139		    {abstract_origin :$foo_label}
140		} {
141		    # Faked entry values are reference to variables 'global1'
142		    # and 'global2' and faked locations are register 0 and
143		    # register 1.
144		    GNU_call_site_parameter {
145			{location {DW_OP_reg0} SPECIAL_expr}
146			{GNU_call_site_value {
147			    addr global1
148			    deref_size 4
149			} SPECIAL_expr}
150		    }
151		    GNU_call_site_parameter {
152			{location {DW_OP_reg1} SPECIAL_expr}
153			{GNU_call_site_value {
154			    addr global2
155			    deref_size 4
156			} SPECIAL_expr}
157		    }
158		}
159	    }
160	}
161    }
162}
163
164if  {[gdb_compile $asm_file ${binfile}2.o object {nodebug}] != ""} {
165    return -1
166}
167
168if  {[gdb_compile [list ${binfile}1.o ${binfile}2.o] \
169	  "${binfile}" executable {}] != ""} {
170    return -1
171}
172
173clean_restart ${testfile}
174
175if ![runto_main] {
176    return -1
177}
178
179gdb_breakpoint "foo"
180
181gdb_continue_to_breakpoint "foo"
182
183gdb_test_no_output "set print entry-values both"
184
185gdb_test_sequence "bt" "bt (1)" {
186    "\[\r\n\]#0 .* foo \\(i=[-]?[0-9]+, i@entry=2, j=[-]?[0-9]+, j@entry=3\\)"
187    "\[\r\n\]#1 .* bar \\(i=<optimized out>, i@entry=<optimized out>\\)"
188    "\[\r\n\]#2 .* \.?main \\(\\)"
189}
190
191# Update global variables 'global1' and 'global2' and test that the
192# entry values are updated too.
193
194gdb_test_no_output "set var *(int *) &global1=10"
195gdb_test_no_output "set var *(int *) &global2=11"
196
197gdb_test_sequence "bt" "bt (2)" {
198    "\[\r\n\]#0 .* foo \\(i=[-]?[0-9]+, i@entry=10, j=[-]?[0-9]+, j@entry=11\\)"
199    "\[\r\n\]#1 .* bar \\(i=<optimized out>, i@entry=<optimized out>\\)"
200    "\[\r\n\]#2 .* \.?main \\(\\)"
201}
202
203# Restart GDB and trace.
204
205clean_restart $binfile
206
207load_lib "trace-support.exp"
208
209if ![runto_main] {
210    return -1
211}
212
213if ![gdb_target_supports_trace] {
214    unsupported "target does not support trace"
215    return -1
216}
217
218gdb_test "trace foo" "Tracepoint $decimal at .*"
219
220# Collect arguments i and j.  Collect 'global1' which is entry value
221# of argument i.  Don't collect 'global2' to test the entry value of
222# argument j.
223
224gdb_trace_setactions "set action for tracepoint 1" "" \
225    "collect i, j, (int) global1, \(\*\(void \*\*\) \(\$$spreg\)\) @ 128" "^$"
226
227gdb_test_no_output "tstart"
228
229gdb_breakpoint "end"
230gdb_continue_to_breakpoint "end"
231
232gdb_test_no_output "tstop"
233
234gdb_test "tfind" "Found trace frame 0, .*" "tfind start"
235
236# Since 'global2' is not collected, j@entry is expected to be 'unavailable'.
237gdb_test "bt 1" "#0 .* foo \\(i=\[-\]?$decimal, i@entry=2, j=\[-\]?$decimal, j@entry=<unavailable>\\).*"
238
239# Test that unavailable "j@entry" is not shown when command option
240# --skip-unavailable is used.
241gdb_test "interpreter-exec mi \"-stack-list-arguments --skip-unavailable --simple-values\"" \
242    "\r\n\\^done,stack-args=\\\[frame={level=\"0\",args=\\\[{name=\"i\",type=\"int\",value=\".*\"},{name=\"i@entry\",type=\"int\",value=\"2\"},{name=\"j\",type=\"int\",value=\".*\"}\\\]},frame=.*\\\].*"
243
244gdb_test "tfind" "Target failed to find requested trace frame\..*"
245