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., 675 Mass Ave, Cambridge, MA 02139, 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.
43 #include <sys/types.h>
47 #include "remote-utils.h"
51 #include "gdb-stabs.h"
54 # define TERMINAL struct termios
56 # define TERMINAL struct sgttyb
59 struct monitor_ops *current_monitor;
60 struct cmd_list_element *showlist;
61 extern struct target_ops rombug_ops; /* Forward declaration */
62 extern struct monitor_ops rombug_cmds; /* Forward declaration */
63 extern struct cmd_list_element *setlist;
64 extern struct cmd_list_element *unsetlist;
65 extern int attach_flag;
67 static void rombug_close();
68 static void rombug_fetch_register();
69 static void rombug_fetch_registers();
70 static void rombug_store_register();
72 static int sr_get_debug(); /* flag set by "set remotedebug" */
74 static int hashmark; /* flag set by "set hash" */
75 static int rombug_is_open = 0;
77 /* FIXME: Replace with sr_get_debug (). */
78 #define LOG_FILE "monitor.log"
79 #if defined (LOG_FILE)
83 static int timeout = 5;
84 static int is_trace_mode = 0;
85 /* Descriptor for I/O to remote machine. Initialize it to NULL*/
86 static serial_t monitor_desc = NULL;
88 /* Send data to monitor. Works just like printf. */
90 printf_monitor(va_alist)
100 pattern = va_arg(args, char *);
102 vsprintf(buf, pattern, args);
104 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
105 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
108 /* Read a character from the remote system, doing all the fancy timeout stuff*/
115 c = SERIAL_READCHAR(monitor_desc, timeout);
122 putc(c & 0x7f, log_file);
128 if (c == SERIAL_TIMEOUT)
131 return c; /* Polls shouldn't generate timeout errors */
133 error("Timeout reading from remote system.");
136 perror_with_name("remote-monitor");
139 /* Scan input from the remote system, until STRING is found. If DISCARD is
140 non-zero, then discard non-matching input, else print it out.
141 Let the user break out immediately. */
143 expect(string, discard)
151 printf ("Expecting \"%s\"\n", string);
156 c = readchar(timeout);
165 printf ("\nMatched\n");
173 fwrite(string, 1, (p - 1) - string, stdout);
182 /* Keep discarding input until we see the ROMBUG prompt.
184 The convention for dealing with the prompt is that you
186 o *then* wait for the prompt.
188 Thus the last thing that a procedure does with the serial line
189 will be an expect_prompt(). Exception: rombug_resume does not
190 wait for the prompt, because the terminal is being handed over
191 to the inferior. However, the next thing which happens after that
192 is a rombug_wait which does wait for the prompt.
193 Note that this includes abnormal exit, e.g. error(). This is
194 necessary to prevent getting into states from which we can't
197 expect_prompt(discard)
200 #if defined (LOG_FILE)
201 /* This is a convenient place to do this. The idea is to do it often
202 enough that we never lose much data if we terminate abnormally. */
206 expect("trace", discard);
209 expect (PROMPT, discard);
213 /* Get a hex digit from the remote system & return its value.
214 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
216 get_hex_digit(ignore_space)
222 ch = readchar(timeout);
223 if (ch >= '0' && ch <= '9')
225 else if (ch >= 'A' && ch <= 'F')
226 return ch - 'A' + 10;
227 else if (ch >= 'a' && ch <= 'f')
228 return ch - 'a' + 10;
229 else if (ch == ' ' && ignore_space)
234 error("Invalid hex digit from remote system.");
239 /* Get a byte from monitor and put it in *BYT. Accept any number
247 val = get_hex_digit (1) << 4;
248 val |= get_hex_digit (0);
252 /* Get N 32-bit words from remote, each preceded by a space,
253 and put them in registers starting at REGNO. */
255 get_hex_regs (n, regno)
263 for (i = 0; i < n; i++)
268 for (j = 0; j < 4; j++)
270 #if TARGET_BYTE_ORDER == BIG_ENDIAN
272 val = (val << 8) + b;
275 val = val + (b << (j*8));
278 supply_register (regno++, (char *) &val);
282 /* This is called not only when we first attach, but also when the
283 user types "run" after having attached. */
285 rombug_create_inferior (execfile, args, env)
292 /* Nonzero value indicates that a process really is running. */
296 error("Can't pass arguments to remote ROMBUG process");
298 if (execfile == 0 || exec_bfd == 0)
299 error("No exec file specified");
301 entry_pt = (int) bfd_get_start_address (exec_bfd);
304 fputs ("\nIn Create_inferior()", log_file);
307 /* The "process" (board) is already stopped awaiting our commands, and
308 the program is already downloaded. We just set its PC and go. */
310 init_wait_for_inferior ();
311 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
314 /* Open a connection to a remote debugger.
315 NAME is the filename used for communication. */
317 static int baudrate = 9600;
318 static char dev_name[100];
321 rombug_open(args, from_tty)
326 error ("Use `target RomBug DEVICE-NAME' to use a serial port, or \n\
327 `target RomBug HOST-NAME:PORT-NUMBER' to use a network connection.");
329 target_preopen(from_tty);
332 unpush_target(&rombug_ops);
334 strcpy(dev_name, args);
335 monitor_desc = SERIAL_OPEN(dev_name);
336 if (monitor_desc == NULL)
337 perror_with_name(dev_name);
339 /* The baud rate was specified when GDB was started. ***
340 if (SERIAL_SETBAUDRATE (monitor_desc, sr_get_baud_rate()))
342 SERIAL_CLOSE (monitor_desc);
343 perror_with_name ("RomBug");
346 SERIAL_RAW(monitor_desc);
349 #if defined (LOG_FILE)
350 log_file = fopen (LOG_FILE, "w");
351 if (log_file == NULL)
352 perror_with_name (LOG_FILE);
355 push_monitor (&rombug_cmds);
357 printf_monitor("\r"); /* CR wakes up monitor */
360 push_target (&rombug_ops);
362 printf("Remote %s connected to %s\n", target_shortname,
366 rombug_fetch_registers();
370 * Close out all files and local state before this target loses control.
374 rombug_close (quitting)
377 if (rombug_is_open) {
378 SERIAL_CLOSE(monitor_desc);
383 #if defined (LOG_FILE)
385 if (ferror(log_file))
386 fprintf(stderr, "Error writing log file.\n");
387 if (fclose(log_file) != 0)
388 fprintf(stderr, "Error closing log file.\n");
395 rombug_link(mod_name, text_reloc)
397 CORE_ADDR *text_reloc;
403 printf_monitor("l %s \r", mod_name);
405 printf_monitor(".r \r");
406 expect(REG_DELIM, 1);
407 for (i=0; i <= 7; i++)
410 for (j = 0; j < 4; j++)
413 val = (val << 8) + b;
421 /* Terminate the open connection to the remote debugger.
422 Use this when you want to detach and do something else
425 rombug_detach (from_tty)
429 printf_monitor (GO_CMD);
432 pop_target(); /* calls rombug_close to do the real work */
434 printf ("Ending remote %s debugging\n", target_shortname);
438 * Tell the remote machine to resume.
441 rombug_resume (pid, step, sig)
443 enum target_signal sig;
446 fprintf (log_file, "\nIn Resume (step=%d, sig=%d)\n", step, sig);
452 printf_monitor (STEP_CMD);
453 /* wait for the echo. */
454 expect (STEP_CMD, 1);
458 printf_monitor (GO_CMD);
459 /* swallow the echo. */
465 * Wait until the remote machine stops, then return,
466 * storing status in status just as `wait' would.
470 rombug_wait (pid, status)
472 struct target_waitstatus *status;
474 int old_timeout = timeout;
475 struct section_offsets *offs;
477 struct obj_section *obj_sec;
480 fputs ("\nIn wait ()", log_file);
483 status->kind = TARGET_WAITKIND_EXITED;
484 status->value.integer = 0;
486 timeout = 0; /* Don't time out -- user program is running. */
487 expect_prompt(1); /* Wait for prompt, outputting extraneous text */
489 status->kind = TARGET_WAITKIND_STOPPED;
490 status->value.sig = TARGET_SIGNAL_TRAP;
491 timeout = old_timeout;
492 rombug_fetch_registers();
493 pc = read_register(PC_REGNUM);
494 addr = read_register(DATABASE_REG);
496 obj_sec = find_pc_section (pc);
498 new_symfile_objfile (obj_sec, 1, 0);
500 offs = ((struct section_offsets *)
501 alloca (sizeof (struct section_offsets)
502 + (symfile_objfile->num_sections * sizeof (offs->offsets))));
503 memcpy (offs, symfile_objfile->section_offsets,
504 (sizeof (struct section_offsets) +
505 (symfile_objfile->num_sections * sizeof (offs->offsets))));
506 ANOFFSET (offs, SECT_OFF_DATA) = addr;
507 ANOFFSET (offs, SECT_OFF_BSS) = addr;
509 /* Because we only set offsets for DATA and BSS sections, and the other
510 offsets are unchanged from ->section_offsets, the other sections won't
512 objfile_relocate (symfile_objfile, offs);
517 /* Return the name of register number regno in the form input and output by
518 monitor. Currently, register_names just happens to contain exactly what
519 monitor wants. Lets take advantage of that just as long as possible! */
534 for (p = reg_names[regno]; *p; p++)
538 p = (char *)reg_names[regno];
545 /* read the remote registers into the block regs. */
548 rombug_fetch_registers ()
554 printf_monitor (GET_REG);
565 for (regno = 8; regno <= 15; regno++)
567 expect(REG_DELIM, 1);
568 if (regno >= 8 && regno <= 13)
571 for (j = 0; j < 2; j++)
573 #if TARGET_BYTE_ORDER == BIG_ENDIAN
575 val = (val << 8) + b;
578 val = val + (b << (j*8));
582 if (regno == 8) i = 10;
583 if (regno >= 9 && regno <= 12) i = regno + 3;
584 if (regno == 13) i = 11;
585 supply_register (i, (char *) &val);
587 else if (regno == 14)
589 get_hex_regs(1, PC_REGNUM);
591 else if (regno == 15)
598 supply_register(regno, (char *) &val);
604 /* Fetch register REGNO, or all registers if REGNO is -1.
605 Returns errno value. */
607 rombug_fetch_register (regno)
614 fprintf (log_file, "\nIn Fetch Register (reg=%s)\n", get_reg_name (regno));
620 rombug_fetch_registers ();
624 char *name = get_reg_name (regno);
625 printf_monitor (GET_REG);
626 if (regno >= 10 && regno <= 15)
631 expect (REG_DELIM, 1);
633 for (j = 0; j < 2; j++)
635 #if TARGET_BYTE_ORDER == BIG_ENDIAN
637 val = (val << 8) + b;
640 val = val + (b << (j*8));
643 supply_register (regno, (char *) &val);
645 else if (regno == 8 || regno == 9)
651 expect (REG_DELIM, 1);
652 get_hex_regs (1, regno);
657 expect (REG_DELIM, 1);
673 /* Store the remote registers from the contents of the block REGS. */
676 rombug_store_registers ()
680 for (regno = 0; regno <= PC_REGNUM; regno++)
681 rombug_store_register(regno);
683 registers_changed ();
686 /* Store register REGNO, or all if REGNO == 0.
687 return errno value. */
689 rombug_store_register (regno)
695 fprintf (log_file, "\nIn Store_register (regno=%d)\n", regno);
698 rombug_store_registers ();
702 printf ("Setting register %s to 0x%x\n", get_reg_name (regno), read_register (regno));
704 name = get_reg_name(regno);
705 if (name == 0) return;
706 printf_monitor (SET_REG, name, read_register (regno));
712 /* Get ready to modify the registers array. On machines which store
713 individual registers, this doesn't need to do anything. On machines
714 which store all the registers in one fell swoop, this makes sure
715 that registers contains all the registers from the program being
719 rombug_prepare_to_store ()
721 /* Do nothing, since we can store individual regs */
727 printf ("\tAttached to %s at %d baud.\n",
731 /* Copy LEN bytes of data from debugger memory at MYADDR
732 to inferior's memory at MEMADDR. Returns length moved. */
734 rombug_write_inferior_memory (memaddr, myaddr, len)
736 unsigned char *myaddr;
743 fprintf (log_file, "\nIn Write_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
745 printf_monitor (MEM_SET_CMD, memaddr + i);
746 for (i = 0; i < len; i++)
748 expect (CMD_DELIM, 1);
749 printf_monitor ("%x", myaddr[i]);
751 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
754 printf_monitor (CMD_END);
760 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
761 at debugger address MYADDR. Returns length moved. */
763 rombug_read_inferior_memory(memaddr, myaddr, len)
771 /* Number of bytes read so far. */
774 /* Starting address of this pass. */
775 unsigned long startaddr;
777 /* Number of bytes to read in this pass. */
781 fprintf (log_file, "\nIn Read_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
784 /* Note that this code works correctly if startaddr is just less
785 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
786 thing). That is, something like
787 rombug_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
788 works--it never adds len To memaddr and gets 0. */
789 /* However, something like
790 rombug_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
791 doesn't need to work. Detect it and give up if there's an attempt
793 if (((memaddr - 1) + len) < memaddr) {
803 if ((startaddr % 16) != 0)
804 len_this_pass -= startaddr % 16;
805 if (len_this_pass > (len - count))
806 len_this_pass = (len - count);
808 printf ("\nDisplay %d bytes at %x\n", len_this_pass, startaddr);
810 printf_monitor (MEM_DIS_CMD, startaddr, 16);
812 for (i = 0; i < len_this_pass; i++)
814 get_hex_byte (&myaddr[count++]);
816 printf ("\nRead a 0x%x from 0x%x\n", myaddr[count-1], startaddr);
819 expect(CMD_DELIM, 1);
822 printf_monitor (CMD_END);
828 /* FIXME-someday! merge these two. */
830 rombug_xfer_inferior_memory (memaddr, myaddr, len, write, target)
835 struct target_ops *target; /* ignored */
838 return rombug_write_inferior_memory (memaddr, myaddr, len);
840 return rombug_read_inferior_memory (memaddr, myaddr, len);
844 rombug_kill (args, from_tty)
848 return; /* ignore attempts to kill target system */
851 /* Clean up when a program exits.
852 The program actually lives on in the remote processor's RAM, and may be
853 run again without a download. Don't leave it full of breakpoint
857 rombug_mourn_inferior ()
859 remove_breakpoints ();
860 generic_mourn_inferior (); /* Do all the proper things now */
863 #define MAX_MONITOR_BREAKPOINTS 16
865 extern int memory_breakpoint_size;
866 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
869 rombug_insert_breakpoint (addr, shadow)
876 fprintf (log_file, "\nIn Insert_breakpoint (addr=%x)\n", addr);
878 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++)
879 if (breakaddr[i] == 0)
883 printf ("Breakpoint at %x\n", addr);
884 rombug_read_inferior_memory(addr, shadow, memory_breakpoint_size);
885 printf_monitor(SET_BREAK_CMD, addr);
890 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
895 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
898 rombug_remove_breakpoint (addr, shadow)
905 fprintf (log_file, "\nIn Remove_breakpoint (addr=%x)\n", addr);
907 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++)
908 if (breakaddr[i] == addr)
911 /* some monitors remove breakpoints based on the address */
912 if (strcasecmp (target_shortname, "bug") == 0)
913 printf_monitor(CLR_BREAK_CMD, addr);
915 printf_monitor(CLR_BREAK_CMD, i);
920 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
924 /* Load a file. This is usually an srecord, which is ascii. No
925 protocol, just sent line by line. */
927 #define DOWNLOAD_LINE_SIZE 100
932 /* this part comment out for os9* */
935 char buf[DOWNLOAD_LINE_SIZE];
939 printf ("Loading %s to monitor\n", arg);
941 download = fopen (arg, "r");
942 if (download == NULL)
944 error (sprintf (buf, "%s Does not exist", arg));
948 printf_monitor (LOAD_CMD);
949 /* expect ("Waiting for S-records from host... ", 1); */
951 while (!feof (download))
953 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
960 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
961 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
965 while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */
966 if (bytes_read < DOWNLOAD_LINE_SIZE)
968 if (!feof (download))
969 error ("Only read %d bytes\n", bytes_read);
978 if (!feof (download))
979 error ("Never got EOF while downloading");
984 /* Put a command string, in args, out to MONITOR.
985 Output from MONITOR is placed on the users terminal until the prompt
989 rombug_command (args, fromtty)
994 fprintf (log_file, "\nIn command (args=%s)\n", args);
996 if (monitor_desc == NULL)
997 error("monitor target not open.");
1000 error("Missing command.");
1002 printf_monitor("%s\r", args);
1007 /* Connect the user directly to MONITOR. This command acts just like the
1008 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
1010 static struct ttystate ttystate;
1014 { printf("\r\n[Exiting connect mode]\r\n");
1015 /*SERIAL_RESTORE(0, &ttystate);*/
1019 connect_command (args, fromtty)
1030 if (monitor_desc == NULL)
1031 error("monitor target not open.");
1034 fprintf("This command takes no args. They have been ignored.\n");
1036 printf("[Entering connect mode. Use ~. or ~^D to escape]\n");
1038 serial_raw(0, &ttystate);
1040 make_cleanup(cleanup_tty, 0);
1048 FD_SET(0, &readfds);
1049 FD_SET(monitor_desc, &readfds);
1050 numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
1052 while (numfds == 0);
1055 perror_with_name("select");
1057 if (FD_ISSET(0, &readfds))
1058 { /* tty input, send to monitor */
1061 perror_with_name("connect");
1063 printf_monitor("%c", c);
1077 if (c == '.' || c == '\004')
1084 if (FD_ISSET(monitor_desc, &readfds))
1100 * Define the monitor command strings. Since these are passed directly
1101 * through to a printf style function, we need can include formatting
1102 * strings. We also need a CR or LF on the end.
1104 struct monitor_ops rombug_cmds = {
1105 "g \r", /* execute or usually GO command */
1106 "g \r", /* continue command */
1107 "gs \r", /* single step */
1108 "b %x\r", /* set a breakpoint */
1109 "k %x\r", /* clear a breakpoint */
1110 "c %x\r", /* set memory to a value */
1111 "d %x %d\r", /* display memory */
1112 "$%08X", /* prompt memory commands use */
1113 ".%s %x\r", /* set a register */
1114 ":", /* delimiter between registers */
1115 ". \r", /* read a register */
1116 "mf \r", /* download command */
1117 "RomBug: ", /* monitor command prompt */
1118 ": ", /* end-of-command delimitor */
1119 ".\r" /* optional command terminator */
1122 struct target_ops rombug_ops = {
1124 "Microware's ROMBUG debug monitor",
1125 "Use a remote computer running the ROMBUG debug monitor.\n\
1126 Specify the serial device it is connected to (e.g. /dev/ttya).",
1133 rombug_fetch_register,
1134 rombug_store_register,
1135 rombug_prepare_to_store,
1136 rombug_xfer_inferior_memory,
1138 rombug_insert_breakpoint,
1139 rombug_remove_breakpoint, /* Breakpoints */
1144 0, /* Terminal handling */
1146 rombug_load, /* load */
1147 rombug_link, /* lookup_symbol */
1148 rombug_create_inferior,
1149 rombug_mourn_inferior,
1151 0, /* notice_signals */
1158 1, /* has execution */
1160 0, /* Section pointers */
1161 OPS_MAGIC, /* Always the last thing */
1165 _initialize_remote_os9k ()
1167 add_target (&rombug_ops);
1170 add_set_cmd ("hash", no_class, var_boolean, (char *)&hashmark,
1171 "Set display of activity while downloading a file.\nWhen enabled, a period \'.\' is displayed.",
1176 add_set_cmd ("timeout", no_class, var_zinteger,
1178 "Set timeout in seconds for remote MIPS serial I/O.",
1183 add_com ("rombug <command>", class_obscure, rombug_command,
1184 "Send a command to the debug monitor.");
1186 add_com ("connect", class_obscure, connect_command,
1187 "Connect the terminal directly up to a serial based command monitor.\nUse <CR>~. or <CR>~^D to break out.");