bcopy -> memcpy
[binutils-gdb.git] / gdb / remote-bug.c
1 /* Remote debugging interface for Motorola's MVME187BUG monitor, an embedded
2 monitor for the m88k.
3
4 Copyright 1992, 1993 Free Software Foundation, Inc.
5 Contributed by Cygnus Support. Written by K. Richard Pixley.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "wait.h"
26 #include "value.h"
27
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <setjmp.h>
33 #include <errno.h>
34
35 #include "terminal.h"
36 #include "target.h"
37 #include "gdbcore.h"
38 #include "serial.h"
39 #include "gdbcmd.h"
40
41 #include "dcache.h"
42
43 extern int sleep();
44
45 /* External data declarations */
46 extern int stop_soon_quietly; /* for wait_for_inferior */
47
48 /* Forward data declarations */
49 extern struct target_ops bug_ops; /* Forward declaration */
50
51 /* Forward function declarations */
52
53 static int bug_clear_breakpoints PARAMS((void));
54 static void bug_close PARAMS((int quitting));
55 static void bug_write_cr PARAMS((char *string));
56
57 static int bug_read_inferior_memory PARAMS((CORE_ADDR memaddr,
58 unsigned char *myaddr,
59 int len));
60
61 static int bug_write_inferior_memory PARAMS((CORE_ADDR memaddr,
62 unsigned char *myaddr,
63 int len));
64
65 /* This is the serial descriptor to our target. */
66
67 static serial_t desc = NULL;
68
69 /* This is our data cache. */
70
71 static DCACHE *bug_dcache;
72
73 /* This variable is somewhat arbitrary. It's here so that it can be
74 set from within a running gdb. */
75
76 static int srec_max_retries = 3;
77
78 /* Each S-record download to the target consists of an S0 header
79 record, some number of S3 data records, and one S7 termination
80 record. I call this download a "frame". Srec_frame says how many
81 bytes will be represented in each frame. */
82
83 static int srec_frame = 160;
84
85 /* This variable determines how many bytes will be represented in each
86 S3 s-record. */
87
88 static int srec_bytes = 40;
89
90 /* At one point it appeared to me as though the bug monitor could not
91 really be expected to receive two sequential characters at 9600
92 baud reliably. Echo-pacing is an attempt to force data across the
93 line even in this condition. Specifically, in echo-pace mode, each
94 character is sent one at a time and we look for the echo before
95 sending the next. This is excruciatingly slow. */
96
97 static int srec_echo_pace = 0;
98
99 /* How long to wait after an srec for a possible error message.
100 Similar to the above, I tried sleeping after sending each S3 record
101 in hopes that I might actually see error messages from the bug
102 monitor. This might actually work if we were to use sleep
103 intervals smaller than 1 second. */
104
105 static int srec_sleep = 0;
106
107 /* Every srec_noise records, flub the checksum. This is a debugging
108 feature. Set the variable to something other than 1 in order to
109 inject *deliberate* checksum errors. One might do this if one
110 wanted to test error handling and recovery. */
111
112 static int srec_noise = 0;
113
114 /***********************************************************************
115 * I/O stuff stolen from remote-eb.c
116 ***********************************************************************/
117
118 /* with a timeout of 2, we time out waiting for the prompt after an
119 s-record dump. */
120 static int timeout = 4;
121
122 static const char *dev_name;
123
124 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
125 bug_open knows that we don't have a file open when the program
126 starts. */
127
128 static int is_open = 0;
129 static int
130 check_open ()
131 {
132 if (!is_open)
133 {
134 error ("remote device not open");
135 }
136
137 return(1);
138 }
139
140 #define ON 1
141 #define OFF 0
142
143 /* Read a character from the remote system, doing all the fancy
144 timeout stuff. */
145 static int
146 readchar ()
147 {
148 int buf;
149
150 buf = SERIAL_READCHAR (desc, timeout);
151
152 if (buf == SERIAL_TIMEOUT)
153 error ("Timeout reading from remote system.");
154
155 if (remote_debug)
156 printf ("%c", buf);
157
158 return buf & 0x7f;
159 }
160
161 static int
162 readchar_nofail ()
163 {
164 int buf;
165
166 buf = SERIAL_READCHAR (desc, timeout);
167 if (buf == SERIAL_TIMEOUT)
168 buf = 0;
169 if (remote_debug)
170 if (buf)
171 printf ("%c", buf);
172 else
173 printf ("<timeout>");
174
175 return buf & 0x7f;
176
177 }
178
179 static int
180 pollchar()
181 {
182 int buf;
183
184 buf = SERIAL_READCHAR (desc, 0);
185 if (buf == SERIAL_TIMEOUT)
186 buf = 0;
187 if (remote_debug)
188 if (buf)
189 printf ("%c", buf);
190 else
191 printf ("<empty poll>");
192
193 return buf & 0x7f;
194 }
195
196 /* Keep discarding input from the remote system, until STRING is found.
197 Let the user break out immediately. */
198 static void
199 expect (string)
200 char *string;
201 {
202 char *p = string;
203
204 immediate_quit = 1;
205 for (;;)
206 {
207 if (readchar () == *p)
208 {
209 p++;
210 if (*p == '\0')
211 {
212 immediate_quit = 0;
213 return;
214 }
215 }
216 else
217 p = string;
218 }
219 }
220
221 /* Keep discarding input until we see the bug prompt.
222
223 The convention for dealing with the prompt is that you
224 o give your command
225 o *then* wait for the prompt.
226
227 Thus the last thing that a procedure does with the serial line
228 will be an expect_prompt(). Exception: bug_resume does not
229 wait for the prompt, because the terminal is being handed over
230 to the inferior. However, the next thing which happens after that
231 is a bug_wait which does wait for the prompt.
232 Note that this includes abnormal exit, e.g. error(). This is
233 necessary to prevent getting into states from which we can't
234 recover. */
235 static void
236 expect_prompt ()
237 {
238 expect ("Bug>");
239 }
240
241 /* Get a hex digit from the remote system & return its value.
242 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
243 static int
244 get_hex_digit (ignore_space)
245 int ignore_space;
246 {
247 int ch;
248
249 for (;;)
250 {
251 ch = readchar ();
252 if (ch >= '0' && ch <= '9')
253 return ch - '0';
254 else if (ch >= 'A' && ch <= 'F')
255 return ch - 'A' + 10;
256 else if (ch >= 'a' && ch <= 'f')
257 return ch - 'a' + 10;
258 else if (ch != ' ' || !ignore_space)
259 {
260 expect_prompt ();
261 error ("Invalid hex digit from remote system.");
262 }
263 }
264 }
265
266 /* Get a byte from bug_desc and put it in *BYT. Accept any number
267 leading spaces. */
268 static void
269 get_hex_byte (byt)
270 char *byt;
271 {
272 int val;
273
274 val = get_hex_digit (1) << 4;
275 val |= get_hex_digit (0);
276 *byt = val;
277 }
278
279 /* Read a 32-bit hex word from the bug, preceded by a space */
280 static long
281 get_hex_word ()
282 {
283 long val;
284 int j;
285
286 val = 0;
287 for (j = 0; j < 8; j++)
288 val = (val << 4) + get_hex_digit (j == 0);
289 return val;
290 }
291
292 /* Called when SIGALRM signal sent due to alarm() timeout. */
293
294 /* Number of SIGTRAPs we need to simulate. That is, the next
295 NEED_ARTIFICIAL_TRAP calls to bug_wait should just return
296 SIGTRAP without actually waiting for anything. */
297
298 static int need_artificial_trap = 0;
299
300 void
301 bug_kill (arg, from_tty)
302 char *arg;
303 int from_tty;
304 {
305
306 }
307
308 /*
309 * Download a file specified in 'args', to the bug.
310 */
311
312 static void
313 bug_load (args, fromtty)
314 char *args;
315 int fromtty;
316 {
317 bfd *abfd;
318 asection *s;
319 char buffer[1024];
320
321 check_open ();
322
323 dcache_flush (bug_dcache);
324 inferior_pid = 0;
325 abfd = bfd_openr (args, 0);
326 if (!abfd)
327 {
328 printf_filtered ("Unable to open file %s\n", args);
329 return;
330 }
331
332 if (bfd_check_format (abfd, bfd_object) == 0)
333 {
334 printf_filtered ("File is not an object file\n");
335 return;
336 }
337
338 s = abfd->sections;
339 while (s != (asection *) NULL)
340 {
341 if (s->flags & SEC_LOAD)
342 {
343 int i;
344
345 char *buffer = xmalloc (srec_frame);
346
347 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
348 fflush (stdout);
349 for (i = 0; i < s->_raw_size; i += srec_frame)
350 {
351 if (srec_frame > s->_raw_size - i)
352 srec_frame = s->_raw_size - i;
353
354 bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
355 bug_write_inferior_memory (s->vma + i, buffer, srec_frame);
356 printf_filtered ("*");
357 fflush (stdout);
358 }
359 printf_filtered ("\n");
360 free (buffer);
361 }
362 s = s->next;
363 }
364 sprintf (buffer, "rs ip %lx", (unsigned long) abfd->start_address);
365 bug_write_cr (buffer);
366 expect_prompt ();
367 }
368
369 /* This is called not only when we first attach, but also when the
370 user types "run" after having attached. */
371 void
372 bug_create_inferior (execfile, args, env)
373 char *execfile;
374 char *args;
375 char **env;
376 {
377 int entry_pt;
378
379 if (args && *args)
380 error ("Can't pass arguments to remote bug process.");
381
382 if (execfile == 0 || exec_bfd == 0)
383 error ("No exec file specified");
384
385 entry_pt = (int) bfd_get_start_address (exec_bfd);
386 check_open ();
387
388 bug_kill (NULL, NULL);
389 bug_clear_breakpoints ();
390 init_wait_for_inferior ();
391 bug_write_cr ("");
392 expect_prompt ();
393
394 insert_breakpoints (); /* Needed to get correct instruction in cache */
395 proceed (entry_pt, -1, 0);
396 }
397
398 static char *
399 get_word (p)
400 char **p;
401 {
402 char *s = *p;
403 char *word;
404 char *copy;
405 size_t len;
406
407 while (isspace (*s))
408 s++;
409
410 word = s;
411
412 len = 0;
413
414 while (*s && !isspace (*s))
415 {
416 s++;
417 len++;
418
419 }
420 copy = xmalloc (len + 1);
421 memcpy (copy, word, len);
422 copy[len] = 0;
423 *p = s;
424 return copy;
425 }
426
427 static int baudrate = 9600;
428
429 static void
430 bug_open (name, from_tty)
431 char *name;
432 int from_tty;
433 {
434 push_target (&bug_ops);
435
436 if (name == 0)
437 {
438 name = "";
439 }
440 if (is_open)
441 bug_close (0);
442 dev_name = strdup (name);
443
444 if (!(desc = SERIAL_OPEN (dev_name)))
445 perror_with_name ((char *) dev_name);
446
447 SERIAL_RAW (desc);
448 is_open = 1;
449
450 bug_dcache = dcache_init (bug_read_inferior_memory, bug_write_inferior_memory);
451
452 /* Hello? Are you there? */
453 SERIAL_WRITE (desc, "\r", 1);
454 expect_prompt ();
455
456 /* Clear any break points */
457 bug_clear_breakpoints ();
458
459 printf_filtered ("Connected to remote 187bug system.\n");
460 }
461
462 /* Close out all files and local state before this target loses control. */
463
464 static void
465 bug_close (quitting)
466 int quitting;
467 {
468 /* Clear any break points */
469 bug_clear_breakpoints ();
470
471 if (is_open)
472 SERIAL_CLOSE (desc);
473
474 is_open = 0;
475 }
476
477 /* Terminate the open connection to the remote debugger.
478 Use this when you want to detach and do something else
479 with your gdb. */
480 void
481 bug_detach (args, from_tty)
482 char *args;
483 int from_tty;
484 {
485 if (is_open)
486 bug_clear_breakpoints ();
487
488 pop_target (); /* calls bug_close to do the real work */
489
490 if (from_tty)
491 printf_filtered ("Ending remote %s debugging\n", target_shortname);
492 }
493
494 /* Tell the remote machine to resume. */
495
496 void
497 bug_resume (pid, step, sig)
498 int pid, step, sig;
499 {
500 dcache_flush (bug_dcache);
501
502 if (step)
503 {
504 bug_write_cr("t");
505
506 /* Force the next bug_wait to return a trap. Not doing anything
507 about I/O from the target means that the user has to type
508 "continue" to see any. FIXME, this should be fixed. */
509 need_artificial_trap = 1;
510 }
511 else
512 bug_write_cr ("g");
513
514 return;
515 }
516
517 /* Given a null terminated list of strings LIST, read the input until we find one of
518 them. Return the index of the string found or -1 on error. '?' means match
519 any single character. Note that with the algorithm we use, the initial
520 character of the string cannot recur in the string, or we will not find some
521 cases of the string in the input. If PASSTHROUGH is non-zero, then
522 pass non-matching data on. */
523
524 static int
525 multi_scan (list, passthrough)
526 char *list[];
527 int passthrough;
528 {
529 char *swallowed = NULL; /* holding area */
530 char *swallowed_p = swallowed; /* Current position in swallowed. */
531 int ch;
532 int ch_handled;
533 int i;
534 int string_count;
535 int max_length;
536 char **plist;
537
538 /* Look through the strings. Count them. Find the largest one so we can
539 allocate a holding area. */
540
541 for (max_length = string_count = i = 0;
542 list[i] != NULL;
543 ++i, ++string_count)
544 {
545 int length = strlen(list[i]);
546
547 if (length > max_length)
548 max_length = length;
549 }
550
551 /* if we have no strings, then something is wrong. */
552 if (string_count == 0)
553 return(-1);
554
555 /* otherwise, we will need a holding area big enough to hold almost two
556 copies of our largest string. */
557 swallowed_p = swallowed = alloca(max_length << 1);
558
559 /* and a list of pointers to current scan points. */
560 plist = alloca(string_count * sizeof(*plist));
561
562 /* and initialize */
563 for (i = 0; i < string_count; ++i)
564 plist[i] = list[i];
565
566 for (ch = readchar(); /* loop forever */ ; ch = readchar())
567 {
568 QUIT; /* Let user quit and leave process running */
569 ch_handled = 0;
570
571 for (i = 0; i < string_count; ++i)
572 {
573 if (ch == *plist[i] || *plist[i] == '?')
574 {
575 ++plist[i];
576 if (*plist[i] == '\0')
577 return(i);
578
579 if (!ch_handled)
580 *swallowed_p++ = ch;
581
582 ch_handled = 1;
583 }
584 else
585 plist[i] = list[i];
586 }
587
588 if (!ch_handled)
589 {
590 char *p;
591
592 /* Print out any characters which have been swallowed. */
593 if (passthrough)
594 {
595 for (p = swallowed; p < swallowed_p; ++p)
596 putc (*p, stdout);
597
598 putc (ch, stdout);
599 }
600
601 swallowed_p = swallowed;
602 }
603 }
604
605 return(-1);
606 }
607
608 /* Wait until the remote machine stops, then return,
609 storing status in STATUS just as `wait' would. */
610
611 static char *wait_strings[] = {
612 "At Breakpoint",
613 "Exception: Data Access Fault (Local Bus Timeout)",
614 "\r8???-Bug>",
615 NULL,
616 };
617
618 int
619 bug_wait (status)
620 WAITTYPE *status;
621 {
622 int old_timeout = timeout;
623 int old_immediate_quit = immediate_quit;
624
625 WSETEXIT ((*status), 0);
626
627 /* read off leftovers from resume so that the rest can be passed
628 back out as stdout. */
629 if (need_artificial_trap == 0)
630 {
631 expect("Effective address: ");
632 (void) get_hex_word();
633 expect ("\r\n");
634 }
635
636 timeout = -1; /* Don't time out -- user program is running. */
637 immediate_quit = 1; /* Helps ability to QUIT */
638
639 switch (multi_scan(wait_strings, need_artificial_trap == 0))
640 {
641 case 0: /* breakpoint case */
642 WSETSTOP ((*status), SIGTRAP);
643 /* user output from the target can be discarded here. (?) */
644 expect_prompt();
645 break;
646
647 case 1: /* bus error */
648 WSETSTOP ((*status), SIGBUS);
649 /* user output from the target can be discarded here. (?) */
650 expect_prompt();
651 break;
652
653 case 2: /* normal case */
654 if (need_artificial_trap != 0)
655 {
656 /* stepping */
657 WSETSTOP ((*status), SIGTRAP);
658 need_artificial_trap--;
659 break;
660 }
661 else
662 {
663 /* exit case */
664 WSETEXIT ((*status), 0);
665 break;
666 }
667
668 case -1: /* trouble */
669 default:
670 fprintf_filtered (stderr,
671 "Trouble reading target during wait\n");
672 break;
673 }
674
675 timeout = old_timeout;
676 immediate_quit = old_immediate_quit;
677 return 0;
678 }
679
680 /* Return the name of register number REGNO
681 in the form input and output by bug.
682
683 Returns a pointer to a static buffer containing the answer. */
684 static char *
685 get_reg_name (regno)
686 int regno;
687 {
688 static char *rn[] = {
689 "r00", "r01", "r02", "r03", "r04", "r05", "r06", "r07",
690 "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15",
691 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
692 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
693
694 /* these get confusing because we omit a few and switch some ordering around. */
695
696 "cr01", /* 32 = psr */
697 "fcr62", /* 33 = fpsr*/
698 "fcr63", /* 34 = fpcr */
699 "ip", /* this is something of a cheat. */
700 /* 35 = sxip */
701 "cr05", /* 36 = snip */
702 "cr06", /* 37 = sfip */
703 };
704
705 return rn[regno];
706 }
707
708 static int
709 bug_write (a, l)
710 char *a;
711 int l;
712 {
713 int i;
714
715 SERIAL_WRITE (desc, a, l);
716
717 if (remote_debug)
718 for (i = 0; i < l; i++)
719 {
720 printf ("%c", a[i]);
721 }
722
723 return(0);
724 }
725
726 static void
727 bug_write_cr (s)
728 char *s;
729 {
730 bug_write (s, strlen (s));
731 bug_write ("\r", 1);
732 return;
733 }
734
735 #if 0 /* not currently used */
736 /* Read from remote while the input matches STRING. Return zero on
737 success, -1 on failure. */
738
739 static int
740 bug_scan (s)
741 char *s;
742 {
743 int c;
744
745 while (*s)
746 {
747 c = readchar();
748 if (c != *s++)
749 {
750 fflush(stdout);
751 printf("\nNext character is '%c' - %d and s is \"%s\".\n", c, c, --s);
752 return(-1);
753 }
754 }
755
756 return(0);
757 }
758 #endif /* never */
759
760 static int
761 bug_srec_write_cr (s)
762 char *s;
763 {
764 char *p = s;
765
766 if (srec_echo_pace)
767 for (p = s; *p; ++p)
768 {
769 if (remote_debug)
770 printf ("%c", *p);
771
772 do
773 SERIAL_WRITE(desc, p, 1);
774 while (readchar_nofail() != *p);
775 }
776 else
777 {
778 bug_write_cr (s);
779 /* return(bug_scan (s) || bug_scan ("\n")); */
780 }
781
782 return(0);
783 }
784
785 /* Store register REGNO, or all if REGNO == -1. */
786
787 static void
788 bug_fetch_register(regno)
789 int regno;
790 {
791 REGISTER_TYPE regval;
792 check_open();
793
794 if (regno == -1)
795 {
796 int i;
797
798 for (i = 0; i < NUM_REGS; ++i)
799 bug_fetch_register(i);
800 }
801 else
802 {
803 bug_write("rs ", 3);
804 bug_write_cr(get_reg_name(regno));
805 expect("=");
806 regval = get_hex_word();
807 expect_prompt();
808
809 /* the following registers contain flag bits in the lower to bit slots.
810 Mask them off */
811 if (regno == PC_REGNUM /* aka sxip */
812 || regno == NPC_REGNUM /* aka snip */
813 || regno == SFIP_REGNUM) /* aka sfip */
814 regval &= ~0x3;
815
816 supply_register(regno, (char *) &regval);
817 }
818
819 return;
820 }
821
822 /* Store register REGNO, or all if REGNO == -1. */
823
824 static void
825 bug_store_register (regno)
826 int regno;
827 {
828 char buffer[1024];
829 check_open();
830
831 if (regno == -1)
832 {
833 int i;
834
835 for (i = 0; i < NUM_REGS; ++i)
836 bug_store_register(i);
837 }
838 else
839 {
840 char *regname;
841
842 regname = (get_reg_name(regno));
843
844 sprintf(buffer, "rs %s %08x",
845 regname,
846 read_register(regno));
847
848 bug_write_cr(buffer);
849 expect_prompt();
850 }
851
852 return;
853 }
854
855 /* Get ready to modify the registers array. On machines which store
856 individual registers, this doesn't need to do anything. On machines
857 which store all the registers in one fell swoop, this makes sure
858 that registers contains all the registers from the program being
859 debugged. */
860
861 void
862 bug_prepare_to_store ()
863 {
864 /* Do nothing, since we can store individual regs */
865 }
866
867 /* Read a word from remote address ADDR and return it.
868 * This goes through the data cache.
869 */
870 int
871 bug_fetch_word (addr)
872 CORE_ADDR addr;
873 {
874 return dcache_fetch (bug_dcache, addr);
875 }
876
877 /* Write a word WORD into remote address ADDR.
878 This goes through the data cache. */
879
880 void
881 bug_store_word (addr, word)
882 CORE_ADDR addr;
883 int word;
884 {
885 dcache_poke (bug_dcache, addr, word);
886 }
887
888 int
889 bug_xfer_inferior_memory (memaddr, myaddr, len, write, target)
890 CORE_ADDR memaddr;
891 char *myaddr;
892 int len;
893 int write;
894 struct target_ops *target; /* ignored */
895 {
896 register int i;
897
898 /* Round starting address down to longword boundary. */
899 register CORE_ADDR addr;
900
901 /* Round ending address up; get number of longwords that makes. */
902 register int count;
903
904 /* Allocate buffer of that many longwords. */
905 register int *buffer;
906
907 addr = memaddr & -sizeof (int);
908 count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
909
910 buffer = (int *) alloca (count * sizeof (int));
911
912 if (write)
913 {
914 /* Fill start and end extra bytes of buffer with existing memory data. */
915
916 if (addr != memaddr || len < (int) sizeof (int))
917 {
918 /* Need part of initial word -- fetch it. */
919 buffer[0] = bug_fetch_word (addr);
920 }
921
922 if (count > 1) /* FIXME, avoid if even boundary */
923 {
924 buffer[count - 1]
925 = bug_fetch_word (addr + (count - 1) * sizeof (int));
926 }
927
928 /* Copy data to be written over corresponding part of buffer */
929
930 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
931
932 /* Write the entire buffer. */
933
934 for (i = 0; i < count; i++, addr += sizeof (int))
935 {
936 errno = 0;
937 bug_store_word (addr, buffer[i]);
938 if (errno)
939 {
940
941 return 0;
942 }
943
944 }
945 }
946 else
947 {
948 /* Read all the longwords */
949 for (i = 0; i < count; i++, addr += sizeof (int))
950 {
951 errno = 0;
952 buffer[i] = bug_fetch_word (addr);
953 if (errno)
954 {
955 return 0;
956 }
957 QUIT;
958 }
959
960 /* Copy appropriate bytes out of the buffer. */
961 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
962 }
963
964 return len;
965 }
966
967 static void
968 start_load()
969 {
970 char *command;
971
972 command = (srec_echo_pace ? "lo 0 ;x" : "lo 0");
973
974 bug_write_cr (command);
975 expect (command);
976 expect ("\r\n");
977 bug_srec_write_cr ("S0030000FC");
978 return;
979 }
980
981 /* This is an extremely vulnerable and fragile function. I've made
982 considerable attempts to make this deterministic, but I've
983 certainly forgotten something. The trouble is that S-records are
984 only a partial file format, not a protocol. Worse, apparently the
985 m88k bug monitor does not run in real time while receiving
986 S-records. Hence, we must pay excruciating attention to when and
987 where error messages are returned, and what has actually been sent.
988
989 Each call represents a chunk of memory to be sent to the target.
990 We break that chunk into an S0 header record, some number of S3
991 data records each containing srec_bytes, and an S7 termination
992 record. */
993
994 static char *srecord_strings[] = {
995 "S-RECORD",
996 "8???-Bug>",
997 NULL,
998 };
999
1000 static int
1001 bug_write_inferior_memory (memaddr, myaddr, len)
1002 CORE_ADDR memaddr;
1003 unsigned char *myaddr;
1004 int len;
1005 {
1006 int done;
1007 int checksum;
1008 int x;
1009 int retries;
1010 char buffer[(srec_bytes + 8) << 1];
1011
1012 retries = 0;
1013
1014 do
1015 {
1016 done = 0;
1017
1018 if (retries > srec_max_retries)
1019 return(-1);
1020
1021 if (retries > 0)
1022 {
1023 if (remote_debug)
1024 printf("\n<retrying...>\n");
1025
1026 /* This expect_prompt call is extremely important. Without
1027 it, we will tend to resend our packet so fast that it
1028 will arrive before the bug monitor is ready to receive
1029 it. This would lead to a very ugly resend loop. */
1030
1031 expect_prompt();
1032 }
1033
1034 start_load();
1035
1036 while (done < len)
1037 {
1038 int thisgo;
1039 int idx;
1040 char *buf = buffer;
1041 CORE_ADDR address;
1042
1043 checksum = 0;
1044 thisgo = len - done;
1045 if (thisgo > srec_bytes)
1046 thisgo = srec_bytes;
1047
1048 address = memaddr + done;
1049 sprintf (buf, "S3%02X%08X", thisgo + 4 + 1, address);
1050 buf += 12;
1051
1052 checksum += (thisgo + 4 + 1
1053 + (address & 0xff)
1054 + ((address >> 8) & 0xff)
1055 + ((address >> 16) & 0xff)
1056 + ((address >> 24) & 0xff));
1057
1058 for (idx = 0; idx < thisgo; idx++)
1059 {
1060 sprintf (buf, "%02X", myaddr[idx + done]);
1061 checksum += myaddr[idx + done];
1062 buf += 2;
1063 }
1064
1065 if (srec_noise > 0)
1066 {
1067 /* FIXME-NOW: insert a deliberate error every now and then.
1068 This is intended for testing/debugging the error handling
1069 stuff. */
1070 static int counter = 0;
1071 if (++counter > srec_noise)
1072 {
1073 counter = 0;
1074 ++checksum;
1075 }
1076 }
1077
1078 sprintf(buf, "%02X", ~checksum & 0xff);
1079 bug_srec_write_cr (buffer);
1080
1081 if (srec_sleep != 0)
1082 sleep(srec_sleep);
1083
1084 /* This pollchar is probably redundant to the multi_scan
1085 below. Trouble is, we can't be sure when or where an
1086 error message will appear. Apparently, when running at
1087 full speed from a typical sun4, error messages tend to
1088 appear to arrive only *after* the s7 record. */
1089
1090 if ((x = pollchar()) != 0)
1091 {
1092 if (remote_debug)
1093 printf("\n<retrying...>\n");
1094
1095 ++retries;
1096
1097 /* flush any remaining input and verify that we are back
1098 at the prompt level. */
1099 expect_prompt();
1100 /* start all over again. */
1101 start_load();
1102 done = 0;
1103 continue;
1104 }
1105
1106 done += thisgo;
1107 }
1108
1109 bug_srec_write_cr("S7060000000000F9");
1110 ++retries;
1111
1112 /* Having finished the load, we need to figure out whether we
1113 had any errors. */
1114 } while (multi_scan(srecord_strings, 0) == 0);;
1115
1116 return(0);
1117 }
1118
1119 void
1120 bug_files_info ()
1121 {
1122 char *file = "nothing";
1123
1124 if (exec_bfd)
1125 file = bfd_get_filename (exec_bfd);
1126
1127 if (exec_bfd)
1128 #ifdef __GO32__
1129 printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
1130 #else
1131 printf_filtered ("\tAttached to %s at %d baud and running program %s\n", dev_name, baudrate, file);
1132 #endif
1133 printf_filtered ("\ton an m88k processor.\n");
1134 }
1135
1136 /* Copy LEN bytes of data from debugger memory at MYADDR
1137 to inferior's memory at MEMADDR. Returns errno value.
1138 * sb/sh instructions don't work on unaligned addresses, when TU=1.
1139 */
1140
1141 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
1142 at debugger address MYADDR. Returns errno value. */
1143 static int
1144 bug_read_inferior_memory (memaddr, myaddr, len)
1145 CORE_ADDR memaddr;
1146 unsigned char *myaddr;
1147 int len;
1148 {
1149 char request[100];
1150 char *buffer;
1151 char *p;
1152 char type;
1153 char size;
1154 unsigned char c;
1155 unsigned int inaddr;
1156 unsigned int checksum;
1157
1158 sprintf(request, "du 0 %x:&%d", memaddr, len);
1159 bug_write_cr(request);
1160
1161 p = buffer = alloca(len);
1162
1163 /* scan up through the header */
1164 expect("S0030000FC");
1165
1166 while (p < buffer + len)
1167 {
1168 /* scan off any white space. */
1169 while (readchar() != 'S') ;;
1170
1171 /* what kind of s-rec? */
1172 type = readchar();
1173
1174 /* scan record size */
1175 get_hex_byte(&size);
1176 checksum = size;
1177 --size;
1178 inaddr = 0;
1179
1180 switch (type)
1181 {
1182 case '7':
1183 case '8':
1184 case '9':
1185 goto done;
1186
1187 case '3':
1188 get_hex_byte(&c);
1189 inaddr = (inaddr << 8) + c;
1190 checksum += c;
1191 --size;
1192 /* intentional fall through */
1193 case '2':
1194 get_hex_byte(&c);
1195 inaddr = (inaddr << 8) + c;
1196 checksum += c;
1197 --size;
1198 /* intentional fall through */
1199 case '1':
1200 get_hex_byte(&c);
1201 inaddr = (inaddr << 8) + c;
1202 checksum += c;
1203 --size;
1204 get_hex_byte(&c);
1205 inaddr = (inaddr << 8) + c;
1206 checksum += c;
1207 --size;
1208 break;
1209
1210 default:
1211 /* bonk */
1212 error("reading s-records.");
1213 }
1214
1215 if (inaddr < memaddr
1216 || (memaddr + len) < (inaddr + size))
1217 error("srec out of memory range.");
1218
1219 if (p != buffer + inaddr - memaddr)
1220 error("srec out of sequence.");
1221
1222 for (; size; --size, ++p)
1223 {
1224 get_hex_byte(p);
1225 checksum += *p;
1226 }
1227
1228 get_hex_byte(&c);
1229 if (c != (~checksum & 0xff))
1230 error("bad s-rec checksum");
1231 }
1232
1233 done:
1234 expect_prompt();
1235 if (p != buffer + len)
1236 return(1);
1237
1238 memcpy(myaddr, buffer, len);
1239 return(0);
1240 }
1241
1242 #define MAX_BREAKS 16
1243 static int num_brkpts = 0;
1244 static int
1245 bug_insert_breakpoint (addr, save)
1246 CORE_ADDR addr;
1247 char *save; /* Throw away, let bug save instructions */
1248 {
1249 check_open ();
1250
1251 if (num_brkpts < MAX_BREAKS)
1252 {
1253 char buffer[100];
1254
1255 num_brkpts++;
1256 sprintf (buffer, "br %x", addr);
1257 bug_write_cr (buffer);
1258 expect_prompt ();
1259 return(0);
1260 }
1261 else
1262 {
1263 fprintf_filtered (stderr,
1264 "Too many break points, break point not installed\n");
1265 return(1);
1266 }
1267
1268 }
1269 static int
1270 bug_remove_breakpoint (addr, save)
1271 CORE_ADDR addr;
1272 char *save; /* Throw away, let bug save instructions */
1273 {
1274 if (num_brkpts > 0)
1275 {
1276 char buffer[100];
1277
1278 num_brkpts--;
1279 sprintf (buffer, "nobr %x", addr);
1280 bug_write_cr (buffer);
1281 expect_prompt ();
1282
1283 }
1284 return (0);
1285 }
1286
1287 /* Clear the bugs notion of what the break points are */
1288 static int
1289 bug_clear_breakpoints ()
1290 {
1291
1292 if (is_open)
1293 {
1294 bug_write_cr ("nobr");
1295 expect("nobr");
1296 expect_prompt ();
1297 }
1298 num_brkpts = 0;
1299 return(0);
1300 }
1301
1302 static void
1303 bug_mourn ()
1304 {
1305 bug_clear_breakpoints ();
1306 generic_mourn_inferior ();
1307 }
1308
1309 /* Put a command string, in args, out to the bug. The bug is assumed to
1310 be in raw mode, all writing/reading done through desc.
1311 Ouput from the bug is placed on the users terminal until the
1312 prompt from the bug is seen.
1313 FIXME: Can't handle commands that take input. */
1314
1315 void
1316 bug_com (args, fromtty)
1317 char *args;
1318 int fromtty;
1319 {
1320 check_open ();
1321
1322 if (!args)
1323 return;
1324
1325 /* Clear all input so only command relative output is displayed */
1326
1327 bug_write_cr (args);
1328 bug_write ("\030", 1);
1329 expect_prompt ();
1330 }
1331
1332 static void
1333 bug_device (args, fromtty)
1334 char *args;
1335 int fromtty;
1336 {
1337 if (args)
1338 dev_name = get_word (&args);
1339
1340 return;
1341 }
1342
1343 #if 0
1344 static
1345 bug_speed (s)
1346 char *s;
1347 {
1348 check_open ();
1349
1350 if (s)
1351 {
1352 char buffer[100];
1353 int newrate = atoi (s);
1354 int which = 0;
1355
1356 if (SERIAL_SETBAUDRATE (desc, newrate))
1357 error ("Can't use %d baud\n", newrate);
1358
1359 printf_filtered ("Checking target is in sync\n");
1360
1361 printf_filtered ("Sending commands to set target to %d\n",
1362 baudrate);
1363
1364 sprintf (buffer, "tm %d. N 8 1", baudrate);
1365 bug_write_cr (buffer);
1366 }
1367 }
1368 #endif /* 0 */
1369
1370 struct target_ops bug_ops =
1371 {
1372 "bug", "Remote BUG monitor",
1373 "Use the mvme187 board running the BUG monitor connected\n\
1374 by a serial line.",
1375
1376 bug_open, bug_close,
1377 0, bug_detach, bug_resume, bug_wait, /* attach */
1378 bug_fetch_register, bug_store_register,
1379 bug_prepare_to_store,
1380 bug_xfer_inferior_memory,
1381 bug_files_info,
1382 bug_insert_breakpoint, bug_remove_breakpoint, /* Breakpoints */
1383 0, 0, 0, 0, 0, /* Terminal handling */
1384 bug_kill, /* FIXME, kill */
1385 bug_load,
1386 0, /* lookup_symbol */
1387 bug_create_inferior, /* create_inferior */
1388 bug_mourn, /* mourn_inferior FIXME */
1389 0, /* can_run */
1390 0, /* notice_signals */
1391 process_stratum, 0, /* next */
1392 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1393 0, 0, /* Section pointers */
1394 OPS_MAGIC, /* Always the last thing */
1395 };
1396
1397 void
1398 _initialize_remote_bug ()
1399 {
1400 add_target (&bug_ops);
1401
1402 add_com ("bug <command>", class_obscure, bug_com,
1403 "Send a command to the BUG monitor.");
1404
1405 add_com ("device", class_obscure, bug_device,
1406 "Set the terminal line for BUG communications");
1407
1408 #if 0
1409 add_com ("speed", class_obscure, bug_speed,
1410 "Set the terminal line speed for BUG communications");
1411 #endif /* 0 */
1412
1413 add_show_from_set
1414 (add_set_cmd ("srec-bytes", class_support, var_uinteger,
1415 (char *) &srec_bytes,
1416 "\
1417 Set the number of bytes represented in each S-record.\n\
1418 This affects the communication protocol with the remote target.",
1419 &setlist),
1420 &showlist);
1421
1422 add_show_from_set
1423 (add_set_cmd ("srec-max-retries", class_support, var_uinteger,
1424 (char *) &srec_max_retries,
1425 "\
1426 Set the number of retries for shipping S-records.\n\
1427 This affects the communication protocol with the remote target.",
1428 &setlist),
1429 &showlist);
1430
1431 add_show_from_set
1432 (add_set_cmd ("srec-frame", class_support, var_uinteger,
1433 (char *) &srec_frame,
1434 "\
1435 Set the number of bytes in an S-record frame.\n\
1436 This affects the communication protocol with the remote target.",
1437 &setlist),
1438 &showlist);
1439
1440 add_show_from_set
1441 (add_set_cmd ("srec-noise", class_support, var_zinteger,
1442 (char *) &srec_noise,
1443 "\
1444 Set number of S-record to send before deliberately flubbing a checksum.\n\
1445 Zero means flub none at all. This affects the communication protocol\n\
1446 with the remote target.",
1447 &setlist),
1448 &showlist);
1449
1450 add_show_from_set
1451 (add_set_cmd ("srec-sleep", class_support, var_zinteger,
1452 (char *) &srec_sleep,
1453 "\
1454 Set number of seconds to sleep after an S-record for a possible error message to arrive.\n\
1455 This affects the communication protocol with the remote target.",
1456 &setlist),
1457 &showlist);
1458
1459 add_show_from_set
1460 (add_set_cmd ("srec-echo-pace", class_support, var_boolean,
1461 (char *) &srec_echo_pace,
1462 "\
1463 Set echo-verification.\n\
1464 When on, use verification by echo when downloading S-records. This is\n\
1465 much slower, but generally more reliable.",
1466 &setlist),
1467 &showlist);
1468
1469 dev_name = NULL;
1470 }