Commit | Line | Data |
---|---|---|
8acc9f48 | 1 | # Copyright 2000-2013 Free Software Foundation, Inc. |
6f8eac0e DJ |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
6f8eac0e | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
6f8eac0e DJ |
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. | |
e22f8b7c | 12 | # |
6f8eac0e | 13 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
6f8eac0e DJ |
15 | |
16 | # This file is based on config/gdbserver.exp, which was written by | |
17 | # Michael Snyder (msnyder@redhat.com). | |
18 | ||
19 | # | |
20 | # To be addressed or set in your baseboard config file: | |
21 | # | |
22 | # set_board_info gdb_protocol "remote" | |
23 | # Unles you have a gdbserver that uses a different protocol... | |
e11ac3a3 JK |
24 | # After GDB starts you should check global $gdbserver_protocol instead as |
25 | # the testfile may force a specific different target protocol itself. | |
6f8eac0e DJ |
26 | # |
27 | # set_board_info gdb_server_prog | |
28 | # This will be the path to the gdbserver program you want to test. | |
29 | # Defaults to "gdbserver". | |
30 | # | |
31 | # set_board_info sockethost | |
32 | # The name of the host computer whose socket is being used. | |
33 | # Defaults to "localhost". Note: old gdbserver requires | |
34 | # that you define this, but libremote/gdbserver does not. | |
35 | # | |
36 | # set_board_info gdb,socketport | |
37 | # Port id to use for socket connection. If not set explicitly, | |
38 | # it will start at "2345" and increment for each use. | |
e11ac3a3 JK |
39 | # After GDB starts you should check global $gdbserver_gdbport for the |
40 | # real port used. It is not useful if $gdbserver_reconnect_p was not set. | |
6f8eac0e DJ |
41 | # |
42 | ||
43 | # | |
44 | # gdb_target_cmd | |
45 | # Send gdb the "target" command | |
46 | # | |
47 | proc gdb_target_cmd { targetname serialport } { | |
48 | global gdb_prompt | |
49 | ||
ef783a7d | 50 | set serialport_re [string_to_regexp $serialport] |
6f8eac0e DJ |
51 | for {set i 1} {$i <= 3} {incr i} { |
52 | send_gdb "target $targetname $serialport\n" | |
53 | gdb_expect 60 { | |
54 | -re "A program is being debugged already.*ill it.*y or n. $" { | |
55 | send_gdb "y\n" | |
56 | exp_continue | |
57 | } | |
401ea829 JB |
58 | -re "unknown host.*$gdb_prompt" { |
59 | verbose "Couldn't look up $serialport" | |
60 | } | |
2f83a18e | 61 | -re "Couldn't establish connection to remote.*$gdb_prompt $" { |
6f8eac0e DJ |
62 | verbose "Connection failed" |
63 | } | |
64 | -re "Remote MIPS debugging.*$gdb_prompt" { | |
65 | verbose "Set target to $targetname" | |
66 | return 0 | |
67 | } | |
ef783a7d | 68 | -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" { |
6f8eac0e DJ |
69 | verbose "Set target to $targetname" |
70 | return 0 | |
71 | } | |
e0f9f062 DE |
72 | -re "Remote debugging using stdio.*$gdb_prompt $" { |
73 | verbose "Set target to $targetname" | |
74 | return 0 | |
75 | } | |
2f83a18e | 76 | -re "Remote target $targetname connected to.*$gdb_prompt $" { |
6f8eac0e DJ |
77 | verbose "Set target to $targetname" |
78 | return 0 | |
79 | } | |
2f83a18e | 80 | -re "Connected to.*$gdb_prompt $" { |
6f8eac0e DJ |
81 | verbose "Set target to $targetname" |
82 | return 0 | |
83 | } | |
2f83a18e DJ |
84 | -re "Ending remote.*$gdb_prompt $" { } |
85 | -re "Connection refused.*$gdb_prompt $" { | |
6f8eac0e DJ |
86 | verbose "Connection refused by remote target. Pausing, and trying again." |
87 | sleep 30 | |
88 | continue | |
89 | } | |
2f83a18e | 90 | -re "Timeout reading from remote system.*$gdb_prompt $" { |
6f8eac0e DJ |
91 | verbose "Got timeout error from gdb." |
92 | } | |
b741e217 DJ |
93 | -notransfer -re "Remote debugging using .*\r\n> $" { |
94 | # We got an unexpected prompt while creating the target. | |
95 | # Leave it there for the test to diagnose. | |
96 | return 1 | |
97 | } | |
6f8eac0e DJ |
98 | timeout { |
99 | send_gdb "\ 3" | |
100 | break | |
101 | } | |
102 | } | |
103 | } | |
104 | return 1 | |
105 | } | |
106 | ||
107 | ||
108 | global portnum | |
109 | set portnum "2345" | |
110 | ||
111 | # Locate the gdbserver binary. Returns "" if gdbserver could not be found. | |
112 | ||
113 | proc find_gdbserver { } { | |
114 | global GDB | |
6fadd07a DE |
115 | global GDBSERVER |
116 | ||
117 | if [info exists GDBSERVER] { | |
118 | return ${GDBSERVER} | |
119 | } | |
6f8eac0e DJ |
120 | |
121 | if [target_info exists gdb_server_prog] { | |
122 | return [target_info gdb_server_prog] | |
123 | } | |
124 | ||
125 | set gdbserver "${GDB}server" | |
126 | if { [file isdirectory $gdbserver] } { | |
127 | append gdbserver "/gdbserver" | |
128 | } | |
129 | ||
130 | if { [file executable $gdbserver] } { | |
131 | return $gdbserver | |
132 | } | |
133 | ||
134 | return "" | |
135 | } | |
136 | ||
137 | # Return non-zero if we should skip gdbserver-specific tests. | |
138 | ||
139 | proc skip_gdbserver_tests { } { | |
140 | if { [find_gdbserver] == "" } { | |
141 | return 1 | |
142 | } | |
143 | ||
144 | return 0 | |
145 | } | |
146 | ||
2d717e4f DJ |
147 | # Download the currently loaded program to the target if necessary. |
148 | # Return the target system filename. | |
6496a609 DE |
149 | # NOTE: This was named "gdbserver_download", but that collides with the |
150 | # dejagnu "download" API function when using load_generic_config "gdbserver". | |
6f8eac0e | 151 | |
6496a609 | 152 | proc gdbserver_download_current_prog { } { |
b741e217 DJ |
153 | global gdbserver_host_exec |
154 | global gdbserver_host_mtime | |
155 | global gdbserver_server_exec | |
156 | global last_loaded_file | |
157 | ||
158 | set host_exec $last_loaded_file | |
159 | ||
160 | # If we already downloaded a file to the target, see if we can reuse it. | |
161 | set reuse 0 | |
162 | if { [info exists gdbserver_server_exec] } { | |
163 | set reuse 1 | |
164 | ||
165 | # If the file has changed, we can not. | |
166 | if { $host_exec != $gdbserver_host_exec } { | |
167 | set reuse 0 | |
168 | } | |
169 | ||
170 | # If the mtime has changed, we can not. | |
171 | if { [file mtime $host_exec] != $gdbserver_host_mtime } { | |
172 | set reuse 0 | |
173 | } | |
174 | } | |
175 | ||
176 | if { $reuse == 0 } { | |
177 | set gdbserver_host_exec $host_exec | |
178 | set gdbserver_host_mtime [file mtime $host_exec] | |
179 | if [is_remote target] { | |
93f02886 | 180 | set gdbserver_server_exec [gdb_download $host_exec] |
b741e217 DJ |
181 | } else { |
182 | set gdbserver_server_exec $host_exec | |
183 | } | |
184 | } | |
6f8eac0e | 185 | |
2d717e4f DJ |
186 | return $gdbserver_server_exec |
187 | } | |
188 | ||
e0f9f062 DE |
189 | # Default routine to compute the argument to "target remote". |
190 | ||
191 | proc gdbserver_default_get_remote_address { host port } { | |
192 | return "$host$port" | |
193 | } | |
194 | ||
2d717e4f DJ |
195 | # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS. |
196 | # The port will be filled in between them automatically. | |
197 | # | |
198 | # Returns the target protocol and socket to connect to. | |
199 | ||
200 | proc gdbserver_start { options arguments } { | |
201 | global portnum | |
202 | ||
6f8eac0e DJ |
203 | # Port id -- either specified in baseboard file, or managed here. |
204 | if [target_info exists gdb,socketport] { | |
205 | set portnum [target_info gdb,socketport] | |
206 | } else { | |
207 | # Bump the port number to avoid conflicts with hung ports. | |
208 | incr portnum | |
209 | } | |
210 | ||
211 | # Extract the local and remote host ids from the target board struct. | |
212 | if [target_info exists sockethost] { | |
2d717e4f | 213 | set debughost [target_info sockethost] |
6f8eac0e DJ |
214 | } else { |
215 | set debughost "localhost:" | |
216 | } | |
217 | ||
e0f9f062 DE |
218 | # Some boards use a different value for the port that is passed to |
219 | # gdbserver and the port that is passed to the "target remote" command. | |
220 | # One example is the stdio gdbserver support. | |
221 | if [target_info exists gdb,get_remote_address] { | |
222 | set get_remote_address [target_info gdb,get_remote_address] | |
223 | } else { | |
224 | set get_remote_address gdbserver_default_get_remote_address | |
225 | } | |
226 | ||
6f8eac0e DJ |
227 | # Extract the protocol |
228 | if [target_info exists gdb_protocol] { | |
229 | set protocol [target_info gdb_protocol] | |
230 | } else { | |
231 | set protocol "remote" | |
232 | } | |
233 | ||
234 | set gdbserver [find_gdbserver] | |
235 | ||
047427a8 JK |
236 | # Loop till we find a free port. |
237 | while 1 { | |
047427a8 JK |
238 | # Fire off the debug agent. |
239 | set gdbserver_command "$gdbserver" | |
03f2bd59 JK |
240 | |
241 | # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be | |
242 | # set to true already during gdbserver_start. | |
243 | global gdbserver_reconnect_p | |
244 | if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} { | |
245 | # GDB client could accidentally connect to a stale server. | |
f196051f | 246 | # append gdbserver_command " --debug --once" |
03f2bd59 JK |
247 | append gdbserver_command " --once" |
248 | } | |
249 | ||
047427a8 JK |
250 | if { $options != "" } { |
251 | append gdbserver_command " $options" | |
252 | } | |
e0f9f062 DE |
253 | if { $portnum != "" } { |
254 | append gdbserver_command " :$portnum" | |
255 | } | |
047427a8 JK |
256 | if { $arguments != "" } { |
257 | append gdbserver_command " $arguments" | |
258 | } | |
6f8eac0e | 259 | |
047427a8 JK |
260 | set server_spawn_id [remote_spawn target $gdbserver_command] |
261 | ||
262 | # Wait for the server to open its TCP socket, so that GDB can connect. | |
263 | expect { | |
264 | -i $server_spawn_id | |
265 | -notransfer | |
266 | -re "Listening on" { } | |
267 | -re "Can't bind address: Address already in use\\.\r\n" { | |
268 | verbose -log "Port $portnum is already in use." | |
269 | if ![target_info exists gdb,socketport] { | |
270 | # Bump the port number to avoid the conflict. | |
271 | wait -i $expect_out(spawn_id) | |
272 | incr portnum | |
273 | continue | |
274 | } | |
275 | } | |
276 | } | |
277 | break | |
6f8eac0e DJ |
278 | } |
279 | ||
280 | # We can't just call close, because if gdbserver is local then that means | |
281 | # that it will get a SIGHUP. Doing it this way could also allow us to | |
282 | # get at the inferior's input or output if necessary, and means that we | |
283 | # don't need to redirect output. | |
284 | expect_background { | |
285 | -i $server_spawn_id | |
2226f861 | 286 | full_buffer { } |
6f8eac0e DJ |
287 | eof { |
288 | # The spawn ID is already closed now (but not yet waited for). | |
289 | wait -i $expect_out(spawn_id) | |
290 | } | |
291 | } | |
292 | ||
e0f9f062 | 293 | return [list $protocol [$get_remote_address $debughost $portnum]] |
6f8eac0e DJ |
294 | } |
295 | ||
2d717e4f DJ |
296 | # Start a gdbserver process running SERVER_EXEC, and connect GDB |
297 | # to it. CHILD_ARGS are passed to the inferior. | |
298 | # | |
299 | # Returns the target protocol and socket to connect to. | |
300 | ||
301 | proc gdbserver_spawn { child_args } { | |
6496a609 | 302 | set target_exec [gdbserver_download_current_prog] |
2d717e4f DJ |
303 | |
304 | # Fire off the debug agent. This flavour of gdbserver takes as | |
305 | # arguments the port information, the name of the executable file to | |
306 | # be debugged, and any arguments. | |
307 | set arguments "$target_exec" | |
308 | if { $child_args != "" } { | |
309 | append arguments " $child_args" | |
310 | } | |
311 | return [gdbserver_start "" $arguments] | |
312 | } | |
313 | ||
6f8eac0e DJ |
314 | # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS |
315 | # to it. Return 0 on success, or non-zero on failure. | |
316 | ||
b741e217 | 317 | proc gdbserver_run { child_args } { |
b0f4b84b DJ |
318 | global gdbserver_protocol |
319 | global gdbserver_gdbport | |
320 | ||
2226f861 DJ |
321 | # Kill anything running before we try to start gdbserver, in case |
322 | # we are sharing a serial connection. | |
323 | global gdb_prompt | |
324 | send_gdb "kill\n" | |
325 | gdb_expect 120 { | |
326 | -re "Kill the program being debugged. .y or n. $" { | |
327 | send_gdb "y\n" | |
328 | verbose "\t\tKilling previous program being debugged" | |
329 | exp_continue | |
330 | } | |
331 | -re "$gdb_prompt $" { | |
332 | # OK. | |
333 | } | |
334 | } | |
335 | ||
b741e217 | 336 | set res [gdbserver_spawn $child_args] |
b0f4b84b DJ |
337 | set gdbserver_protocol [lindex $res 0] |
338 | set gdbserver_gdbport [lindex $res 1] | |
339 | ||
340 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
341 | } | |
342 | ||
343 | # Reconnect to the previous gdbserver session. | |
344 | ||
345 | proc gdbserver_reconnect { } { | |
346 | global gdbserver_protocol | |
347 | global gdbserver_gdbport | |
6f8eac0e | 348 | |
4ec70201 | 349 | global gdbserver_reconnect_p |
03f2bd59 JK |
350 | if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} { |
351 | error "gdbserver_reconnect_p is not set before gdbserver_reconnect" | |
352 | return 0 | |
353 | } | |
354 | ||
b0f4b84b | 355 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] |
6f8eac0e | 356 | } |
2d717e4f | 357 | |
048fe15a PA |
358 | # Start and connect to a gdbserver in extended mode. Note this frobs |
359 | # $gdbserver_protocol, so should be used only from a board that | |
360 | # usually connects in target remote mode. | |
2d717e4f | 361 | proc gdbserver_start_extended { } { |
e11ac3a3 JK |
362 | global gdbserver_protocol |
363 | global gdbserver_gdbport | |
364 | global use_gdb_stub | |
365 | ||
2d717e4f | 366 | set res [gdbserver_start "--multi" ""] |
a1999d40 PA |
367 | set gdbserver_protocol [lindex $res 0] |
368 | if { [string first "extended-" $gdbserver_protocol] != 0} { | |
369 | set gdbserver_protocol "extended-$gdbserver_protocol" | |
370 | } | |
2d717e4f DJ |
371 | set gdbserver_gdbport [lindex $res 1] |
372 | ||
e11ac3a3 JK |
373 | # Even if the board file is testing with target remote, our caller |
374 | # wants to test against gdbserver in extended-remote mode. Make sure to | |
375 | # disable stub-like techniques. | |
376 | set use_gdb_stub 0 | |
377 | ||
2d717e4f DJ |
378 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] |
379 | } | |
048fe15a PA |
380 | |
381 | # Start and connect to a gdbserver in extended/multi mode. Unlike | |
382 | # gdbserver_start_extended, this does not frob $gdbserver_protocol. | |
383 | ||
384 | proc gdbserver_start_multi { } { | |
385 | global gdbserver_protocol | |
386 | global gdbserver_gdbport | |
387 | ||
388 | set res [gdbserver_start "--multi" ""] | |
389 | set gdbserver_protocol [lindex $res 0] | |
390 | set gdbserver_gdbport [lindex $res 1] | |
391 | ||
392 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
393 | } | |
394 | ||
395 | # Start a gdbserver process in multi/extended mode, and have GDB | |
396 | # connect to it (MI version). Return 0 on success, or non-zero on | |
397 | # failure. | |
398 | ||
399 | proc mi_gdbserver_start_multi { } { | |
400 | global gdbserver_protocol | |
401 | global gdbserver_gdbport | |
402 | ||
403 | set res [gdbserver_start "--multi" ""] | |
404 | set gdbserver_protocol [lindex $res 0] | |
405 | set gdbserver_gdbport [lindex $res 1] | |
406 | ||
407 | return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
408 | } |