1# Copyright 2013-2024 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
16load_lib trace-support.exp
17
18standard_testfile actions.c
19
20require gdb_trace_common_supports_arch
21
22if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {debug}] } {
23    return -1
24}
25
26if ![runto_main] {
27    return -1
28}
29
30if ![gdb_target_supports_trace] {
31    unsupported "current target does not support trace"
32    return -1
33}
34
35gdb_exit
36
37load_lib mi-support.exp
38set MIFLAGS "-i=mi"
39
40mi_clean_restart $binfile
41mi_runto_main
42
43mi_gdb_test "-break-insert end" \
44    "\\^done,bkpt=\{number=\"${decimal}\",type=\"breakpoint\".*\"\}" \
45    "insert tracepoint on end"
46mi_gdb_test "-break-insert -a gdb_recursion_test" \
47    "\\^done,bkpt=\{number=\"${decimal}\",type=\"tracepoint\".*\"\}" \
48    "insert tracepoint on gdb_recursion_test"
49
50mi_gdb_test "-trace-define-variable \$tsv 1" {.*\^done} \
51    "-trace-define-variable"
52
53mi_gdb_test "-break-commands 3 \"collect gdb_char_test\" \"collect gdb_union1_test\" \"collect gdb_struct1_test.l\" \"collect gdb_arr_test\[0\]\" \"collect $${pcreg}\" \"teval \$tsv += 1\" \"collect \$tsv\" \"end\" " \
54    {\^done} "set action"
55
56mi_gdb_test "-break-insert -a gdb_c_test" \
57    "\\^done,bkpt=\{number=\"${decimal}\",type=\"tracepoint\".*\"\}" \
58    "insert tracepoint on gdb_c_test"
59
60# Define an action.
61# Collect a global variable to be sure no registers are collected
62# except PC.
63mi_gdb_test "-break-commands 4 \"collect gdb_char_test\" \"end\" " \
64    {\^done} "set action on tracepoint 4"
65
66mi_gdb_test "-trace-start" {.*\^done} "trace start"
67mi_send_resuming_command "exec-continue" "continuing to end"
68mi_expect_stop \
69    "breakpoint-hit" "end" ".*" ".*" ".*" {"" "disp=\"keep\""} \
70    "stop at end"
71mi_gdb_test "-trace-stop" {.*\^done,stop-reason=.*} "trace stop"
72
73# Save trace frames to tfile.
74set tracefile [standard_output_file ${testfile}]
75mi_gdb_test "-trace-save ${tracefile}.tf" ".*\\^done" \
76    "save tfile trace file"
77# Save trace frames to ctf.
78mi_gdb_test "-trace-save -ctf ${tracefile}.ctf" ".*\\^done" \
79    "save ctf trace file"
80
81# Test the MI command '-trace-frame-collected' with DATA_SOURCE as
82# trace data source.
83
84proc test_trace_frame_collected { data_source } {
85    global decimal hex
86    global mi_gdb_prompt
87
88    with_test_prefix "$data_source" {
89	mi_gdb_test "-trace-find frame-number 0" \
90	    ".*\\^done,found=\"1\",tracepoint=\"${decimal}\",traceframe=\"0\",frame=\{.*" \
91	    "-trace-find frame-number 0"
92
93	set reg_pattern "\{number=\"$decimal\",value=\"$hex\"\}"
94
95	# Test MI command '-trace-frame-collected' dumps only
96	# collected registers.
97
98	# While the tracepoint has no explicit action that causes
99	# collection of registers other than the PC, some
100	# architectures manage to collect or guess more than that.
101	if { [istarget "s390*-*-*"] } {
102	    # On s390 PC is a pseudo-register - collecting it also
103	    # collects the underlying PSWA register.
104	    if { "$data_source" != "tfile" } {
105		set reg_pattern "$reg_pattern,$reg_pattern"
106	    } else {
107		# For tfile, PSWM and CC are also guessed.
108		set reg_pattern "$reg_pattern,$reg_pattern,$reg_pattern,$reg_pattern"
109	    }
110	} elseif {[is_amd64_regs_target] && [is_ilp32_target]} {
111	    # x32.  While on the 64-bit ABI gdb only exposes 64-bit
112	    # $pc/$rip, on x32, GDB exposes 32-bit $eip as well, as a
113	    # pseudo-register.  Thus, collecting $pc/$rip
114	    # automatically always collects $eip as well.
115	    set reg_pattern "$reg_pattern,$reg_pattern"
116	}
117
118	mi_gdb_test "-trace-frame-collected" \
119	    "\\^done,explicit-variables=\\\[\{name=\"gdb_char_test\",value=\".*\"\}\\\],computed-expressions=\\\[\\\],registers=\\\[$reg_pattern\\\],tvars=\\\[\\\],memory=\\\[\{address=\"$hex\",length=\"1\"\}\\\]"\
120	    "-trace-frame-collected (register)"
121
122	mi_gdb_test "-trace-find frame-number 1" \
123	    ".*\\^done,found=\"1\",tracepoint=\"${decimal}\",traceframe=\"1\",frame=\{.*" \
124	    "-trace-find frame-number 1"
125
126	# Test MI command '-trace-frame-collected'
127	mi_gdb_test "-trace-frame-collected" \
128	    "\\^done,explicit-variables=\\\[\{name=\"gdb_char_test\",value=\".*\"\},\{name=\"gdb_union1_test\",value=\".*\"\}\\\],computed-expressions=\\\[\{name=\"gdb_struct1_test\.l\",.*\},\{name=\"gdb_arr_test\\\[0\\\]\",.*\}\\\],registers=\\\[.*\\\],tvars=\\\[\{name=\"\\\$tsv\",current=\"2\"\}\\\],memory=\\\[\{address=\"$hex\",length=\"1\"\},.*\\\]"\
129	    "-trace-frame-collected"
130	mi_gdb_test "-trace-frame-collected --var-print-values 2 --comp-print-values --simple-values --registers-format x --memory-contents" \
131	    "\\^done,explicit-variables=\\\[\{name=\"gdb_char_test\",type=\"char\",value=\".*\"\},\{name=\"gdb_union1_test\",type=\"union GDB_UNION_TEST\"\}\\\],computed-expressions=\\\[.*\\\],registers=\\\[.*\\\],tvars=\\\[\{name=\"\\\$tsv\",current=\"2\"\}\\\],memory=\\\[\{address=\"$hex\",length=\"1\",contents=\".*\"\},.*\\\]" \
132	    "-trace-frame-collected --var-print-values 2 --comp-print-values --simple-values --registers-format x --memory-contents"
133
134	mi_gdb_test "-trace-find none" ".*\\^done,found=\"0\".*" \
135	    "-trace-find none"
136    }
137}
138
139test_trace_frame_collected "live"
140
141# Change target to ctf if GDB supports it.
142set msg "-target-select ctf"
143send_gdb "-target-select ctf ${tracefile}.ctf\n"
144gdb_expect {
145    -re ".*\\^connected.*${mi_gdb_prompt}$" {
146	# GDB supports ctf, do the test.
147	test_trace_frame_collected "ctf"
148    }
149    -re ".*\\^error,msg=\"Undefined target command.*${mi_gdb_prompt}$" {
150	# GDB doesn't support ctf, skip the test.
151	unsupported "gdb does not support ctf target"
152    }
153    -re ".*$mi_gdb_prompt$" {
154	fail "$msg"
155    }
156    timeout {
157	fail "$msg (timeout)"
158    }
159}
160
161# Change target to tfile.
162mi_gdb_test "-target-select tfile ${tracefile}.tf" ".*\\^connected.*" \
163    "-target-select tfile"
164test_trace_frame_collected "tfile"
165
166mi_gdb_exit
167