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