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