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