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