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