OSDN Git Service

Cleanup delete_breakpoint cleanups.
[pf3gnuchains/pf3gnuchains4x.git] / gdb / remote-eb.c
1 /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB.
2    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.  Written by Jim Kingdon for Cygnus.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /* This is like remote.c but is for an esoteric situation--
23    having a a29k board in a PC hooked up to a unix machine with
24    a serial line, and running ctty com1 on the PC, through which
25    the unix machine can run ebmon.  Not to mention that the PC
26    has PC/NFS, so it can access the same executables that gdb can,
27    over the net in real time.  */
28
29 #include "defs.h"
30 #include "gdb_string.h"
31
32 #include "inferior.h"
33 #include "bfd.h"
34 #include "symfile.h"
35 #include "gdb_wait.h"
36 #include "value.h"
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include "terminal.h"
42 #include "target.h"
43 #include "gdbcore.h"
44
45 extern struct target_ops eb_ops;        /* Forward declaration */
46
47 static void eb_close ();
48
49 #define LOG_FILE "eb.log"
50 #if defined (LOG_FILE)
51 FILE *log_file;
52 #endif
53
54 static int timeout = 24;
55
56 /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
57    eb_open knows that we don't have a file open when the program
58    starts.  */
59 int eb_desc = -1;
60
61 /* stream which is fdopen'd from eb_desc.  Only valid when
62    eb_desc != -1.  */
63 FILE *eb_stream;
64
65 /* Read a character from the remote system, doing all the fancy
66    timeout stuff.  */
67 static int
68 readchar ()
69 {
70   char buf;
71
72   buf = '\0';
73 #ifdef HAVE_TERMIO
74   /* termio does the timeout for us.  */
75   read (eb_desc, &buf, 1);
76 #else
77   alarm (timeout);
78   if (read (eb_desc, &buf, 1) < 0)
79     {
80       if (errno == EINTR)
81         error ("Timeout reading from remote system.");
82       else
83         perror_with_name ("remote");
84     }
85   alarm (0);
86 #endif
87
88   if (buf == '\0')
89     error ("Timeout reading from remote system.");
90 #if defined (LOG_FILE)
91   putc (buf & 0x7f, log_file);
92 #endif
93   return buf & 0x7f;
94 }
95
96 /* Keep discarding input from the remote system, until STRING is found. 
97    Let the user break out immediately.  */
98 static void
99 expect (string)
100      char *string;
101 {
102   char *p = string;
103
104   immediate_quit = 1;
105   while (1)
106     {
107       if (readchar () == *p)
108         {
109           p++;
110           if (*p == '\0')
111             {
112               immediate_quit = 0;
113               return;
114             }
115         }
116       else
117         p = string;
118     }
119 }
120
121 /* Keep discarding input until we see the ebmon prompt.
122
123    The convention for dealing with the prompt is that you
124    o give your command
125    o *then* wait for the prompt.
126
127    Thus the last thing that a procedure does with the serial line
128    will be an expect_prompt().  Exception:  eb_resume does not
129    wait for the prompt, because the terminal is being handed over
130    to the inferior.  However, the next thing which happens after that
131    is a eb_wait which does wait for the prompt.
132    Note that this includes abnormal exit, e.g. error().  This is
133    necessary to prevent getting into states from which we can't
134    recover.  */
135 static void
136 expect_prompt ()
137 {
138 #if defined (LOG_FILE)
139   /* This is a convenient place to do this.  The idea is to do it often
140      enough that we never lose much data if we terminate abnormally.  */
141   fflush (log_file);
142 #endif
143   expect ("\n# ");
144 }
145
146 /* Get a hex digit from the remote system & return its value.
147    If ignore_space is nonzero, ignore spaces (not newline, tab, etc).  */
148 static int
149 get_hex_digit (ignore_space)
150      int ignore_space;
151 {
152   int ch;
153   while (1)
154     {
155       ch = readchar ();
156       if (ch >= '0' && ch <= '9')
157         return ch - '0';
158       else if (ch >= 'A' && ch <= 'F')
159         return ch - 'A' + 10;
160       else if (ch >= 'a' && ch <= 'f')
161         return ch - 'a' + 10;
162       else if (ch == ' ' && ignore_space)
163         ;
164       else
165         {
166           expect_prompt ();
167           error ("Invalid hex digit from remote system.");
168         }
169     }
170 }
171
172 /* Get a byte from eb_desc and put it in *BYT.  Accept any number
173    leading spaces.  */
174 static void
175 get_hex_byte (byt)
176      char *byt;
177 {
178   int val;
179
180   val = get_hex_digit (1) << 4;
181   val |= get_hex_digit (0);
182   *byt = val;
183 }
184
185 /* Get N 32-bit words from remote, each preceded by a space,
186    and put them in registers starting at REGNO.  */
187 static void
188 get_hex_regs (n, regno)
189      int n;
190      int regno;
191 {
192   long val;
193   int i;
194
195   for (i = 0; i < n; i++)
196     {
197       int j;
198
199       val = 0;
200       for (j = 0; j < 8; j++)
201         val = (val << 4) + get_hex_digit (j == 0);
202       supply_register (regno++, (char *) &val);
203     }
204 }
205
206 /* Called when SIGALRM signal sent due to alarm() timeout.  */
207 #ifndef HAVE_TERMIO
208
209 #ifndef __STDC__
210 #define volatile
211 /**/
212 #endif
213 volatile int n_alarms;
214
215 void
216 eb_timer ()
217 {
218 #if 0
219   if (kiodebug)
220     printf ("eb_timer called\n");
221 #endif
222   n_alarms++;
223 }
224 #endif
225
226 /* malloc'd name of the program on the remote system.  */
227 static char *prog_name = NULL;
228
229 /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
230    command.  "gi" is supposed to happen exactly once for each "yc".  */
231 static int need_gi = 0;
232
233 /* Number of SIGTRAPs we need to simulate.  That is, the next
234    NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
235    SIGTRAP without actually waiting for anything.  */
236
237 static int need_artificial_trap = 0;
238
239 /* This is called not only when we first attach, but also when the
240    user types "run" after having attached.  */
241 static void
242 eb_create_inferior (execfile, args, env)
243      char *execfile;
244      char *args;
245      char **env;
246 {
247   int entry_pt;
248
249   if (args && *args)
250     error ("Can't pass arguments to remote EBMON process");
251
252   if (execfile == 0 || exec_bfd == 0)
253     error ("No executable file specified");
254
255   entry_pt = (int) bfd_get_start_address (exec_bfd);
256
257   {
258     /* OK, now read in the file.  Y=read, C=COFF, D=no symbols
259        0=start address, %s=filename.  */
260
261     fprintf (eb_stream, "YC D,0:%s", prog_name);
262
263     if (args != NULL)
264       fprintf (eb_stream, " %s", args);
265
266     fprintf (eb_stream, "\n");
267     fflush (eb_stream);
268
269     expect_prompt ();
270
271     need_gi = 1;
272   }
273
274 /* The "process" (board) is already stopped awaiting our commands, and
275    the program is already downloaded.  We just set its PC and go.  */
276
277   clear_proceed_status ();
278
279   /* Tell wait_for_inferior that we've started a new process.  */
280   init_wait_for_inferior ();
281
282   /* Set up the "saved terminal modes" of the inferior
283      based on what modes we are starting it with.  */
284   target_terminal_init ();
285
286   /* Install inferior's terminal modes.  */
287   target_terminal_inferior ();
288
289   /* insert_step_breakpoint ();  FIXME, do we need this?  */
290   proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);     /* Let 'er rip... */
291 }
292
293 /* Translate baud rates from integers to damn B_codes.  Unix should
294    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
295
296 #ifndef B19200
297 #define B19200 EXTA
298 #endif
299 #ifndef B38400
300 #define B38400 EXTB
301 #endif
302
303 struct
304 {
305   int rate, damn_b;
306 }
307 baudtab[] =
308 {
309   {
310     0, B0
311   }
312   ,
313   {
314     50, B50
315   }
316   ,
317   {
318     75, B75
319   }
320   ,
321   {
322     110, B110
323   }
324   ,
325   {
326     134, B134
327   }
328   ,
329   {
330     150, B150
331   }
332   ,
333   {
334     200, B200
335   }
336   ,
337   {
338     300, B300
339   }
340   ,
341   {
342     600, B600
343   }
344   ,
345   {
346     1200, B1200
347   }
348   ,
349   {
350     1800, B1800
351   }
352   ,
353   {
354     2400, B2400
355   }
356   ,
357   {
358     4800, B4800
359   }
360   ,
361   {
362     9600, B9600
363   }
364   ,
365   {
366     19200, B19200
367   }
368   ,
369   {
370     38400, B38400
371   }
372   ,
373   {
374     -1, -1
375   }
376   ,
377 };
378
379 int
380 damn_b (rate)
381      int rate;
382 {
383   int i;
384
385   for (i = 0; baudtab[i].rate != -1; i++)
386     if (rate == baudtab[i].rate)
387       return baudtab[i].damn_b;
388   return B38400;                /* Random */
389 }
390
391
392 /* Open a connection to a remote debugger.
393    NAME is the filename used for communication, then a space,
394    then the name of the program as we should name it to EBMON.  */
395
396 static int baudrate = 9600;
397 static char *dev_name;
398 void
399 eb_open (name, from_tty)
400      char *name;
401      int from_tty;
402 {
403   TERMINAL sg;
404
405   char *p;
406
407   target_preopen (from_tty);
408
409   /* Find the first whitespace character, it separates dev_name from
410      prog_name.  */
411   if (name == 0)
412     goto erroid;
413
414   for (p = name;
415        *p != '\0' && !isspace (*p); p++)
416     ;
417   if (*p == '\0')
418   erroid:
419     error ("\
420 Please include the name of the device for the serial port,\n\
421 the baud rate, and the name of the program to run on the remote system.");
422   dev_name = alloca (p - name + 1);
423   strncpy (dev_name, name, p - name);
424   dev_name[p - name] = '\0';
425
426   /* Skip over the whitespace after dev_name */
427   for (; isspace (*p); p++)
428     /*EMPTY */ ;
429
430   if (1 != sscanf (p, "%d ", &baudrate))
431     goto erroid;
432
433   /* Skip the number and then the spaces */
434   for (; isdigit (*p); p++)
435     /*EMPTY */ ;
436   for (; isspace (*p); p++)
437     /*EMPTY */ ;
438
439   if (prog_name != NULL)
440     free (prog_name);
441   prog_name = savestring (p, strlen (p));
442
443   eb_close (0);
444
445   eb_desc = open (dev_name, O_RDWR);
446   if (eb_desc < 0)
447     perror_with_name (dev_name);
448   ioctl (eb_desc, TIOCGETP, &sg);
449 #ifdef HAVE_TERMIO
450   sg.c_cc[VMIN] = 0;            /* read with timeout.  */
451   sg.c_cc[VTIME] = timeout * 10;
452   sg.c_lflag &= ~(ICANON | ECHO);
453   sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
454 #else
455   sg.sg_ispeed = damn_b (baudrate);
456   sg.sg_ospeed = damn_b (baudrate);
457   sg.sg_flags |= RAW | ANYP;
458   sg.sg_flags &= ~ECHO;
459 #endif
460
461   ioctl (eb_desc, TIOCSETP, &sg);
462   eb_stream = fdopen (eb_desc, "r+");
463
464   push_target (&eb_ops);
465   if (from_tty)
466     printf ("Remote %s debugging %s using %s\n", target_shortname,
467             prog_name, dev_name);
468
469 #ifndef HAVE_TERMIO
470 #ifndef NO_SIGINTERRUPT
471   /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
472      the read.  */
473   if (siginterrupt (SIGALRM, 1) != 0)
474     perror ("eb_open: error in siginterrupt");
475 #endif
476
477   /* Set up read timeout timer.  */
478   if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1)
479     perror ("eb_open: error in signal");
480 #endif
481
482 #if defined (LOG_FILE)
483   log_file = fopen (LOG_FILE, "w");
484   if (log_file == NULL)
485     perror_with_name (LOG_FILE);
486 #endif
487
488   /* Hello?  Are you there?  */
489   write (eb_desc, "\n", 1);
490
491   expect_prompt ();
492 }
493
494 /* Close out all files and local state before this target loses control. */
495
496 static void
497 eb_close (quitting)
498      int quitting;
499 {
500
501   /* Due to a bug in Unix, fclose closes not only the stdio stream,
502      but also the file descriptor.  So we don't actually close
503      eb_desc.  */
504   if (eb_stream)
505     fclose (eb_stream);         /* This also closes eb_desc */
506   if (eb_desc >= 0)
507     /* close (eb_desc); */
508
509     /* Do not try to close eb_desc again, later in the program.  */
510     eb_stream = NULL;
511   eb_desc = -1;
512
513 #if defined (LOG_FILE)
514   if (log_file)
515     {
516       if (ferror (log_file))
517         printf ("Error writing log file.\n");
518       if (fclose (log_file) != 0)
519         printf ("Error closing log file.\n");
520     }
521 #endif
522 }
523
524 /* Terminate the open connection to the remote debugger.
525    Use this when you want to detach and do something else
526    with your gdb.  */
527 void
528 eb_detach (from_tty)
529      int from_tty;
530 {
531   pop_target ();                /* calls eb_close to do the real work */
532   if (from_tty)
533     printf ("Ending remote %s debugging\n", target_shortname);
534 }
535
536 /* Tell the remote machine to resume.  */
537
538 void
539 eb_resume (pid, step, sig)
540      int pid, step;
541      enum target_signal sig;
542 {
543   if (step)
544     {
545       write (eb_desc, "t 1,s\n", 6);
546       /* Wait for the echo.  */
547       expect ("t 1,s\r");
548       /* Then comes a line containing the instruction we stepped to.  */
549       expect ("\n@");
550       /* Then we get the prompt.  */
551       expect_prompt ();
552
553       /* Force the next eb_wait to return a trap.  Not doing anything
554          about I/O from the target means that the user has to type
555          "continue" to see any.  This should be fixed.  */
556       need_artificial_trap = 1;
557     }
558   else
559     {
560       if (need_gi)
561         {
562           need_gi = 0;
563           write (eb_desc, "gi\n", 3);
564
565           /* Swallow the echo of "gi".  */
566           expect ("gi\r");
567         }
568       else
569         {
570           write (eb_desc, "GR\n", 3);
571           /* Swallow the echo.  */
572           expect ("GR\r");
573         }
574     }
575 }
576
577 /* Wait until the remote machine stops, then return,
578    storing status in STATUS just as `wait' would.  */
579
580 int
581 eb_wait (status)
582      struct target_waitstatus *status;
583 {
584   /* Strings to look for.  '?' means match any single character.  
585      Note that with the algorithm we use, the initial character
586      of the string cannot recur in the string, or we will not
587      find some cases of the string in the input.  */
588
589   static char bpt[] = "Invalid interrupt taken - #0x50 - ";
590   /* It would be tempting to look for "\n[__exit + 0x8]\n"
591      but that requires loading symbols with "yc i" and even if
592      we did do that we don't know that the file has symbols.  */
593   static char exitmsg[] = "\n@????????I    JMPTI     GR121,LR0";
594   char *bp = bpt;
595   char *ep = exitmsg;
596
597   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.  */
598   char swallowed[50];
599   /* Current position in swallowed.  */
600   char *swallowed_p = swallowed;
601
602   int ch;
603   int ch_handled;
604
605   int old_timeout = timeout;
606
607   status->kind = TARGET_WAITKIND_EXITED;
608   status->value.integer = 0;
609
610   if (need_artificial_trap != 0)
611     {
612       status->kind = TARGET_WAITKIND_STOPPED;
613       status->value.sig = TARGET_SIGNAL_TRAP;
614       need_artificial_trap--;
615       return 0;
616     }
617
618   timeout = 0;                  /* Don't time out -- user program is running. */
619   while (1)
620     {
621       ch_handled = 0;
622       ch = readchar ();
623       if (ch == *bp)
624         {
625           bp++;
626           if (*bp == '\0')
627             break;
628           ch_handled = 1;
629
630           *swallowed_p++ = ch;
631         }
632       else
633         bp = bpt;
634
635       if (ch == *ep || *ep == '?')
636         {
637           ep++;
638           if (*ep == '\0')
639             break;
640
641           if (!ch_handled)
642             *swallowed_p++ = ch;
643           ch_handled = 1;
644         }
645       else
646         ep = exitmsg;
647
648       if (!ch_handled)
649         {
650           char *p;
651
652           /* Print out any characters which have been swallowed.  */
653           for (p = swallowed; p < swallowed_p; ++p)
654             putc (*p, stdout);
655           swallowed_p = swallowed;
656
657           putc (ch, stdout);
658         }
659     }
660   expect_prompt ();
661   if (*bp == '\0')
662     {
663       status->kind = TARGET_WAITKIND_STOPPED;
664       status->value.sig = TARGET_SIGNAL_TRAP;
665     }
666   else
667     {
668       status->kind = TARGET_WAITKIND_EXITED;
669       status->value.integer = 0;
670     }
671   timeout = old_timeout;
672
673   return 0;
674 }
675
676 /* Return the name of register number REGNO
677    in the form input and output by EBMON.
678
679    Returns a pointer to a static buffer containing the answer.  */
680 static char *
681 get_reg_name (regno)
682      int regno;
683 {
684   static char buf[80];
685   if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
686     sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96);
687   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
688     sprintf (buf, "LR%03d", regno - LR0_REGNUM);
689   else if (regno == Q_REGNUM)
690     strcpy (buf, "SR131");
691   else if (regno >= BP_REGNUM && regno <= CR_REGNUM)
692     sprintf (buf, "SR%03d", regno - BP_REGNUM + 133);
693   else if (regno == ALU_REGNUM)
694     strcpy (buf, "SR132");
695   else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM)
696     sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128);
697   else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM)
698     sprintf (buf, "SR%03d", regno - VAB_REGNUM);
699   else if (regno == GR1_REGNUM)
700     strcpy (buf, "GR001");
701   return buf;
702 }
703
704 /* Read the remote registers into the block REGS.  */
705
706 static void
707 eb_fetch_registers ()
708 {
709   int reg_index;
710   int regnum_index;
711   char tempbuf[10];
712   int i;
713
714 #if 0
715   /* This should not be necessary, because one is supposed to read the
716      registers only when the inferior is stopped (at least with
717      ptrace() and why not make it the same for remote?).  */
718   /* ^A is the "normal character" used to make sure we are talking to EBMON
719      and not to the program being debugged.  */
720   write (eb_desc, "\001\n");
721   expect_prompt ();
722 #endif
723
724   write (eb_desc, "dw gr96,gr127\n", 14);
725   for (reg_index = 96, regnum_index = GR96_REGNUM;
726        reg_index < 128;
727        reg_index += 4, regnum_index += 4)
728     {
729       sprintf (tempbuf, "GR%03d ", reg_index);
730       expect (tempbuf);
731       get_hex_regs (4, regnum_index);
732       expect ("\n");
733     }
734
735   for (i = 0; i < 128; i += 32)
736     {
737       /* The PC has a tendency to hang if we get these
738          all in one fell swoop ("dw lr0,lr127").  */
739       sprintf (tempbuf, "dw lr%d\n", i);
740       write (eb_desc, tempbuf, strlen (tempbuf));
741       for (reg_index = i, regnum_index = LR0_REGNUM + i;
742            reg_index < i + 32;
743            reg_index += 4, regnum_index += 4)
744         {
745           sprintf (tempbuf, "LR%03d ", reg_index);
746           expect (tempbuf);
747           get_hex_regs (4, regnum_index);
748           expect ("\n");
749         }
750     }
751
752   write (eb_desc, "dw sr133,sr133\n", 15);
753   expect ("SR133          ");
754   get_hex_regs (1, BP_REGNUM);
755   expect ("\n");
756
757   write (eb_desc, "dw sr134,sr134\n", 15);
758   expect ("SR134                   ");
759   get_hex_regs (1, FC_REGNUM);
760   expect ("\n");
761
762   write (eb_desc, "dw sr135,sr135\n", 15);
763   expect ("SR135                            ");
764   get_hex_regs (1, CR_REGNUM);
765   expect ("\n");
766
767   write (eb_desc, "dw sr131,sr131\n", 15);
768   expect ("SR131                            ");
769   get_hex_regs (1, Q_REGNUM);
770   expect ("\n");
771
772   write (eb_desc, "dw sr0,sr14\n", 12);
773   for (reg_index = 0, regnum_index = VAB_REGNUM;
774        regnum_index <= LRU_REGNUM;
775        regnum_index += 4, reg_index += 4)
776     {
777       sprintf (tempbuf, "SR%03d ", reg_index);
778       expect (tempbuf);
779       get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index);
780       expect ("\n");
781     }
782
783   /* There doesn't seem to be any way to get these.  */
784   {
785     int val = -1;
786     supply_register (FPE_REGNUM, (char *) &val);
787     supply_register (INTE_REGNUM, (char *) &val);
788     supply_register (FPS_REGNUM, (char *) &val);
789     supply_register (EXO_REGNUM, (char *) &val);
790   }
791
792   write (eb_desc, "dw gr1,gr1\n", 11);
793   expect ("GR001 ");
794   get_hex_regs (1, GR1_REGNUM);
795   expect_prompt ();
796 }
797
798 /* Fetch register REGNO, or all registers if REGNO is -1.
799    Returns errno value.  */
800 void
801 eb_fetch_register (regno)
802      int regno;
803 {
804   if (regno == -1)
805     eb_fetch_registers ();
806   else
807     {
808       char *name = get_reg_name (regno);
809       fprintf (eb_stream, "dw %s,%s\n", name, name);
810       expect (name);
811       expect (" ");
812       get_hex_regs (1, regno);
813       expect_prompt ();
814     }
815   return;
816 }
817
818 /* Store the remote registers from the contents of the block REGS.  */
819
820 static void
821 eb_store_registers ()
822 {
823   int i, j;
824   fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM));
825   expect_prompt ();
826
827   for (j = 0; j < 32; j += 16)
828     {
829       fprintf (eb_stream, "s gr%d,", j + 96);
830       for (i = 0; i < 15; ++i)
831         fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i));
832       fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15));
833       expect_prompt ();
834     }
835
836   for (j = 0; j < 128; j += 16)
837     {
838       fprintf (eb_stream, "s lr%d,", j);
839       for (i = 0; i < 15; ++i)
840         fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i));
841       fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15));
842       expect_prompt ();
843     }
844
845   fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM),
846            read_register (FC_REGNUM), read_register (CR_REGNUM));
847   expect_prompt ();
848   fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM));
849   expect_prompt ();
850   fprintf (eb_stream, "s sr0,");
851   for (i = 0; i < 11; ++i)
852     fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i));
853   fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11));
854   expect_prompt ();
855 }
856
857 /* Store register REGNO, or all if REGNO == 0.
858    Return errno value.  */
859 void
860 eb_store_register (regno)
861      int regno;
862 {
863   if (regno == -1)
864     eb_store_registers ();
865   else
866     {
867       char *name = get_reg_name (regno);
868       fprintf (eb_stream, "s %s,%x\n", name, read_register (regno));
869       /* Setting GR1 changes the numbers of all the locals, so
870          invalidate the register cache.  Do this *after* calling
871          read_register, because we want read_register to return the
872          value that write_register has just stuffed into the registers
873          array, not the value of the register fetched from the
874          inferior.  */
875       if (regno == GR1_REGNUM)
876         registers_changed ();
877       expect_prompt ();
878     }
879 }
880
881 /* Get ready to modify the registers array.  On machines which store
882    individual registers, this doesn't need to do anything.  On machines
883    which store all the registers in one fell swoop, this makes sure
884    that registers contains all the registers from the program being
885    debugged.  */
886
887 void
888 eb_prepare_to_store ()
889 {
890   /* Do nothing, since we can store individual regs */
891 }
892
893
894 /* FIXME-someday!  Merge these two.  */
895 int
896 eb_xfer_inferior_memory (memaddr, myaddr, len, write, target)
897      CORE_ADDR memaddr;
898      char *myaddr;
899      int len;
900      int write;
901      struct target_ops *target; /* ignored */
902 {
903   if (write)
904     return eb_write_inferior_memory (memaddr, myaddr, len);
905   else
906     return eb_read_inferior_memory (memaddr, myaddr, len);
907 }
908
909 void
910 eb_files_info ()
911 {
912   printf ("\tAttached to %s at %d baud and running program %s.\n",
913           dev_name, baudrate, prog_name);
914 }
915
916 /* Copy LEN bytes of data from debugger memory at MYADDR
917    to inferior's memory at MEMADDR.  Returns length moved.  */
918 int
919 eb_write_inferior_memory (memaddr, myaddr, len)
920      CORE_ADDR memaddr;
921      char *myaddr;
922      int len;
923 {
924   int i;
925
926   for (i = 0; i < len; i++)
927     {
928       if ((i % 16) == 0)
929         fprintf (eb_stream, "sb %x,", memaddr + i);
930       if ((i % 16) == 15 || i == len - 1)
931         {
932           fprintf (eb_stream, "%x\n", ((unsigned char *) myaddr)[i]);
933           expect_prompt ();
934         }
935       else
936         fprintf (eb_stream, "%x,", ((unsigned char *) myaddr)[i]);
937     }
938   return len;
939 }
940
941 /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
942    at debugger address MYADDR.  Returns length moved.  */
943 int
944 eb_read_inferior_memory (memaddr, myaddr, len)
945      CORE_ADDR memaddr;
946      char *myaddr;
947      int len;
948 {
949   int i;
950
951   /* Number of bytes read so far.  */
952   int count;
953
954   /* Starting address of this pass.  */
955   unsigned long startaddr;
956
957   /* Number of bytes to read in this pass.  */
958   int len_this_pass;
959
960   /* Note that this code works correctly if startaddr is just less
961      than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
962      thing).  That is, something like
963      eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
964      works--it never adds len to memaddr and gets 0.  */
965   /* However, something like
966      eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
967      doesn't need to work.  Detect it and give up if there's an attempt
968      to do that.  */
969   if (((memaddr - 1) + len) < memaddr)
970     {
971       errno = EIO;
972       return 0;
973     }
974
975   startaddr = memaddr;
976   count = 0;
977   while (count < len)
978     {
979       len_this_pass = 16;
980       if ((startaddr % 16) != 0)
981         len_this_pass -= startaddr % 16;
982       if (len_this_pass > (len - count))
983         len_this_pass = (len - count);
984
985       fprintf (eb_stream, "db %x,%x\n", startaddr,
986                (startaddr - 1) + len_this_pass);
987       expect ("\n");
988
989       /* Look for 8 hex digits.  */
990       i = 0;
991       while (1)
992         {
993           if (isxdigit (readchar ()))
994             ++i;
995           else
996             {
997               expect_prompt ();
998               error ("Hex digit expected from remote system.");
999             }
1000           if (i >= 8)
1001             break;
1002         }
1003
1004       expect ("  ");
1005
1006       for (i = 0; i < len_this_pass; i++)
1007         get_hex_byte (&myaddr[count++]);
1008
1009       expect_prompt ();
1010
1011       startaddr += len_this_pass;
1012     }
1013   return len;
1014 }
1015
1016 static void
1017 eb_kill (args, from_tty)
1018      char *args;
1019      int from_tty;
1020 {
1021   return;                       /* Ignore attempts to kill target system */
1022 }
1023
1024 /* Clean up when a program exits.
1025
1026    The program actually lives on in the remote processor's RAM, and may be
1027    run again without a download.  Don't leave it full of breakpoint
1028    instructions.  */
1029
1030 void
1031 eb_mourn_inferior ()
1032 {
1033   remove_breakpoints ();
1034   unpush_target (&eb_ops);
1035   generic_mourn_inferior ();    /* Do all the proper things now */
1036 }
1037 /* Define the target subroutine names */
1038
1039 struct target_ops eb_ops;
1040
1041 static void
1042 init_eb_ops (void)
1043 {
1044   eb_ops.to_shortname = "amd-eb";
1045   eb_ops.to_longname = "Remote serial AMD EBMON target";
1046   eb_ops.to_doc = "Use a remote computer running EBMON connected by a serial line.\n\
1047 Arguments are the name of the device for the serial line,\n\
1048 the speed to connect at in bits per second, and the filename of the\n\
1049 executable as it exists on the remote computer.  For example,\n\
1050 target amd-eb /dev/ttya 9600 demo",
1051     eb_ops.to_open = eb_open;
1052   eb_ops.to_close = eb_close;
1053   eb_ops.to_attach = 0;
1054   eb_ops.to_post_attach = NULL;
1055   eb_ops.to_require_attach = NULL;
1056   eb_ops.to_detach = eb_detach;
1057   eb_ops.to_require_detach = NULL;
1058   eb_ops.to_resume = eb_resume;
1059   eb_ops.to_wait = eb_wait;
1060   eb_ops.to_post_wait = NULL;
1061   eb_ops.to_fetch_registers = eb_fetch_register;
1062   eb_ops.to_store_registers = eb_store_register;
1063   eb_ops.to_prepare_to_store = eb_prepare_to_store;
1064   eb_ops.to_xfer_memory = eb_xfer_inferior_memory;
1065   eb_ops.to_files_info = eb_files_info;
1066   eb_ops.to_insert_breakpoint = 0;
1067   eb_ops.to_remove_breakpoint = 0;      /* Breakpoints */
1068   eb_ops.to_terminal_init = 0;
1069   eb_ops.to_terminal_inferior = 0;
1070   eb_ops.to_terminal_ours_for_output = 0;
1071   eb_ops.to_terminal_ours = 0;
1072   eb_ops.to_terminal_info = 0;  /* Terminal handling */
1073   eb_ops.to_kill = eb_kill;
1074   eb_ops.to_load = generic_load;        /* load */
1075   eb_ops.to_lookup_symbol = 0;  /* lookup_symbol */
1076   eb_ops.to_create_inferior = eb_create_inferior;
1077   eb_ops.to_post_startup_inferior = NULL;
1078   eb_ops.to_acknowledge_created_inferior = NULL;
1079   eb_ops.to_clone_and_follow_inferior = NULL;
1080   eb_ops.to_post_follow_inferior_by_clone = NULL;
1081   eb_ops.to_insert_fork_catchpoint = NULL;
1082   eb_ops.to_remove_fork_catchpoint = NULL;
1083   eb_ops.to_insert_vfork_catchpoint = NULL;
1084   eb_ops.to_remove_vfork_catchpoint = NULL;
1085   eb_ops.to_has_forked = NULL;
1086   eb_ops.to_has_vforked = NULL;
1087   eb_ops.to_can_follow_vfork_prior_to_exec = NULL;
1088   eb_ops.to_post_follow_vfork = NULL;
1089   eb_ops.to_insert_exec_catchpoint = NULL;
1090   eb_ops.to_remove_exec_catchpoint = NULL;
1091   eb_ops.to_has_execd = NULL;
1092   eb_ops.to_reported_exec_events_per_exec_call = NULL;
1093   eb_ops.to_has_exited = NULL;
1094   eb_ops.to_mourn_inferior = eb_mourn_inferior;
1095   eb_ops.to_can_run = 0;        /* can_run */
1096   eb_ops.to_notice_signals = 0; /* notice_signals */
1097   eb_ops.to_thread_alive = 0;   /* thread-alive */
1098   eb_ops.to_stop = 0;           /* to_stop */
1099   eb_ops.to_pid_to_exec_file = NULL;
1100   eb_ops.to_core_file_to_sym_file = NULL;
1101   eb_ops.to_stratum = process_stratum;
1102   eb_ops.DONT_USE = 0;          /* next */
1103   eb_ops.to_has_all_memory = 1;
1104   eb_ops.to_has_memory = 1;
1105   eb_ops.to_has_stack = 1;
1106   eb_ops.to_has_registers = 1;
1107   eb_ops.to_has_execution = 1;  /* all mem, mem, stack, regs, exec */
1108   eb_ops.to_sections = 0;       /* sections */
1109   eb_ops.to_sections_end = 0;   /* sections end */
1110   eb_ops.to_magic = OPS_MAGIC;  /* Always the last thing */
1111 };
1112
1113 void
1114 _initialize_remote_eb ()
1115 {
1116   init_eb_ops ();
1117   add_target (&eb_ops);
1118 }