]>
Commit | Line | Data |
---|---|---|
213516ef | 1 | # Copyright 2000-2023 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 | # | |
81dc3ab5 TV |
44 | # gdb_target_cmd_ext |
45 | # Send gdb the "target" command. Returns 0 on success, 1 on failure, 2 on | |
46 | # unsupported. | |
e5a1a79a AH |
47 | # If specified, then ADDITIONAL_TEXT must match the text that comes after |
48 | # the connection message in order for the procedure to succeed. | |
6f8eac0e | 49 | # |
81dc3ab5 | 50 | proc gdb_target_cmd_ext { targetname serialport {additional_text ""} } { |
8e94bb3e | 51 | global gdb_prompt gdb_target_remote_cmd_msg |
6f8eac0e | 52 | |
ef783a7d | 53 | set serialport_re [string_to_regexp $serialport] |
6f8eac0e DJ |
54 | for {set i 1} {$i <= 3} {incr i} { |
55 | send_gdb "target $targetname $serialport\n" | |
56 | gdb_expect 60 { | |
57 | -re "A program is being debugged already.*ill it.*y or n. $" { | |
58 | send_gdb "y\n" | |
59 | exp_continue | |
60 | } | |
401ea829 JB |
61 | -re "unknown host.*$gdb_prompt" { |
62 | verbose "Couldn't look up $serialport" | |
63 | } | |
2f83a18e | 64 | -re "Couldn't establish connection to remote.*$gdb_prompt $" { |
6f8eac0e | 65 | verbose "Connection failed" |
cd211c75 LM |
66 | } |
67 | -re "Cannot assign requested address.*$gdb_prompt $" { | |
68 | verbose "Could not assign requested address" | |
6f8eac0e | 69 | } |
1dadb1dd PA |
70 | -re "Non-stop mode requested, but remote does not support non-stop.*$gdb_prompt $" { |
71 | verbose "remote does not support non-stop" | |
72 | return 1 | |
73 | } | |
e5a1a79a | 74 | -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" { |
6f8eac0e | 75 | verbose "Set target to $targetname" |
8e94bb3e | 76 | set gdb_target_remote_cmd_msg $expect_out(buffer) |
6f8eac0e DJ |
77 | return 0 |
78 | } | |
e5a1a79a | 79 | -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" { |
6f8eac0e | 80 | verbose "Set target to $targetname" |
8e94bb3e | 81 | set gdb_target_remote_cmd_msg $expect_out(buffer) |
6f8eac0e DJ |
82 | return 0 |
83 | } | |
e5a1a79a | 84 | -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" { |
e0f9f062 | 85 | verbose "Set target to $targetname" |
8e94bb3e | 86 | set gdb_target_remote_cmd_msg $expect_out(buffer) |
e0f9f062 DE |
87 | return 0 |
88 | } | |
e5a1a79a | 89 | -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" { |
6f8eac0e | 90 | verbose "Set target to $targetname" |
8e94bb3e | 91 | set gdb_target_remote_cmd_msg $expect_out(buffer) |
6f8eac0e DJ |
92 | return 0 |
93 | } | |
e5a1a79a | 94 | -re "Connected to.*$additional_text.*$gdb_prompt $" { |
6f8eac0e | 95 | verbose "Set target to $targetname" |
8e94bb3e | 96 | set gdb_target_remote_cmd_msg $expect_out(buffer) |
6f8eac0e DJ |
97 | return 0 |
98 | } | |
2f83a18e DJ |
99 | -re "Ending remote.*$gdb_prompt $" { } |
100 | -re "Connection refused.*$gdb_prompt $" { | |
6f8eac0e DJ |
101 | verbose "Connection refused by remote target. Pausing, and trying again." |
102 | sleep 30 | |
103 | continue | |
104 | } | |
2f83a18e | 105 | -re "Timeout reading from remote system.*$gdb_prompt $" { |
6f8eac0e DJ |
106 | verbose "Got timeout error from gdb." |
107 | } | |
b741e217 DJ |
108 | -notransfer -re "Remote debugging using .*\r\n> $" { |
109 | # We got an unexpected prompt while creating the target. | |
110 | # Leave it there for the test to diagnose. | |
111 | return 1 | |
112 | } | |
81dc3ab5 TV |
113 | -re ": Network is unreachable.\r\n.*$gdb_prompt $" { |
114 | return 2 | |
115 | } | |
6f8eac0e DJ |
116 | timeout { |
117 | send_gdb "\ 3" | |
118 | break | |
119 | } | |
120 | } | |
121 | } | |
122 | return 1 | |
123 | } | |
124 | ||
81dc3ab5 TV |
125 | # Like gdb_target_cmd_ext, but returns 0 on success, 1 on failure. |
126 | ||
8a625130 TV |
127 | proc gdb_target_cmd { args } { |
128 | set res [eval gdb_target_cmd_ext $args] | |
81dc3ab5 TV |
129 | return [expr $res == 0 ? 0 : 1] |
130 | } | |
6f8eac0e DJ |
131 | |
132 | global portnum | |
133 | set portnum "2345" | |
134 | ||
135 | # Locate the gdbserver binary. Returns "" if gdbserver could not be found. | |
136 | ||
137 | proc find_gdbserver { } { | |
138 | global GDB | |
6fadd07a DE |
139 | global GDBSERVER |
140 | ||
141 | if [info exists GDBSERVER] { | |
142 | return ${GDBSERVER} | |
143 | } | |
6f8eac0e DJ |
144 | |
145 | if [target_info exists gdb_server_prog] { | |
146 | return [target_info gdb_server_prog] | |
147 | } | |
148 | ||
f251f505 TT |
149 | set toplevel [file join [file dirname $GDB] .. gdbserver] |
150 | foreach gdbserver [list "${GDB}server" $toplevel] { | |
151 | if { [file isdirectory $gdbserver] } { | |
152 | append gdbserver "/gdbserver" | |
153 | } | |
154 | ||
155 | if { [file executable $gdbserver] } { | |
156 | return $gdbserver | |
157 | } | |
6f8eac0e DJ |
158 | } |
159 | ||
160 | return "" | |
161 | } | |
162 | ||
163 | # Return non-zero if we should skip gdbserver-specific tests. | |
164 | ||
165 | proc skip_gdbserver_tests { } { | |
166 | if { [find_gdbserver] == "" } { | |
167 | return 1 | |
168 | } | |
169 | ||
bae62ee2 YQ |
170 | # If GDB is lack of XML support, and targets, like arm, have |
171 | # multiple target descriptions, GDB doesn't know which target | |
172 | # description GDBserver uses, and may fail to parse 'g' packet | |
173 | # after connection. | |
174 | if { [gdb_skip_xml_test] | |
175 | && ([istarget "arm*-*-linux*"] | |
08d237dd | 176 | || [istarget "aarch64*-*-linux*"] |
bae62ee2 YQ |
177 | || [istarget "mips*-*-linux*"] |
178 | || [istarget "powerpc*-*-linux*"] | |
179 | || [istarget "s390*-*-linux*"] | |
180 | || [istarget "x86_64-*-linux*"] | |
181 | || [istarget "i\[34567\]86-*-linux*"]) } { | |
182 | return 1 | |
183 | } | |
184 | ||
6f8eac0e DJ |
185 | return 0 |
186 | } | |
187 | ||
2d717e4f DJ |
188 | # Download the currently loaded program to the target if necessary. |
189 | # Return the target system filename. | |
6496a609 DE |
190 | # NOTE: This was named "gdbserver_download", but that collides with the |
191 | # dejagnu "download" API function when using load_generic_config "gdbserver". | |
6f8eac0e | 192 | |
6496a609 | 193 | proc gdbserver_download_current_prog { } { |
b741e217 DJ |
194 | global gdbserver_host_exec |
195 | global gdbserver_host_mtime | |
196 | global gdbserver_server_exec | |
197 | global last_loaded_file | |
198 | ||
096be756 PA |
199 | if { ![info exists last_loaded_file] } { |
200 | return "" | |
201 | } | |
202 | ||
b741e217 DJ |
203 | set host_exec $last_loaded_file |
204 | ||
205 | # If we already downloaded a file to the target, see if we can reuse it. | |
206 | set reuse 0 | |
207 | if { [info exists gdbserver_server_exec] } { | |
208 | set reuse 1 | |
209 | ||
210 | # If the file has changed, we can not. | |
211 | if { $host_exec != $gdbserver_host_exec } { | |
212 | set reuse 0 | |
213 | } | |
214 | ||
215 | # If the mtime has changed, we can not. | |
216 | if { [file mtime $host_exec] != $gdbserver_host_mtime } { | |
217 | set reuse 0 | |
218 | } | |
219 | } | |
220 | ||
221 | if { $reuse == 0 } { | |
222 | set gdbserver_host_exec $host_exec | |
223 | set gdbserver_host_mtime [file mtime $host_exec] | |
7817ea46 | 224 | set gdbserver_server_exec [gdb_remote_download target $host_exec] |
b741e217 | 225 | } |
6f8eac0e | 226 | |
2d717e4f DJ |
227 | return $gdbserver_server_exec |
228 | } | |
229 | ||
e0f9f062 DE |
230 | # Default routine to compute the argument to "target remote". |
231 | ||
232 | proc gdbserver_default_get_remote_address { host port } { | |
ab19de87 DE |
233 | # Historically HOST included the trailing ":". |
234 | # To avoid breaking any board files out there we leave things alone. | |
e0f9f062 DE |
235 | return "$host$port" |
236 | } | |
237 | ||
ab19de87 DE |
238 | # Default routine to compute the "comm" argument for gdbserver. |
239 | ||
240 | proc gdbserver_default_get_comm_port { port } { | |
c7ab0aef | 241 | return "$port" |
ab19de87 DE |
242 | } |
243 | ||
2d717e4f DJ |
244 | # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS. |
245 | # The port will be filled in between them automatically. | |
246 | # | |
247 | # Returns the target protocol and socket to connect to. | |
248 | ||
249 | proc gdbserver_start { options arguments } { | |
250 | global portnum | |
c7ab0aef | 251 | global GDB_TEST_SOCKETHOST |
2d717e4f | 252 | |
6f8eac0e DJ |
253 | # Port id -- either specified in baseboard file, or managed here. |
254 | if [target_info exists gdb,socketport] { | |
255 | set portnum [target_info gdb,socketport] | |
256 | } else { | |
257 | # Bump the port number to avoid conflicts with hung ports. | |
258 | incr portnum | |
259 | } | |
260 | ||
261 | # Extract the local and remote host ids from the target board struct. | |
c7ab0aef SDJ |
262 | if { [info exists GDB_TEST_SOCKETHOST] } { |
263 | # The user is not supposed to provide a port number, just a | |
264 | # hostname/address, therefore we add the trailing ":" here. | |
265 | set debughost "${GDB_TEST_SOCKETHOST}:" | |
266 | # Escape open and close square brackets. | |
267 | set debughost_tmp [string map { [ \\[ ] \\] } $debughost] | |
268 | # We need a "gdbserver" version of the debughost, which will | |
269 | # have the possible connection prefix stripped. This is | |
270 | # because gdbserver currently doesn't recognize the prefixes. | |
271 | regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver | |
272 | } elseif [target_info exists sockethost] { | |
2d717e4f | 273 | set debughost [target_info sockethost] |
c7ab0aef | 274 | set debughost_gdbserver $debughost |
6f8eac0e DJ |
275 | } else { |
276 | set debughost "localhost:" | |
c7ab0aef | 277 | set debughost_gdbserver $debughost |
6f8eac0e DJ |
278 | } |
279 | ||
e0f9f062 DE |
280 | # Some boards use a different value for the port that is passed to |
281 | # gdbserver and the port that is passed to the "target remote" command. | |
282 | # One example is the stdio gdbserver support. | |
283 | if [target_info exists gdb,get_remote_address] { | |
284 | set get_remote_address [target_info gdb,get_remote_address] | |
285 | } else { | |
286 | set get_remote_address gdbserver_default_get_remote_address | |
287 | } | |
ab19de87 DE |
288 | if [target_info exists gdbserver,get_comm_port] { |
289 | set get_comm_port [target_info gdbserver,get_comm_port] | |
290 | } else { | |
291 | set get_comm_port gdbserver_default_get_comm_port | |
292 | } | |
e0f9f062 | 293 | |
6f8eac0e DJ |
294 | # Extract the protocol |
295 | if [target_info exists gdb_protocol] { | |
296 | set protocol [target_info gdb_protocol] | |
297 | } else { | |
298 | set protocol "remote" | |
299 | } | |
300 | ||
301 | set gdbserver [find_gdbserver] | |
302 | ||
047427a8 JK |
303 | # Loop till we find a free port. |
304 | while 1 { | |
047427a8 JK |
305 | # Fire off the debug agent. |
306 | set gdbserver_command "$gdbserver" | |
03f2bd59 JK |
307 | |
308 | # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be | |
309 | # set to true already during gdbserver_start. | |
310 | global gdbserver_reconnect_p | |
dd06d4d6 AH |
311 | global srcdir |
312 | global subdir | |
03f2bd59 JK |
313 | if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} { |
314 | # GDB client could accidentally connect to a stale server. | |
315 | append gdbserver_command " --once" | |
316 | } | |
317 | ||
dd06d4d6 AH |
318 | # Enable debug if set. |
319 | if [gdbserver_debug_enabled] { | |
320 | global gdbserverdebug | |
b420b89e AH |
321 | set enabled 0 |
322 | foreach entry [split $gdbserverdebug ,] { | |
323 | switch -- $entry { | |
324 | "debug" { | |
325 | append gdbserver_command " --debug" | |
326 | set enabled 1 | |
327 | } | |
328 | "remote" { | |
329 | append gdbserver_command " --remote-debug" | |
330 | set enabled 1 | |
331 | } | |
332 | } | |
333 | } | |
334 | # Ensure debugfile is only added if something has been enabled | |
335 | if { $enabled } { | |
336 | set debugfile [standard_output_file gdbserver.debug] | |
337 | append gdbserver_command " --debug-file=$debugfile" | |
dd06d4d6 AH |
338 | } |
339 | } | |
340 | ||
047427a8 JK |
341 | if { $options != "" } { |
342 | append gdbserver_command " $options" | |
343 | } | |
c7ab0aef SDJ |
344 | if { $debughost_gdbserver != "" } { |
345 | append gdbserver_command " $debughost_gdbserver" | |
346 | } | |
e0f9f062 | 347 | if { $portnum != "" } { |
c7ab0aef SDJ |
348 | if { $debughost_gdbserver == "" } { |
349 | append gdbserver_command " " | |
350 | } | |
351 | append gdbserver_command "[$get_comm_port $portnum]" | |
e0f9f062 | 352 | } |
047427a8 JK |
353 | if { $arguments != "" } { |
354 | append gdbserver_command " $arguments" | |
355 | } | |
6f8eac0e | 356 | |
408e9b8b AH |
357 | gdbserver_write_cmd_file $gdbserver_command |
358 | ||
6423214f | 359 | global server_spawn_id |
047427a8 JK |
360 | set server_spawn_id [remote_spawn target $gdbserver_command] |
361 | ||
f71c18e7 PA |
362 | # GDBserver doesn't do inferior I/O through GDB. But we can |
363 | # talk to the program using GDBserver's tty instead. | |
364 | global inferior_spawn_id | |
365 | set inferior_spawn_id $server_spawn_id | |
366 | ||
047427a8 JK |
367 | # Wait for the server to open its TCP socket, so that GDB can connect. |
368 | expect { | |
369 | -i $server_spawn_id | |
2bdd10b7 | 370 | -timeout 120 |
047427a8 JK |
371 | -notransfer |
372 | -re "Listening on" { } | |
01abb042 | 373 | -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" { |
047427a8 JK |
374 | verbose -log "Port $portnum is already in use." |
375 | if ![target_info exists gdb,socketport] { | |
376 | # Bump the port number to avoid the conflict. | |
377 | wait -i $expect_out(spawn_id) | |
378 | incr portnum | |
379 | continue | |
380 | } | |
381 | } | |
fb66cde8 | 382 | -re ".*: cannot resolve name: .*\r\n" { |
c7ab0aef SDJ |
383 | error "gdbserver cannot resolve name." |
384 | } | |
09635af7 MR |
385 | timeout { |
386 | error "Timeout waiting for gdbserver response." | |
387 | } | |
047427a8 JK |
388 | } |
389 | break | |
6f8eac0e DJ |
390 | } |
391 | ||
e0f9f062 | 392 | return [list $protocol [$get_remote_address $debughost $portnum]] |
6f8eac0e DJ |
393 | } |
394 | ||
2d717e4f DJ |
395 | # Start a gdbserver process running SERVER_EXEC, and connect GDB |
396 | # to it. CHILD_ARGS are passed to the inferior. | |
397 | # | |
398 | # Returns the target protocol and socket to connect to. | |
399 | ||
400 | proc gdbserver_spawn { child_args } { | |
6496a609 | 401 | set target_exec [gdbserver_download_current_prog] |
2d717e4f DJ |
402 | |
403 | # Fire off the debug agent. This flavour of gdbserver takes as | |
404 | # arguments the port information, the name of the executable file to | |
405 | # be debugged, and any arguments. | |
406 | set arguments "$target_exec" | |
407 | if { $child_args != "" } { | |
408 | append arguments " $child_args" | |
409 | } | |
410 | return [gdbserver_start "" $arguments] | |
411 | } | |
412 | ||
6423214f PA |
413 | # Close the GDBserver connection. |
414 | ||
415 | proc close_gdbserver {} { | |
038d4868 | 416 | global server_spawn_id |
6423214f PA |
417 | |
418 | # We can't just call close, because if gdbserver is local then that means | |
419 | # that it will get a SIGHUP. Doing it this way could also allow us to | |
420 | # get at the inferior's input or output if necessary, and means that we | |
421 | # don't need to redirect output. | |
422 | ||
423 | if {![info exists server_spawn_id]} { | |
424 | return | |
425 | } | |
426 | ||
427 | verbose "Quitting GDBserver" | |
428 | ||
429 | catch "close -i $server_spawn_id" | |
e0d6d274 KB |
430 | |
431 | # If gdbserver misbehaves, and ignores the close, waiting for it | |
432 | # without the -nowait flag will cause testing to hang. Passing | |
433 | # -nowait makes expect tell Tcl to wait for the process in the | |
434 | # background. | |
435 | catch "wait -nowait -i $server_spawn_id" | |
6423214f PA |
436 | unset server_spawn_id |
437 | } | |
438 | ||
f73842fc YQ |
439 | # Hook into GDB exit, and close GDBserver. We must load this |
440 | # explicitly here, and rename the procedures we want to override. | |
441 | load_lib mi-support.exp | |
6423214f | 442 | |
9c211fd8 | 443 | if { [info procs gdbserver_orig_gdb_exit] == "" } { |
6423214f | 444 | rename gdb_exit gdbserver_orig_gdb_exit |
f73842fc | 445 | rename mi_gdb_exit gdbserver_orig_mi_gdb_exit |
6423214f | 446 | } |
f73842fc | 447 | |
f32682ea TV |
448 | # Cleanup gdbserver $server_spawn_id |
449 | ||
450 | proc gdbserver_exit { is_mi } { | |
038d4868 | 451 | global gdb_spawn_id server_spawn_id |
6423214f PA |
452 | global gdb_prompt |
453 | ||
454 | if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} { | |
2efe15c4 YQ |
455 | # GDB may be terminated in an expected way or an unexpected way, |
456 | # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset. | |
457 | # Catch the exceptions. | |
458 | catch { | |
f73842fc YQ |
459 | if { $is_mi } { |
460 | set monitor_exit "-interpreter-exec console \"monitor exit\"" | |
461 | } else { | |
462 | set monitor_exit "monitor exit" | |
463 | } | |
464 | send_gdb "$monitor_exit\n"; | |
2efe15c4 YQ |
465 | # We use expect rather than gdb_expect because |
466 | # we want to suppress printing exception messages, otherwise, | |
467 | # remote_expect, invoked by gdb_expect, prints the exceptions. | |
337882d4 | 468 | set have_prompt 0 |
2efe15c4 YQ |
469 | expect { |
470 | -i "$gdb_spawn_id" -re "$gdb_prompt $" { | |
337882d4 TV |
471 | set have_prompt 1 |
472 | if { [info exists server_spawn_id] } { | |
473 | exp_continue | |
474 | } | |
2efe15c4 YQ |
475 | } |
476 | -i "$server_spawn_id" eof { | |
477 | wait -i $expect_out(spawn_id) | |
478 | unset server_spawn_id | |
337882d4 TV |
479 | if { ! $have_prompt } { |
480 | exp_continue | |
481 | } | |
2efe15c4 | 482 | } |
f32682ea TV |
483 | timeout { |
484 | warning "Timed out waiting for EOF in server after $monitor_exit" | |
485 | } | |
6423214f PA |
486 | } |
487 | } | |
488 | } | |
489 | close_gdbserver | |
f32682ea TV |
490 | } |
491 | ||
492 | # Local version of gdb_exit that also cleans up gdbserver $server_spawn_id. | |
493 | ||
494 | proc gdbserver_gdb_exit { is_mi } { | |
495 | global gdb_spawn_id server_spawn_id | |
496 | global gdb_prompt | |
497 | global gdbserver_reconnect_p | |
498 | ||
499 | # Leave GDBserver running if we're exiting GDB in order to | |
500 | # reconnect to the same instance of GDBserver again. | |
501 | if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} { | |
502 | if { $is_mi } { | |
503 | gdbserver_orig_mi_gdb_exit | |
504 | } else { | |
505 | gdbserver_orig_gdb_exit | |
506 | } | |
507 | return | |
508 | } | |
509 | ||
510 | gdbserver_exit $is_mi | |
6423214f | 511 | |
f73842fc YQ |
512 | if { $is_mi } { |
513 | gdbserver_orig_mi_gdb_exit | |
514 | } else { | |
515 | gdbserver_orig_gdb_exit | |
516 | } | |
517 | } | |
518 | ||
519 | proc gdb_exit {} { | |
520 | gdbserver_gdb_exit 0 | |
521 | } | |
522 | ||
523 | proc mi_gdb_exit {} { | |
524 | gdbserver_gdb_exit 1 | |
6423214f PA |
525 | } |
526 | ||
6f8eac0e | 527 | # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS |
09635af7 MR |
528 | # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver |
529 | # failed to start or 1 if we couldn't connect to it. | |
6f8eac0e | 530 | |
b741e217 | 531 | proc gdbserver_run { child_args } { |
b0f4b84b DJ |
532 | global gdbserver_protocol |
533 | global gdbserver_gdbport | |
534 | ||
2226f861 DJ |
535 | # Kill anything running before we try to start gdbserver, in case |
536 | # we are sharing a serial connection. | |
537 | global gdb_prompt | |
ce3ebcaa | 538 | send_gdb "kill\n" optional |
2226f861 DJ |
539 | gdb_expect 120 { |
540 | -re "Kill the program being debugged. .y or n. $" { | |
541 | send_gdb "y\n" | |
542 | verbose "\t\tKilling previous program being debugged" | |
543 | exp_continue | |
544 | } | |
545 | -re "$gdb_prompt $" { | |
546 | # OK. | |
547 | } | |
548 | } | |
549 | ||
09635af7 MR |
550 | if { [catch { gdbserver_spawn $child_args } res] == 1 } { |
551 | perror $res | |
552 | return 2 | |
553 | } | |
b0f4b84b DJ |
554 | set gdbserver_protocol [lindex $res 0] |
555 | set gdbserver_gdbport [lindex $res 1] | |
556 | ||
557 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
558 | } | |
559 | ||
560 | # Reconnect to the previous gdbserver session. | |
561 | ||
562 | proc gdbserver_reconnect { } { | |
563 | global gdbserver_protocol | |
564 | global gdbserver_gdbport | |
6f8eac0e | 565 | |
4ec70201 | 566 | global gdbserver_reconnect_p |
03f2bd59 JK |
567 | if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} { |
568 | error "gdbserver_reconnect_p is not set before gdbserver_reconnect" | |
569 | return 0 | |
570 | } | |
571 | ||
b0f4b84b | 572 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] |
6f8eac0e | 573 | } |
2d717e4f | 574 | |
8cfe207c YQ |
575 | # Start gdbserver in extended mode with OPTIONS and connect to it. Note |
576 | # this frobs $gdbserver_protocol, so should be used only from a board | |
577 | # that usually connects in target remote mode. | |
578 | proc gdbserver_start_extended { {options ""} } { | |
e11ac3a3 JK |
579 | global gdbserver_protocol |
580 | global gdbserver_gdbport | |
581 | global use_gdb_stub | |
582 | ||
8cfe207c YQ |
583 | set gdbserver_options "--multi" |
584 | ||
585 | if { $options != "" } { | |
586 | append gdbserver_options " $options" | |
587 | } | |
588 | ||
589 | if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } { | |
09635af7 MR |
590 | perror $res |
591 | return 2 | |
592 | } | |
a1999d40 PA |
593 | set gdbserver_protocol [lindex $res 0] |
594 | if { [string first "extended-" $gdbserver_protocol] != 0} { | |
595 | set gdbserver_protocol "extended-$gdbserver_protocol" | |
596 | } | |
2d717e4f DJ |
597 | set gdbserver_gdbport [lindex $res 1] |
598 | ||
e11ac3a3 JK |
599 | # Even if the board file is testing with target remote, our caller |
600 | # wants to test against gdbserver in extended-remote mode. Make sure to | |
601 | # disable stub-like techniques. | |
602 | set use_gdb_stub 0 | |
603 | ||
2d717e4f DJ |
604 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] |
605 | } | |
048fe15a PA |
606 | |
607 | # Start and connect to a gdbserver in extended/multi mode. Unlike | |
608 | # gdbserver_start_extended, this does not frob $gdbserver_protocol. | |
609 | ||
610 | proc gdbserver_start_multi { } { | |
611 | global gdbserver_protocol | |
612 | global gdbserver_gdbport | |
613 | ||
09635af7 MR |
614 | if { [catch { gdbserver_start "--multi" "" } res] == 1 } { |
615 | perror $res | |
616 | return 2 | |
617 | } | |
048fe15a PA |
618 | set gdbserver_protocol [lindex $res 0] |
619 | set gdbserver_gdbport [lindex $res 1] | |
620 | ||
621 | return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
622 | } | |
623 | ||
624 | # Start a gdbserver process in multi/extended mode, and have GDB | |
625 | # connect to it (MI version). Return 0 on success, or non-zero on | |
626 | # failure. | |
627 | ||
628 | proc mi_gdbserver_start_multi { } { | |
629 | global gdbserver_protocol | |
630 | global gdbserver_gdbport | |
631 | ||
09635af7 MR |
632 | if { [catch { gdbserver_start "--multi" "" } res] == 1 } { |
633 | perror $res | |
634 | return 2 | |
635 | } | |
048fe15a PA |
636 | set gdbserver_protocol [lindex $res 0] |
637 | set gdbserver_gdbport [lindex $res 1] | |
638 | ||
639 | return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] | |
640 | } | |
dd06d4d6 AH |
641 | |
642 | # Check if debugging is enabled for gdbserver. | |
643 | ||
644 | proc gdbserver_debug_enabled { } { | |
645 | global gdbserverdebug | |
646 | ||
647 | # If not already read, get the debug setting from environment or board setting. | |
648 | if ![info exists gdbserverdebug] { | |
649 | global env | |
650 | if [info exists env(GDBSERVER_DEBUG)] { | |
651 | set gdbserverdebug $env(GDBSERVER_DEBUG) | |
652 | } elseif [target_info exists gdbserver,debug] { | |
653 | set gdbserverdebug [target_info gdbserver,debug] | |
654 | } else { | |
655 | return 0 | |
656 | } | |
657 | } | |
658 | ||
b420b89e AH |
659 | # Expand the all option |
660 | if { $gdbserverdebug == "all" } { | |
661 | set gdbserverdebug "debug,remote,replay" | |
662 | } | |
663 | ||
664 | # Ensure it is not empty. | |
665 | return [expr { $gdbserverdebug != "" }] | |
dd06d4d6 | 666 | } |
408e9b8b AH |
667 | |
668 | # Write the command line used to invocate gdbserver to the cmd file. | |
669 | ||
670 | proc gdbserver_write_cmd_file { cmdline } { | |
671 | set logfile [standard_output_file_with_gdb_instance gdbserver.cmd] | |
672 | set cmd_file [open $logfile w] | |
673 | puts $cmd_file $cmdline | |
674 | catch "close $cmd_file" | |
675 | } | |
b420b89e AH |
676 | |
677 | # Override gdb_debug_init so that we can set replay logging in GDB if required. | |
678 | # Backup the original function so we can call it afterwards | |
679 | ||
680 | rename gdb_debug_init _gdb_debug_init | |
681 | ||
682 | proc gdb_debug_init { } { | |
683 | global gdbserverdebug | |
684 | global gdb_prompt | |
685 | ||
686 | if [gdbserver_debug_enabled] { | |
687 | foreach entry [split $gdbserverdebug ,] { | |
688 | if { $entry == "replay" } { | |
689 | set replayfile [standard_output_file_with_gdb_instance gdbserver.replay] | |
690 | send_gdb "set remotelogfile $replayfile\n" optional | |
691 | gdb_expect 10 { | |
692 | -re "$gdb_prompt $" {} | |
693 | } | |
694 | } | |
695 | } | |
696 | } | |
697 | ||
698 | # Now call the standard debug init function | |
699 | _gdb_debug_init | |
700 | } |