1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file was derived from various remote-* modules. It is a collection
22 of generic support functions so GDB can talk directly to a ROM based
23 monitor. This saves use from having to hack an exception based handler
24 into existance, and makes for quick porting.
26 This module talks to a debug monitor called 'MONITOR', which
27 We communicate with MONITOR via either a direct serial line, or a TCP
28 (or possibly TELNET) stream to a terminal multiplexor,
29 which in turn talks to the target board.
39 #include <sys/types.h>
43 #include "remote-utils.h"
46 # define TERMINAL struct termios
48 # define TERMINAL struct sgttyb
51 struct monitor_ops *current_monitor;
52 extern struct cmd_list_element *setlist;
53 extern struct cmd_list_element *unsetlist;
54 struct cmd_list_element *showlist;
56 static int hashmark; /* flag set by "set hash" */
58 /* FIXME: Replace with sr_get_debug (). */
59 #define LOG_FILE "monitor.log"
60 #if defined (LOG_FILE)
64 static int timeout = 24;
66 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
67 monitor_open knows that we don't have a file open when the program starts.
69 static serial_t monitor_desc = NULL;
71 /* sets the download protocol, choices are srec, generic, boot */
73 static char *loadtype_str;
74 static void set_loadtype_command();
77 * set_loadtype_command -- set the type for downloading. Check to make
78 * sure you have a support protocol for this target.
81 set_loadtype_command (ignore, from_tty, c)
84 struct cmd_list_element *c;
88 if (strcmp (LOADTYPES, "")) {
89 error ("No loadtype set");
93 type = strtok(LOADTYPES, ",");
94 if (STREQ (type, (*(char **) c->var))) {
95 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
99 while (type = strtok (NULL, ",") != (char *)NULL)
100 if (STREQ (type, (*(char **) c->var)))
101 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
103 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
107 * printf_monitor -- send data to monitor. Works just like printf.
110 printf_monitor(va_alist)
120 pattern = va_arg(args, char *);
122 vsprintf(buf, pattern, args);
124 if (sr_get_debug() > 3)
125 printf ("Sending to monitor,\r\n\t\"%s\".\r\n", buf);
127 if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
128 fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
131 /* Read a character from the remote system, doing all the fancy
139 c = SERIAL_READCHAR(monitor_desc, timeout);
146 putc(c & 0x7f, log_file);
152 if (c == SERIAL_TIMEOUT)
155 return c; /* Polls shouldn't generate timeout errors */
157 error("Timeout reading from remote system.");
160 perror_with_name("remote-monitor");
163 /* Scan input from the remote system, until STRING is found. If DISCARD is
164 non-zero, then discard non-matching input, else print it out.
165 Let the user break out immediately. */
167 expect (string, discard)
176 printf ("Expecting \"%s\".\n", string);
180 c = readchar(timeout);
187 printf ("\nMatched\n");
192 fwrite(string, 1, (p - 1) - string, stdout);
201 /* Keep discarding input until we see the MONITOR prompt.
203 The convention for dealing with the prompt is that you
205 o *then* wait for the prompt.
207 Thus the last thing that a procedure does with the serial line
208 will be an expect_prompt(). Exception: monitor_resume does not
209 wait for the prompt, because the terminal is being handed over
210 to the inferior. However, the next thing which happens after that
211 is a monitor_wait which does wait for the prompt.
212 Note that this includes abnormal exit, e.g. error(). This is
213 necessary to prevent getting into states from which we can't
216 expect_prompt(discard)
219 #if defined (LOG_FILE)
220 /* This is a convenient place to do this. The idea is to do it often
221 enough that we never lose much data if we terminate abnormally. */
224 expect (PROMPT, discard);
228 * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
240 if (sr_get_debug() > 5)
241 printf ("Ignoring \'%c\'.\n", ch);
244 if (sr_get_debug() > 5)
245 printf ("Accepting \'%c\'.\n", ch);
251 * get_hex_digit -- Get a hex digit from the remote system & return its value.
252 * If ignore is nonzero, ignore spaces, newline & tabs.
255 get_hex_digit(ignore)
260 ch = readchar(timeout);
263 if (sr_get_debug() > 4)
264 printf ("get_hex_digit() got a 0x%x(%c)\n", ch, ch);
266 if (ch >= '0' && ch <= '9')
268 else if (ch >= 'A' && ch <= 'F')
269 return ch - 'A' + 10;
270 else if (ch >= 'a' && ch <= 'f')
271 return ch - 'a' + 10;
272 else if (ch == ' ' && ignore)
276 error("Invalid hex digit from remote system.");
281 /* get_hex_byte -- Get a byte from monitor and put it in *BYT.
282 * Accept any number leading spaces.
290 val = get_hex_digit (1) << 4;
291 if (sr_get_debug() > 3)
292 printf ("\nget_hex_digit() -- Read first nibble 0x%x\n", val);
294 val |= get_hex_digit (0);
295 if (sr_get_debug() > 3)
296 printf ("\nget_hex_digit() -- Read second nibble 0x%x\n", val);
299 if (sr_get_debug() > 3)
300 printf ("\nget_hex_digit() -- Read a 0x%x\n", val);
304 * get_hex_word -- Get N 32-bit words from remote, each preceded by a space,
305 * and put them in registers starting at REGNO.
314 for (i = 0; i < 8; i++)
315 val = (val << 4) + get_hex_digit (i == 0);
317 if (sr_get_debug() > 3)
318 printf ("\nget_hex_word() got a 0x%x.\n", val);
323 /* This is called not only when we first attach, but also when the
324 user types "run" after having attached. */
326 monitor_create_inferior (execfile, args, env)
334 error("Can't pass arguments to remote MONITOR process");
336 if (execfile == 0 || exec_bfd == 0)
337 error("No exec file specified");
339 entry_pt = (int) bfd_get_start_address (exec_bfd);
342 fputs ("\nIn Create_inferior()", log_file);
345 /* The "process" (board) is already stopped awaiting our commands, and
346 the program is already downloaded. We just set its PC and go. */
348 clear_proceed_status ();
350 /* Tell wait_for_inferior that we've started a new process. */
351 init_wait_for_inferior ();
353 /* Set up the "saved terminal modes" of the inferior
354 based on what modes we are starting it with. */
355 target_terminal_init ();
357 /* Install inferior's terminal modes. */
358 target_terminal_inferior ();
360 /* insert_step_breakpoint (); FIXME, do we need this? */
363 proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
366 /* Open a connection to a remote debugger.
367 NAME is the filename used for communication. */
369 static int baudrate = 9600;
370 static char dev_name[100];
373 monitor_open(args, name, from_tty)
380 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
381 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
386 strcpy(dev_name, args);
387 monitor_desc = SERIAL_OPEN(dev_name);
389 if (monitor_desc == NULL)
390 perror_with_name(dev_name);
394 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
396 SERIAL_CLOSE (monitor_desc);
397 perror_with_name (name);
401 SERIAL_RAW(monitor_desc);
403 #if defined (LOG_FILE)
404 log_file = fopen (LOG_FILE, "w");
405 if (log_file == NULL)
406 perror_with_name (LOG_FILE);
409 /* wake up the monitor and see if it's alive */
410 printf_monitor(INIT_CMD);
411 expect_prompt(1); /* See if we get a prompt */
413 /* try again to be sure */
414 printf_monitor(INIT_CMD);
415 expect_prompt(1); /* See if we get a prompt */
418 printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
424 * _close -- Close out all files and local state before this target loses control.
428 monitor_close (quitting)
431 SERIAL_CLOSE(monitor_desc);
434 #if defined (LOG_FILE)
436 if (ferror(log_file))
437 fprintf(stderr, "Error writing log file.\n");
438 if (fclose(log_file) != 0)
439 fprintf(stderr, "Error closing log file.\n");
444 /* Terminate the open connection to the remote debugger.
445 Use this when you want to detach and do something else
448 monitor_detach (from_tty)
451 pop_target(); /* calls monitor_close to do the real work */
453 printf ("Ending remote %s debugging\n", target_shortname);
457 * _resume -- Tell the remote machine to resume.
460 monitor_resume (pid, step, sig)
462 enum target_signal sig;
465 fprintf (log_file, "\nIn Resume (step=%d, sig=%d)\n", step, sig);
470 printf_monitor (STEP_CMD);
471 /* wait for the echo. */
472 expect (STEP_CMD, 1);
476 printf_monitor (GO_CMD);
477 /* swallow the echo. */
483 * _wait -- Wait until the remote machine stops, then return,
484 * storing status in status just as `wait' would.
488 monitor_wait (pid, status)
490 struct target_waitstatus *status;
492 int old_timeout = timeout;
494 fputs ("\nIn wait ()", log_file);
497 status->kind = TARGET_WAITKIND_EXITED;
498 status->value.integer = 0;
500 timeout = 0; /* Don't time out -- user program is running. */
502 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
504 status->kind = TARGET_WAITKIND_STOPPED;
505 status->value.sig = TARGET_SIGNAL_TRAP;
507 timeout = old_timeout;
512 /* Return the name of register number regno in the form input and output by
513 monitor. Currently, register_names just happens to contain exactly what
514 monitor wants. Lets take advantage of that just as long as possible! */
529 for (p = REGNAMES(regno); *p; p++)
534 if (sr_get_debug() > 5)
535 printf ("Got name \"%s\" from regno #%d.\n", buf, regno);
540 /* read the remote registers into the block regs. */
543 monitor_fetch_registers ()
547 /* yeah yeah, i know this is horribly inefficient. but it isn't done
548 very often... i'll clean it up later. */
550 for (regno = 0; regno <= PC_REGNUM; regno++)
551 monitor_fetch_register(regno);
555 * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO is -1.
556 * Returns errno value.
559 monitor_fetch_register (regno)
565 fprintf (log_file, "\nIn Fetch Register (reg=%s)\n", get_reg_name (regno));
570 monitor_fetch_registers ();
572 char *name = get_reg_name (regno);
575 printf_monitor (ROMCMD(GET_REG), name); /* send the command */
576 expect (name, 1); /* then strip the leading garbage */
577 if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */
578 expect (ROMDELIM(GET_REG), 1);
581 val = get_hex_word(); /* get the value, ignore junk */
582 supply_register (regno, (char *) &val);
584 if (*ROMDELIM(GET_REG) != 0) {
585 /*** expect (ROMRES(GET_REG)); ***/
586 printf_monitor (CMD_END);
593 /* Store the remote registers from the contents of the block REGS. */
596 monitor_store_registers ()
601 fprintf (log_file, "\nIn Fetch Registers\n");
604 for (regno = 0; regno <= PC_REGNUM; regno++)
605 monitor_store_register(regno);
607 registers_changed ();
610 /* Store register REGNO, or all if REGNO == 0.
611 return errno value. */
613 monitor_store_register (regno)
619 fprintf (log_file, "\nIn Store_register (regno=%d)\n", regno);
623 monitor_store_registers ();
625 if (sr_get_debug() > 3)
626 printf ("Setting register %s to 0x%x\n", get_reg_name (regno), read_register (regno));
628 name = get_reg_name (regno);
631 printf_monitor (ROMCMD(SET_REG), name); /* send the command */
632 expect (name, 1); /* then strip the leading garbage */
633 if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */
634 expect (ROMDELIM(SET_REG), 1);
637 if (*ROMDELIM(SET_REG) != 0) {
638 printf_monitor ("%s%s\n",read_register(regno), CMD_END);
645 printf_monitor (SET_REG, get_reg_name (regno),
646 read_register (regno));
652 /* Get ready to modify the registers array. On machines which store
653 individual registers, this doesn't need to do anything. On machines
654 which store all the registers in one fell swoop, this makes sure
655 that registers contains all the registers from the program being
659 monitor_prepare_to_store ()
661 /* Do nothing, since we can store individual regs */
665 monitor_files_info ()
667 printf ("\tAttached to %s at %d baud.\n",
671 /* Copy LEN bytes of data from debugger memory at MYADDR
672 to inferior's memory at MEMADDR. Returns length moved. */
674 monitor_write_inferior_memory (memaddr, myaddr, len)
676 unsigned char *myaddr;
683 fprintf (log_file, "\nIn Write_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
686 #define MEM_PROMPT "" /* FIXME, bogus */
687 for (i = 0; i < len; i++)
689 printf_monitor (SET_MEM, memaddr + i);
690 expect (sprintf (buf, MEM_PROMPT, memaddr + i), 1);
692 printf_monitor ("%x", myaddr[i]);
694 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
697 /*** expect (sprintf (buf, MEM_PROMPT, memaddr + i +1), 1);
698 expect (CMD_DELIM); ***/
699 printf_monitor (CMD_END);
707 * monitor_read_inferior_memory -- read LEN bytes from inferior memory
708 * at MEMADDR. Put the result at debugger address MYADDR. Returns
712 monitor_read_inferior_memory(memaddr, myaddr, len)
720 /* Number of bytes read so far. */
723 /* Starting address of this pass. */
724 unsigned long startaddr;
726 /* Number of bytes to read in this pass. */
730 fprintf (log_file, "\nIn Read_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
733 /* Note that this code works correctly if startaddr is just less
734 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
735 thing). That is, something like
736 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
737 works--it never adds len To memaddr and gets 0. */
738 /* However, something like
739 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
740 doesn't need to work. Detect it and give up if there's an attempt
742 if (((memaddr - 1) + len) < memaddr) {
749 while (count < len) {
751 if ((startaddr % 16) != 0)
752 len_this_pass -= startaddr % 16;
753 if (len_this_pass > (len - count))
754 len_this_pass = (len - count);
756 printf ("\nDisplay %d bytes at %x\n", len_this_pass, startaddr);
758 for (i = 0; i < len_this_pass; i++) {
759 printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
760 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
762 expect (buf,1); /* get the command echo */
763 get_hex_word(1); /* strip away the address */
765 if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */
766 expect (ROMDELIM(GET_MEM), 1);
768 sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
769 expect (buf,1); /* get the command echo */
770 get_hex_word(1); /* strip away the address */
772 get_hex_byte (&myaddr[count++]); /* get the value at this address */
774 if (*ROMDELIM(GET_MEM) != 0) {
775 printf_monitor (CMD_END);
784 /* FIXME-someday! merge these two. */
786 monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
791 struct target_ops *target; /* ignored */
794 return monitor_write_inferior_memory (memaddr, myaddr, len);
796 return monitor_read_inferior_memory (memaddr, myaddr, len);
800 monitor_kill (args, from_tty)
804 return; /* ignore attempts to kill target system */
807 /* Clean up when a program exits.
808 The program actually lives on in the remote processor's RAM, and may be
809 run again without a download. Don't leave it full of breakpoint
813 monitor_mourn_inferior ()
815 remove_breakpoints ();
816 generic_mourn_inferior (); /* Do all the proper things now */
819 #define MAX_MONITOR_BREAKPOINTS 16
821 extern int memory_breakpoint_size;
822 static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
825 * monitor_insert_breakpoint -- add a breakpoint
828 monitor_insert_breakpoint (addr, shadow)
835 fprintf (log_file, "\nIn Insert_breakpoint (addr=%x)\n", addr);
838 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++)
839 if (breakaddr[i] == 0)
843 printf ("Breakpoint at %x\n", addr);
844 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
845 printf_monitor(SET_BREAK_CMD, addr);
850 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
855 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
858 monitor_remove_breakpoint (addr, shadow)
865 fprintf (log_file, "\nIn Remove_breakpoint (addr=%x)\n", addr);
867 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++)
868 if (breakaddr[i] == addr)
871 /* some monitors remove breakpoints based on the address */
872 if (strcasecmp (target_shortname, "bug") == 0)
873 printf_monitor(CLR_BREAK_CMD, addr);
875 printf_monitor(CLR_BREAK_CMD, i);
880 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
884 /* Load a file. This is usually an srecord, which is ascii. No
885 protocol, just sent line by line. */
887 #define DOWNLOAD_LINE_SIZE 100
893 char buf[DOWNLOAD_LINE_SIZE];
897 printf ("Loading %s to monitor\n", arg);
899 download = fopen (arg, "r");
900 if (download == NULL)
902 error (sprintf (buf, "%s Does not exist", arg));
906 printf_monitor (LOAD_CMD);
907 /* expect ("Waiting for S-records from host... ", 1); */
909 while (!feof (download))
911 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
918 if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
919 fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
923 while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */
924 if (bytes_read < DOWNLOAD_LINE_SIZE)
926 if (!feof (download))
927 error ("Only read %d bytes\n", bytes_read);
936 if (!feof (download))
937 error ("Never got EOF while downloading");
941 /* Put a command string, in args, out to MONITOR. Output from MONITOR is placed
942 on the users terminal until the prompt is seen. */
945 monitor_command (args, fromtty)
950 fprintf (log_file, "\nIn command (args=%s)\n", args);
952 if (monitor_desc == NULL)
953 error("monitor target not open.");
956 error("Missing command.");
958 printf_monitor("%s\r", args);
963 * _initialize_remote_monitors -- setup a few addtitional commands that
964 * are usually only used by monitors.
967 _initialize_remote_monitors ()
969 struct cmd_list_element *c;
971 /* this sets the type of download protocol */
972 c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
973 "Set the type of the remote load protocol.\n", &setlist);
974 c->function.sfunc = set_loadtype_command;
975 add_show_from_set (c, &showlist);
976 loadtype_str = savestring ("generic", 8);
978 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
980 "Set display of activity while downloading a file.\n\
981 When enabled, a period \'.\' is displayed.",
985 /* generic monitor command */
986 add_com ("monitor", class_obscure, monitor_command,
987 "Send a command to the debug monitor.");