cb79d15d88ecec53808e6a2662fe27781ee03528
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.
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 2 of the License, or
10 (at your option) any later version.
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; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This is like remote.c but is for an esoteric situation--
22 having a a29k board in a PC hooked up to a unix machine with
23 a serial line, and running ctty com1 on the PC, through which
24 the unix machine can run ebmon. Not to mention that the PC
25 has PC/NFS, so it can access the same executables that gdb can,
26 over the net in real time. */
28 #define TM_FILE_OVERRIDE
31 #include "a29k/tm-a29k.h"
45 extern struct target_ops eb_ops
; /* Forward declaration */
47 static void eb_close();
49 #define LOG_FILE "eb.log"
50 #if defined (LOG_FILE)
54 static int timeout
= 24;
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
61 /* stream which is fdopen'd from eb_desc. Only valid when
65 /* Read a character from the remote system, doing all the fancy
74 /* termio does the timeout for us. */
75 read (eb_desc
, &buf
, 1);
78 if (read (eb_desc
, &buf
, 1) < 0)
81 error ("Timeout reading from remote system.");
83 perror_with_name ("remote");
89 error ("Timeout reading from remote system.");
90 #if defined (LOG_FILE)
91 putc (buf
& 0x7f, log_file
);
96 /* Keep discarding input from the remote system, until STRING is found.
97 Let the user break out immediately. */
107 if (readchar() == *p
)
121 /* Keep discarding input until we see the ebmon prompt.
123 The convention for dealing with the prompt is that you
125 o *then* wait for the prompt.
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
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. */
146 /* Get a hex digit from the remote system & return its value.
147 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
149 get_hex_digit (ignore_space
)
156 if (ch
>= '0' && ch
<= '9')
158 else if (ch
>= 'A' && ch
<= 'F')
159 return ch
- 'A' + 10;
160 else if (ch
>= 'a' && ch
<= 'f')
161 return ch
- 'a' + 10;
162 else if (ch
== ' ' && ignore_space
)
167 error ("Invalid hex digit from remote system.");
172 /* Get a byte from eb_desc and put it in *BYT. Accept any number
180 val
= get_hex_digit (1) << 4;
181 val
|= get_hex_digit (0);
185 /* Get N 32-bit words from remote, each preceded by a space,
186 and put them in registers starting at REGNO. */
188 get_hex_regs (n
, regno
)
195 for (i
= 0; i
< n
; i
++)
200 for (j
= 0; j
< 8; j
++)
201 val
= (val
<< 4) + get_hex_digit (j
== 0);
202 supply_register (regno
++, &val
);
206 /* Called when SIGALRM signal sent due to alarm() timeout. */
210 #define volatile /**/
212 volatile int n_alarms
;
219 printf ("eb_timer called\n");
225 /* malloc'd name of the program on the remote system. */
226 static char *prog_name
= NULL
;
228 /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
229 command. "gi" is supposed to happen exactly once for each "yc". */
230 static int need_gi
= 0;
232 /* Number of SIGTRAPs we need to simulate. That is, the next
233 NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
234 SIGTRAP without actually waiting for anything. */
236 static int need_artificial_trap
= 0;
238 /* This is called not only when we first attach, but also when the
239 user types "run" after having attached. */
241 eb_create_inferior (execfile
, args
, env
)
249 error ("Can't pass arguments to remote EBMON process");
251 if (execfile
== 0 || exec_bfd
== 0)
252 error ("No exec file specified");
254 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
256 #ifdef CREATE_INFERIOR_HOOK
257 CREATE_INFERIOR_HOOK (0); /* No process-ID */
261 /* OK, now read in the file. Y=read, C=COFF, D=no symbols
262 0=start address, %s=filename. */
264 fprintf (eb_stream
, "YC D,0:%s", prog_name
);
267 fprintf(eb_stream
, " %s", args
);
269 fprintf (eb_stream
, "\n");
277 /* The "process" (board) is already stopped awaiting our commands, and
278 the program is already downloaded. We just set its PC and go. */
280 clear_proceed_status ();
282 /* Tell wait_for_inferior that we've started a new process. */
283 init_wait_for_inferior ();
285 /* Set up the "saved terminal modes" of the inferior
286 based on what modes we are starting it with. */
287 target_terminal_init ();
289 /* Install inferior's terminal modes. */
290 target_terminal_inferior ();
292 /* insert_step_breakpoint (); FIXME, do we need this? */
293 proceed ((CORE_ADDR
)entry_pt
, -1, 0); /* Let 'er rip... */
296 /* Translate baud rates from integers to damn B_codes. Unix should
297 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
306 struct {int rate
, damn_b
;} baudtab
[] = {
331 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
332 if (rate
== baudtab
[i
].rate
) return baudtab
[i
].damn_b
;
333 return B38400
; /* Random */
337 /* Open a connection to a remote debugger.
338 NAME is the filename used for communication, then a space,
339 then the name of the program as we should name it to EBMON. */
341 static int baudrate
= 9600;
342 static char *dev_name
;
344 eb_open (name
, from_tty
)
352 target_preopen (from_tty
);
354 /* Find the first whitespace character, it separates dev_name from
360 *p
!= '\0' && !isspace (*p
); p
++)
365 Please include the name of the device for the serial port,\n\
366 the baud rate, and the name of the program to run on the remote system.");
367 dev_name
= alloca (p
- name
+ 1);
368 strncpy (dev_name
, name
, p
- name
);
369 dev_name
[p
- name
] = '\0';
371 /* Skip over the whitespace after dev_name */
372 for (; isspace (*p
); p
++)
375 if (1 != sscanf (p
, "%d ", &baudrate
))
378 /* Skip the number and then the spaces */
379 for (; isdigit (*p
); p
++)
381 for (; isspace (*p
); p
++)
384 if (prog_name
!= NULL
)
386 prog_name
= savestring (p
, strlen (p
));
390 eb_desc
= open (dev_name
, O_RDWR
);
392 perror_with_name (dev_name
);
393 ioctl (eb_desc
, TIOCGETP
, &sg
);
395 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
396 sg
.c_cc
[VTIME
] = timeout
* 10;
397 sg
.c_lflag
&= ~(ICANON
| ECHO
);
398 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | damn_b (baudrate
);
400 sg
.sg_ispeed
= damn_b (baudrate
);
401 sg
.sg_ospeed
= damn_b (baudrate
);
402 sg
.sg_flags
|= RAW
| ANYP
;
403 sg
.sg_flags
&= ~ECHO
;
406 ioctl (eb_desc
, TIOCSETP
, &sg
);
407 eb_stream
= fdopen (eb_desc
, "r+");
409 push_target (&eb_ops
);
411 printf ("Remote %s debugging %s using %s\n", target_shortname
,
412 prog_name
, dev_name
);
415 #ifndef NO_SIGINTERRUPT
416 /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
418 if (siginterrupt (SIGALRM
, 1) != 0)
419 perror ("eb_open: error in siginterrupt");
422 /* Set up read timeout timer. */
423 if ((void (*)) signal (SIGALRM
, eb_timer
) == (void (*)) -1)
424 perror ("eb_open: error in signal");
427 #if defined (LOG_FILE)
428 log_file
= fopen (LOG_FILE
, "w");
429 if (log_file
== NULL
)
430 perror_with_name (LOG_FILE
);
433 /* Hello? Are you there? */
434 write (eb_desc
, "\n", 1);
439 /* Close out all files and local state before this target loses control. */
446 /* Due to a bug in Unix, fclose closes not only the stdio stream,
447 but also the file descriptor. So we don't actually close
450 fclose (eb_stream
); /* This also closes eb_desc */
452 /* close (eb_desc); */
454 /* Do not try to close eb_desc again, later in the program. */
458 #if defined (LOG_FILE)
460 if (ferror (log_file
))
461 printf ("Error writing log file.\n");
462 if (fclose (log_file
) != 0)
463 printf ("Error closing log file.\n");
468 /* Terminate the open connection to the remote debugger.
469 Use this when you want to detach and do something else
475 pop_target(); /* calls eb_close to do the real work */
477 printf ("Ending remote %s debugging\n", target_shortname
);
480 /* Tell the remote machine to resume. */
483 eb_resume (step
, sig
)
488 write (eb_desc
, "t 1,s\n", 6);
489 /* Wait for the echo. */
491 /* Then comes a line containing the instruction we stepped to. */
493 /* Then we get the prompt. */
496 /* Force the next eb_wait to return a trap. Not doing anything
497 about I/O from the target means that the user has to type
498 "continue" to see any. This should be fixed. */
499 need_artificial_trap
= 1;
506 write (eb_desc
, "gi\n", 3);
508 /* Swallow the echo of "gi". */
513 write (eb_desc
, "GR\n", 3);
514 /* Swallow the echo. */
520 /* Wait until the remote machine stops, then return,
521 storing status in STATUS just as `wait' would. */
527 /* Strings to look for. '?' means match any single character.
528 Note that with the algorithm we use, the initial character
529 of the string cannot recur in the string, or we will not
530 find some cases of the string in the input. */
532 static char bpt
[] = "Invalid interrupt taken - #0x50 - ";
533 /* It would be tempting to look for "\n[__exit + 0x8]\n"
534 but that requires loading symbols with "yc i" and even if
535 we did do that we don't know that the file has symbols. */
536 static char exitmsg
[] = "\n@????????I JMPTI GR121,LR0";
540 /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */
542 /* Current position in swallowed. */
543 char *swallowed_p
= swallowed
;
548 int old_timeout
= timeout
;
550 WSETEXIT ((*status
), 0);
552 if (need_artificial_trap
!= 0)
554 WSETSTOP ((*status
), SIGTRAP
);
555 need_artificial_trap
--;
559 timeout
= 0; /* Don't time out -- user program is running. */
576 if (ch
== *ep
|| *ep
== '?')
593 /* Print out any characters which have been swallowed. */
594 for (p
= swallowed
; p
< swallowed_p
; ++p
)
596 swallowed_p
= swallowed
;
603 WSETSTOP ((*status
), SIGTRAP
);
605 WSETEXIT ((*status
), 0);
606 timeout
= old_timeout
;
611 /* Return the name of register number REGNO
612 in the form input and output by EBMON.
614 Returns a pointer to a static buffer containing the answer. */
620 if (regno
>= GR96_REGNUM
&& regno
< GR96_REGNUM
+ 32)
621 sprintf (buf
, "GR%03d", regno
- GR96_REGNUM
+ 96);
622 else if (regno
>= LR0_REGNUM
&& regno
< LR0_REGNUM
+ 128)
623 sprintf (buf
, "LR%03d", regno
- LR0_REGNUM
);
624 else if (regno
== Q_REGNUM
)
625 strcpy (buf
, "SR131");
626 else if (regno
>= BP_REGNUM
&& regno
<= CR_REGNUM
)
627 sprintf (buf
, "SR%03d", regno
- BP_REGNUM
+ 133);
628 else if (regno
== ALU_REGNUM
)
629 strcpy (buf
, "SR132");
630 else if (regno
>= IPC_REGNUM
&& regno
<= IPB_REGNUM
)
631 sprintf (buf
, "SR%03d", regno
- IPC_REGNUM
+ 128);
632 else if (regno
>= VAB_REGNUM
&& regno
<= LRU_REGNUM
)
633 sprintf (buf
, "SR%03d", regno
- VAB_REGNUM
);
634 else if (regno
== GR1_REGNUM
)
635 strcpy (buf
, "GR001");
639 /* Read the remote registers into the block REGS. */
642 eb_fetch_registers ()
650 /* This should not be necessary, because one is supposed to read the
651 registers only when the inferior is stopped (at least with
652 ptrace() and why not make it the same for remote?). */
653 /* ^A is the "normal character" used to make sure we are talking to EBMON
654 and not to the program being debugged. */
655 write (eb_desc
, "\001\n");
659 write (eb_desc
, "dw gr96,gr127\n", 14);
660 for (reg_index
= 96, regnum_index
= GR96_REGNUM
;
662 reg_index
+= 4, regnum_index
+= 4)
664 sprintf (tempbuf
, "GR%03d ", reg_index
);
666 get_hex_regs (4, regnum_index
);
670 for (i
= 0; i
< 128; i
+= 32)
672 /* The PC has a tendency to hang if we get these
673 all in one fell swoop ("dw lr0,lr127"). */
674 sprintf (tempbuf
, "dw lr%d\n", i
);
675 write (eb_desc
, tempbuf
, strlen (tempbuf
));
676 for (reg_index
= i
, regnum_index
= LR0_REGNUM
+ i
;
678 reg_index
+= 4, regnum_index
+= 4)
680 sprintf (tempbuf
, "LR%03d ", reg_index
);
682 get_hex_regs (4, regnum_index
);
687 write (eb_desc
, "dw sr133,sr133\n", 15);
689 get_hex_regs (1, BP_REGNUM
);
692 write (eb_desc
, "dw sr134,sr134\n", 15);
694 get_hex_regs (1, FC_REGNUM
);
697 write (eb_desc
, "dw sr135,sr135\n", 15);
699 get_hex_regs (1, CR_REGNUM
);
702 write (eb_desc
, "dw sr131,sr131\n", 15);
704 get_hex_regs (1, Q_REGNUM
);
707 write (eb_desc
, "dw sr0,sr14\n", 12);
708 for (reg_index
= 0, regnum_index
= VAB_REGNUM
;
709 regnum_index
<= LRU_REGNUM
;
710 regnum_index
+= 4, reg_index
+= 4)
712 sprintf (tempbuf
, "SR%03d ", reg_index
);
714 get_hex_regs (reg_index
== 12 ? 3 : 4, regnum_index
);
718 /* There doesn't seem to be any way to get these. */
721 supply_register (FPE_REGNUM
, &val
);
722 supply_register (INTE_REGNUM
, &val
);
723 supply_register (FPS_REGNUM
, &val
);
724 supply_register (EXO_REGNUM
, &val
);
727 write (eb_desc
, "dw gr1,gr1\n", 11);
729 get_hex_regs (1, GR1_REGNUM
);
733 /* Fetch register REGNO, or all registers if REGNO is -1.
734 Returns errno value. */
736 eb_fetch_register (regno
)
740 eb_fetch_registers ();
743 char *name
= get_reg_name (regno
);
744 fprintf (eb_stream
, "dw %s,%s\n", name
, name
);
747 get_hex_regs (1, regno
);
753 /* Store the remote registers from the contents of the block REGS. */
756 eb_store_registers ()
759 fprintf (eb_stream
, "s gr1,%x\n", read_register (GR1_REGNUM
));
762 for (j
= 0; j
< 32; j
+= 16)
764 fprintf (eb_stream
, "s gr%d,", j
+ 96);
765 for (i
= 0; i
< 15; ++i
)
766 fprintf (eb_stream
, "%x,", read_register (GR96_REGNUM
+ j
+ i
));
767 fprintf (eb_stream
, "%x\n", read_register (GR96_REGNUM
+ j
+ 15));
771 for (j
= 0; j
< 128; j
+= 16)
773 fprintf (eb_stream
, "s lr%d,", j
);
774 for (i
= 0; i
< 15; ++i
)
775 fprintf (eb_stream
, "%x,", read_register (LR0_REGNUM
+ j
+ i
));
776 fprintf (eb_stream
, "%x\n", read_register (LR0_REGNUM
+ j
+ 15));
780 fprintf (eb_stream
, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM
),
781 read_register (FC_REGNUM
), read_register (CR_REGNUM
));
783 fprintf (eb_stream
, "s sr131,%x\n", read_register (Q_REGNUM
));
785 fprintf (eb_stream
, "s sr0,");
786 for (i
= 0; i
< 11; ++i
)
787 fprintf (eb_stream
, "%x,", read_register (VAB_REGNUM
+ i
));
788 fprintf (eb_stream
, "%x\n", read_register (VAB_REGNUM
+ 11));
792 /* Store register REGNO, or all if REGNO == 0.
793 Return errno value. */
795 eb_store_register (regno
)
799 eb_store_registers ();
802 char *name
= get_reg_name (regno
);
803 fprintf (eb_stream
, "s %s,%x\n", name
, read_register (regno
));
804 /* Setting GR1 changes the numbers of all the locals, so
805 invalidate the register cache. Do this *after* calling
806 read_register, because we want read_register to return the
807 value that write_register has just stuffed into the registers
808 array, not the value of the register fetched from the
810 if (regno
== GR1_REGNUM
)
811 registers_changed ();
816 /* Get ready to modify the registers array. On machines which store
817 individual registers, this doesn't need to do anything. On machines
818 which store all the registers in one fell swoop, this makes sure
819 that registers contains all the registers from the program being
823 eb_prepare_to_store ()
825 /* Do nothing, since we can store individual regs */
829 /* FIXME-someday! Merge these two. */
831 eb_xfer_inferior_memory (memaddr
, myaddr
, len
, write
, target
)
836 struct target_ops
*target
; /* ignored */
839 return eb_write_inferior_memory (memaddr
, myaddr
, len
);
841 return eb_read_inferior_memory (memaddr
, myaddr
, len
);
847 printf ("\tAttached to %s at %d baud and running program %s.\n",
848 dev_name
, baudrate
, prog_name
);
851 /* Copy LEN bytes of data from debugger memory at MYADDR
852 to inferior's memory at MEMADDR. Returns length moved. */
854 eb_write_inferior_memory (memaddr
, myaddr
, len
)
861 for (i
= 0; i
< len
; i
++)
864 fprintf (eb_stream
, "sb %x,", memaddr
+ i
);
865 if ((i
% 16) == 15 || i
== len
- 1)
867 fprintf (eb_stream
, "%x\n", ((unsigned char *)myaddr
)[i
]);
871 fprintf (eb_stream
, "%x,", ((unsigned char *)myaddr
)[i
]);
876 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
877 at debugger address MYADDR. Returns length moved. */
879 eb_read_inferior_memory(memaddr
, myaddr
, len
)
886 /* Number of bytes read so far. */
889 /* Starting address of this pass. */
890 unsigned long startaddr
;
892 /* Number of bytes to read in this pass. */
895 /* Note that this code works correctly if startaddr is just less
896 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
897 thing). That is, something like
898 eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
899 works--it never adds len to memaddr and gets 0. */
900 /* However, something like
901 eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
902 doesn't need to work. Detect it and give up if there's an attempt
904 if (((memaddr
- 1) + len
) < memaddr
) {
914 if ((startaddr
% 16) != 0)
915 len_this_pass
-= startaddr
% 16;
916 if (len_this_pass
> (len
- count
))
917 len_this_pass
= (len
- count
);
919 fprintf (eb_stream
, "db %x,%x\n", startaddr
,
920 (startaddr
- 1) + len_this_pass
);
923 /* Look for 8 hex digits. */
927 if (isxdigit (readchar ()))
932 error ("Hex digit expected from remote system.");
940 for (i
= 0; i
< len_this_pass
; i
++)
941 get_hex_byte (&myaddr
[count
++]);
945 startaddr
+= len_this_pass
;
951 eb_kill (args
, from_tty
)
955 return; /* Ignore attempts to kill target system */
958 /* Clean up when a program exits.
960 The program actually lives on in the remote processor's RAM, and may be
961 run again without a download. Don't leave it full of breakpoint
967 remove_breakpoints ();
968 unpush_target (&eb_ops
);
969 generic_mourn_inferior (); /* Do all the proper things now */
971 /* Define the target subroutine names */
973 struct target_ops eb_ops
= {
974 "amd-eb", "Remote serial AMD EBMON target",
975 "Use a remote computer running EBMON connected by a serial line.\n\
976 Arguments are the name of the device for the serial line,\n\
977 the speed to connect at in bits per second, and the filename of the\n\
978 executable as it exists on the remote computer. For example,\n\
979 target amd-eb /dev/ttya 9600 demo",
981 0, eb_detach
, eb_resume
, eb_wait
,
982 eb_fetch_register
, eb_store_register
,
984 eb_xfer_inferior_memory
, eb_files_info
,
985 0, 0, /* Breakpoints */
986 0, 0, 0, 0, 0, /* Terminal handling */
988 generic_load
, /* load */
989 0, /* lookup_symbol */
993 0, /* notice_signals */
994 process_stratum
, 0, /* next */
995 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
996 0, 0, /* Section pointers */
997 OPS_MAGIC
, /* Always the last thing */
1001 _initialize_remote_eb ()
1003 add_target (&eb_ops
);
This page took 0.049283 seconds and 4 git commands to generate.