hp merge changes -- too numerous to mention here; see ChangeLog and
[external/binutils.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "wait.h"
26
27 #include "gdb_string.h"
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <setjmp.h>
32 #include <errno.h>
33
34 #include "terminal.h"
35 #include "gdbcore.h"
36 #include "gdbcmd.h"
37
38 #include "remote-utils.h"
39
40
41 extern int sleep();
42
43 /* External data declarations */
44 extern int stop_soon_quietly;   /* for wait_for_inferior */
45
46 /* Forward data declarations */
47 extern struct target_ops bug_ops;       /* Forward declaration */
48
49 /* Forward function declarations */
50 static int bug_clear_breakpoints PARAMS((void));
51
52 static int bug_read_memory PARAMS((CORE_ADDR memaddr,
53                                             unsigned char *myaddr,
54                                             int len));
55
56 static int bug_write_memory PARAMS((CORE_ADDR memaddr,
57                                              unsigned char *myaddr,
58                                              int len));
59
60 /* This variable is somewhat arbitrary.  It's here so that it can be
61    set from within a running gdb.  */
62
63 static int srec_max_retries = 3;
64
65 /* Each S-record download to the target consists of an S0 header
66    record, some number of S3 data records, and one S7 termination
67    record.  I call this download a "frame".  Srec_frame says how many
68    bytes will be represented in each frame.  */
69
70 #define SREC_SIZE 160
71 static int srec_frame = SREC_SIZE;
72
73 /* This variable determines how many bytes will be represented in each
74    S3 s-record.  */
75
76 static int srec_bytes = 40;
77
78 /* At one point it appeared to me as though the bug monitor could not
79    really be expected to receive two sequential characters at 9600
80    baud reliably.  Echo-pacing is an attempt to force data across the
81    line even in this condition.  Specifically, in echo-pace mode, each
82    character is sent one at a time and we look for the echo before
83    sending the next.  This is excruciatingly slow.  */
84
85 static int srec_echo_pace = 0;
86
87 /* How long to wait after an srec for a possible error message.
88    Similar to the above, I tried sleeping after sending each S3 record
89    in hopes that I might actually see error messages from the bug
90    monitor.  This might actually work if we were to use sleep
91    intervals smaller than 1 second.  */
92
93 static int srec_sleep = 0;
94
95 /* Every srec_noise records, flub the checksum.  This is a debugging
96    feature.  Set the variable to something other than 1 in order to
97    inject *deliberate* checksum errors.  One might do this if one
98    wanted to test error handling and recovery.  */
99
100 static int srec_noise = 0;
101
102 /* Called when SIGALRM signal sent due to alarm() timeout.  */
103
104 /* Number of SIGTRAPs we need to simulate.  That is, the next
105    NEED_ARTIFICIAL_TRAP calls to bug_wait should just return
106    SIGTRAP without actually waiting for anything.  */
107
108 static int need_artificial_trap = 0;
109
110 /*
111  * Download a file specified in 'args', to the bug.
112  */
113
114 static void
115 bug_load (args, fromtty)
116      char *args;
117      int fromtty;
118 {
119   bfd *abfd;
120   asection *s;
121   char buffer[1024];
122
123   sr_check_open ();
124
125   dcache_flush (gr_get_dcache());
126   inferior_pid = 0;
127   abfd = bfd_openr (args, 0);
128   if (!abfd)
129     {
130       printf_filtered ("Unable to open file %s\n", args);
131       return;
132     }
133
134   if (bfd_check_format (abfd, bfd_object) == 0)
135     {
136       printf_filtered ("File is not an object file\n");
137       return;
138     }
139
140   s = abfd->sections;
141   while (s != (asection *) NULL)
142     {
143       srec_frame = SREC_SIZE;
144       if (s->flags & SEC_LOAD)
145         {
146           int i;
147
148           char *buffer = xmalloc (srec_frame);
149
150           printf_filtered ("%s\t: 0x%4x .. 0x%4x  ", s->name, s->vma, s->vma + s->_raw_size);
151           gdb_flush (gdb_stdout);
152           for (i = 0; i < s->_raw_size; i += srec_frame)
153             {
154               if (srec_frame > s->_raw_size - i)
155                 srec_frame = s->_raw_size - i;
156
157               bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
158               bug_write_memory (s->vma + i, buffer, srec_frame);
159               printf_filtered ("*");
160               gdb_flush (gdb_stdout);
161             }
162           printf_filtered ("\n");
163           free (buffer);
164         }
165       s = s->next;
166     }
167   sprintf (buffer, "rs ip %lx", (unsigned long) abfd->start_address);
168   sr_write_cr (buffer);
169   gr_expect_prompt ();
170 }
171
172 #if 0
173 static char *
174 get_word (p)
175      char **p;
176 {
177   char *s = *p;
178   char *word;
179   char *copy;
180   size_t len;
181
182   while (isspace (*s))
183     s++;
184
185   word = s;
186
187   len = 0;
188
189   while (*s && !isspace (*s))
190     {
191       s++;
192       len++;
193
194     }
195   copy = xmalloc (len + 1);
196   memcpy (copy, word, len);
197   copy[len] = 0;
198   *p = s;
199   return copy;
200 }
201 #endif
202
203 static struct gr_settings bug_settings = {
204   NULL, /* dcache */
205   "Bug>", /* prompt */
206   &bug_ops, /* ops */
207   bug_clear_breakpoints, /* clear_all_breakpoints */
208   bug_read_memory, /* readfunc */
209   bug_write_memory, /* writefunc */
210   gr_generic_checkin, /* checkin */
211 };
212
213 static char *cpu_check_strings[] = {
214   "=",
215   "Invalid Register",
216 };
217
218 static void
219 bug_open (args, from_tty)
220      char *args;
221      int from_tty;
222 {
223   if (args == NULL)
224       args = "";
225
226   gr_open(args, from_tty, &bug_settings);
227   /* decide *now* whether we are on an 88100 or an 88110 */
228   sr_write_cr("rs cr06");
229   sr_expect("rs cr06");
230
231   switch (gr_multi_scan(cpu_check_strings, 0))
232     {
233     case 0: /* this is an m88100 */
234       target_is_m88110 = 0;
235       break;
236     case 1: /* this is an m88110 */
237       target_is_m88110 = 1;
238       break;
239     default:
240       abort();
241     }
242 }
243
244 /* Tell the remote machine to resume.  */
245
246 void
247 bug_resume (pid, step, sig)
248      int pid, step;
249      enum target_signal sig;
250 {
251   dcache_flush (gr_get_dcache());
252
253   if (step)
254     {
255       sr_write_cr("t");
256
257       /* Force the next bug_wait to return a trap.  Not doing anything
258        about I/O from the target means that the user has to type
259        "continue" to see any.  FIXME, this should be fixed.  */
260       need_artificial_trap = 1;
261     }
262   else
263       sr_write_cr ("g");
264
265   return;
266 }
267
268 /* Wait until the remote machine stops, then return,
269    storing status in STATUS just as `wait' would.  */
270
271 static char *wait_strings[] = {
272   "At Breakpoint",
273   "Exception: Data Access Fault (Local Bus Timeout)",
274   "\r8??\?-Bug>",       /* The '\?' avoids creating a trigraph */
275   "\r197-Bug>",
276   NULL,
277 };
278
279 int
280 bug_wait (pid, status)
281      int pid;
282      struct target_waitstatus *status;
283 {
284   int old_timeout = sr_get_timeout();
285   int old_immediate_quit = immediate_quit;
286
287   status->kind = TARGET_WAITKIND_EXITED;
288   status->value.integer = 0;
289
290   /* read off leftovers from resume so that the rest can be passed
291      back out as stdout.  */
292   if (need_artificial_trap == 0)
293     {
294       sr_expect("Effective address: ");
295       (void) sr_get_hex_word();
296       sr_expect ("\r\n");
297     }
298
299   sr_set_timeout(-1); /* Don't time out -- user program is running. */
300   immediate_quit = 1; /* Helps ability to QUIT */
301
302   switch (gr_multi_scan(wait_strings, need_artificial_trap == 0))
303     {
304     case 0: /* breakpoint case */
305       status->kind = TARGET_WAITKIND_STOPPED;
306       status->value.sig = TARGET_SIGNAL_TRAP;
307       /* user output from the target can be discarded here. (?) */
308       gr_expect_prompt();
309       break;
310
311     case 1: /* bus error */
312       status->kind = TARGET_WAITKIND_STOPPED;
313       status->value.sig = TARGET_SIGNAL_BUS;
314       /* user output from the target can be discarded here. (?) */
315       gr_expect_prompt();
316       break;
317
318     case 2: /* normal case */
319     case 3:
320       if (need_artificial_trap != 0)
321         {
322           /* stepping */
323           status->kind = TARGET_WAITKIND_STOPPED;
324           status->value.sig = TARGET_SIGNAL_TRAP;
325           need_artificial_trap--;
326           break;
327         }
328       else
329         {
330           /* exit case */
331           status->kind = TARGET_WAITKIND_EXITED;
332           status->value.integer = 0;
333           break;
334         }
335
336     case -1: /* trouble */
337     default:
338       fprintf_filtered (gdb_stderr,
339                         "Trouble reading target during wait\n");
340       break;
341     }
342
343   sr_set_timeout(old_timeout);
344   immediate_quit = old_immediate_quit;
345   return 0;
346 }
347
348 /* Return the name of register number REGNO
349    in the form input and output by bug.
350
351    Returns a pointer to a static buffer containing the answer.  */
352 static char *
353 get_reg_name (regno)
354      int regno;
355 {
356   static char *rn[] = {
357     "r00", "r01", "r02", "r03", "r04", "r05", "r06", "r07",
358     "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15",
359     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
360     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
361
362     /* these get confusing because we omit a few and switch some ordering around. */
363
364     "cr01",  /* 32 = psr */
365     "fcr62", /* 33 = fpsr*/
366     "fcr63", /* 34 = fpcr */
367     "ip",                       /* this is something of a cheat. */
368   /* 35 = sxip */
369     "cr05", /* 36 = snip */
370     "cr06", /* 37 = sfip */
371
372     "x00", "x01", "x02", "x03", "x04", "x05", "x06", "x07",
373     "x08", "x09", "x10", "x11", "x12", "x13", "x14", "x15",
374     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
375     "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
376   };
377
378   return rn[regno];
379 }
380
381 #if 0 /* not currently used */
382 /* Read from remote while the input matches STRING.  Return zero on
383    success, -1 on failure.  */
384
385 static int
386 bug_scan (s)
387      char *s;
388 {
389   int c;
390
391   while (*s)
392     {
393       c = sr_readchar();
394       if (c != *s++)
395         {
396           fflush(stdout);
397           printf("\nNext character is '%c' - %d and s is \"%s\".\n", c, c, --s);
398           return(-1);
399         }
400     }
401
402   return(0);
403 }
404 #endif /* never */
405
406 static int
407 bug_srec_write_cr (s)
408      char *s;
409 {
410   char *p = s;
411
412   if (srec_echo_pace)
413     for (p = s; *p; ++p)
414       {
415         if (sr_get_debug() > 0)
416           printf ("%c", *p);
417
418         do
419           SERIAL_WRITE(sr_get_desc(), p, 1);
420         while (sr_pollchar() != *p);
421       }
422   else
423     {  
424       sr_write_cr (s);
425 /*       return(bug_scan (s) || bug_scan ("\n")); */
426     }
427
428   return(0);
429 }
430
431 /* Store register REGNO, or all if REGNO == -1. */
432
433 static void
434 bug_fetch_register(regno)
435      int regno;
436 {
437   sr_check_open();
438
439   if (regno == -1)
440     {
441       int i;
442
443       for (i = 0; i < NUM_REGS; ++i)
444         bug_fetch_register(i);
445     }
446   else if (target_is_m88110 && regno == SFIP_REGNUM)
447     {
448       /* m88110 has no sfip. */
449       long l = 0;
450       supply_register(regno, (char *) &l);
451     }
452   else if (regno < XFP_REGNUM)
453     {
454       char buffer[MAX_REGISTER_RAW_SIZE];
455
456       sr_write ("rs ", 3);
457       sr_write_cr (get_reg_name(regno));
458       sr_expect ("=");
459       store_unsigned_integer (buffer, REGISTER_RAW_SIZE (regno),
460                               sr_get_hex_word());
461       gr_expect_prompt ();
462       supply_register (regno, buffer);
463     }
464   else
465     {
466       /* Float register so we need to parse a strange data format. */
467       long p;
468       unsigned char fpreg_buf[10];
469
470       sr_write("rs ", 3);
471       sr_write(get_reg_name(regno), strlen(get_reg_name(regno)));
472       sr_write_cr(";d");
473       sr_expect("rs");
474       sr_expect(get_reg_name(regno));
475       sr_expect(";d");
476       sr_expect("=");
477
478       /* sign */
479       p = sr_get_hex_digit(1);
480       fpreg_buf[0] = p << 7;
481
482       /* exponent */
483       sr_expect("_");
484       p = sr_get_hex_digit(1);
485       fpreg_buf[0] += (p << 4);
486       fpreg_buf[0] += sr_get_hex_digit(1);
487
488       fpreg_buf[1] = sr_get_hex_digit(1) << 4;
489
490       /* fraction */
491       sr_expect("_");
492       fpreg_buf[1] += sr_get_hex_digit(1);
493
494       fpreg_buf[2] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
495       fpreg_buf[3] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
496       fpreg_buf[4] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
497       fpreg_buf[5] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
498       fpreg_buf[6] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
499       fpreg_buf[7] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1);
500       fpreg_buf[8] = 0;
501       fpreg_buf[9] = 0;
502
503       gr_expect_prompt();
504       supply_register(regno, fpreg_buf);
505     }
506
507   return;
508 }
509
510 /* Store register REGNO, or all if REGNO == -1. */
511
512 static void
513 bug_store_register (regno)
514      int regno;
515 {
516   char buffer[1024];
517   sr_check_open();
518
519   if (regno == -1)
520     {
521       int i;
522
523       for (i = 0; i < NUM_REGS; ++i)
524         bug_store_register(i);
525     }
526   else
527     {
528       char *regname;
529
530       regname = get_reg_name(regno);
531
532       if (target_is_m88110 && regno == SFIP_REGNUM)
533         return;
534       else if (regno < XFP_REGNUM)
535         sprintf(buffer, "rs %s %08x",
536                 regname,
537                 read_register(regno));
538       else
539         {
540           unsigned char *fpreg_buf =
541             (unsigned char *)&registers[REGISTER_BYTE(regno)];
542           
543           sprintf(buffer, "rs %s %1x_%02x%1x_%1x%02x%02x%02x%02x%02x%02x;d",
544                   regname,
545                   /* sign */
546                   (fpreg_buf[0] >> 7) & 0xf,
547                   /* exponent */
548                   fpreg_buf[0] & 0x7f,
549                   (fpreg_buf[1] >> 8) & 0xf,
550                   /* fraction */
551                   fpreg_buf[1] & 0xf,
552                   fpreg_buf[2],
553                   fpreg_buf[3],
554                   fpreg_buf[4],
555                   fpreg_buf[5],
556                   fpreg_buf[6],
557                   fpreg_buf[7]);
558         }
559
560       sr_write_cr(buffer);
561       gr_expect_prompt();
562     }
563
564   return;
565 }
566
567 int
568 bug_xfer_memory (memaddr, myaddr, len, write, target)
569      CORE_ADDR memaddr;
570      char *myaddr;
571      int len;
572      int write;
573      struct target_ops *target; /* ignored */
574 {
575   register int i;
576
577   /* Round starting address down to longword boundary.  */
578   register CORE_ADDR addr;
579
580   /* Round ending address up; get number of longwords that makes.  */
581   register int count;
582
583   /* Allocate buffer of that many longwords.  */
584   register int *buffer;
585
586   addr = memaddr & -sizeof (int);
587   count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
588
589   buffer = (int *) alloca (count * sizeof (int));
590
591   if (write)
592     {
593       /* Fill start and end extra bytes of buffer with existing memory data.  */
594
595       if (addr != memaddr || len < (int) sizeof (int))
596         {
597           /* Need part of initial word -- fetch it.  */
598           buffer[0] = gr_fetch_word (addr);
599         }
600
601       if (count > 1)            /* FIXME, avoid if even boundary */
602         {
603           buffer[count - 1]
604             = gr_fetch_word (addr + (count - 1) * sizeof (int));
605         }
606
607       /* Copy data to be written over corresponding part of buffer */
608
609       memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
610
611       /* Write the entire buffer.  */
612
613       for (i = 0; i < count; i++, addr += sizeof (int))
614         {
615           errno = 0;
616           gr_store_word (addr, buffer[i]);
617           if (errno)
618             {
619
620               return 0;
621             }
622
623         }
624     }
625   else
626     {
627       /* Read all the longwords */
628       for (i = 0; i < count; i++, addr += sizeof (int))
629         {
630           errno = 0;
631           buffer[i] = gr_fetch_word (addr);
632           if (errno)
633             {
634               return 0;
635             }
636           QUIT;
637         }
638
639       /* Copy appropriate bytes out of the buffer.  */
640       memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
641     }
642
643   return len;
644 }
645
646 static void
647 start_load()
648 {
649   char *command;
650
651   command = (srec_echo_pace ? "lo 0 ;x" : "lo 0");
652
653   sr_write_cr (command);
654   sr_expect (command);
655   sr_expect ("\r\n");
656   bug_srec_write_cr ("S0030000FC");
657   return;
658 }
659
660 /* This is an extremely vulnerable and fragile function.  I've made
661    considerable attempts to make this deterministic, but I've
662    certainly forgotten something.  The trouble is that S-records are
663    only a partial file format, not a protocol.  Worse, apparently the
664    m88k bug monitor does not run in real time while receiving
665    S-records.  Hence, we must pay excruciating attention to when and
666    where error messages are returned, and what has actually been sent.
667
668    Each call represents a chunk of memory to be sent to the target.
669    We break that chunk into an S0 header record, some number of S3
670    data records each containing srec_bytes, and an S7 termination
671    record.  */
672
673 static char *srecord_strings[] = {
674   "S-RECORD",
675   "-Bug>",
676   NULL,
677 };
678
679 static int
680 bug_write_memory (memaddr, myaddr, len)
681      CORE_ADDR memaddr;
682      unsigned char *myaddr;
683      int len;
684 {
685   int done;
686   int checksum;
687   int x;
688   int retries;
689   char *buffer = alloca ((srec_bytes + 8) << 1);
690
691   retries = 0;
692
693   do
694     {
695       done = 0;
696
697       if (retries > srec_max_retries)
698         return(-1);
699
700       if (retries > 0)
701         {
702           if (sr_get_debug() > 0)
703             printf("\n<retrying...>\n");
704
705           /* This gr_expect_prompt call is extremely important.  Without
706              it, we will tend to resend our packet so fast that it
707              will arrive before the bug monitor is ready to receive
708              it.  This would lead to a very ugly resend loop.  */
709
710           gr_expect_prompt();
711         }
712
713       start_load();
714
715       while (done < len)
716         {
717           int thisgo;
718           int idx;
719           char *buf = buffer;
720           CORE_ADDR address;
721
722           checksum = 0;
723           thisgo = len - done;
724           if (thisgo > srec_bytes)
725             thisgo = srec_bytes;
726
727           address = memaddr + done;
728           sprintf (buf, "S3%02X%08X", thisgo + 4 + 1, address);
729           buf += 12;
730
731           checksum += (thisgo + 4 + 1
732                        + (address & 0xff)
733                        + ((address >>  8) & 0xff)
734                        + ((address >> 16) & 0xff)
735                        + ((address >> 24) & 0xff));
736
737           for (idx = 0; idx < thisgo; idx++)
738             {
739               sprintf (buf, "%02X", myaddr[idx + done]);
740               checksum += myaddr[idx + done];
741               buf += 2;
742             }
743
744           if (srec_noise > 0)
745             {
746               /* FIXME-NOW: insert a deliberate error every now and then.
747                  This is intended for testing/debugging the error handling
748                  stuff.  */
749               static int counter = 0;
750               if (++counter > srec_noise)
751                 {
752                   counter = 0;
753                   ++checksum;
754                 }
755             }
756
757           sprintf(buf, "%02X", ~checksum & 0xff);
758           bug_srec_write_cr (buffer);
759
760           if (srec_sleep != 0)
761             sleep(srec_sleep);
762
763           /* This pollchar is probably redundant to the gr_multi_scan
764              below.  Trouble is, we can't be sure when or where an
765              error message will appear.  Apparently, when running at
766              full speed from a typical sun4, error messages tend to
767              appear to arrive only *after* the s7 record.   */
768
769           if ((x = sr_pollchar()) != 0)
770             {
771               if (sr_get_debug() > 0)
772                 printf("\n<retrying...>\n");
773
774               ++retries;
775
776               /* flush any remaining input and verify that we are back
777                  at the prompt level. */
778               gr_expect_prompt();
779               /* start all over again. */
780               start_load();
781               done = 0;
782               continue;
783             }
784
785           done += thisgo;
786         }
787
788       bug_srec_write_cr("S7060000000000F9");
789       ++retries;
790
791       /* Having finished the load, we need to figure out whether we
792          had any errors.  */
793     } while (gr_multi_scan(srecord_strings, 0) == 0);;
794
795   return(0);
796 }
797
798 /* Copy LEN bytes of data from debugger memory at MYADDR
799    to inferior's memory at MEMADDR.  Returns errno value.
800  * sb/sh instructions don't work on unaligned addresses, when TU=1.
801  */
802
803 /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
804    at debugger address MYADDR.  Returns errno value.  */
805 static int
806 bug_read_memory (memaddr, myaddr, len)
807      CORE_ADDR memaddr;
808      unsigned char *myaddr;
809      int len;
810 {
811   char request[100];
812   char *buffer;
813   char *p;
814   char type;
815   char size;
816   unsigned char c;
817   unsigned int inaddr;
818   unsigned int checksum;
819
820   sprintf(request, "du 0 %x:&%d", memaddr, len);
821   sr_write_cr(request);
822
823   p = buffer = alloca(len);
824
825   /* scan up through the header */
826   sr_expect("S0030000FC");
827
828   while (p < buffer + len)
829     {
830       /* scan off any white space. */
831       while (sr_readchar() != 'S') ;;
832
833       /* what kind of s-rec? */
834       type = sr_readchar();
835
836       /* scan record size */
837       sr_get_hex_byte(&size);
838       checksum = size;
839       --size;
840       inaddr = 0;
841
842       switch (type)
843         {
844         case '7':
845         case '8':
846         case '9':
847           goto done;
848
849         case '3':
850           sr_get_hex_byte(&c);
851           inaddr = (inaddr << 8) + c;
852           checksum += c;
853           --size;
854           /* intentional fall through */
855         case '2':
856           sr_get_hex_byte(&c);
857           inaddr = (inaddr << 8) + c;
858           checksum += c;
859           --size;
860           /* intentional fall through */
861         case '1':
862           sr_get_hex_byte(&c);
863           inaddr = (inaddr << 8) + c;
864           checksum += c;
865           --size;
866           sr_get_hex_byte(&c);
867           inaddr = (inaddr << 8) + c;
868           checksum += c;
869           --size;
870           break;
871
872         default:
873           /* bonk */
874           error("reading s-records.");
875         }
876
877       if (inaddr < memaddr
878           || (memaddr + len) < (inaddr + size))
879         error("srec out of memory range.");
880
881       if (p != buffer + inaddr - memaddr)
882         error("srec out of sequence.");
883
884       for (; size; --size, ++p)
885         {
886           sr_get_hex_byte(p);
887           checksum += *p;
888         }
889
890       sr_get_hex_byte(&c);
891       if (c != (~checksum & 0xff))
892         error("bad s-rec checksum");
893     }
894
895  done:
896   gr_expect_prompt();
897   if (p != buffer + len)
898     return(1);
899
900   memcpy(myaddr, buffer, len);
901   return(0);
902 }
903
904 #define MAX_BREAKS      16
905 static int num_brkpts = 0;
906 static int
907 bug_insert_breakpoint (addr, save)
908      CORE_ADDR addr;
909      char *save;                /* Throw away, let bug save instructions */
910 {
911   sr_check_open ();
912
913   if (num_brkpts < MAX_BREAKS)
914     {
915       char buffer[100];
916
917       num_brkpts++;
918       sprintf (buffer, "br %x", addr);
919       sr_write_cr (buffer);
920       gr_expect_prompt ();
921       return(0);
922     }
923   else
924     {
925       fprintf_filtered (gdb_stderr,
926                       "Too many break points, break point not installed\n");
927       return(1);
928     }
929
930 }
931 static int
932 bug_remove_breakpoint (addr, save)
933      CORE_ADDR addr;
934      char *save;                /* Throw away, let bug save instructions */
935 {
936   if (num_brkpts > 0)
937     {
938       char buffer[100];
939
940       num_brkpts--;
941       sprintf (buffer, "nobr %x", addr);
942       sr_write_cr (buffer);
943       gr_expect_prompt ();
944
945     }
946   return (0);
947 }
948
949 /* Clear the bugs notion of what the break points are */
950 static int
951 bug_clear_breakpoints ()
952 {
953
954   if (sr_is_open())
955     {
956       sr_write_cr ("nobr");
957       sr_expect("nobr");
958       gr_expect_prompt ();
959     }
960   num_brkpts = 0;
961   return(0);
962 }
963
964 struct target_ops bug_ops ;
965
966 static void init_bug_ops(void)
967 {
968   bug_ops.to_shortname =   "bug"; "Remote BUG monitor",
969                                     bug_ops.to_longname =   "Use the mvme187 board running the BUG monitor connected by a serial line.";
970   bug_ops.to_doc =   " ";
971   bug_ops.to_open =   bug_open;
972   bug_ops.to_close =   gr_close;
973   bug_ops.to_attach =   0;
974   bug_ops.to_post_attach = NULL;
975   bug_ops.to_require_attach = NULL;
976   bug_ops.to_detach =   gr_detach;
977   bug_ops.to_require_detach = NULL;
978   bug_ops.to_resume =   bug_resume;
979   bug_ops.to_wait  =   bug_wait;
980   bug_ops.to_post_wait = NULL;
981   bug_ops.to_fetch_registers  =   bug_fetch_register;
982   bug_ops.to_store_registers  =   bug_store_register;
983   bug_ops.to_prepare_to_store =   gr_prepare_to_store;
984   bug_ops.to_xfer_memory  =   bug_xfer_memory;
985   bug_ops.to_files_info  =   gr_files_info;
986   bug_ops.to_insert_breakpoint =   bug_insert_breakpoint;
987   bug_ops.to_remove_breakpoint =   bug_remove_breakpoint;
988   bug_ops.to_terminal_init  =   0;
989   bug_ops.to_terminal_inferior =   0;
990   bug_ops.to_terminal_ours_for_output =   0;
991   bug_ops.to_terminal_ours  =   0;
992   bug_ops.to_terminal_info  =   0;              
993   bug_ops.to_kill  =   gr_kill;                 
994   bug_ops.to_load  =   bug_load;
995   bug_ops.to_lookup_symbol =   0;                       
996   bug_ops.to_create_inferior =   gr_create_inferior;    
997   bug_ops.to_post_startup_inferior = NULL;
998   bug_ops.to_acknowledge_created_inferior = NULL;
999   bug_ops.to_clone_and_follow_inferior = NULL;          
1000   bug_ops.to_post_follow_inferior_by_clone = NULL;  
1001   bug_ops.to_insert_fork_catchpoint = NULL;
1002   bug_ops.to_remove_fork_catchpoint = NULL;
1003   bug_ops.to_insert_vfork_catchpoint = NULL;
1004   bug_ops.to_remove_vfork_catchpoint = NULL;                      
1005   bug_ops.to_has_forked = NULL;
1006   bug_ops.to_has_vforked = NULL; 
1007   bug_ops.to_can_follow_vfork_prior_to_exec = NULL;                       
1008   bug_ops.to_post_follow_vfork = NULL;
1009   bug_ops.to_insert_exec_catchpoint = NULL;
1010   bug_ops.to_remove_exec_catchpoint = NULL;
1011   bug_ops.to_has_execd = NULL;
1012   bug_ops.to_reported_exec_events_per_exec_call = NULL;
1013   bug_ops.to_has_exited = NULL;
1014   bug_ops.to_mourn_inferior =   gr_mourn;               
1015   bug_ops.to_can_run  =   0;                            
1016   bug_ops.to_notice_signals =   0;                      
1017   bug_ops.to_thread_alive  =  0 ;
1018   bug_ops.to_stop  =   0;
1019   bug_ops.to_pid_to_exec_file = NULL;
1020   bug_ops.to_core_file_to_sym_file = NULL;
1021   bug_ops.to_stratum =   process_stratum ;
1022   bug_ops.DONT_USE =   0;
1023   bug_ops.to_has_all_memory =   1;
1024   bug_ops.to_has_memory =   1;
1025   bug_ops.to_has_stack =   1;   
1026   bug_ops.to_has_registers =   0;
1027   bug_ops.to_has_execution =   0;                       
1028   bug_ops.to_sections =   0 ;
1029   bug_ops.to_sections_end = 0 ;
1030   bug_ops.to_magic = OPS_MAGIC;                 /* Always the last thing */
1031 } /* init_bug_ops */
1032
1033 void
1034 _initialize_remote_bug ()
1035 {
1036   init_bug_ops() ;
1037   add_target (&bug_ops);
1038
1039   add_show_from_set
1040     (add_set_cmd ("srec-bytes", class_support, var_uinteger,
1041                   (char *) &srec_bytes,
1042                   "\
1043 Set the number of bytes represented in each S-record.\n\
1044 This affects the communication protocol with the remote target.",
1045                   &setlist),
1046      &showlist);
1047
1048   add_show_from_set
1049     (add_set_cmd ("srec-max-retries", class_support, var_uinteger,
1050                   (char *) &srec_max_retries,
1051                   "\
1052 Set the number of retries for shipping S-records.\n\
1053 This affects the communication protocol with the remote target.",
1054                   &setlist),
1055      &showlist);
1056
1057 #if 0
1058   /* This needs to set SREC_SIZE, not srec_frame which gets changed at the
1059      end of a download.  But do we need the option at all?  */
1060   add_show_from_set
1061     (add_set_cmd ("srec-frame", class_support, var_uinteger,
1062                   (char *) &srec_frame,
1063                   "\
1064 Set the number of bytes in an S-record frame.\n\
1065 This affects the communication protocol with the remote target.",
1066                   &setlist),
1067      &showlist);
1068 #endif /* 0 */
1069
1070   add_show_from_set
1071     (add_set_cmd ("srec-noise", class_support, var_zinteger,
1072                   (char *) &srec_noise,
1073                   "\
1074 Set number of S-record to send before deliberately flubbing a checksum.\n\
1075 Zero means flub none at all.  This affects the communication protocol\n\
1076 with the remote target.",
1077                   &setlist),
1078      &showlist);
1079
1080   add_show_from_set
1081     (add_set_cmd ("srec-sleep", class_support, var_zinteger,
1082                   (char *) &srec_sleep,
1083                   "\
1084 Set number of seconds to sleep after an S-record for a possible error message to arrive.\n\
1085 This affects the communication protocol with the remote target.",
1086                   &setlist),
1087      &showlist);
1088
1089   add_show_from_set
1090     (add_set_cmd ("srec-echo-pace", class_support, var_boolean,
1091                   (char *) &srec_echo_pace,
1092                   "\
1093 Set echo-verification.\n\
1094 When on, use verification by echo when downloading S-records.  This is\n\
1095 much slower, but generally more reliable.", 
1096                   &setlist),
1097      &showlist);
1098 }