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