1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1995, 1996
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
5 Resurrected from the ashes by Stu Grossman.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file was derived from various remote-* modules. It is a collection
24 of generic support functions so GDB can talk directly to a ROM based
25 monitor. This saves use from having to hack an exception based handler
26 into existance, and makes for quick porting.
28 This module talks to a debug monitor called 'MONITOR', which
29 We communicate with MONITOR via either a direct serial line, or a TCP
30 (or possibly TELNET) stream to a terminal multiplexor,
31 which in turn talks to the target board. */
33 /* FIXME 32x64: This code assumes that registers and addresses are at
34 most 32 bits long. If they can be larger, you will need to declare
35 values as LONGEST and use %llx or some such to print values when
36 building commands to send to the monitor. Since we don't know of
37 any actual 64-bit targets with ROM monitors that use this code,
38 it's not an issue right now. -sts 4/18/96 */
44 #ifdef ANSI_PROTOTYPES
51 #include "gdb_string.h"
52 #include <sys/types.h>
58 #include "gnu-regex.h"
62 static int readchar PARAMS ((int timeout));
64 static void monitor_command PARAMS ((char *args, int fromtty));
66 static void monitor_fetch_register PARAMS ((int regno));
67 static void monitor_store_register PARAMS ((int regno));
69 static void monitor_detach PARAMS ((char *args, int from_tty));
70 static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig));
71 static void monitor_interrupt PARAMS ((int signo));
72 static void monitor_interrupt_twice PARAMS ((int signo));
73 static void monitor_interrupt_query PARAMS ((void));
74 static void monitor_wait_cleanup PARAMS ((int old_timeout));
76 static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status));
77 static void monitor_fetch_registers PARAMS ((int regno));
78 static void monitor_store_registers PARAMS ((int regno));
79 static void monitor_prepare_to_store PARAMS ((void));
80 static int monitor_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, int write, struct target_ops *target));
81 static void monitor_files_info PARAMS ((struct target_ops *ops));
82 static int monitor_insert_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
83 static int monitor_remove_breakpoint PARAMS ((CORE_ADDR addr, char *shadow));
84 static void monitor_kill PARAMS ((void));
85 static void monitor_load PARAMS ((char *file, int from_tty));
86 static void monitor_mourn_inferior PARAMS ((void));
87 static void monitor_stop PARAMS ((void));
88 static void monitor_debug PARAMS ((char *prefix, char *string, char *suffix));
90 static int monitor_read_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len));
91 static int monitor_write_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len));
93 static int monitor_expect_regexp PARAMS ((struct re_pattern_buffer *pat,
94 char *buf, int buflen));
95 static int from_hex PARAMS ((int a));
96 static unsigned long get_hex_word PARAMS ((void));
98 static struct monitor_ops *current_monitor;
100 static int hashmark; /* flag set by "set hash" */
102 static int timeout = 30;
104 static int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */
106 static void (*ofunc)(); /* Old SIGINT signal handler */
108 /* Descriptor for I/O to remote machine. Initialize it to NULL so
109 that monitor_open knows that we don't have a file open when the
112 static serial_t monitor_desc = NULL;
114 /* Pointer to regexp pattern matching data */
116 static struct re_pattern_buffer register_pattern;
117 static char register_fastmap[256];
119 static struct re_pattern_buffer getmem_resp_delim_pattern;
120 static char getmem_resp_delim_fastmap[256];
122 static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
123 monitor_wait wakes up. */
125 static DCACHE *remote_dcache;
127 /* monitor_debug is like fputs_unfiltered, except it prints special
128 characters in printable fashion. */
131 monitor_debug (prefix, string, suffix)
138 /* print prefix and suffix after each line */
139 static int new_line=1;
140 if (new_line==1) { /* print prefix if last char was a newline */
141 fputs_unfiltered (prefix, gdb_stderr);
144 if (strchr(string,'\n')) /* save state for next call */
147 while ((ch = *string++) != '\0')
152 fputc_unfiltered (ch, gdb_stderr);
155 fprintf_unfiltered (gdb_stderr, "\\%03o", ch);
159 case '\\': fputs_unfiltered ("\\\\", gdb_stderr); break;
160 case '\b': fputs_unfiltered ("\\b", gdb_stderr); break;
161 case '\f': fputs_unfiltered ("\\f", gdb_stderr); break;
162 case '\n': fputs_unfiltered ("\\n", gdb_stderr); break;
163 case '\r': fputs_unfiltered ("\\r", gdb_stderr); break;
164 case '\t': fputs_unfiltered ("\\t", gdb_stderr); break;
165 case '\v': fputs_unfiltered ("\\v", gdb_stderr); break;
169 if (new_line==1) { /* print suffix if last char was a newline */
170 fputs_unfiltered (suffix, gdb_stderr);
171 fputs_unfiltered ("\n", gdb_stderr);
175 /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
176 Works just like printf. */
179 #ifdef ANSI_PROTOTYPES
180 monitor_printf_noecho (char *pattern, ...)
182 monitor_printf_noecho (va_alist)
191 va_start (args, pattern);
195 pattern = va_arg (args, char *);
198 vsprintf (sndbuf, pattern, args);
200 if (remote_debug > 0)
201 monitor_debug ("sent -->", sndbuf, "<--");
203 len = strlen (sndbuf);
205 if (len + 1 > sizeof sndbuf)
208 if (SERIAL_WRITE(monitor_desc, sndbuf, len))
209 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
212 /* monitor_printf -- Send data to monitor and check the echo. Works just like
216 #ifdef ANSI_PROTOTYPES
217 monitor_printf (char *pattern, ...)
219 monitor_printf (va_alist)
227 #ifdef ANSI_PROTOTYPES
228 va_start (args, pattern);
232 pattern = va_arg (args, char *);
235 vsprintf (sndbuf, pattern, args);
237 if (remote_debug > 0)
238 monitor_debug ("sent -->", sndbuf, "<--");
240 len = strlen (sndbuf);
242 if (len + 1 > sizeof sndbuf)
245 if (SERIAL_WRITE(monitor_desc, sndbuf, len))
246 fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
248 /* We used to expect that the next immediate output was the characters we
249 just output, but sometimes some extra junk appeared before the characters
250 we expected, like an extra prompt, or a portmaster sending telnet negotiations.
251 So, just start searching for what we sent, and skip anything unknown. */
252 monitor_expect (sndbuf, (char *)0, 0);
255 /* Read a character from the remote system, doing all the fancy
263 static enum { last_random, last_nl, last_cr, last_crnl } state = last_random;
269 c = SERIAL_READCHAR (monitor_desc, timeout);
274 if (remote_debug > 0)
279 monitor_debug ("read -->", buf, "<--");
283 /* Canonicialize \n\r combinations into one \r */
284 if ((current_monitor->flags & MO_HANDLE_NL) != 0)
286 if ((c == '\r' && state == last_nl)
287 || (c == '\n' && state == last_cr))
308 if (c == SERIAL_TIMEOUT)
309 #ifdef MAINTENANCE_CMDS
310 if (in_monitor_wait) /* Watchdog went off */
312 target_mourn_inferior ();
313 error ("Watchdog has expired. Target detached.\n");
317 error ("Timeout reading from remote system.");
319 perror_with_name ("remote-monitor");
322 /* Scan input from the remote system, until STRING is found. If BUF is non-
323 zero, then collect input until we have collected either STRING or BUFLEN-1
324 chars. In either case we terminate BUF with a 0. If input overflows BUF
325 because STRING can't be found, return -1, else return number of chars in BUF
326 (minus the terminating NUL). Note that in the non-overflow case, STRING
327 will be at the end of BUF. */
330 monitor_expect (string, buf, buflen)
336 int obuflen = buflen;
351 c = readchar (timeout);
358 c = readchar (timeout);
360 /* Don't expect any ^C sent to be echoed */
362 if (*p == '\003' || c == *p)
372 return obuflen - buflen;
387 /* Search for a regexp. */
390 monitor_expect_regexp (pat, buf, buflen)
391 struct re_pattern_buffer *pat;
402 mybuf = alloca (1024);
411 if (p - mybuf >= buflen)
412 { /* Buffer about to overflow */
414 /* On overflow, we copy the upper half of the buffer to the lower half. Not
415 great, but it usually works... */
417 memcpy (mybuf, mybuf + buflen / 2, buflen / 2);
418 p = mybuf + buflen / 2;
421 *p++ = readchar (timeout);
423 retval = re_search (pat, mybuf, p - mybuf, 0, p - mybuf, NULL);
429 /* Keep discarding input until we see the MONITOR prompt.
431 The convention for dealing with the prompt is that you
433 o *then* wait for the prompt.
435 Thus the last thing that a procedure does with the serial line
436 will be an monitor_expect_prompt(). Exception: monitor_resume does not
437 wait for the prompt, because the terminal is being handed over
438 to the inferior. However, the next thing which happens after that
439 is a monitor_wait which does wait for the prompt.
440 Note that this includes abnormal exit, e.g. error(). This is
441 necessary to prevent getting into states from which we can't
445 monitor_expect_prompt (buf, buflen)
449 return monitor_expect (PROMPT, buf, buflen);
452 /* Get N 32-bit words from remote, each preceded by a space, and put
453 them in registers starting at REGNO. */
463 ch = readchar (timeout);
468 for (i = 7; i >= 1; i--)
470 ch = readchar (timeout);
473 val = (val << 4) | from_hex (ch);
480 compile_pattern (pattern, compiled_pattern, fastmap)
482 struct re_pattern_buffer *compiled_pattern;
488 compiled_pattern->fastmap = fastmap;
490 tmp = re_set_syntax (RE_SYNTAX_EMACS);
491 val = re_compile_pattern (pattern,
497 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern, val);
500 re_compile_fastmap (compiled_pattern);
503 /* Open a connection to a remote debugger. NAME is the filename used
504 for communication. */
506 static char *dev_name;
507 static struct target_ops *targ_ops;
510 monitor_open (args, mon_ops, from_tty)
512 struct monitor_ops *mon_ops;
519 if (mon_ops->magic != MONITOR_OPS_MAGIC)
520 error ("Magic number of monitor_ops struct wrong.");
522 targ_ops = mon_ops->target;
523 name = targ_ops->to_shortname;
526 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
527 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
529 target_preopen (from_tty);
531 /* Setup pattern for register dump */
533 if (mon_ops->register_pattern)
534 compile_pattern (mon_ops->register_pattern, ®ister_pattern,
537 if (mon_ops->getmem.resp_delim)
538 compile_pattern (mon_ops->getmem.resp_delim, &getmem_resp_delim_pattern,
539 getmem_resp_delim_fastmap);
541 unpush_target (targ_ops);
545 dev_name = strsave (args);
547 monitor_desc = SERIAL_OPEN (dev_name);
550 perror_with_name (dev_name);
554 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
556 SERIAL_CLOSE (monitor_desc);
557 perror_with_name (dev_name);
561 SERIAL_RAW (monitor_desc);
563 SERIAL_FLUSH_INPUT (monitor_desc);
565 /* some systems only work with 2 stop bits */
567 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
569 current_monitor = mon_ops;
571 /* See if we can wake up the monitor. First, try sending a stop sequence,
572 then send the init strings. Last, remove all breakpoints. */
574 if (current_monitor->stop)
577 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
579 monitor_expect_prompt (NULL, 0);
583 /* wake up the monitor and see if it's alive */
584 for (p = mon_ops->init; *p != NULL; p++)
586 /* Some of the characters we send may not be echoed,
587 but we hope to get a prompt at the end of it all. */
589 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
592 monitor_printf_noecho (*p);
593 monitor_expect_prompt (NULL, 0);
596 SERIAL_FLUSH_INPUT (monitor_desc);
598 /* Remove all breakpoints */
600 if (mon_ops->clr_all_break)
602 monitor_printf (mon_ops->clr_all_break);
603 monitor_expect_prompt (NULL, 0);
607 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
609 push_target (targ_ops);
611 inferior_pid = 42000; /* Make run command think we are busy... */
613 /* Give monitor_wait something to read */
615 monitor_printf (current_monitor->line_term);
617 remote_dcache = dcache_init (monitor_read_memory, monitor_write_memory);
622 /* Close out all files and local state before this target loses
626 monitor_close (quitting)
630 SERIAL_CLOSE (monitor_desc);
634 /* Terminate the open connection to the remote debugger. Use this
635 when you want to detach and do something else with your gdb. */
638 monitor_detach (args, from_tty)
642 pop_target (); /* calls monitor_close to do the real work */
644 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
647 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
650 monitor_supply_register (regno, valstr)
655 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
658 val = strtoul (valstr, &p, 16);
660 if (val == 0 && valstr == p)
661 error ("monitor_supply_register (%d): bad value from monitor: %s.",
664 /* supply register stores in target byte order, so swap here */
666 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
668 supply_register (regno, regbuf);
673 /* Tell the remote machine to resume. */
676 monitor_resume (pid, step, sig)
678 enum target_signal sig;
680 dcache_flush (remote_dcache);
682 monitor_printf (STEP_CMD);
685 monitor_printf (CONT_CMD);
686 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
691 /* Parse the output of a register dump command. A monitor specific
692 regexp is used to extract individual register descriptions of the
693 form REG=VAL. Each description is split up into a name and a value
694 string which are passed down to monitor specific code. */
697 parse_register_dump (buf, len)
703 int regnamelen, vallen;
705 /* Element 0 points to start of register name, and element 1
706 points to the start of the register value. */
707 struct re_registers register_strings;
709 if (re_search (®ister_pattern, buf, len, 0, len,
710 ®ister_strings) == -1)
713 regnamelen = register_strings.end[1] - register_strings.start[1];
714 regname = buf + register_strings.start[1];
715 vallen = register_strings.end[2] - register_strings.start[2];
716 val = buf + register_strings.start[2];
718 current_monitor->supply_register (regname, regnamelen, val, vallen);
720 buf += register_strings.end[0];
721 len -= register_strings.end[0];
725 /* Send ^C to target to halt it. Target will respond, and send us a
729 monitor_interrupt (signo)
732 /* If this doesn't work, try more severe steps. */
733 signal (signo, monitor_interrupt_twice);
736 printf_unfiltered ("monitor_interrupt called\n");
741 /* The user typed ^C twice. */
744 monitor_interrupt_twice (signo)
747 signal (signo, ofunc);
749 monitor_interrupt_query ();
751 signal (signo, monitor_interrupt);
754 /* Ask the user what to do when an interrupt is received. */
757 monitor_interrupt_query ()
759 target_terminal_ours ();
761 if (query ("Interrupted while waiting for the program.\n\
762 Give up (and stop debugging it)? "))
764 target_mourn_inferior ();
765 return_to_top_level (RETURN_QUIT);
768 target_terminal_inferior ();
772 monitor_wait_cleanup (old_timeout)
775 timeout = old_timeout;
776 signal (SIGINT, ofunc);
780 /* Wait until the remote machine stops, then return, storing status in
781 status just as `wait' would. */
784 monitor_wait (pid, status)
786 struct target_waitstatus *status;
788 int old_timeout = timeout;
791 struct cleanup *old_chain;
793 status->kind = TARGET_WAITKIND_EXITED;
794 status->value.integer = 0;
796 old_chain = make_cleanup (monitor_wait_cleanup, old_timeout);
798 #ifdef MAINTENANCE_CMDS
800 timeout = watchdog > 0 ? watchdog : -1;
802 timeout = -1; /* Don't time out -- user program is running. */
805 ofunc = (void (*)()) signal (SIGINT, monitor_interrupt);
809 resp_len = monitor_expect_prompt (buf, sizeof (buf));
812 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
814 while (resp_len < 0);
816 signal (SIGINT, ofunc);
818 timeout = old_timeout;
820 if (dump_reg_flag && current_monitor->dump_registers)
824 monitor_printf (current_monitor->dump_registers);
825 resp_len = monitor_expect_prompt (buf, sizeof (buf));
828 if (current_monitor->register_pattern)
829 parse_register_dump (buf, resp_len);
831 status->kind = TARGET_WAITKIND_STOPPED;
832 status->value.sig = TARGET_SIGNAL_TRAP;
834 discard_cleanups (old_chain);
841 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
845 monitor_fetch_register (regno)
849 static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
850 char regbuf[MAX_REGISTER_RAW_SIZE * 2 + 1];
853 name = REGNAMES (regno);
857 supply_register (regno, zerobuf);
861 /* send the register examine command */
863 monitor_printf (current_monitor->getreg.cmd, name);
865 /* If RESP_DELIM is specified, we search for that as a leading
866 delimiter for the register value. Otherwise, we just start
867 searching from the start of the buf. */
869 if (current_monitor->getreg.resp_delim)
870 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
872 /* Read upto the maximum number of hex digits for this register, skipping
873 spaces, but stop reading if something else is seen. Some monitors
874 like to drop leading zeros. */
876 for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++)
879 c = readchar (timeout);
881 c = readchar (timeout);
889 regbuf[i] = '\000'; /* terminate the number */
891 /* If TERM is present, we wait for that to show up. Also, (if TERM
892 is present), we will send TERM_CMD if that is present. In any
893 case, we collect all of the output into buf, and then wait for
894 the normal prompt. */
896 if (current_monitor->getreg.term)
898 monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */
900 if (current_monitor->getreg.term_cmd)
902 monitor_printf (current_monitor->getreg.term_cmd);
903 monitor_expect_prompt (NULL, 0);
907 monitor_expect_prompt (NULL, 0); /* get response */
909 monitor_supply_register (regno, regbuf);
912 /* Read the remote registers into the block regs. */
914 static void monitor_dump_regs ()
916 if (current_monitor->dump_registers)
921 monitor_printf (current_monitor->dump_registers);
922 resp_len = monitor_expect_prompt (buf, sizeof (buf));
923 parse_register_dump (buf, resp_len);
926 abort(); /* Need some way to read registers */
930 monitor_fetch_registers (regno)
933 if (current_monitor->getreg.cmd)
937 monitor_fetch_register (regno);
941 for (regno = 0; regno < NUM_REGS; regno++)
942 monitor_fetch_register (regno);
945 monitor_dump_regs ();
949 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
952 monitor_store_register (regno)
958 name = REGNAMES (regno);
962 val = read_register (regno);
964 /* send the register deposit command */
966 monitor_printf (current_monitor->setreg.cmd, name, val);
968 /* It's possible that there are actually some monitors out there that
969 will prompt you when you set a register. In that case, you may
970 need to add some code here to deal with TERM and TERM_CMD (see
971 monitor_fetch_register to get an idea of what's needed...) */
973 monitor_expect_prompt (NULL, 0);
976 /* Store the remote registers. */
979 monitor_store_registers (regno)
984 monitor_store_register (regno);
988 for (regno = 0; regno < NUM_REGS; regno++)
989 monitor_store_register (regno);
992 /* Get ready to modify the registers array. On machines which store
993 individual registers, this doesn't need to do anything. On machines
994 which store all the registers in one fell swoop, this makes sure
995 that registers contains all the registers from the program being
999 monitor_prepare_to_store ()
1001 /* Do nothing, since we can store individual regs */
1005 monitor_files_info (ops)
1006 struct target_ops *ops;
1008 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
1012 monitor_write_memory (memaddr, myaddr, len)
1021 /* Use memory fill command for leading 0 bytes. */
1023 if (current_monitor->fill)
1025 for (i = 0; i < len; i++)
1029 if (i > 4) /* More than 4 zeros is worth doing */
1031 if (current_monitor->flags & MO_FILL_USES_ADDR)
1032 monitor_printf (current_monitor->fill, memaddr, memaddr + i, 0);
1034 monitor_printf (current_monitor->fill, memaddr, i, 0);
1036 monitor_expect_prompt (NULL, 0);
1042 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll)
1045 cmd = current_monitor->setmem.cmdll;
1047 else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl)
1050 cmd = current_monitor->setmem.cmdl;
1052 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw)
1055 cmd = current_monitor->setmem.cmdw;
1060 cmd = current_monitor->setmem.cmdb;
1063 val = extract_unsigned_integer (myaddr, len);
1065 monitor_printf (cmd, memaddr, val);
1067 monitor_expect_prompt (NULL, 0);
1072 /* This is an alternate form of monitor_read_memory which is used for monitors
1073 which can only read a single byte/word/etc. at a time. */
1076 monitor_read_memory_single (memaddr, myaddr, len)
1082 char membuf[sizeof(int) * 2 + 1];
1087 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll)
1090 cmd = current_monitor->getmem.cmdll;
1092 else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl)
1095 cmd = current_monitor->getmem.cmdl;
1097 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw)
1100 cmd = current_monitor->getmem.cmdw;
1105 cmd = current_monitor->getmem.cmdb;
1108 /* Send the examine command. */
1110 monitor_printf (cmd, memaddr);
1112 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1113 the register value. Otherwise, we just start searching from the start of
1116 if (current_monitor->getmem.resp_delim)
1117 monitor_expect_regexp (&getmem_resp_delim_pattern, NULL, 0);
1119 /* Now, read the appropriate number of hex digits for this loc, skipping
1122 for (i = 0; i < len * 2; i++)
1128 c = readchar (timeout);
1134 error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
1135 memaddr, i, membuf, c);
1141 membuf[i] = '\000'; /* terminate the number */
1143 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1144 present), we will send TERM_CMD if that is present. In any case, we collect
1145 all of the output into buf, and then wait for the normal prompt. */
1147 if (current_monitor->getmem.term)
1149 monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */
1151 if (current_monitor->getmem.term_cmd)
1153 monitor_printf (current_monitor->getmem.term_cmd);
1154 monitor_expect_prompt (NULL, 0);
1158 monitor_expect_prompt (NULL, 0); /* get response */
1161 val = strtoul (membuf, &p, 16);
1163 if (val == 0 && membuf == p)
1164 error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.",
1167 /* supply register stores in target byte order, so swap here */
1169 store_unsigned_integer (myaddr, len, val);
1174 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
1175 at MEMADDR. Returns length moved. Currently, we only do one byte at a
1179 monitor_read_memory (memaddr, myaddr, len)
1185 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
1192 if (current_monitor->flags & MO_GETMEM_READ_SINGLE)
1193 return monitor_read_memory_single (memaddr, myaddr, len);
1195 len = min (len, 16);
1197 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
1198 if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0)
1199 len = ((memaddr + len) & ~0xf) - memaddr;
1201 /* send the memory examine command */
1203 if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE)
1204 monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len - 1);
1206 monitor_printf (current_monitor->getmem.cmdb, memaddr, len);
1208 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1209 present), we will send TERM_CMD if that is present. In any case, we collect
1210 all of the output into buf, and then wait for the normal prompt. */
1212 if (current_monitor->getmem.term)
1214 resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
1217 error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.",
1218 memaddr, resp_len, buf);
1220 if (current_monitor->getmem.term_cmd)
1222 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
1223 strlen (current_monitor->getmem.term_cmd));
1224 monitor_expect_prompt (NULL, 0);
1228 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
1232 /* If RESP_DELIM is specified, we search for that as a leading delimiter for
1233 the values. Otherwise, we just start searching from the start of the buf.
1236 if (current_monitor->getmem.resp_delim)
1239 struct re_registers resp_strings;
1242 retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp,
1246 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1247 memaddr, resp_len, buf);
1249 p += resp_strings.end[0];
1251 p = strstr (p, current_monitor->getmem.resp_delim);
1253 error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.",
1254 memaddr, resp_len, buf);
1255 p += strlen (current_monitor->getmem.resp_delim);
1259 for (i = len; i > 0; i--)
1261 /* Skip non-hex chars, but bomb on end of string and newlines */
1267 if (*p == '\000' || *p == '\n' || *p == '\r')
1268 error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr, resp_len, buf);
1272 val = strtoul (p, &p1, 16);
1274 if (val == 0 && p == p1)
1275 error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr,
1290 monitor_xfer_memory (memaddr, myaddr, len, write, target)
1295 struct target_ops *target; /* ignored */
1297 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, write);
1303 return; /* ignore attempts to kill target system */
1306 /* All we actually do is set the PC to the start address of exec_bfd, and start
1307 the program at that point. */
1310 monitor_create_inferior (exec_file, args, env)
1315 if (args && (*args != '\000'))
1316 error ("Args are not supported by the monitor.");
1318 clear_proceed_status ();
1319 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1322 /* Clean up when a program exits.
1323 The program actually lives on in the remote processor's RAM, and may be
1324 run again without a download. Don't leave it full of breakpoint
1328 monitor_mourn_inferior ()
1330 unpush_target (targ_ops);
1331 generic_mourn_inferior (); /* Do all the proper things now */
1334 #define NUM_MONITOR_BREAKPOINTS 8
1336 static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0};
1338 /* Tell the monitor to add a breakpoint. */
1341 monitor_insert_breakpoint (addr, shadow)
1346 static unsigned char break_insn[] = BREAKPOINT;
1348 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
1350 if (breakaddr[i] == 0)
1352 breakaddr[i] = addr;
1353 monitor_read_memory (addr, shadow, sizeof (break_insn));
1354 monitor_printf (SET_BREAK_CMD, addr);
1355 monitor_expect_prompt (NULL, 0);
1360 error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS);
1363 /* Tell the monitor to remove a breakpoint. */
1366 monitor_remove_breakpoint (addr, shadow)
1372 for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++)
1374 if (breakaddr[i] == addr)
1377 /* some monitors remove breakpoints based on the address */
1378 if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR)
1379 monitor_printf (CLR_BREAK_CMD, addr);
1381 monitor_printf (CLR_BREAK_CMD, i);
1382 monitor_expect_prompt (NULL, 0);
1386 fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
1390 /* monitor_load -- download a file. */
1393 monitor_load (file, from_tty)
1397 dcache_flush (remote_dcache);
1399 if (current_monitor->load_routine)
1400 current_monitor->load_routine (monitor_desc, file, hashmark);
1402 { /* The default is ascii S-records */
1403 monitor_printf (LOAD_CMD); /* tell the monitor to load */
1404 if (current_monitor->loadresp)
1405 monitor_expect (current_monitor->loadresp, NULL, 0);
1407 load_srec (monitor_desc, file, 32, SREC_ALL, hashmark);
1409 monitor_expect_prompt (NULL, 0);
1412 /* Finally, make the PC point at the start address */
1415 write_pc (bfd_get_start_address (exec_bfd));
1417 inferior_pid = 0; /* No process now */
1419 /* This is necessary because many things were based on the PC at the time that
1420 we attached to the monitor, which is no longer valid now that we have loaded
1421 new code (and just changed the PC). Another way to do this might be to call
1422 normal_stop, except that the stack may not be valid, and things would get
1423 horribly confused... */
1425 clear_symtab_users ();
1431 if ((current_monitor->flags & MO_SEND_BREAK_ON_STOP) != 0)
1432 SERIAL_SEND_BREAK (monitor_desc);
1433 if (current_monitor->stop)
1434 monitor_printf_noecho (current_monitor->stop);
1437 /* Put a command string, in args, out to MONITOR. Output from MONITOR
1438 is placed on the users terminal until the prompt is seen. FIXME: We
1439 read the characters ourseleves here cause of a nasty echo. */
1442 monitor_command (args, from_tty)
1450 if (monitor_desc == NULL)
1451 error ("monitor target not open.");
1455 /* Send the command. Note that if no args were supplied, then we're
1456 just sending the monitor a newline, which is sometimes useful. */
1458 monitor_printf ("%s\r", (args ? args : ""));
1460 resp_len = monitor_expect_prompt (buf, sizeof buf);
1462 fputs_unfiltered (buf, gdb_stdout); /* Output the response */
1465 /* Convert hex digit A to a number. */
1471 if (a >= '0' && a <= '9')
1473 if (a >= 'a' && a <= 'f')
1474 return a - 'a' + 10;
1475 if (a >= 'A' && a <= 'F')
1476 return a - 'A' + 10;
1478 error ("Reply contains invalid hex digit 0x%x", a);
1481 static struct target_ops monitor_ops =
1483 NULL, /* to_shortname */
1484 NULL, /* to_longname */
1487 monitor_close, /* to_close */
1488 NULL, /* to_attach */
1489 monitor_detach, /* to_detach */
1490 monitor_resume, /* to_resume */
1491 monitor_wait, /* to_wait */
1492 monitor_fetch_registers, /* to_fetch_registers */
1493 monitor_store_registers, /* to_store_registers */
1494 monitor_prepare_to_store, /* to_prepare_to_store */
1495 monitor_xfer_memory, /* to_xfer_memory */
1496 monitor_files_info, /* to_files_info */
1497 monitor_insert_breakpoint, /* to_insert_breakpoint */
1498 monitor_remove_breakpoint, /* to_remove_breakpoint */
1499 0, /* to_terminal_init */
1500 0, /* to_terminal_inferior */
1501 0, /* to_terminal_ours_for_output */
1502 0, /* to_terminal_ours */
1503 0, /* to_terminal_info */
1504 monitor_kill, /* to_kill */
1505 monitor_load, /* to_load */
1506 0, /* to_lookup_symbol */
1507 monitor_create_inferior, /* to_create_inferior */
1508 monitor_mourn_inferior, /* to_mourn_inferior */
1510 0, /* to_notice_signals */
1511 0, /* to_thread_alive */
1512 monitor_stop, /* to_stop */
1513 process_stratum, /* to_stratum */
1515 1, /* to_has_all_memory */
1516 1, /* to_has_memory */
1517 1, /* to_has_stack */
1518 1, /* to_has_registers */
1519 1, /* to_has_execution */
1521 0, /* sections_end */
1522 OPS_MAGIC /* to_magic */
1525 /* Init the target_ops structure pointed at by OPS */
1528 init_monitor_ops (ops)
1529 struct target_ops *ops;
1531 memcpy (ops, &monitor_ops, sizeof monitor_ops);
1534 /* Define additional commands that are usually only used by monitors. */
1537 _initialize_remote_monitors ()
1539 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
1541 "Set display of activity while downloading a file.\n\
1542 When enabled, a hashmark \'#\' is displayed.",
1546 add_com ("monitor", class_obscure, monitor_command,
1547 "Send a command to the debug monitor.");