1 # Copyright (C) 2013-2023 Free Software Foundation, Inc.
2 # Contributed by Hui Zhu <hui@codesourcery.com>
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.
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.
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/>.
17 load_lib mi-support.exp
24 lappend_include_file flags $srcdir/lib/unbuffer_output.c
26 if {[build_executable $testfile.exp $testfile $srcfile $flags] == -1} {
27 untested "failed to compile"
31 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
32 set dp_location1 [gdb_get_line_number "set dprintf 1 here"]
34 mi_clean_restart $binfile
38 mi_gdb_test "[incr i]-dprintf-insert" \
39 "$i\\^error,msg=\"-dprintf-insert: Missing <location>\"" "mi insert without location"
41 mi_gdb_test "[incr i]-dprintf-insert foo" \
42 "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert breakpoint without format string"
44 mi_gdb_test "[incr i]-dprintf-insert 29" \
45 "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert second breakpoint without format string"
47 mi_gdb_test "-break-insert main" ".*" "mi insert breakpoint main"
49 mi_gdb_test "-dprintf-insert --function main \"hello\"" \
50 "\\^done,bkpt={.*}" "explicit dprintf at main"
52 mi_gdb_test "-dprintf-insert --source $srcfile --line $dp_location1 \"hello\"" \
53 "\\^done,bkpt={.*}" "explicit breakpoint at $srcfile:$dp_location1"
55 mi_gdb_test "-dprintf-insert --source $srcfile \"hello\"" \
56 "\\^error,msg=\"-dprintf-insert: --source option requires --function, --label, or --line\"" "invalid explicit dprintf"
60 set bps [mi_make_breakpoint -type dprintf -func foo -file ".*mi-dprintf.c" \
61 -fullname ".*mi-dprintf.c"]
62 mi_gdb_test "[incr i]-dprintf-insert foo \"\\\"foobarbazqux\\\" At foo entry\\n\"" \
63 "$i\\^done,$bps" "mi insert dprintf foo"
65 set bps [mi_make_breakpoint -type dprintf -func foo \
66 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
68 mi_gdb_test "[incr i]-dprintf-insert $dp_location1 \"arg=%d, g=%d\\n\" arg g" \
69 "$i\\^done,$bps" "mi insert dprintf dp_location1"
72 lappend bps [mi_make_breakpoint -type dprintf -func foo \
73 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c"]
74 lappend bps [mi_make_breakpoint -type dprintf -func foo \
75 -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
77 mi_gdb_test "[incr i]-break-info" \
78 "$i\\^done,[mi_make_breakpoint_table $bps]" \
81 mi_gdb_test "-break-insert $bp_location1" ".*" "mi insert breakpoint bp_location1"
83 # Helper for mi_continue_dprintf.
85 proc mi_expect_dprintf {args where msg} {
87 global inferior_spawn_id gdb_spawn_id
89 if { $args == "call" || $args == "fprintf" } {
90 set foobarbazqux "\"foobarbazqux\""
91 set sid $inferior_spawn_id
93 set foobarbazqux "\\\\\"foobarbazqux\\\\\""
97 # Don't expect the prompt here to avoid a race with mi_expect_stop
98 # in case $inferior_spawn_id != $gdb_spawn_id. Otherwise, in that
99 # case, we could see the gdb prompt before the inferior output.
102 -re ".*$foobarbazqux $where" {
109 fail "$msg (timeout)"
112 mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "$msg stop"
115 proc mi_continue_dprintf {args} {
116 with_test_prefix $args {
117 set msg "mi 1st dprintf"
119 mi_expect_dprintf $args "At foo entry.*arg=1234, g=1234" $msg
121 set msg "mi 2nd dprintf"
122 mi_send_resuming_command "exec-continue" "$msg continue"
123 mi_expect_dprintf $args "At foo entry.*arg=1235, g=2222" $msg
127 mi_continue_dprintf "gdb"
129 # The "call" style depends on having I/O functions available, so test.
130 set has_stderr_symbol [mi_gdb_is_stderr_available]
132 if ![target_info exists gdb,noinferiorio] {
134 # Now switch styles and rerun; in the absence of redirection the
135 # output should be the same.
137 mi_gdb_test "set dprintf-style call" ".*" "mi set dprintf style to call"
138 mi_continue_dprintf "call"
140 # Some compilers don't add information about stderr,
141 # so skip these tests if needed.
142 if {$::has_stderr_symbol} {
143 mi_gdb_test "set dprintf-function fprintf" ".*" "mi set dprintf function"
144 mi_gdb_test "set dprintf-channel stderr" ".*" "mi set dprintf channel"
145 mi_continue_dprintf "fprintf"
149 set target_can_dprintf 0
150 set msg "set dprintf style to agent"
151 send_gdb "set dprintf-style agent\n"
153 -re "warning: Target cannot run dprintf commands, falling back to GDB printf.*$mi_gdb_prompt$" {
156 -re ".*done.*$mi_gdb_prompt$" {
157 set target_can_dprintf 1
160 -re ".*$mi_gdb_prompt$" {
168 if $target_can_dprintf {
169 if {[mi_run_cmd] < 0} {
170 # This likely means we failed to use target side commands in
171 # combination with software breakpoints. IOW, the target
172 # likely doesn't support target-side software breakpoints.
173 set target_can_dprintf 0
174 unsupported "send dprintf to target"
177 if $target_can_dprintf {
178 mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "mi expect stop"
180 mi_send_resuming_command "exec-continue" "mi 1st dprintf continue, agent"
181 mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 1st dprintf, agent"
183 mi_send_resuming_command "exec-continue" "mi 2nd dprintf continue, agent"
185 # The =breakpoint-modified text is a part of the
186 # "-exec-continue" output.
187 set msg "mi info dprintf second time"
189 -re "=breakpoint-modified," {
192 -re ".*$mi_gdb_prompt$" {
200 mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 2nd dprintf, agent"
204 mi_gdb_test "set dprintf-style foobar" ".*error.*" "mi set dprintf style to an unrecognized type"