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