OSDN Git Service

Protoization.
[pf3gnuchains/pf3gnuchains4x.git] / gdb / remote-eb.c
1 /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB.
2    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.  Written by Jim Kingdon for Cygnus.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /* This is like remote.c but is for an esoteric situation--
23    having a a29k board in a PC hooked up to a unix machine with
24    a serial line, and running ctty com1 on the PC, through which
25    the unix machine can run ebmon.  Not to mention that the PC
26    has PC/NFS, so it can access the same executables that gdb can,
27    over the net in real time.  */
28
29 #include "defs.h"
30 #include "gdb_string.h"
31
32 #include "inferior.h"
33 #include "bfd.h"
34 #include "symfile.h"
35 #include "gdb_wait.h"
36 #include "value.h"
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include "terminal.h"
42 #include "target.h"
43 #include "gdbcore.h"
44
45 extern struct target_ops eb_ops;        /* Forward declaration */
46
47 static void eb_close ();
48
49 #define LOG_FILE "eb.log"
50 #if defined (LOG_FILE)
51 FILE *log_file;
52 #endif
53
54 static int timeout = 24;
55
56 /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
57    eb_open knows that we don't have a file open when the program
58    starts.  */
59 int eb_desc = -1;
60
61 /* stream which is fdopen'd from eb_desc.  Only valid when
62    eb_desc != -1.  */
63 FILE *eb_stream;
64
65 /* Read a character from the remote system, doing all the fancy
66    timeout stuff.  */
67 static int
68 readchar (void)
69 {
70   char buf;
71
72   buf = '\0';
73 #ifdef HAVE_TERMIO
74   /* termio does the timeout for us.  */
75   read (eb_desc, &buf, 1);
76 #else
77   alarm (timeout);
78   if (read (eb_desc, &buf, 1) < 0)
79     {
80       if (errno == EINTR)
81         error ("Timeout reading from remote system.");
82       else
83         perror_with_name ("remote");
84     }
85   alarm (0);
86 #endif
87
88   if (buf == '\0')
89     error ("Timeout reading from remote system.");
90 #if defined (LOG_FILE)
91   putc (buf & 0x7f, log_file);
92 #endif
93   return buf & 0x7f;
94 }
95
96 /* Keep discarding input from the remote system, until STRING is found. 
97    Let the user break out immediately.  */
98 static void
99 expect (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 (void)
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 (int ignore_space)
149 {
150   int ch;
151   while (1)
152     {
153       ch = readchar ();
154       if (ch >= '0' && ch <= '9')
155         return ch - '0';
156       else if (ch >= 'A' && ch <= 'F')
157         return ch - 'A' + 10;
158       else if (ch >= 'a' && ch <= 'f')
159         return ch - 'a' + 10;
160       else if (ch == ' ' && ignore_space)
161         ;
162       else
163         {
164           expect_prompt ();
165           error ("Invalid hex digit from remote system.");
166         }
167     }
168 }
169
170 /* Get a byte from eb_desc and put it in *BYT.  Accept any number
171    leading spaces.  */
172 static void
173 get_hex_byte (char *byt)
174 {
175   int val;
176
177   val = get_hex_digit (1) << 4;
178   val |= get_hex_digit (0);
179   *byt = val;
180 }
181
182 /* Get N 32-bit words from remote, each preceded by a space,
183    and put them in registers starting at REGNO.  */
184 static void
185 get_hex_regs (int n, int regno)
186 {
187   long val;
188   int i;
189
190   for (i = 0; i < n; i++)
191     {
192       int j;
193
194       val = 0;
195       for (j = 0; j < 8; j++)
196         val = (val << 4) + get_hex_digit (j == 0);
197       supply_register (regno++, (char *) &val);
198     }
199 }
200
201 /* Called when SIGALRM signal sent due to alarm() timeout.  */
202 #ifndef HAVE_TERMIO
203
204 #ifndef __STDC__
205 #define volatile
206 /**/
207 #endif
208 volatile int n_alarms;
209
210 void
211 eb_timer (void)
212 {
213 #if 0
214   if (kiodebug)
215     printf ("eb_timer called\n");
216 #endif
217   n_alarms++;
218 }
219 #endif
220
221 /* malloc'd name of the program on the remote system.  */
222 static char *prog_name = NULL;
223
224 /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
225    command.  "gi" is supposed to happen exactly once for each "yc".  */
226 static int need_gi = 0;
227
228 /* Number of SIGTRAPs we need to simulate.  That is, the next
229    NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
230    SIGTRAP without actually waiting for anything.  */
231
232 static int need_artificial_trap = 0;
233
234 /* This is called not only when we first attach, but also when the
235    user types "run" after having attached.  */
236 static void
237 eb_create_inferior (char *execfile, char *args, char **env)
238 {
239   int entry_pt;
240
241   if (args && *args)
242     error ("Can't pass arguments to remote EBMON process");
243
244   if (execfile == 0 || exec_bfd == 0)
245     error ("No executable file specified");
246
247   entry_pt = (int) bfd_get_start_address (exec_bfd);
248
249   {
250     /* OK, now read in the file.  Y=read, C=COFF, D=no symbols
251        0=start address, %s=filename.  */
252
253     fprintf (eb_stream, "YC D,0:%s", prog_name);
254
255     if (args != NULL)
256       fprintf (eb_stream, " %s", args);
257
258     fprintf (eb_stream, "\n");
259     fflush (eb_stream);
260
261     expect_prompt ();
262
263     need_gi = 1;
264   }
265
266 /* The "process" (board) is already stopped awaiting our commands, and
267    the program is already downloaded.  We just set its PC and go.  */
268
269   clear_proceed_status ();
270
271   /* Tell wait_for_inferior that we've started a new process.  */
272   init_wait_for_inferior ();
273
274   /* Set up the "saved terminal modes" of the inferior
275      based on what modes we are starting it with.  */
276   target_terminal_init ();
277
278   /* Install inferior's terminal modes.  */
279   target_terminal_inferior ();
280
281   /* insert_step_breakpoint ();  FIXME, do we need this?  */
282   proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);     /* Let 'er rip... */
283 }
284
285 /* Translate baud rates from integers to damn B_codes.  Unix should
286    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
287
288 #ifndef B19200
289 #define B19200 EXTA
290 #endif
291 #ifndef B38400
292 #define B38400 EXTB
293 #endif
294
295 struct
296 {
297   int rate, damn_b;
298 }
299 baudtab[] =
300 {
301   {
302     0, B0
303   }
304   ,
305   {
306     50, B50
307   }
308   ,
309   {
310     75, B75
311   }
312   ,
313   {
314     110, B110
315   }
316   ,
317   {
318     134, B134
319   }
320   ,
321   {
322     150, B150
323   }
324   ,
325   {
326     200, B200
327   }
328   ,
329   {
330     300, B300
331   }
332   ,
333   {
334     600, B600
335   }
336   ,
337   {
338     1200, B1200
339   }
340   ,
341   {
342     1800, B1800
343   }
344   ,
345   {
346     2400, B2400
347   }
348   ,
349   {
350     4800, B4800
351   }
352   ,
353   {
354     9600, B9600
355   }
356   ,
357   {
358     19200, B19200
359   }
360   ,
361   {
362     38400, B38400
363   }
364   ,
365   {
366     -1, -1
367   }
368   ,
369 };
370
371 int
372 damn_b (int rate)
373 {
374   int i;
375
376   for (i = 0; baudtab[i].rate != -1; i++)
377     if (rate == baudtab[i].rate)
378       return baudtab[i].damn_b;
379   return B38400;                /* Random */
380 }
381
382
383 /* Open a connection to a remote debugger.
384    NAME is the filename used for communication, then a space,
385    then the name of the program as we should name it to EBMON.  */
386
387 static int baudrate = 9600;
388 static char *dev_name;
389 void
390 eb_open (char *name, int from_tty)
391 {
392   TERMINAL sg;
393
394   char *p;
395
396   target_preopen (from_tty);
397
398   /* Find the first whitespace character, it separates dev_name from
399      prog_name.  */
400   if (name == 0)
401     goto erroid;
402
403   for (p = name;
404        *p != '\0' && !isspace (*p); p++)
405     ;
406   if (*p == '\0')
407   erroid:
408     error ("\
409 Please include the name of the device for the serial port,\n\
410 the baud rate, and the name of the program to run on the remote system.");
411   dev_name = alloca (p - name + 1);
412   strncpy (dev_name, name, p - name);
413   dev_name[p - name] = '\0';
414
415   /* Skip over the whitespace after dev_name */
416   for (; isspace (*p); p++)
417     /*EMPTY */ ;
418
419   if (1 != sscanf (p, "%d ", &baudrate))
420     goto erroid;
421
422   /* Skip the number and then the spaces */
423   for (; isdigit (*p); p++)
424     /*EMPTY */ ;
425   for (; isspace (*p); p++)
426     /*EMPTY */ ;
427
428   if (prog_name != NULL)
429     free (prog_name);
430   prog_name = savestring (p, strlen (p));
431
432   eb_close (0);
433
434   eb_desc = open (dev_name, O_RDWR);
435   if (eb_desc < 0)
436     perror_with_name (dev_name);
437   ioctl (eb_desc, TIOCGETP, &sg);
438 #ifdef HAVE_TERMIO
439   sg.c_cc[VMIN] = 0;            /* read with timeout.  */
440   sg.c_cc[VTIME] = timeout * 10;
441   sg.c_lflag &= ~(ICANON | ECHO);
442   sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
443 #else
444   sg.sg_ispeed = damn_b (baudrate);
445   sg.sg_ospeed = damn_b (baudrate);
446   sg.sg_flags |= RAW | ANYP;
447   sg.sg_flags &= ~ECHO;
448 #endif
449
450   ioctl (eb_desc, TIOCSETP, &sg);
451   eb_stream = fdopen (eb_desc, "r+");
452
453   push_target (&eb_ops);
454   if (from_tty)
455     printf ("Remote %s debugging %s using %s\n", target_shortname,
456             prog_name, dev_name);
457
458 #ifndef HAVE_TERMIO
459 #ifndef NO_SIGINTERRUPT
460   /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
461      the read.  */
462   if (siginterrupt (SIGALRM, 1) != 0)
463     perror ("eb_open: error in siginterrupt");
464 #endif
465
466   /* Set up read timeout timer.  */
467   if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1)
468     perror ("eb_open: error in signal");
469 #endif
470
471 #if defined (LOG_FILE)
472   log_file = fopen (LOG_FILE, "w");
473   if (log_file == NULL)
474     perror_with_name (LOG_FILE);
475 #endif
476
477   /* Hello?  Are you there?  */
478   write (eb_desc, "\n", 1);
479
480   expect_prompt ();
481 }
482
483 /* Close out all files and local state before this target loses control. */
484
485 static void
486 eb_close (int quitting)
487 {
488
489   /* Due to a bug in Unix, fclose closes not only the stdio stream,
490      but also the file descriptor.  So we don't actually close
491      eb_desc.  */
492   if (eb_stream)
493     fclose (eb_stream);         /* This also closes eb_desc */
494   if (eb_desc >= 0)
495     /* close (eb_desc); */
496
497     /* Do not try to close eb_desc again, later in the program.  */
498     eb_stream = NULL;
499   eb_desc = -1;
500
501 #if defined (LOG_FILE)
502   if (log_file)
503     {
504       if (ferror (log_file))
505         printf ("Error writing log file.\n");
506       if (fclose (log_file) != 0)
507         printf ("Error closing log file.\n");
508     }
509 #endif
510 }
511
512 /* Terminate the open connection to the remote debugger.
513    Use this when you want to detach and do something else
514    with your gdb.  */
515 void
516 eb_detach (int from_tty)
517 {
518   pop_target ();                /* calls eb_close to do the real work */
519   if (from_tty)
520     printf ("Ending remote %s debugging\n", target_shortname);
521 }
522
523 /* Tell the remote machine to resume.  */
524
525 void
526 eb_resume (int pid, int step, enum target_signal sig)
527 {
528   if (step)
529     {
530       write (eb_desc, "t 1,s\n", 6);
531       /* Wait for the echo.  */
532       expect ("t 1,s\r");
533       /* Then comes a line containing the instruction we stepped to.  */
534       expect ("\n@");
535       /* Then we get the prompt.  */
536       expect_prompt ();
537
538       /* Force the next eb_wait to return a trap.  Not doing anything
539          about I/O from the target means that the user has to type
540          "continue" to see any.  This should be fixed.  */
541       need_artificial_trap = 1;
542     }
543   else
544     {
545       if (need_gi)
546         {
547           need_gi = 0;
548           write (eb_desc, "gi\n", 3);
549
550           /* Swallow the echo of "gi".  */
551           expect ("gi\r");
552         }
553       else
554         {
555           write (eb_desc, "GR\n", 3);
556           /* Swallow the echo.  */
557           expect ("GR\r");
558         }
559     }
560 }
561
562 /* Wait until the remote machine stops, then return,
563    storing status in STATUS just as `wait' would.  */
564
565 int
566 eb_wait (struct target_waitstatus *status)
567 {
568   /* Strings to look for.  '?' means match any single character.  
569      Note that with the algorithm we use, the initial character
570      of the string cannot recur in the string, or we will not
571      find some cases of the string in the input.  */
572
573   static char bpt[] = "Invalid interrupt taken - #0x50 - ";
574   /* It would be tempting to look for "\n[__exit + 0x8]\n"
575      but that requires loading symbols with "yc i" and even if
576      we did do that we don't know that the file has symbols.  */
577   static char exitmsg[] = "\n@????????I    JMPTI     GR121,LR0";
578   char *bp = bpt;
579   char *ep = exitmsg;
580
581   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.  */
582   char swallowed[50];
583   /* Current position in swallowed.  */
584   char *swallowed_p = swallowed;
585
586   int ch;
587   int ch_handled;
588
589   int old_timeout = timeout;
590
591   status->kind = TARGET_WAITKIND_EXITED;
592   status->value.integer = 0;
593
594   if (need_artificial_trap != 0)
595     {
596       status->kind = TARGET_WAITKIND_STOPPED;
597       status->value.sig = TARGET_SIGNAL_TRAP;
598       need_artificial_trap--;
599       return 0;
600     }
601
602   timeout = 0;                  /* Don't time out -- user program is running. */
603   while (1)
604     {
605       ch_handled = 0;
606       ch = readchar ();
607       if (ch == *bp)
608         {
609           bp++;
610           if (*bp == '\0')
611             break;
612           ch_handled = 1;
613
614           *swallowed_p++ = ch;
615         }
616       else
617         bp = bpt;
618
619       if (ch == *ep || *ep == '?')
620         {
621           ep++;
622           if (*ep == '\0')
623             break;
624
625           if (!ch_handled)
626             *swallowed_p++ = ch;
627           ch_handled = 1;
628         }
629       else
630         ep = exitmsg;
631
632       if (!ch_handled)
633         {
634           char *p;
635
636           /* Print out any characters which have been swallowed.  */
637           for (p = swallowed; p < swallowed_p; ++p)
638             putc (*p, stdout);
639           swallowed_p = swallowed;
640
641           putc (ch, stdout);
642         }
643     }
644   expect_prompt ();
645   if (*bp == '\0')
646     {
647       status->kind = TARGET_WAITKIND_STOPPED;
648       status->value.sig = TARGET_SIGNAL_TRAP;
649     }
650   else
651     {
652       status->kind = TARGET_WAITKIND_EXITED;
653       status->value.integer = 0;
654     }
655   timeout = old_timeout;
656
657   return 0;
658 }
659
660 /* Return the name of register number REGNO
661    in the form input and output by EBMON.
662
663    Returns a pointer to a static buffer containing the answer.  */
664 static char *
665 get_reg_name (int regno)
666 {
667   static char buf[80];
668   if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
669     sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96);
670   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
671     sprintf (buf, "LR%03d", regno - LR0_REGNUM);
672   else if (regno == Q_REGNUM)
673     strcpy (buf, "SR131");
674   else if (regno >= BP_REGNUM && regno <= CR_REGNUM)
675     sprintf (buf, "SR%03d", regno - BP_REGNUM + 133);
676   else if (regno == ALU_REGNUM)
677     strcpy (buf, "SR132");
678   else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM)
679     sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128);
680   else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM)
681     sprintf (buf, "SR%03d", regno - VAB_REGNUM);
682   else if (regno == GR1_REGNUM)
683     strcpy (buf, "GR001");
684   return buf;
685 }
686
687 /* Read the remote registers into the block REGS.  */
688
689 static void
690 eb_fetch_registers (void)
691 {
692   int reg_index;
693   int regnum_index;
694   char tempbuf[10];
695   int i;
696
697 #if 0
698   /* This should not be necessary, because one is supposed to read the
699      registers only when the inferior is stopped (at least with
700      ptrace() and why not make it the same for remote?).  */
701   /* ^A is the "normal character" used to make sure we are talking to EBMON
702      and not to the program being debugged.  */
703   write (eb_desc, "\001\n");
704   expect_prompt ();
705 #endif
706
707   write (eb_desc, "dw gr96,gr127\n", 14);
708   for (reg_index = 96, regnum_index = GR96_REGNUM;
709        reg_index < 128;
710        reg_index += 4, regnum_index += 4)
711     {
712       sprintf (tempbuf, "GR%03d ", reg_index);
713       expect (tempbuf);
714       get_hex_regs (4, regnum_index);
715       expect ("\n");
716     }
717
718   for (i = 0; i < 128; i += 32)
719     {
720       /* The PC has a tendency to hang if we get these
721          all in one fell swoop ("dw lr0,lr127").  */
722       sprintf (tempbuf, "dw lr%d\n", i);
723       write (eb_desc, tempbuf, strlen (tempbuf));
724       for (reg_index = i, regnum_index = LR0_REGNUM + i;
725            reg_index < i + 32;
726            reg_index += 4, regnum_index += 4)
727         {
728           sprintf (tempbuf, "LR%03d ", reg_index);
729           expect (tempbuf);
730           get_hex_regs (4, regnum_index);
731           expect ("\n");
732         }
733     }
734
735   write (eb_desc, "dw sr133,sr133\n", 15);
736   expect ("SR133          ");
737   get_hex_regs (1, BP_REGNUM);
738   expect ("\n");
739
740   write (eb_desc, "dw sr134,sr134\n", 15);
741   expect ("SR134                   ");
742   get_hex_regs (1, FC_REGNUM);
743   expect ("\n");
744
745   write (eb_desc, "dw sr135,sr135\n", 15);
746   expect ("SR135                            ");
747   get_hex_regs (1, CR_REGNUM);
748   expect ("\n");
749
750   write (eb_desc, "dw sr131,sr131\n", 15);
751   expect ("SR131                            ");
752   get_hex_regs (1, Q_REGNUM);
753   expect ("\n");
754
755   write (eb_desc, "dw sr0,sr14\n", 12);
756   for (reg_index = 0, regnum_index = VAB_REGNUM;
757        regnum_index <= LRU_REGNUM;
758        regnum_index += 4, reg_index += 4)
759     {
760       sprintf (tempbuf, "SR%03d ", reg_index);
761       expect (tempbuf);
762       get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index);
763       expect ("\n");
764     }
765
766   /* There doesn't seem to be any way to get these.  */
767   {
768     int val = -1;
769     supply_register (FPE_REGNUM, (char *) &val);
770     supply_register (INTE_REGNUM, (char *) &val);
771     supply_register (FPS_REGNUM, (char *) &val);
772     supply_register (EXO_REGNUM, (char *) &val);
773   }
774
775   write (eb_desc, "dw gr1,gr1\n", 11);
776   expect ("GR001 ");
777   get_hex_regs (1, GR1_REGNUM);
778   expect_prompt ();
779 }
780
781 /* Fetch register REGNO, or all registers if REGNO is -1.
782    Returns errno value.  */
783 void
784 eb_fetch_register (int regno)
785 {
786   if (regno == -1)
787     eb_fetch_registers ();
788   else
789     {
790       char *name = get_reg_name (regno);
791       fprintf (eb_stream, "dw %s,%s\n", name, name);
792       expect (name);
793       expect (" ");
794       get_hex_regs (1, regno);
795       expect_prompt ();
796     }
797   return;
798 }
799
800 /* Store the remote registers from the contents of the block REGS.  */
801
802 static void
803 eb_store_registers (void)
804 {
805   int i, j;
806   fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM));
807   expect_prompt ();
808
809   for (j = 0; j < 32; j += 16)
810     {
811       fprintf (eb_stream, "s gr%d,", j + 96);
812       for (i = 0; i < 15; ++i)
813         fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i));
814       fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15));
815       expect_prompt ();
816     }
817
818   for (j = 0; j < 128; j += 16)
819     {
820       fprintf (eb_stream, "s lr%d,", j);
821       for (i = 0; i < 15; ++i)
822         fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i));
823       fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15));
824       expect_prompt ();
825     }
826
827   fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM),
828            read_register (FC_REGNUM), read_register (CR_REGNUM));
829   expect_prompt ();
830   fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM));
831   expect_prompt ();
832   fprintf (eb_stream, "s sr0,");
833   for (i = 0; i < 11; ++i)
834     fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i));
835   fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11));
836   expect_prompt ();
837 }
838
839 /* Store register REGNO, or all if REGNO == 0.
840    Return errno value.  */
841 void
842 eb_store_register (int regno)
843 {
844   if (regno == -1)
845     eb_store_registers ();
846   else
847     {
848       char *name = get_reg_name (regno);
849       fprintf (eb_stream, "s %s,%x\n", name, read_register (regno));
850       /* Setting GR1 changes the numbers of all the locals, so
851          invalidate the register cache.  Do this *after* calling
852          read_register, because we want read_register to return the
853          value that write_register has just stuffed into the registers
854          array, not the value of the register fetched from the
855          inferior.  */
856       if (regno == GR1_REGNUM)
857         registers_changed ();
858       expect_prompt ();
859     }
860 }
861
862 /* Get ready to modify the registers array.  On machines which store
863    individual registers, this doesn't need to do anything.  On machines
864    which store all the registers in one fell swoop, this makes sure
865    that registers contains all the registers from the program being
866    debugged.  */
867
868 void
869 eb_prepare_to_store (void)
870 {
871   /* Do nothing, since we can store individual regs */
872 }
873
874
875 /* FIXME-someday!  Merge these two.  */
876 int
877 eb_xfer_inferior_memory (memaddr, myaddr, len, write, target)
878      CORE_ADDR memaddr;
879      char *myaddr;
880      int len;
881      int write;
882      struct target_ops *target; /* ignored */
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 }