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