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