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