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