1 /* Memory-access and commands for remote es1800 processes, for GDB.
2 Copyright (C) 1988, 1992 Free Software Foundation, Inc.
4 This file is added to GDB to make it possible to do debugging via an
5 ES-1800 emulator. The code was originally written by Johan Holmberg
6 TT/SJ Ericsson Telecom AB and later modified by Johan Henriksson
7 TT/SJ. It was modified for gdb 4.0 by TX/DK Jan Nordenand by TX/DKG
10 This file is part of GDB.
12 GDB is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 1, or (at your option)
17 GDB is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GDB; see the file COPYING. If not, write to
24 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* Emulator communication protocol.
28 All values are encoded in ascii hex digits.
35 - 0 - - 1 - - 2 - - 3 - - 4 - - 5 - -- 6 - - 7 -
36 D = XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
37 A = XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
38 PC = XXXXXX SSP = XXXXXX USP = XXXXXX SR = XXXXXXXX
40 Each byte of register data is described by two hex digits.
62 Each byte of register data is described by two hex digits.
68 AA..AA is address, XXXXXXX is the contents
73 AA..AA is address, XXXXXXXX is data
79 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
85 AA..AA is address to resume. If AA..AA is omitted, resume at same address.
95 #include <sys/ioctl.h>
104 #include "inferior.h"
108 #include "remote-utils.h"
112 /* Prototypes for local functions */
115 es1800_child_detach PARAMS ((char *, int));
118 es1800_child_open PARAMS ((char *, int));
121 es1800_transparent PARAMS ((char *, int));
124 es1800_create_inferior PARAMS ((char *, char *, char **));
127 es1800_load PARAMS ((char *, int));
130 es1800_kill PARAMS ((void));
133 verify_break PARAMS ((int));
136 es1800_remove_breakpoint PARAMS ((CORE_ADDR, char *));
139 es1800_insert_breakpoint PARAMS ((CORE_ADDR, char *));
142 es1800_files_info PARAMS ((struct target_ops *));
145 es1800_xfer_inferior_memory PARAMS ((CORE_ADDR, char *, int, int,
146 struct target_ops *));
149 es1800_prepare_to_store PARAMS ((void));
151 static int es1800_wait PARAMS ((int, struct target_waitstatus *));
153 static void es1800_resume PARAMS ((int, int, enum target_signal));
156 es1800_detach PARAMS ((char *, int));
159 es1800_attach PARAMS ((char *, int));
162 damn_b PARAMS ((char *));
165 es1800_open PARAMS ((char *, int));
168 es1800_timer PARAMS ((void));
171 es1800_reset PARAMS ((char *));
174 es1800_request_quit PARAMS ((void));
177 readchar PARAMS ((void));
180 expect PARAMS ((char *, int));
183 expect_prompt PARAMS ((void));
186 download PARAMS ((FILE *, int, int));
190 bfd_copy PARAMS ((bfd *, bfd *));
194 get_break_addr PARAMS ((int, CORE_ADDR *));
197 fromhex PARAMS ((int));
200 tohex PARAMS ((int));
203 es1800_close PARAMS ((int));
206 es1800_fetch_registers PARAMS ((void));
209 es1800_fetch_register PARAMS ((int));
212 es1800_store_register PARAMS ((int));
215 es1800_read_bytes PARAMS ((CORE_ADDR, char *, int));
218 es1800_write_bytes PARAMS ((CORE_ADDR, char *, int));
221 send_with_reply PARAMS ((char *, char *, int));
224 send_command PARAMS ((char *));
227 send PARAMS ((char *));
230 getmessage PARAMS ((char *, int));
233 es1800_mourn_inferior PARAMS ((void));
236 es1800_create_break_insn PARAMS ((char *, int));
239 es1800_init_break PARAMS ((char *, int));
241 /* Local variables */
243 /* FIXME: Convert this to use "set remotedebug" instead. */
244 #define LOG_FILE "es1800.log"
245 #if defined (LOG_FILE)
246 static FILE *log_file;
249 extern struct target_ops es1800_ops; /* Forward decl */
250 extern struct target_ops es1800_child_ops; /* Forward decl */
253 static int timeout = 100;
254 static char *savename; /* Name of i/o device used */
255 static serial_ttystate es1800_saved_ttystate;
256 static int es1800_fc_save; /* Save fcntl state */
258 /* indicates that the emulator uses 32-bit data-adress (68020-mode)
259 instead of 24-bit (68000 -mode) */
263 #define MODE (m68020 ? "M68020" : "M68000" )
264 #define ES1800_BREAK_VEC (0xf)
266 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
267 es1800_open knows that we don't have a file open when the program
270 static serial_t es1800_desc = NULL;
273 #define HDRLEN sizeof("@.BAAAAAAAA=$VV\r")
275 /* Maximum number of bytes to read/write at once. The value here
276 is chosen to fill up a packet. */
278 #define MAXBUFBYTES ((PBUFSIZ-150)*16/75 )
280 static int es1800_break_vec = 0;
281 static char es1800_break_insn[2];
282 static long es1800_break_address;
283 static void (*old_sigint)(); /* Old signal-handler for sigint */
284 static jmp_buf interrupt;
286 /* Local signalhandler to allow breaking tranfers or program run.
287 Rely on global variables: old_sigint(), interrupt */
290 es1800_request_quit ()
292 /* restore original signalhandler */
293 signal (SIGINT, old_sigint);
294 longjmp (interrupt, 1);
299 Sending reset character(octal 32) to emulator.
300 quit - return to '(esgdb)' prompt or continue */
310 printf ("\nResetting emulator... ");
312 strcpy (buf, "\032");
322 /* Open a connection to a remote debugger and push the new target
323 onto the stack. Check if the emulator is responding and find out
324 what kind of processor the emulator is connected to.
325 Initiate the breakpoint handling in the emulator.
327 name - the filename used for communication (ex. '/dev/tta')
328 from_tty - says whether to be verbose or not */
331 es1800_open (name, from_tty)
341 if (!name) /* no device name given in target command */
343 error_no_arg ("serial port device name");
346 target_preopen (from_tty);
349 /* open the device and configure it for communication */
353 es1800_desc = SERIAL_OPEN (name);
354 if (es1800_desc == NULL)
356 perror_with_name (name);
358 savename = savestring (name, strlen (name));
360 es1800_saved_ttystate = SERIAL_GET_TTY_STATE (es1800_desc);
362 if ((fcflag = fcntl (es1800_desc->fd, F_GETFL, 0)) == -1)
364 perror_with_name ("fcntl serial");
366 es1800_fc_save = fcflag;
368 fcflag = (fcflag & (FREAD | FWRITE)); /* mask out any funny stuff */
369 if (fcntl (es1800_desc->fd, F_SETFL, fcflag) == -1)
371 perror_with_name ("fcntl serial");
376 if (SERIAL_SETBAUDRATE (es1800_desc, baud_rate))
378 SERIAL_CLOSE (es1800_desc);
379 perror_with_name (name);
383 SERIAL_RAW (es1800_desc);
385 /* If there is something sitting in the buffer we might take it as a
386 response to a command, which would be bad. */
387 SERIAL_FLUSH_INPUT (es1800_desc);
389 #endif /* DEBUG_STDIN */
391 push_target (&es1800_ops); /* Switch to using remote target now */
394 printf ("Remote ES1800 debugging using %s\n", name);
397 #if defined (LOG_FILE)
399 log_file = fopen (LOG_FILE, "w");
400 if (log_file == NULL)
402 perror_with_name (LOG_FILE);
405 #endif /* LOG_FILE */
407 /* Hello? Are you there?, also check mode */
409 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
410 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
414 getmessage (buf, sizeof (buf)); /* send reset character */
418 printf ("Checking mode.... ");
420 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
422 /* What kind of processor am i talking to ?*/
424 while (*p++ != '\n') {;}
425 while (*p++ != '\n') {;}
426 while (*p++ != '\n') {;}
427 for (i = 0; i < 20; i++, p++) {;}
428 m68020 = !strncmp (p, "68020", 5);
431 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE, p[0], p[1], p[2],
435 /* if no init_break statement is present in .gdb file we have to check
436 whether to download a breakpoint routine or not */
439 if ((es1800_break_vec == 0) || (verify_break (es1800_break_vec) != 0)
440 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
442 CORE_ADDR memaddress;
443 printf ("Give the start address of the breakpoint routine: ");
444 scanf ("%li", &memaddress);
445 es1800_init_break ((es1800_break_vec ? es1800_break_vec :
446 ES1800_BREAK_VEC), memaddress);
452 /* Close out all files and local state before this target loses control.
453 quitting - are we quitting gdb now? */
456 es1800_close (quitting)
459 if (es1800_desc != NULL)
461 printf ("\nClosing connection to emulator...\n");
462 if (SERIAL_SET_TTY_STATE (es1800_desc, es1800_saved_ttystate) < 0)
463 print_sys_errmsg ("warning: unable to restore tty state", errno);
464 fcntl (es1800_desc->fd, F_SETFL, es1800_fc_save);
465 SERIAL_CLOSE (es1800_desc);
468 if (savename != NULL)
474 #if defined (LOG_FILE)
476 if (log_file != NULL)
478 if (ferror (log_file))
480 printf ("Error writing log file.\n");
482 if (fclose (log_file) != 0)
484 printf ("Error closing log file.\n");
489 #endif /* LOG_FILE */
493 /* Attaches to a process on the target side
494 proc_id - the id of the process to be attached.
495 from_tty - says whether to be verbose or not */
498 es1800_attach (args, from_tty)
502 error ("Cannot attach to pid %s, this feature is not implemented yet.",
507 /* Takes a program previously attached to and detaches it.
508 We better not have left any breakpoints
509 in the program or it'll die when it hits one.
510 Close the open connection to the remote debugger.
511 Use this when you want to detach and do something else
514 args - arguments given to the 'detach' command
515 from_tty - says whether to be verbose or not */
518 es1800_detach (args, from_tty)
524 error ("Argument given to \"detach\" when remotely debugging.");
529 printf ("Ending es1800 remote debugging.\n");
534 /* Tell the remote machine to resume.
535 step - single-step or run free
536 siggnal - the signal value to be given to the target (0 = no signal) */
539 es1800_resume (pid, step, siggnal)
542 enum target_signal siggnal;
548 error ("Can't send signals to a remote system.");
552 strcpy (buf,"STP\r");
557 send_command ("RBK");
561 /* Wait until the remote machine stops, then return,
562 storing status in STATUS just as `wait' would.
566 es1800_wait (pid, status)
568 struct target_waitstatus *status;
570 unsigned char buf[PBUFSIZ];
571 int old_timeout = timeout;
573 status->kind = TARGET_WAITKIND_EXITED;
574 status->value.integer = 0;
576 timeout = 0; /* Don't time out -- user program is running. */
577 if (!setjmp (interrupt))
579 old_sigint = signal (SIGINT, es1800_request_quit);
582 getmessage (buf, sizeof(buf));
583 if (strncmp ( buf, "\r\n* BREAK *", 11) == 0)
585 status->kind = TARGET_WAITKIND_STOPPED;
586 status->value.sig = TARGET_SIGNAL_TRAP;
587 send_command ("STP"); /* Restore stack and PC and such */
590 send_command ("STP");
594 if (strncmp (buf, "STP\r\n ", 6) == 0)
596 status->kind = TARGET_WAITKIND_STOPPED;
597 status->value.sig = TARGET_SIGNAL_TRAP;
600 if (buf[strlen (buf) - 2] == 'R')
602 printf ("Unexpected emulator reply: \n%s\n", buf);
606 printf ("Unexpected stop: \n%s\n", buf);
607 status->kind = TARGET_WAITKIND_STOPPED;
608 status->value.sig = TARGET_SIGNAL_QUIT;
616 printf ("\nStopping emulator...");
617 if (!setjmp (interrupt))
619 old_sigint = signal (SIGINT, es1800_request_quit);
620 send_command ("STP");
621 printf (" emulator stopped\n");
622 status->kind = TARGET_WAITKIND_STOPPED;
623 status->value.sig = TARGET_SIGNAL_INT;
628 es1800_reset ((char*) 1);
631 signal (SIGINT, old_sigint);
632 timeout = old_timeout;
637 /* Fetch register values from remote machine.
638 regno - the register to be fetched (fetch all registers if -1) */
641 es1800_fetch_register (regno)
648 static char regtab[18][4] =
650 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
651 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
655 if ((regno < 15) || (regno == 16) || (regno == 17))
658 send_with_reply (regtab[regno], buf, sizeof (buf));
660 for (k = 0; k < 4; k++)
662 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
664 error ("Emulator reply is too short: %s", buf);
666 registers[r++] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
671 es1800_fetch_registers ();
675 /* Read the remote registers into REGISTERS.
676 Always fetches all registers. */
679 es1800_fetch_registers ()
682 char SR_buf[PBUFSIZ];
688 send_with_reply ("DR", buf, sizeof (buf));
690 /* Reply is edited to a string that describes registers byte by byte,
691 each byte encoded as two hex characters. */
696 /* parsing row one - D0-D7-registers */
698 while (*p++ != '\n') {;}
699 for (i = 4; i < 70; i += (i == 39 ? 3 : 1))
701 for (k = 0; k < 4; k++)
703 if (p[i+0] == 0 || p[i+1] == 0)
705 error ("Emulator reply is too short: %s", buf);
707 registers[r++] = (fromhex (p[i+0]) * 16) + fromhex (p[i+1]);
713 /* parsing row two - A0-A6-registers */
715 while (*p++ != '\n') {;}
716 for (i = 4; i < 61; i += (i == 39 ? 3 : 1))
718 for (k = 0; k < 4; k++)
720 if (p[i+0] == 0 || p[i+1] == 0)
722 error ("Emulator reply is too short: %s", buf);
724 registers[r++] = (fromhex (p[i+0])) * 16 + fromhex (p[i+1]);
730 while (*p++ != '\n') {;}
732 /* fetch SSP-, SR- and PC-registers */
734 /* first - check STATUS-word and decide which stackpointer to use */
736 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
742 if (*p == '3') /* use masterstackpointer MSP */
744 send_with_reply ("MSP", buf, sizeof (buf));
746 else if (*p == '2') /* use interruptstackpointer ISP */
748 send_with_reply ("ISP", buf, sizeof (buf));
750 else /* use userstackpointer USP */
752 send_with_reply ("USP", buf, sizeof (buf));
755 for (k = 0; k<4; k++)
757 if (p[k*2+1] == 0 || p[k*2+2] == 0)
759 error ("Emulator reply is too short: %s", buf);
761 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
765 for (k = 0; k < 4; k++)
767 if (p[k*2+1] == 0 || p[k*2+2] == 0)
769 error ("Emulator reply is too short: %s", buf);
772 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
774 send_with_reply ("PC", buf, sizeof (buf));
776 for (k = 0; k<4; k++)
778 if (p[k*2+1] == 0 || p[k*2+2] == 0)
780 error ("Emulator reply is too short: %s", buf);
782 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
785 else /* 68000-mode */
787 if (*p == '2') /* use supervisorstackpointer SSP */
789 send_with_reply ("SSP", buf, sizeof (buf));
791 else /* use userstackpointer USP */
793 send_with_reply ("USP", buf, sizeof (buf));
796 /* fetch STACKPOINTER */
799 for (k = 0; k < 4; k++)
801 if (p[k*2 + 1] == 0 || p[k*2 + 2] == 0)
803 error ("Emulator reply is too short: %s", buf);
805 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
811 for (k = 0; k < 4; k++)
813 if (p[k*2+1] == 0 || p[k*2+2] == 0)
815 error ("Emulator reply is too short: %s", buf);
818 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
823 send_with_reply ("PC", buf, sizeof (buf));
825 for (k = 0; k < 4; k++)
827 if (p[k*2+1] == 0 || p[k*2+2] == 0)
829 error ("Emulator reply is too short: %s", buf);
831 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
836 /* Store register value, located in REGISTER, on the target processor.
837 regno - the register-number of the register to store
838 (-1 means store them all)
839 FIXME: Return errno value. */
842 es1800_store_register(regno)
846 static char regtab[18][4] =
848 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
849 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
854 char SR_buf[PBUFSIZ];
855 char stack_pointer[4];
862 r = (unsigned char *) registers;
864 if (regno == -1) /* write all registers */
869 else /* write one register */
876 if ((regno == -1) || (regno == 15))
878 /* fetch current status */
879 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
884 if (*p == '3') /* use masterstackpointer MSP */
886 strcpy (stack_pointer,"MSP");
890 if (*p == '2') /* use interruptstackpointer ISP */
892 strcpy (stack_pointer,"ISP");
896 strcpy (stack_pointer,"USP"); /* use userstackpointer USP */
900 else /* 68000-mode */
902 if (*p == '2') /* use supervisorstackpointer SSP */
904 strcpy (stack_pointer,"SSP");
908 strcpy (stack_pointer,"USP");/* use userstackpointer USP */
911 strcpy (regtab[15],stack_pointer);
914 for (i = j; i<k; i++)
916 buf[0] = regtab[i][0];
917 buf[1] = regtab[i][1];
918 buf[2] = regtab[i][2];
921 buf[5] = tohex ((*r >> 4) & 0x0f);
922 buf[6] = tohex (*r++ & 0x0f);
923 buf[7] = tohex ((*r >> 4) & 0x0f);
924 buf[8] = tohex (*r++ & 0x0f);
925 buf[9] = tohex ((*r >> 4) & 0x0f);
926 buf[10] = tohex (*r++ & 0x0f);
927 buf[11] = tohex ((*r >> 4) & 0x0f);
928 buf[12] = tohex (*r++ & 0x0f);
931 send_with_reply (buf, buf, sizeof (buf)); /* FIXME, reply not used? */
936 /* Prepare to store registers. */
939 es1800_prepare_to_store ()
941 /* Do nothing, since we can store individual regs */
944 /* Convert hex digit A to a number. */
950 if (a >= '0' && a <= '9')
954 else if (a >= 'a' && a <= 'f')
958 else if (a >= 'A' && a <= 'F')
964 error ("Reply contains invalid hex digit");
970 /* Convert number NIB to a hex digit. */
982 return ('A' + nib - 10);
986 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
987 to or from debugger address MYADDR. Write to inferior if WRITE is
988 nonzero. Returns length of data written or read; 0 for error.
990 memaddr - the target's address
991 myaddr - gdb's address
992 len - number of bytes
993 write - write if != 0 otherwise read */
996 es1800_xfer_inferior_memory (memaddr, myaddr, len, write, tops)
1001 struct target_ops *tops; /* Unused */
1008 xfersize = len > MAXBUFBYTES ? MAXBUFBYTES : len;
1011 es1800_write_bytes (memaddr, myaddr, xfersize);
1015 es1800_read_bytes (memaddr, myaddr, xfersize);
1017 memaddr += xfersize;
1021 return (origlen); /* no error possible */
1025 /* Write memory data directly to the emulator.
1026 This does not inform the data cache; the data cache uses this.
1027 MEMADDR is the address in the remote memory space.
1028 MYADDR is the address of the buffer in our space.
1029 LEN is the number of bytes.
1031 memaddr - the target's address
1032 myaddr - gdb's address
1033 len - number of bytes */
1036 es1800_write_bytes (memaddr, myaddr, len)
1046 for (i = 0; i < len; i++)
1048 sprintf (buf, "@.B$%x=$%x", memaddr+i, (*p++) & 0xff);
1049 send_with_reply (buf, buf, sizeof (buf)); /* FIXME send_command? */
1054 /* Read memory data directly from the emulator.
1055 This does not use the data cache; the data cache uses this.
1057 memaddr - the target's address
1058 myaddr - gdb's address
1059 len - number of bytes */
1062 es1800_read_bytes (memaddr, myaddr, len)
1067 static int DB_tab[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1074 if (len > PBUFSIZ / 2 - 1)
1079 if (len == 1) /* The emulator does not like expressions like: */
1081 len = 2; /* DB.B $20018 TO $20018 */
1084 /* Reply describes registers byte by byte, each byte encoded as two hex
1087 sprintf (buf, "DB.B $%x TO $%x", memaddr, memaddr+len-1);
1088 send_with_reply (buf, buf, sizeof (buf));
1090 low_addr = memaddr&0x0f;
1091 for (i = low_addr; i < low_addr + len; i++)
1093 if ((!(i % 16)) && i)
1094 { /* if (i = 16,32,48) */
1095 while (*p++!='\n') {;}
1098 p = b + DB_tab[i%16] + (m68020 ? 2 : 0);
1099 if (p[0] == 32 || p[1] == 32)
1101 error ("Emulator reply is too short: %s", buf);
1103 myaddr[i-low_addr] = fromhex (p[0]) * 16 + fromhex (p[1]);
1107 /* Information about the current target */
1110 es1800_files_info (tops)
1111 struct target_ops *tops; /* Unused */
1113 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename, 19200,
1118 /* We read the contents of the target location and stash it,
1119 then overwrite it with a breakpoint instruction.
1121 addr - is the target location in the target machine.
1122 contents_cache - is a pointer to memory allocated for saving the target contents.
1123 It is guaranteed by the caller to be long enough to save sizeof
1126 FIXME: This size is target_arch dependent and should be available in
1127 the target_arch transfer vector, if we ever have one... */
1130 es1800_insert_breakpoint (addr, contents_cache)
1132 char *contents_cache;
1136 val = target_read_memory (addr, contents_cache, sizeof (es1800_break_insn));
1140 val = target_write_memory (addr, es1800_break_insn,
1141 sizeof (es1800_break_insn));
1148 /* Write back the stashed instruction
1150 addr - is the target location in the target machine.
1151 contents_cache - is a pointer to memory allocated for saving the target contents.
1152 It is guaranteed by the caller to be long enough to save sizeof
1153 BREAKPOINT bytes. */
1156 es1800_remove_breakpoint (addr, contents_cache)
1158 char *contents_cache;
1161 return (target_write_memory (addr, contents_cache,
1162 sizeof (es1800_break_insn)));
1165 /* create_break_insn ()
1166 Primitive datastructures containing the es1800 breakpoint instruction */
1169 es1800_create_break_insn (ins, vec)
1182 Seach for breakpoint routine in emulator memory.
1183 returns non-zero on failure
1184 vec - trap vector used for breakpoints */
1190 CORE_ADDR memaddress;
1192 char *instr = "NqNqNqNs"; /* breakpoint routine */
1195 get_break_addr (vec, &memaddress);
1199 status = target_read_memory (memaddress, buf, 8);
1202 memory_error (status, memaddress);
1204 return (STRCMP (instr, buf));
1210 /* get_break_addr ()
1211 find address of breakpint routine
1212 vec - trap vector used for breakpoints
1213 addrp - store the address here */
1216 get_break_addr (vec, addrp)
1220 CORE_ADDR memaddress = 0;
1229 send_with_reply ("VBR ", buf, sizeof (buf));
1231 for (k = 0; k < 4; k++)
1233 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1235 error ("Emulator reply is too short: %s", buf);
1237 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1239 /* base addr of exception vector table */
1240 memaddress = *((CORE_ADDR *) base_addr);
1243 memaddress += (vec + 32) * 4; /* address of trap vector */
1244 status = target_read_memory (memaddress, (char *) addrp, 4);
1247 memory_error (status, memaddress);
1252 /* Kill an inferior process */
1257 if (inferior_pid != 0)
1260 es1800_mourn_inferior ();
1265 /* Load a file to the ES1800 emulator.
1266 Converts the file from a.out format into Extended Tekhex format
1267 before the file is loaded.
1268 Also loads the trap routine, and sets the ES1800 breakpoint on it
1269 filename - the a.out to be loaded
1270 from_tty - says whether to be verbose or not
1271 FIXME Uses emulator overlay memory for trap routine */
1274 es1800_load (filename, from_tty)
1282 struct cleanup *old_chain;
1283 int es1800_load_format = 5;
1285 if (es1800_desc == NULL)
1287 printf ("No emulator attached, type emulator-command first\n");
1291 filename = tilde_expand (filename);
1292 make_cleanup (free, filename);
1294 switch (es1800_load_format)
1296 case 2: /* Extended Tekhex */
1299 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename);
1301 sprintf (buf, "tekhex %s", filename);
1303 sprintf (loadname, "out.hex");
1306 case 5: /* Motorola S-rec */
1309 printf ("Converting \"%s\" to Motorola S-record format\n",
1312 /* in the future the source code in copy (part of binutils-1.93) will
1313 be included in this file */
1315 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1318 sprintf (loadname, "/tmp/out.hex");
1322 error ("Downloading format not defined\n");
1325 breakpoint_init_inferior ();
1329 printf ("Downloading \"%s\" to the ES 1800\n",filename);
1331 if ((instream = fopen (loadname, "r")) == NULL)
1333 perror_with_name ("fopen:");
1336 old_chain = make_cleanup (fclose, instream);
1341 download (instream, from_tty, es1800_load_format);
1343 /* if breakpoint routine is not present anymore we have to check
1344 whether to download a new breakpoint routine or not */
1346 if ((verify_break (es1800_break_vec) != 0)
1347 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1350 printf ("Using break vector 0x%x\n", es1800_break_vec);
1351 sprintf (buf, "0x%x ", es1800_break_vec);
1352 printf ("Give the start address of the breakpoint routine: ");
1353 fgets (buf + strlen (buf), sizeof (buf) - strlen (buf), stdin);
1354 es1800_init_break (buf, 0);
1357 do_cleanups (old_chain);
1359 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1362 if (fclose (instream) == EOF)
1367 if (es1800_load_format != 2)
1369 sprintf (buf, "/usr/bin/rm %s", loadname);
1373 symbol_file_command (filename, from_tty); /* reading symbol table */
1379 #define NUMCPYBYTES 20
1382 bfd_copy (from_bfd, to_bfd)
1388 char buf[NUMCPYBYTES];
1390 for (p = from_bfd->sections; p != NULL; p = p->next)
1392 printf (" Copying section %s. Size = %x.\n", p->name, p->_cooked_size);
1393 printf (" vma = %x, offset = %x, output_sec = %x\n",
1394 p->vma, p->output_offset, p->output_section);
1395 new = bfd_make_section (to_bfd, p->name);
1396 if (p->_cooked_size &&
1397 !bfd_set_section_size (to_bfd, new, p->_cooked_size))
1399 error ("Wrong BFD size!\n");
1401 if (!bfd_set_section_flags (to_bfd, new, p->flags))
1403 error ("bfd_set_section_flags");
1407 for (i = 0; (i + NUMCPYBYTES) < p->_cooked_size ; i += NUMCPYBYTES)
1409 if (!bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1410 (bfd_size_type) NUMCPYBYTES))
1412 error ("bfd_get_section_contents\n");
1414 if (!bfd_set_section_contents (to_bfd, new, (PTR) buf, (file_ptr) i,
1415 (bfd_size_type) NUMCPYBYTES))
1417 error ("bfd_set_section_contents\n");
1420 bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1421 (bfd_size_type) (p->_cooked_size - i));
1422 bfd_set_section_contents (to_bfd, new, (PTR) buf,(file_ptr) i,
1423 (bfd_size_type) (p->_cooked_size - i));
1429 /* Start an process on the es1800 and set inferior_pid to the new
1431 execfile - the file to run
1432 args - arguments passed to the program
1433 env - the environment vector to pass */
1436 es1800_create_inferior (execfile, args, env)
1444 struct expression *expr;
1445 register struct cleanup *old_chain = 0;
1451 error ("Can't pass arguments to remote ES1800 process");
1455 if (query ("Use 'start' as entry point? "))
1457 expr = parse_c_expression ("start");
1458 old_chain = make_cleanup (free_current_contents, &expr);
1459 val = evaluate_expression (expr);
1460 entry_pt = (val->location).address;
1464 printf ("Enter the program's entry point (in hexadecimal): ");
1465 scanf ("%x", &entry_pt);
1469 if (execfile == 0 || exec_bfd == 0)
1471 error ("No exec file specified");
1474 entry_pt = (int) bfd_get_start_address (exec_bfd);
1478 /* Now that we have a child process, make it our target. */
1480 push_target (&es1800_child_ops);
1482 /* The "process" (board) is already stopped awaiting our commands, and
1483 the program is already downloaded. We just set its PC and go. */
1485 inferior_pid = pid; /* Needed for wait_for_inferior below */
1487 clear_proceed_status ();
1489 /* Tell wait_for_inferior that we've started a new process. */
1491 init_wait_for_inferior ();
1493 /* Set up the "saved terminal modes" of the inferior
1494 based on what modes we are starting it with. */
1496 target_terminal_init ();
1498 /* Install inferior's terminal modes. */
1500 target_terminal_inferior ();
1502 /* remote_start (args); */
1503 /* trap_expected = 0; */
1504 /* insert_step_breakpoint (); FIXME, do we need this? */
1506 /* Let 'er rip... */
1507 proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1512 /* The process has died, clean up. */
1515 es1800_mourn_inferior ()
1517 remove_breakpoints ();
1518 unpush_target (&es1800_child_ops);
1519 generic_mourn_inferior (); /* Do all the proper things now */
1522 /* ES1800-protocol specific routines */
1524 /* Keep discarding input from the remote system, until STRING is found.
1525 Let the user break out immediately.
1526 string - the string to expect
1527 nowait - break out if string not the emulator's first respond otherwise
1528 read until string is found (== 0) */
1531 expect (string, nowait)
1546 if (c == toupper (*p))
1561 printf ("\'%s\' expected\n" , string);
1562 printf ("char %d is %d", p - string, c);
1568 /* Keep discarding input until we see the prompt. */
1577 /* Read one character */
1581 /* read from stdin */
1589 printf ("readchar, give one character\n");
1592 #if defined (LOG_FILE)
1593 putc (buf[0] & 0x7f, log_file);
1596 return (buf[0] & 0x7f);
1599 #else /* !DEBUG_STDIN */
1601 /* Read a character from the remote system, doing all the fancy
1609 ch = SERIAL_READCHAR (es1800_desc, timeout);
1611 /* FIXME: doing an error() here will probably cause trouble, at least if from
1613 if (ch == SERIAL_TIMEOUT)
1614 error ("Timeout reading from remote system.");
1615 else if (ch == SERIAL_ERROR)
1616 perror_with_name ("remote read");
1618 #if defined (LOG_FILE)
1619 putc (ch & 0x7f, log_file);
1626 #endif /* DEBUG_STDIN */
1629 /* Send a command to the emulator and save the reply.
1630 Report an error if we get an error reply.
1631 string - the es1800 command
1632 buf - containing the emulator reply on return
1633 len - size of buf */
1636 send_with_reply (string, buf, len)
1641 SERIAL_WRITE (es1800_desc, "\r", 1);
1648 getmessage (buf, len);
1652 /* Send the command in STR to the emulator adding \r. check
1653 the echo for consistency.
1654 string - the es1800 command */
1657 send_command (string)
1661 SERIAL_WRITE (es1800_desc, "\r", 1);
1671 string - the es1800 command */
1679 fprintf (stderr, "Sending: %s\n", string);
1681 SERIAL_WRITE (es1800_desc, string, strlen (string));
1685 /* Read a message from the emulator and store it in BUF.
1686 buf - containing the emulator reply on return
1687 len - size of buf */
1690 getmessage (buf, len)
1696 int prompt_found = 0;
1699 #if defined (LOG_FILE)
1700 /* This is a convenient place to do this. The idea is to do it often
1701 enough that we never lose much data if we terminate abnormally. */
1711 if (len-- < 2) /* char and terminaling NULL */
1713 error ("input buffer overrun\n");
1718 if ((c == '>') && (*(bp - 1) == ' '))
1723 while (!prompt_found);
1728 fprintf (stderr,"message received :%s\n", buf);
1733 download (instream, from_tty, format)
1742 send_command ("SET #2,$1A"); /* reset char = ^Z */
1743 send_command ("SET #3,$11,$13"); /* XON XOFF */
1746 send_command ("SET #26,#2");
1750 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1752 send_command ("DFB = $10");
1753 send_command ("PUR");
1754 send_command ("CES");
1759 printf (" 0 records loaded...\r");
1761 while (fgets (buf, 160, instream))
1766 printf ("%5d\b\b\b\b\b",++i);
1769 if ((c = readchar ()) != 006)
1771 error ("expected ACK");
1780 /* Additional commands */
1782 #if defined (TIOCGETP) && defined (FNDELAY) && defined (EWOULDBLOCK)
1783 #define PROVIDE_TRANSPARENT
1786 #ifdef PROVIDE_TRANSPARENT
1787 /* Talk directly to the emulator
1788 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1792 es1800_transparent (args, from_tty)
1797 struct sgttyb modebl;
1800 struct sgttyb console_mode_save;
1801 int console_fc_save;
1806 char consolebuf[80];
1808 char es1800_buf[80];
1812 if (es1800_desc == NULL)
1814 printf ("No emulator attached, type emulator-command first\n");
1819 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1820 printf ("To leave this mode (transparent mode), press ^E.\n");
1825 if ((console = open ("/dev/tty", O_RDWR)) == -1)
1827 perror_with_name ("/dev/tty:");
1830 if ((fcflag = fcntl (console, F_GETFL, 0)) == -1)
1832 perror_with_name ("fcntl console");
1835 console_fc_save = fcflag;
1836 fcflag = fcflag | FNDELAY;
1838 if (fcntl (console, F_SETFL, fcflag) == -1)
1840 perror_with_name ("fcntl console");
1843 if (ioctl (console, TIOCGETP, &modebl))
1845 perror_with_name ("ioctl console");
1848 console_mode_save = modebl;
1849 modebl.sg_flags = RAW;
1851 if (ioctl (console, TIOCSETP, &modebl))
1853 perror_with_name ("ioctl console");
1856 if ((fcflag = fcntl (es1800_desc->fd, F_GETFL, 0)) == -1)
1858 perror_with_name ("fcntl serial");
1861 es1800_fc_save = fcflag;
1862 fcflag = fcflag | FNDELAY;
1864 if (fcntl (es1800_desc->fd, F_SETFL, fcflag) == -1)
1866 perror_with_name ("fcntl serial");
1871 cc = read (console, inputbuf, inputcnt);
1874 if ((*inputbuf & 0x7f) == 0x05)
1878 for (i = 0; i < cc; )
1880 es1800_buf[es1800_cnt++] = inputbuf[i++];
1882 if ((cc = SERIAL_WRITE (es1800_desc, es1800_buf, es1800_cnt)) == -1)
1884 perror_with_name ("FEL! write:");
1887 if (es1800_cnt && cc)
1889 for (i = 0; i < es1800_cnt; i++)
1891 es1800_buf[i] = es1800_buf[cc+i];
1895 else if (errno != EWOULDBLOCK)
1897 perror_with_name ("FEL! read:");
1900 cc = read (es1800_desc->fd,inputbuf,inputcnt);
1903 for (i = 0; i < cc; )
1905 consolebuf[consolecnt++] = inputbuf[i++];
1907 if ((cc = write (console,consolebuf,consolecnt)) == -1)
1909 perror_with_name ("FEL! write:");
1912 if (consolecnt && cc)
1914 for (i = 0; i < consolecnt; i++)
1916 consolebuf[i] = consolebuf[cc+i];
1920 else if (errno != EWOULDBLOCK)
1922 perror_with_name ("FEL! read:");
1926 console_fc_save = console_fc_save & !FNDELAY;
1927 if (fcntl (console, F_SETFL, console_fc_save) == -1)
1929 perror_with_name ("FEL! fcntl");
1932 if (ioctl (console, TIOCSETP, &console_mode_save))
1934 perror_with_name ("FEL! ioctl");
1939 if (fcntl (es1800_desc->fd, F_SETFL, es1800_fc_save) == -1)
1941 perror_with_name ("FEL! fcntl");
1947 #endif /* PROVIDE_TRANSPARENT */
1950 es1800_init_break (args, from_tty)
1954 CORE_ADDR memaddress = 0;
1963 error_no_arg ("a trap vector");
1966 if (!(space_index = strchr (args, ' ')))
1968 error ("Two arguments needed (trap vector and address of break routine).\n");
1971 *space_index = '\0';
1973 es1800_break_vec = strtol (args, (char **) NULL, 0);
1974 es1800_break_address = parse_and_eval_address (space_index + 1);
1976 es1800_create_break_insn (es1800_break_insn, es1800_break_vec);
1980 send_with_reply ("VBR ", buf, sizeof (buf));
1982 for (k = 0; k < 4; k++)
1984 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1986 error ("Emulator reply is too short: %s", buf);
1988 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1990 /* base addr of exception vector table */
1991 memaddress = *((CORE_ADDR *) base_addr);
1994 memaddress += (es1800_break_vec + 32) * 4; /* address of trap vector */
1996 sprintf (buf, "@.L%lx=$%lx", memaddress, es1800_break_address);
1997 send_command (buf); /* set the address of the break routine in the */
2000 sprintf (buf, "@.L%lx=$4E714E71", es1800_break_address); /* NOP; NOP */
2002 sprintf (buf, "@.L%lx=$4E714E73", es1800_break_address + 4); /* NOP; RTE */
2005 sprintf (buf, "AC2=$%lx", es1800_break_address + 4);
2006 /* breakpoint at es1800-break_address */
2008 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2012 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2013 es1800_break_vec, es1800_break_address);
2018 es1800_child_open (arg, from_tty)
2022 error ("Use the \"run\" command to start a child process.");
2026 es1800_child_detach (args, from_tty)
2032 error ("Argument given to \"detach\" when remotely debugging.");
2038 printf ("Ending debugging the process %d.\n", inferior_pid);
2043 /* Define the target subroutine names */
2045 struct target_ops es1800_ops =
2047 "es1800", /* to_shortname */
2049 "Remote serial target in ES1800-emulator protocol",
2051 "Remote debugging on the es1800 emulator via a serial line.\n\
2052 Specify the serial device it is connected to (e.g. /dev/ttya).",
2053 es1800_open, /* to_open */
2054 es1800_close, /* to_close */
2055 es1800_attach, /* to_attach */
2056 es1800_detach, /* to_detach */
2057 es1800_resume, /* to_resume */
2059 NULL, /* to_fetch_registers */
2060 NULL, /* to_store_registers */
2061 es1800_prepare_to_store, /* to_prepare_to_store */
2062 es1800_xfer_inferior_memory, /* to_xfer_memory */
2063 es1800_files_info, /* to_files_info */
2064 es1800_insert_breakpoint, /* to_insert_breakpoint */
2065 es1800_remove_breakpoint, /* to_remove_breakpoint */
2066 NULL, /* to_terminal_init */
2067 NULL, /* to_terminal_inferior */
2068 NULL, /* to_terminal_ours_for_output */
2069 NULL, /* to_terminal_ours */
2070 NULL, /* to_terminal_info */
2072 es1800_load, /* to_load */
2073 NULL, /* to_lookup_symbol */
2074 es1800_create_inferior, /* to_create_inferior */
2075 NULL, /* to_mourn_inferior */
2077 0, /* notice_signals */
2078 core_stratum, /* to_stratum */
2080 0, /* to_has_all_memory */
2081 1, /* to_has_memory */
2082 0, /* to_has_stack */
2083 0, /* to_has_registers */
2084 0, /* to_has_execution */
2085 NULL, /* to_sections */
2086 NULL, /* to_sections_end */
2087 OPS_MAGIC /* to_magic (always last) */
2090 /* Define the target subroutine names */
2092 struct target_ops es1800_child_ops =
2094 "es1800_process", /* to_shortname */
2096 "Remote serial target in ES1800-emulator protocol",
2098 "Remote debugging on the es1800 emulator via a serial line.\n\
2099 Specify the serial device it is connected to (e.g. /dev/ttya).",
2100 es1800_child_open, /* to_open */
2101 NULL, /* to_close */
2102 es1800_attach, /* to_attach */
2103 es1800_child_detach, /* to_detach */
2104 es1800_resume, /* to_resume */
2105 es1800_wait, /* to_wait */
2106 es1800_fetch_register, /* to_fetch_registers */
2107 es1800_store_register, /* to_store_registers */
2108 es1800_prepare_to_store, /* to_prepare_to_store */
2109 es1800_xfer_inferior_memory, /* to_xfer_memory */
2110 es1800_files_info, /* to_files_info */
2111 es1800_insert_breakpoint, /* to_insert_breakpoint */
2112 es1800_remove_breakpoint, /* to_remove_breakpoint */
2113 NULL, /* to_terminal_init */
2114 NULL, /* to_terminal_inferior */
2115 NULL, /* to_terminal_ours_for_output */
2116 NULL, /* to_terminal_ours */
2117 NULL, /* to_terminal_info */
2118 es1800_kill, /* to_kill */
2119 es1800_load, /* to_load */
2120 NULL, /* to_lookup_symbol */
2121 es1800_create_inferior, /* to_create_inferior */
2122 es1800_mourn_inferior, /* to_mourn_inferior */
2124 0, /* notice_signals */
2125 process_stratum, /* to_stratum */
2127 1, /* to_has_all_memory */
2128 1, /* to_has_memory */
2129 1, /* to_has_stack */
2130 1, /* to_has_registers */
2131 1, /* to_has_execution */
2132 NULL, /* to_sections */
2133 NULL, /* to_sections_end */
2134 OPS_MAGIC /* to_magic (always last) */
2138 _initialize_es1800 ()
2140 add_target (&es1800_ops);
2141 add_target (&es1800_child_ops);
2142 #ifdef PROVIDE_TRANSPARENT
2143 add_com ("transparent", class_support, es1800_transparent,
2144 "Start transparent communication with the ES 1800 emulator.");
2145 #endif /* PROVIDE_TRANSPARENT */
2146 add_com ("init_break", class_support, es1800_init_break,
2147 "Download break routine and initialize break facility on ES 1800");