fix typo, added @table
[deliverable/binutils-gdb.git] / gdb / remote-ser.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 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
71 kill req k
72 */
73
74 #include "defs.h"
75 #include <string.h>
76 #include "serial.h"
77 #include "frame.h"
78 #include "inferior.h"
79 #include "target.h"
80 #include "wait.h"
81 #include "terminal.h"
82 #include "gdbcmd.h"
83
84 #include <signal.h>
85
86 static int kiodebug = 0;
87 static int timeout = 5;
88
89 #define PBUFSIZ 1024
90
91 /* Maximum number of bytes to read/write at once. The value here
92 is chosen to fill up a packet (the headers account for the 32). */
93 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
94
95 /* Round up PBUFSIZ to hold all the registers, at least. */
96 #if REGISTER_BYTES > MAXBUFBYTES
97 #undef PBUFSIZ
98 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
99 #endif
100 \f
101 /* remote_detach()
102 takes a program previously attached to and detaches it.
103 We better not have left any breakpoints
104 in the program or it'll die when it hits one.
105 Close the open connection to the remote debugger.
106 Use this when you want to detach and do something else
107 with your gdb. */
108
109 static void
110 remote_detach (args, from_tty)
111 char *args;
112 int from_tty;
113 {
114 if (args)
115 error ("Argument given to \"detach\" when remotely debugging.");
116
117 pop_target ();
118 if (from_tty)
119 printf ("Ending remote debugging.\n");
120 }
121
122 /* Convert hex digit A to a number. */
123
124 static int
125 fromhex (a)
126 int a;
127 {
128 if (a >= '0' && a <= '9')
129 return a - '0';
130 else if (a >= 'a' && a <= 'f')
131 return a - 'a' + 10;
132 else
133 error ("Reply contains invalid hex digit");
134 return -1;
135 }
136
137 /* Convert number NIB to a hex digit. */
138
139 static int
140 tohex (nib)
141 int nib;
142 {
143 if (nib < 10)
144 return '0'+nib;
145 else
146 return 'a'+nib-10;
147 }
148 \f
149 /* Tell the remote machine to resume. */
150
151 /* Send a packet to the remote machine, with error checking.
152 The data of the packet is in BUF. */
153
154 static void
155 putpkt (buf)
156 char *buf;
157 {
158 int i;
159 unsigned char csum = 0;
160 char buf2[PBUFSIZ];
161 int cnt = strlen (buf);
162 char ch;
163 char *p;
164
165 /* Copy the packet into buffer BUF2, encapsulating it
166 and giving it a checksum. */
167
168 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
169 abort();
170
171 p = buf2;
172 *p++ = '$';
173
174 for (i = 0; i < cnt; i++)
175 {
176 csum += buf[i];
177 *p++ = buf[i];
178 }
179 *p++ = '#';
180 *p++ = tohex ((csum >> 4) & 0xf);
181 *p++ = tohex (csum & 0xf);
182
183 /* Send it over and over until we get a positive ack. */
184
185 do {
186 if (kiodebug)
187 {
188 *p = '\0';
189 printf ("Sending packet: %s...", buf2); fflush(stdout);
190 }
191 serial_write (buf2, p - buf2);
192
193 /* read until either a timeout occurs (\0) or '+' is read */
194 do {
195 ch = readchar ();
196 if (kiodebug) {
197 if (ch == '+')
198 printf("Ack\n");
199 else
200 printf ("%02X%c ", ch&0xFF, ch);
201 }
202 } while ((ch != '+') && (ch != '\0'));
203 } while (ch != '+');
204 }
205
206 /* Read a packet from the remote machine, with error checking,
207 and store it in BUF. BUF is expected to be of size PBUFSIZ. */
208
209 static void
210 getpkt (buf)
211 char *buf;
212 {
213 char *bp;
214 unsigned char csum;
215 int c;
216 unsigned char c1, c2;
217
218 while (1)
219 {
220 /* Force csum to be zero here because of possible error retry. */
221 csum = 0;
222
223 while ((c = readchar()) != '$');
224
225 bp = buf;
226 while (1)
227 {
228 c = readchar ();
229 if (c == '#')
230 break;
231 if (bp >= buf+PBUFSIZ-1)
232 {
233 *bp = '\0';
234 printf_filtered ("Remote packet too long: %s\n", buf);
235 goto whole;
236 }
237 *bp++ = c;
238 csum += c;
239 }
240 *bp = 0;
241
242 c1 = fromhex (readchar ());
243 c2 = fromhex (readchar ());
244 if ((csum & 0xff) == (c1 << 4) + c2)
245 break;
246 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
247 (c1 << 4) + c2, csum & 0xff, buf);
248 /* Try the whole thing again. */
249 whole:
250 serial_write ("-", 1);
251 }
252
253 #if 0
254 immediate_quit--;
255 #endif
256
257 serial_write ("+", 1);
258
259 if (kiodebug)
260 fprintf (stderr,"Packet received: %s\n", buf);
261 }
262
263 static void
264 remote_resume (step, siggnal)
265 int step, siggnal;
266 {
267 char buf[PBUFSIZ];
268
269 if (siggnal)
270 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
271 siggnal);
272
273 #if 0
274 dcache_flush ();
275 #endif
276
277 strcpy (buf, step ? "s": "c");
278
279 putpkt (buf);
280 }
281
282 /* Send ^C to target to halt it. Target will respond, and send us a
283 packet. */
284
285 void remote_interrupt(signo)
286 int signo;
287 {
288
289 if (kiodebug)
290 printf ("remote_interrupt called\n");
291
292 serial_write ("\003", 1); /* Send a ^C */
293 }
294
295
296 /* Wait until the remote machine stops, then return,
297 storing status in STATUS just as `wait' would.
298 Returns "pid" (though it's not clear what, if anything, that
299 means in the case of this target). */
300
301 static int
302 remote_wait (status)
303 WAITTYPE *status;
304 {
305 unsigned char buf[PBUFSIZ];
306 void (*ofunc)();
307 unsigned char *p;
308 int i;
309 char regs[REGISTER_RAW_SIZE (PC_REGNUM) + REGISTER_RAW_SIZE (FP_REGNUM)];
310
311 WSETEXIT ((*status), 0);
312
313 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
314 getpkt ((char *) buf);
315 signal (SIGINT, ofunc);
316
317 if (buf[0] == 'E')
318 error ("Remote failure reply: %s", buf);
319 if (buf[0] == 'T')
320 {
321 /* Expedited reply, containing Signal, PC, and FP. */
322 p = &buf[3]; /* after Txx */
323 for (i = 0; i < sizeof (regs); i++)
324 {
325 if (p[0] == 0 || p[1] == 0)
326 error ("Remote reply is too short: %s", buf);
327 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
328 p += 2;
329 }
330 supply_register (PC_REGNUM, &regs[0]);
331 supply_register (FP_REGNUM, &regs[REGISTER_RAW_SIZE (PC_REGNUM)]);
332 }
333 else if (buf[0] != 'S')
334 error ("Invalid remote reply: %s", buf);
335
336 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
337
338 return 0;
339 }
340
341 /* Send the command in BUF to the remote machine,
342 and read the reply into BUF.
343 Report an error if we get an error reply. */
344
345 static void
346 remote_send (buf)
347 char *buf;
348 {
349
350 putpkt (buf);
351 getpkt (buf);
352
353 if (buf[0] == 'E')
354 error ("Remote failure reply: %s", buf);
355 }
356
357 /* Read the remote registers into the block REGS. */
358 /* Currently we just read all the registers, so we don't use regno. */
359 /* ARGSUSED */
360 static void
361 remote_fetch_registers (regno)
362 int regno;
363 {
364 char buf[PBUFSIZ];
365 int i;
366 char *p;
367 char regs[REGISTER_BYTES];
368
369 sprintf (buf, "g");
370 remote_send (buf);
371
372 /* Reply describes registers byte by byte, each byte encoded as two
373 hex characters. Suck them all up, then supply them to the
374 register cacheing/storage mechanism. */
375
376 p = buf;
377 for (i = 0; i < REGISTER_BYTES; i++)
378 {
379 if (p[0] == 0 || p[1] == 0)
380 error ("Remote reply is too short: %s", buf);
381 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
382 p += 2;
383 }
384 for (i = 0; i < NUM_REGS; i++)
385 supply_register (i, &regs[REGISTER_BYTE(i)]);
386 }
387
388 /* Prepare to store registers. Since we send them all, we have to
389 read out the ones we don't want to change first. */
390
391 static void
392 remote_prepare_to_store ()
393 {
394 remote_fetch_registers (-1);
395 }
396
397 /* Store the remote registers from the contents of the block REGISTERS.
398 FIXME, eventually just store one register if that's all that is needed. */
399
400 /* ARGSUSED */
401 static void
402 remote_store_registers (regno)
403 int regno;
404 {
405 char buf[PBUFSIZ];
406 int i;
407 char *p;
408
409 buf[0] = 'G';
410
411 /* Command describes registers byte by byte,
412 each byte encoded as two hex characters. */
413
414 p = buf + 1;
415 for (i = 0; i < REGISTER_BYTES; i++)
416 {
417 *p++ = tohex ((registers[i] >> 4) & 0xf);
418 *p++ = tohex (registers[i] & 0xf);
419 }
420 *p = '\0';
421
422 remote_send (buf);
423 }
424
425 \f
426 /* Write memory data directly to the remote machine.
427 This does not inform the data cache; the data cache uses this.
428 MEMADDR is the address in the remote memory space.
429 MYADDR is the address of the buffer in our space.
430 LEN is the number of bytes. */
431
432 static void
433 remote_write_bytes (memaddr, myaddr, len)
434 CORE_ADDR memaddr;
435 char *myaddr;
436 int len;
437 {
438 char buf[PBUFSIZ];
439 int i;
440 char *p;
441
442 if (len > PBUFSIZ / 2 - 20)
443 abort ();
444
445 sprintf (buf, "M%x,%x:", memaddr, len);
446
447 /* We send target system values byte by byte, in increasing byte addresses,
448 each byte encoded as two hex characters. */
449
450 p = buf + strlen (buf);
451 for (i = 0; i < len; i++)
452 {
453 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
454 *p++ = tohex (myaddr[i] & 0xf);
455 }
456 *p = '\0';
457
458 remote_send (buf);
459 }
460
461 /* Read memory data directly from the remote machine.
462 This does not use the data cache; the data cache uses this.
463 MEMADDR is the address in the remote memory space.
464 MYADDR is the address of the buffer in our space.
465 LEN is the number of bytes. */
466
467 static void
468 remote_read_bytes (memaddr, myaddr, len)
469 CORE_ADDR memaddr;
470 char *myaddr;
471 int len;
472 {
473 char buf[PBUFSIZ];
474 int i;
475 char *p;
476
477 if (len > PBUFSIZ / 2 - 1)
478 abort ();
479
480 sprintf (buf, "m%x,%x", memaddr, len);
481 remote_send (buf);
482
483 /* Reply describes memory byte by byte,
484 each byte encoded as two hex characters. */
485
486 p = buf;
487 for (i = 0; i < len; i++)
488 {
489 if (p[0] == 0 || p[1] == 0)
490 error ("Remote reply is too short: %s", buf);
491 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
492 p += 2;
493 }
494 }
495 \f
496 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
497 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
498 nonzero. Returns length of data written or read; 0 for error. */
499
500 /* ARGSUSED */
501 static int
502 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
503 CORE_ADDR memaddr;
504 char *myaddr;
505 int len;
506 int should_write;
507 struct target_ops *target; /* ignored */
508 {
509 int origlen = len;
510 int xfersize;
511 while (len > 0)
512 {
513 if (len > MAXBUFBYTES)
514 xfersize = MAXBUFBYTES;
515 else
516 xfersize = len;
517
518 if (should_write)
519 remote_write_bytes(memaddr, myaddr, xfersize);
520 else
521 remote_read_bytes (memaddr, myaddr, xfersize);
522 memaddr += xfersize;
523 myaddr += xfersize;
524 len -= xfersize;
525 }
526 return origlen; /* no error possible */
527 }
528
529 static void
530 remote_files_info (ignore)
531 struct target_ops *ignore;
532 {
533 printf ("Debugging a target over a serial line.\n");
534 }
535 \f
536 /*
537
538 A debug packet whose contents are <data>
539 is encapsulated for transmission in the form:
540
541 $ <data> # CSUM1 CSUM2
542
543 <data> must be ASCII alphanumeric and cannot include characters
544 '$' or '#'
545
546 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
547 checksum of <data>, the most significant nibble is sent first.
548 the hex digits 0-9,a-f are used.
549
550 Receiver responds with:
551
552 + - if CSUM is correct and ready for next packet
553 - - if CSUM is incorrect
554
555 */
556
557 /* Read a single character from the remote end.
558 (If supported, we actually read many characters and buffer them up.) */
559
560 static int
561 readchar ()
562 {
563 static int inbuf_index, inbuf_count;
564 #define INBUFSIZE PBUFSIZ
565 static char inbuf[INBUFSIZE];
566
567 if (inbuf_index >= inbuf_count)
568 {
569 /* Time to do another read... */
570 inbuf_index = 0;
571 inbuf_count = 0;
572 inbuf[0] = 0; /* Just in case */
573 if ((inbuf[inbuf_index] = serial_readchar (timeout)) < 0)
574 inbuf_count = -1;
575 else
576 inbuf_count = 1;
577 }
578
579 /* Just return the next character from the buffer. */
580 return inbuf[inbuf_index++] & 0x7f;
581 }
582
583
584 \f
585 /* The data cache leads to incorrect results because it doesn't know about
586 volatile variables, thus making it impossible to debug functions which
587 use hardware registers. Therefore it is #if 0'd out. Effect on
588 performance is some, for backtraces of functions with a few
589 arguments each. For functions with many arguments, the stack
590 frames don't fit in the cache blocks, which makes the cache less
591 helpful. Disabling the cache is a big performance win for fetching
592 large structures, because the cache code fetched data in 16-byte
593 chunks. */
594 #if 0
595 /* The data cache records all the data read from the remote machine
596 since the last time it stopped.
597
598 Each cache block holds 16 bytes of data
599 starting at a multiple-of-16 address. */
600
601 #define DCACHE_SIZE 64 /* Number of cache blocks */
602
603 struct dcache_block {
604 struct dcache_block *next, *last;
605 unsigned int addr; /* Address for which data is recorded. */
606 int data[4];
607 };
608
609 struct dcache_block dcache_free, dcache_valid;
610
611 /* Free all the data cache blocks, thus discarding all cached data. */
612
613 static void
614 dcache_flush ()
615 {
616 register struct dcache_block *db;
617
618 while ((db = dcache_valid.next) != &dcache_valid)
619 {
620 remque (db);
621 insque (db, &dcache_free);
622 }
623 }
624
625 /*
626 * If addr is present in the dcache, return the address of the block
627 * containing it.
628 */
629
630 struct dcache_block *
631 dcache_hit (addr)
632 {
633 register struct dcache_block *db;
634
635 if (addr & 3)
636 abort ();
637
638 /* Search all cache blocks for one that is at this address. */
639 db = dcache_valid.next;
640 while (db != &dcache_valid)
641 {
642 if ((addr & 0xfffffff0) == db->addr)
643 return db;
644 db = db->next;
645 }
646 return NULL;
647 }
648
649 /* Return the int data at address ADDR in dcache block DC. */
650
651 int
652 dcache_value (db, addr)
653 struct dcache_block *db;
654 unsigned int addr;
655 {
656 if (addr & 3)
657 abort ();
658 return (db->data[(addr>>2)&3]);
659 }
660
661 /* Get a free cache block, put it on the valid list,
662 and return its address. The caller should store into the block
663 the address and data that it describes. */
664
665 struct dcache_block *
666 dcache_alloc ()
667 {
668 register struct dcache_block *db;
669
670 if ((db = dcache_free.next) == &dcache_free)
671 /* If we can't get one from the free list, take last valid */
672 db = dcache_valid.last;
673
674 remque (db);
675 insque (db, &dcache_valid);
676 return (db);
677 }
678
679 /* Return the contents of the word at address ADDR in the remote machine,
680 using the data cache. */
681
682 int
683 dcache_fetch (addr)
684 CORE_ADDR addr;
685 {
686 register struct dcache_block *db;
687
688 db = dcache_hit (addr);
689 if (db == 0)
690 {
691 db = dcache_alloc ();
692 remote_read_bytes (addr & ~0xf, db->data, 16);
693 db->addr = addr & ~0xf;
694 }
695 return (dcache_value (db, addr));
696 }
697
698 /* Write the word at ADDR both in the data cache and in the remote machine. */
699
700 dcache_poke (addr, data)
701 CORE_ADDR addr;
702 int data;
703 {
704 register struct dcache_block *db;
705
706 /* First make sure the word is IN the cache. DB is its cache block. */
707 db = dcache_hit (addr);
708 if (db == 0)
709 {
710 db = dcache_alloc ();
711 remote_read_bytes (addr & ~0xf, db->data, 16);
712 db->addr = addr & ~0xf;
713 }
714
715 /* Modify the word in the cache. */
716 db->data[(addr>>2)&3] = data;
717
718 /* Send the changed word. */
719 remote_write_bytes (addr, &data, 4);
720 }
721
722 /* Initialize the data cache. */
723
724 dcache_init ()
725 {
726 register i;
727 register struct dcache_block *db;
728
729 db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) *
730 DCACHE_SIZE);
731 dcache_free.next = dcache_free.last = &dcache_free;
732 dcache_valid.next = dcache_valid.last = &dcache_valid;
733 for (i=0;i<DCACHE_SIZE;i++,db++)
734 insque (db, &dcache_free);
735 }
736 #endif /* 0 */
737
738 /* Define the target subroutine names */
739
740 struct target_ops remote_ops = {
741 "remote-ser", /* to_shortname */
742 "Remote serial target in gdb-specific protocol with serial-packaging routines", /* to_longname */
743 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
744 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
745 serial_open, /* to_open */
746 serial_close, /* to_close */
747 NULL, /* to_attach */
748 remote_detach, /* to_detach */
749 remote_resume, /* to_resume */
750 remote_wait, /* to_wait */
751 remote_fetch_registers, /* to_fetch_registers */
752 remote_store_registers, /* to_store_registers */
753 remote_prepare_to_store, /* to_prepare_to_store */
754 NULL, /* to_convert_to_virtual */
755 NULL, /* to_convert_from_virtual */
756 remote_xfer_memory, /* to_xfer_memory */
757 remote_files_info, /* to_files_info */
758 NULL, /* to_insert_breakpoint */
759 NULL, /* to_remove_breakpoint */
760 NULL, /* to_terminal_init */
761 NULL, /* to_terminal_inferior */
762 NULL, /* to_terminal_ours_for_output */
763 NULL, /* to_terminal_ours */
764 NULL, /* to_terminal_info */
765 NULL, /* to_kill */
766 NULL, /* to_load */
767 NULL, /* to_lookup_symbol */
768 NULL, /* to_create_inferior */
769 NULL, /* to_mourn_inferior */
770 process_stratum, /* to_stratum */
771 NULL, /* to_next */
772 1, /* to_has_all_memory */
773 1, /* to_has_memory */
774 1, /* to_has_stack */
775 1, /* to_has_registers */
776 1, /* to_has_execution */
777 NULL, /* sections */
778 NULL, /* sections_end */
779 OPS_MAGIC /* to_magic */
780 };
781
782 void
783 _initialize_remote ()
784 {
785 add_target (&remote_ops);
786
787 add_show_from_set (
788 add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug,
789 "Set debugging of remote serial I/O.\n\
790 When enabled, each packet sent or received with the remote target\n\
791 is displayed.", &setlist),
792 &showlist);
793 }
794
This page took 0.046079 seconds and 4 git commands to generate.