Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
94d4b713 | 2 | Copyright 1988, 1991, 1992, 1993, 1994 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 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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: | |
bd5635a1 RP |
41 | All values are encoded in ascii hex digits. |
42 | ||
43 | Request Packet | |
44 | ||
45 | read registers g | |
46 | reply XX....X Each byte of register data | |
47 | is described by two hex digits. | |
48 | Registers are in the internal order | |
49 | for GDB, and the bytes in a register | |
50 | are in the same order the machine uses. | |
51 | or ENN for an error. | |
52 | ||
53 | write regs GXX..XX Each byte of register data | |
54 | is described by two hex digits. | |
55 | reply OK for success | |
56 | ENN for an error | |
57 | ||
0c993550 | 58 | write reg Pn...=r... Write register n... with value r..., |
4aa6fe10 JK |
59 | which contains two hex digits for each |
60 | byte in the register (target byte | |
61 | order). | |
62 | reply OK for success | |
63 | ENN for an error | |
64 | (not supported by all stubs). | |
65 | ||
bd5635a1 RP |
66 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
67 | reply XX..XX XX..XX is mem contents | |
d538b510 RP |
68 | Can be fewer bytes than requested |
69 | if able to read only part of the data. | |
bd5635a1 RP |
70 | or ENN NN is errno |
71 | ||
72 | write mem MAA..AA,LLLL:XX..XX | |
73 | AA..AA is address, | |
74 | LLLL is number of bytes, | |
75 | XX..XX is data | |
76 | reply OK for success | |
d538b510 RP |
77 | ENN for an error (this includes the case |
78 | where only part of the data was | |
79 | written). | |
bd5635a1 RP |
80 | |
81 | cont cAA..AA AA..AA is address to resume | |
82 | If AA..AA is omitted, | |
83 | resume at same address. | |
84 | ||
85 | step sAA..AA AA..AA is address to resume | |
86 | If AA..AA is omitted, | |
87 | resume at same address. | |
88 | ||
89 | last signal ? Reply the current reason for stopping. | |
90 | This is the same reply as is generated | |
91 | for step or cont : SAA where AA is the | |
92 | signal number. | |
93 | ||
94 | There is no immediate reply to step or cont. | |
95 | The reply comes when the machine stops. | |
96 | It is SAA AA is the "signal number" | |
97 | ||
e50ebec8 JK |
98 | or... TAAn...:r...;n:r...;n...:r...; |
99 | AA = signal number | |
100 | n... = register number | |
101 | r... = register contents | |
72bba93b | 102 | or... WAA The process exited, and AA is |
758aeb93 ILT |
103 | the exit status. This is only |
104 | applicable for certains sorts of | |
105 | targets. | |
d538b510 RP |
106 | kill request k |
107 | ||
108 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
109 | reset r reset -- see sparc stub. | |
110 | reserved <other> On other requests, the stub should | |
111 | ignore the request and send an empty | |
112 | response ($#<checksum>). This way | |
113 | we can extend the protocol and GDB | |
114 | can tell whether the stub it is | |
115 | talking to uses the old or the new. | |
72bba93b | 116 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
117 | AA for a match with pattern PP and |
118 | mask MM. PP and MM are 4 bytes. | |
119 | Not supported by all stubs. | |
120 | ||
72bba93b SG |
121 | general query qXXXX Request info about XXXX. |
122 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
123 | query sect offs qOffsets Get section offsets. Reply is | |
124 | Text=xxx;Data=yyy;Bss=zzz | |
72bba93b | 125 | |
94d4b713 JK |
126 | Responses can be run-length encoded to save space. A '*' means that |
127 | the next two characters are hex digits giving a repeat count which | |
128 | stands for that many repititions of the character preceding the '*'. | |
129 | Note that this means that responses cannot contain '*'. Example: | |
4aa6fe10 | 130 | "0*03" means the same as "0000". */ |
bd5635a1 | 131 | |
d747e0af | 132 | #include "defs.h" |
bd5635a1 RP |
133 | #include <string.h> |
134 | #include <fcntl.h> | |
bd5635a1 RP |
135 | #include "frame.h" |
136 | #include "inferior.h" | |
e50ebec8 | 137 | #include "bfd.h" |
6b27ebe8 | 138 | #include "symfile.h" |
bd5635a1 RP |
139 | #include "target.h" |
140 | #include "wait.h" | |
141 | #include "terminal.h" | |
8f86a4e4 | 142 | #include "gdbcmd.h" |
758aeb93 ILT |
143 | #include "objfiles.h" |
144 | #include "gdb-stabs.h" | |
bd5635a1 | 145 | |
d538b510 RP |
146 | #include "dcache.h" |
147 | ||
8f86a4e4 | 148 | #if !defined(DONT_USE_REMOTE) |
bd5635a1 RP |
149 | #ifdef USG |
150 | #include <sys/types.h> | |
151 | #endif | |
152 | ||
153 | #include <signal.h> | |
ebdb9ade | 154 | #include "serial.h" |
bd5635a1 | 155 | |
b543979c JG |
156 | /* Prototypes for local functions */ |
157 | ||
d538b510 RP |
158 | static int |
159 | remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len)); | |
b543979c | 160 | |
d538b510 RP |
161 | static int |
162 | remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len)); | |
b543979c JG |
163 | |
164 | static void | |
5af4f5f6 | 165 | remote_files_info PARAMS ((struct target_ops *ignore)); |
b543979c JG |
166 | |
167 | static int | |
5af4f5f6 JK |
168 | remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, |
169 | int should_write, struct target_ops *target)); | |
b543979c JG |
170 | |
171 | static void | |
172 | remote_prepare_to_store PARAMS ((void)); | |
173 | ||
174 | static void | |
5af4f5f6 | 175 | remote_fetch_registers PARAMS ((int regno)); |
b543979c JG |
176 | |
177 | static void | |
94d4b713 | 178 | remote_resume PARAMS ((int pid, int step, enum target_signal siggnal)); |
b543979c | 179 | |
7c622b41 | 180 | static int |
5af4f5f6 | 181 | remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 182 | |
b543979c | 183 | static void |
5af4f5f6 | 184 | remote_open PARAMS ((char *name, int from_tty)); |
b543979c JG |
185 | |
186 | static void | |
5af4f5f6 | 187 | remote_close PARAMS ((int quitting)); |
b543979c JG |
188 | |
189 | static void | |
5af4f5f6 | 190 | remote_store_registers PARAMS ((int regno)); |
b543979c JG |
191 | |
192 | static void | |
5af4f5f6 | 193 | getpkt PARAMS ((char *buf, int forever)); |
b543979c JG |
194 | |
195 | static void | |
5af4f5f6 | 196 | putpkt PARAMS ((char *buf)); |
b543979c JG |
197 | |
198 | static void | |
5af4f5f6 | 199 | remote_send PARAMS ((char *buf)); |
b543979c JG |
200 | |
201 | static int | |
202 | readchar PARAMS ((void)); | |
203 | ||
94d4b713 | 204 | static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); |
b543979c JG |
205 | |
206 | static int | |
5af4f5f6 | 207 | tohex PARAMS ((int nib)); |
b543979c JG |
208 | |
209 | static int | |
5af4f5f6 | 210 | fromhex PARAMS ((int a)); |
b543979c JG |
211 | |
212 | static void | |
5af4f5f6 JK |
213 | remote_detach PARAMS ((char *args, int from_tty)); |
214 | ||
215 | static void | |
216 | remote_interrupt PARAMS ((int signo)); | |
217 | ||
218 | static void | |
219 | remote_interrupt_twice PARAMS ((int signo)); | |
b543979c | 220 | |
981a3309 SG |
221 | static void |
222 | interrupt_query PARAMS ((void)); | |
223 | ||
bd5635a1 RP |
224 | extern struct target_ops remote_ops; /* Forward decl */ |
225 | ||
ebdb9ade JK |
226 | /* This was 5 seconds, which is a long time to sit and wait. |
227 | Unless this is going though some terminal server or multiplexer or | |
228 | other form of hairy serial connection, I would think 2 seconds would | |
229 | be plenty. */ | |
230 | static int timeout = 2; | |
bd5635a1 RP |
231 | |
232 | #if 0 | |
233 | int icache; | |
234 | #endif | |
235 | ||
16e1d1d3 | 236 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
237 | remote_open knows that we don't have a file open when the program |
238 | starts. */ | |
ebdb9ade | 239 | serial_t remote_desc = NULL; |
bd5635a1 | 240 | |
4d57c599 JK |
241 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
242 | and i386-stub.c. Normally, no one would notice because it only matters | |
243 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
244 | to be more than 400 if required to hold the registers (see below, where | |
245 | we round it up based on REGISTER_BYTES). */ | |
246 | #define PBUFSIZ 400 | |
bd5635a1 RP |
247 | |
248 | /* Maximum number of bytes to read/write at once. The value here | |
249 | is chosen to fill up a packet (the headers account for the 32). */ | |
250 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
251 | ||
b543979c | 252 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
253 | /* The blank line after the #if seems to be required to work around a |
254 | bug in HP's PA compiler. */ | |
b543979c | 255 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
256 | |
257 | #undef PBUFSIZ | |
b543979c | 258 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 259 | #endif |
4aa6fe10 JK |
260 | |
261 | /* Should we try the 'P' request? If this is set to one when the stub | |
262 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
263 | static int stub_supports_P = 1; | |
264 | ||
bd5635a1 | 265 | \f |
bd5635a1 RP |
266 | /* Clean up connection to a remote debugger. */ |
267 | ||
e1ce8aa5 | 268 | /* ARGSUSED */ |
b543979c | 269 | static void |
bd5635a1 RP |
270 | remote_close (quitting) |
271 | int quitting; | |
272 | { | |
ebdb9ade JK |
273 | if (remote_desc) |
274 | SERIAL_CLOSE (remote_desc); | |
275 | remote_desc = NULL; | |
b543979c JG |
276 | } |
277 | ||
72bba93b SG |
278 | /* Query the remote side for the text, data and bss offsets. */ |
279 | ||
280 | static void | |
281 | get_offsets () | |
282 | { | |
1c95d7ab | 283 | unsigned char buf[PBUFSIZ]; |
72bba93b SG |
284 | int nvals; |
285 | CORE_ADDR text_addr, data_addr, bss_addr; | |
286 | struct section_offsets *offs; | |
287 | ||
288 | putpkt ("qOffsets"); | |
289 | ||
1c95d7ab | 290 | getpkt (buf, 0); |
72bba93b | 291 | |
1c95d7ab JK |
292 | if (buf[0] == '\000') |
293 | return; /* Return silently. Stub doesn't support this | |
294 | command. */ | |
72bba93b SG |
295 | if (buf[0] == 'E') |
296 | { | |
297 | warning ("Remote failure reply: %s", buf); | |
298 | return; | |
299 | } | |
300 | ||
301 | nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr, | |
302 | &bss_addr); | |
303 | if (nvals != 3) | |
304 | error ("Malformed response to offset query, %s", buf); | |
305 | ||
306 | if (symfile_objfile == NULL) | |
307 | return; | |
308 | ||
309 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
310 | + symfile_objfile->num_sections | |
311 | * sizeof (offs->offsets)); | |
312 | memcpy (offs, symfile_objfile->section_offsets, | |
313 | sizeof (struct section_offsets) | |
314 | + symfile_objfile->num_sections | |
315 | * sizeof (offs->offsets)); | |
316 | ||
4aa6fe10 JK |
317 | /* FIXME: This code assumes gdb-stabs.h is being used; it's broken |
318 | for xcoff, dwarf, sdb-coff, etc. But there is no simple | |
319 | canonical representation for this stuff. (Just what does "text" | |
320 | as seen by the stub mean, anyway? I think it means all sections | |
321 | with SEC_CODE set, but we currently have no way to deal with that). */ | |
322 | ||
72bba93b | 323 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; |
1624c38f SG |
324 | |
325 | /* This is a temporary kludge to force data and bss to use the same offsets | |
326 | because that's what nlmconv does now. The real solution requires changes | |
327 | to the stub and remote.c that I don't have time to do right now. */ | |
328 | ||
72bba93b | 329 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 330 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
331 | |
332 | objfile_relocate (symfile_objfile, offs); | |
333 | } | |
334 | ||
7c622b41 JG |
335 | /* Stub for catch_errors. */ |
336 | ||
337 | static int | |
338 | remote_start_remote (dummy) | |
339 | char *dummy; | |
340 | { | |
ac7a377f JK |
341 | immediate_quit = 1; /* Allow user to interrupt it */ |
342 | ||
7c622b41 | 343 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
344 | |
345 | SERIAL_WRITE (remote_desc, "+", 1); | |
346 | ||
347 | get_offsets (); /* Get text, data & bss offsets */ | |
348 | ||
7c622b41 | 349 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 350 | immediate_quit = 0; |
7c622b41 JG |
351 | |
352 | start_remote (); /* Initialize gdb process mechanisms */ | |
72bba93b | 353 | |
7c622b41 JG |
354 | return 1; |
355 | } | |
356 | ||
bd5635a1 RP |
357 | /* Open a connection to a remote debugger. |
358 | NAME is the filename used for communication. */ | |
359 | ||
d538b510 RP |
360 | static DCACHE *remote_dcache; |
361 | ||
b543979c | 362 | static void |
bd5635a1 RP |
363 | remote_open (name, from_tty) |
364 | char *name; | |
365 | int from_tty; | |
366 | { | |
bd5635a1 RP |
367 | if (name == 0) |
368 | error ( | |
369 | "To open a remote debug connection, you need to specify what serial\n\ | |
370 | device is attached to the remote system (e.g. /dev/ttya)."); | |
371 | ||
f2fc6e7a JK |
372 | target_preopen (from_tty); |
373 | ||
ebdb9ade | 374 | unpush_target (&remote_ops); |
bd5635a1 | 375 | |
d538b510 | 376 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 377 | |
ebdb9ade JK |
378 | remote_desc = SERIAL_OPEN (name); |
379 | if (!remote_desc) | |
bd5635a1 RP |
380 | perror_with_name (name); |
381 | ||
94d4b713 | 382 | if (baud_rate != -1) |
b543979c | 383 | { |
94d4b713 JK |
384 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
385 | { | |
386 | SERIAL_CLOSE (remote_desc); | |
387 | perror_with_name (name); | |
388 | } | |
b543979c | 389 | } |
ebdb9ade JK |
390 | |
391 | SERIAL_RAW (remote_desc); | |
bd5635a1 | 392 | |
e15f2a54 JK |
393 | /* If there is something sitting in the buffer we might take it as a |
394 | response to a command, which would be bad. */ | |
395 | SERIAL_FLUSH_INPUT (remote_desc); | |
396 | ||
bd5635a1 | 397 | if (from_tty) |
7c622b41 JG |
398 | { |
399 | puts_filtered ("Remote debugging using "); | |
400 | puts_filtered (name); | |
401 | puts_filtered ("\n"); | |
402 | } | |
bd5635a1 | 403 | push_target (&remote_ops); /* Switch to using remote target now */ |
bd5635a1 | 404 | |
4aa6fe10 JK |
405 | /* Start out by trying the 'P' request to set registers. We set this each |
406 | time that we open a new target so that if the user switches from one | |
407 | stub to another, we can (if the target is closed and reopened) cope. */ | |
408 | stub_supports_P = 1; | |
409 | ||
ac7a377f JK |
410 | /* Start the remote connection; if error (0), discard this target. |
411 | In particular, if the user quits, be sure to discard it | |
412 | (we'd be in an inconsistent state otherwise). */ | |
7c622b41 | 413 | if (!catch_errors (remote_start_remote, (char *)0, |
e50ebec8 | 414 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
7c622b41 | 415 | pop_target(); |
bd5635a1 RP |
416 | } |
417 | ||
418 | /* remote_detach() | |
419 | takes a program previously attached to and detaches it. | |
420 | We better not have left any breakpoints | |
421 | in the program or it'll die when it hits one. | |
422 | Close the open connection to the remote debugger. | |
423 | Use this when you want to detach and do something else | |
424 | with your gdb. */ | |
425 | ||
426 | static void | |
427 | remote_detach (args, from_tty) | |
428 | char *args; | |
429 | int from_tty; | |
430 | { | |
431 | if (args) | |
432 | error ("Argument given to \"detach\" when remotely debugging."); | |
433 | ||
434 | pop_target (); | |
435 | if (from_tty) | |
7c622b41 | 436 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
437 | } |
438 | ||
439 | /* Convert hex digit A to a number. */ | |
440 | ||
441 | static int | |
442 | fromhex (a) | |
443 | int a; | |
444 | { | |
445 | if (a >= '0' && a <= '9') | |
446 | return a - '0'; | |
447 | else if (a >= 'a' && a <= 'f') | |
448 | return a - 'a' + 10; | |
449 | else | |
450 | error ("Reply contains invalid hex digit"); | |
451 | return -1; | |
452 | } | |
453 | ||
454 | /* Convert number NIB to a hex digit. */ | |
455 | ||
456 | static int | |
457 | tohex (nib) | |
458 | int nib; | |
459 | { | |
460 | if (nib < 10) | |
461 | return '0'+nib; | |
462 | else | |
463 | return 'a'+nib-10; | |
464 | } | |
465 | \f | |
466 | /* Tell the remote machine to resume. */ | |
467 | ||
b543979c | 468 | static void |
d538b510 | 469 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
470 | int pid, step; |
471 | enum target_signal siggnal; | |
bd5635a1 RP |
472 | { |
473 | char buf[PBUFSIZ]; | |
474 | ||
475 | if (siggnal) | |
ebdb9ade | 476 | { |
ebdb9ade | 477 | target_terminal_ours_for_output (); |
94d4b713 JK |
478 | printf_filtered |
479 | ("Can't send signals to a remote system. %s not sent.\n", | |
480 | target_signal_to_name (siggnal)); | |
ebdb9ade JK |
481 | target_terminal_inferior (); |
482 | } | |
bd5635a1 | 483 | |
d538b510 | 484 | dcache_flush (remote_dcache); |
bd5635a1 RP |
485 | |
486 | strcpy (buf, step ? "s": "c"); | |
487 | ||
488 | putpkt (buf); | |
489 | } | |
ebdb9ade | 490 | \f |
b543979c JG |
491 | /* Send ^C to target to halt it. Target will respond, and send us a |
492 | packet. */ | |
493 | ||
5af4f5f6 JK |
494 | static void |
495 | remote_interrupt (signo) | |
e676a15f | 496 | int signo; |
b543979c | 497 | { |
ebdb9ade JK |
498 | /* If this doesn't work, try more severe steps. */ |
499 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 500 | |
d0d8484a | 501 | if (remote_debug) |
199b2450 | 502 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 503 | |
ebdb9ade | 504 | SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */ |
b543979c JG |
505 | } |
506 | ||
5af4f5f6 JK |
507 | static void (*ofunc)(); |
508 | ||
ebdb9ade JK |
509 | /* The user typed ^C twice. */ |
510 | static void | |
511 | remote_interrupt_twice (signo) | |
512 | int signo; | |
513 | { | |
514 | signal (signo, ofunc); | |
515 | ||
981a3309 SG |
516 | interrupt_query (); |
517 | ||
518 | signal (signo, remote_interrupt); | |
519 | } | |
520 | ||
521 | /* Ask the user what to do when an interrupt is received. */ | |
522 | ||
523 | static void | |
524 | interrupt_query () | |
525 | { | |
ebdb9ade | 526 | target_terminal_ours (); |
981a3309 | 527 | |
6b27ebe8 | 528 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
529 | Give up (and stop debugging it)? ")) |
530 | { | |
531 | target_mourn_inferior (); | |
e50ebec8 | 532 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 533 | } |
981a3309 SG |
534 | |
535 | target_terminal_inferior (); | |
ebdb9ade | 536 | } |
b543979c | 537 | |
bd5635a1 | 538 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
539 | storing status in STATUS just as `wait' would. |
540 | Returns "pid" (though it's not clear what, if anything, that | |
541 | means in the case of this target). */ | |
bd5635a1 | 542 | |
b543979c | 543 | static int |
d0d8484a SG |
544 | remote_wait (pid, status) |
545 | int pid; | |
94d4b713 | 546 | struct target_waitstatus *status; |
bd5635a1 RP |
547 | { |
548 | unsigned char buf[PBUFSIZ]; | |
8f86a4e4 | 549 | |
94d4b713 JK |
550 | status->kind = TARGET_WAITKIND_EXITED; |
551 | status->value.integer = 0; | |
b543979c | 552 | |
4f8a48e5 | 553 | while (1) |
8f86a4e4 | 554 | { |
4f8a48e5 | 555 | unsigned char *p; |
a03d4f8e | 556 | |
4f8a48e5 ILT |
557 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
558 | getpkt ((char *) buf, 1); | |
559 | signal (SIGINT, ofunc); | |
4ecee2f9 | 560 | |
4f8a48e5 ILT |
561 | if (buf[0] == 'E') |
562 | warning ("Remote failure reply: %s", buf); | |
563 | else if (buf[0] == 'T') | |
8f86a4e4 | 564 | { |
4f8a48e5 ILT |
565 | int i; |
566 | long regno; | |
567 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 568 | |
4f8a48e5 ILT |
569 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
570 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
571 | ss = signal number | |
572 | n... = register number | |
573 | r... = register contents | |
574 | */ | |
5af4f5f6 | 575 | |
4f8a48e5 | 576 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 577 | |
4f8a48e5 ILT |
578 | while (*p) |
579 | { | |
580 | unsigned char *p1; | |
5af4f5f6 | 581 | |
4f8a48e5 | 582 | regno = strtol (p, &p1, 16); /* Read the register number */ |
5af4f5f6 | 583 | |
4f8a48e5 ILT |
584 | if (p1 == p) |
585 | warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n", | |
586 | p1, buf); | |
4ecee2f9 | 587 | |
4f8a48e5 | 588 | p = p1; |
4ecee2f9 | 589 | |
4f8a48e5 ILT |
590 | if (*p++ != ':') |
591 | warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n", | |
592 | p, buf); | |
a03d4f8e | 593 | |
4f8a48e5 ILT |
594 | if (regno >= NUM_REGS) |
595 | warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n", | |
596 | regno, p, buf); | |
597 | ||
598 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
599 | { | |
600 | if (p[0] == 0 || p[1] == 0) | |
601 | warning ("Remote reply is too short: %s", buf); | |
602 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
603 | p += 2; | |
604 | } | |
605 | ||
606 | if (*p++ != ';') | |
607 | warning ("Remote register badly formatted: %s", buf); | |
608 | ||
609 | supply_register (regno, regs); | |
610 | } | |
611 | break; | |
8f86a4e4 | 612 | } |
4f8a48e5 ILT |
613 | else if (buf[0] == 'W') |
614 | { | |
615 | /* The remote process exited. */ | |
94d4b713 JK |
616 | status->kind = TARGET_WAITKIND_EXITED; |
617 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
4f8a48e5 ILT |
618 | return 0; |
619 | } | |
620 | else if (buf[0] == 'S') | |
621 | break; | |
622 | else | |
623 | warning ("Invalid remote reply: %s", buf); | |
758aeb93 | 624 | } |
8f86a4e4 | 625 | |
94d4b713 JK |
626 | status->kind = TARGET_WAITKIND_STOPPED; |
627 | status->value.sig = (enum target_signal) | |
628 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
8f86a4e4 | 629 | |
e1ce8aa5 | 630 | return 0; |
bd5635a1 RP |
631 | } |
632 | ||
55fea07b JK |
633 | /* Number of bytes of registers this stub implements. */ |
634 | static int register_bytes_found; | |
635 | ||
bd5635a1 | 636 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
637 | /* Currently we just read all the registers, so we don't use regno. */ |
638 | /* ARGSUSED */ | |
b543979c | 639 | static void |
bd5635a1 RP |
640 | remote_fetch_registers (regno) |
641 | int regno; | |
642 | { | |
643 | char buf[PBUFSIZ]; | |
644 | int i; | |
645 | char *p; | |
646 | char regs[REGISTER_BYTES]; | |
647 | ||
648 | sprintf (buf, "g"); | |
649 | remote_send (buf); | |
650 | ||
55fea07b JK |
651 | /* Unimplemented registers read as all bits zero. */ |
652 | memset (regs, 0, REGISTER_BYTES); | |
653 | ||
981a3309 SG |
654 | /* We can get out of synch in various cases. If the first character |
655 | in the buffer is not a hex character, assume that has happened | |
656 | and try to fetch another packet to read. */ | |
657 | while ((buf[0] < '0' || buf[0] > '9') | |
658 | && (buf[0] < 'a' || buf[0] > 'f')) | |
659 | { | |
d0d8484a | 660 | if (remote_debug) |
199b2450 | 661 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
662 | getpkt (buf, 0); |
663 | } | |
664 | ||
bd5635a1 RP |
665 | /* Reply describes registers byte by byte, each byte encoded as two |
666 | hex characters. Suck them all up, then supply them to the | |
667 | register cacheing/storage mechanism. */ | |
668 | ||
669 | p = buf; | |
670 | for (i = 0; i < REGISTER_BYTES; i++) | |
671 | { | |
55fea07b JK |
672 | if (p[0] == 0) |
673 | break; | |
674 | if (p[1] == 0) | |
675 | { | |
676 | warning ("Remote reply is of odd length: %s", buf); | |
677 | /* Don't change register_bytes_found in this case, and don't | |
678 | print a second warning. */ | |
679 | goto supply_them; | |
680 | } | |
bd5635a1 RP |
681 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
682 | p += 2; | |
683 | } | |
55fea07b JK |
684 | |
685 | if (i != register_bytes_found) | |
686 | { | |
687 | register_bytes_found = i; | |
688 | #ifdef REGISTER_BYTES_OK | |
689 | if (!REGISTER_BYTES_OK (i)) | |
690 | warning ("Remote reply is too short: %s", buf); | |
691 | #endif | |
692 | } | |
693 | ||
694 | supply_them: | |
bd5635a1 RP |
695 | for (i = 0; i < NUM_REGS; i++) |
696 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
697 | } |
698 | ||
4aa6fe10 JK |
699 | /* Prepare to store registers. Since we may send them all (using a |
700 | 'G' request), we have to read out the ones we don't want to change | |
701 | first. */ | |
bd5635a1 | 702 | |
b543979c | 703 | static void |
bd5635a1 RP |
704 | remote_prepare_to_store () |
705 | { | |
34517ebc JG |
706 | /* Make sure the entire registers array is valid. */ |
707 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
708 | } |
709 | ||
4aa6fe10 JK |
710 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
711 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 712 | |
b543979c | 713 | static void |
bd5635a1 RP |
714 | remote_store_registers (regno) |
715 | int regno; | |
716 | { | |
717 | char buf[PBUFSIZ]; | |
718 | int i; | |
719 | char *p; | |
720 | ||
4aa6fe10 JK |
721 | if (regno >= 0 && stub_supports_P) |
722 | { | |
723 | /* Try storing a single register. */ | |
724 | char *regp; | |
725 | ||
0c993550 | 726 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
727 | p = buf + strlen (buf); |
728 | regp = ®isters[REGISTER_BYTE (regno)]; | |
729 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
730 | { | |
731 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
732 | *p++ = tohex (regp[i] & 0xf); | |
733 | } | |
734 | *p = '\0'; | |
735 | remote_send (buf); | |
736 | if (buf[0] != '\0') | |
737 | { | |
738 | /* The stub understands the 'P' request. We are done. */ | |
739 | return; | |
740 | } | |
741 | ||
742 | /* The stub does not support the 'P' request. Use 'G' instead, | |
743 | and don't try using 'P' in the future (it will just waste our | |
744 | time). */ | |
745 | stub_supports_P = 0; | |
746 | } | |
747 | ||
bd5635a1 | 748 | buf[0] = 'G'; |
4aa6fe10 | 749 | |
bd5635a1 RP |
750 | /* Command describes registers byte by byte, |
751 | each byte encoded as two hex characters. */ | |
752 | ||
753 | p = buf + 1; | |
55fea07b JK |
754 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
755 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
756 | { |
757 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
758 | *p++ = tohex (registers[i] & 0xf); | |
759 | } | |
760 | *p = '\0'; | |
761 | ||
762 | remote_send (buf); | |
bd5635a1 RP |
763 | } |
764 | ||
b43e0347 JK |
765 | #if 0 |
766 | ||
767 | /* Use of the data cache is disabled because it loses for looking at | |
768 | and changing hardware I/O ports and the like. Accepting `volatile' | |
769 | would perhaps be one way to fix it, but a better way which would | |
770 | win for more cases would be to use the executable file for the text | |
771 | segment, like the `icache' code below but done cleanly (in some | |
772 | target-independent place, perhaps in target_xfer_memory, perhaps | |
773 | based on assigning each target a speed or perhaps by some simpler | |
774 | mechanism). */ | |
775 | ||
bd5635a1 RP |
776 | /* Read a word from remote address ADDR and return it. |
777 | This goes through the data cache. */ | |
778 | ||
b43e0347 | 779 | static int |
bd5635a1 RP |
780 | remote_fetch_word (addr) |
781 | CORE_ADDR addr; | |
782 | { | |
d538b510 | 783 | #if 0 |
bd5635a1 RP |
784 | if (icache) |
785 | { | |
786 | extern CORE_ADDR text_start, text_end; | |
787 | ||
788 | if (addr >= text_start && addr < text_end) | |
789 | { | |
790 | int buffer; | |
791 | xfer_core_file (addr, &buffer, sizeof (int)); | |
792 | return buffer; | |
793 | } | |
794 | } | |
d538b510 RP |
795 | #endif |
796 | return dcache_fetch (remote_dcache, addr); | |
bd5635a1 RP |
797 | } |
798 | ||
799 | /* Write a word WORD into remote address ADDR. | |
800 | This goes through the data cache. */ | |
801 | ||
b43e0347 | 802 | static void |
bd5635a1 RP |
803 | remote_store_word (addr, word) |
804 | CORE_ADDR addr; | |
805 | int word; | |
806 | { | |
d538b510 | 807 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 808 | } |
b43e0347 | 809 | #endif /* 0 */ |
bd5635a1 RP |
810 | \f |
811 | /* Write memory data directly to the remote machine. | |
812 | This does not inform the data cache; the data cache uses this. | |
813 | MEMADDR is the address in the remote memory space. | |
814 | MYADDR is the address of the buffer in our space. | |
d538b510 | 815 | LEN is the number of bytes. |
bd5635a1 | 816 | |
d538b510 RP |
817 | Returns number of bytes transferred, or 0 for error. */ |
818 | ||
819 | static int | |
bd5635a1 RP |
820 | remote_write_bytes (memaddr, myaddr, len) |
821 | CORE_ADDR memaddr; | |
d538b510 | 822 | unsigned char *myaddr; |
bd5635a1 RP |
823 | int len; |
824 | { | |
825 | char buf[PBUFSIZ]; | |
826 | int i; | |
827 | char *p; | |
828 | ||
d24c0599 JK |
829 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
830 | result in a buffer like sprintf. */ | |
4aa6fe10 | 831 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len); |
bd5635a1 | 832 | |
b543979c | 833 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
834 | each byte encoded as two hex characters. */ |
835 | ||
836 | p = buf + strlen (buf); | |
837 | for (i = 0; i < len; i++) | |
838 | { | |
839 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
840 | *p++ = tohex (myaddr[i] & 0xf); | |
841 | } | |
842 | *p = '\0'; | |
843 | ||
d538b510 RP |
844 | putpkt (buf); |
845 | getpkt (buf, 0); | |
846 | ||
847 | if (buf[0] == 'E') | |
848 | { | |
849 | /* There is no correspondance between what the remote protocol uses | |
850 | for errors and errno codes. We would like a cleaner way of | |
851 | representing errors (big enough to include errno codes, bfd_error | |
852 | codes, and others). But for now just return EIO. */ | |
853 | errno = EIO; | |
854 | return 0; | |
855 | } | |
856 | return len; | |
bd5635a1 RP |
857 | } |
858 | ||
859 | /* Read memory data directly from the remote machine. | |
860 | This does not use the data cache; the data cache uses this. | |
861 | MEMADDR is the address in the remote memory space. | |
862 | MYADDR is the address of the buffer in our space. | |
d538b510 | 863 | LEN is the number of bytes. |
bd5635a1 | 864 | |
d538b510 RP |
865 | Returns number of bytes transferred, or 0 for error. */ |
866 | ||
867 | static int | |
bd5635a1 RP |
868 | remote_read_bytes (memaddr, myaddr, len) |
869 | CORE_ADDR memaddr; | |
d538b510 | 870 | unsigned char *myaddr; |
bd5635a1 RP |
871 | int len; |
872 | { | |
873 | char buf[PBUFSIZ]; | |
874 | int i; | |
875 | char *p; | |
876 | ||
877 | if (len > PBUFSIZ / 2 - 1) | |
878 | abort (); | |
879 | ||
d24c0599 JK |
880 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
881 | result in a buffer like sprintf. */ | |
4aa6fe10 | 882 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len); |
d538b510 RP |
883 | putpkt (buf); |
884 | getpkt (buf, 0); | |
885 | ||
886 | if (buf[0] == 'E') | |
887 | { | |
888 | /* There is no correspondance between what the remote protocol uses | |
889 | for errors and errno codes. We would like a cleaner way of | |
890 | representing errors (big enough to include errno codes, bfd_error | |
891 | codes, and others). But for now just return EIO. */ | |
892 | errno = EIO; | |
893 | return 0; | |
894 | } | |
bd5635a1 | 895 | |
b543979c | 896 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
897 | each byte encoded as two hex characters. */ |
898 | ||
899 | p = buf; | |
900 | for (i = 0; i < len; i++) | |
901 | { | |
902 | if (p[0] == 0 || p[1] == 0) | |
d538b510 RP |
903 | /* Reply is short. This means that we were able to read only part |
904 | of what we wanted to. */ | |
905 | break; | |
bd5635a1 RP |
906 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
907 | p += 2; | |
908 | } | |
d538b510 | 909 | return i; |
bd5635a1 RP |
910 | } |
911 | \f | |
912 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 913 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
914 | nonzero. Returns length of data written or read; 0 for error. */ |
915 | ||
b543979c JG |
916 | /* ARGSUSED */ |
917 | static int | |
918 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
919 | CORE_ADDR memaddr; |
920 | char *myaddr; | |
921 | int len; | |
e1ce8aa5 | 922 | int should_write; |
b543979c | 923 | struct target_ops *target; /* ignored */ |
bd5635a1 | 924 | { |
bd5635a1 | 925 | int xfersize; |
d538b510 RP |
926 | int bytes_xferred; |
927 | int total_xferred = 0; | |
928 | ||
bd5635a1 RP |
929 | while (len > 0) |
930 | { | |
931 | if (len > MAXBUFBYTES) | |
932 | xfersize = MAXBUFBYTES; | |
933 | else | |
934 | xfersize = len; | |
935 | ||
e1ce8aa5 | 936 | if (should_write) |
94d4b713 JK |
937 | bytes_xferred = remote_write_bytes (memaddr, |
938 | (unsigned char *)myaddr, xfersize); | |
bd5635a1 | 939 | else |
94d4b713 JK |
940 | bytes_xferred = remote_read_bytes (memaddr, |
941 | (unsigned char *)myaddr, xfersize); | |
d538b510 RP |
942 | |
943 | /* If we get an error, we are done xferring. */ | |
944 | if (bytes_xferred == 0) | |
945 | break; | |
946 | ||
947 | memaddr += bytes_xferred; | |
948 | myaddr += bytes_xferred; | |
949 | len -= bytes_xferred; | |
950 | total_xferred += bytes_xferred; | |
bd5635a1 | 951 | } |
d538b510 | 952 | return total_xferred; |
bd5635a1 RP |
953 | } |
954 | ||
94d4b713 JK |
955 | #if 0 |
956 | /* Enable after 4.12. */ | |
957 | ||
958 | void | |
959 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
960 | addr_found, data_found) | |
961 | int len; | |
962 | char *data; | |
963 | char *mask; | |
964 | CORE_ADDR startaddr; | |
965 | int increment; | |
966 | CORE_ADDR lorange; | |
967 | CORE_ADDR hirange; | |
968 | CORE_ADDR *addr_found; | |
969 | char *data_found; | |
970 | { | |
971 | if (increment == -4 && len == 4) | |
972 | { | |
973 | long mask_long, data_long; | |
974 | long data_found_long; | |
975 | CORE_ADDR addr_we_found; | |
976 | char buf[PBUFSIZ]; | |
977 | long returned_long[2]; | |
978 | char *p; | |
979 | ||
980 | mask_long = extract_unsigned_integer (mask, len); | |
981 | data_long = extract_unsigned_integer (data, len); | |
982 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
983 | putpkt (buf); | |
984 | getpkt (buf, 0); | |
985 | if (buf[0] == '\0') | |
986 | { | |
987 | /* The stub doesn't support the 't' request. We might want to | |
988 | remember this fact, but on the other hand the stub could be | |
989 | switched on us. Maybe we should remember it only until | |
990 | the next "target remote". */ | |
991 | generic_search (len, data, mask, startaddr, increment, lorange, | |
992 | hirange, addr_found, data_found); | |
993 | return; | |
994 | } | |
995 | ||
996 | if (buf[0] == 'E') | |
997 | /* There is no correspondance between what the remote protocol uses | |
998 | for errors and errno codes. We would like a cleaner way of | |
999 | representing errors (big enough to include errno codes, bfd_error | |
1000 | codes, and others). But for now just use EIO. */ | |
1001 | memory_error (EIO, startaddr); | |
1002 | p = buf; | |
1003 | addr_we_found = 0; | |
1004 | while (*p != '\0' && *p != ',') | |
1005 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1006 | if (*p == '\0') | |
1007 | error ("Protocol error: short return for search"); | |
1008 | ||
1009 | data_found_long = 0; | |
1010 | while (*p != '\0' && *p != ',') | |
1011 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1012 | /* Ignore anything after this comma, for future extensions. */ | |
1013 | ||
1014 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1015 | { | |
1016 | *addr_found = 0; | |
1017 | return; | |
1018 | } | |
1019 | ||
1020 | *addr_found = addr_we_found; | |
1021 | *data_found = store_unsigned_integer (data_we_found, len); | |
1022 | return; | |
1023 | } | |
1024 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1025 | hirange, addr_found, data_found); | |
1026 | } | |
1027 | #endif /* 0 */ | |
1028 | \f | |
b543979c | 1029 | static void |
8f86a4e4 | 1030 | remote_files_info (ignore) |
5af4f5f6 | 1031 | struct target_ops *ignore; |
bd5635a1 | 1032 | { |
7c622b41 | 1033 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1034 | } |
1035 | \f | |
e50ebec8 JK |
1036 | /* Stuff for dealing with the packets which are part of this protocol. |
1037 | See comment at top of file for details. */ | |
bd5635a1 | 1038 | |
ebdb9ade | 1039 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1040 | |
bd5635a1 RP |
1041 | static int |
1042 | readchar () | |
1043 | { | |
ebdb9ade | 1044 | int ch; |
bd5635a1 | 1045 | |
ebdb9ade | 1046 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1047 | |
ebdb9ade JK |
1048 | if (ch < 0) |
1049 | return ch; | |
bd5635a1 | 1050 | |
ebdb9ade | 1051 | return ch & 0x7f; |
bd5635a1 RP |
1052 | } |
1053 | ||
1054 | /* Send the command in BUF to the remote machine, | |
1055 | and read the reply into BUF. | |
1056 | Report an error if we get an error reply. */ | |
1057 | ||
1058 | static void | |
1059 | remote_send (buf) | |
1060 | char *buf; | |
1061 | { | |
1062 | ||
1063 | putpkt (buf); | |
7c622b41 | 1064 | getpkt (buf, 0); |
bd5635a1 RP |
1065 | |
1066 | if (buf[0] == 'E') | |
1067 | error ("Remote failure reply: %s", buf); | |
1068 | } | |
1069 | ||
1070 | /* Send a packet to the remote machine, with error checking. | |
1071 | The data of the packet is in BUF. */ | |
1072 | ||
1073 | static void | |
1074 | putpkt (buf) | |
1075 | char *buf; | |
1076 | { | |
1077 | int i; | |
1078 | unsigned char csum = 0; | |
b543979c | 1079 | char buf2[PBUFSIZ]; |
bd5635a1 | 1080 | int cnt = strlen (buf); |
ebdb9ade | 1081 | int ch; |
bd5635a1 RP |
1082 | char *p; |
1083 | ||
1084 | /* Copy the packet into buffer BUF2, encapsulating it | |
1085 | and giving it a checksum. */ | |
1086 | ||
b543979c JG |
1087 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
1088 | abort(); | |
1089 | ||
bd5635a1 RP |
1090 | p = buf2; |
1091 | *p++ = '$'; | |
1092 | ||
1093 | for (i = 0; i < cnt; i++) | |
1094 | { | |
1095 | csum += buf[i]; | |
1096 | *p++ = buf[i]; | |
1097 | } | |
1098 | *p++ = '#'; | |
1099 | *p++ = tohex ((csum >> 4) & 0xf); | |
1100 | *p++ = tohex (csum & 0xf); | |
1101 | ||
1102 | /* Send it over and over until we get a positive ack. */ | |
1103 | ||
6b27ebe8 JK |
1104 | while (1) |
1105 | { | |
1624c38f SG |
1106 | int started_error_output = 0; |
1107 | ||
d0d8484a | 1108 | if (remote_debug) |
6b27ebe8 JK |
1109 | { |
1110 | *p = '\0'; | |
1624c38f SG |
1111 | printf_unfiltered ("Sending packet: %s...", buf2); |
1112 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1113 | } |
1114 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1115 | perror_with_name ("putpkt: write failed"); | |
1116 | ||
1117 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1118 | while (1) | |
1119 | { | |
1120 | ch = readchar (); | |
1121 | ||
1624c38f SG |
1122 | if (remote_debug) |
1123 | { | |
1124 | switch (ch) | |
1125 | { | |
1126 | case '+': | |
1127 | case SERIAL_TIMEOUT: | |
1128 | case SERIAL_ERROR: | |
1129 | case SERIAL_EOF: | |
1130 | case '$': | |
1131 | if (started_error_output) | |
1132 | { | |
1133 | putc_unfiltered ('\n'); | |
1134 | started_error_output = 0; | |
1135 | } | |
1136 | } | |
1137 | } | |
1138 | ||
6b27ebe8 JK |
1139 | switch (ch) |
1140 | { | |
1141 | case '+': | |
d0d8484a | 1142 | if (remote_debug) |
199b2450 | 1143 | printf_unfiltered("Ack\n"); |
6b27ebe8 JK |
1144 | return; |
1145 | case SERIAL_TIMEOUT: | |
1146 | break; /* Retransmit buffer */ | |
1147 | case SERIAL_ERROR: | |
1148 | perror_with_name ("putpkt: couldn't read ACK"); | |
1149 | case SERIAL_EOF: | |
1150 | error ("putpkt: EOF while trying to read ACK"); | |
1624c38f SG |
1151 | case '$': |
1152 | { | |
1153 | unsigned char junkbuf[PBUFSIZ]; | |
1154 | ||
1155 | /* It's probably an old response, and we're out of sync. Just | |
1156 | gobble up the packet and ignore it. */ | |
1157 | getpkt (junkbuf, 0); | |
1158 | continue; /* Now, go look for + */ | |
1159 | } | |
6b27ebe8 | 1160 | default: |
d0d8484a | 1161 | if (remote_debug) |
1624c38f SG |
1162 | { |
1163 | if (!started_error_output) | |
1164 | { | |
1165 | started_error_output = 1; | |
1166 | printf_unfiltered ("putpkt: Junk: "); | |
1167 | } | |
1168 | putc_unfiltered (ch & 0177); | |
1169 | } | |
6b27ebe8 JK |
1170 | continue; |
1171 | } | |
1172 | break; /* Here to retransmit */ | |
1173 | } | |
981a3309 | 1174 | |
94d4b713 JK |
1175 | #if 0 |
1176 | /* This is wrong. If doing a long backtrace, the user should be | |
1177 | able to get out next time we call QUIT, without anything as violent | |
1178 | as interrupt_query. If we want to provide a way out of here | |
1179 | without getting to the next QUIT, it should be based on hitting | |
1180 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1181 | if (quit_flag) |
1182 | { | |
1183 | quit_flag = 0; | |
1184 | interrupt_query (); | |
1185 | } | |
94d4b713 | 1186 | #endif |
6b27ebe8 | 1187 | } |
bd5635a1 RP |
1188 | } |
1189 | ||
1190 | /* Read a packet from the remote machine, with error checking, | |
7c622b41 JG |
1191 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1192 | If FOREVER, wait forever rather than timing out; this is used | |
1193 | while the target is executing user code. */ | |
bd5635a1 RP |
1194 | |
1195 | static void | |
94d4b713 JK |
1196 | getpkt (retbuf, forever) |
1197 | char *retbuf; | |
ebdb9ade | 1198 | int forever; |
bd5635a1 RP |
1199 | { |
1200 | char *bp; | |
1201 | unsigned char csum; | |
7c622b41 | 1202 | int c = 0; |
bd5635a1 | 1203 | unsigned char c1, c2; |
38094c60 | 1204 | int retries = 0; |
94d4b713 JK |
1205 | char buf[PBUFSIZ]; |
1206 | ||
38094c60 | 1207 | #define MAX_RETRIES 10 |
bd5635a1 | 1208 | |
bd5635a1 RP |
1209 | while (1) |
1210 | { | |
94d4b713 JK |
1211 | #if 0 |
1212 | /* This is wrong. If doing a long backtrace, the user should be | |
1213 | able to get out time next we call QUIT, without anything as violent | |
1214 | as interrupt_query. If we want to provide a way out of here | |
1215 | without getting to the next QUIT, it should be based on hitting | |
1216 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1217 | if (quit_flag) |
1218 | { | |
1219 | quit_flag = 0; | |
1220 | interrupt_query (); | |
1221 | } | |
94d4b713 | 1222 | #endif |
981a3309 | 1223 | |
7c622b41 JG |
1224 | /* This can loop forever if the remote side sends us characters |
1225 | continuously, but if it pauses, we'll get a zero from readchar | |
1226 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 JK |
1227 | |
1228 | c = readchar(); | |
1229 | if (c > 0 && c != '$') | |
1230 | continue; | |
1231 | ||
1232 | if (c == SERIAL_TIMEOUT) | |
1233 | { | |
1234 | if (forever) | |
1235 | continue; | |
0c993550 JK |
1236 | if (remote_debug) |
1237 | puts_filtered ("Timed out.\n"); | |
1238 | goto whole; | |
6b27ebe8 JK |
1239 | } |
1240 | ||
1241 | if (c == SERIAL_EOF) | |
1242 | error ("Remote connection closed"); | |
1243 | if (c == SERIAL_ERROR) | |
1244 | perror_with_name ("Remote communication error"); | |
7c622b41 | 1245 | |
bd5635a1 RP |
1246 | /* Force csum to be zero here because of possible error retry. */ |
1247 | csum = 0; | |
bd5635a1 | 1248 | bp = buf; |
7c622b41 | 1249 | |
bd5635a1 RP |
1250 | while (1) |
1251 | { | |
1252 | c = readchar (); | |
ebdb9ade | 1253 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1254 | { |
d0d8484a | 1255 | if (remote_debug) |
7c622b41 JG |
1256 | puts_filtered ("Timeout in mid-packet, retrying\n"); |
1257 | goto whole; /* Start a new packet, count retries */ | |
1258 | } | |
1259 | if (c == '$') | |
1260 | { | |
d0d8484a | 1261 | if (remote_debug) |
7c622b41 JG |
1262 | puts_filtered ("Saw new packet start in middle of old one\n"); |
1263 | goto whole; /* Start a new packet, count retries */ | |
1264 | } | |
bd5635a1 RP |
1265 | if (c == '#') |
1266 | break; | |
8f86a4e4 JG |
1267 | if (bp >= buf+PBUFSIZ-1) |
1268 | { | |
1269 | *bp = '\0'; | |
7c622b41 JG |
1270 | puts_filtered ("Remote packet too long: "); |
1271 | puts_filtered (buf); | |
1272 | puts_filtered ("\n"); | |
8f86a4e4 JG |
1273 | goto whole; |
1274 | } | |
bd5635a1 RP |
1275 | *bp++ = c; |
1276 | csum += c; | |
1277 | } | |
1278 | *bp = 0; | |
1279 | ||
1280 | c1 = fromhex (readchar ()); | |
1281 | c2 = fromhex (readchar ()); | |
1282 | if ((csum & 0xff) == (c1 << 4) + c2) | |
1283 | break; | |
7c622b41 JG |
1284 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
1285 | (c1 << 4) + c2, csum & 0xff); | |
1286 | puts_filtered (buf); | |
1287 | puts_filtered ("\n"); | |
38094c60 | 1288 | |
8f86a4e4 JG |
1289 | /* Try the whole thing again. */ |
1290 | whole: | |
38094c60 JG |
1291 | if (++retries < MAX_RETRIES) |
1292 | { | |
ebdb9ade | 1293 | SERIAL_WRITE (remote_desc, "-", 1); |
38094c60 JG |
1294 | } |
1295 | else | |
1296 | { | |
199b2450 | 1297 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
38094c60 JG |
1298 | break; |
1299 | } | |
bd5635a1 RP |
1300 | } |
1301 | ||
94d4b713 JK |
1302 | /* Deal with run-length encoding. */ |
1303 | { | |
1304 | char *src = buf; | |
1305 | char *dest = retbuf; | |
1306 | int i; | |
1307 | int repeat; | |
1308 | do { | |
1309 | if (*src == '*') | |
1310 | { | |
1311 | if (src[1] == '\0' || src[2] == '\0') | |
1312 | { | |
1313 | if (remote_debug) | |
1314 | puts_filtered ("Packet too short, retrying\n"); | |
1315 | goto whole; | |
1316 | } | |
1317 | repeat = (fromhex (src[1]) << 4) + fromhex (src[2]); | |
1318 | for (i = 0; i < repeat; ++i) | |
1319 | { | |
1320 | *dest++ = src[-1]; | |
1321 | } | |
1322 | src += 2; | |
1323 | } | |
1324 | else | |
1325 | { | |
1326 | *dest++ = *src; | |
1327 | } | |
1328 | } while (*src++ != '\0'); | |
1329 | } | |
7c622b41 | 1330 | |
ebdb9ade | 1331 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 | 1332 | |
d0d8484a | 1333 | if (remote_debug) |
199b2450 | 1334 | fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf); |
bd5635a1 RP |
1335 | } |
1336 | \f | |
ebdb9ade JK |
1337 | static void |
1338 | remote_kill () | |
1339 | { | |
1340 | putpkt ("k"); | |
1341 | /* Don't wait for it to die. I'm not really sure it matters whether | |
1342 | we do or not. For the existing stubs, kill is a noop. */ | |
1343 | target_mourn_inferior (); | |
1344 | } | |
bd5635a1 | 1345 | |
ebdb9ade JK |
1346 | static void |
1347 | remote_mourn () | |
1348 | { | |
1349 | unpush_target (&remote_ops); | |
1350 | generic_mourn_inferior (); | |
1351 | } | |
1352 | \f | |
5af4f5f6 JK |
1353 | #ifdef REMOTE_BREAKPOINT |
1354 | ||
1355 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction | |
1356 | than other targets. */ | |
1357 | static unsigned char break_insn[] = REMOTE_BREAKPOINT; | |
1358 | ||
1359 | /* Check that it fits in BREAKPOINT_MAX bytes. */ | |
1360 | static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT; | |
1361 | ||
1362 | #else /* No REMOTE_BREAKPOINT. */ | |
1363 | ||
1364 | /* Same old breakpoint instruction. This code does nothing different | |
1365 | than mem-break.c. */ | |
1366 | static unsigned char break_insn[] = BREAKPOINT; | |
1367 | ||
1368 | #endif /* No REMOTE_BREAKPOINT. */ | |
1369 | ||
1370 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1371 | support. We read the contents of the target location and stash it, | |
1372 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1373 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1374 | memory allocated for saving the target contents. It is guaranteed | |
1375 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1376 | is accomplished via BREAKPOINT_MAX). */ | |
1377 | ||
d538b510 | 1378 | static int |
5af4f5f6 JK |
1379 | remote_insert_breakpoint (addr, contents_cache) |
1380 | CORE_ADDR addr; | |
1381 | char *contents_cache; | |
1382 | { | |
1383 | int val; | |
1384 | ||
1385 | val = target_read_memory (addr, contents_cache, sizeof break_insn); | |
1386 | ||
1387 | if (val == 0) | |
1388 | val = target_write_memory (addr, (char *)break_insn, sizeof break_insn); | |
1389 | ||
1390 | return val; | |
1391 | } | |
1392 | ||
d538b510 | 1393 | static int |
5af4f5f6 JK |
1394 | remote_remove_breakpoint (addr, contents_cache) |
1395 | CORE_ADDR addr; | |
1396 | char *contents_cache; | |
1397 | { | |
1398 | return target_write_memory (addr, contents_cache, sizeof break_insn); | |
1399 | } | |
1400 | \f | |
bd5635a1 RP |
1401 | /* Define the target subroutine names */ |
1402 | ||
1403 | struct target_ops remote_ops = { | |
b543979c JG |
1404 | "remote", /* to_shortname */ |
1405 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1406 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1407 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1408 | remote_open, /* to_open */ | |
1409 | remote_close, /* to_close */ | |
1410 | NULL, /* to_attach */ | |
1411 | remote_detach, /* to_detach */ | |
1412 | remote_resume, /* to_resume */ | |
1413 | remote_wait, /* to_wait */ | |
1414 | remote_fetch_registers, /* to_fetch_registers */ | |
1415 | remote_store_registers, /* to_store_registers */ | |
1416 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1417 | remote_xfer_memory, /* to_xfer_memory */ |
1418 | remote_files_info, /* to_files_info */ | |
5af4f5f6 JK |
1419 | |
1420 | remote_insert_breakpoint, /* to_insert_breakpoint */ | |
1421 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
1422 | ||
b543979c JG |
1423 | NULL, /* to_terminal_init */ |
1424 | NULL, /* to_terminal_inferior */ | |
1425 | NULL, /* to_terminal_ours_for_output */ | |
1426 | NULL, /* to_terminal_ours */ | |
1427 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1428 | remote_kill, /* to_kill */ |
6b27ebe8 | 1429 | generic_load, /* to_load */ |
b543979c JG |
1430 | NULL, /* to_lookup_symbol */ |
1431 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1432 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1433 | 0, /* to_can_run */ |
7c622b41 | 1434 | 0, /* to_notice_signals */ |
b543979c JG |
1435 | process_stratum, /* to_stratum */ |
1436 | NULL, /* to_next */ | |
1437 | 1, /* to_has_all_memory */ | |
1438 | 1, /* to_has_memory */ | |
1439 | 1, /* to_has_stack */ | |
1440 | 1, /* to_has_registers */ | |
1441 | 1, /* to_has_execution */ | |
1442 | NULL, /* sections */ | |
1443 | NULL, /* sections_end */ | |
1444 | OPS_MAGIC /* to_magic */ | |
bd5635a1 | 1445 | }; |
976bb0be | 1446 | #endif /* Use remote. */ |
bd5635a1 RP |
1447 | |
1448 | void | |
1449 | _initialize_remote () | |
1450 | { | |
976bb0be | 1451 | #if !defined(DONT_USE_REMOTE) |
bd5635a1 | 1452 | add_target (&remote_ops); |
8f86a4e4 | 1453 | #endif |
976bb0be | 1454 | } |