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