Commit | Line | Data |
---|---|---|
b543979c JG |
1 | /* Remote target communications for serial-line targets in custom GDB protocol |
2 | Copyright 1988, 1991, 1992 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 | ||
66 | kill req k | |
67 | */ | |
68 | ||
d747e0af | 69 | #include "defs.h" |
bd5635a1 RP |
70 | #include <string.h> |
71 | #include <fcntl.h> | |
bd5635a1 RP |
72 | #include "frame.h" |
73 | #include "inferior.h" | |
74 | #include "target.h" | |
75 | #include "wait.h" | |
76 | #include "terminal.h" | |
77 | ||
78 | #ifdef USG | |
79 | #include <sys/types.h> | |
80 | #endif | |
81 | ||
82 | #include <signal.h> | |
83 | ||
b543979c JG |
84 | /* Prototypes for local functions */ |
85 | ||
86 | static void | |
87 | remote_write_bytes PARAMS ((CORE_ADDR, char *, int)); | |
88 | ||
89 | static void | |
90 | remote_read_bytes PARAMS ((CORE_ADDR, char *, int)); | |
91 | ||
92 | static void | |
93 | remote_files_info PARAMS ((struct target_ops *)); | |
94 | ||
95 | static int | |
96 | remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *)); | |
97 | ||
98 | static void | |
99 | remote_prepare_to_store PARAMS ((void)); | |
100 | ||
101 | static void | |
102 | remote_fetch_registers PARAMS ((int)); | |
103 | ||
104 | static void | |
105 | remote_resume PARAMS ((int, int)); | |
106 | ||
107 | static void | |
108 | remote_open PARAMS ((char *, int)); | |
109 | ||
110 | static void | |
111 | remote_close PARAMS ((int)); | |
112 | ||
113 | static void | |
114 | remote_store_registers PARAMS ((int)); | |
115 | ||
116 | static void | |
117 | getpkt PARAMS ((char *)); | |
118 | ||
119 | static void | |
120 | putpkt PARAMS ((char *)); | |
121 | ||
122 | static void | |
123 | remote_send PARAMS ((char *)); | |
124 | ||
125 | static int | |
126 | readchar PARAMS ((void)); | |
127 | ||
128 | static int | |
129 | remote_wait PARAMS ((WAITTYPE *)); | |
130 | ||
131 | static int | |
132 | tohex PARAMS ((int)); | |
133 | ||
134 | static int | |
135 | fromhex PARAMS ((int)); | |
136 | ||
137 | static void | |
138 | remote_detach PARAMS ((char *, int)); | |
139 | ||
bd5635a1 RP |
140 | |
141 | extern struct target_ops remote_ops; /* Forward decl */ | |
142 | ||
143 | static int kiodebug; | |
144 | static int timeout = 5; | |
145 | ||
146 | #if 0 | |
147 | int icache; | |
148 | #endif | |
149 | ||
150 | /* Descriptor for I/O to remote machine. Initialize it to -1 so that | |
151 | remote_open knows that we don't have a file open when the program | |
152 | starts. */ | |
153 | int remote_desc = -1; | |
154 | ||
b543979c | 155 | #define PBUFSIZ 1024 |
bd5635a1 RP |
156 | |
157 | /* Maximum number of bytes to read/write at once. The value here | |
158 | is chosen to fill up a packet (the headers account for the 32). */ | |
159 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
160 | ||
b543979c JG |
161 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
162 | #if REGISTER_BYTES > MAXBUFBYTES | |
163 | #undef PBUFSIZ | |
164 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) | |
bd5635a1 | 165 | #endif |
bd5635a1 RP |
166 | \f |
167 | /* Called when SIGALRM signal sent due to alarm() timeout. */ | |
168 | #ifndef HAVE_TERMIO | |
169 | void | |
170 | remote_timer () | |
171 | { | |
172 | if (kiodebug) | |
173 | printf ("remote_timer called\n"); | |
174 | ||
175 | alarm (timeout); | |
176 | } | |
177 | #endif | |
178 | ||
bd5635a1 RP |
179 | /* Clean up connection to a remote debugger. */ |
180 | ||
e1ce8aa5 | 181 | /* ARGSUSED */ |
b543979c | 182 | static void |
bd5635a1 RP |
183 | remote_close (quitting) |
184 | int quitting; | |
185 | { | |
186 | if (remote_desc >= 0) | |
187 | close (remote_desc); | |
188 | remote_desc = -1; | |
189 | } | |
190 | ||
b543979c JG |
191 | /* Translate baud rates from integers to damn B_codes. Unix should |
192 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
193 | ||
194 | #ifndef B19200 | |
195 | #define B19200 EXTA | |
196 | #endif | |
197 | #ifndef B38400 | |
198 | #define B38400 EXTB | |
199 | #endif | |
200 | ||
201 | static struct {int rate, damn_b;} baudtab[] = { | |
202 | {0, B0}, | |
203 | {50, B50}, | |
204 | {75, B75}, | |
205 | {110, B110}, | |
206 | {134, B134}, | |
207 | {150, B150}, | |
208 | {200, B200}, | |
209 | {300, B300}, | |
210 | {600, B600}, | |
211 | {1200, B1200}, | |
212 | {1800, B1800}, | |
213 | {2400, B2400}, | |
214 | {4800, B4800}, | |
215 | {9600, B9600}, | |
216 | {19200, B19200}, | |
217 | {38400, B38400}, | |
218 | {-1, -1}, | |
219 | }; | |
220 | ||
221 | static int | |
222 | damn_b (rate) | |
223 | int rate; | |
224 | { | |
225 | int i; | |
226 | ||
227 | for (i = 0; baudtab[i].rate != -1; i++) | |
228 | if (rate == baudtab[i].rate) return baudtab[i].damn_b; | |
229 | return B38400; /* Random */ | |
230 | } | |
231 | ||
bd5635a1 RP |
232 | /* Open a connection to a remote debugger. |
233 | NAME is the filename used for communication. */ | |
234 | ||
b543979c | 235 | static void |
bd5635a1 RP |
236 | remote_open (name, from_tty) |
237 | char *name; | |
238 | int from_tty; | |
239 | { | |
240 | TERMINAL sg; | |
b543979c JG |
241 | int a_rate, b_rate; |
242 | int baudrate_set = 0; | |
bd5635a1 RP |
243 | |
244 | if (name == 0) | |
245 | error ( | |
246 | "To open a remote debug connection, you need to specify what serial\n\ | |
247 | device is attached to the remote system (e.g. /dev/ttya)."); | |
248 | ||
f2fc6e7a JK |
249 | target_preopen (from_tty); |
250 | ||
bd5635a1 RP |
251 | remote_close (0); |
252 | ||
253 | #if 0 | |
254 | dcache_init (); | |
255 | #endif | |
256 | ||
257 | remote_desc = open (name, O_RDWR); | |
258 | if (remote_desc < 0) | |
259 | perror_with_name (name); | |
260 | ||
b543979c JG |
261 | if (baud_rate) |
262 | { | |
263 | if (1 != sscanf (baud_rate, "%d ", &a_rate)) | |
264 | { | |
265 | b_rate = damn_b (a_rate); | |
266 | baudrate_set = 1; | |
267 | } | |
268 | } | |
269 | ||
bd5635a1 RP |
270 | ioctl (remote_desc, TIOCGETP, &sg); |
271 | #ifdef HAVE_TERMIO | |
272 | sg.c_cc[VMIN] = 0; /* read with timeout. */ | |
273 | sg.c_cc[VTIME] = timeout * 10; | |
274 | sg.c_lflag &= ~(ICANON | ECHO); | |
b543979c JG |
275 | sg.c_cflag &= ~PARENB; /* No parity */ |
276 | sg.c_cflag |= CS8; /* 8-bit path */ | |
277 | if (baudrate_set) | |
d747e0af | 278 | sg.c_cflag = (sg.c_cflag & ~CBAUD) | b_rate; |
bd5635a1 | 279 | #else |
b543979c JG |
280 | sg.sg_flags |= RAW | ANYP; |
281 | sg.sg_flags &= ~ECHO; | |
282 | if (baudrate_set) | |
283 | { | |
284 | sg.sg_ispeed = b_rate; | |
285 | sg.sg_ospeed = b_rate; | |
286 | } | |
bd5635a1 RP |
287 | #endif |
288 | ioctl (remote_desc, TIOCSETP, &sg); | |
289 | ||
290 | if (from_tty) | |
291 | printf ("Remote debugging using %s\n", name); | |
292 | push_target (&remote_ops); /* Switch to using remote target now */ | |
bd5635a1 RP |
293 | |
294 | #ifndef HAVE_TERMIO | |
295 | #ifndef NO_SIGINTERRUPT | |
296 | /* Cause SIGALRM's to make reads fail. */ | |
297 | if (siginterrupt (SIGALRM, 1) != 0) | |
298 | perror ("remote_open: error in siginterrupt"); | |
299 | #endif | |
300 | ||
301 | /* Set up read timeout timer. */ | |
e1ce8aa5 | 302 | if ((void (*)()) signal (SIGALRM, remote_timer) == (void (*)()) -1) |
bd5635a1 RP |
303 | perror ("remote_open: error in signal"); |
304 | #endif | |
305 | ||
f2ebc25f JK |
306 | /* Ack any packet which the remote side has already sent. */ |
307 | write (remote_desc, "+", 1); | |
bd5635a1 | 308 | putpkt ("?"); /* initiate a query from remote machine */ |
f2ebc25f JK |
309 | |
310 | start_remote (); /* Initialize gdb process mechanisms */ | |
bd5635a1 RP |
311 | } |
312 | ||
313 | /* remote_detach() | |
314 | takes a program previously attached to and detaches it. | |
315 | We better not have left any breakpoints | |
316 | in the program or it'll die when it hits one. | |
317 | Close the open connection to the remote debugger. | |
318 | Use this when you want to detach and do something else | |
319 | with your gdb. */ | |
320 | ||
321 | static void | |
322 | remote_detach (args, from_tty) | |
323 | char *args; | |
324 | int from_tty; | |
325 | { | |
326 | if (args) | |
327 | error ("Argument given to \"detach\" when remotely debugging."); | |
328 | ||
329 | pop_target (); | |
330 | if (from_tty) | |
331 | printf ("Ending remote debugging.\n"); | |
332 | } | |
333 | ||
334 | /* Convert hex digit A to a number. */ | |
335 | ||
336 | static int | |
337 | fromhex (a) | |
338 | int a; | |
339 | { | |
340 | if (a >= '0' && a <= '9') | |
341 | return a - '0'; | |
342 | else if (a >= 'a' && a <= 'f') | |
343 | return a - 'a' + 10; | |
344 | else | |
345 | error ("Reply contains invalid hex digit"); | |
346 | return -1; | |
347 | } | |
348 | ||
349 | /* Convert number NIB to a hex digit. */ | |
350 | ||
351 | static int | |
352 | tohex (nib) | |
353 | int nib; | |
354 | { | |
355 | if (nib < 10) | |
356 | return '0'+nib; | |
357 | else | |
358 | return 'a'+nib-10; | |
359 | } | |
360 | \f | |
361 | /* Tell the remote machine to resume. */ | |
362 | ||
b543979c | 363 | static void |
bd5635a1 RP |
364 | remote_resume (step, siggnal) |
365 | int step, siggnal; | |
366 | { | |
367 | char buf[PBUFSIZ]; | |
368 | ||
369 | if (siggnal) | |
370 | error ("Can't send signals to a remote system."); | |
371 | ||
372 | #if 0 | |
373 | dcache_flush (); | |
374 | #endif | |
375 | ||
376 | strcpy (buf, step ? "s": "c"); | |
377 | ||
378 | putpkt (buf); | |
379 | } | |
380 | ||
b543979c JG |
381 | /* Send ^C to target to halt it. Target will respond, and send us a |
382 | packet. */ | |
383 | ||
384 | void remote_interrupt() | |
385 | { | |
386 | write (remote_desc, "\003", 1); /* Send a ^C */ | |
387 | } | |
388 | ||
389 | ||
bd5635a1 | 390 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
391 | storing status in STATUS just as `wait' would. |
392 | Returns "pid" (though it's not clear what, if anything, that | |
393 | means in the case of this target). */ | |
bd5635a1 | 394 | |
b543979c | 395 | static int |
bd5635a1 RP |
396 | remote_wait (status) |
397 | WAITTYPE *status; | |
398 | { | |
399 | unsigned char buf[PBUFSIZ]; | |
b543979c JG |
400 | void (*ofunc)(); |
401 | ||
bd5635a1 | 402 | WSETEXIT ((*status), 0); |
b543979c JG |
403 | |
404 | ofunc = signal (SIGINT, remote_interrupt); | |
405 | getpkt ((char *) buf); | |
406 | signal (SIGINT, ofunc); | |
407 | ||
bd5635a1 RP |
408 | if (buf[0] == 'E') |
409 | error ("Remote failure reply: %s", buf); | |
410 | if (buf[0] != 'S') | |
411 | error ("Invalid remote reply: %s", buf); | |
412 | WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2])))); | |
e1ce8aa5 | 413 | return 0; |
bd5635a1 RP |
414 | } |
415 | ||
416 | /* Read the remote registers into the block REGS. */ | |
417 | ||
e1ce8aa5 JK |
418 | /* Currently we just read all the registers, so we don't use regno. */ |
419 | /* ARGSUSED */ | |
b543979c | 420 | static void |
bd5635a1 RP |
421 | remote_fetch_registers (regno) |
422 | int regno; | |
423 | { | |
424 | char buf[PBUFSIZ]; | |
425 | int i; | |
426 | char *p; | |
427 | char regs[REGISTER_BYTES]; | |
428 | ||
429 | sprintf (buf, "g"); | |
430 | remote_send (buf); | |
431 | ||
432 | /* Reply describes registers byte by byte, each byte encoded as two | |
433 | hex characters. Suck them all up, then supply them to the | |
434 | register cacheing/storage mechanism. */ | |
435 | ||
436 | p = buf; | |
437 | for (i = 0; i < REGISTER_BYTES; i++) | |
438 | { | |
439 | if (p[0] == 0 || p[1] == 0) | |
440 | error ("Remote reply is too short: %s", buf); | |
441 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
442 | p += 2; | |
443 | } | |
444 | for (i = 0; i < NUM_REGS; i++) | |
445 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
446 | } |
447 | ||
448 | /* Prepare to store registers. Since we send them all, we have to | |
449 | read out the ones we don't want to change first. */ | |
450 | ||
b543979c | 451 | static void |
bd5635a1 RP |
452 | remote_prepare_to_store () |
453 | { | |
454 | remote_fetch_registers (-1); | |
455 | } | |
456 | ||
457 | /* Store the remote registers from the contents of the block REGISTERS. | |
458 | FIXME, eventually just store one register if that's all that is needed. */ | |
459 | ||
e1ce8aa5 | 460 | /* ARGSUSED */ |
b543979c | 461 | static void |
bd5635a1 RP |
462 | remote_store_registers (regno) |
463 | int regno; | |
464 | { | |
465 | char buf[PBUFSIZ]; | |
466 | int i; | |
467 | char *p; | |
468 | ||
469 | buf[0] = 'G'; | |
470 | ||
471 | /* Command describes registers byte by byte, | |
472 | each byte encoded as two hex characters. */ | |
473 | ||
474 | p = buf + 1; | |
475 | for (i = 0; i < REGISTER_BYTES; i++) | |
476 | { | |
477 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
478 | *p++ = tohex (registers[i] & 0xf); | |
479 | } | |
480 | *p = '\0'; | |
481 | ||
482 | remote_send (buf); | |
bd5635a1 RP |
483 | } |
484 | ||
485 | #if 0 | |
486 | /* Read a word from remote address ADDR and return it. | |
487 | This goes through the data cache. */ | |
488 | ||
489 | int | |
490 | remote_fetch_word (addr) | |
491 | CORE_ADDR addr; | |
492 | { | |
493 | if (icache) | |
494 | { | |
495 | extern CORE_ADDR text_start, text_end; | |
496 | ||
497 | if (addr >= text_start && addr < text_end) | |
498 | { | |
499 | int buffer; | |
500 | xfer_core_file (addr, &buffer, sizeof (int)); | |
501 | return buffer; | |
502 | } | |
503 | } | |
504 | return dcache_fetch (addr); | |
505 | } | |
506 | ||
507 | /* Write a word WORD into remote address ADDR. | |
508 | This goes through the data cache. */ | |
509 | ||
510 | void | |
511 | remote_store_word (addr, word) | |
512 | CORE_ADDR addr; | |
513 | int word; | |
514 | { | |
515 | dcache_poke (addr, word); | |
516 | } | |
517 | #endif /* 0 */ | |
518 | \f | |
519 | /* Write memory data directly to the remote machine. | |
520 | This does not inform the data cache; the data cache uses this. | |
521 | MEMADDR is the address in the remote memory space. | |
522 | MYADDR is the address of the buffer in our space. | |
523 | LEN is the number of bytes. */ | |
524 | ||
b543979c | 525 | static void |
bd5635a1 RP |
526 | remote_write_bytes (memaddr, myaddr, len) |
527 | CORE_ADDR memaddr; | |
528 | char *myaddr; | |
529 | int len; | |
530 | { | |
531 | char buf[PBUFSIZ]; | |
532 | int i; | |
533 | char *p; | |
534 | ||
535 | if (len > PBUFSIZ / 2 - 20) | |
536 | abort (); | |
537 | ||
538 | sprintf (buf, "M%x,%x:", memaddr, len); | |
539 | ||
b543979c | 540 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
541 | each byte encoded as two hex characters. */ |
542 | ||
543 | p = buf + strlen (buf); | |
544 | for (i = 0; i < len; i++) | |
545 | { | |
546 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
547 | *p++ = tohex (myaddr[i] & 0xf); | |
548 | } | |
549 | *p = '\0'; | |
550 | ||
551 | remote_send (buf); | |
552 | } | |
553 | ||
554 | /* Read memory data directly from the remote machine. | |
555 | This does not use the data cache; the data cache uses this. | |
556 | MEMADDR is the address in the remote memory space. | |
557 | MYADDR is the address of the buffer in our space. | |
558 | LEN is the number of bytes. */ | |
559 | ||
b543979c | 560 | static void |
bd5635a1 RP |
561 | remote_read_bytes (memaddr, myaddr, len) |
562 | CORE_ADDR memaddr; | |
563 | char *myaddr; | |
564 | int len; | |
565 | { | |
566 | char buf[PBUFSIZ]; | |
567 | int i; | |
568 | char *p; | |
569 | ||
570 | if (len > PBUFSIZ / 2 - 1) | |
571 | abort (); | |
572 | ||
573 | sprintf (buf, "m%x,%x", memaddr, len); | |
574 | remote_send (buf); | |
575 | ||
b543979c | 576 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
577 | each byte encoded as two hex characters. */ |
578 | ||
579 | p = buf; | |
580 | for (i = 0; i < len; i++) | |
581 | { | |
582 | if (p[0] == 0 || p[1] == 0) | |
583 | error ("Remote reply is too short: %s", buf); | |
584 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
585 | p += 2; | |
586 | } | |
587 | } | |
588 | \f | |
589 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 590 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
591 | nonzero. Returns length of data written or read; 0 for error. */ |
592 | ||
b543979c JG |
593 | /* ARGSUSED */ |
594 | static int | |
595 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
596 | CORE_ADDR memaddr; |
597 | char *myaddr; | |
598 | int len; | |
e1ce8aa5 | 599 | int should_write; |
b543979c | 600 | struct target_ops *target; /* ignored */ |
bd5635a1 RP |
601 | { |
602 | int origlen = len; | |
603 | int xfersize; | |
604 | while (len > 0) | |
605 | { | |
606 | if (len > MAXBUFBYTES) | |
607 | xfersize = MAXBUFBYTES; | |
608 | else | |
609 | xfersize = len; | |
610 | ||
e1ce8aa5 | 611 | if (should_write) |
bd5635a1 RP |
612 | remote_write_bytes(memaddr, myaddr, xfersize); |
613 | else | |
614 | remote_read_bytes (memaddr, myaddr, xfersize); | |
615 | memaddr += xfersize; | |
616 | myaddr += xfersize; | |
617 | len -= xfersize; | |
618 | } | |
619 | return origlen; /* no error possible */ | |
620 | } | |
621 | ||
b543979c JG |
622 | static void |
623 | remote_files_info (target) | |
624 | struct target_ops *target; | |
bd5635a1 RP |
625 | { |
626 | printf ("remote files info missing here. FIXME.\n"); | |
627 | } | |
628 | \f | |
629 | /* | |
630 | ||
631 | A debug packet whose contents are <data> | |
632 | is encapsulated for transmission in the form: | |
633 | ||
634 | $ <data> # CSUM1 CSUM2 | |
635 | ||
636 | <data> must be ASCII alphanumeric and cannot include characters | |
637 | '$' or '#' | |
638 | ||
639 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
640 | checksum of <data>, the most significant nibble is sent first. | |
641 | the hex digits 0-9,a-f are used. | |
642 | ||
643 | Receiver responds with: | |
644 | ||
645 | + - if CSUM is correct and ready for next packet | |
646 | - - if CSUM is incorrect | |
647 | ||
648 | */ | |
649 | ||
b543979c JG |
650 | /* Read a single character from the remote end. |
651 | (If supported, we actually read many characters and buffer them up.) */ | |
652 | ||
bd5635a1 RP |
653 | static int |
654 | readchar () | |
655 | { | |
656 | char buf; | |
b543979c JG |
657 | static int inbuf_index, inbuf_count; |
658 | #define INBUFSIZE PBUFSIZ | |
659 | static char inbuf[INBUFSIZE]; | |
bd5635a1 | 660 | |
b543979c JG |
661 | if (inbuf_index >= inbuf_count) |
662 | { | |
663 | /* Time to do another read... */ | |
664 | inbuf_index = 0; | |
665 | inbuf_count = 0; | |
666 | inbuf[0] = 0; /* Just in case */ | |
bd5635a1 | 667 | #ifdef HAVE_TERMIO |
b543979c JG |
668 | /* termio does the timeout for us. */ |
669 | inbuf_count = read (remote_desc, inbuf, INBUFSIZE); | |
bd5635a1 | 670 | #else |
b543979c JG |
671 | alarm (timeout); |
672 | inbuf_count = read (remote_desc, inbuf, INBUFSIZE); | |
673 | alarm (0); | |
bd5635a1 | 674 | #endif |
b543979c | 675 | } |
bd5635a1 | 676 | |
b543979c JG |
677 | /* Just return the next character from the buffer. */ |
678 | return inbuf[inbuf_index++] & 0x7f; | |
bd5635a1 RP |
679 | } |
680 | ||
681 | /* Send the command in BUF to the remote machine, | |
682 | and read the reply into BUF. | |
683 | Report an error if we get an error reply. */ | |
684 | ||
685 | static void | |
686 | remote_send (buf) | |
687 | char *buf; | |
688 | { | |
689 | ||
690 | putpkt (buf); | |
691 | getpkt (buf); | |
692 | ||
693 | if (buf[0] == 'E') | |
694 | error ("Remote failure reply: %s", buf); | |
695 | } | |
696 | ||
697 | /* Send a packet to the remote machine, with error checking. | |
698 | The data of the packet is in BUF. */ | |
699 | ||
700 | static void | |
701 | putpkt (buf) | |
702 | char *buf; | |
703 | { | |
704 | int i; | |
705 | unsigned char csum = 0; | |
b543979c | 706 | char buf2[PBUFSIZ]; |
bd5635a1 RP |
707 | int cnt = strlen (buf); |
708 | char ch; | |
709 | char *p; | |
710 | ||
711 | /* Copy the packet into buffer BUF2, encapsulating it | |
712 | and giving it a checksum. */ | |
713 | ||
b543979c JG |
714 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
715 | abort(); | |
716 | ||
bd5635a1 RP |
717 | p = buf2; |
718 | *p++ = '$'; | |
719 | ||
720 | for (i = 0; i < cnt; i++) | |
721 | { | |
722 | csum += buf[i]; | |
723 | *p++ = buf[i]; | |
724 | } | |
725 | *p++ = '#'; | |
726 | *p++ = tohex ((csum >> 4) & 0xf); | |
727 | *p++ = tohex (csum & 0xf); | |
728 | ||
729 | /* Send it over and over until we get a positive ack. */ | |
730 | ||
731 | do { | |
732 | if (kiodebug) | |
733 | { | |
734 | *p = '\0'; | |
735 | printf ("Sending packet: %s (%s)\n", buf2, buf); | |
736 | } | |
737 | write (remote_desc, buf2, p - buf2); | |
738 | ||
739 | /* read until either a timeout occurs (\0) or '+' is read */ | |
740 | do { | |
741 | ch = readchar (); | |
742 | } while ((ch != '+') && (ch != '\0')); | |
743 | } while (ch != '+'); | |
744 | } | |
745 | ||
746 | /* Read a packet from the remote machine, with error checking, | |
747 | and store it in BUF. */ | |
748 | ||
749 | static void | |
750 | getpkt (buf) | |
751 | char *buf; | |
752 | { | |
753 | char *bp; | |
754 | unsigned char csum; | |
755 | int c; | |
756 | unsigned char c1, c2; | |
757 | ||
eb7ba50c JK |
758 | #if 0 |
759 | /* Sorry, this will cause all hell to break loose, i.e. we'll end | |
760 | up in the command loop with an inferior, but (at least if this | |
761 | happens in remote_wait or some such place) without a current_frame, | |
762 | having set up prev_* in wait_for_inferior, etc. | |
763 | ||
764 | If it is necessary to have such an "emergency exit", seems like | |
765 | the only plausible thing to do is to say the inferior died, and | |
766 | make the user reattach if they want to. Perhaps with a prompt | |
767 | asking for confirmation. */ | |
768 | ||
bd5635a1 RP |
769 | /* allow immediate quit while reading from device, it could be hung */ |
770 | immediate_quit++; | |
eb7ba50c | 771 | #endif /* 0 */ |
bd5635a1 RP |
772 | |
773 | while (1) | |
774 | { | |
775 | /* Force csum to be zero here because of possible error retry. */ | |
776 | csum = 0; | |
777 | ||
778 | while ((c = readchar()) != '$'); | |
779 | ||
780 | bp = buf; | |
781 | while (1) | |
782 | { | |
783 | c = readchar (); | |
784 | if (c == '#') | |
785 | break; | |
786 | *bp++ = c; | |
787 | csum += c; | |
788 | } | |
789 | *bp = 0; | |
790 | ||
791 | c1 = fromhex (readchar ()); | |
792 | c2 = fromhex (readchar ()); | |
793 | if ((csum & 0xff) == (c1 << 4) + c2) | |
794 | break; | |
795 | printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
796 | (c1 << 4) + c2, csum & 0xff, buf); | |
797 | write (remote_desc, "-", 1); | |
798 | } | |
799 | ||
eb7ba50c | 800 | #if 0 |
bd5635a1 | 801 | immediate_quit--; |
eb7ba50c | 802 | #endif |
bd5635a1 RP |
803 | |
804 | write (remote_desc, "+", 1); | |
805 | ||
806 | if (kiodebug) | |
807 | fprintf (stderr,"Packet received :%s\n", buf); | |
808 | } | |
809 | \f | |
810 | /* The data cache leads to incorrect results because it doesn't know about | |
811 | volatile variables, thus making it impossible to debug functions which | |
812 | use hardware registers. Therefore it is #if 0'd out. Effect on | |
813 | performance is some, for backtraces of functions with a few | |
814 | arguments each. For functions with many arguments, the stack | |
815 | frames don't fit in the cache blocks, which makes the cache less | |
816 | helpful. Disabling the cache is a big performance win for fetching | |
817 | large structures, because the cache code fetched data in 16-byte | |
818 | chunks. */ | |
819 | #if 0 | |
820 | /* The data cache records all the data read from the remote machine | |
821 | since the last time it stopped. | |
822 | ||
823 | Each cache block holds 16 bytes of data | |
824 | starting at a multiple-of-16 address. */ | |
825 | ||
826 | #define DCACHE_SIZE 64 /* Number of cache blocks */ | |
827 | ||
828 | struct dcache_block { | |
829 | struct dcache_block *next, *last; | |
830 | unsigned int addr; /* Address for which data is recorded. */ | |
831 | int data[4]; | |
832 | }; | |
833 | ||
834 | struct dcache_block dcache_free, dcache_valid; | |
835 | ||
836 | /* Free all the data cache blocks, thus discarding all cached data. */ | |
837 | ||
838 | static void | |
839 | dcache_flush () | |
840 | { | |
841 | register struct dcache_block *db; | |
842 | ||
843 | while ((db = dcache_valid.next) != &dcache_valid) | |
844 | { | |
845 | remque (db); | |
846 | insque (db, &dcache_free); | |
847 | } | |
848 | } | |
849 | ||
850 | /* | |
851 | * If addr is present in the dcache, return the address of the block | |
852 | * containing it. | |
853 | */ | |
854 | ||
855 | struct dcache_block * | |
856 | dcache_hit (addr) | |
857 | { | |
858 | register struct dcache_block *db; | |
859 | ||
860 | if (addr & 3) | |
861 | abort (); | |
862 | ||
863 | /* Search all cache blocks for one that is at this address. */ | |
864 | db = dcache_valid.next; | |
865 | while (db != &dcache_valid) | |
866 | { | |
867 | if ((addr & 0xfffffff0) == db->addr) | |
868 | return db; | |
869 | db = db->next; | |
870 | } | |
871 | return NULL; | |
872 | } | |
873 | ||
874 | /* Return the int data at address ADDR in dcache block DC. */ | |
875 | ||
876 | int | |
877 | dcache_value (db, addr) | |
878 | struct dcache_block *db; | |
879 | unsigned int addr; | |
880 | { | |
881 | if (addr & 3) | |
882 | abort (); | |
883 | return (db->data[(addr>>2)&3]); | |
884 | } | |
885 | ||
886 | /* Get a free cache block, put it on the valid list, | |
887 | and return its address. The caller should store into the block | |
888 | the address and data that it describes. */ | |
889 | ||
890 | struct dcache_block * | |
891 | dcache_alloc () | |
892 | { | |
893 | register struct dcache_block *db; | |
894 | ||
895 | if ((db = dcache_free.next) == &dcache_free) | |
896 | /* If we can't get one from the free list, take last valid */ | |
897 | db = dcache_valid.last; | |
898 | ||
899 | remque (db); | |
900 | insque (db, &dcache_valid); | |
901 | return (db); | |
902 | } | |
903 | ||
904 | /* Return the contents of the word at address ADDR in the remote machine, | |
905 | using the data cache. */ | |
906 | ||
907 | int | |
908 | dcache_fetch (addr) | |
909 | CORE_ADDR addr; | |
910 | { | |
911 | register struct dcache_block *db; | |
912 | ||
913 | db = dcache_hit (addr); | |
914 | if (db == 0) | |
915 | { | |
916 | db = dcache_alloc (); | |
917 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
918 | db->addr = addr & ~0xf; | |
919 | } | |
920 | return (dcache_value (db, addr)); | |
921 | } | |
922 | ||
923 | /* Write the word at ADDR both in the data cache and in the remote machine. */ | |
924 | ||
925 | dcache_poke (addr, data) | |
926 | CORE_ADDR addr; | |
927 | int data; | |
928 | { | |
929 | register struct dcache_block *db; | |
930 | ||
931 | /* First make sure the word is IN the cache. DB is its cache block. */ | |
932 | db = dcache_hit (addr); | |
933 | if (db == 0) | |
934 | { | |
935 | db = dcache_alloc (); | |
936 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
937 | db->addr = addr & ~0xf; | |
938 | } | |
939 | ||
940 | /* Modify the word in the cache. */ | |
941 | db->data[(addr>>2)&3] = data; | |
942 | ||
943 | /* Send the changed word. */ | |
944 | remote_write_bytes (addr, &data, 4); | |
945 | } | |
946 | ||
947 | /* Initialize the data cache. */ | |
948 | ||
949 | dcache_init () | |
950 | { | |
951 | register i; | |
952 | register struct dcache_block *db; | |
953 | ||
954 | db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * | |
955 | DCACHE_SIZE); | |
956 | dcache_free.next = dcache_free.last = &dcache_free; | |
957 | dcache_valid.next = dcache_valid.last = &dcache_valid; | |
958 | for (i=0;i<DCACHE_SIZE;i++,db++) | |
959 | insque (db, &dcache_free); | |
960 | } | |
961 | #endif /* 0 */ | |
962 | ||
963 | /* Define the target subroutine names */ | |
964 | ||
965 | struct target_ops remote_ops = { | |
b543979c JG |
966 | "remote", /* to_shortname */ |
967 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
968 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
969 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
970 | remote_open, /* to_open */ | |
971 | remote_close, /* to_close */ | |
972 | NULL, /* to_attach */ | |
973 | remote_detach, /* to_detach */ | |
974 | remote_resume, /* to_resume */ | |
975 | remote_wait, /* to_wait */ | |
976 | remote_fetch_registers, /* to_fetch_registers */ | |
977 | remote_store_registers, /* to_store_registers */ | |
978 | remote_prepare_to_store, /* to_prepare_to_store */ | |
979 | NULL, /* to_convert_to_virtual */ | |
980 | NULL, /* to_convert_from_virtual */ | |
981 | remote_xfer_memory, /* to_xfer_memory */ | |
982 | remote_files_info, /* to_files_info */ | |
983 | NULL, /* to_insert_breakpoint */ | |
984 | NULL, /* to_remove_breakpoint */ | |
985 | NULL, /* to_terminal_init */ | |
986 | NULL, /* to_terminal_inferior */ | |
987 | NULL, /* to_terminal_ours_for_output */ | |
988 | NULL, /* to_terminal_ours */ | |
989 | NULL, /* to_terminal_info */ | |
990 | NULL, /* to_kill */ | |
991 | NULL, /* to_load */ | |
992 | NULL, /* to_lookup_symbol */ | |
993 | NULL, /* to_create_inferior */ | |
994 | NULL, /* to_mourn_inferior */ | |
995 | process_stratum, /* to_stratum */ | |
996 | NULL, /* to_next */ | |
997 | 1, /* to_has_all_memory */ | |
998 | 1, /* to_has_memory */ | |
999 | 1, /* to_has_stack */ | |
1000 | 1, /* to_has_registers */ | |
1001 | 1, /* to_has_execution */ | |
1002 | NULL, /* sections */ | |
1003 | NULL, /* sections_end */ | |
1004 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1005 | }; |
1006 | ||
1007 | void | |
1008 | _initialize_remote () | |
1009 | { | |
1010 | add_target (&remote_ops); | |
1011 | } |