]>
Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
0a325463 | 2 | Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
b543979c | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
b543979c JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
b543979c | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
b543979c | 17 | along with this program; if not, write to the Free Software |
998cfe7d | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 RP |
19 | |
20 | /* Remote communication protocol. | |
e50ebec8 JK |
21 | |
22 | A debug packet whose contents are <data> | |
23 | is encapsulated for transmission in the form: | |
24 | ||
25 | $ <data> # CSUM1 CSUM2 | |
26 | ||
27 | <data> must be ASCII alphanumeric and cannot include characters | |
0c993550 JK |
28 | '$' or '#'. If <data> starts with two characters followed by |
29 | ':', then the existing stubs interpret this as a sequence number. | |
e50ebec8 JK |
30 | |
31 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
32 | checksum of <data>, the most significant nibble is sent first. | |
33 | the hex digits 0-9,a-f are used. | |
34 | ||
35 | Receiver responds with: | |
36 | ||
37 | + - if CSUM is correct and ready for next packet | |
38 | - - if CSUM is incorrect | |
39 | ||
40 | <data> is as follows: | |
4cc1b3f7 JK |
41 | Most values are encoded in ascii hex digits. Signal numbers are according |
42 | to the numbering in target.h. | |
bd5635a1 RP |
43 | |
44 | Request Packet | |
45 | ||
4cc1b3f7 JK |
46 | set thread Hct... Set thread for subsequent operations. |
47 | c = 'c' for thread used in step and | |
48 | continue; t... can be -1 for all | |
49 | threads. | |
50 | c = 'g' for thread used in other | |
51 | operations. If zero, pick a thread, | |
52 | any thread. | |
53 | reply OK for success | |
54 | ENN for an error. | |
55 | ||
bd5635a1 RP |
56 | read registers g |
57 | reply XX....X Each byte of register data | |
58 | is described by two hex digits. | |
59 | Registers are in the internal order | |
60 | for GDB, and the bytes in a register | |
61 | are in the same order the machine uses. | |
62 | or ENN for an error. | |
63 | ||
64 | write regs GXX..XX Each byte of register data | |
65 | is described by two hex digits. | |
66 | reply OK for success | |
67 | ENN for an error | |
68 | ||
0c993550 | 69 | write reg Pn...=r... Write register n... with value r..., |
4aa6fe10 JK |
70 | which contains two hex digits for each |
71 | byte in the register (target byte | |
72 | order). | |
73 | reply OK for success | |
74 | ENN for an error | |
75 | (not supported by all stubs). | |
76 | ||
bd5635a1 RP |
77 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
78 | reply XX..XX XX..XX is mem contents | |
d538b510 RP |
79 | Can be fewer bytes than requested |
80 | if able to read only part of the data. | |
bd5635a1 RP |
81 | or ENN NN is errno |
82 | ||
83 | write mem MAA..AA,LLLL:XX..XX | |
84 | AA..AA is address, | |
85 | LLLL is number of bytes, | |
86 | XX..XX is data | |
87 | reply OK for success | |
d538b510 RP |
88 | ENN for an error (this includes the case |
89 | where only part of the data was | |
90 | written). | |
bd5635a1 | 91 | |
4cc1b3f7 | 92 | continue cAA..AA AA..AA is address to resume |
bd5635a1 RP |
93 | If AA..AA is omitted, |
94 | resume at same address. | |
95 | ||
96 | step sAA..AA AA..AA is address to resume | |
97 | If AA..AA is omitted, | |
98 | resume at same address. | |
99 | ||
0a325463 SG |
100 | continue with Csig;AA..AA Continue with signal sig (hex signal |
101 | signal number). If ;AA..AA is omitted, resume | |
102 | at same address. | |
4cc1b3f7 | 103 | |
0a325463 | 104 | step with Ssig;AA..AA Like 'C' but step not continue. |
4cc1b3f7 JK |
105 | signal |
106 | ||
bd5635a1 RP |
107 | last signal ? Reply the current reason for stopping. |
108 | This is the same reply as is generated | |
109 | for step or cont : SAA where AA is the | |
110 | signal number. | |
111 | ||
b52cac6b FF |
112 | detach D Reply OK. |
113 | ||
bd5635a1 RP |
114 | There is no immediate reply to step or cont. |
115 | The reply comes when the machine stops. | |
4cc1b3f7 | 116 | It is SAA AA is the signal number. |
bd5635a1 | 117 | |
4cc1b3f7 | 118 | or... TAAn...:r...;n...:r...;n...:r...; |
e50ebec8 | 119 | AA = signal number |
4cc1b3f7 JK |
120 | n... = register number (hex) |
121 | r... = register contents | |
122 | n... = `thread' | |
123 | r... = thread process ID. This is | |
124 | a hex integer. | |
125 | n... = other string not starting | |
126 | with valid hex digit. | |
127 | gdb should ignore this n,r pair | |
128 | and go on to the next. This way | |
129 | we can extend the protocol. | |
72bba93b | 130 | or... WAA The process exited, and AA is |
758aeb93 ILT |
131 | the exit status. This is only |
132 | applicable for certains sorts of | |
133 | targets. | |
4cc1b3f7 JK |
134 | or... XAA The process terminated with signal |
135 | AA. | |
998cfe7d SC |
136 | or... OXX..XX XX..XX is hex encoding of ASCII data. This |
137 | can happen at any time while the program is | |
4cc1b3f7 JK |
138 | running and the debugger should |
139 | continue to wait for 'W', 'T', etc. | |
140 | ||
2b576293 C |
141 | thread alive TXX Find out if the thread XX is alive. |
142 | reply OK thread is still alive | |
143 | ENN thread is dead | |
144 | ||
145 | remote restart RXX Restart the remote server | |
146 | ||
147 | extended ops ! Use the extended remote protocol. | |
148 | Sticky -- only needs to be set once. | |
149 | ||
d538b510 RP |
150 | kill request k |
151 | ||
152 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
153 | reset r reset -- see sparc stub. | |
154 | reserved <other> On other requests, the stub should | |
155 | ignore the request and send an empty | |
156 | response ($#<checksum>). This way | |
157 | we can extend the protocol and GDB | |
158 | can tell whether the stub it is | |
159 | talking to uses the old or the new. | |
72bba93b | 160 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
161 | AA for a match with pattern PP and |
162 | mask MM. PP and MM are 4 bytes. | |
163 | Not supported by all stubs. | |
164 | ||
72bba93b SG |
165 | general query qXXXX Request info about XXXX. |
166 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
167 | query sect offs qOffsets Get section offsets. Reply is | |
168 | Text=xxx;Data=yyy;Bss=zzz | |
72bba93b | 169 | |
94d4b713 | 170 | Responses can be run-length encoded to save space. A '*' means that |
284f4ee9 | 171 | the next character is an ASCII encoding giving a repeat count which |
94d4b713 | 172 | stands for that many repititions of the character preceding the '*'. |
284f4ee9 SC |
173 | The encoding is n+29, yielding a printable character where n >=3 |
174 | (which is where rle starts to win). Don't use an n > 126. | |
175 | ||
176 | So | |
177 | "0* " means the same as "0000". */ | |
bd5635a1 | 178 | |
d747e0af | 179 | #include "defs.h" |
2b576293 | 180 | #include "gdb_string.h" |
bd5635a1 | 181 | #include <fcntl.h> |
bd5635a1 RP |
182 | #include "frame.h" |
183 | #include "inferior.h" | |
e50ebec8 | 184 | #include "bfd.h" |
6b27ebe8 | 185 | #include "symfile.h" |
bd5635a1 RP |
186 | #include "target.h" |
187 | #include "wait.h" | |
0d14c7df | 188 | /*#include "terminal.h"*/ |
8f86a4e4 | 189 | #include "gdbcmd.h" |
758aeb93 ILT |
190 | #include "objfiles.h" |
191 | #include "gdb-stabs.h" | |
cb1709ae | 192 | #include "gdbthread.h" |
bd5635a1 | 193 | |
d538b510 RP |
194 | #include "dcache.h" |
195 | ||
bd5635a1 RP |
196 | #ifdef USG |
197 | #include <sys/types.h> | |
198 | #endif | |
199 | ||
200 | #include <signal.h> | |
ebdb9ade | 201 | #include "serial.h" |
bd5635a1 | 202 | |
b543979c JG |
203 | /* Prototypes for local functions */ |
204 | ||
45993f61 | 205 | static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 206 | char *myaddr, int len)); |
b543979c | 207 | |
45993f61 | 208 | static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 209 | char *myaddr, int len)); |
b543979c | 210 | |
45993f61 | 211 | static void remote_files_info PARAMS ((struct target_ops *ignore)); |
b543979c | 212 | |
45993f61 SC |
213 | static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, |
214 | int len, int should_write, | |
215 | struct target_ops *target)); | |
b543979c | 216 | |
45993f61 | 217 | static void remote_prepare_to_store PARAMS ((void)); |
b543979c | 218 | |
45993f61 | 219 | static void remote_fetch_registers PARAMS ((int regno)); |
b543979c | 220 | |
45993f61 SC |
221 | static void remote_resume PARAMS ((int pid, int step, |
222 | enum target_signal siggnal)); | |
b543979c | 223 | |
45993f61 | 224 | static int remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 225 | |
45993f61 | 226 | static void remote_open PARAMS ((char *name, int from_tty)); |
b543979c | 227 | |
2b576293 C |
228 | static void extended_remote_open PARAMS ((char *name, int from_tty)); |
229 | ||
dd0ce8f6 | 230 | static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p)); |
2b576293 | 231 | |
45993f61 | 232 | static void remote_close PARAMS ((int quitting)); |
b543979c | 233 | |
45993f61 | 234 | static void remote_store_registers PARAMS ((int regno)); |
b543979c | 235 | |
2b576293 C |
236 | static void remote_mourn PARAMS ((void)); |
237 | ||
238 | static void extended_remote_restart PARAMS ((void)); | |
239 | ||
240 | static void extended_remote_mourn PARAMS ((void)); | |
241 | ||
242 | static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); | |
243 | ||
244 | static void remote_mourn_1 PARAMS ((struct target_ops *)); | |
245 | ||
45993f61 | 246 | static void remote_send PARAMS ((char *buf)); |
b543979c | 247 | |
45993f61 | 248 | static int readchar PARAMS ((int timeout)); |
b543979c | 249 | |
94d4b713 | 250 | static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); |
b543979c | 251 | |
45993f61 | 252 | static void remote_kill PARAMS ((void)); |
b543979c | 253 | |
45993f61 | 254 | static int tohex PARAMS ((int nib)); |
b543979c | 255 | |
45993f61 | 256 | static void remote_detach PARAMS ((char *args, int from_tty)); |
5af4f5f6 | 257 | |
45993f61 | 258 | static void remote_interrupt PARAMS ((int signo)); |
b543979c | 259 | |
45993f61 SC |
260 | static void remote_interrupt_twice PARAMS ((int signo)); |
261 | ||
262 | static void interrupt_query PARAMS ((void)); | |
981a3309 | 263 | |
b607efe7 FF |
264 | static void set_thread PARAMS ((int, int)); |
265 | ||
266 | static int remote_thread_alive PARAMS ((int)); | |
267 | ||
268 | static void get_offsets PARAMS ((void)); | |
269 | ||
270 | static int read_frame PARAMS ((char *)); | |
271 | ||
272 | static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); | |
273 | ||
274 | static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); | |
275 | ||
dd0ce8f6 AC |
276 | static int hexnumlen PARAMS ((ULONGEST num)); |
277 | ||
dd0ce8f6 AC |
278 | /* exported functions */ |
279 | ||
280 | extern int fromhex PARAMS ((int a)); | |
281 | extern void getpkt PARAMS ((char *buf, int forever)); | |
282 | extern int putpkt PARAMS ((char *buf)); | |
283 | ||
c719b714 JM |
284 | /* Define the target subroutine names */ |
285 | ||
286 | static struct target_ops remote_ops ; | |
287 | ||
288 | static void init_remote_ops(void) | |
289 | { | |
290 | remote_ops.to_shortname = "remote"; | |
291 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; | |
292 | remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\ | |
293 | Specify the serial device it is connected to (e.g. /dev/ttya)." ; | |
294 | remote_ops.to_open = remote_open; | |
295 | remote_ops.to_close = remote_close; | |
296 | remote_ops.to_attach = NULL; | |
297 | remote_ops.to_detach = remote_detach; | |
298 | remote_ops.to_resume = remote_resume; | |
299 | remote_ops.to_wait = remote_wait; | |
300 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
301 | remote_ops.to_store_registers = remote_store_registers; | |
302 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
303 | remote_ops.to_xfer_memory = remote_xfer_memory; | |
304 | remote_ops.to_files_info = remote_files_info; | |
305 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; | |
306 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
307 | remote_ops.to_terminal_init = NULL; | |
308 | remote_ops.to_terminal_inferior = NULL; | |
309 | remote_ops.to_terminal_ours_for_output = NULL; | |
310 | remote_ops.to_terminal_ours = NULL; | |
311 | remote_ops.to_terminal_info = NULL; | |
312 | remote_ops.to_kill = remote_kill; | |
313 | remote_ops.to_load = generic_load; | |
314 | remote_ops.to_lookup_symbol = NULL; | |
315 | remote_ops.to_create_inferior = NULL; | |
316 | remote_ops.to_mourn_inferior = remote_mourn; | |
317 | remote_ops.to_can_run = 0; | |
318 | remote_ops.to_notice_signals = 0; | |
319 | remote_ops.to_thread_alive = remote_thread_alive; | |
320 | remote_ops.to_stop = 0; | |
321 | remote_ops.to_stratum = process_stratum; | |
322 | remote_ops.DONT_USE = NULL; | |
323 | remote_ops.to_has_all_memory = 1; | |
324 | remote_ops.to_has_memory = 1; | |
325 | remote_ops.to_has_stack = 1; | |
326 | remote_ops.to_has_registers = 1; | |
327 | remote_ops.to_has_execution = 1; | |
328 | remote_ops.to_sections = NULL; | |
329 | remote_ops.to_sections_end = NULL; | |
330 | remote_ops.to_magic = OPS_MAGIC ; | |
331 | } /* init_remote_ops */ | |
332 | ||
333 | static struct target_ops extended_remote_ops ; | |
334 | ||
335 | static void init_extended_remote_ops(void) | |
336 | { | |
337 | extended_remote_ops.to_shortname = "extended-remote"; | |
338 | extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol"; | |
339 | extended_remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\ | |
340 | Specify the serial device it is connected to (e.g. /dev/ttya).", | |
341 | extended_remote_ops.to_open = extended_remote_open; | |
342 | extended_remote_ops.to_close = remote_close; | |
343 | extended_remote_ops.to_attach = NULL; | |
344 | extended_remote_ops.to_detach = remote_detach; | |
345 | extended_remote_ops.to_resume = remote_resume; | |
346 | extended_remote_ops.to_wait = remote_wait; | |
347 | extended_remote_ops.to_fetch_registers = remote_fetch_registers; | |
348 | extended_remote_ops.to_store_registers = remote_store_registers; | |
349 | extended_remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
350 | extended_remote_ops.to_xfer_memory = remote_xfer_memory; | |
351 | extended_remote_ops.to_files_info = remote_files_info; | |
352 | extended_remote_ops.to_insert_breakpoint = remote_insert_breakpoint; | |
353 | extended_remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
354 | extended_remote_ops.to_terminal_init = NULL; | |
355 | extended_remote_ops.to_terminal_inferior = NULL; | |
356 | extended_remote_ops.to_terminal_ours_for_output = NULL; | |
357 | extended_remote_ops.to_terminal_ours = NULL; | |
358 | extended_remote_ops.to_terminal_info = NULL; | |
359 | extended_remote_ops.to_kill = remote_kill; | |
360 | extended_remote_ops.to_load = generic_load; | |
361 | extended_remote_ops.to_lookup_symbol = NULL; | |
362 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; | |
363 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
364 | extended_remote_ops.to_can_run = 0; | |
365 | extended_remote_ops.to_notice_signals = 0; | |
366 | extended_remote_ops.to_thread_alive = remote_thread_alive; | |
367 | extended_remote_ops.to_stop = 0; | |
368 | extended_remote_ops.to_stratum = process_stratum; | |
369 | extended_remote_ops.DONT_USE = NULL; | |
370 | extended_remote_ops.to_has_all_memory = 1; | |
371 | extended_remote_ops.to_has_memory = 1; | |
372 | extended_remote_ops.to_has_stack = 1; | |
373 | extended_remote_ops.to_has_registers = 1; | |
374 | extended_remote_ops.to_has_execution = 1; | |
375 | extended_remote_ops.to_sections = NULL; | |
376 | extended_remote_ops.to_sections_end = NULL; | |
377 | extended_remote_ops.to_magic = OPS_MAGIC ; | |
378 | } | |
379 | ||
dd0ce8f6 | 380 | |
ebdb9ade JK |
381 | /* This was 5 seconds, which is a long time to sit and wait. |
382 | Unless this is going though some terminal server or multiplexer or | |
383 | other form of hairy serial connection, I would think 2 seconds would | |
384 | be plenty. */ | |
bd5635a1 | 385 | |
cb1709ae DP |
386 | /* Changed to allow option to set timeout value. |
387 | was static int remote_timeout = 2; */ | |
388 | extern int remote_timeout; | |
bd5635a1 | 389 | |
b52cac6b FF |
390 | /* This variable chooses whether to send a ^C or a break when the user |
391 | requests program interruption. Although ^C is usually what remote | |
392 | systems expect, and that is the default here, sometimes a break is | |
393 | preferable instead. */ | |
394 | ||
395 | static int remote_break; | |
396 | ||
16e1d1d3 | 397 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
398 | remote_open knows that we don't have a file open when the program |
399 | starts. */ | |
0a325463 | 400 | static serial_t remote_desc = NULL; |
bd5635a1 | 401 | |
4d57c599 JK |
402 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
403 | and i386-stub.c. Normally, no one would notice because it only matters | |
404 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
405 | to be more than 400 if required to hold the registers (see below, where | |
406 | we round it up based on REGISTER_BYTES). */ | |
407 | #define PBUFSIZ 400 | |
bd5635a1 RP |
408 | |
409 | /* Maximum number of bytes to read/write at once. The value here | |
410 | is chosen to fill up a packet (the headers account for the 32). */ | |
411 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
412 | ||
b543979c | 413 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
414 | /* The blank line after the #if seems to be required to work around a |
415 | bug in HP's PA compiler. */ | |
b543979c | 416 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
417 | |
418 | #undef PBUFSIZ | |
b543979c | 419 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 420 | #endif |
4aa6fe10 | 421 | |
fea17b55 SS |
422 | /* This variable sets the number of bytes to be written to the target |
423 | in a single packet. Normally PBUFSIZ is satisfactory, but some | |
424 | targets need smaller values (perhaps because the receiving end | |
425 | is slow). */ | |
426 | ||
427 | static int remote_write_size = PBUFSIZ; | |
428 | ||
0a325463 SG |
429 | /* This is the size (in chars) of the first response to the `g' command. This |
430 | is used to limit the size of the memory read and write commands to prevent | |
dd0ce8f6 AC |
431 | stub buffers from overflowing. The size does not include headers and |
432 | trailers, it is only the payload size. */ | |
0a325463 SG |
433 | |
434 | static int remote_register_buf_size = 0; | |
435 | ||
4aa6fe10 JK |
436 | /* Should we try the 'P' request? If this is set to one when the stub |
437 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
438 | static int stub_supports_P = 1; | |
439 | ||
0a325463 SG |
440 | /* These are pointers to hook functions that may be set in order to |
441 | modify resume/wait behavior for a particular architecture. */ | |
442 | ||
443 | void (*target_resume_hook) PARAMS ((void)); | |
444 | void (*target_wait_loop_hook) PARAMS ((void)); | |
445 | ||
4cc1b3f7 JK |
446 | \f |
447 | /* These are the threads which we last sent to the remote system. -1 for all | |
448 | or -2 for not sent yet. */ | |
449 | int general_thread; | |
450 | int cont_thread; | |
451 | ||
452 | static void | |
453 | set_thread (th, gen) | |
454 | int th; | |
455 | int gen; | |
456 | { | |
457 | char buf[PBUFSIZ]; | |
458 | int state = gen ? general_thread : cont_thread; | |
459 | if (state == th) | |
460 | return; | |
461 | buf[0] = 'H'; | |
462 | buf[1] = gen ? 'g' : 'c'; | |
463 | if (th == 42000) | |
464 | { | |
465 | buf[2] = '0'; | |
466 | buf[3] = '\0'; | |
467 | } | |
468 | else if (th < 0) | |
469 | sprintf (&buf[2], "-%x", -th); | |
470 | else | |
471 | sprintf (&buf[2], "%x", th); | |
472 | putpkt (buf); | |
473 | getpkt (buf, 0); | |
474 | if (gen) | |
475 | general_thread = th; | |
476 | else | |
477 | cont_thread = th; | |
478 | } | |
bd5635a1 | 479 | \f |
2b576293 | 480 | /* Return nonzero if the thread TH is still alive on the remote system. */ |
43fc25c8 JL |
481 | |
482 | static int | |
483 | remote_thread_alive (th) | |
484 | int th; | |
485 | { | |
486 | char buf[PBUFSIZ]; | |
487 | ||
488 | buf[0] = 'T'; | |
489 | if (th < 0) | |
490 | sprintf (&buf[1], "-%x", -th); | |
491 | else | |
492 | sprintf (&buf[1], "%x", th); | |
493 | putpkt (buf); | |
494 | getpkt (buf, 0); | |
495 | return (buf[0] == 'O' && buf[1] == 'K'); | |
496 | } | |
2b576293 C |
497 | |
498 | /* Restart the remote side; this is an extended protocol operation. */ | |
499 | ||
500 | static void | |
501 | extended_remote_restart () | |
502 | { | |
503 | char buf[PBUFSIZ]; | |
504 | ||
505 | /* Send the restart command; for reasons I don't understand the | |
506 | remote side really expects a number after the "R". */ | |
507 | buf[0] = 'R'; | |
508 | sprintf (&buf[1], "%x", 0); | |
509 | putpkt (buf); | |
510 | ||
511 | /* Now query for status so this looks just like we restarted | |
512 | gdbserver from scratch. */ | |
513 | putpkt ("?"); | |
514 | getpkt (buf, 0); | |
515 | } | |
43fc25c8 | 516 | \f |
bd5635a1 RP |
517 | /* Clean up connection to a remote debugger. */ |
518 | ||
e1ce8aa5 | 519 | /* ARGSUSED */ |
b543979c | 520 | static void |
bd5635a1 RP |
521 | remote_close (quitting) |
522 | int quitting; | |
523 | { | |
ebdb9ade JK |
524 | if (remote_desc) |
525 | SERIAL_CLOSE (remote_desc); | |
526 | remote_desc = NULL; | |
b543979c JG |
527 | } |
528 | ||
72bba93b SG |
529 | /* Query the remote side for the text, data and bss offsets. */ |
530 | ||
531 | static void | |
532 | get_offsets () | |
533 | { | |
dd0ce8f6 AC |
534 | char buf[PBUFSIZ], *ptr; |
535 | int lose; | |
72bba93b SG |
536 | CORE_ADDR text_addr, data_addr, bss_addr; |
537 | struct section_offsets *offs; | |
538 | ||
539 | putpkt ("qOffsets"); | |
540 | ||
1c95d7ab | 541 | getpkt (buf, 0); |
72bba93b | 542 | |
1c95d7ab JK |
543 | if (buf[0] == '\000') |
544 | return; /* Return silently. Stub doesn't support this | |
545 | command. */ | |
72bba93b SG |
546 | if (buf[0] == 'E') |
547 | { | |
548 | warning ("Remote failure reply: %s", buf); | |
549 | return; | |
550 | } | |
551 | ||
dd0ce8f6 AC |
552 | /* Pick up each field in turn. This used to be done with scanf, but |
553 | scanf will make trouble if CORE_ADDR size doesn't match | |
554 | conversion directives correctly. The following code will work | |
555 | with any size of CORE_ADDR. */ | |
556 | text_addr = data_addr = bss_addr = 0; | |
557 | ptr = buf; | |
558 | lose = 0; | |
559 | ||
560 | if (strncmp (ptr, "Text=", 5) == 0) | |
561 | { | |
562 | ptr += 5; | |
563 | /* Don't use strtol, could lose on big values. */ | |
564 | while (*ptr && *ptr != ';') | |
565 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
566 | } | |
567 | else | |
568 | lose = 1; | |
569 | ||
570 | if (!lose && strncmp (ptr, ";Data=", 6) == 0) | |
571 | { | |
572 | ptr += 6; | |
573 | while (*ptr && *ptr != ';') | |
574 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
575 | } | |
576 | else | |
577 | lose = 1; | |
578 | ||
579 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
580 | { | |
581 | ptr += 5; | |
582 | while (*ptr && *ptr != ';') | |
583 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
584 | } | |
585 | else | |
586 | lose = 1; | |
587 | ||
588 | if (lose) | |
72bba93b SG |
589 | error ("Malformed response to offset query, %s", buf); |
590 | ||
591 | if (symfile_objfile == NULL) | |
592 | return; | |
593 | ||
594 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
595 | + symfile_objfile->num_sections | |
596 | * sizeof (offs->offsets)); | |
597 | memcpy (offs, symfile_objfile->section_offsets, | |
598 | sizeof (struct section_offsets) | |
599 | + symfile_objfile->num_sections | |
600 | * sizeof (offs->offsets)); | |
601 | ||
602 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; | |
1624c38f SG |
603 | |
604 | /* This is a temporary kludge to force data and bss to use the same offsets | |
605 | because that's what nlmconv does now. The real solution requires changes | |
606 | to the stub and remote.c that I don't have time to do right now. */ | |
607 | ||
72bba93b | 608 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 609 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
610 | |
611 | objfile_relocate (symfile_objfile, offs); | |
612 | } | |
613 | ||
7c622b41 JG |
614 | /* Stub for catch_errors. */ |
615 | ||
616 | static int | |
617 | remote_start_remote (dummy) | |
618 | char *dummy; | |
619 | { | |
ac7a377f JK |
620 | immediate_quit = 1; /* Allow user to interrupt it */ |
621 | ||
7c622b41 | 622 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
623 | SERIAL_WRITE (remote_desc, "+", 1); |
624 | ||
4cc1b3f7 JK |
625 | /* Let the stub know that we want it to return the thread. */ |
626 | set_thread (-1, 0); | |
627 | ||
72bba93b SG |
628 | get_offsets (); /* Get text, data & bss offsets */ |
629 | ||
7c622b41 | 630 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 631 | immediate_quit = 0; |
7c622b41 JG |
632 | |
633 | start_remote (); /* Initialize gdb process mechanisms */ | |
634 | return 1; | |
635 | } | |
636 | ||
bd5635a1 RP |
637 | /* Open a connection to a remote debugger. |
638 | NAME is the filename used for communication. */ | |
639 | ||
2b576293 C |
640 | static void |
641 | remote_open (name, from_tty) | |
642 | char *name; | |
643 | int from_tty; | |
644 | { | |
dd0ce8f6 | 645 | remote_open_1 (name, from_tty, &remote_ops, 0); |
2b576293 C |
646 | } |
647 | ||
648 | /* Open a connection to a remote debugger using the extended | |
935e77f5 | 649 | remote gdb protocol. NAME is the filename used for communication. */ |
2b576293 C |
650 | |
651 | static void | |
652 | extended_remote_open (name, from_tty) | |
653 | char *name; | |
654 | int from_tty; | |
655 | { | |
dd0ce8f6 | 656 | remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/); |
2b576293 C |
657 | } |
658 | ||
659 | /* Generic code for opening a connection to a remote target. */ | |
d538b510 RP |
660 | static DCACHE *remote_dcache; |
661 | ||
b543979c | 662 | static void |
dd0ce8f6 | 663 | remote_open_1 (name, from_tty, target, extended_p) |
bd5635a1 RP |
664 | char *name; |
665 | int from_tty; | |
2b576293 | 666 | struct target_ops *target; |
dd0ce8f6 | 667 | int extended_p; |
bd5635a1 | 668 | { |
bd5635a1 | 669 | if (name == 0) |
45993f61 | 670 | error ("To open a remote debug connection, you need to specify what serial\n\ |
bd5635a1 RP |
671 | device is attached to the remote system (e.g. /dev/ttya)."); |
672 | ||
f2fc6e7a JK |
673 | target_preopen (from_tty); |
674 | ||
2b576293 | 675 | unpush_target (target); |
bd5635a1 | 676 | |
d538b510 | 677 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 678 | |
ebdb9ade JK |
679 | remote_desc = SERIAL_OPEN (name); |
680 | if (!remote_desc) | |
bd5635a1 RP |
681 | perror_with_name (name); |
682 | ||
94d4b713 | 683 | if (baud_rate != -1) |
b543979c | 684 | { |
94d4b713 JK |
685 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
686 | { | |
687 | SERIAL_CLOSE (remote_desc); | |
688 | perror_with_name (name); | |
689 | } | |
b543979c | 690 | } |
ebdb9ade | 691 | |
45993f61 | 692 | |
ebdb9ade | 693 | SERIAL_RAW (remote_desc); |
bd5635a1 | 694 | |
e15f2a54 JK |
695 | /* If there is something sitting in the buffer we might take it as a |
696 | response to a command, which would be bad. */ | |
697 | SERIAL_FLUSH_INPUT (remote_desc); | |
698 | ||
bd5635a1 | 699 | if (from_tty) |
7c622b41 JG |
700 | { |
701 | puts_filtered ("Remote debugging using "); | |
702 | puts_filtered (name); | |
703 | puts_filtered ("\n"); | |
704 | } | |
2b576293 | 705 | push_target (target); /* Switch to using remote target now */ |
bd5635a1 | 706 | |
4aa6fe10 JK |
707 | /* Start out by trying the 'P' request to set registers. We set this each |
708 | time that we open a new target so that if the user switches from one | |
709 | stub to another, we can (if the target is closed and reopened) cope. */ | |
710 | stub_supports_P = 1; | |
711 | ||
4cc1b3f7 JK |
712 | general_thread = -2; |
713 | cont_thread = -2; | |
714 | ||
a1e0ba7a SG |
715 | /* Without this, some commands which require an active target (such as kill) |
716 | won't work. This variable serves (at least) double duty as both the pid | |
717 | of the target process (if it has such), and as a flag indicating that a | |
718 | target is active. These functions should be split out into seperate | |
719 | variables, especially since GDB will someday have a notion of debugging | |
720 | several processes. */ | |
721 | ||
4fb7359d | 722 | inferior_pid = 42000; |
4fb7359d SG |
723 | /* Start the remote connection; if error (0), discard this target. |
724 | In particular, if the user quits, be sure to discard it | |
725 | (we'd be in an inconsistent state otherwise). */ | |
726 | if (!catch_errors (remote_start_remote, (char *)0, | |
45993f61 | 727 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
4fb7359d | 728 | pop_target(); |
dd0ce8f6 AC |
729 | |
730 | ||
731 | if (extended_p) | |
732 | { | |
733 | /* tell the remote that we're using the extended protocol. */ | |
734 | char buf[PBUFSIZ]; | |
735 | putpkt ("!"); | |
736 | getpkt (buf, 0); | |
737 | } | |
bd5635a1 RP |
738 | } |
739 | ||
b52cac6b FF |
740 | /* This takes a program previously attached to and detaches it. After |
741 | this is done, GDB can be used to debug some other program. We | |
742 | better not have left any breakpoints in the target program or it'll | |
743 | die when it hits one. */ | |
bd5635a1 RP |
744 | |
745 | static void | |
746 | remote_detach (args, from_tty) | |
747 | char *args; | |
748 | int from_tty; | |
749 | { | |
b52cac6b FF |
750 | char buf[PBUFSIZ]; |
751 | ||
bd5635a1 RP |
752 | if (args) |
753 | error ("Argument given to \"detach\" when remotely debugging."); | |
b52cac6b FF |
754 | |
755 | /* Tell the remote target to detach. */ | |
756 | strcpy (buf, "D"); | |
757 | remote_send (buf); | |
758 | ||
bd5635a1 RP |
759 | pop_target (); |
760 | if (from_tty) | |
7c622b41 | 761 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
762 | } |
763 | ||
764 | /* Convert hex digit A to a number. */ | |
765 | ||
dd0ce8f6 | 766 | int |
bd5635a1 RP |
767 | fromhex (a) |
768 | int a; | |
769 | { | |
770 | if (a >= '0' && a <= '9') | |
771 | return a - '0'; | |
772 | else if (a >= 'a' && a <= 'f') | |
773 | return a - 'a' + 10; | |
dd0ce8f6 AC |
774 | else if (a >= 'A' && a <= 'F') |
775 | return a - 'A' + 10; | |
ec10503a | 776 | else |
6c27841f | 777 | error ("Reply contains invalid hex digit %d", a); |
bd5635a1 RP |
778 | } |
779 | ||
780 | /* Convert number NIB to a hex digit. */ | |
781 | ||
782 | static int | |
783 | tohex (nib) | |
784 | int nib; | |
785 | { | |
786 | if (nib < 10) | |
787 | return '0'+nib; | |
788 | else | |
789 | return 'a'+nib-10; | |
790 | } | |
791 | \f | |
792 | /* Tell the remote machine to resume. */ | |
793 | ||
4cc1b3f7 JK |
794 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; |
795 | int last_sent_step; | |
796 | ||
b543979c | 797 | static void |
d538b510 | 798 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
799 | int pid, step; |
800 | enum target_signal siggnal; | |
bd5635a1 RP |
801 | { |
802 | char buf[PBUFSIZ]; | |
803 | ||
4cc1b3f7 JK |
804 | if (pid == -1) |
805 | set_thread (inferior_pid, 0); | |
806 | else | |
807 | set_thread (pid, 0); | |
bd5635a1 | 808 | |
d538b510 | 809 | dcache_flush (remote_dcache); |
bd5635a1 | 810 | |
4cc1b3f7 JK |
811 | last_sent_signal = siggnal; |
812 | last_sent_step = step; | |
813 | ||
0a325463 SG |
814 | /* A hook for when we need to do something at the last moment before |
815 | resumption. */ | |
816 | if (target_resume_hook) | |
817 | (*target_resume_hook) (); | |
818 | ||
4cc1b3f7 JK |
819 | if (siggnal != TARGET_SIGNAL_0) |
820 | { | |
821 | buf[0] = step ? 'S' : 'C'; | |
822 | buf[1] = tohex (((int)siggnal >> 4) & 0xf); | |
823 | buf[2] = tohex ((int)siggnal & 0xf); | |
824 | buf[3] = '\0'; | |
825 | } | |
826 | else | |
827 | strcpy (buf, step ? "s": "c"); | |
bd5635a1 RP |
828 | |
829 | putpkt (buf); | |
830 | } | |
ebdb9ade | 831 | \f |
b543979c JG |
832 | /* Send ^C to target to halt it. Target will respond, and send us a |
833 | packet. */ | |
834 | ||
5af4f5f6 JK |
835 | static void |
836 | remote_interrupt (signo) | |
e676a15f | 837 | int signo; |
b543979c | 838 | { |
ebdb9ade JK |
839 | /* If this doesn't work, try more severe steps. */ |
840 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 841 | |
d0d8484a | 842 | if (remote_debug) |
199b2450 | 843 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 844 | |
b52cac6b FF |
845 | /* Send a break or a ^C, depending on user preference. */ |
846 | if (remote_break) | |
847 | SERIAL_SEND_BREAK (remote_desc); | |
848 | else | |
849 | SERIAL_WRITE (remote_desc, "\003", 1); | |
b543979c JG |
850 | } |
851 | ||
5af4f5f6 JK |
852 | static void (*ofunc)(); |
853 | ||
ebdb9ade JK |
854 | /* The user typed ^C twice. */ |
855 | static void | |
856 | remote_interrupt_twice (signo) | |
857 | int signo; | |
858 | { | |
859 | signal (signo, ofunc); | |
860 | ||
981a3309 SG |
861 | interrupt_query (); |
862 | ||
863 | signal (signo, remote_interrupt); | |
864 | } | |
865 | ||
866 | /* Ask the user what to do when an interrupt is received. */ | |
867 | ||
868 | static void | |
869 | interrupt_query () | |
870 | { | |
ebdb9ade | 871 | target_terminal_ours (); |
981a3309 | 872 | |
6b27ebe8 | 873 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
874 | Give up (and stop debugging it)? ")) |
875 | { | |
876 | target_mourn_inferior (); | |
e50ebec8 | 877 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 878 | } |
981a3309 SG |
879 | |
880 | target_terminal_inferior (); | |
ebdb9ade | 881 | } |
b543979c | 882 | |
4cc1b3f7 JK |
883 | /* If nonzero, ignore the next kill. */ |
884 | int kill_kludge; | |
885 | ||
dd0ce8f6 AC |
886 | void |
887 | remote_console_output (msg) | |
888 | char *msg; | |
889 | { | |
890 | char *p; | |
891 | ||
892 | for (p = msg; *p; p +=2) | |
893 | { | |
894 | char tb[2]; | |
895 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
896 | tb[0] = c; | |
897 | tb[1] = 0; | |
898 | if (target_output_hook) | |
899 | target_output_hook (tb); | |
900 | else | |
901 | fputs_filtered (tb, gdb_stdout); | |
902 | } | |
903 | } | |
904 | ||
bd5635a1 | 905 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
906 | storing status in STATUS just as `wait' would. |
907 | Returns "pid" (though it's not clear what, if anything, that | |
908 | means in the case of this target). */ | |
bd5635a1 | 909 | |
b543979c | 910 | static int |
d0d8484a SG |
911 | remote_wait (pid, status) |
912 | int pid; | |
94d4b713 | 913 | struct target_waitstatus *status; |
bd5635a1 RP |
914 | { |
915 | unsigned char buf[PBUFSIZ]; | |
4cc1b3f7 | 916 | int thread_num = -1; |
8f86a4e4 | 917 | |
94d4b713 JK |
918 | status->kind = TARGET_WAITKIND_EXITED; |
919 | status->value.integer = 0; | |
b543979c | 920 | |
4f8a48e5 | 921 | while (1) |
8f86a4e4 | 922 | { |
4f8a48e5 | 923 | unsigned char *p; |
a03d4f8e | 924 | |
4f8a48e5 ILT |
925 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
926 | getpkt ((char *) buf, 1); | |
927 | signal (SIGINT, ofunc); | |
4ecee2f9 | 928 | |
0a325463 SG |
929 | /* This is a hook for when we need to do something (perhaps the |
930 | collection of trace data) every time the target stops. */ | |
931 | if (target_wait_loop_hook) | |
932 | (*target_wait_loop_hook) (); | |
933 | ||
754e5da2 | 934 | switch (buf[0]) |
8f86a4e4 | 935 | { |
754e5da2 SG |
936 | case 'E': /* Error of some sort */ |
937 | warning ("Remote failure reply: %s", buf); | |
938 | continue; | |
939 | case 'T': /* Status with PC, SP, FP, ... */ | |
940 | { | |
941 | int i; | |
942 | long regno; | |
943 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 944 | |
754e5da2 SG |
945 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
946 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
947 | ss = signal number | |
948 | n... = register number | |
949 | r... = register contents | |
950 | */ | |
754e5da2 | 951 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 952 | |
754e5da2 SG |
953 | while (*p) |
954 | { | |
955 | unsigned char *p1; | |
45993f61 | 956 | char *p_temp; |
5af4f5f6 | 957 | |
2b576293 | 958 | regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */ |
45993f61 | 959 | p1 = (unsigned char *)p_temp; |
5af4f5f6 | 960 | |
754e5da2 | 961 | if (p1 == p) |
754e5da2 | 962 | { |
2b576293 | 963 | p1 = (unsigned char *) strchr ((const char *) p, ':'); |
4cc1b3f7 JK |
964 | if (p1 == NULL) |
965 | warning ("Malformed packet (missing colon): %s\n\ | |
966 | Packet: '%s'\n", | |
967 | p, buf); | |
2b576293 | 968 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) |
4cc1b3f7 | 969 | { |
2b576293 | 970 | thread_num = strtol ((const char *) ++p1, &p_temp, 16); |
45993f61 | 971 | p = (unsigned char *)p_temp; |
4cc1b3f7 JK |
972 | } |
973 | } | |
974 | else | |
975 | { | |
976 | p = p1; | |
977 | ||
978 | if (*p++ != ':') | |
979 | warning ("Malformed packet (missing colon): %s\n\ | |
980 | Packet: '%s'\n", | |
981 | p, buf); | |
982 | ||
983 | if (regno >= NUM_REGS) | |
2b576293 | 984 | warning ("Remote sent bad register number %ld: %s\n\ |
4cc1b3f7 JK |
985 | Packet: '%s'\n", |
986 | regno, p, buf); | |
987 | ||
988 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
989 | { | |
990 | if (p[0] == 0 || p[1] == 0) | |
991 | warning ("Remote reply is too short: %s", buf); | |
0d14c7df | 992 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
4cc1b3f7 JK |
993 | p += 2; |
994 | } | |
995 | supply_register (regno, regs); | |
754e5da2 | 996 | } |
4f8a48e5 | 997 | |
754e5da2 SG |
998 | if (*p++ != ';') |
999 | warning ("Remote register badly formatted: %s", buf); | |
754e5da2 SG |
1000 | } |
1001 | } | |
1002 | /* fall through */ | |
1003 | case 'S': /* Old style status, just signal only */ | |
1004 | status->kind = TARGET_WAITKIND_STOPPED; | |
1005 | status->value.sig = (enum target_signal) | |
1006 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
4f8a48e5 | 1007 | |
4cc1b3f7 | 1008 | goto got_status; |
754e5da2 SG |
1009 | case 'W': /* Target exited */ |
1010 | { | |
1011 | /* The remote process exited. */ | |
1012 | status->kind = TARGET_WAITKIND_EXITED; | |
1013 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
4cc1b3f7 | 1014 | goto got_status; |
754e5da2 | 1015 | } |
4cc1b3f7 JK |
1016 | case 'X': |
1017 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
1018 | status->value.sig = (enum target_signal) | |
1019 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
1020 | kill_kludge = 1; | |
1021 | ||
1022 | goto got_status; | |
754e5da2 | 1023 | case 'O': /* Console output */ |
dd0ce8f6 | 1024 | remote_console_output (buf + 1); |
754e5da2 | 1025 | continue; |
4cc1b3f7 JK |
1026 | case '\0': |
1027 | if (last_sent_signal != TARGET_SIGNAL_0) | |
1028 | { | |
1029 | /* Zero length reply means that we tried 'S' or 'C' and | |
1030 | the remote system doesn't support it. */ | |
1031 | target_terminal_ours_for_output (); | |
1032 | printf_filtered | |
1033 | ("Can't send signals to this remote system. %s not sent.\n", | |
1034 | target_signal_to_name (last_sent_signal)); | |
1035 | last_sent_signal = TARGET_SIGNAL_0; | |
1036 | target_terminal_inferior (); | |
1037 | ||
2b576293 C |
1038 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); |
1039 | putpkt ((char *) buf); | |
4cc1b3f7 JK |
1040 | continue; |
1041 | } | |
1042 | /* else fallthrough */ | |
754e5da2 SG |
1043 | default: |
1044 | warning ("Invalid remote reply: %s", buf); | |
1045 | continue; | |
4f8a48e5 | 1046 | } |
758aeb93 | 1047 | } |
4cc1b3f7 JK |
1048 | got_status: |
1049 | if (thread_num != -1) | |
1050 | { | |
1051 | /* Initial thread value can only be acquired via wait, so deal with | |
1052 | this marker which is used before the first thread value is | |
1053 | acquired. */ | |
1054 | if (inferior_pid == 42000) | |
1055 | { | |
1056 | inferior_pid = thread_num; | |
1057 | add_thread (inferior_pid); | |
1058 | } | |
1059 | return thread_num; | |
1060 | } | |
1061 | return inferior_pid; | |
bd5635a1 RP |
1062 | } |
1063 | ||
55fea07b JK |
1064 | /* Number of bytes of registers this stub implements. */ |
1065 | static int register_bytes_found; | |
1066 | ||
bd5635a1 | 1067 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
1068 | /* Currently we just read all the registers, so we don't use regno. */ |
1069 | /* ARGSUSED */ | |
b543979c | 1070 | static void |
bd5635a1 RP |
1071 | remote_fetch_registers (regno) |
1072 | int regno; | |
1073 | { | |
1074 | char buf[PBUFSIZ]; | |
1075 | int i; | |
1076 | char *p; | |
1077 | char regs[REGISTER_BYTES]; | |
1078 | ||
4cc1b3f7 JK |
1079 | set_thread (inferior_pid, 1); |
1080 | ||
bd5635a1 RP |
1081 | sprintf (buf, "g"); |
1082 | remote_send (buf); | |
1083 | ||
0a325463 SG |
1084 | if (remote_register_buf_size == 0) |
1085 | remote_register_buf_size = strlen (buf); | |
1086 | ||
55fea07b JK |
1087 | /* Unimplemented registers read as all bits zero. */ |
1088 | memset (regs, 0, REGISTER_BYTES); | |
1089 | ||
981a3309 SG |
1090 | /* We can get out of synch in various cases. If the first character |
1091 | in the buffer is not a hex character, assume that has happened | |
1092 | and try to fetch another packet to read. */ | |
1093 | while ((buf[0] < '0' || buf[0] > '9') | |
1094 | && (buf[0] < 'a' || buf[0] > 'f')) | |
1095 | { | |
d0d8484a | 1096 | if (remote_debug) |
199b2450 | 1097 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
1098 | getpkt (buf, 0); |
1099 | } | |
1100 | ||
bd5635a1 RP |
1101 | /* Reply describes registers byte by byte, each byte encoded as two |
1102 | hex characters. Suck them all up, then supply them to the | |
1103 | register cacheing/storage mechanism. */ | |
1104 | ||
1105 | p = buf; | |
1106 | for (i = 0; i < REGISTER_BYTES; i++) | |
1107 | { | |
55fea07b JK |
1108 | if (p[0] == 0) |
1109 | break; | |
1110 | if (p[1] == 0) | |
1111 | { | |
1112 | warning ("Remote reply is of odd length: %s", buf); | |
1113 | /* Don't change register_bytes_found in this case, and don't | |
1114 | print a second warning. */ | |
1115 | goto supply_them; | |
1116 | } | |
bd5635a1 RP |
1117 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
1118 | p += 2; | |
1119 | } | |
55fea07b JK |
1120 | |
1121 | if (i != register_bytes_found) | |
1122 | { | |
1123 | register_bytes_found = i; | |
1124 | #ifdef REGISTER_BYTES_OK | |
1125 | if (!REGISTER_BYTES_OK (i)) | |
1126 | warning ("Remote reply is too short: %s", buf); | |
1127 | #endif | |
1128 | } | |
1129 | ||
1130 | supply_them: | |
bd5635a1 RP |
1131 | for (i = 0; i < NUM_REGS; i++) |
1132 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
1133 | } |
1134 | ||
4aa6fe10 JK |
1135 | /* Prepare to store registers. Since we may send them all (using a |
1136 | 'G' request), we have to read out the ones we don't want to change | |
1137 | first. */ | |
bd5635a1 | 1138 | |
b543979c | 1139 | static void |
bd5635a1 RP |
1140 | remote_prepare_to_store () |
1141 | { | |
34517ebc JG |
1142 | /* Make sure the entire registers array is valid. */ |
1143 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
1144 | } |
1145 | ||
4aa6fe10 JK |
1146 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
1147 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 1148 | |
b543979c | 1149 | static void |
bd5635a1 RP |
1150 | remote_store_registers (regno) |
1151 | int regno; | |
1152 | { | |
1153 | char buf[PBUFSIZ]; | |
1154 | int i; | |
1155 | char *p; | |
1156 | ||
4cc1b3f7 JK |
1157 | set_thread (inferior_pid, 1); |
1158 | ||
4aa6fe10 JK |
1159 | if (regno >= 0 && stub_supports_P) |
1160 | { | |
1161 | /* Try storing a single register. */ | |
1162 | char *regp; | |
1163 | ||
0c993550 | 1164 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
1165 | p = buf + strlen (buf); |
1166 | regp = ®isters[REGISTER_BYTE (regno)]; | |
1167 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
1168 | { | |
1169 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
1170 | *p++ = tohex (regp[i] & 0xf); | |
1171 | } | |
1172 | *p = '\0'; | |
1173 | remote_send (buf); | |
1174 | if (buf[0] != '\0') | |
1175 | { | |
1176 | /* The stub understands the 'P' request. We are done. */ | |
1177 | return; | |
1178 | } | |
1179 | ||
1180 | /* The stub does not support the 'P' request. Use 'G' instead, | |
1181 | and don't try using 'P' in the future (it will just waste our | |
1182 | time). */ | |
1183 | stub_supports_P = 0; | |
1184 | } | |
1185 | ||
bd5635a1 | 1186 | buf[0] = 'G'; |
4aa6fe10 | 1187 | |
bd5635a1 RP |
1188 | /* Command describes registers byte by byte, |
1189 | each byte encoded as two hex characters. */ | |
1190 | ||
1191 | p = buf + 1; | |
55fea07b JK |
1192 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
1193 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
1194 | { |
1195 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
1196 | *p++ = tohex (registers[i] & 0xf); | |
1197 | } | |
1198 | *p = '\0'; | |
1199 | ||
1200 | remote_send (buf); | |
bd5635a1 RP |
1201 | } |
1202 | ||
45993f61 SC |
1203 | /* |
1204 | Use of the data cache *used* to be disabled because it loses for looking at | |
b43e0347 | 1205 | and changing hardware I/O ports and the like. Accepting `volatile' |
45993f61 SC |
1206 | would perhaps be one way to fix it. Another idea would be to use the |
1207 | executable file for the text segment (for all SEC_CODE sections? | |
1208 | For all SEC_READONLY sections?). This has problems if you want to | |
1209 | actually see what the memory contains (e.g. self-modifying code, | |
1210 | clobbered memory, user downloaded the wrong thing). | |
1211 | ||
1212 | Because it speeds so much up, it's now enabled, if you're playing | |
1213 | with registers you turn it of (set remotecache 0) | |
1214 | */ | |
b43e0347 | 1215 | |
bd5635a1 RP |
1216 | /* Read a word from remote address ADDR and return it. |
1217 | This goes through the data cache. */ | |
1218 | ||
2b576293 | 1219 | #if 0 /* unused? */ |
b43e0347 | 1220 | static int |
bd5635a1 RP |
1221 | remote_fetch_word (addr) |
1222 | CORE_ADDR addr; | |
1223 | { | |
d538b510 | 1224 | return dcache_fetch (remote_dcache, addr); |
bd5635a1 RP |
1225 | } |
1226 | ||
1227 | /* Write a word WORD into remote address ADDR. | |
1228 | This goes through the data cache. */ | |
1229 | ||
b43e0347 | 1230 | static void |
bd5635a1 RP |
1231 | remote_store_word (addr, word) |
1232 | CORE_ADDR addr; | |
1233 | int word; | |
1234 | { | |
d538b510 | 1235 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 1236 | } |
2b576293 | 1237 | #endif /* 0 (unused?) */ |
45993f61 | 1238 | |
bd5635a1 | 1239 | \f |
0a325463 SG |
1240 | |
1241 | /* Return the number of hex digits in num. */ | |
1242 | ||
1243 | static int | |
1244 | hexnumlen (num) | |
1245 | ULONGEST num; | |
1246 | { | |
1247 | int i; | |
1248 | ||
1249 | for (i = 0; num != 0; i++) | |
1250 | num >>= 4; | |
1251 | ||
dd0ce8f6 | 1252 | return max (i, 1); |
0a325463 SG |
1253 | } |
1254 | ||
bd5635a1 RP |
1255 | /* Write memory data directly to the remote machine. |
1256 | This does not inform the data cache; the data cache uses this. | |
1257 | MEMADDR is the address in the remote memory space. | |
1258 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1259 | LEN is the number of bytes. |
bd5635a1 | 1260 | |
d538b510 RP |
1261 | Returns number of bytes transferred, or 0 for error. */ |
1262 | ||
1263 | static int | |
bd5635a1 RP |
1264 | remote_write_bytes (memaddr, myaddr, len) |
1265 | CORE_ADDR memaddr; | |
43fc25c8 | 1266 | char *myaddr; |
bd5635a1 RP |
1267 | int len; |
1268 | { | |
0a325463 SG |
1269 | int max_buf_size; /* Max size of packet output buffer */ |
1270 | int origlen; | |
1271 | ||
ec10503a | 1272 | /* Chop the transfer down if necessary */ |
bd5635a1 | 1273 | |
0a325463 | 1274 | max_buf_size = min (remote_write_size, PBUFSIZ); |
dd0ce8f6 AC |
1275 | if (remote_register_buf_size != 0) |
1276 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
0a325463 | 1277 | |
dd0ce8f6 AC |
1278 | /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */ |
1279 | max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4; | |
0a325463 SG |
1280 | |
1281 | origlen = len; | |
1282 | while (len > 0) | |
ec10503a | 1283 | { |
0a325463 SG |
1284 | char buf[PBUFSIZ]; |
1285 | char *p; | |
1286 | int todo; | |
1287 | int i; | |
fea17b55 | 1288 | |
0a325463 | 1289 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
bd5635a1 | 1290 | |
ec10503a SC |
1291 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1292 | result in a buffer like sprintf. */ | |
0a325463 | 1293 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); |
bd5635a1 | 1294 | |
ec10503a SC |
1295 | /* We send target system values byte by byte, in increasing byte addresses, |
1296 | each byte encoded as two hex characters. */ | |
bd5635a1 | 1297 | |
ec10503a SC |
1298 | p = buf + strlen (buf); |
1299 | for (i = 0; i < todo; i++) | |
1300 | { | |
0a325463 SG |
1301 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); |
1302 | *p++ = tohex (myaddr[i] & 0xf); | |
ec10503a SC |
1303 | } |
1304 | *p = '\0'; | |
d538b510 | 1305 | |
ec10503a SC |
1306 | putpkt (buf); |
1307 | getpkt (buf, 0); | |
1308 | ||
1309 | if (buf[0] == 'E') | |
1310 | { | |
1311 | /* There is no correspondance between what the remote protocol uses | |
1312 | for errors and errno codes. We would like a cleaner way of | |
1313 | representing errors (big enough to include errno codes, bfd_error | |
1314 | codes, and others). But for now just return EIO. */ | |
1315 | errno = EIO; | |
1316 | return 0; | |
1317 | } | |
0a325463 SG |
1318 | myaddr += todo; |
1319 | memaddr += todo; | |
1320 | len -= todo; | |
d538b510 | 1321 | } |
0a325463 | 1322 | return origlen; |
bd5635a1 RP |
1323 | } |
1324 | ||
1325 | /* Read memory data directly from the remote machine. | |
1326 | This does not use the data cache; the data cache uses this. | |
1327 | MEMADDR is the address in the remote memory space. | |
1328 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1329 | LEN is the number of bytes. |
bd5635a1 | 1330 | |
d538b510 RP |
1331 | Returns number of bytes transferred, or 0 for error. */ |
1332 | ||
1333 | static int | |
bd5635a1 RP |
1334 | remote_read_bytes (memaddr, myaddr, len) |
1335 | CORE_ADDR memaddr; | |
43fc25c8 | 1336 | char *myaddr; |
bd5635a1 RP |
1337 | int len; |
1338 | { | |
0a325463 SG |
1339 | int max_buf_size; /* Max size of packet output buffer */ |
1340 | int origlen; | |
bd5635a1 | 1341 | |
0a325463 SG |
1342 | /* Chop the transfer down if necessary */ |
1343 | ||
1344 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
dd0ce8f6 AC |
1345 | if (remote_register_buf_size != 0) |
1346 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
0a325463 SG |
1347 | |
1348 | origlen = len; | |
1349 | while (len > 0) | |
0d14c7df | 1350 | { |
0a325463 SG |
1351 | char buf[PBUFSIZ]; |
1352 | char *p; | |
1353 | int todo; | |
1354 | int i; | |
1355 | ||
1356 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ | |
bd5635a1 | 1357 | |
0d14c7df FF |
1358 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1359 | result in a buffer like sprintf. */ | |
0a325463 | 1360 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); |
0d14c7df FF |
1361 | putpkt (buf); |
1362 | getpkt (buf, 0); | |
d538b510 | 1363 | |
0d14c7df FF |
1364 | if (buf[0] == 'E') |
1365 | { | |
1366 | /* There is no correspondance between what the remote protocol uses | |
1367 | for errors and errno codes. We would like a cleaner way of | |
1368 | representing errors (big enough to include errno codes, bfd_error | |
1369 | codes, and others). But for now just return EIO. */ | |
1370 | errno = EIO; | |
1371 | return 0; | |
1372 | } | |
bd5635a1 | 1373 | |
b543979c | 1374 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
1375 | each byte encoded as two hex characters. */ |
1376 | ||
0d14c7df FF |
1377 | p = buf; |
1378 | for (i = 0; i < todo; i++) | |
1379 | { | |
1380 | if (p[0] == 0 || p[1] == 0) | |
1381 | /* Reply is short. This means that we were able to read only part | |
1382 | of what we wanted to. */ | |
0a325463 SG |
1383 | return i + (origlen - len); |
1384 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
0d14c7df FF |
1385 | p += 2; |
1386 | } | |
0a325463 SG |
1387 | myaddr += todo; |
1388 | memaddr += todo; | |
1389 | len -= todo; | |
bd5635a1 | 1390 | } |
0a325463 | 1391 | return origlen; |
bd5635a1 RP |
1392 | } |
1393 | \f | |
1394 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 1395 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
1396 | nonzero. Returns length of data written or read; 0 for error. */ |
1397 | ||
b543979c JG |
1398 | /* ARGSUSED */ |
1399 | static int | |
1400 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
1401 | CORE_ADDR memaddr; |
1402 | char *myaddr; | |
1403 | int len; | |
e1ce8aa5 | 1404 | int should_write; |
b543979c | 1405 | struct target_ops *target; /* ignored */ |
bd5635a1 | 1406 | { |
c719b714 JM |
1407 | #ifdef REMOTE_TRANSLATE_XFER_ADDRESS |
1408 | CORE_ADDR targaddr; | |
1409 | int targlen; | |
1410 | REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen); | |
1411 | if (targlen == 0) | |
1412 | return 0; | |
1413 | memaddr = targaddr; | |
1414 | len = targlen; | |
1415 | #endif | |
1416 | ||
45993f61 | 1417 | return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write); |
bd5635a1 RP |
1418 | } |
1419 | ||
45993f61 | 1420 | |
94d4b713 JK |
1421 | #if 0 |
1422 | /* Enable after 4.12. */ | |
1423 | ||
1424 | void | |
1425 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
1426 | addr_found, data_found) | |
1427 | int len; | |
1428 | char *data; | |
1429 | char *mask; | |
1430 | CORE_ADDR startaddr; | |
1431 | int increment; | |
1432 | CORE_ADDR lorange; | |
1433 | CORE_ADDR hirange; | |
1434 | CORE_ADDR *addr_found; | |
1435 | char *data_found; | |
1436 | { | |
1437 | if (increment == -4 && len == 4) | |
1438 | { | |
1439 | long mask_long, data_long; | |
1440 | long data_found_long; | |
1441 | CORE_ADDR addr_we_found; | |
1442 | char buf[PBUFSIZ]; | |
1443 | long returned_long[2]; | |
1444 | char *p; | |
1445 | ||
1446 | mask_long = extract_unsigned_integer (mask, len); | |
1447 | data_long = extract_unsigned_integer (data, len); | |
1448 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
1449 | putpkt (buf); | |
1450 | getpkt (buf, 0); | |
1451 | if (buf[0] == '\0') | |
1452 | { | |
1453 | /* The stub doesn't support the 't' request. We might want to | |
1454 | remember this fact, but on the other hand the stub could be | |
1455 | switched on us. Maybe we should remember it only until | |
1456 | the next "target remote". */ | |
1457 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1458 | hirange, addr_found, data_found); | |
1459 | return; | |
1460 | } | |
1461 | ||
1462 | if (buf[0] == 'E') | |
1463 | /* There is no correspondance between what the remote protocol uses | |
1464 | for errors and errno codes. We would like a cleaner way of | |
1465 | representing errors (big enough to include errno codes, bfd_error | |
1466 | codes, and others). But for now just use EIO. */ | |
1467 | memory_error (EIO, startaddr); | |
1468 | p = buf; | |
1469 | addr_we_found = 0; | |
1470 | while (*p != '\0' && *p != ',') | |
1471 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1472 | if (*p == '\0') | |
1473 | error ("Protocol error: short return for search"); | |
1474 | ||
1475 | data_found_long = 0; | |
1476 | while (*p != '\0' && *p != ',') | |
1477 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1478 | /* Ignore anything after this comma, for future extensions. */ | |
1479 | ||
1480 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1481 | { | |
1482 | *addr_found = 0; | |
1483 | return; | |
1484 | } | |
1485 | ||
1486 | *addr_found = addr_we_found; | |
1487 | *data_found = store_unsigned_integer (data_we_found, len); | |
1488 | return; | |
1489 | } | |
1490 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1491 | hirange, addr_found, data_found); | |
1492 | } | |
1493 | #endif /* 0 */ | |
1494 | \f | |
b543979c | 1495 | static void |
8f86a4e4 | 1496 | remote_files_info (ignore) |
5af4f5f6 | 1497 | struct target_ops *ignore; |
bd5635a1 | 1498 | { |
7c622b41 | 1499 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1500 | } |
1501 | \f | |
e50ebec8 JK |
1502 | /* Stuff for dealing with the packets which are part of this protocol. |
1503 | See comment at top of file for details. */ | |
bd5635a1 | 1504 | |
ebdb9ade | 1505 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1506 | |
bd5635a1 | 1507 | static int |
754e5da2 SG |
1508 | readchar (timeout) |
1509 | int timeout; | |
bd5635a1 | 1510 | { |
ebdb9ade | 1511 | int ch; |
bd5635a1 | 1512 | |
ebdb9ade | 1513 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1514 | |
754e5da2 SG |
1515 | switch (ch) |
1516 | { | |
1517 | case SERIAL_EOF: | |
1518 | error ("Remote connection closed"); | |
1519 | case SERIAL_ERROR: | |
1520 | perror_with_name ("Remote communication error"); | |
1521 | case SERIAL_TIMEOUT: | |
1522 | return ch; | |
1523 | default: | |
1524 | return ch & 0x7f; | |
1525 | } | |
bd5635a1 RP |
1526 | } |
1527 | ||
1528 | /* Send the command in BUF to the remote machine, | |
1529 | and read the reply into BUF. | |
1530 | Report an error if we get an error reply. */ | |
1531 | ||
1532 | static void | |
1533 | remote_send (buf) | |
1534 | char *buf; | |
1535 | { | |
bd5635a1 | 1536 | putpkt (buf); |
7c622b41 | 1537 | getpkt (buf, 0); |
bd5635a1 RP |
1538 | |
1539 | if (buf[0] == 'E') | |
1540 | error ("Remote failure reply: %s", buf); | |
1541 | } | |
1542 | ||
1543 | /* Send a packet to the remote machine, with error checking. | |
1544 | The data of the packet is in BUF. */ | |
1545 | ||
dd0ce8f6 | 1546 | int |
bd5635a1 RP |
1547 | putpkt (buf) |
1548 | char *buf; | |
1549 | { | |
1550 | int i; | |
1551 | unsigned char csum = 0; | |
b543979c | 1552 | char buf2[PBUFSIZ]; |
bd5635a1 | 1553 | int cnt = strlen (buf); |
ebdb9ade | 1554 | int ch; |
45993f61 | 1555 | int tcount = 0; |
bd5635a1 RP |
1556 | char *p; |
1557 | ||
1558 | /* Copy the packet into buffer BUF2, encapsulating it | |
1559 | and giving it a checksum. */ | |
1560 | ||
b52cac6b | 1561 | if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ |
b543979c JG |
1562 | abort(); |
1563 | ||
bd5635a1 RP |
1564 | p = buf2; |
1565 | *p++ = '$'; | |
1566 | ||
1567 | for (i = 0; i < cnt; i++) | |
1568 | { | |
1569 | csum += buf[i]; | |
1570 | *p++ = buf[i]; | |
1571 | } | |
1572 | *p++ = '#'; | |
1573 | *p++ = tohex ((csum >> 4) & 0xf); | |
1574 | *p++ = tohex (csum & 0xf); | |
1575 | ||
1576 | /* Send it over and over until we get a positive ack. */ | |
1577 | ||
6b27ebe8 JK |
1578 | while (1) |
1579 | { | |
1624c38f SG |
1580 | int started_error_output = 0; |
1581 | ||
d0d8484a | 1582 | if (remote_debug) |
6b27ebe8 JK |
1583 | { |
1584 | *p = '\0'; | |
1624c38f SG |
1585 | printf_unfiltered ("Sending packet: %s...", buf2); |
1586 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1587 | } |
1588 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1589 | perror_with_name ("putpkt: write failed"); | |
1590 | ||
1591 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1592 | while (1) | |
1593 | { | |
754e5da2 | 1594 | ch = readchar (remote_timeout); |
6b27ebe8 | 1595 | |
45993f61 | 1596 | if (remote_debug) |
1624c38f SG |
1597 | { |
1598 | switch (ch) | |
1599 | { | |
1600 | case '+': | |
1601 | case SERIAL_TIMEOUT: | |
1624c38f SG |
1602 | case '$': |
1603 | if (started_error_output) | |
1604 | { | |
45993f61 | 1605 | putchar_unfiltered ('\n'); |
1624c38f SG |
1606 | started_error_output = 0; |
1607 | } | |
1608 | } | |
1609 | } | |
1610 | ||
6b27ebe8 JK |
1611 | switch (ch) |
1612 | { | |
1613 | case '+': | |
d0d8484a | 1614 | if (remote_debug) |
199b2450 | 1615 | printf_unfiltered("Ack\n"); |
4cc1b3f7 | 1616 | return 1; |
6b27ebe8 | 1617 | case SERIAL_TIMEOUT: |
45993f61 SC |
1618 | tcount ++; |
1619 | if (tcount > 3) | |
1620 | return 0; | |
6b27ebe8 | 1621 | break; /* Retransmit buffer */ |
1624c38f SG |
1622 | case '$': |
1623 | { | |
6c27841f | 1624 | char junkbuf[PBUFSIZ]; |
1624c38f SG |
1625 | |
1626 | /* It's probably an old response, and we're out of sync. Just | |
1627 | gobble up the packet and ignore it. */ | |
1628 | getpkt (junkbuf, 0); | |
1629 | continue; /* Now, go look for + */ | |
1630 | } | |
6b27ebe8 | 1631 | default: |
d0d8484a | 1632 | if (remote_debug) |
1624c38f SG |
1633 | { |
1634 | if (!started_error_output) | |
1635 | { | |
1636 | started_error_output = 1; | |
1637 | printf_unfiltered ("putpkt: Junk: "); | |
1638 | } | |
45993f61 | 1639 | putchar_unfiltered (ch & 0177); |
1624c38f | 1640 | } |
6b27ebe8 JK |
1641 | continue; |
1642 | } | |
1643 | break; /* Here to retransmit */ | |
1644 | } | |
981a3309 | 1645 | |
94d4b713 JK |
1646 | #if 0 |
1647 | /* This is wrong. If doing a long backtrace, the user should be | |
1648 | able to get out next time we call QUIT, without anything as violent | |
1649 | as interrupt_query. If we want to provide a way out of here | |
1650 | without getting to the next QUIT, it should be based on hitting | |
1651 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1652 | if (quit_flag) |
1653 | { | |
1654 | quit_flag = 0; | |
1655 | interrupt_query (); | |
1656 | } | |
94d4b713 | 1657 | #endif |
6b27ebe8 | 1658 | } |
bd5635a1 RP |
1659 | } |
1660 | ||
754e5da2 SG |
1661 | /* Come here after finding the start of the frame. Collect the rest into BUF, |
1662 | verifying the checksum, length, and handling run-length compression. | |
1663 | Returns 0 on any error, 1 on success. */ | |
1664 | ||
1665 | static int | |
1666 | read_frame (buf) | |
1667 | char *buf; | |
1668 | { | |
1669 | unsigned char csum; | |
1670 | char *bp; | |
1671 | int c; | |
1672 | ||
1673 | csum = 0; | |
1674 | bp = buf; | |
1675 | ||
1676 | while (1) | |
1677 | { | |
1678 | c = readchar (remote_timeout); | |
1679 | ||
1680 | switch (c) | |
1681 | { | |
1682 | case SERIAL_TIMEOUT: | |
1683 | if (remote_debug) | |
1684 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
1685 | return 0; | |
1686 | case '$': | |
1687 | if (remote_debug) | |
1688 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
1689 | return 0; /* Start a new packet, count retries */ | |
1690 | case '#': | |
1691 | { | |
1692 | unsigned char pktcsum; | |
1693 | ||
1694 | *bp = '\000'; | |
1695 | ||
205fc02b SC |
1696 | pktcsum = fromhex (readchar (remote_timeout)) << 4; |
1697 | pktcsum |= fromhex (readchar (remote_timeout)); | |
754e5da2 SG |
1698 | |
1699 | if (csum == pktcsum) | |
1700 | return 1; | |
1701 | ||
6c27841f SG |
1702 | if (remote_debug) |
1703 | { | |
1704 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
1705 | pktcsum, csum); | |
1706 | puts_filtered (buf); | |
1707 | puts_filtered ("\n"); | |
1708 | } | |
754e5da2 SG |
1709 | return 0; |
1710 | } | |
1711 | case '*': /* Run length encoding */ | |
284f4ee9 | 1712 | csum += c; |
754e5da2 SG |
1713 | c = readchar (remote_timeout); |
1714 | csum += c; | |
1715 | c = c - ' ' + 3; /* Compute repeat count */ | |
1716 | ||
6c27841f SG |
1717 | |
1718 | if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) | |
754e5da2 SG |
1719 | { |
1720 | memset (bp, *(bp - 1), c); | |
1721 | bp += c; | |
1722 | continue; | |
1723 | } | |
1724 | ||
1725 | *bp = '\0'; | |
1726 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
1727 | puts_filtered (buf); | |
1728 | puts_filtered ("\n"); | |
754e5da2 | 1729 | return 0; |
284f4ee9 | 1730 | |
754e5da2 SG |
1731 | default: |
1732 | if (bp < buf + PBUFSIZ - 1) | |
1733 | { | |
1734 | *bp++ = c; | |
1735 | csum += c; | |
1736 | continue; | |
1737 | } | |
1738 | ||
1739 | *bp = '\0'; | |
1740 | puts_filtered ("Remote packet too long: "); | |
1741 | puts_filtered (buf); | |
1742 | puts_filtered ("\n"); | |
1743 | ||
1744 | return 0; | |
1745 | } | |
1746 | } | |
1747 | } | |
1748 | ||
bd5635a1 | 1749 | /* Read a packet from the remote machine, with error checking, |
7c622b41 JG |
1750 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1751 | If FOREVER, wait forever rather than timing out; this is used | |
1752 | while the target is executing user code. */ | |
bd5635a1 | 1753 | |
dd0ce8f6 | 1754 | void |
754e5da2 SG |
1755 | getpkt (buf, forever) |
1756 | char *buf; | |
ebdb9ade | 1757 | int forever; |
bd5635a1 | 1758 | { |
754e5da2 SG |
1759 | int c; |
1760 | int tries; | |
1761 | int timeout; | |
1762 | int val; | |
94d4b713 | 1763 | |
45993f61 SC |
1764 | strcpy (buf,"timeout"); |
1765 | ||
754e5da2 | 1766 | if (forever) |
45993f61 SC |
1767 | { |
1768 | #ifdef MAINTENANCE_CMDS | |
1769 | timeout = watchdog > 0 ? watchdog : -1; | |
1770 | #else | |
1771 | timeout = -1; | |
1772 | #endif | |
1773 | } | |
1774 | ||
754e5da2 SG |
1775 | else |
1776 | timeout = remote_timeout; | |
bd5635a1 | 1777 | |
45993f61 | 1778 | #define MAX_TRIES 3 |
981a3309 | 1779 | |
754e5da2 SG |
1780 | for (tries = 1; tries <= MAX_TRIES; tries++) |
1781 | { | |
7c622b41 JG |
1782 | /* This can loop forever if the remote side sends us characters |
1783 | continuously, but if it pauses, we'll get a zero from readchar | |
1784 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 | 1785 | |
754e5da2 SG |
1786 | /* Note that we will only wait forever prior to the start of a packet. |
1787 | After that, we expect characters to arrive at a brisk pace. They | |
1788 | should show up within remote_timeout intervals. */ | |
6b27ebe8 | 1789 | |
754e5da2 | 1790 | do |
6b27ebe8 | 1791 | { |
754e5da2 | 1792 | c = readchar (timeout); |
7c622b41 | 1793 | |
ebdb9ade | 1794 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1795 | { |
45993f61 SC |
1796 | #ifdef MAINTENANCE_CMDS |
1797 | if (forever) /* Watchdog went off. Kill the target. */ | |
1798 | { | |
1799 | target_mourn_inferior (); | |
1800 | error ("Watchdog has expired. Target detached.\n"); | |
1801 | } | |
1802 | #endif | |
d0d8484a | 1803 | if (remote_debug) |
754e5da2 SG |
1804 | puts_filtered ("Timed out.\n"); |
1805 | goto retry; | |
7c622b41 | 1806 | } |
bd5635a1 | 1807 | } |
754e5da2 | 1808 | while (c != '$'); |
bd5635a1 | 1809 | |
754e5da2 | 1810 | /* We've found the start of a packet, now collect the data. */ |
38094c60 | 1811 | |
754e5da2 SG |
1812 | val = read_frame (buf); |
1813 | ||
1814 | if (val == 1) | |
38094c60 | 1815 | { |
754e5da2 | 1816 | if (remote_debug) |
dd0ce8f6 | 1817 | fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf); |
754e5da2 SG |
1818 | SERIAL_WRITE (remote_desc, "+", 1); |
1819 | return; | |
38094c60 | 1820 | } |
754e5da2 SG |
1821 | |
1822 | /* Try the whole thing again. */ | |
45993f61 | 1823 | retry: |
754e5da2 | 1824 | SERIAL_WRITE (remote_desc, "-", 1); |
bd5635a1 RP |
1825 | } |
1826 | ||
754e5da2 | 1827 | /* We have tried hard enough, and just can't receive the packet. Give up. */ |
7c622b41 | 1828 | |
754e5da2 | 1829 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
ebdb9ade | 1830 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
1831 | } |
1832 | \f | |
ebdb9ade JK |
1833 | static void |
1834 | remote_kill () | |
1835 | { | |
4cc1b3f7 JK |
1836 | /* For some mysterious reason, wait_for_inferior calls kill instead of |
1837 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
1838 | if (kill_kludge) | |
1839 | { | |
1840 | kill_kludge = 0; | |
1841 | target_mourn_inferior (); | |
1842 | return; | |
1843 | } | |
1844 | ||
1845 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
1846 | speaking terms with the remote system. */ | |
1847 | catch_errors (putpkt, "k", "", RETURN_MASK_ERROR); | |
1848 | ||
ebdb9ade JK |
1849 | /* Don't wait for it to die. I'm not really sure it matters whether |
1850 | we do or not. For the existing stubs, kill is a noop. */ | |
1851 | target_mourn_inferior (); | |
1852 | } | |
bd5635a1 | 1853 | |
ebdb9ade JK |
1854 | static void |
1855 | remote_mourn () | |
1856 | { | |
2b576293 C |
1857 | remote_mourn_1 (&remote_ops); |
1858 | } | |
1859 | ||
1860 | static void | |
1861 | extended_remote_mourn () | |
1862 | { | |
1863 | /* We do _not_ want to mourn the target like this; this will | |
1864 | remove the extended remote target from the target stack, | |
1865 | and the next time the user says "run" it'll fail. | |
1866 | ||
1867 | FIXME: What is the right thing to do here? */ | |
1868 | #if 0 | |
1869 | remote_mourn_1 (&extended_remote_ops); | |
1870 | #endif | |
1871 | } | |
1872 | ||
1873 | /* Worker function for remote_mourn. */ | |
1874 | static void | |
1875 | remote_mourn_1 (target) | |
1876 | struct target_ops *target; | |
1877 | { | |
1878 | unpush_target (target); | |
ebdb9ade JK |
1879 | generic_mourn_inferior (); |
1880 | } | |
2b576293 C |
1881 | |
1882 | /* In the extended protocol we want to be able to do things like | |
1883 | "run" and have them basically work as expected. So we need | |
1884 | a special create_inferior function. | |
1885 | ||
1886 | FIXME: One day add support for changing the exec file | |
1887 | we're debugging, arguments and an environment. */ | |
1888 | ||
1889 | static void | |
1890 | extended_remote_create_inferior (exec_file, args, env) | |
1891 | char *exec_file; | |
1892 | char *args; | |
1893 | char **env; | |
1894 | { | |
1895 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
1896 | the remote server. */ | |
1897 | remove_breakpoints (); | |
1898 | ||
1899 | /* Now restart the remote server. */ | |
1900 | extended_remote_restart (); | |
1901 | ||
1902 | /* Now put the breakpoints back in. This way we're safe if the | |
1903 | restart function works via a unix fork on the remote side. */ | |
1904 | insert_breakpoints (); | |
1905 | ||
1906 | /* Clean up from the last time we were running. */ | |
1907 | clear_proceed_status (); | |
1908 | ||
1909 | /* Let the remote process run. */ | |
1910 | proceed (-1, TARGET_SIGNAL_0, 0); | |
1911 | } | |
1912 | ||
ebdb9ade | 1913 | \f |
5af4f5f6 | 1914 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction |
fea17b55 SS |
1915 | than other targets; in those use REMOTE_BREAKPOINT instead of just |
1916 | BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT | |
1917 | and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call | |
1918 | the standard routines that are in mem-break.c. */ | |
1919 | ||
1920 | /* FIXME, these ought to be done in a more dynamic fashion. For instance, | |
1921 | the choice of breakpoint instruction affects target program design and | |
1922 | vice versa, and by making it user-tweakable, the special code here | |
1923 | goes away and we need fewer special GDB configurations. */ | |
1924 | ||
1925 | #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) | |
1926 | #define REMOTE_BREAKPOINT | |
1927 | #endif | |
1928 | ||
1929 | #ifdef REMOTE_BREAKPOINT | |
5af4f5f6 | 1930 | |
fea17b55 SS |
1931 | /* If the target isn't bi-endian, just pretend it is. */ |
1932 | #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) | |
1933 | #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1934 | #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1935 | #endif | |
5af4f5f6 | 1936 | |
fea17b55 SS |
1937 | static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; |
1938 | static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; | |
5af4f5f6 | 1939 | |
fea17b55 | 1940 | #endif /* REMOTE_BREAKPOINT */ |
5af4f5f6 JK |
1941 | |
1942 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1943 | support. We read the contents of the target location and stash it, | |
1944 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1945 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1946 | memory allocated for saving the target contents. It is guaranteed | |
1947 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1948 | is accomplished via BREAKPOINT_MAX). */ | |
1949 | ||
d538b510 | 1950 | static int |
5af4f5f6 JK |
1951 | remote_insert_breakpoint (addr, contents_cache) |
1952 | CORE_ADDR addr; | |
1953 | char *contents_cache; | |
1954 | { | |
fea17b55 | 1955 | #ifdef REMOTE_BREAKPOINT |
5af4f5f6 JK |
1956 | int val; |
1957 | ||
fea17b55 | 1958 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); |
5af4f5f6 JK |
1959 | |
1960 | if (val == 0) | |
fea17b55 SS |
1961 | { |
1962 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
1963 | val = target_write_memory (addr, (char *) big_break_insn, | |
1964 | sizeof big_break_insn); | |
1965 | else | |
1966 | val = target_write_memory (addr, (char *) little_break_insn, | |
1967 | sizeof little_break_insn); | |
1968 | } | |
5af4f5f6 JK |
1969 | |
1970 | return val; | |
fea17b55 SS |
1971 | #else |
1972 | return memory_insert_breakpoint (addr, contents_cache); | |
1973 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 JK |
1974 | } |
1975 | ||
d538b510 | 1976 | static int |
5af4f5f6 JK |
1977 | remote_remove_breakpoint (addr, contents_cache) |
1978 | CORE_ADDR addr; | |
1979 | char *contents_cache; | |
1980 | { | |
fea17b55 SS |
1981 | #ifdef REMOTE_BREAKPOINT |
1982 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); | |
1983 | #else | |
1984 | return memory_remove_breakpoint (addr, contents_cache); | |
1985 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 | 1986 | } |
2b576293 | 1987 | |
0a325463 SG |
1988 | /* Some targets are only capable of doing downloads, and afterwards they switch |
1989 | to the remote serial protocol. This function provides a clean way to get | |
1990 | from the download target to the remote target. It's basically just a | |
1991 | wrapper so that we don't have to expose any of the internal workings of | |
1992 | remote.c. | |
1993 | ||
1994 | Prior to calling this routine, you should shutdown the current target code, | |
1995 | else you will get the "A program is being debugged already..." message. | |
1996 | Usually a call to pop_target() suffices. | |
1997 | */ | |
1998 | ||
1999 | void | |
2000 | push_remote_target (name, from_tty) | |
2001 | char *name; | |
2002 | int from_tty; | |
2003 | { | |
2004 | printf_filtered ("Switching to remote protocol\n"); | |
2005 | remote_open (name, from_tty); | |
2006 | } | |
2007 | ||
dd0ce8f6 AC |
2008 | /* Other targets want to use the entire remote serial module but with |
2009 | certain remote_ops overridden. */ | |
2010 | ||
2011 | void | |
2012 | open_remote_target (name, from_tty, target, extended_p) | |
2013 | char *name; | |
2014 | int from_tty; | |
2015 | struct target_ops *target; | |
2016 | int extended_p; | |
2017 | { | |
2018 | printf_filtered ("Selecting the %sremote protocol\n", | |
2019 | (extended_p ? "extended-" : "")); | |
2020 | remote_open_1 (name, from_tty, target, extended_p); | |
2021 | } | |
2022 | ||
c719b714 | 2023 | |
bd5635a1 RP |
2024 | void |
2025 | _initialize_remote () | |
2026 | { | |
c719b714 | 2027 | init_remote_ops() ; |
3ff4b4c6 | 2028 | init_extended_remote_ops() ; |
bd5635a1 | 2029 | add_target (&remote_ops); |
2b576293 | 2030 | add_target (&extended_remote_ops); |
2c441e7f SS |
2031 | |
2032 | add_show_from_set (add_set_cmd ("remotetimeout", no_class, | |
2033 | var_integer, (char *)&remote_timeout, | |
2034 | "Set timeout value for remote read.\n", &setlist), | |
2035 | &showlist); | |
b52cac6b FF |
2036 | |
2037 | add_show_from_set (add_set_cmd ("remotebreak", no_class, | |
2038 | var_integer, (char *)&remote_break, | |
2039 | "Set whether to send break if interrupted.\n", &setlist), | |
2040 | &showlist); | |
fea17b55 SS |
2041 | |
2042 | add_show_from_set (add_set_cmd ("remotewritesize", no_class, | |
2043 | var_integer, (char *)&remote_write_size, | |
2044 | "Set the maximum number of bytes in each memory write packet.\n", &setlist), | |
2045 | &showlist); | |
976bb0be | 2046 | } |