]>
Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
7c622b41 | 2 | Copyright 1988, 1991, 1992, 1993 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. | |
21 | All values are encoded in ascii hex digits. | |
22 | ||
23 | Request Packet | |
24 | ||
25 | read registers g | |
26 | reply XX....X Each byte of register data | |
27 | is described by two hex digits. | |
28 | Registers are in the internal order | |
29 | for GDB, and the bytes in a register | |
30 | are in the same order the machine uses. | |
31 | or ENN for an error. | |
32 | ||
33 | write regs GXX..XX Each byte of register data | |
34 | is described by two hex digits. | |
35 | reply OK for success | |
36 | ENN for an error | |
37 | ||
38 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. | |
39 | reply XX..XX XX..XX is mem contents | |
40 | or ENN NN is errno | |
41 | ||
42 | write mem MAA..AA,LLLL:XX..XX | |
43 | AA..AA is address, | |
44 | LLLL is number of bytes, | |
45 | XX..XX is data | |
46 | reply OK for success | |
47 | ENN for an error | |
48 | ||
49 | cont cAA..AA AA..AA is address to resume | |
50 | If AA..AA is omitted, | |
51 | resume at same address. | |
52 | ||
53 | step sAA..AA AA..AA is address to resume | |
54 | If AA..AA is omitted, | |
55 | resume at same address. | |
56 | ||
57 | last signal ? Reply the current reason for stopping. | |
58 | This is the same reply as is generated | |
59 | for step or cont : SAA where AA is the | |
60 | signal number. | |
61 | ||
62 | There is no immediate reply to step or cont. | |
63 | The reply comes when the machine stops. | |
64 | It is SAA AA is the "signal number" | |
65 | ||
8f86a4e4 JG |
66 | or... TAAPPPPPPPPFFFFFFFF |
67 | where AA is the signal number, | |
68 | PPPPPPPP is the PC (PC_REGNUM), and | |
69 | FFFFFFFF is the frame ptr (FP_REGNUM). | |
70 | ||
bd5635a1 RP |
71 | kill req k |
72 | */ | |
73 | ||
d747e0af | 74 | #include "defs.h" |
bd5635a1 RP |
75 | #include <string.h> |
76 | #include <fcntl.h> | |
bd5635a1 RP |
77 | #include "frame.h" |
78 | #include "inferior.h" | |
79 | #include "target.h" | |
80 | #include "wait.h" | |
81 | #include "terminal.h" | |
8f86a4e4 | 82 | #include "gdbcmd.h" |
bd5635a1 | 83 | |
8f86a4e4 | 84 | #if !defined(DONT_USE_REMOTE) |
bd5635a1 RP |
85 | #ifdef USG |
86 | #include <sys/types.h> | |
87 | #endif | |
88 | ||
89 | #include <signal.h> | |
ebdb9ade | 90 | #include "serial.h" |
bd5635a1 | 91 | |
b543979c JG |
92 | /* Prototypes for local functions */ |
93 | ||
94 | static void | |
95 | remote_write_bytes PARAMS ((CORE_ADDR, char *, int)); | |
96 | ||
97 | static void | |
98 | remote_read_bytes PARAMS ((CORE_ADDR, char *, int)); | |
99 | ||
100 | static void | |
101 | remote_files_info PARAMS ((struct target_ops *)); | |
102 | ||
103 | static int | |
104 | remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *)); | |
105 | ||
106 | static void | |
107 | remote_prepare_to_store PARAMS ((void)); | |
108 | ||
109 | static void | |
110 | remote_fetch_registers PARAMS ((int)); | |
111 | ||
112 | static void | |
113 | remote_resume PARAMS ((int, int)); | |
114 | ||
7c622b41 JG |
115 | static int |
116 | remote_start_remote PARAMS ((char *)); | |
117 | ||
b543979c JG |
118 | static void |
119 | remote_open PARAMS ((char *, int)); | |
120 | ||
121 | static void | |
122 | remote_close PARAMS ((int)); | |
123 | ||
124 | static void | |
125 | remote_store_registers PARAMS ((int)); | |
126 | ||
127 | static void | |
7c622b41 | 128 | getpkt PARAMS ((char *, int)); |
b543979c JG |
129 | |
130 | static void | |
131 | putpkt PARAMS ((char *)); | |
132 | ||
133 | static void | |
134 | remote_send PARAMS ((char *)); | |
135 | ||
136 | static int | |
137 | readchar PARAMS ((void)); | |
138 | ||
139 | static int | |
140 | remote_wait PARAMS ((WAITTYPE *)); | |
141 | ||
142 | static int | |
143 | tohex PARAMS ((int)); | |
144 | ||
145 | static int | |
146 | fromhex PARAMS ((int)); | |
147 | ||
148 | static void | |
149 | remote_detach PARAMS ((char *, int)); | |
150 | ||
bd5635a1 RP |
151 | extern struct target_ops remote_ops; /* Forward decl */ |
152 | ||
8f86a4e4 | 153 | static int kiodebug = 0; |
ebdb9ade JK |
154 | /* This was 5 seconds, which is a long time to sit and wait. |
155 | Unless this is going though some terminal server or multiplexer or | |
156 | other form of hairy serial connection, I would think 2 seconds would | |
157 | be plenty. */ | |
158 | static int timeout = 2; | |
bd5635a1 RP |
159 | |
160 | #if 0 | |
161 | int icache; | |
162 | #endif | |
163 | ||
164 | /* Descriptor for I/O to remote machine. Initialize it to -1 so that | |
165 | remote_open knows that we don't have a file open when the program | |
166 | starts. */ | |
ebdb9ade | 167 | serial_t remote_desc = NULL; |
bd5635a1 | 168 | |
b543979c | 169 | #define PBUFSIZ 1024 |
bd5635a1 RP |
170 | |
171 | /* Maximum number of bytes to read/write at once. The value here | |
172 | is chosen to fill up a packet (the headers account for the 32). */ | |
173 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
174 | ||
b543979c JG |
175 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
176 | #if REGISTER_BYTES > MAXBUFBYTES | |
177 | #undef PBUFSIZ | |
178 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) | |
bd5635a1 | 179 | #endif |
bd5635a1 | 180 | \f |
bd5635a1 RP |
181 | /* Clean up connection to a remote debugger. */ |
182 | ||
e1ce8aa5 | 183 | /* ARGSUSED */ |
b543979c | 184 | static void |
bd5635a1 RP |
185 | remote_close (quitting) |
186 | int quitting; | |
187 | { | |
ebdb9ade JK |
188 | if (remote_desc) |
189 | SERIAL_CLOSE (remote_desc); | |
190 | remote_desc = NULL; | |
b543979c JG |
191 | } |
192 | ||
7c622b41 JG |
193 | /* Stub for catch_errors. */ |
194 | ||
195 | static int | |
196 | remote_start_remote (dummy) | |
197 | char *dummy; | |
198 | { | |
199 | /* Ack any packet which the remote side has already sent. */ | |
ebdb9ade | 200 | SERIAL_WRITE (remote_desc, "+\r", 2); |
7c622b41 JG |
201 | putpkt ("?"); /* initiate a query from remote machine */ |
202 | ||
203 | start_remote (); /* Initialize gdb process mechanisms */ | |
204 | return 1; | |
205 | } | |
206 | ||
bd5635a1 RP |
207 | /* Open a connection to a remote debugger. |
208 | NAME is the filename used for communication. */ | |
209 | ||
b543979c | 210 | static void |
bd5635a1 RP |
211 | remote_open (name, from_tty) |
212 | char *name; | |
213 | int from_tty; | |
214 | { | |
bd5635a1 RP |
215 | if (name == 0) |
216 | error ( | |
217 | "To open a remote debug connection, you need to specify what serial\n\ | |
218 | device is attached to the remote system (e.g. /dev/ttya)."); | |
219 | ||
f2fc6e7a JK |
220 | target_preopen (from_tty); |
221 | ||
ebdb9ade | 222 | unpush_target (&remote_ops); |
bd5635a1 RP |
223 | |
224 | #if 0 | |
225 | dcache_init (); | |
226 | #endif | |
227 | ||
ebdb9ade JK |
228 | remote_desc = SERIAL_OPEN (name); |
229 | if (!remote_desc) | |
bd5635a1 RP |
230 | perror_with_name (name); |
231 | ||
b543979c JG |
232 | if (baud_rate) |
233 | { | |
ebdb9ade | 234 | int rate; |
b543979c | 235 | |
ebdb9ade JK |
236 | if (sscanf (baud_rate, "%d", &rate) == 1) |
237 | if (SERIAL_SETBAUDRATE (remote_desc, rate)) | |
238 | { | |
239 | SERIAL_CLOSE (remote_desc); | |
240 | perror_with_name (name); | |
241 | } | |
b543979c | 242 | } |
ebdb9ade JK |
243 | |
244 | SERIAL_RAW (remote_desc); | |
bd5635a1 RP |
245 | |
246 | if (from_tty) | |
7c622b41 JG |
247 | { |
248 | puts_filtered ("Remote debugging using "); | |
249 | puts_filtered (name); | |
250 | puts_filtered ("\n"); | |
251 | } | |
bd5635a1 | 252 | push_target (&remote_ops); /* Switch to using remote target now */ |
bd5635a1 | 253 | |
7c622b41 JG |
254 | /* Start the remote connection; if error (0), discard this target. */ |
255 | immediate_quit++; /* Allow user to interrupt it */ | |
256 | if (!catch_errors (remote_start_remote, (char *)0, | |
257 | "Couldn't establish connection to remote target\n")) | |
258 | pop_target(); | |
bd5635a1 RP |
259 | } |
260 | ||
261 | /* remote_detach() | |
262 | takes a program previously attached to and detaches it. | |
263 | We better not have left any breakpoints | |
264 | in the program or it'll die when it hits one. | |
265 | Close the open connection to the remote debugger. | |
266 | Use this when you want to detach and do something else | |
267 | with your gdb. */ | |
268 | ||
269 | static void | |
270 | remote_detach (args, from_tty) | |
271 | char *args; | |
272 | int from_tty; | |
273 | { | |
274 | if (args) | |
275 | error ("Argument given to \"detach\" when remotely debugging."); | |
276 | ||
277 | pop_target (); | |
278 | if (from_tty) | |
7c622b41 | 279 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
280 | } |
281 | ||
282 | /* Convert hex digit A to a number. */ | |
283 | ||
284 | static int | |
285 | fromhex (a) | |
286 | int a; | |
287 | { | |
288 | if (a >= '0' && a <= '9') | |
289 | return a - '0'; | |
290 | else if (a >= 'a' && a <= 'f') | |
291 | return a - 'a' + 10; | |
292 | else | |
293 | error ("Reply contains invalid hex digit"); | |
294 | return -1; | |
295 | } | |
296 | ||
297 | /* Convert number NIB to a hex digit. */ | |
298 | ||
299 | static int | |
300 | tohex (nib) | |
301 | int nib; | |
302 | { | |
303 | if (nib < 10) | |
304 | return '0'+nib; | |
305 | else | |
306 | return 'a'+nib-10; | |
307 | } | |
308 | \f | |
309 | /* Tell the remote machine to resume. */ | |
310 | ||
b543979c | 311 | static void |
bd5635a1 RP |
312 | remote_resume (step, siggnal) |
313 | int step, siggnal; | |
314 | { | |
315 | char buf[PBUFSIZ]; | |
316 | ||
317 | if (siggnal) | |
ebdb9ade JK |
318 | { |
319 | char *name; | |
320 | target_terminal_ours_for_output (); | |
321 | printf_filtered ("Can't send signals to a remote system. "); | |
322 | name = strsigno (siggnal); | |
323 | if (name) | |
324 | printf_filtered (name); | |
325 | else | |
326 | printf_filtered ("Signal %d", siggnal); | |
327 | printf_filtered (" not sent.\n"); | |
328 | target_terminal_inferior (); | |
329 | } | |
bd5635a1 RP |
330 | |
331 | #if 0 | |
332 | dcache_flush (); | |
333 | #endif | |
334 | ||
335 | strcpy (buf, step ? "s": "c"); | |
336 | ||
337 | putpkt (buf); | |
338 | } | |
ebdb9ade JK |
339 | \f |
340 | static void remote_interrupt_twice PARAMS ((int)); | |
341 | static void (*ofunc)(); | |
bd5635a1 | 342 | |
b543979c JG |
343 | /* Send ^C to target to halt it. Target will respond, and send us a |
344 | packet. */ | |
345 | ||
e676a15f FF |
346 | void remote_interrupt(signo) |
347 | int signo; | |
b543979c | 348 | { |
ebdb9ade JK |
349 | /* If this doesn't work, try more severe steps. */ |
350 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 JG |
351 | |
352 | if (kiodebug) | |
353 | printf ("remote_interrupt called\n"); | |
354 | ||
ebdb9ade | 355 | SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */ |
b543979c JG |
356 | } |
357 | ||
ebdb9ade JK |
358 | /* The user typed ^C twice. */ |
359 | static void | |
360 | remote_interrupt_twice (signo) | |
361 | int signo; | |
362 | { | |
363 | signal (signo, ofunc); | |
364 | ||
365 | target_terminal_ours (); | |
366 | if (query ("Interrupted while waiting for the inferior.\n\ | |
367 | Give up (and stop debugging it)? ")) | |
368 | { | |
369 | target_mourn_inferior (); | |
370 | return_to_top_level (); | |
371 | } | |
372 | else | |
373 | { | |
374 | signal (signo, remote_interrupt); | |
375 | target_terminal_inferior (); | |
376 | } | |
377 | } | |
b543979c | 378 | |
bd5635a1 | 379 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
380 | storing status in STATUS just as `wait' would. |
381 | Returns "pid" (though it's not clear what, if anything, that | |
382 | means in the case of this target). */ | |
bd5635a1 | 383 | |
b543979c | 384 | static int |
bd5635a1 RP |
385 | remote_wait (status) |
386 | WAITTYPE *status; | |
387 | { | |
388 | unsigned char buf[PBUFSIZ]; | |
8f86a4e4 JG |
389 | unsigned char *p; |
390 | int i; | |
a03d4f8e | 391 | long regno; |
34517ebc | 392 | char regs[MAX_REGISTER_RAW_SIZE]; |
8f86a4e4 | 393 | |
bd5635a1 | 394 | WSETEXIT ((*status), 0); |
b543979c | 395 | |
38094c60 | 396 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
7c622b41 | 397 | getpkt ((char *) buf, 1); |
b543979c JG |
398 | signal (SIGINT, ofunc); |
399 | ||
bd5635a1 RP |
400 | if (buf[0] == 'E') |
401 | error ("Remote failure reply: %s", buf); | |
8f86a4e4 JG |
402 | if (buf[0] == 'T') |
403 | { | |
4ecee2f9 | 404 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
a03d4f8e JG |
405 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
406 | ss = signal number | |
407 | n... = register number | |
408 | r... = register contents | |
409 | */ | |
410 | ||
8f86a4e4 | 411 | p = &buf[3]; /* after Txx */ |
4ecee2f9 SG |
412 | |
413 | while (*p) | |
8f86a4e4 | 414 | { |
a03d4f8e JG |
415 | regno = strtol (p, &p, 16); /* Read the register number */ |
416 | ||
417 | if (*p++ != ':' | |
418 | || regno >= NUM_REGS) | |
419 | error ("Remote sent bad register number %s", buf); | |
4ecee2f9 SG |
420 | |
421 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
422 | { | |
423 | if (p[0] == 0 || p[1] == 0) | |
424 | error ("Remote reply is too short: %s", buf); | |
425 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
426 | p += 2; | |
427 | } | |
428 | ||
a03d4f8e JG |
429 | if (*p++ != ';') |
430 | error("Remote register badly formatted: %s", buf); | |
431 | ||
4ecee2f9 | 432 | supply_register (regno, regs); |
8f86a4e4 | 433 | } |
8f86a4e4 JG |
434 | } |
435 | else if (buf[0] != 'S') | |
bd5635a1 | 436 | error ("Invalid remote reply: %s", buf); |
8f86a4e4 | 437 | |
bd5635a1 | 438 | WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2])))); |
8f86a4e4 | 439 | |
e1ce8aa5 | 440 | return 0; |
bd5635a1 RP |
441 | } |
442 | ||
443 | /* Read the remote registers into the block REGS. */ | |
e1ce8aa5 JK |
444 | /* Currently we just read all the registers, so we don't use regno. */ |
445 | /* ARGSUSED */ | |
b543979c | 446 | static void |
bd5635a1 RP |
447 | remote_fetch_registers (regno) |
448 | int regno; | |
449 | { | |
450 | char buf[PBUFSIZ]; | |
451 | int i; | |
452 | char *p; | |
453 | char regs[REGISTER_BYTES]; | |
454 | ||
455 | sprintf (buf, "g"); | |
456 | remote_send (buf); | |
457 | ||
458 | /* Reply describes registers byte by byte, each byte encoded as two | |
459 | hex characters. Suck them all up, then supply them to the | |
460 | register cacheing/storage mechanism. */ | |
461 | ||
462 | p = buf; | |
463 | for (i = 0; i < REGISTER_BYTES; i++) | |
464 | { | |
465 | if (p[0] == 0 || p[1] == 0) | |
466 | error ("Remote reply is too short: %s", buf); | |
467 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
468 | p += 2; | |
469 | } | |
470 | for (i = 0; i < NUM_REGS; i++) | |
471 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
472 | } |
473 | ||
474 | /* Prepare to store registers. Since we send them all, we have to | |
475 | read out the ones we don't want to change first. */ | |
476 | ||
b543979c | 477 | static void |
bd5635a1 RP |
478 | remote_prepare_to_store () |
479 | { | |
34517ebc JG |
480 | /* Make sure the entire registers array is valid. */ |
481 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
482 | } |
483 | ||
484 | /* Store the remote registers from the contents of the block REGISTERS. | |
485 | FIXME, eventually just store one register if that's all that is needed. */ | |
486 | ||
e1ce8aa5 | 487 | /* ARGSUSED */ |
b543979c | 488 | static void |
bd5635a1 RP |
489 | remote_store_registers (regno) |
490 | int regno; | |
491 | { | |
492 | char buf[PBUFSIZ]; | |
493 | int i; | |
494 | char *p; | |
495 | ||
496 | buf[0] = 'G'; | |
497 | ||
498 | /* Command describes registers byte by byte, | |
499 | each byte encoded as two hex characters. */ | |
500 | ||
501 | p = buf + 1; | |
502 | for (i = 0; i < REGISTER_BYTES; i++) | |
503 | { | |
504 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
505 | *p++ = tohex (registers[i] & 0xf); | |
506 | } | |
507 | *p = '\0'; | |
508 | ||
509 | remote_send (buf); | |
bd5635a1 RP |
510 | } |
511 | ||
512 | #if 0 | |
513 | /* Read a word from remote address ADDR and return it. | |
514 | This goes through the data cache. */ | |
515 | ||
516 | int | |
517 | remote_fetch_word (addr) | |
518 | CORE_ADDR addr; | |
519 | { | |
520 | if (icache) | |
521 | { | |
522 | extern CORE_ADDR text_start, text_end; | |
523 | ||
524 | if (addr >= text_start && addr < text_end) | |
525 | { | |
526 | int buffer; | |
527 | xfer_core_file (addr, &buffer, sizeof (int)); | |
528 | return buffer; | |
529 | } | |
530 | } | |
531 | return dcache_fetch (addr); | |
532 | } | |
533 | ||
534 | /* Write a word WORD into remote address ADDR. | |
535 | This goes through the data cache. */ | |
536 | ||
537 | void | |
538 | remote_store_word (addr, word) | |
539 | CORE_ADDR addr; | |
540 | int word; | |
541 | { | |
542 | dcache_poke (addr, word); | |
543 | } | |
544 | #endif /* 0 */ | |
545 | \f | |
546 | /* Write memory data directly to the remote machine. | |
547 | This does not inform the data cache; the data cache uses this. | |
548 | MEMADDR is the address in the remote memory space. | |
549 | MYADDR is the address of the buffer in our space. | |
550 | LEN is the number of bytes. */ | |
551 | ||
b543979c | 552 | static void |
bd5635a1 RP |
553 | remote_write_bytes (memaddr, myaddr, len) |
554 | CORE_ADDR memaddr; | |
555 | char *myaddr; | |
556 | int len; | |
557 | { | |
558 | char buf[PBUFSIZ]; | |
559 | int i; | |
560 | char *p; | |
561 | ||
562 | if (len > PBUFSIZ / 2 - 20) | |
563 | abort (); | |
564 | ||
565 | sprintf (buf, "M%x,%x:", memaddr, len); | |
566 | ||
b543979c | 567 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
568 | each byte encoded as two hex characters. */ |
569 | ||
570 | p = buf + strlen (buf); | |
571 | for (i = 0; i < len; i++) | |
572 | { | |
573 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
574 | *p++ = tohex (myaddr[i] & 0xf); | |
575 | } | |
576 | *p = '\0'; | |
577 | ||
578 | remote_send (buf); | |
579 | } | |
580 | ||
581 | /* Read memory data directly from the remote machine. | |
582 | This does not use the data cache; the data cache uses this. | |
583 | MEMADDR is the address in the remote memory space. | |
584 | MYADDR is the address of the buffer in our space. | |
585 | LEN is the number of bytes. */ | |
586 | ||
b543979c | 587 | static void |
bd5635a1 RP |
588 | remote_read_bytes (memaddr, myaddr, len) |
589 | CORE_ADDR memaddr; | |
590 | char *myaddr; | |
591 | int len; | |
592 | { | |
593 | char buf[PBUFSIZ]; | |
594 | int i; | |
595 | char *p; | |
596 | ||
597 | if (len > PBUFSIZ / 2 - 1) | |
598 | abort (); | |
599 | ||
600 | sprintf (buf, "m%x,%x", memaddr, len); | |
601 | remote_send (buf); | |
602 | ||
b543979c | 603 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
604 | each byte encoded as two hex characters. */ |
605 | ||
606 | p = buf; | |
607 | for (i = 0; i < len; i++) | |
608 | { | |
609 | if (p[0] == 0 || p[1] == 0) | |
610 | error ("Remote reply is too short: %s", buf); | |
611 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
612 | p += 2; | |
613 | } | |
614 | } | |
615 | \f | |
616 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 617 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
618 | nonzero. Returns length of data written or read; 0 for error. */ |
619 | ||
b543979c JG |
620 | /* ARGSUSED */ |
621 | static int | |
622 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
623 | CORE_ADDR memaddr; |
624 | char *myaddr; | |
625 | int len; | |
e1ce8aa5 | 626 | int should_write; |
b543979c | 627 | struct target_ops *target; /* ignored */ |
bd5635a1 RP |
628 | { |
629 | int origlen = len; | |
630 | int xfersize; | |
631 | while (len > 0) | |
632 | { | |
633 | if (len > MAXBUFBYTES) | |
634 | xfersize = MAXBUFBYTES; | |
635 | else | |
636 | xfersize = len; | |
637 | ||
e1ce8aa5 | 638 | if (should_write) |
bd5635a1 RP |
639 | remote_write_bytes(memaddr, myaddr, xfersize); |
640 | else | |
641 | remote_read_bytes (memaddr, myaddr, xfersize); | |
642 | memaddr += xfersize; | |
643 | myaddr += xfersize; | |
644 | len -= xfersize; | |
645 | } | |
646 | return origlen; /* no error possible */ | |
647 | } | |
648 | ||
b543979c | 649 | static void |
8f86a4e4 JG |
650 | remote_files_info (ignore) |
651 | struct target_ops *ignore; | |
bd5635a1 | 652 | { |
7c622b41 | 653 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
654 | } |
655 | \f | |
656 | /* | |
657 | ||
658 | A debug packet whose contents are <data> | |
659 | is encapsulated for transmission in the form: | |
660 | ||
661 | $ <data> # CSUM1 CSUM2 | |
662 | ||
663 | <data> must be ASCII alphanumeric and cannot include characters | |
664 | '$' or '#' | |
665 | ||
666 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
667 | checksum of <data>, the most significant nibble is sent first. | |
668 | the hex digits 0-9,a-f are used. | |
669 | ||
670 | Receiver responds with: | |
671 | ||
672 | + - if CSUM is correct and ready for next packet | |
673 | - - if CSUM is incorrect | |
674 | ||
675 | */ | |
676 | ||
ebdb9ade | 677 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 678 | |
bd5635a1 RP |
679 | static int |
680 | readchar () | |
681 | { | |
ebdb9ade | 682 | int ch; |
bd5635a1 | 683 | |
ebdb9ade | 684 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 685 | |
ebdb9ade JK |
686 | if (ch < 0) |
687 | return ch; | |
bd5635a1 | 688 | |
ebdb9ade | 689 | return ch & 0x7f; |
bd5635a1 RP |
690 | } |
691 | ||
692 | /* Send the command in BUF to the remote machine, | |
693 | and read the reply into BUF. | |
694 | Report an error if we get an error reply. */ | |
695 | ||
696 | static void | |
697 | remote_send (buf) | |
698 | char *buf; | |
699 | { | |
700 | ||
701 | putpkt (buf); | |
7c622b41 | 702 | getpkt (buf, 0); |
bd5635a1 RP |
703 | |
704 | if (buf[0] == 'E') | |
705 | error ("Remote failure reply: %s", buf); | |
706 | } | |
707 | ||
708 | /* Send a packet to the remote machine, with error checking. | |
709 | The data of the packet is in BUF. */ | |
710 | ||
711 | static void | |
712 | putpkt (buf) | |
713 | char *buf; | |
714 | { | |
715 | int i; | |
716 | unsigned char csum = 0; | |
b543979c | 717 | char buf2[PBUFSIZ]; |
bd5635a1 | 718 | int cnt = strlen (buf); |
ebdb9ade | 719 | int ch; |
bd5635a1 RP |
720 | char *p; |
721 | ||
722 | /* Copy the packet into buffer BUF2, encapsulating it | |
723 | and giving it a checksum. */ | |
724 | ||
b543979c JG |
725 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
726 | abort(); | |
727 | ||
bd5635a1 RP |
728 | p = buf2; |
729 | *p++ = '$'; | |
730 | ||
731 | for (i = 0; i < cnt; i++) | |
732 | { | |
733 | csum += buf[i]; | |
734 | *p++ = buf[i]; | |
735 | } | |
736 | *p++ = '#'; | |
737 | *p++ = tohex ((csum >> 4) & 0xf); | |
738 | *p++ = tohex (csum & 0xf); | |
739 | ||
740 | /* Send it over and over until we get a positive ack. */ | |
741 | ||
742 | do { | |
743 | if (kiodebug) | |
744 | { | |
745 | *p = '\0'; | |
8f86a4e4 | 746 | printf ("Sending packet: %s...", buf2); fflush(stdout); |
bd5635a1 | 747 | } |
ebdb9ade | 748 | SERIAL_WRITE (remote_desc, buf2, p - buf2); |
bd5635a1 | 749 | |
ebdb9ade | 750 | /* read until either a timeout occurs (-2) or '+' is read */ |
bd5635a1 RP |
751 | do { |
752 | ch = readchar (); | |
8f86a4e4 JG |
753 | if (kiodebug) { |
754 | if (ch == '+') | |
755 | printf("Ack\n"); | |
756 | else | |
757 | printf ("%02X%c ", ch&0xFF, ch); | |
758 | } | |
ebdb9ade | 759 | } while ((ch != '+') && (ch != SERIAL_TIMEOUT)); |
bd5635a1 RP |
760 | } while (ch != '+'); |
761 | } | |
762 | ||
763 | /* Read a packet from the remote machine, with error checking, | |
7c622b41 JG |
764 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
765 | If FOREVER, wait forever rather than timing out; this is used | |
766 | while the target is executing user code. */ | |
bd5635a1 RP |
767 | |
768 | static void | |
7c622b41 | 769 | getpkt (buf, forever) |
bd5635a1 | 770 | char *buf; |
ebdb9ade | 771 | int forever; |
bd5635a1 RP |
772 | { |
773 | char *bp; | |
774 | unsigned char csum; | |
7c622b41 | 775 | int c = 0; |
bd5635a1 | 776 | unsigned char c1, c2; |
38094c60 JG |
777 | int retries = 0; |
778 | #define MAX_RETRIES 10 | |
bd5635a1 | 779 | |
bd5635a1 RP |
780 | while (1) |
781 | { | |
7c622b41 JG |
782 | /* This can loop forever if the remote side sends us characters |
783 | continuously, but if it pauses, we'll get a zero from readchar | |
784 | because of timeout. Then we'll count that as a retry. */ | |
785 | while (c != '$') | |
ebdb9ade | 786 | if ((c = readchar()) == SERIAL_TIMEOUT) |
7c622b41 JG |
787 | if (!forever) |
788 | { | |
789 | if (++retries >= MAX_RETRIES) | |
790 | if (kiodebug) puts_filtered ("Timed out.\n"); | |
791 | goto out; | |
792 | } | |
793 | ||
bd5635a1 RP |
794 | /* Force csum to be zero here because of possible error retry. */ |
795 | csum = 0; | |
bd5635a1 | 796 | bp = buf; |
7c622b41 | 797 | |
bd5635a1 RP |
798 | while (1) |
799 | { | |
800 | c = readchar (); | |
ebdb9ade | 801 | if (c == SERIAL_TIMEOUT) |
7c622b41 JG |
802 | { |
803 | if (kiodebug) | |
804 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
805 | goto whole; /* Start a new packet, count retries */ | |
806 | } | |
807 | if (c == '$') | |
808 | { | |
809 | if (kiodebug) | |
810 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
811 | goto whole; /* Start a new packet, count retries */ | |
812 | } | |
bd5635a1 RP |
813 | if (c == '#') |
814 | break; | |
8f86a4e4 JG |
815 | if (bp >= buf+PBUFSIZ-1) |
816 | { | |
817 | *bp = '\0'; | |
7c622b41 JG |
818 | puts_filtered ("Remote packet too long: "); |
819 | puts_filtered (buf); | |
820 | puts_filtered ("\n"); | |
8f86a4e4 JG |
821 | goto whole; |
822 | } | |
bd5635a1 RP |
823 | *bp++ = c; |
824 | csum += c; | |
825 | } | |
826 | *bp = 0; | |
827 | ||
828 | c1 = fromhex (readchar ()); | |
829 | c2 = fromhex (readchar ()); | |
830 | if ((csum & 0xff) == (c1 << 4) + c2) | |
831 | break; | |
7c622b41 JG |
832 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
833 | (c1 << 4) + c2, csum & 0xff); | |
834 | puts_filtered (buf); | |
835 | puts_filtered ("\n"); | |
38094c60 | 836 | |
8f86a4e4 JG |
837 | /* Try the whole thing again. */ |
838 | whole: | |
38094c60 JG |
839 | if (++retries < MAX_RETRIES) |
840 | { | |
ebdb9ade | 841 | SERIAL_WRITE (remote_desc, "-", 1); |
38094c60 JG |
842 | } |
843 | else | |
844 | { | |
845 | printf ("Ignoring packet error, continuing...\n"); | |
846 | break; | |
847 | } | |
bd5635a1 RP |
848 | } |
849 | ||
7c622b41 JG |
850 | out: |
851 | ||
ebdb9ade | 852 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
853 | |
854 | if (kiodebug) | |
8f86a4e4 | 855 | fprintf (stderr,"Packet received: %s\n", buf); |
bd5635a1 RP |
856 | } |
857 | \f | |
858 | /* The data cache leads to incorrect results because it doesn't know about | |
859 | volatile variables, thus making it impossible to debug functions which | |
860 | use hardware registers. Therefore it is #if 0'd out. Effect on | |
861 | performance is some, for backtraces of functions with a few | |
862 | arguments each. For functions with many arguments, the stack | |
863 | frames don't fit in the cache blocks, which makes the cache less | |
864 | helpful. Disabling the cache is a big performance win for fetching | |
865 | large structures, because the cache code fetched data in 16-byte | |
866 | chunks. */ | |
867 | #if 0 | |
868 | /* The data cache records all the data read from the remote machine | |
869 | since the last time it stopped. | |
870 | ||
871 | Each cache block holds 16 bytes of data | |
872 | starting at a multiple-of-16 address. */ | |
873 | ||
874 | #define DCACHE_SIZE 64 /* Number of cache blocks */ | |
875 | ||
876 | struct dcache_block { | |
877 | struct dcache_block *next, *last; | |
878 | unsigned int addr; /* Address for which data is recorded. */ | |
879 | int data[4]; | |
880 | }; | |
881 | ||
882 | struct dcache_block dcache_free, dcache_valid; | |
883 | ||
884 | /* Free all the data cache blocks, thus discarding all cached data. */ | |
885 | ||
886 | static void | |
887 | dcache_flush () | |
888 | { | |
889 | register struct dcache_block *db; | |
890 | ||
891 | while ((db = dcache_valid.next) != &dcache_valid) | |
892 | { | |
893 | remque (db); | |
894 | insque (db, &dcache_free); | |
895 | } | |
896 | } | |
897 | ||
898 | /* | |
899 | * If addr is present in the dcache, return the address of the block | |
900 | * containing it. | |
901 | */ | |
902 | ||
903 | struct dcache_block * | |
904 | dcache_hit (addr) | |
905 | { | |
906 | register struct dcache_block *db; | |
907 | ||
908 | if (addr & 3) | |
909 | abort (); | |
910 | ||
911 | /* Search all cache blocks for one that is at this address. */ | |
912 | db = dcache_valid.next; | |
913 | while (db != &dcache_valid) | |
914 | { | |
915 | if ((addr & 0xfffffff0) == db->addr) | |
916 | return db; | |
917 | db = db->next; | |
918 | } | |
919 | return NULL; | |
920 | } | |
921 | ||
922 | /* Return the int data at address ADDR in dcache block DC. */ | |
923 | ||
924 | int | |
925 | dcache_value (db, addr) | |
926 | struct dcache_block *db; | |
927 | unsigned int addr; | |
928 | { | |
929 | if (addr & 3) | |
930 | abort (); | |
931 | return (db->data[(addr>>2)&3]); | |
932 | } | |
933 | ||
934 | /* Get a free cache block, put it on the valid list, | |
935 | and return its address. The caller should store into the block | |
936 | the address and data that it describes. */ | |
937 | ||
938 | struct dcache_block * | |
939 | dcache_alloc () | |
940 | { | |
941 | register struct dcache_block *db; | |
942 | ||
943 | if ((db = dcache_free.next) == &dcache_free) | |
944 | /* If we can't get one from the free list, take last valid */ | |
945 | db = dcache_valid.last; | |
946 | ||
947 | remque (db); | |
948 | insque (db, &dcache_valid); | |
949 | return (db); | |
950 | } | |
951 | ||
952 | /* Return the contents of the word at address ADDR in the remote machine, | |
953 | using the data cache. */ | |
954 | ||
955 | int | |
956 | dcache_fetch (addr) | |
957 | CORE_ADDR addr; | |
958 | { | |
959 | register struct dcache_block *db; | |
960 | ||
961 | db = dcache_hit (addr); | |
962 | if (db == 0) | |
963 | { | |
964 | db = dcache_alloc (); | |
965 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
966 | db->addr = addr & ~0xf; | |
967 | } | |
968 | return (dcache_value (db, addr)); | |
969 | } | |
970 | ||
971 | /* Write the word at ADDR both in the data cache and in the remote machine. */ | |
972 | ||
973 | dcache_poke (addr, data) | |
974 | CORE_ADDR addr; | |
975 | int data; | |
976 | { | |
977 | register struct dcache_block *db; | |
978 | ||
979 | /* First make sure the word is IN the cache. DB is its cache block. */ | |
980 | db = dcache_hit (addr); | |
981 | if (db == 0) | |
982 | { | |
983 | db = dcache_alloc (); | |
984 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
985 | db->addr = addr & ~0xf; | |
986 | } | |
987 | ||
988 | /* Modify the word in the cache. */ | |
989 | db->data[(addr>>2)&3] = data; | |
990 | ||
991 | /* Send the changed word. */ | |
992 | remote_write_bytes (addr, &data, 4); | |
993 | } | |
994 | ||
995 | /* Initialize the data cache. */ | |
996 | ||
997 | dcache_init () | |
998 | { | |
999 | register i; | |
1000 | register struct dcache_block *db; | |
1001 | ||
1002 | db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * | |
1003 | DCACHE_SIZE); | |
1004 | dcache_free.next = dcache_free.last = &dcache_free; | |
1005 | dcache_valid.next = dcache_valid.last = &dcache_valid; | |
1006 | for (i=0;i<DCACHE_SIZE;i++,db++) | |
1007 | insque (db, &dcache_free); | |
1008 | } | |
1009 | #endif /* 0 */ | |
ebdb9ade JK |
1010 | \f |
1011 | static void | |
1012 | remote_kill () | |
1013 | { | |
1014 | putpkt ("k"); | |
1015 | /* Don't wait for it to die. I'm not really sure it matters whether | |
1016 | we do or not. For the existing stubs, kill is a noop. */ | |
1017 | target_mourn_inferior (); | |
1018 | } | |
bd5635a1 | 1019 | |
ebdb9ade JK |
1020 | static void |
1021 | remote_mourn () | |
1022 | { | |
1023 | unpush_target (&remote_ops); | |
1024 | generic_mourn_inferior (); | |
1025 | } | |
1026 | \f | |
bd5635a1 RP |
1027 | /* Define the target subroutine names */ |
1028 | ||
1029 | struct target_ops remote_ops = { | |
b543979c JG |
1030 | "remote", /* to_shortname */ |
1031 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1032 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1033 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1034 | remote_open, /* to_open */ | |
1035 | remote_close, /* to_close */ | |
1036 | NULL, /* to_attach */ | |
1037 | remote_detach, /* to_detach */ | |
1038 | remote_resume, /* to_resume */ | |
1039 | remote_wait, /* to_wait */ | |
1040 | remote_fetch_registers, /* to_fetch_registers */ | |
1041 | remote_store_registers, /* to_store_registers */ | |
1042 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1043 | remote_xfer_memory, /* to_xfer_memory */ |
1044 | remote_files_info, /* to_files_info */ | |
1045 | NULL, /* to_insert_breakpoint */ | |
1046 | NULL, /* to_remove_breakpoint */ | |
1047 | NULL, /* to_terminal_init */ | |
1048 | NULL, /* to_terminal_inferior */ | |
1049 | NULL, /* to_terminal_ours_for_output */ | |
1050 | NULL, /* to_terminal_ours */ | |
1051 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1052 | remote_kill, /* to_kill */ |
b543979c JG |
1053 | NULL, /* to_load */ |
1054 | NULL, /* to_lookup_symbol */ | |
1055 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1056 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1057 | 0, /* to_can_run */ |
7c622b41 | 1058 | 0, /* to_notice_signals */ |
b543979c JG |
1059 | process_stratum, /* to_stratum */ |
1060 | NULL, /* to_next */ | |
1061 | 1, /* to_has_all_memory */ | |
1062 | 1, /* to_has_memory */ | |
1063 | 1, /* to_has_stack */ | |
1064 | 1, /* to_has_registers */ | |
1065 | 1, /* to_has_execution */ | |
1066 | NULL, /* sections */ | |
1067 | NULL, /* sections_end */ | |
1068 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1069 | }; |
1070 | ||
1071 | void | |
1072 | _initialize_remote () | |
1073 | { | |
1074 | add_target (&remote_ops); | |
8f86a4e4 JG |
1075 | |
1076 | add_show_from_set ( | |
1077 | add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug, | |
1078 | "Set debugging of remote serial I/O.\n\ | |
1079 | When enabled, each packet sent or received with the remote target\n\ | |
1080 | is displayed.", &setlist), | |
1081 | &showlist); | |
bd5635a1 | 1082 | } |
8f86a4e4 JG |
1083 | |
1084 | #endif |