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