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