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