]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/dprintf.exp
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / dprintf.exp
1 # Copyright (C) 2012-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
16 standard_testfile
17
18 set flags {}
19 lappend flags debug
20 lappend_include_file flags $srcdir/lib/unbuffer_output.c
21
22 if { [prepare_for_testing "failed to prepare" $testfile $srcfile $flags] } {
23 return -1
24 }
25
26 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
27 set dp_location1 [gdb_get_line_number "set dprintf 1 here"]
28
29 if ![runto_main] {
30 return -1
31 }
32
33 gdb_test "dprintf" "Format string required"
34
35 gdb_test "dprintf foo" "Format string required"
36
37 gdb_test "dprintf 29" "Format string required"
38
39 delete_breakpoints
40
41 gdb_breakpoint "main"
42
43 gdb_test "dprintf foo,\"At foo entry\\n\"" \
44 "Dprintf .*"
45
46 gdb_test "ignore \$bpnum 1" ".*Will ignore next crossing of breakpoint.*"
47
48 gdb_test "dprintf $dp_location1,\"arg=%d, g=%d\\n\", arg, g" \
49 "Dprintf .*"
50
51 gdb_test_sequence "info breakpoints" "dprintf info 1" {
52 "\[\r\n\]Num Type Disp Enb Address +What"
53 "\[\r\n\]2 breakpoint"
54 "\[\r\n\]3 dprintf"
55 "\[\r\n\] printf \"At foo entry\\\\n\""
56 "\[\r\n\]4 dprintf"
57 "\[\r\n\] printf \"arg=%d, g=%d\\\\n\", arg, g"
58 }
59
60 gdb_test "break $bp_location1" \
61 "Breakpoint .*"
62
63 gdb_run_cmd
64
65 gdb_test "" "Breakpoint"
66
67 gdb_test "continue" "arg=1234, g=1234.*" "1st dprintf, gdb"
68
69 gdb_test "continue" "At foo entry.*arg=1235, g=2222.*" "2nd dprintf, gdb"
70
71 # Restart GDB and set set up for testing.
72
73 proc restart {} {
74 global binfile
75 global bp_location1 dp_location1
76
77 clean_restart $binfile
78
79 if ![runto_main] {
80 return -1
81 }
82
83 gdb_test "dprintf foo,\"At foo entry\\n\"" \
84 "Dprintf .*"
85
86 gdb_test "dprintf $dp_location1,\"arg=%d, g=%d\\n\", arg, g" \
87 "Dprintf .*"
88
89 gdb_test "break $bp_location1" "Breakpoint .*"
90 }
91
92 # Test continuing across a dprintf. PATTERN matches the output
93 # generated by the dprintf. MSG is used as test message.
94
95 proc test_dprintf {pattern msg} {
96 gdb_test_stdio "continue" $pattern "" "$msg"
97 }
98
99 # Test the "call" style.
100
101 proc test_call {} {
102 global binfile
103 global bp_location1
104
105 # Now switch styles and rerun; in the absence of redirection the
106 # output should be the same.
107
108 with_test_prefix "printf" {
109 restart
110
111 gdb_test_no_output "set dprintf-style call" "set dprintf style to call"
112
113 test_dprintf "At foo entry.*arg=1234, g=1234\r\n" "1st dprintf"
114
115 test_dprintf "At foo entry.*arg=1235, g=2222\r\n" "2nd dprintf"
116 }
117
118 gdb_test_multiple "print stderr" "stderr symbol check" {
119 -re "\\'stderr\\' has unknown type.*" {
120 untested "No information available for stderr, exiting early"
121 return
122 }
123 -re "\\\$1.*" {
124 pass $gdb_test_name
125 }
126 }
127
128 with_test_prefix "fprintf" {
129 restart
130
131 gdb_test_no_output "set dprintf-function fprintf" "set dprintf function"
132 gdb_test_no_output "set dprintf-channel stderr" "set dprintf channel"
133
134 gdb_test_no_output "set dprintf-style call" "set dprintf style to call"
135
136 test_dprintf "At foo entry.*arg=1234, g=1234\r\n" "1st dprintf"
137
138 test_dprintf "At foo entry.*arg=1235, g=2222\r\n" "2nd dprintf"
139 }
140 }
141
142 # The "call" style depends on having I/O functions available.
143
144 if ![target_info exists gdb,noinferiorio] {
145 with_test_prefix "call" {
146 test_call
147 }
148 }
149
150 # Test the "agent" style.
151
152 proc test_agent {} {
153 global binfile
154 global gdb_prompt
155
156 restart
157
158 set target_can_dprintf 1
159 set msg "set dprintf style to agent"
160 gdb_test_multiple "set dprintf-style agent" $msg {
161 -re "warning: Target cannot run dprintf commands.*\r\n$gdb_prompt $" {
162
163 # The target reports that it doesn't support target side
164 # commands at all.
165 set target_can_dprintf 0
166 unsupported "$msg"
167 }
168 -re ".*$gdb_prompt $" {
169 pass "$msg"
170 }
171 }
172
173 if !$target_can_dprintf {
174 return
175 }
176
177 # Even if the target reports that it does support target side
178 # commands, we can only tell that it supports them in combination
179 # with a particular breakpoint type (Z0, Z1, etc.) when we try to
180 # insert the breakpoint. When "set breakpoint always-inserted is
181 # off", that'll be on next continue.
182 set msg "1st dprintf"
183 gdb_test_multiple "continue" $msg {
184 -re "Warning:.*Target doesn't support breakpoints that have target side commands.*\r\n$gdb_prompt $" {
185 set target_can_dprintf 0
186 unsupported "$msg"
187 }
188 -re "Breakpoint \[0-9\]+, foo .*$gdb_prompt $" {
189 pass "$msg"
190 }
191 }
192
193 if $target_can_dprintf {
194 gdb_test "continue" "Breakpoint \[0-9\]+, foo .*" "2nd dprintf"
195
196 gdb_test_sequence "info breakpoints" "dprintf info" {
197 "\[\r\n\]Num Type Disp Enb Address +What"
198 "\[\r\n\]1 breakpoint"
199 "\[\r\n\]\tbreakpoint already hit 1 time"
200 "\[\r\n\]2 dprintf"
201 "\[\r\n\] agent-printf \"At foo entry\\\\n\""
202 "\[\r\n\]3 dprintf"
203 "\[\r\n\] agent-printf \"arg=%d, g=%d\\\\n\", arg, g"
204 "\[\r\n\]4 breakpoint"
205 "\[\r\n\]\tbreakpoint already hit 2 times"
206 }
207 }
208 }
209
210 with_test_prefix "agent" {
211 test_agent
212 }
213
214 gdb_test "set dprintf-style foobar" "Undefined item: \"foobar\"." \
215 "Set dprintf style to an unrecognized type"
216
217 # Test that force-disabling the BreakpointCommands RSP feature works
218 # as expected. dprintf relies on support for target-side breakpoint
219 # commands --- use it as proxy.
220 if [gdb_protocol_is_remote] {
221 gdb_test \
222 "set remote breakpoint-commands-packet off" \
223 "Support for the 'BreakpointCommands' packet on the current remote target is set to \"off\"."
224
225 gdb_test "set dprintf-style agent" \
226 "warning: Target cannot run dprintf commands.*" \
227 "set dprintf-style agent, with feature disabled"
228 }