]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.mi/mi-dprintf.exp
6254787f2124d48721c5ae5ae08320504fd96b32
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.mi / mi-dprintf.exp
1 # Copyright (C) 2013-2020 Free Software Foundation, Inc.
2 # Contributed by Hui Zhu <hui@codesourcery.com>
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 load_lib mi-support.exp
18 set MIFLAGS "-i=mi"
19
20 standard_testfile
21
22 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
23 untested "failed to compile"
24 return -1
25 }
26
27 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
28 set dp_location1 [gdb_get_line_number "set dprintf 1 here"]
29
30 mi_clean_restart $binfile
31 mi_runto_main
32
33 set i 0
34 mi_gdb_test "[incr i]-dprintf-insert" \
35 "$i\\^error,msg=\"-dprintf-insert: Missing <location>\"" "mi insert without location"
36
37 mi_gdb_test "[incr i]-dprintf-insert foo" \
38 "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert breakpoint without format string"
39
40 mi_gdb_test "[incr i]-dprintf-insert 29" \
41 "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert second breakpoint without format string"
42
43 mi_gdb_test "-break-insert main" ".*" "mi insert breakpoint main"
44
45 mi_gdb_test "-dprintf-insert --function main \"hello\"" \
46 "\\^done,bkpt={.*}" "explicit dprintf at main"
47
48 mi_gdb_test "-dprintf-insert --source $srcfile --line $dp_location1 \"hello\"" \
49 "\\^done,bkpt={.*}" "explicit breakpoint at $srcfile:$dp_location1"
50
51 mi_gdb_test "-dprintf-insert --source $srcfile \"hello\"" \
52 "\\^error,msg=\"-dprintf-insert: --source option requires --function, --label, or --line\"" "invalid explicit dprintf"
53
54 mi_delete_breakpoints
55
56 set bps [mi_make_breakpoint -type dprintf -func foo -file ".*mi-dprintf.c" \
57 -fullname ".*mi-dprintf.c"]
58 mi_gdb_test "[incr i]-dprintf-insert foo \"\\\"foobarbazqux\\\" At foo entry\\n\"" \
59 "$i\\^done,$bps" "mi insert dprintf foo"
60
61 set bps [mi_make_breakpoint -type dprintf -func foo \
62 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
63 -line $dp_location1]
64 mi_gdb_test "[incr i]-dprintf-insert $dp_location1 \"arg=%d, g=%d\\n\" arg g" \
65 "$i\\^done,$bps" "mi insert dprintf dp_location1"
66
67 set bps {}
68 lappend bps [mi_make_breakpoint -type dprintf -func foo \
69 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c"]
70 lappend bps [mi_make_breakpoint -type dprintf -func foo \
71 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
72 -line $dp_location1]
73 mi_gdb_test "[incr i]-break-info" \
74 "$i\\^done,[mi_make_breakpoint_table $bps]" \
75 "mi info dprintf"
76
77 mi_gdb_test "-break-insert $bp_location1" ".*" "mi insert breakpoint bp_location1"
78
79 # Helper for mi_continue_dprintf.
80
81 proc mi_expect_dprintf {args where msg} {
82 global mi_gdb_prompt
83 global inferior_spawn_id gdb_spawn_id
84
85 if { $args == "call" || $args == "fprintf" } {
86 set foobarbazqux "\"foobarbazqux\""
87 set sid $inferior_spawn_id
88 } else {
89 set foobarbazqux "\\\\\"foobarbazqux\\\\\""
90 set sid $gdb_spawn_id
91 }
92
93 # Don't expect the prompt here to avoid a race with mi_expect_stop
94 # in case $inferior_spawn_id != $gdb_spawn_id. Otherwise, in that
95 # case, we could see the gdb prompt before the inferior output.
96 gdb_expect {
97 -i $sid
98 -re ".*$foobarbazqux $where" {
99 pass $msg
100 }
101 eof {
102 fail "$msg (eof)"
103 }
104 timeout {
105 fail "$msg (timeout)"
106 }
107 }
108 mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "$msg stop"
109 }
110
111 proc mi_continue_dprintf {args} {
112 with_test_prefix $args {
113 set msg "mi 1st dprintf"
114 mi_run_cmd
115 mi_expect_dprintf $args "At foo entry.*arg=1234, g=1234" $msg
116
117 set msg "mi 2nd dprintf"
118 mi_send_resuming_command "exec-continue" "$msg continue"
119 mi_expect_dprintf $args "At foo entry.*arg=1235, g=2222" $msg
120 }
121 }
122
123 mi_continue_dprintf "gdb"
124
125 # The "call" style depends on having I/O functions available, so test.
126
127 if ![target_info exists gdb,noinferiorio] {
128
129 # Now switch styles and rerun; in the absence of redirection the
130 # output should be the same.
131
132 mi_gdb_test "set dprintf-style call" ".*" "mi set dprintf style to call"
133 mi_continue_dprintf "call"
134
135 mi_gdb_test "set dprintf-function fprintf" ".*" "mi set dprintf-channel stderr"
136 mi_gdb_test "set dprintf-channel stderr" ".*" "mi set dprintf channel"
137 mi_continue_dprintf "fprintf"
138 }
139
140 set target_can_dprintf 0
141 set msg "set dprintf style to agent"
142 send_gdb "set dprintf-style agent\n"
143 gdb_expect {
144 -re "warning: Target cannot run dprintf commands, falling back to GDB printf.*$mi_gdb_prompt$" {
145 unsupported "$msg"
146 }
147 -re ".*done.*$mi_gdb_prompt$" {
148 set target_can_dprintf 1
149 pass "$msg"
150 }
151 -re ".*$mi_gdb_prompt$" {
152 fail "$msg"
153 }
154 timeout {
155 fail "$msg"
156 }
157 }
158
159 if $target_can_dprintf {
160 if {[mi_run_cmd] < 0} {
161 # This likely means we failed to use target side commands in
162 # combination with software breakpoints. IOW, the target
163 # likely doesn't support target-side software breakpoints.
164 set target_can_dprintf 0
165 unsupported "send dprintf to target"
166 }
167
168 if $target_can_dprintf {
169 mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "mi expect stop"
170
171 mi_send_resuming_command "exec-continue" "mi 1st dprintf continue, agent"
172 mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 1st dprintf, agent"
173
174 mi_send_resuming_command "exec-continue" "mi 2nd dprintf continue, agent"
175
176 # The =breakpoint-modified text is a part of the
177 # "-exec-continue" output.
178 set msg "mi info dprintf second time"
179 gdb_expect {
180 -re "=breakpoint-modified," {
181 pass $msg
182 }
183 -re ".*$mi_gdb_prompt$" {
184 fail "$msg"
185 }
186 timeout {
187 fail "$msg"
188 }
189 }
190
191 mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 2nd dprintf, agent"
192 }
193 }
194
195 mi_gdb_test "set dprintf-style foobar" ".*error.*" "mi set dprintf style to an unrecognized type"