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