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");
374 if (SERIAL_SETBAUDRATE (es1800_desc, baud_rate))
376 SERIAL_CLOSE (es1800_desc);
377 perror_with_name (name);
380 SERIAL_RAW (es1800_desc);
382 /* If there is something sitting in the buffer we might take it as a
383 response to a command, which would be bad. */
384 SERIAL_FLUSH_INPUT (es1800_desc);
386 #endif /* DEBUG_STDIN */
388 push_target (&es1800_ops); /* Switch to using remote target now */
391 printf ("Remote ES1800 debugging using %s\n", name);
394 #if defined (LOG_FILE)
396 log_file = fopen (LOG_FILE, "w");
397 if (log_file == NULL)
399 perror_with_name (LOG_FILE);
402 #endif /* LOG_FILE */
404 /* Hello? Are you there?, also check mode */
406 /* send_with_reply( "DB 0 TO 1", buf, sizeof(buf)); */
407 /* for (p = buf, i = 0; *p++ =='0';) */ /* count the number of zeros */
411 getmessage (buf, sizeof (buf)); /* send reset character */
415 printf ("Checking mode.... ");
417 /* m68020 = (i==8); */ /* if eight zeros then we are in m68020 mode */
419 /* What kind of processor am i talking to ?*/
421 while (*p++ != '\n') {;}
422 while (*p++ != '\n') {;}
423 while (*p++ != '\n') {;}
424 for (i = 0; i < 20; i++, p++) {;}
425 m68020 = !strncmp (p, "68020", 5);
428 printf ("You are in %s(%c%c%c%c%c)-mode\n", MODE, p[0], p[1], p[2],
432 /* if no init_break statement is present in .gdb file we have to check
433 whether to download a breakpoint routine or not */
436 if ((es1800_break_vec == 0) || (verify_break (es1800_break_vec) != 0)
437 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
439 CORE_ADDR memaddress;
440 printf ("Give the start address of the breakpoint routine: ");
441 scanf ("%li", &memaddress);
442 es1800_init_break ((es1800_break_vec ? es1800_break_vec :
443 ES1800_BREAK_VEC), memaddress);
449 /* Close out all files and local state before this target loses control.
450 quitting - are we quitting gdb now? */
453 es1800_close (quitting)
456 if (es1800_desc != NULL)
458 printf ("\nClosing connection to emulator...\n");
459 if (SERIAL_SET_TTY_STATE (es1800_desc, es1800_saved_ttystate) < 0)
460 print_sys_errmsg ("warning: unable to restore tty state", errno);
461 fcntl (es1800_desc->fd, F_SETFL, es1800_fc_save);
462 SERIAL_CLOSE (es1800_desc);
465 if (savename != NULL)
471 #if defined (LOG_FILE)
473 if (log_file != NULL)
475 if (ferror (log_file))
477 printf ("Error writing log file.\n");
479 if (fclose (log_file) != 0)
481 printf ("Error closing log file.\n");
486 #endif /* LOG_FILE */
490 /* Attaches to a process on the target side
491 proc_id - the id of the process to be attached.
492 from_tty - says whether to be verbose or not */
495 es1800_attach (args, from_tty)
499 error ("Cannot attach to pid %s, this feature is not implemented yet.",
504 /* Takes a program previously attached to and detaches it.
505 We better not have left any breakpoints
506 in the program or it'll die when it hits one.
507 Close the open connection to the remote debugger.
508 Use this when you want to detach and do something else
511 args - arguments given to the 'detach' command
512 from_tty - says whether to be verbose or not */
515 es1800_detach (args, from_tty)
521 error ("Argument given to \"detach\" when remotely debugging.");
526 printf ("Ending es1800 remote debugging.\n");
531 /* Tell the remote machine to resume.
532 step - single-step or run free
533 siggnal - the signal value to be given to the target (0 = no signal) */
536 es1800_resume (pid, step, siggnal)
539 enum target_signal siggnal;
545 error ("Can't send signals to a remote system.");
549 strcpy (buf,"STP\r");
554 send_command ("RBK");
558 /* Wait until the remote machine stops, then return,
559 storing status in STATUS just as `wait' would.
563 es1800_wait (pid, status)
565 struct target_waitstatus *status;
567 unsigned char buf[PBUFSIZ];
568 int old_timeout = timeout;
570 status->kind = TARGET_WAITKIND_EXITED;
571 status->value.integer = 0;
573 timeout = 0; /* Don't time out -- user program is running. */
574 if (!setjmp (interrupt))
576 old_sigint = signal (SIGINT, es1800_request_quit);
579 getmessage (buf, sizeof(buf));
580 if (strncmp ( buf, "\r\n* BREAK *", 11) == 0)
582 status->kind = TARGET_WAITKIND_STOPPED;
583 status->value.sig = TARGET_SIGNAL_TRAP;
584 send_command ("STP"); /* Restore stack and PC and such */
587 send_command ("STP");
591 if (strncmp (buf, "STP\r\n ", 6) == 0)
593 status->kind = TARGET_WAITKIND_STOPPED;
594 status->value.sig = TARGET_SIGNAL_TRAP;
597 if (buf[strlen (buf) - 2] == 'R')
599 printf ("Unexpected emulator reply: \n%s\n", buf);
603 printf ("Unexpected stop: \n%s\n", buf);
604 status->kind = TARGET_WAITKIND_STOPPED;
605 status->value.sig = TARGET_SIGNAL_QUIT;
613 printf ("\nStopping emulator...");
614 if (!setjmp (interrupt))
616 old_sigint = signal (SIGINT, es1800_request_quit);
617 send_command ("STP");
618 printf (" emulator stopped\n");
619 status->kind = TARGET_WAITKIND_STOPPED;
620 status->value.sig = TARGET_SIGNAL_INT;
625 es1800_reset ((char*) 1);
628 signal (SIGINT, old_sigint);
629 timeout = old_timeout;
634 /* Fetch register values from remote machine.
635 regno - the register to be fetched (fetch all registers if -1) */
638 es1800_fetch_register (regno)
645 static char regtab[18][4] =
647 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
648 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
652 if ((regno < 15) || (regno == 16) || (regno == 17))
655 send_with_reply (regtab[regno], buf, sizeof (buf));
657 for (k = 0; k < 4; k++)
659 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
661 error ("Emulator reply is too short: %s", buf);
663 registers[r++] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
668 es1800_fetch_registers ();
672 /* Read the remote registers into REGISTERS.
673 Always fetches all registers. */
676 es1800_fetch_registers ()
679 char SR_buf[PBUFSIZ];
685 send_with_reply ("DR", buf, sizeof (buf));
687 /* Reply is edited to a string that describes registers byte by byte,
688 each byte encoded as two hex characters. */
693 /* parsing row one - D0-D7-registers */
695 while (*p++ != '\n') {;}
696 for (i = 4; i < 70; i += (i == 39 ? 3 : 1))
698 for (k = 0; k < 4; k++)
700 if (p[i+0] == 0 || p[i+1] == 0)
702 error ("Emulator reply is too short: %s", buf);
704 registers[r++] = (fromhex (p[i+0]) * 16) + fromhex (p[i+1]);
710 /* parsing row two - A0-A6-registers */
712 while (*p++ != '\n') {;}
713 for (i = 4; i < 61; i += (i == 39 ? 3 : 1))
715 for (k = 0; k < 4; k++)
717 if (p[i+0] == 0 || p[i+1] == 0)
719 error ("Emulator reply is too short: %s", buf);
721 registers[r++] = (fromhex (p[i+0])) * 16 + fromhex (p[i+1]);
727 while (*p++ != '\n') {;}
729 /* fetch SSP-, SR- and PC-registers */
731 /* first - check STATUS-word and decide which stackpointer to use */
733 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
739 if (*p == '3') /* use masterstackpointer MSP */
741 send_with_reply ("MSP", buf, sizeof (buf));
743 else if (*p == '2') /* use interruptstackpointer ISP */
745 send_with_reply ("ISP", buf, sizeof (buf));
747 else /* use userstackpointer USP */
749 send_with_reply ("USP", buf, sizeof (buf));
752 for (k = 0; k<4; k++)
754 if (p[k*2+1] == 0 || p[k*2+2] == 0)
756 error ("Emulator reply is too short: %s", buf);
758 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
762 for (k = 0; k < 4; k++)
764 if (p[k*2+1] == 0 || p[k*2+2] == 0)
766 error ("Emulator reply is too short: %s", buf);
769 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
771 send_with_reply ("PC", buf, sizeof (buf));
773 for (k = 0; k<4; k++)
775 if (p[k*2+1] == 0 || p[k*2+2] == 0)
777 error ("Emulator reply is too short: %s", buf);
779 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
782 else /* 68000-mode */
784 if (*p == '2') /* use supervisorstackpointer SSP */
786 send_with_reply ("SSP", buf, sizeof (buf));
788 else /* use userstackpointer USP */
790 send_with_reply ("USP", buf, sizeof (buf));
793 /* fetch STACKPOINTER */
796 for (k = 0; k < 4; k++)
798 if (p[k*2 + 1] == 0 || p[k*2 + 2] == 0)
800 error ("Emulator reply is too short: %s", buf);
802 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
808 for (k = 0; k < 4; k++)
810 if (p[k*2+1] == 0 || p[k*2+2] == 0)
812 error ("Emulator reply is too short: %s", buf);
815 fromhex (SR_buf[k*2+1]) * 16 + fromhex (SR_buf[k*2+2]);
820 send_with_reply ("PC", buf, sizeof (buf));
822 for (k = 0; k < 4; k++)
824 if (p[k*2+1] == 0 || p[k*2+2] == 0)
826 error ("Emulator reply is too short: %s", buf);
828 registers[r++] = fromhex (buf[k*2+1]) * 16 + fromhex (buf[k*2+2]);
833 /* Store register value, located in REGISTER, on the target processor.
834 regno - the register-number of the register to store
835 (-1 means store them all)
836 FIXME: Return errno value. */
839 es1800_store_register(regno)
843 static char regtab[18][4] =
845 "D0 ", "D1 ", "D2 ", "D3 ", "D4 ", "D5 ", "D6 ", "D7 ",
846 "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ", "A6 ", "SSP",
851 char SR_buf[PBUFSIZ];
852 char stack_pointer[4];
859 r = (unsigned char *) registers;
861 if (regno == -1) /* write all registers */
866 else /* write one register */
873 if ((regno == -1) || (regno == 15))
875 /* fetch current status */
876 send_with_reply ("SR", SR_buf, sizeof (SR_buf));
881 if (*p == '3') /* use masterstackpointer MSP */
883 strcpy (stack_pointer,"MSP");
887 if (*p == '2') /* use interruptstackpointer ISP */
889 strcpy (stack_pointer,"ISP");
893 strcpy (stack_pointer,"USP"); /* use userstackpointer USP */
897 else /* 68000-mode */
899 if (*p == '2') /* use supervisorstackpointer SSP */
901 strcpy (stack_pointer,"SSP");
905 strcpy (stack_pointer,"USP");/* use userstackpointer USP */
908 strcpy (regtab[15],stack_pointer);
911 for (i = j; i<k; i++)
913 buf[0] = regtab[i][0];
914 buf[1] = regtab[i][1];
915 buf[2] = regtab[i][2];
918 buf[5] = tohex ((*r >> 4) & 0x0f);
919 buf[6] = tohex (*r++ & 0x0f);
920 buf[7] = tohex ((*r >> 4) & 0x0f);
921 buf[8] = tohex (*r++ & 0x0f);
922 buf[9] = tohex ((*r >> 4) & 0x0f);
923 buf[10] = tohex (*r++ & 0x0f);
924 buf[11] = tohex ((*r >> 4) & 0x0f);
925 buf[12] = tohex (*r++ & 0x0f);
928 send_with_reply (buf, buf, sizeof (buf)); /* FIXME, reply not used? */
933 /* Prepare to store registers. */
936 es1800_prepare_to_store ()
938 /* Do nothing, since we can store individual regs */
941 /* Convert hex digit A to a number. */
947 if (a >= '0' && a <= '9')
951 else if (a >= 'a' && a <= 'f')
955 else if (a >= 'A' && a <= 'F')
961 error ("Reply contains invalid hex digit");
967 /* Convert number NIB to a hex digit. */
979 return ('A' + nib - 10);
983 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
984 to or from debugger address MYADDR. Write to inferior if WRITE is
985 nonzero. Returns length of data written or read; 0 for error.
987 memaddr - the target's address
988 myaddr - gdb's address
989 len - number of bytes
990 write - write if != 0 otherwise read */
993 es1800_xfer_inferior_memory (memaddr, myaddr, len, write, tops)
998 struct target_ops *tops; /* Unused */
1005 xfersize = len > MAXBUFBYTES ? MAXBUFBYTES : len;
1008 es1800_write_bytes (memaddr, myaddr, xfersize);
1012 es1800_read_bytes (memaddr, myaddr, xfersize);
1014 memaddr += xfersize;
1018 return (origlen); /* no error possible */
1022 /* Write memory data directly to the emulator.
1023 This does not inform the data cache; the data cache uses this.
1024 MEMADDR is the address in the remote memory space.
1025 MYADDR is the address of the buffer in our space.
1026 LEN is the number of bytes.
1028 memaddr - the target's address
1029 myaddr - gdb's address
1030 len - number of bytes */
1033 es1800_write_bytes (memaddr, myaddr, len)
1043 for (i = 0; i < len; i++)
1045 sprintf (buf, "@.B$%x=$%x", memaddr+i, (*p++) & 0xff);
1046 send_with_reply (buf, buf, sizeof (buf)); /* FIXME send_command? */
1051 /* Read memory data directly from the emulator.
1052 This does not use the data cache; the data cache uses this.
1054 memaddr - the target's address
1055 myaddr - gdb's address
1056 len - number of bytes */
1059 es1800_read_bytes (memaddr, myaddr, len)
1064 static int DB_tab[16] = {8,11,14,17,20,23,26,29,34,37,40,43,46,49,52,55};
1071 if (len > PBUFSIZ / 2 - 1)
1076 if (len == 1) /* The emulator does not like expressions like: */
1078 len = 2; /* DB.B $20018 TO $20018 */
1081 /* Reply describes registers byte by byte, each byte encoded as two hex
1084 sprintf (buf, "DB.B $%x TO $%x", memaddr, memaddr+len-1);
1085 send_with_reply (buf, buf, sizeof (buf));
1087 low_addr = memaddr&0x0f;
1088 for (i = low_addr; i < low_addr + len; i++)
1090 if ((!(i % 16)) && i)
1091 { /* if (i = 16,32,48) */
1092 while (*p++!='\n') {;}
1095 p = b + DB_tab[i%16] + (m68020 ? 2 : 0);
1096 if (p[0] == 32 || p[1] == 32)
1098 error ("Emulator reply is too short: %s", buf);
1100 myaddr[i-low_addr] = fromhex (p[0]) * 16 + fromhex (p[1]);
1104 /* Information about the current target */
1107 es1800_files_info (tops)
1108 struct target_ops *tops; /* Unused */
1110 printf ("ES1800 Attached to %s at %d baud in %s mode\n", savename, 19200,
1115 /* We read the contents of the target location and stash it,
1116 then overwrite it with a breakpoint instruction.
1118 addr - is the target location in the target machine.
1119 contents_cache - is a pointer to memory allocated for saving the target contents.
1120 It is guaranteed by the caller to be long enough to save sizeof
1123 FIXME: This size is target_arch dependent and should be available in
1124 the target_arch transfer vector, if we ever have one... */
1127 es1800_insert_breakpoint (addr, contents_cache)
1129 char *contents_cache;
1133 val = target_read_memory (addr, contents_cache, sizeof (es1800_break_insn));
1137 val = target_write_memory (addr, es1800_break_insn,
1138 sizeof (es1800_break_insn));
1145 /* Write back the stashed instruction
1147 addr - is the target location in the target machine.
1148 contents_cache - is a pointer to memory allocated for saving the target contents.
1149 It is guaranteed by the caller to be long enough to save sizeof
1150 BREAKPOINT bytes. */
1153 es1800_remove_breakpoint (addr, contents_cache)
1155 char *contents_cache;
1158 return (target_write_memory (addr, contents_cache,
1159 sizeof (es1800_break_insn)));
1162 /* create_break_insn ()
1163 Primitive datastructures containing the es1800 breakpoint instruction */
1166 es1800_create_break_insn (ins, vec)
1179 Seach for breakpoint routine in emulator memory.
1180 returns non-zero on failure
1181 vec - trap vector used for breakpoints */
1187 CORE_ADDR memaddress;
1189 char *instr = "NqNqNqNs"; /* breakpoint routine */
1192 get_break_addr (vec, &memaddress);
1196 status = target_read_memory (memaddress, buf, 8);
1199 memory_error (status, memaddress);
1201 return (STRCMP (instr, buf));
1207 /* get_break_addr ()
1208 find address of breakpint routine
1209 vec - trap vector used for breakpoints
1210 addrp - store the address here */
1213 get_break_addr (vec, addrp)
1217 CORE_ADDR memaddress = 0;
1226 send_with_reply ("VBR ", buf, sizeof (buf));
1228 for (k = 0; k < 4; k++)
1230 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1232 error ("Emulator reply is too short: %s", buf);
1234 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1236 /* base addr of exception vector table */
1237 memaddress = *((CORE_ADDR *) base_addr);
1240 memaddress += (vec + 32) * 4; /* address of trap vector */
1241 status = target_read_memory (memaddress, (char *) addrp, 4);
1244 memory_error (status, memaddress);
1249 /* Kill an inferior process */
1254 if (inferior_pid != 0)
1257 es1800_mourn_inferior ();
1262 /* Load a file to the ES1800 emulator.
1263 Converts the file from a.out format into Extended Tekhex format
1264 before the file is loaded.
1265 Also loads the trap routine, and sets the ES1800 breakpoint on it
1266 filename - the a.out to be loaded
1267 from_tty - says whether to be verbose or not
1268 FIXME Uses emulator overlay memory for trap routine */
1271 es1800_load (filename, from_tty)
1279 struct cleanup *old_chain;
1280 int es1800_load_format = 5;
1282 if (es1800_desc == NULL)
1284 printf ("No emulator attached, type emulator-command first\n");
1288 filename = tilde_expand (filename);
1289 make_cleanup (free, filename);
1291 switch (es1800_load_format)
1293 case 2: /* Extended Tekhex */
1296 printf ("Converting \"%s\" to Extended Tekhex Format\n", filename);
1298 sprintf (buf, "tekhex %s", filename);
1300 sprintf (loadname, "out.hex");
1303 case 5: /* Motorola S-rec */
1306 printf ("Converting \"%s\" to Motorola S-record format\n",
1309 /* in the future the source code in copy (part of binutils-1.93) will
1310 be included in this file */
1312 "copy -s \"a.out-sunos-big\" -d \"srec\" %s /tmp/out.hex",
1315 sprintf (loadname, "/tmp/out.hex");
1319 error ("Downloading format not defined\n");
1322 breakpoint_init_inferior ();
1326 printf ("Downloading \"%s\" to the ES 1800\n",filename);
1328 if ((instream = fopen (loadname, "r")) == NULL)
1330 perror_with_name ("fopen:");
1333 old_chain = make_cleanup (fclose, instream);
1338 download (instream, from_tty, es1800_load_format);
1340 /* if breakpoint routine is not present anymore we have to check
1341 whether to download a new breakpoint routine or not */
1343 if ((verify_break (es1800_break_vec) != 0)
1344 && query ("No breakpoint routine in ES 1800 emulator!\nDownload a breakpoint routine to the emulator? "))
1347 printf ("Using break vector 0x%x\n", es1800_break_vec);
1348 sprintf (buf, "0x%x ", es1800_break_vec);
1349 printf ("Give the start address of the breakpoint routine: ");
1350 fgets (buf + strlen (buf), sizeof (buf) - strlen (buf), stdin);
1351 es1800_init_break (buf, 0);
1354 do_cleanups (old_chain);
1356 readchar (); /* FIXME I am getting a ^G = 7 after the prompt */
1359 if (fclose (instream) == EOF)
1364 if (es1800_load_format != 2)
1366 sprintf (buf, "/usr/bin/rm %s", loadname);
1370 symbol_file_command (filename, from_tty); /* reading symbol table */
1376 #define NUMCPYBYTES 20
1379 bfd_copy (from_bfd, to_bfd)
1385 char buf[NUMCPYBYTES];
1387 for (p = from_bfd->sections; p != NULL; p = p->next)
1389 printf (" Copying section %s. Size = %x.\n", p->name, p->_cooked_size);
1390 printf (" vma = %x, offset = %x, output_sec = %x\n",
1391 p->vma, p->output_offset, p->output_section);
1392 new = bfd_make_section (to_bfd, p->name);
1393 if (p->_cooked_size &&
1394 !bfd_set_section_size (to_bfd, new, p->_cooked_size))
1396 error ("Wrong BFD size!\n");
1398 if (!bfd_set_section_flags (to_bfd, new, p->flags))
1400 error ("bfd_set_section_flags");
1404 for (i = 0; (i + NUMCPYBYTES) < p->_cooked_size ; i += NUMCPYBYTES)
1406 if (!bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1407 (bfd_size_type) NUMCPYBYTES))
1409 error ("bfd_get_section_contents\n");
1411 if (!bfd_set_section_contents (to_bfd, new, (PTR) buf, (file_ptr) i,
1412 (bfd_size_type) NUMCPYBYTES))
1414 error ("bfd_set_section_contents\n");
1417 bfd_get_section_contents (from_bfd, p, (PTR) buf, (file_ptr) i,
1418 (bfd_size_type) (p->_cooked_size - i));
1419 bfd_set_section_contents (to_bfd, new, (PTR) buf,(file_ptr) i,
1420 (bfd_size_type) (p->_cooked_size - i));
1426 /* Start an process on the es1800 and set inferior_pid to the new
1428 execfile - the file to run
1429 args - arguments passed to the program
1430 env - the environment vector to pass */
1433 es1800_create_inferior (execfile, args, env)
1441 struct expression *expr;
1442 register struct cleanup *old_chain = 0;
1448 error ("Can't pass arguments to remote ES1800 process");
1452 if (query ("Use 'start' as entry point? "))
1454 expr = parse_c_expression ("start");
1455 old_chain = make_cleanup (free_current_contents, &expr);
1456 val = evaluate_expression (expr);
1457 entry_pt = (val->location).address;
1461 printf ("Enter the program's entry point (in hexadecimal): ");
1462 scanf ("%x", &entry_pt);
1466 if (execfile == 0 || exec_bfd == 0)
1468 error ("No exec file specified");
1471 entry_pt = (int) bfd_get_start_address (exec_bfd);
1475 /* Now that we have a child process, make it our target. */
1477 push_target (&es1800_child_ops);
1479 /* The "process" (board) is already stopped awaiting our commands, and
1480 the program is already downloaded. We just set its PC and go. */
1482 inferior_pid = pid; /* Needed for wait_for_inferior below */
1484 clear_proceed_status ();
1486 /* Tell wait_for_inferior that we've started a new process. */
1488 init_wait_for_inferior ();
1490 /* Set up the "saved terminal modes" of the inferior
1491 based on what modes we are starting it with. */
1493 target_terminal_init ();
1495 /* Install inferior's terminal modes. */
1497 target_terminal_inferior ();
1499 /* remote_start (args); */
1500 /* trap_expected = 0; */
1501 /* insert_step_breakpoint (); FIXME, do we need this? */
1503 /* Let 'er rip... */
1504 proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1509 /* The process has died, clean up. */
1512 es1800_mourn_inferior ()
1514 remove_breakpoints ();
1515 unpush_target (&es1800_child_ops);
1516 generic_mourn_inferior (); /* Do all the proper things now */
1519 /* ES1800-protocol specific routines */
1521 /* Keep discarding input from the remote system, until STRING is found.
1522 Let the user break out immediately.
1523 string - the string to expect
1524 nowait - break out if string not the emulator's first respond otherwise
1525 read until string is found (== 0) */
1528 expect (string, nowait)
1543 if (c == toupper (*p))
1558 printf ("\'%s\' expected\n" , string);
1559 printf ("char %d is %d", p - string, c);
1565 /* Keep discarding input until we see the prompt. */
1574 /* Read one character */
1578 /* read from stdin */
1586 printf ("readchar, give one character\n");
1589 #if defined (LOG_FILE)
1590 putc (buf[0] & 0x7f, log_file);
1593 return (buf[0] & 0x7f);
1596 #else /* !DEBUG_STDIN */
1598 /* Read a character from the remote system, doing all the fancy
1606 ch = SERIAL_READCHAR (es1800_desc, timeout);
1608 /* FIXME: doing an error() here will probably cause trouble, at least if from
1610 if (ch == SERIAL_TIMEOUT)
1611 error ("Timeout reading from remote system.");
1612 else if (ch == SERIAL_ERROR)
1613 perror_with_name ("remote read");
1615 #if defined (LOG_FILE)
1616 putc (ch & 0x7f, log_file);
1623 #endif /* DEBUG_STDIN */
1626 /* Send a command to the emulator and save the reply.
1627 Report an error if we get an error reply.
1628 string - the es1800 command
1629 buf - containing the emulator reply on return
1630 len - size of buf */
1633 send_with_reply (string, buf, len)
1638 SERIAL_WRITE (es1800_desc, "\r", 1);
1645 getmessage (buf, len);
1649 /* Send the command in STR to the emulator adding \r. check
1650 the echo for consistency.
1651 string - the es1800 command */
1654 send_command (string)
1658 SERIAL_WRITE (es1800_desc, "\r", 1);
1668 string - the es1800 command */
1676 fprintf (stderr, "Sending: %s\n", string);
1678 SERIAL_WRITE (es1800_desc, string, strlen (string));
1682 /* Read a message from the emulator and store it in BUF.
1683 buf - containing the emulator reply on return
1684 len - size of buf */
1687 getmessage (buf, len)
1693 int prompt_found = 0;
1696 #if defined (LOG_FILE)
1697 /* This is a convenient place to do this. The idea is to do it often
1698 enough that we never lose much data if we terminate abnormally. */
1708 if (len-- < 2) /* char and terminaling NULL */
1710 error ("input buffer overrun\n");
1715 if ((c == '>') && (*(bp - 1) == ' '))
1720 while (!prompt_found);
1725 fprintf (stderr,"message received :%s\n", buf);
1730 download (instream, from_tty, format)
1739 send_command ("SET #2,$1A"); /* reset char = ^Z */
1740 send_command ("SET #3,$11,$13"); /* XON XOFF */
1743 send_command ("SET #26,#2");
1747 send_command ("SET #26,#5"); /* Format=Extended Tekhex */
1749 send_command ("DFB = $10");
1750 send_command ("PUR");
1751 send_command ("CES");
1756 printf (" 0 records loaded...\r");
1758 while (fgets (buf, 160, instream))
1763 printf ("%5d\b\b\b\b\b",++i);
1766 if ((c = readchar ()) != 006)
1768 error ("expected ACK");
1777 /* Additional commands */
1779 #if defined (TIOCGETP) && defined (FNDELAY) && defined (EWOULDBLOCK)
1780 #define PROVIDE_TRANSPARENT
1783 #ifdef PROVIDE_TRANSPARENT
1784 /* Talk directly to the emulator
1785 FIXME, uses busy wait, and is SUNOS (or at least BSD) specific */
1789 es1800_transparent (args, from_tty)
1794 struct sgttyb modebl;
1797 struct sgttyb console_mode_save;
1798 int console_fc_save;
1803 char consolebuf[80];
1805 char es1800_buf[80];
1809 if (es1800_desc == NULL)
1811 printf ("No emulator attached, type emulator-command first\n");
1816 printf ("You are now communicating directly with the ES 1800 emulator.\n");
1817 printf ("To leave this mode (transparent mode), press ^E.\n");
1822 if ((console = open ("/dev/tty", O_RDWR)) == -1)
1824 perror_with_name ("/dev/tty:");
1827 if ((fcflag = fcntl (console, F_GETFL, 0)) == -1)
1829 perror_with_name ("fcntl console");
1832 console_fc_save = fcflag;
1833 fcflag = fcflag | FNDELAY;
1835 if (fcntl (console, F_SETFL, fcflag) == -1)
1837 perror_with_name ("fcntl console");
1840 if (ioctl (console, TIOCGETP, &modebl))
1842 perror_with_name ("ioctl console");
1845 console_mode_save = modebl;
1846 modebl.sg_flags = RAW;
1848 if (ioctl (console, TIOCSETP, &modebl))
1850 perror_with_name ("ioctl console");
1853 if ((fcflag = fcntl (es1800_desc->fd, F_GETFL, 0)) == -1)
1855 perror_with_name ("fcntl serial");
1858 es1800_fc_save = fcflag;
1859 fcflag = fcflag | FNDELAY;
1861 if (fcntl (es1800_desc->fd, F_SETFL, fcflag) == -1)
1863 perror_with_name ("fcntl serial");
1868 cc = read (console, inputbuf, inputcnt);
1871 if ((*inputbuf & 0x7f) == 0x05)
1875 for (i = 0; i < cc; )
1877 es1800_buf[es1800_cnt++] = inputbuf[i++];
1879 if ((cc = SERIAL_WRITE (es1800_desc, es1800_buf, es1800_cnt)) == -1)
1881 perror_with_name ("FEL! write:");
1884 if (es1800_cnt && cc)
1886 for (i = 0; i < es1800_cnt; i++)
1888 es1800_buf[i] = es1800_buf[cc+i];
1892 else if (errno != EWOULDBLOCK)
1894 perror_with_name ("FEL! read:");
1897 cc = read (es1800_desc->fd,inputbuf,inputcnt);
1900 for (i = 0; i < cc; )
1902 consolebuf[consolecnt++] = inputbuf[i++];
1904 if ((cc = write (console,consolebuf,consolecnt)) == -1)
1906 perror_with_name ("FEL! write:");
1909 if (consolecnt && cc)
1911 for (i = 0; i < consolecnt; i++)
1913 consolebuf[i] = consolebuf[cc+i];
1917 else if (errno != EWOULDBLOCK)
1919 perror_with_name ("FEL! read:");
1923 console_fc_save = console_fc_save & !FNDELAY;
1924 if (fcntl (console, F_SETFL, console_fc_save) == -1)
1926 perror_with_name ("FEL! fcntl");
1929 if (ioctl (console, TIOCSETP, &console_mode_save))
1931 perror_with_name ("FEL! ioctl");
1936 if (fcntl (es1800_desc->fd, F_SETFL, es1800_fc_save) == -1)
1938 perror_with_name ("FEL! fcntl");
1944 #endif /* PROVIDE_TRANSPARENT */
1947 es1800_init_break (args, from_tty)
1951 CORE_ADDR memaddress = 0;
1960 error_no_arg ("a trap vector");
1963 if (!(space_index = strchr (args, ' ')))
1965 error ("Two arguments needed (trap vector and address of break routine).\n");
1968 *space_index = '\0';
1970 es1800_break_vec = strtol (args, (char **) NULL, 0);
1971 es1800_break_address = parse_and_eval_address (space_index + 1);
1973 es1800_create_break_insn (es1800_break_insn, es1800_break_vec);
1977 send_with_reply ("VBR ", buf, sizeof (buf));
1979 for (k = 0; k < 4; k++)
1981 if ((p[k*2 + 1] == 0) || (p[k*2 + 2] == 0))
1983 error ("Emulator reply is too short: %s", buf);
1985 base_addr[k] = (fromhex (p[k*2 + 1]) * 16) + fromhex (p[k*2 + 2]);
1987 /* base addr of exception vector table */
1988 memaddress = *((CORE_ADDR *) base_addr);
1991 memaddress += (es1800_break_vec + 32) * 4; /* address of trap vector */
1993 sprintf (buf, "@.L%lx=$%lx", memaddress, es1800_break_address);
1994 send_command (buf); /* set the address of the break routine in the */
1997 sprintf (buf, "@.L%lx=$4E714E71", es1800_break_address); /* NOP; NOP */
1999 sprintf (buf, "@.L%lx=$4E714E73", es1800_break_address + 4); /* NOP; RTE */
2002 sprintf (buf, "AC2=$%lx", es1800_break_address + 4);
2003 /* breakpoint at es1800-break_address */
2005 send_command ("WHEN AC2 THEN BRK"); /* ie in exception routine */
2009 printf ("Breakpoint (trap $%x) routine at address: %lx\n",
2010 es1800_break_vec, es1800_break_address);
2015 es1800_child_open (arg, from_tty)
2019 error ("Use the \"run\" command to start a child process.");
2023 es1800_child_detach (args, from_tty)
2029 error ("Argument given to \"detach\" when remotely debugging.");
2035 printf ("Ending debugging the process %d.\n", inferior_pid);
2040 /* Define the target subroutine names */
2042 static struct target_ops es1800_ops =
2044 "es1800", /* to_shortname */
2046 "Remote serial target in ES1800-emulator protocol",
2048 "Remote debugging on the es1800 emulator via a serial line.\n\
2049 Specify the serial device it is connected to (e.g. /dev/ttya).",
2050 es1800_open, /* to_open */
2051 es1800_close, /* to_close */
2052 es1800_attach, /* to_attach */
2053 es1800_detach, /* to_detach */
2054 es1800_resume, /* to_resume */
2056 NULL, /* to_fetch_registers */
2057 NULL, /* to_store_registers */
2058 es1800_prepare_to_store, /* to_prepare_to_store */
2059 es1800_xfer_inferior_memory, /* to_xfer_memory */
2060 es1800_files_info, /* to_files_info */
2061 es1800_insert_breakpoint, /* to_insert_breakpoint */
2062 es1800_remove_breakpoint, /* to_remove_breakpoint */
2063 NULL, /* to_terminal_init */
2064 NULL, /* to_terminal_inferior */
2065 NULL, /* to_terminal_ours_for_output */
2066 NULL, /* to_terminal_ours */
2067 NULL, /* to_terminal_info */
2069 es1800_load, /* to_load */
2070 NULL, /* to_lookup_symbol */
2071 es1800_create_inferior, /* to_create_inferior */
2072 NULL, /* to_mourn_inferior */
2074 0, /* notice_signals */
2075 core_stratum, /* to_stratum */
2077 0, /* to_has_all_memory */
2078 1, /* to_has_memory */
2079 0, /* to_has_stack */
2080 0, /* to_has_registers */
2081 0, /* to_has_execution */
2082 NULL, /* to_sections */
2083 NULL, /* to_sections_end */
2084 OPS_MAGIC /* to_magic (always last) */
2087 /* Define the target subroutine names */
2089 static struct target_ops es1800_child_ops =
2091 "es1800_process", /* to_shortname */
2093 "Remote serial target in ES1800-emulator protocol",
2095 "Remote debugging on the es1800 emulator via a serial line.\n\
2096 Specify the serial device it is connected to (e.g. /dev/ttya).",
2097 es1800_child_open, /* to_open */
2098 NULL, /* to_close */
2099 es1800_attach, /* to_attach */
2100 es1800_child_detach, /* to_detach */
2101 es1800_resume, /* to_resume */
2102 es1800_wait, /* to_wait */
2103 es1800_fetch_register, /* to_fetch_registers */
2104 es1800_store_register, /* to_store_registers */
2105 es1800_prepare_to_store, /* to_prepare_to_store */
2106 es1800_xfer_inferior_memory, /* to_xfer_memory */
2107 es1800_files_info, /* to_files_info */
2108 es1800_insert_breakpoint, /* to_insert_breakpoint */
2109 es1800_remove_breakpoint, /* to_remove_breakpoint */
2110 NULL, /* to_terminal_init */
2111 NULL, /* to_terminal_inferior */
2112 NULL, /* to_terminal_ours_for_output */
2113 NULL, /* to_terminal_ours */
2114 NULL, /* to_terminal_info */
2115 es1800_kill, /* to_kill */
2116 es1800_load, /* to_load */
2117 NULL, /* to_lookup_symbol */
2118 es1800_create_inferior, /* to_create_inferior */
2119 es1800_mourn_inferior, /* to_mourn_inferior */
2121 0, /* notice_signals */
2122 process_stratum, /* to_stratum */
2124 1, /* to_has_all_memory */
2125 1, /* to_has_memory */
2126 1, /* to_has_stack */
2127 1, /* to_has_registers */
2128 1, /* to_has_execution */
2129 NULL, /* to_sections */
2130 NULL, /* to_sections_end */
2131 OPS_MAGIC /* to_magic (always last) */
2135 _initialize_es1800 ()
2137 add_target (&es1800_ops);
2138 add_target (&es1800_child_ops);
2139 #ifdef PROVIDE_TRANSPARENT
2140 add_com ("transparent", class_support, es1800_transparent,
2141 "Start transparent communication with the ES 1800 emulator.");
2142 #endif /* PROVIDE_TRANSPARENT */
2143 add_com ("init_break", class_support, es1800_init_break,
2144 "Download break routine and initialize break facility on ES 1800");