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