1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* This file was derived from remote-eb.c, which did a similar job, but for
21 an AMD-29K running EBMON. That file was in turn derived from remote.c
22 as mentioned in the following comment (left in for comic relief):
24 "This is like remote.c but is for a different situation--
25 having a PC running os9000 hook up with a unix machine with
26 a serial line, and running ctty com2 on the PC. os9000 has a debug
27 monitor called ROMBUG running. Not to mention that the PC
28 has PC/NFS, so it can access the same executables that gdb can,
29 over the net in real time."
31 In reality, this module talks to a debug monitor called 'ROMBUG', which
32 We communicate with ROMBUG via a direct serial line, the network version
33 of ROMBUG is not available yet.
40 #ifdef ANSI_PROTOTYPES
46 #include "gdb_string.h"
47 #include <sys/types.h>
51 #include "remote-utils.h"
55 #include "gdb-stabs.h"
57 struct monitor_ops *current_monitor;
58 struct cmd_list_element *showlist;
59 extern struct target_ops rombug_ops; /* Forward declaration */
60 extern struct monitor_ops rombug_cmds; /* Forward declaration */
61 extern struct cmd_list_element *setlist;
62 extern struct cmd_list_element *unsetlist;
63 extern int attach_flag;
65 static void rombug_close();
66 static void rombug_fetch_register();
67 static void rombug_fetch_registers();
68 static void rombug_store_register();
70 static int sr_get_debug(); /* flag set by "set remotedebug" */
72 static int hashmark; /* flag set by "set hash" */
73 static int rombug_is_open = 0;
75 /* FIXME: Replace with sr_get_debug (). */
76 #define LOG_FILE "monitor.log"
78 static int monitor_log = 0;
79 static int tty_xon = 0;
80 static int tty_xoff = 0;
82 static int timeout = 10;
83 static int is_trace_mode = 0;
84 /* Descriptor for I/O to remote machine. Initialize it to NULL*/
85 static serial_t monitor_desc = NULL;
87 static CORE_ADDR bufaddr = 0;
88 static int buflen = 0;
89 static char readbuf[16];
91 /* Send data to monitor. Works just like printf. */
93 #ifdef ANSI_PROTOTYPES
94 printf_monitor(char *pattern, ...)
96 printf_monitor(va_alist)
104 #ifdef ANSI_PROTOTYPES
105 va_start (args, pattern);
109 pattern = va_arg(args, char *);
112 vsprintf(buf, pattern, args);
115 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
116 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
119 /* Read a character from the remote system, doing all the fancy timeout stuff*/
126 c = SERIAL_READCHAR(monitor_desc, timeout);
131 if (monitor_log && isascii(c))
132 putc(c & 0x7f, log_file);
137 if (c == SERIAL_TIMEOUT)
140 return c; /* Polls shouldn't generate timeout errors */
142 error("Timeout reading from remote system.");
145 perror_with_name("remote-monitor");
148 /* Scan input from the remote system, until STRING is found. If DISCARD is
149 non-zero, then discard non-matching input, else print it out.
150 Let the user break out immediately. */
152 expect(string, discard)
160 printf ("Expecting \"%s\"\n", string);
165 c = readchar(timeout);
174 printf ("\nMatched\n");
182 fwrite(string, 1, (p - 1) - string, stdout);
191 /* Keep discarding input until we see the ROMBUG prompt.
193 The convention for dealing with the prompt is that you
195 o *then* wait for the prompt.
197 Thus the last thing that a procedure does with the serial line
198 will be an expect_prompt(). Exception: rombug_resume does not
199 wait for the prompt, because the terminal is being handed over
200 to the inferior. However, the next thing which happens after that
201 is a rombug_wait which does wait for the prompt.
202 Note that this includes abnormal exit, e.g. error(). This is
203 necessary to prevent getting into states from which we can't
206 expect_prompt(discard)
210 /* This is a convenient place to do this. The idea is to do it often
211 enough that we never lose much data if we terminate abnormally. */
215 expect("trace", discard);
217 expect (PROMPT, discard);
221 /* Get a hex digit from the remote system & return its value.
222 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
224 get_hex_digit(ignore_space)
230 ch = readchar(timeout);
231 if (ch >= '0' && ch <= '9')
233 else if (ch >= 'A' && ch <= 'F')
234 return ch - 'A' + 10;
235 else if (ch >= 'a' && ch <= 'f')
236 return ch - 'a' + 10;
237 else if (ch == ' ' && ignore_space)
242 error("Invalid hex digit from remote system.");
247 /* Get a byte from monitor and put it in *BYT. Accept any number
255 val = get_hex_digit (1) << 4;
256 val |= get_hex_digit (0);
260 /* Get N 32-bit words from remote, each preceded by a space,
261 and put them in registers starting at REGNO. */
263 get_hex_regs (n, regno)
271 for (i = 0; i < n; i++)
276 for (j = 0; j < 4; j++)
279 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
280 val = (val << 8) + b;
282 val = val + (b << (j*8));
284 supply_register (regno++, (char *) &val);
288 /* This is called not only when we first attach, but also when the
289 user types "run" after having attached. */
291 rombug_create_inferior (execfile, args, env)
299 error("Can't pass arguments to remote ROMBUG process");
301 if (execfile == 0 || exec_bfd == 0)
302 error("No exec file specified");
304 entry_pt = (int) bfd_get_start_address (exec_bfd);
307 fputs ("\nIn Create_inferior()", log_file);
310 /* The "process" (board) is already stopped awaiting our commands, and
311 the program is already downloaded. We just set its PC and go. */
313 init_wait_for_inferior ();
314 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
317 /* Open a connection to a remote debugger.
318 NAME is the filename used for communication. */
320 static char dev_name[100];
323 rombug_open(args, from_tty)
328 error ("Use `target RomBug DEVICE-NAME' to use a serial port, or \n\
329 `target RomBug HOST-NAME:PORT-NUMBER' to use a network connection.");
331 target_preopen(from_tty);
334 unpush_target(&rombug_ops);
336 strcpy(dev_name, args);
337 monitor_desc = SERIAL_OPEN(dev_name);
338 if (monitor_desc == NULL)
339 perror_with_name(dev_name);
341 /* if baud rate is set by 'set remotebaud' */
342 if (SERIAL_SETBAUDRATE (monitor_desc, sr_get_baud_rate()))
344 SERIAL_CLOSE (monitor_desc);
345 perror_with_name ("RomBug");
347 SERIAL_RAW(monitor_desc);
348 if (tty_xon || tty_xoff)
350 struct hardware_ttystate { struct termios t;} *tty_s;
352 tty_s =(struct hardware_ttystate *)SERIAL_GET_TTY_STATE(monitor_desc);
353 if (tty_xon) tty_s->t.c_iflag |= IXON;
354 if (tty_xoff) tty_s->t.c_iflag |= IXOFF;
355 SERIAL_SET_TTY_STATE(monitor_desc, (serial_ttystate) tty_s);
360 log_file = fopen (LOG_FILE, "w");
361 if (log_file == NULL)
362 perror_with_name (LOG_FILE);
364 push_monitor (&rombug_cmds);
365 printf_monitor("\r"); /* CR wakes up monitor */
367 push_target (&rombug_ops);
371 printf("Remote %s connected to %s\n", target_shortname,
374 rombug_fetch_registers();
376 printf_monitor ("ov e \r");
383 * Close out all files and local state before this target loses control.
387 rombug_close (quitting)
390 if (rombug_is_open) {
391 SERIAL_CLOSE(monitor_desc);
397 if (ferror(log_file))
398 fprintf(stderr, "Error writing log file.\n");
399 if (fclose(log_file) != 0)
400 fprintf(stderr, "Error closing log file.\n");
406 rombug_link(mod_name, text_reloc)
408 CORE_ADDR *text_reloc;
414 printf_monitor("l %s \r", mod_name);
416 printf_monitor(".r \r");
417 expect(REG_DELIM, 1);
418 for (i=0; i <= 7; i++)
421 for (j = 0; j < 4; j++)
424 val = (val << 8) + b;
432 /* Terminate the open connection to the remote debugger.
433 Use this when you want to detach and do something else
436 rombug_detach (from_tty)
440 printf_monitor (GO_CMD);
443 pop_target(); /* calls rombug_close to do the real work */
445 printf ("Ending remote %s debugging\n", target_shortname);
449 * Tell the remote machine to resume.
452 rombug_resume (pid, step, sig)
454 enum target_signal sig;
457 fprintf (log_file, "\nIn Resume (step=%d, sig=%d)\n", step, sig);
462 printf_monitor (STEP_CMD);
463 /* wait for the echo. **
464 expect (STEP_CMD, 1);
469 printf_monitor (GO_CMD);
470 /* swallow the echo. **
479 * Wait until the remote machine stops, then return,
480 * storing status in status just as `wait' would.
484 rombug_wait (pid, status)
486 struct target_waitstatus *status;
488 int old_timeout = timeout;
489 struct section_offsets *offs;
491 struct obj_section *obj_sec;
494 fputs ("\nIn wait ()", log_file);
496 status->kind = TARGET_WAITKIND_EXITED;
497 status->value.integer = 0;
499 timeout = -1; /* Don't time out -- user program is running. */
500 expect ("eax:", 0); /* output any message before register display */
501 expect_prompt(1); /* Wait for prompt, outputting extraneous text */
503 status->kind = TARGET_WAITKIND_STOPPED;
504 status->value.sig = TARGET_SIGNAL_TRAP;
505 timeout = old_timeout;
506 rombug_fetch_registers();
509 pc = read_register(PC_REGNUM);
510 addr = read_register(DATABASE_REG);
511 obj_sec = find_pc_section (pc);
514 if (obj_sec->objfile != symfile_objfile)
515 new_symfile_objfile(obj_sec->objfile, 1, 0);
516 offs = ((struct section_offsets *)
517 alloca (sizeof (struct section_offsets)
518 + (symfile_objfile->num_sections * sizeof (offs->offsets))));
519 memcpy (offs, symfile_objfile->section_offsets,
520 (sizeof (struct section_offsets) +
521 (symfile_objfile->num_sections * sizeof (offs->offsets))));
522 ANOFFSET (offs, SECT_OFF_DATA) = addr;
523 ANOFFSET (offs, SECT_OFF_BSS) = addr;
525 objfile_relocate(symfile_objfile, offs);
531 /* Return the name of register number regno in the form input and output by
532 monitor. Currently, register_names just happens to contain exactly what
533 monitor wants. Lets take advantage of that just as long as possible! */
548 for (p = reg_names[regno]; *p; p++)
552 p = (char *)reg_names[regno];
559 /* read the remote registers into the block regs. */
562 rombug_fetch_registers ()
568 printf_monitor (GET_REG);
579 for (regno = 8; regno <= 15; regno++)
581 expect(REG_DELIM, 1);
582 if (regno >= 8 && regno <= 13)
585 for (j = 0; j < 2; j++)
588 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
589 val = (val << 8) + b;
591 val = val + (b << (j*8));
594 if (regno == 8) i = 10;
595 if (regno >= 9 && regno <= 12) i = regno + 3;
596 if (regno == 13) i = 11;
597 supply_register (i, (char *) &val);
599 else if (regno == 14)
601 get_hex_regs(1, PC_REGNUM);
603 else if (regno == 15)
610 supply_register(regno, (char *) &val);
617 /* Fetch register REGNO, or all registers if REGNO is -1.
618 Returns errno value. */
620 rombug_fetch_register (regno)
627 fprintf (log_file, "\nIn Fetch Register (reg=%s)\n", get_reg_name (regno));
633 rombug_fetch_registers ();
637 char *name = get_reg_name (regno);
638 printf_monitor (GET_REG);
639 if (regno >= 10 && regno <= 15)
644 expect (REG_DELIM, 1);
646 for (j = 0; j < 2; j++)
649 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
650 val = (val << 8) + b;
652 val = val + (b << (j*8));
654 supply_register (regno, (char *) &val);
656 else if (regno == 8 || regno == 9)
662 expect (REG_DELIM, 1);
663 get_hex_regs (1, regno);
668 expect (REG_DELIM, 1);
684 /* Store the remote registers from the contents of the block REGS. */
687 rombug_store_registers ()
691 for (regno = 0; regno <= PC_REGNUM; regno++)
692 rombug_store_register(regno);
694 registers_changed ();
697 /* Store register REGNO, or all if REGNO == 0.
698 return errno value. */
700 rombug_store_register (regno)
706 fprintf (log_file, "\nIn Store_register (regno=%d)\n", regno);
709 rombug_store_registers ();
713 printf ("Setting register %s to 0x%x\n", get_reg_name (regno), read_register (regno));
715 name = get_reg_name(regno);
716 if (name == 0) return;
717 printf_monitor (SET_REG, name, read_register (regno));
724 /* Get ready to modify the registers array. On machines which store
725 individual registers, this doesn't need to do anything. On machines
726 which store all the registers in one fell swoop, this makes sure
727 that registers contains all the registers from the program being
731 rombug_prepare_to_store ()
733 /* Do nothing, since we can store individual regs */
739 printf ("\tAttached to %s at %d baud.\n",
740 dev_name, sr_get_baud_rate());
743 /* Copy LEN bytes of data from debugger memory at MYADDR
744 to inferior's memory at MEMADDR. Returns length moved. */
746 rombug_write_inferior_memory (memaddr, myaddr, len)
748 unsigned char *myaddr;
755 fprintf (log_file, "\nIn Write_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
757 printf_monitor (MEM_SET_CMD, memaddr);
758 for (i = 0; i < len; i++)
760 expect (CMD_DELIM, 1);
761 printf_monitor ("%x \r", myaddr[i]);
763 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
765 expect (CMD_DELIM, 1);
767 printf_monitor (CMD_END);
776 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
777 at debugger address MYADDR. Returns length moved. */
779 rombug_read_inferior_memory(memaddr, myaddr, len)
786 /* Number of bytes read so far. */
789 /* Starting address of this pass. */
790 unsigned long startaddr;
792 /* Number of bytes to read in this pass. */
796 fprintf (log_file, "\nIn Read_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
798 /* Note that this code works correctly if startaddr is just less
799 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
800 thing). That is, something like
801 rombug_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
802 works--it never adds len To memaddr and gets 0. */
803 /* However, something like
804 rombug_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
805 doesn't need to work. Detect it and give up if there's an attempt
807 if (((memaddr - 1) + len) < memaddr) {
811 if (bufaddr <= memaddr && (memaddr+len) <= (bufaddr+buflen))
813 memcpy(myaddr, &readbuf[memaddr-bufaddr], len);
822 if ((startaddr % 16) != 0)
823 len_this_pass -= startaddr % 16;
824 if (len_this_pass > (len - count))
825 len_this_pass = (len - count);
827 printf ("\nDisplay %d bytes at %x\n", len_this_pass, startaddr);
829 printf_monitor (MEM_DIS_CMD, startaddr, 8);
831 for (i = 0; i < 16; i++)
833 get_hex_byte (&readbuf[i]);
837 memcpy(&myaddr[count], readbuf, len_this_pass);
838 count += len_this_pass;
839 startaddr += len_this_pass;
840 expect(CMD_DELIM, 1);
843 printf_monitor (CMD_END);
850 /* FIXME-someday! merge these two. */
852 rombug_xfer_inferior_memory (memaddr, myaddr, len, write, target)
857 struct target_ops *target; /* ignored */
860 return rombug_write_inferior_memory (memaddr, myaddr, len);
862 return rombug_read_inferior_memory (memaddr, myaddr, len);
866 rombug_kill (args, from_tty)
870 return; /* ignore attempts to kill target system */
873 /* Clean up when a program exits.
874 The program actually lives on in the remote processor's RAM, and may be
875 run again without a download. Don't leave it full of breakpoint
879 rombug_mourn_inferior ()
881 remove_breakpoints ();
882 generic_mourn_inferior (); /* Do all the proper things now */
885 #define MAX_MONITOR_BREAKPOINTS 16
887 extern int memory_breakpoint_size;
888 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
891 rombug_insert_breakpoint (addr, shadow)
898 fprintf (log_file, "\nIn Insert_breakpoint (addr=%x)\n", addr);
900 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++)
901 if (breakaddr[i] == 0)
905 printf ("Breakpoint at %x\n", addr);
906 rombug_read_inferior_memory(addr, shadow, memory_breakpoint_size);
907 printf_monitor(SET_BREAK_CMD, addr);
913 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
918 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
921 rombug_remove_breakpoint (addr, shadow)
928 fprintf (log_file, "\nIn Remove_breakpoint (addr=%x)\n", addr);
930 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++)
931 if (breakaddr[i] == addr)
934 printf_monitor(CLR_BREAK_CMD, addr);
940 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
944 /* Load a file. This is usually an srecord, which is ascii. No
945 protocol, just sent line by line. */
947 #define DOWNLOAD_LINE_SIZE 100
952 /* this part comment out for os9* */
955 char buf[DOWNLOAD_LINE_SIZE];
959 printf ("Loading %s to monitor\n", arg);
961 download = fopen (arg, "r");
962 if (download == NULL)
964 error (sprintf (buf, "%s Does not exist", arg));
968 printf_monitor (LOAD_CMD);
969 /* expect ("Waiting for S-records from host... ", 1); */
971 while (!feof (download))
973 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
980 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
981 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
985 while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */
986 if (bytes_read < DOWNLOAD_LINE_SIZE)
988 if (!feof (download))
989 error ("Only read %d bytes\n", bytes_read);
998 if (!feof (download))
999 error ("Never got EOF while downloading");
1004 /* Put a command string, in args, out to MONITOR.
1005 Output from MONITOR is placed on the users terminal until the prompt
1009 rombug_command (args, fromtty)
1013 if (monitor_desc == NULL)
1014 error("monitor target not open.");
1017 fprintf (log_file, "\nIn command (args=%s)\n", args);
1020 error("Missing command.");
1022 printf_monitor("%s\r", args);
1027 /* Connect the user directly to MONITOR. This command acts just like the
1028 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
1030 static struct ttystate ttystate;
1034 { printf("\r\n[Exiting connect mode]\r\n");
1035 /*SERIAL_RESTORE(0, &ttystate);*/
1039 connect_command (args, fromtty)
1050 if (monitor_desc == NULL)
1051 error("monitor target not open.");
1054 fprintf("This command takes no args. They have been ignored.\n");
1056 printf("[Entering connect mode. Use ~. or ~^D to escape]\n");
1058 serial_raw(0, &ttystate);
1060 make_cleanup(cleanup_tty, 0);
1068 FD_SET(0, &readfds);
1069 FD_SET(monitor_desc, &readfds);
1070 numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
1072 while (numfds == 0);
1075 perror_with_name("select");
1077 if (FD_ISSET(0, &readfds))
1078 { /* tty input, send to monitor */
1081 perror_with_name("connect");
1083 printf_monitor("%c", c);
1097 if (c == '.' || c == '\004')
1104 if (FD_ISSET(monitor_desc, &readfds))
1120 * Define the monitor command strings. Since these are passed directly
1121 * through to a printf style function, we need can include formatting
1122 * strings. We also need a CR or LF on the end.
1124 struct monitor_ops rombug_cmds = {
1125 "g \r", /* execute or usually GO command */
1126 "g \r", /* continue command */
1127 "t \r", /* single step */
1128 "b %x\r", /* set a breakpoint */
1129 "k %x\r", /* clear a breakpoint */
1130 "c %x\r", /* set memory to a value */
1131 "d %x %d\r", /* display memory */
1132 "$%08X", /* prompt memory commands use */
1133 ".%s %x\r", /* set a register */
1134 ":", /* delimiter between registers */
1135 ". \r", /* read a register */
1136 "mf \r", /* download command */
1137 "RomBug: ", /* monitor command prompt */
1138 ": ", /* end-of-command delimitor */
1139 ".\r" /* optional command terminator */
1142 struct target_ops rombug_ops = {
1144 "Microware's ROMBUG debug monitor",
1145 "Use a remote computer running the ROMBUG debug monitor.\n\
1146 Specify the serial device it is connected to (e.g. /dev/ttya).",
1153 rombug_fetch_register,
1154 rombug_store_register,
1155 rombug_prepare_to_store,
1156 rombug_xfer_inferior_memory,
1158 rombug_insert_breakpoint,
1159 rombug_remove_breakpoint, /* Breakpoints */
1164 0, /* Terminal handling */
1166 rombug_load, /* load */
1167 rombug_link, /* lookup_symbol */
1168 rombug_create_inferior,
1169 rombug_mourn_inferior,
1171 0, /* notice_signals */
1179 1, /* has execution */
1181 0, /* Section pointers */
1182 OPS_MAGIC, /* Always the last thing */
1186 _initialize_remote_os9k ()
1188 add_target (&rombug_ops);
1191 add_set_cmd ("hash", no_class, var_boolean, (char *)&hashmark,
1192 "Set display of activity while downloading a file.\nWhen enabled, a period \'.\' is displayed.",
1197 add_set_cmd ("timeout", no_class, var_zinteger,
1199 "Set timeout in seconds for remote MIPS serial I/O.",
1204 add_set_cmd ("remotelog", no_class, var_zinteger,
1205 (char *) &monitor_log,
1206 "Set monitor activity log on(=1) or off(=0).",
1211 add_set_cmd ("remotexon", no_class, var_zinteger,
1213 "Set remote tty line XON control",
1218 add_set_cmd ("remotexoff", no_class, var_zinteger,
1220 "Set remote tty line XOFF control",
1224 add_com ("rombug <command>", class_obscure, rombug_command,
1225 "Send a command to the debug monitor.");
1227 add_com ("connect", class_obscure, connect_command,
1228 "Connect the terminal directly up to a serial based command monitor.\nUse <CR>~. or <CR>~^D to break out.");