Update copyright year range in all GDB files.
[external/binutils.git] / gdb / testsuite / gdb.mi / mi-dprintf.exp
1 #   Copyright (C) 2013-2019 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 gdb_exit
21 if [mi_gdb_start] {
22     continue
23 }
24
25 standard_testfile
26
27 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
28     untested "failed to compile"
29     return -1
30 }
31
32 mi_delete_breakpoints
33
34 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
35 set dp_location1 [gdb_get_line_number "set dprintf 1 here"]
36
37 mi_run_to_main
38
39 set i 0
40 mi_gdb_test "[incr i]-dprintf-insert" \
41     "$i\\^error,msg=\"-dprintf-insert: Missing <location>\"" "mi insert without location"
42
43 mi_gdb_test "[incr i]-dprintf-insert foo" \
44     "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert breakpoint without format string"
45
46 mi_gdb_test "[incr i]-dprintf-insert 29" \
47     "$i\\^error,msg=\"-dprintf-insert: Missing <format>\"" "mi insert second breakpoint without format string"
48
49 mi_gdb_test "-break-insert main" ".*" "mi insert breakpoint main"
50
51 mi_gdb_test "-dprintf-insert --function main \"hello\"" \
52     "\\^done,bkpt={.*}" "explicit dprintf at main"
53
54 mi_gdb_test "-dprintf-insert --source $srcfile --line $dp_location1 \"hello\"" \
55     "\\^done,bkpt={.*}" "explicit breakpoint at $srcfile:$dp_location1"
56
57 mi_gdb_test "-dprintf-insert --source $srcfile \"hello\"" \
58     "\\^error,msg=\"-dprintf-insert: --source option requires --function, --label, or --line\"" "invalid explicit dprintf"
59
60 mi_delete_breakpoints
61
62 set bps [mi_make_breakpoint -type dprintf -func foo -file ".*mi-dprintf.c" \
63              -fullname ".*mi-dprintf.c"]
64 mi_gdb_test "[incr i]-dprintf-insert foo \"\\\"foobarbazqux\\\" At foo entry\\n\"" \
65     "$i\\^done,$bps" "mi insert dprintf foo"
66
67 set bps [mi_make_breakpoint -type dprintf -func foo \
68                    -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
69                    -line $dp_location1]
70 mi_gdb_test "[incr i]-dprintf-insert $dp_location1 \"arg=%d, g=%d\\n\" arg g" \
71                    "$i\\^done,$bps" "mi insert dprintf dp_location1"
72
73 set bps {}
74 lappend bps [mi_make_breakpoint -type dprintf -func foo \
75                  -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c"]
76 lappend bps [mi_make_breakpoint -type dprintf -func foo \
77                  -file ".*mi-dprintf.c" -fullname ".*mi-dprintf.c" \
78                  -line $dp_location1]
79 mi_gdb_test "[incr i]-break-info" \
80     "$i\\^done,[mi_make_breakpoint_table $bps]" \
81     "mi info dprintf"
82
83 mi_gdb_test "-break-insert $bp_location1" ".*" "mi insert breakpoint bp_location1"
84
85 # Helper for mi_continue_dprintf.
86
87 proc mi_expect_dprintf {args where msg} {
88     global mi_gdb_prompt
89     global inferior_spawn_id gdb_spawn_id
90
91     if { $args == "call" || $args == "fprintf" } {
92         set foobarbazqux "\"foobarbazqux\""
93         set sid $inferior_spawn_id
94     } else {
95         set foobarbazqux "\\\\\"foobarbazqux\\\\\""
96         set sid $gdb_spawn_id
97     }
98
99     # Don't expect the prompt here to avoid a race with mi_expect_stop
100     # in case $inferior_spawn_id != $gdb_spawn_id.  Otherwise, in that
101     # case, we could see the gdb prompt before the inferior output.
102     gdb_expect {
103         -i $sid
104         -re ".*$foobarbazqux $where" {
105             pass $msg
106         }
107         eof {
108             fail "$msg (eof)"
109         }
110         timeout {
111             fail "$msg (timeout)"
112         }
113     }
114     mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "$msg stop"
115 }
116
117 proc mi_continue_dprintf {args} {
118     with_test_prefix $args {
119         set msg "mi 1st dprintf"
120         mi_run_cmd
121         mi_expect_dprintf $args "At foo entry.*arg=1234, g=1234" $msg
122
123         set msg "mi 2nd dprintf"
124         mi_send_resuming_command "exec-continue" "$msg continue"
125         mi_expect_dprintf $args "At foo entry.*arg=1235, g=2222" $msg
126     }
127 }
128
129 mi_continue_dprintf "gdb"
130
131 # The "call" style depends on having I/O functions available, so test.
132
133 if ![target_info exists gdb,noinferiorio] {
134
135     # Now switch styles and rerun; in the absence of redirection the
136     # output should be the same.
137
138     mi_gdb_test "set dprintf-style call" ".*" "mi set dprintf style to call"
139     mi_continue_dprintf "call"
140
141     mi_gdb_test "set dprintf-function fprintf" ".*" "mi set dprintf-channel stderr"
142     mi_gdb_test "set dprintf-channel stderr" ".*" "mi set dprintf channel"
143     mi_continue_dprintf "fprintf"
144 }
145
146 set target_can_dprintf 0
147 set msg "set dprintf style to agent"
148 send_gdb "set dprintf-style agent\n"
149 gdb_expect {
150     -re "warning: Target cannot run dprintf commands, falling back to GDB printf.*$mi_gdb_prompt$" {
151         unsupported "$msg"
152     }
153     -re ".*done.*$mi_gdb_prompt$" {
154         set target_can_dprintf 1
155         pass "$msg"
156     }
157     -re ".*$mi_gdb_prompt$" {
158         fail "$msg"
159     }
160     timeout {
161         fail "$msg"
162     }
163 }
164
165 if $target_can_dprintf {
166     if {[mi_run_cmd] < 0} {
167         # This likely means we failed to use target side commands in
168         # combination with software breakpoints.  IOW, the target
169         # likely doesn't support target-side software breakpoints.
170         set target_can_dprintf 0
171         unsupported "send dprintf to target"
172     }
173
174     if $target_can_dprintf {
175         mi_expect_stop ".*" ".*" ".*" ".*" ".*" "" "mi expect stop"
176
177         mi_send_resuming_command "exec-continue" "mi 1st dprintf continue, agent"
178         mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 1st dprintf, agent"
179
180         mi_send_resuming_command "exec-continue" "mi 2nd dprintf continue, agent"
181
182         # The =breakpoint-modified text is a part of the
183         # "-exec-continue" output.
184         set msg "mi info dprintf second time"
185         gdb_expect {
186             -re "=breakpoint-modified," {
187                 pass $msg
188             }
189             -re ".*$mi_gdb_prompt$" {
190                 fail "$msg"
191             }
192             timeout {
193                 fail "$msg"
194             }
195         }
196
197         mi_expect_stop ".*" "foo" ".*" ".*" ".*" "" "mi 2nd dprintf, agent"
198     }
199 }
200
201 mi_gdb_test "set dprintf-style foobar" ".*error.*" "mi set dprintf style to an unrecognized type"