* stabsread.c (rs6000_builtin_type): Make logical types be
[external/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., 675 Mass Ave, Cambridge, MA 02139, 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 <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 #ifdef CREATE_INFERIOR_HOOK
256   CREATE_INFERIOR_HOOK (0);             /* No process-ID */
257 #endif  
258
259   {
260     /* OK, now read in the file.  Y=read, C=COFF, D=no symbols
261        0=start address, %s=filename.  */
262
263     fprintf (eb_stream, "YC D,0:%s", prog_name);
264
265     if (args != NULL)
266         fprintf(eb_stream, " %s", args);
267
268     fprintf (eb_stream, "\n");
269     fflush (eb_stream);
270
271     expect_prompt ();
272
273     need_gi = 1;
274   }
275
276 /* The "process" (board) is already stopped awaiting our commands, and
277    the program is already downloaded.  We just set its PC and go.  */
278
279   clear_proceed_status ();
280
281   /* Tell wait_for_inferior that we've started a new process.  */
282   init_wait_for_inferior ();
283
284   /* Set up the "saved terminal modes" of the inferior
285      based on what modes we are starting it with.  */
286   target_terminal_init ();
287
288   /* Install inferior's terminal modes.  */
289   target_terminal_inferior ();
290
291   /* insert_step_breakpoint ();  FIXME, do we need this?  */
292   proceed ((CORE_ADDR)entry_pt, -1, 0);         /* Let 'er rip... */
293 }
294
295 /* Translate baud rates from integers to damn B_codes.  Unix should
296    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
297
298 #ifndef B19200
299 #define B19200 EXTA
300 #endif
301 #ifndef B38400
302 #define B38400 EXTB
303 #endif
304
305 struct {int rate, damn_b;} baudtab[] = {
306         {0, B0},
307         {50, B50},
308         {75, B75},
309         {110, B110},
310         {134, B134},
311         {150, B150},
312         {200, B200},
313         {300, B300},
314         {600, B600},
315         {1200, B1200},
316         {1800, B1800},
317         {2400, B2400},
318         {4800, B4800},
319         {9600, B9600},
320         {19200, B19200},
321         {38400, B38400},
322         {-1, -1},
323 };
324
325 int damn_b (rate)
326      int rate;
327 {
328   int i;
329
330   for (i = 0; baudtab[i].rate != -1; i++)
331     if (rate == baudtab[i].rate) return baudtab[i].damn_b;
332   return B38400;        /* Random */
333 }
334
335
336 /* Open a connection to a remote debugger.
337    NAME is the filename used for communication, then a space,
338    then the name of the program as we should name it to EBMON.  */
339
340 static int baudrate = 9600;
341 static char *dev_name;
342 void
343 eb_open (name, from_tty)
344      char *name;
345      int from_tty;
346 {
347   TERMINAL sg;
348
349   char *p;
350
351   target_preopen (from_tty);
352   
353   /* Find the first whitespace character, it separates dev_name from
354      prog_name.  */
355   if (name == 0)
356     goto erroid;
357
358   for (p = name;
359        *p != '\0' && !isspace (*p); p++)
360     ;
361   if (*p == '\0')
362 erroid:
363     error ("\
364 Please include the name of the device for the serial port,\n\
365 the baud rate, and the name of the program to run on the remote system.");
366   dev_name = alloca (p - name + 1);
367   strncpy (dev_name, name, p - name);
368   dev_name[p - name] = '\0';
369
370   /* Skip over the whitespace after dev_name */
371   for (; isspace (*p); p++)
372     /*EMPTY*/;
373   
374   if (1 != sscanf (p, "%d ", &baudrate))
375     goto erroid;
376
377   /* Skip the number and then the spaces */
378   for (; isdigit (*p); p++)
379     /*EMPTY*/;
380   for (; isspace (*p); p++)
381     /*EMPTY*/;
382   
383   if (prog_name != NULL)
384     free (prog_name);
385   prog_name = savestring (p, strlen (p));
386
387   eb_close (0);
388
389   eb_desc = open (dev_name, O_RDWR);
390   if (eb_desc < 0)
391     perror_with_name (dev_name);
392   ioctl (eb_desc, TIOCGETP, &sg);
393 #ifdef HAVE_TERMIO
394   sg.c_cc[VMIN] = 0;            /* read with timeout.  */
395   sg.c_cc[VTIME] = timeout * 10;
396   sg.c_lflag &= ~(ICANON | ECHO);
397   sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
398 #else
399   sg.sg_ispeed = damn_b (baudrate);
400   sg.sg_ospeed = damn_b (baudrate);
401   sg.sg_flags |= RAW | ANYP;
402   sg.sg_flags &= ~ECHO;
403 #endif
404
405   ioctl (eb_desc, TIOCSETP, &sg);
406   eb_stream = fdopen (eb_desc, "r+");
407
408   push_target (&eb_ops);
409   if (from_tty)
410     printf ("Remote %s debugging %s using %s\n", target_shortname,
411             prog_name, dev_name);
412
413 #ifndef HAVE_TERMIO
414 #ifndef NO_SIGINTERRUPT
415   /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
416      the read.  */
417   if (siginterrupt (SIGALRM, 1) != 0)
418     perror ("eb_open: error in siginterrupt");
419 #endif
420
421   /* Set up read timeout timer.  */
422   if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1)
423     perror ("eb_open: error in signal");
424 #endif
425
426 #if defined (LOG_FILE)
427   log_file = fopen (LOG_FILE, "w");
428   if (log_file == NULL)
429     perror_with_name (LOG_FILE);
430 #endif
431
432   /* Hello?  Are you there?  */
433   write (eb_desc, "\n", 1);
434   
435   expect_prompt ();
436 }
437
438 /* Close out all files and local state before this target loses control. */
439
440 static void
441 eb_close (quitting)
442      int quitting;
443 {
444
445   /* Due to a bug in Unix, fclose closes not only the stdio stream,
446      but also the file descriptor.  So we don't actually close
447      eb_desc.  */
448   if (eb_stream)
449     fclose (eb_stream); /* This also closes eb_desc */
450   if (eb_desc >= 0)
451     /* close (eb_desc); */
452
453   /* Do not try to close eb_desc again, later in the program.  */
454   eb_stream = NULL;
455   eb_desc = -1;
456
457 #if defined (LOG_FILE)
458   if (log_file) {
459     if (ferror (log_file))
460       printf ("Error writing log file.\n");
461     if (fclose (log_file) != 0)
462       printf ("Error closing log file.\n");
463   }
464 #endif
465 }
466
467 /* Terminate the open connection to the remote debugger.
468    Use this when you want to detach and do something else
469    with your gdb.  */
470 void
471 eb_detach (from_tty)
472      int from_tty;
473 {
474   pop_target();         /* calls eb_close to do the real work */
475   if (from_tty)
476     printf ("Ending remote %s debugging\n", target_shortname);
477 }
478  
479 /* Tell the remote machine to resume.  */
480
481 void
482 eb_resume (step, sig)
483      int step, sig;
484 {
485   if (step)
486     {
487       write (eb_desc, "t 1,s\n", 6);
488       /* Wait for the echo.  */
489       expect ("t 1,s\r");
490       /* Then comes a line containing the instruction we stepped to.  */
491       expect ("\n@");
492       /* Then we get the prompt.  */
493       expect_prompt ();
494
495       /* Force the next eb_wait to return a trap.  Not doing anything
496          about I/O from the target means that the user has to type
497          "continue" to see any.  This should be fixed.  */
498       need_artificial_trap = 1;
499     }
500   else
501     {
502       if (need_gi)
503         {
504           need_gi = 0;
505           write (eb_desc, "gi\n", 3);
506           
507           /* Swallow the echo of "gi".  */
508           expect ("gi\r");
509         }
510       else
511         {
512           write (eb_desc, "GR\n", 3);
513           /* Swallow the echo.  */
514           expect ("GR\r");
515         }
516     }
517 }
518
519 /* Wait until the remote machine stops, then return,
520    storing status in STATUS just as `wait' would.  */
521
522 int
523 eb_wait (status)
524      WAITTYPE *status;
525 {
526   /* Strings to look for.  '?' means match any single character.  
527      Note that with the algorithm we use, the initial character
528      of the string cannot recur in the string, or we will not
529      find some cases of the string in the input.  */
530   
531   static char bpt[] = "Invalid interrupt taken - #0x50 - ";
532   /* It would be tempting to look for "\n[__exit + 0x8]\n"
533      but that requires loading symbols with "yc i" and even if
534      we did do that we don't know that the file has symbols.  */
535   static char exitmsg[] = "\n@????????I    JMPTI     GR121,LR0";
536   char *bp = bpt;
537   char *ep = exitmsg;
538
539   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.  */
540   char swallowed[50];
541   /* Current position in swallowed.  */
542   char *swallowed_p = swallowed;
543
544   int ch;
545   int ch_handled;
546
547   int old_timeout = timeout;
548
549   WSETEXIT ((*status), 0);
550
551   if (need_artificial_trap != 0)
552     {
553       WSETSTOP ((*status), SIGTRAP);
554       need_artificial_trap--;
555       return 0;
556     }
557
558   timeout = 0;          /* Don't time out -- user program is running. */
559   while (1)
560     {
561       ch_handled = 0;
562       ch = readchar ();
563       if (ch == *bp)
564         {
565           bp++;
566           if (*bp == '\0')
567             break;
568           ch_handled = 1;
569
570           *swallowed_p++ = ch;
571         }
572       else
573         bp = bpt;
574
575       if (ch == *ep || *ep == '?')
576         {
577           ep++;
578           if (*ep == '\0')
579             break;
580
581           if (!ch_handled)
582             *swallowed_p++ = ch;
583           ch_handled = 1;
584         }
585       else
586         ep = exitmsg;
587
588       if (!ch_handled)
589         {
590           char *p;
591
592           /* Print out any characters which have been swallowed.  */
593           for (p = swallowed; p < swallowed_p; ++p)
594             putc (*p, stdout);
595           swallowed_p = swallowed;
596           
597           putc (ch, stdout);
598         }
599     }
600   expect_prompt ();
601   if (*bp== '\0')
602     WSETSTOP ((*status), SIGTRAP);
603   else
604     WSETEXIT ((*status), 0);
605   timeout = old_timeout;
606
607   return 0;
608 }
609
610 /* Return the name of register number REGNO
611    in the form input and output by EBMON.
612
613    Returns a pointer to a static buffer containing the answer.  */
614 static char *
615 get_reg_name (regno)
616      int regno;
617 {
618   static char buf[80];
619   if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
620     sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96);
621   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
622     sprintf (buf, "LR%03d", regno - LR0_REGNUM);
623   else if (regno == Q_REGNUM)
624     strcpy (buf, "SR131");
625   else if (regno >= BP_REGNUM && regno <= CR_REGNUM)
626     sprintf (buf, "SR%03d", regno - BP_REGNUM + 133);
627   else if (regno == ALU_REGNUM)
628     strcpy (buf, "SR132");
629   else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM)
630     sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128);
631   else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM)
632     sprintf (buf, "SR%03d", regno - VAB_REGNUM);
633   else if (regno == GR1_REGNUM)
634     strcpy (buf, "GR001");
635   return buf;
636 }
637
638 /* Read the remote registers into the block REGS.  */
639
640 static void
641 eb_fetch_registers ()
642 {
643   int reg_index;
644   int regnum_index;
645   char tempbuf[10];
646   int i;
647
648 #if 0
649   /* This should not be necessary, because one is supposed to read the
650      registers only when the inferior is stopped (at least with
651      ptrace() and why not make it the same for remote?).  */
652   /* ^A is the "normal character" used to make sure we are talking to EBMON
653      and not to the program being debugged.  */
654   write (eb_desc, "\001\n");
655   expect_prompt ();
656 #endif
657
658   write (eb_desc, "dw gr96,gr127\n", 14);
659   for (reg_index = 96, regnum_index = GR96_REGNUM;
660        reg_index < 128;
661        reg_index += 4, regnum_index += 4)
662     {
663       sprintf (tempbuf, "GR%03d ", reg_index);
664       expect (tempbuf);
665       get_hex_regs (4, regnum_index);
666       expect ("\n");
667     }
668
669   for (i = 0; i < 128; i += 32)
670     {
671       /* The PC has a tendency to hang if we get these
672          all in one fell swoop ("dw lr0,lr127").  */
673       sprintf (tempbuf, "dw lr%d\n", i);
674       write (eb_desc, tempbuf, strlen (tempbuf));
675       for (reg_index = i, regnum_index = LR0_REGNUM + i;
676            reg_index < i + 32;
677            reg_index += 4, regnum_index += 4)
678         {
679           sprintf (tempbuf, "LR%03d ", reg_index);
680           expect (tempbuf);
681           get_hex_regs (4, regnum_index);
682           expect ("\n");
683         }
684     }
685
686   write (eb_desc, "dw sr133,sr133\n", 15);
687   expect ("SR133          ");
688   get_hex_regs (1, BP_REGNUM);
689   expect ("\n");
690
691   write (eb_desc, "dw sr134,sr134\n", 15);
692   expect ("SR134                   ");
693   get_hex_regs (1, FC_REGNUM);
694   expect ("\n");
695
696   write (eb_desc, "dw sr135,sr135\n", 15);
697   expect ("SR135                            ");
698   get_hex_regs (1, CR_REGNUM);
699   expect ("\n");
700
701   write (eb_desc, "dw sr131,sr131\n", 15);
702   expect ("SR131                            ");
703   get_hex_regs (1, Q_REGNUM);
704   expect ("\n");
705
706   write (eb_desc, "dw sr0,sr14\n", 12);
707   for (reg_index = 0, regnum_index = VAB_REGNUM;
708        regnum_index <= LRU_REGNUM;
709        regnum_index += 4, reg_index += 4)
710     {
711       sprintf (tempbuf, "SR%03d ", reg_index);
712       expect (tempbuf);
713       get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index);
714       expect ("\n");
715     }
716
717   /* There doesn't seem to be any way to get these.  */
718   {
719     int val = -1;
720     supply_register (FPE_REGNUM, (char *) &val);
721     supply_register (INTE_REGNUM, (char *) &val);
722     supply_register (FPS_REGNUM, (char *) &val);
723     supply_register (EXO_REGNUM, (char *) &val);
724   }
725
726   write (eb_desc, "dw gr1,gr1\n", 11);
727   expect ("GR001 ");
728   get_hex_regs (1, GR1_REGNUM);
729   expect_prompt ();
730 }
731
732 /* Fetch register REGNO, or all registers if REGNO is -1.
733    Returns errno value.  */
734 void
735 eb_fetch_register (regno)
736      int regno;
737 {
738   if (regno == -1)
739     eb_fetch_registers ();
740   else
741     {
742       char *name = get_reg_name (regno);
743       fprintf (eb_stream, "dw %s,%s\n", name, name);
744       expect (name);
745       expect (" ");
746       get_hex_regs (1, regno);
747       expect_prompt ();
748     }
749   return;
750 }
751
752 /* Store the remote registers from the contents of the block REGS.  */
753
754 static void
755 eb_store_registers ()
756 {
757   int i, j;
758   fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM));
759   expect_prompt ();
760
761   for (j = 0; j < 32; j += 16)
762     {
763       fprintf (eb_stream, "s gr%d,", j + 96);
764       for (i = 0; i < 15; ++i)
765         fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i));
766       fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15));
767       expect_prompt ();
768     }
769
770   for (j = 0; j < 128; j += 16)
771     {
772       fprintf (eb_stream, "s lr%d,", j);
773       for (i = 0; i < 15; ++i)
774         fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i));
775       fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15));
776       expect_prompt ();
777     }
778
779   fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM),
780            read_register (FC_REGNUM), read_register (CR_REGNUM));
781   expect_prompt ();
782   fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM));
783   expect_prompt ();
784   fprintf (eb_stream, "s sr0,");
785   for (i = 0; i < 11; ++i)
786     fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i));
787   fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11));
788   expect_prompt ();
789 }
790
791 /* Store register REGNO, or all if REGNO == 0.
792    Return errno value.  */
793 void
794 eb_store_register (regno)
795      int regno;
796 {
797   if (regno == -1)
798     eb_store_registers ();
799   else
800     {
801       char *name = get_reg_name (regno);
802       fprintf (eb_stream, "s %s,%x\n", name, read_register (regno));
803       /* Setting GR1 changes the numbers of all the locals, so
804          invalidate the register cache.  Do this *after* calling
805          read_register, because we want read_register to return the
806          value that write_register has just stuffed into the registers
807          array, not the value of the register fetched from the
808          inferior.  */
809       if (regno == GR1_REGNUM)
810         registers_changed ();
811       expect_prompt ();
812     }
813 }
814
815 /* Get ready to modify the registers array.  On machines which store
816    individual registers, this doesn't need to do anything.  On machines
817    which store all the registers in one fell swoop, this makes sure
818    that registers contains all the registers from the program being
819    debugged.  */
820
821 void
822 eb_prepare_to_store ()
823 {
824   /* Do nothing, since we can store individual regs */
825 }
826
827
828 /* FIXME-someday!  Merge these two.  */
829 int
830 eb_xfer_inferior_memory (memaddr, myaddr, len, write, target)
831      CORE_ADDR memaddr;
832      char *myaddr;
833      int len;
834      int write;
835      struct target_ops *target;         /* ignored */
836 {
837   if (write)
838     return eb_write_inferior_memory (memaddr, myaddr, len);
839   else
840     return eb_read_inferior_memory (memaddr, myaddr, len);
841 }
842
843 void
844 eb_files_info ()
845 {
846   printf ("\tAttached to %s at %d baud and running program %s.\n",
847           dev_name, baudrate, prog_name);
848 }
849
850 /* Copy LEN bytes of data from debugger memory at MYADDR
851    to inferior's memory at MEMADDR.  Returns length moved.  */
852 int
853 eb_write_inferior_memory (memaddr, myaddr, len)
854      CORE_ADDR memaddr;
855      char *myaddr;
856      int len;
857 {
858   int i;
859
860   for (i = 0; i < len; i++)
861     {
862       if ((i % 16) == 0)
863         fprintf (eb_stream, "sb %x,", memaddr + i);
864       if ((i % 16) == 15 || i == len - 1)
865         {
866           fprintf (eb_stream, "%x\n", ((unsigned char *)myaddr)[i]);
867           expect_prompt ();
868         }
869       else
870         fprintf (eb_stream, "%x,", ((unsigned char *)myaddr)[i]);
871     }
872   return len;
873 }
874
875 /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
876    at debugger address MYADDR.  Returns length moved.  */
877 int
878 eb_read_inferior_memory(memaddr, myaddr, len)
879      CORE_ADDR memaddr;
880      char *myaddr;
881      int len;
882 {
883   int i;
884
885   /* Number of bytes read so far.  */
886   int count;
887
888   /* Starting address of this pass.  */
889   unsigned long startaddr;
890
891   /* Number of bytes to read in this pass.  */
892   int len_this_pass;
893
894   /* Note that this code works correctly if startaddr is just less
895      than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
896      thing).  That is, something like
897      eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
898      works--it never adds len to memaddr and gets 0.  */
899   /* However, something like
900      eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
901      doesn't need to work.  Detect it and give up if there's an attempt
902      to do that.  */
903   if (((memaddr - 1) + len) < memaddr) {
904     errno = EIO;
905     return 0;
906   }
907   
908   startaddr = memaddr;
909   count = 0;
910   while (count < len)
911     {
912       len_this_pass = 16;
913       if ((startaddr % 16) != 0)
914         len_this_pass -= startaddr % 16;
915       if (len_this_pass > (len - count))
916         len_this_pass = (len - count);
917
918       fprintf (eb_stream, "db %x,%x\n", startaddr,
919                (startaddr - 1) + len_this_pass);
920       expect ("\n");
921
922       /* Look for 8 hex digits.  */
923       i = 0;
924       while (1)
925         {
926           if (isxdigit (readchar ()))
927             ++i;
928           else
929             {
930               expect_prompt ();
931               error ("Hex digit expected from remote system.");
932             }
933           if (i >= 8)
934             break;
935         }
936
937       expect ("  ");
938
939       for (i = 0; i < len_this_pass; i++)
940         get_hex_byte (&myaddr[count++]);
941
942       expect_prompt ();
943
944       startaddr += len_this_pass;
945     }
946   return len;
947 }
948
949 static void
950 eb_kill (args, from_tty)
951      char *args;
952      int from_tty;
953 {
954   return;               /* Ignore attempts to kill target system */
955 }
956
957 /* Clean up when a program exits.
958
959    The program actually lives on in the remote processor's RAM, and may be
960    run again without a download.  Don't leave it full of breakpoint
961    instructions.  */
962
963 void
964 eb_mourn_inferior ()
965 {
966   remove_breakpoints ();
967   unpush_target (&eb_ops);
968   generic_mourn_inferior ();    /* Do all the proper things now */
969 }
970 /* Define the target subroutine names */
971
972 struct target_ops eb_ops = {
973         "amd-eb", "Remote serial AMD EBMON target",
974         "Use a remote computer running EBMON connected by a serial line.\n\
975 Arguments are the name of the device for the serial line,\n\
976 the speed to connect at in bits per second, and the filename of the\n\
977 executable as it exists on the remote computer.  For example,\n\
978         target amd-eb /dev/ttya 9600 demo",
979         eb_open, eb_close, 
980         0, eb_detach, eb_resume, eb_wait,
981         eb_fetch_register, eb_store_register,
982         eb_prepare_to_store,
983         eb_xfer_inferior_memory, eb_files_info,
984         0, 0,   /* Breakpoints */
985         0, 0, 0, 0, 0,  /* Terminal handling */
986         eb_kill,
987         generic_load,   /* load */
988         0, /* lookup_symbol */
989         eb_create_inferior,
990         eb_mourn_inferior,
991         0,      /* can_run */
992         0, /* notice_signals */
993         process_stratum, 0, /* next */
994         1, 1, 1, 1, 1,  /* all mem, mem, stack, regs, exec */
995         0, 0,                   /* Section pointers */
996         OPS_MAGIC,              /* Always the last thing */
997 };
998
999 void
1000 _initialize_remote_eb ()
1001 {
1002   add_target (&eb_ops);
1003 }