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