1 /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB.
2 Copyright 1990-1991 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Jim Kingdon for Cygnus.
5 This file is part of GDB.
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 1, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This is like remote.c but is for an esoteric situation--
22 having a 29k board in a PC hooked up to a unix machine with
23 a serial line, and running ctty com1 on the PC, through which
24 the unix machine can run ebmon. Not to mention that the PC
25 has PC/NFS, so it can access the same executables that gdb can,
26 over the net in real time. */
32 #include "param-no-tm.h"
43 extern void add_syms_addr_command ();
44 extern struct value
*call_function_by_hand();
46 extern struct target_ops eb_ops
; /* Forward declaration */
48 #define LOG_FILE "eb.log"
49 #if defined (LOG_FILE)
53 static int timeout
= 5;
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
60 /* stream which is fdopen'd from eb_desc. Only valid when
64 /* Read a character from the remote system, doing all the fancy
73 /* termio does the timeout for us. */
74 read (eb_desc
, &buf
, 1);
77 if (read (eb_desc
, &buf
, 1) < 0)
80 error ("Timeout reading from remote system.");
82 perror_with_name ("remote");
88 error ("Timeout reading from remote system.");
89 #if defined (LOG_FILE)
90 putc (buf
& 0x7f, log_file
);
95 /* Keep discarding input from the remote system, until STRING is found.
96 Let the user break out immediately. */
106 if (readchar() == *p
)
120 /* Keep discarding input until we see the ebmon prompt.
122 The convention for dealing with the prompt is that you
124 o *then* wait for the prompt.
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
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. */
145 /* Get a hex digit from the remote system & return its value.
146 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
148 get_hex_digit (ignore_space
)
155 if (ch
>= '0' && ch
<= '9')
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
)
166 error ("Invalid hex digit from remote system.");
171 /* Get a byte from eb_desc and put it in *BYT. Accept any number
179 val
= get_hex_digit (1) << 4;
180 val
|= get_hex_digit (0);
184 /* Get N 32-bit words from remote, each preceded by a space,
185 and put them in registers starting at REGNO. */
187 get_hex_regs (n
, regno
)
194 for (i
= 0; i
< n
; i
++)
199 for (j
= 0; j
< 8; j
++)
200 val
= (val
<< 4) + get_hex_digit (j
== 0);
201 supply_register (regno
++, &val
);
205 /* Called when SIGALRM signal sent due to alarm() timeout. */
209 #define volatile /**/
211 volatile int n_alarms
;
218 printf ("eb_timer called\n");
224 /* malloc'd name of the program on the remote system. */
225 static char *prog_name
= NULL
;
227 /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
228 command. "gi" is supposed to happen exactly once for each "yc". */
229 static int need_gi
= 0;
231 /* Number of SIGTRAPs we need to simulate. That is, the next
232 NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
233 SIGTRAP without actually waiting for anything. */
235 static int need_artificial_trap
= 0;
237 /* This is called not only when we first attach, but also when the
238 user types "run" after having attached. */
240 eb_start (inferior_args
)
243 /* OK, now read in the file. Y=read, C=COFF, D=no symbols
244 0=start address, %s=filename. */
246 fprintf (eb_stream
, "YC D,0:%s", prog_name
);
248 if (inferior_args
!= NULL
)
249 fprintf(eb_stream
, " %s", inferior_args
);
251 fprintf (eb_stream
, "\n");
259 /* Translate baud rates from integers to damn B_codes. Unix should
260 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
269 struct {int rate
, damn_b
;} baudtab
[] = {
294 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
295 if (rate
== baudtab
[i
].rate
) return baudtab
[i
].damn_b
;
296 return B38400
; /* Random */
300 /* Open a connection to a remote debugger.
301 NAME is the filename used for communication, then a space,
302 then the name of the program as we should name it to EBMON. */
304 static int baudrate
= 9600;
305 static char *dev_name
;
307 eb_open (name
, from_tty
)
315 /* Find the first whitespace character, it separates dev_name from
321 *p
!= '\0' && !isspace (*p
); p
++)
326 Please include the name of the device for the serial port,\n\
327 the baud rate, and the name of the program to run on the remote system.");
328 dev_name
= alloca (p
- name
+ 1);
329 strncpy (dev_name
, name
, p
- name
);
330 dev_name
[p
- name
] = '\0';
332 /* Skip over the whitespace after dev_name */
333 for (; isspace (*p
); p
++)
336 if (1 != sscanf (p
, "%d ", &baudrate
))
339 /* Skip the number and then the spaces */
340 for (; isdigit (*p
); p
++)
342 for (; isspace (*p
); p
++)
345 if (prog_name
!= NULL
)
347 prog_name
= savestring (p
, strlen (p
));
351 eb_desc
= open (dev_name
, O_RDWR
);
353 perror_with_name (dev_name
);
354 ioctl (eb_desc
, TIOCGETP
, &sg
);
356 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
357 sg
.c_cc
[VTIME
] = timeout
* 10;
358 sg
.c_lflag
&= ~(ICANON
| ECHO
);
359 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | damn_b (baudrate
);
361 sg
.sg_ispeed
= damn_b (baudrate
);
362 sg
.sg_ospeed
= damn_b (baudrate
);
363 sg
.sg_flags
|= RAW
| ANYP
;
364 sg
.sg_flags
&= ~ECHO
;
367 ioctl (eb_desc
, TIOCSETP
, &sg
);
368 eb_stream
= fdopen (eb_desc
, "r+");
370 push_target (&eb_ops
);
372 printf ("Remote %s debugging %s using %s\n", target_shortname
,
373 prog_name
, dev_name
);
376 #ifndef NO_SIGINTERRUPT
377 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
379 if (siginterrupt (SIGALRM
, 1) != 0)
380 perror ("eb_open: error in siginterrupt");
383 /* Set up read timeout timer. */
384 if ((void (*)) signal (SIGALRM
, eb_timer
) == (void (*)) -1)
385 perror ("eb_open: error in signal");
388 #if defined (LOG_FILE)
389 log_file
= fopen (LOG_FILE
, "w");
390 if (log_file
== NULL
)
391 perror_with_name (LOG_FILE
);
394 /* Hello? Are you there? */
395 write (eb_desc
, "\n", 1);
400 /* Close out all files and local state before this target loses control. */
407 /* Due to a bug in Unix, fclose closes not only the stdio stream,
408 but also the file descriptor. So we don't actually close
411 fclose (eb_stream
); /* This also closes eb_desc */
413 /* close (eb_desc); */
415 /* Do not try to close eb_desc again, later in the program. */
419 #if defined (LOG_FILE)
420 if (ferror (log_file
))
421 printf ("Error writing log file.\n");
422 if (fclose (log_file
) != 0)
423 printf ("Error closing log file.\n");
427 /* Terminate the open connection to the remote debugger.
428 Use this when you want to detach and do something else
434 pop_target(); /* calls eb_close to do the real work */
436 printf ("Ending remote %s debugging\n", target_shortname
);
439 /* Tell the remote machine to resume. */
442 eb_resume (step
, sig
)
447 write (eb_desc
, "t 1,s\n", 6);
448 /* Wait for the echo. */
450 /* Then comes a line containing the instruction we stepped to. */
452 /* Then we get the prompt. */
455 /* Force the next eb_wait to return a trap. Not doing anything
456 about I/O from the target means that the user has to type
457 "continue" to see any. This should be fixed. */
458 need_artificial_trap
= 1;
465 write (eb_desc
, "gi\n", 3);
467 /* Swallow the echo of "gi". */
472 write (eb_desc
, "GR\n", 3);
473 /* Swallow the echo. */
479 /* Wait until the remote machine stops, then return,
480 storing status in STATUS just as `wait' would. */
486 /* Strings to look for. '?' means match any single character.
487 Note that with the algorithm we use, the initial character
488 of the string cannot recur in the string, or we will not
489 find some cases of the string in the input. */
491 static char bpt
[] = "Invalid interrupt taken - #0x50 - ";
492 /* It would be tempting to look for "\n[__exit + 0x8]\n"
493 but that requires loading symbols with "yc i" and even if
494 we did do that we don't know that the file has symbols. */
495 static char exitmsg
[] = "\n@????????I JMPTI GR121,LR0";
499 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */
501 /* Current position in swallowed. */
502 char *swallowed_p
= swallowed
;
507 int old_timeout
= timeout
;
509 WSETEXIT ((*status
), 0);
511 if (need_artificial_trap
!= 0)
513 WSETSTOP ((*status
), SIGTRAP
);
514 need_artificial_trap
--;
518 timeout
= 0; /* Don't time out -- user program is running. */
535 if (ch
== *ep
|| *ep
== '?')
552 /* Print out any characters which have been swallowed. */
553 for (p
= swallowed
; p
< swallowed_p
; ++p
)
555 swallowed_p
= swallowed
;
562 WSETSTOP ((*status
), SIGTRAP
);
564 WSETEXIT ((*status
), 0);
565 timeout
= old_timeout
;
570 /* Return the name of register number REGNO
571 in the form input and output by EBMON.
573 Returns a pointer to a static buffer containing the answer. */
579 if (regno
>= GR96_REGNUM
&& regno
< GR96_REGNUM
+ 32)
580 sprintf (buf
, "GR%03d", regno
- GR96_REGNUM
+ 96);
581 else if (regno
>= LR0_REGNUM
&& regno
< LR0_REGNUM
+ 128)
582 sprintf (buf
, "LR%03d", regno
- LR0_REGNUM
);
583 else if (regno
== Q_REGNUM
)
584 strcpy (buf
, "SR131");
585 else if (regno
>= BP_REGNUM
&& regno
<= CR_REGNUM
)
586 sprintf (buf
, "SR%03d", regno
- BP_REGNUM
+ 133);
587 else if (regno
== ALU_REGNUM
)
588 strcpy (buf
, "SR132");
589 else if (regno
>= IPC_REGNUM
&& regno
<= IPB_REGNUM
)
590 sprintf (buf
, "SR%03d", regno
- IPC_REGNUM
+ 128);
591 else if (regno
>= VAB_REGNUM
&& regno
<= LRU_REGNUM
)
592 sprintf (buf
, "SR%03d", regno
- VAB_REGNUM
);
593 else if (regno
== GR1_REGNUM
)
594 strcpy (buf
, "GR001");
598 /* Read the remote registers into the block REGS. */
601 eb_fetch_registers ()
609 /* This should not be necessary, because one is supposed to read the
610 registers only when the inferior is stopped (at least with
611 ptrace() and why not make it the same for remote?). */
612 /* ^A is the "normal character" used to make sure we are talking to EBMON
613 and not to the program being debugged. */
614 write (eb_desc
, "\001\n");
618 write (eb_desc
, "dw gr96,gr127\n", 14);
619 for (reg_index
= 96, regnum_index
= GR96_REGNUM
;
621 reg_index
+= 4, regnum_index
+= 4)
623 sprintf (tempbuf
, "GR%03d ", reg_index
);
625 get_hex_regs (4, regnum_index
);
629 for (i
= 0; i
< 128; i
+= 32)
631 /* The PC has a tendency to hang if we get these
632 all in one fell swoop ("dw lr0,lr127"). */
633 sprintf (tempbuf
, "dw lr%d\n", i
);
634 write (eb_desc
, tempbuf
, strlen (tempbuf
));
635 for (reg_index
= i
, regnum_index
= LR0_REGNUM
+ i
;
637 reg_index
+= 4, regnum_index
+= 4)
639 sprintf (tempbuf
, "LR%03d ", reg_index
);
641 get_hex_regs (4, regnum_index
);
646 write (eb_desc
, "dw sr133,sr133\n", 15);
648 get_hex_regs (1, BP_REGNUM
);
651 write (eb_desc
, "dw sr134,sr134\n", 15);
653 get_hex_regs (1, FC_REGNUM
);
656 write (eb_desc
, "dw sr135,sr135\n", 15);
658 get_hex_regs (1, CR_REGNUM
);
661 write (eb_desc
, "dw sr131,sr131\n", 15);
663 get_hex_regs (1, Q_REGNUM
);
666 write (eb_desc
, "dw sr0,sr14\n", 12);
667 for (reg_index
= 0, regnum_index
= VAB_REGNUM
;
668 regnum_index
<= LRU_REGNUM
;
669 regnum_index
+= 4, reg_index
+= 4)
671 sprintf (tempbuf
, "SR%03d ", reg_index
);
673 get_hex_regs (reg_index
== 12 ? 3 : 4, regnum_index
);
677 /* There doesn't seem to be any way to get these. */
680 supply_register (FPE_REGNUM
, &val
);
681 supply_register (INT_REGNUM
, &val
);
682 supply_register (FPS_REGNUM
, &val
);
683 supply_register (EXO_REGNUM
, &val
);
686 write (eb_desc
, "dw gr1,gr1\n", 11);
688 get_hex_regs (1, GR1_REGNUM
);
692 /* Fetch register REGNO, or all registers if REGNO is -1.
693 Returns errno value. */
695 eb_fetch_register (regno
)
699 eb_fetch_registers ();
702 char *name
= get_reg_name (regno
);
703 fprintf (eb_stream
, "dw %s,%s\n", name
, name
);
706 get_hex_regs (1, regno
);
712 /* Store the remote registers from the contents of the block REGS. */
715 eb_store_registers ()
718 fprintf (eb_stream
, "s gr1,%x\n", read_register (GR1_REGNUM
));
721 for (j
= 0; j
< 32; j
+= 16)
723 fprintf (eb_stream
, "s gr%d,", j
+ 96);
724 for (i
= 0; i
< 15; ++i
)
725 fprintf (eb_stream
, "%x,", read_register (GR96_REGNUM
+ j
+ i
));
726 fprintf (eb_stream
, "%x\n", read_register (GR96_REGNUM
+ j
+ 15));
730 for (j
= 0; j
< 128; j
+= 16)
732 fprintf (eb_stream
, "s lr%d,", j
);
733 for (i
= 0; i
< 15; ++i
)
734 fprintf (eb_stream
, "%x,", read_register (LR0_REGNUM
+ j
+ i
));
735 fprintf (eb_stream
, "%x\n", read_register (LR0_REGNUM
+ j
+ 15));
739 fprintf (eb_stream
, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM
),
740 read_register (FC_REGNUM
), read_register (CR_REGNUM
));
742 fprintf (eb_stream
, "s sr131,%x\n", read_register (Q_REGNUM
));
744 fprintf (eb_stream
, "s sr0,");
745 for (i
= 0; i
< 11; ++i
)
746 fprintf (eb_stream
, "%x,", read_register (VAB_REGNUM
+ i
));
747 fprintf (eb_stream
, "%x\n", read_register (VAB_REGNUM
+ 11));
751 /* Store register REGNO, or all if REGNO == 0.
752 Return errno value. */
754 eb_store_register (regno
)
758 eb_store_registers ();
761 char *name
= get_reg_name (regno
);
762 fprintf (eb_stream
, "s %s,%x\n", name
, read_register (regno
));
763 /* Setting GR1 changes the numbers of all the locals, so
764 invalidate the register cache. Do this *after* calling
765 read_register, because we want read_register to return the
766 value that write_register has just stuffed into the registers
767 array, not the value of the register fetched from the
769 if (regno
== GR1_REGNUM
)
770 registers_changed ();
776 /* Get ready to modify the registers array. On machines which store
777 individual registers, this doesn't need to do anything. On machines
778 which store all the registers in one fell swoop, this makes sure
779 that registers contains all the registers from the program being
783 eb_prepare_to_store ()
785 /* Do nothing, since we can store individual regs */
788 /* FIXME! Merge these two. */
790 eb_xfer_inferior_memory (memaddr
, myaddr
, len
, write
)
797 return eb_write_inferior_memory (memaddr
, myaddr
, len
);
799 return eb_write_inferior_memory (memaddr
, myaddr
, len
);
805 printf ("\tAttached to %s at %d baud and running program %s.\n",
806 dev_name
, baudrate
, prog_name
);
809 /* Copy LEN bytes of data from debugger memory at MYADDR
810 to inferior's memory at MEMADDR. Returns errno value. */
812 eb_write_inferior_memory (memaddr
, myaddr
, len
)
819 for (i
= 0; i
< len
; i
++)
822 fprintf (eb_stream
, "sb %x,", memaddr
+ i
);
823 if ((i
% 16) == 15 || i
== len
- 1)
825 fprintf (eb_stream
, "%x\n", ((unsigned char *)myaddr
)[i
]);
829 fprintf (eb_stream
, "%x,", ((unsigned char *)myaddr
)[i
]);
834 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
835 at debugger address MYADDR. Returns errno value. */
837 eb_read_inferior_memory(memaddr
, myaddr
, len
)
844 /* Number of bytes read so far. */
847 /* Starting address of this pass. */
848 unsigned long startaddr
;
850 /* Number of bytes to read in this pass. */
853 /* Note that this code works correctly if startaddr is just less
854 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
855 thing). That is, something like
856 eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
857 works--it never adds len to memaddr and gets 0. */
858 /* However, something like
859 eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
860 doesn't need to work. Detect it and give up if there's an attempt
862 if (((memaddr
- 1) + len
) < memaddr
)
870 if ((startaddr
% 16) != 0)
871 len_this_pass
-= startaddr
% 16;
872 if (len_this_pass
> (len
- count
))
873 len_this_pass
= (len
- count
);
875 fprintf (eb_stream
, "db %x,%x\n", startaddr
,
876 (startaddr
- 1) + len_this_pass
);
879 /* Look for 8 hex digits. */
883 if (isxdigit (readchar ()))
888 error ("Hex digit expected from remote system.");
896 for (i
= 0; i
< len_this_pass
; i
++)
897 get_hex_byte (&myaddr
[count
++]);
901 startaddr
+= len_this_pass
;
906 /* Define the target subroutine names */
908 struct target_ops eb_ops
= {
909 "amd-eb", "Remote serial AMD EBMON target",
911 0, eb_detach
, eb_resume
, eb_wait
,
912 eb_fetch_register
, eb_store_register
,
913 eb_prepare_to_store
, 0, 0, /* conv_to, conv_from */
914 eb_xfer_inferior_memory
, eb_files_info
,
915 0, 0, /* Breakpoints */
916 0, 0, 0, 0, 0, /* Terminal handling */
918 0, add_syms_addr_command
, /* load */
919 call_function_by_hand
,
920 0, /* lookup_symbol */
921 0, /* create_inferior FIXME, eb_start here or something? */
922 0, /* mourn_inferior FIXME */
923 process_stratum
, 0, /* next */
924 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
925 OPS_MAGIC
, /* Always the last thing */
929 _initialize_remote_eb ()
931 add_target (&eb_ops
);
This page took 0.078227 seconds and 5 git commands to generate.