1 /* Remote debugging interface for boot monitors, for GDB.
2 Copyright 1990-1993, 1995-1997, 1999-2000, 2001 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
4 Resurrected from the ashes by Stu Grossman.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 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 existence, 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 */
45 #include "gdb_string.h"
46 #include <sys/types.h>
52 #include "gdb_regex.h"
55 static char *dev_name;
56 static struct target_ops *targ_ops;
58 static void monitor_vsprintf (char *sndbuf, char *pattern, va_list args);
60 static int readchar (int timeout);
62 static void monitor_fetch_register (int regno);
63 static void monitor_store_register (int regno);
65 static void monitor_printable_string (char *newstr, char *oldstr, int len);
66 static void monitor_error (char *function, char *message, CORE_ADDR memaddr, int len, char *string, int final_char);
67 static void monitor_detach (char *args, int from_tty);
68 static void monitor_resume (int pid, int step, enum target_signal sig);
69 static void monitor_interrupt (int signo);
70 static void monitor_interrupt_twice (int signo);
71 static void monitor_interrupt_query (void);
72 static void monitor_wait_cleanup (void *old_timeout);
74 static int monitor_wait (int pid, struct target_waitstatus *status);
75 static void monitor_fetch_registers (int regno);
76 static void monitor_store_registers (int regno);
77 static void monitor_prepare_to_store (void);
78 static int monitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
80 struct mem_attrib *attrib,
81 struct target_ops *target);
82 static void monitor_files_info (struct target_ops *ops);
83 static int monitor_insert_breakpoint (CORE_ADDR addr, char *shadow);
84 static int monitor_remove_breakpoint (CORE_ADDR addr, char *shadow);
85 static void monitor_kill (void);
86 static void monitor_load (char *file, int from_tty);
87 static void monitor_mourn_inferior (void);
88 static void monitor_stop (void);
90 static int monitor_read_memory (CORE_ADDR addr, char *myaddr, int len);
91 static int monitor_write_memory (CORE_ADDR addr, char *myaddr, int len);
92 static int monitor_write_memory_bytes (CORE_ADDR addr, char *myaddr, int len);
93 static int monitor_write_memory_block (CORE_ADDR memaddr,
94 char *myaddr, int len);
95 static int monitor_expect_regexp (struct re_pattern_buffer *pat,
96 char *buf, int buflen);
97 static void monitor_dump_regs (void);
99 static int from_hex (int a);
100 static unsigned long get_hex_word (void);
102 static void parse_register_dump (char *, int);
104 static struct monitor_ops *current_monitor;
106 static int hashmark; /* flag set by "set hash" */
108 static int timeout = 30;
110 static int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */
112 static void (*ofunc) (); /* Old SIGINT signal handler */
114 static CORE_ADDR *breakaddr;
116 /* Descriptor for I/O to remote machine. Initialize it to NULL so
117 that monitor_open knows that we don't have a file open when the
120 static serial_t monitor_desc = NULL;
122 /* Pointer to regexp pattern matching data */
124 static struct re_pattern_buffer register_pattern;
125 static char register_fastmap[256];
127 static struct re_pattern_buffer getmem_resp_delim_pattern;
128 static char getmem_resp_delim_fastmap[256];
130 static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
131 monitor_wait wakes up. */
133 static int first_time = 0; /* is this the first time we're executing after
134 gaving created the child proccess? */
136 #define TARGET_BUF_SIZE 2048
138 /* Monitor specific debugging information. Typically only useful to
139 the developer of a new monitor interface. */
141 static void monitor_debug (const char *fmt, ...) ATTR_FORMAT(printf, 1, 2);
143 static int monitor_debug_p = 0;
145 /* NOTE: This file alternates between monitor_debug_p and remote_debug
146 when determining if debug information is printed. Perhaphs this
147 could be simplified. */
150 monitor_debug (const char *fmt, ...)
155 va_start (args, fmt);
156 vfprintf_filtered (gdb_stdlog, fmt, args);
162 /* Convert a string into a printable representation, Return # byte in
163 the new string. When LEN is >0 it specifies the size of the
164 string. Otherwize strlen(oldstr) is used. */
167 monitor_printable_string (char *newstr, char *oldstr, int len)
173 len = strlen (oldstr);
175 for (i = 0; i < len; i++)
186 sprintf (newstr, "\\x%02x", ch & 0xff);
225 /* Print monitor errors with a string, converting the string to printable
229 monitor_error (char *function, char *message,
230 CORE_ADDR memaddr, int len, char *string, int final_char)
232 int real_len = (len == 0 && string != (char *) 0) ? strlen (string) : len;
233 char *safe_string = alloca ((real_len * 4) + 1);
234 monitor_printable_string (safe_string, string, real_len);
237 error ("%s (0x%s): %s: %s%c", function, paddr_nz (memaddr), message, safe_string, final_char);
239 error ("%s (0x%s): %s: %s", function, paddr_nz (memaddr), message, safe_string);
242 /* Convert hex digit A to a number. */
247 if (a >= '0' && a <= '9')
249 else if (a >= 'a' && a <= 'f')
251 else if (a >= 'A' && a <= 'F')
254 error ("Invalid hex digit %d", a);
257 /* monitor_vsprintf - similar to vsprintf but handles 64-bit addresses
259 This function exists to get around the problem that many host platforms
260 don't have a printf that can print 64-bit addresses. The %A format
261 specification is recognized as a special case, and causes the argument
262 to be printed as a 64-bit hexadecimal address.
264 Only format specifiers of the form "[0-9]*[a-z]" are recognized.
265 If it is a '%s' format, the argument is a string; otherwise the
266 argument is assumed to be a long integer.
268 %% is also turned into a single %.
272 monitor_vsprintf (char *sndbuf, char *pattern, va_list args)
282 for (p = pattern; *p; p++)
286 /* Copy the format specifier to a separate buffer. */
288 for (i = 1; *p >= '0' && *p <= '9' && i < (int) sizeof (format) - 2;
291 format[i] = fmt = *p;
292 format[i + 1] = '\0';
294 /* Fetch the next argument and print it. */
298 strcpy (sndbuf, "%");
301 arg_addr = va_arg (args, CORE_ADDR);
302 strcpy (sndbuf, paddr_nz (arg_addr));
305 arg_string = va_arg (args, char *);
306 sprintf (sndbuf, format, arg_string);
309 arg_int = va_arg (args, long);
310 sprintf (sndbuf, format, arg_int);
313 sndbuf += strlen (sndbuf);
322 /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
323 Works just like printf. */
326 monitor_printf_noecho (char *pattern,...)
332 va_start (args, pattern);
334 monitor_vsprintf (sndbuf, pattern, args);
336 len = strlen (sndbuf);
337 if (len + 1 > sizeof sndbuf)
338 internal_error (__FILE__, __LINE__, "failed internal consistency check");
342 char *safe_string = (char *) alloca ((strlen (sndbuf) * 4) + 1);
343 monitor_printable_string (safe_string, sndbuf, 0);
344 fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string);
347 monitor_write (sndbuf, len);
350 /* monitor_printf -- Send data to monitor and check the echo. Works just like
354 monitor_printf (char *pattern,...)
360 va_start (args, pattern);
362 monitor_vsprintf (sndbuf, pattern, args);
364 len = strlen (sndbuf);
365 if (len + 1 > sizeof sndbuf)
366 internal_error (__FILE__, __LINE__, "failed internal consistency check");
370 char *safe_string = (char *) alloca ((len * 4) + 1);
371 monitor_printable_string (safe_string, sndbuf, 0);
372 fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string);
375 monitor_write (sndbuf, len);
377 /* We used to expect that the next immediate output was the characters we
378 just output, but sometimes some extra junk appeared before the characters
379 we expected, like an extra prompt, or a portmaster sending telnet negotiations.
380 So, just start searching for what we sent, and skip anything unknown. */
381 monitor_debug ("ExpectEcho\n");
382 monitor_expect (sndbuf, (char *) 0, 0);
386 /* Write characters to the remote system. */
389 monitor_write (char *buf, int buflen)
391 if (SERIAL_WRITE (monitor_desc, buf, buflen))
392 fprintf_unfiltered (gdb_stderr, "SERIAL_WRITE failed: %s\n",
393 safe_strerror (errno));
397 /* Read a binary character from the remote system, doing all the fancy
398 timeout stuff, but without interpreting the character in any way,
399 and without printing remote debug information. */
402 monitor_readchar (void)
410 c = SERIAL_READCHAR (monitor_desc, timeout);
413 c &= 0xff; /* don't lose bit 7 */
420 if (c == SERIAL_TIMEOUT)
421 error ("Timeout reading from remote system.");
423 perror_with_name ("remote-monitor");
427 /* Read a character from the remote system, doing all the fancy
431 readchar (int timeout)
436 last_random, last_nl, last_cr, last_crnl
444 c = SERIAL_READCHAR (monitor_desc, timeout);
449 /* This seems to interfere with proper function of the
451 if (monitor_debug_p || remote_debug)
456 puts_debug ("read -->", buf, "<--");
461 /* Canonicialize \n\r combinations into one \r */
462 if ((current_monitor->flags & MO_HANDLE_NL) != 0)
464 if ((c == '\r' && state == last_nl)
465 || (c == '\n' && state == last_cr))
486 if (c == SERIAL_TIMEOUT)
488 /* I fail to see how detaching here can be useful */
489 if (in_monitor_wait) /* Watchdog went off */
491 target_mourn_inferior ();
492 error ("GDB serial timeout has expired. Target detached.\n");
496 error ("Timeout reading from remote system.");
498 perror_with_name ("remote-monitor");
501 /* Scan input from the remote system, until STRING is found. If BUF is non-
502 zero, then collect input until we have collected either STRING or BUFLEN-1
503 chars. In either case we terminate BUF with a 0. If input overflows BUF
504 because STRING can't be found, return -1, else return number of chars in BUF
505 (minus the terminating NUL). Note that in the non-overflow case, STRING
506 will be at the end of BUF. */
509 monitor_expect (char *string, char *buf, int buflen)
512 int obuflen = buflen;
514 extern struct target_ops *targ_ops;
518 char *safe_string = (char *) alloca ((strlen (string) * 4) + 1);
519 monitor_printable_string (safe_string, string, 0);
520 fprintf_unfiltered (gdb_stdlog, "MON Expecting '%s'\n", safe_string);
535 c = readchar (timeout);
542 c = readchar (timeout);
544 /* Don't expect any ^C sent to be echoed */
546 if (*p == '\003' || c == *p)
556 return obuflen - buflen;
562 else if ((c == '\021' || c == '\023') &&
563 (STREQ (targ_ops->to_shortname, "m32r")
564 || STREQ (targ_ops->to_shortname, "mon2000")))
565 { /* m32r monitor emits random DC1/DC3 chars */
570 /* We got a character that doesn't match the string. We need to
571 back up p, but how far? If we're looking for "..howdy" and the
572 monitor sends "...howdy"? There's certainly a match in there,
573 but when we receive the third ".", we won't find it if we just
574 restart the matching at the beginning of the string.
576 This is a Boyer-Moore kind of situation. We want to reset P to
577 the end of the longest prefix of STRING that is a suffix of
578 what we've read so far. In the example above, that would be
579 ".." --- the longest prefix of "..howdy" that is a suffix of
580 "...". This longest prefix could be the empty string, if C
581 is nowhere to be found in STRING.
583 If this longest prefix is not the empty string, it must contain
584 C, so let's search from the end of STRING for instances of C,
585 and see if the portion of STRING before that is a suffix of
586 what we read before C. Actually, we can search backwards from
587 p, since we know no prefix can be longer than that.
589 Note that we can use STRING itself, along with C, as a record
590 of what we've received so far. :) */
593 for (i = (p - string) - 1; i >= 0; i--)
596 /* Is this prefix a suffix of what we've read so far?
598 string[0 .. i-1] == string[p - i, p - 1]? */
599 if (! memcmp (string, p - i, i))
611 /* Search for a regexp. */
614 monitor_expect_regexp (struct re_pattern_buffer *pat, char *buf, int buflen)
618 monitor_debug ("MON Expecting regexp\n");
623 mybuf = alloca (TARGET_BUF_SIZE);
624 buflen = TARGET_BUF_SIZE;
632 if (p - mybuf >= buflen)
633 { /* Buffer about to overflow */
635 /* On overflow, we copy the upper half of the buffer to the lower half. Not
636 great, but it usually works... */
638 memcpy (mybuf, mybuf + buflen / 2, buflen / 2);
639 p = mybuf + buflen / 2;
642 *p++ = readchar (timeout);
644 retval = re_search (pat, mybuf, p - mybuf, 0, p - mybuf, NULL);
650 /* Keep discarding input until we see the MONITOR prompt.
652 The convention for dealing with the prompt is that you
654 o *then* wait for the prompt.
656 Thus the last thing that a procedure does with the serial line will
657 be an monitor_expect_prompt(). Exception: monitor_resume does not
658 wait for the prompt, because the terminal is being handed over to
659 the inferior. However, the next thing which happens after that is
660 a monitor_wait which does wait for the prompt. Note that this
661 includes abnormal exit, e.g. error(). This is necessary to prevent
662 getting into states from which we can't recover. */
665 monitor_expect_prompt (char *buf, int buflen)
667 monitor_debug ("MON Expecting prompt\n");
668 return monitor_expect (current_monitor->prompt, buf, buflen);
671 /* Get N 32-bit words from remote, each preceded by a space, and put
672 them in registers starting at REGNO. */
683 ch = readchar (timeout);
684 while (isspace (ch));
688 for (i = 7; i >= 1; i--)
690 ch = readchar (timeout);
693 val = (val << 4) | from_hex (ch);
701 compile_pattern (char *pattern, struct re_pattern_buffer *compiled_pattern,
707 compiled_pattern->fastmap = fastmap;
709 tmp = re_set_syntax (RE_SYNTAX_EMACS);
710 val = re_compile_pattern (pattern,
716 error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern, val);
719 re_compile_fastmap (compiled_pattern);
722 /* Open a connection to a remote debugger. NAME is the filename used
723 for communication. */
726 monitor_open (char *args, struct monitor_ops *mon_ops, int from_tty)
731 if (mon_ops->magic != MONITOR_OPS_MAGIC)
732 error ("Magic number of monitor_ops struct wrong.");
734 targ_ops = mon_ops->target;
735 name = targ_ops->to_shortname;
738 error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
739 `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
741 target_preopen (from_tty);
743 /* Setup pattern for register dump */
745 if (mon_ops->register_pattern)
746 compile_pattern (mon_ops->register_pattern, ®ister_pattern,
749 if (mon_ops->getmem.resp_delim)
750 compile_pattern (mon_ops->getmem.resp_delim, &getmem_resp_delim_pattern,
751 getmem_resp_delim_fastmap);
753 unpush_target (targ_ops);
757 dev_name = xstrdup (args);
759 monitor_desc = SERIAL_OPEN (dev_name);
762 perror_with_name (dev_name);
766 if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate))
768 SERIAL_CLOSE (monitor_desc);
769 perror_with_name (dev_name);
773 SERIAL_RAW (monitor_desc);
775 SERIAL_FLUSH_INPUT (monitor_desc);
777 /* some systems only work with 2 stop bits */
779 SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits);
781 current_monitor = mon_ops;
783 /* See if we can wake up the monitor. First, try sending a stop sequence,
784 then send the init strings. Last, remove all breakpoints. */
786 if (current_monitor->stop)
789 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
791 monitor_debug ("EXP Open echo\n");
792 monitor_expect_prompt (NULL, 0);
796 /* wake up the monitor and see if it's alive */
797 for (p = mon_ops->init; *p != NULL; p++)
799 /* Some of the characters we send may not be echoed,
800 but we hope to get a prompt at the end of it all. */
802 if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0)
805 monitor_printf_noecho (*p);
806 monitor_expect_prompt (NULL, 0);
809 SERIAL_FLUSH_INPUT (monitor_desc);
811 /* Alloc breakpoints */
812 if (mon_ops->set_break != NULL)
814 if (mon_ops->num_breakpoints == 0)
815 mon_ops->num_breakpoints = 8;
817 breakaddr = (CORE_ADDR *) xmalloc (mon_ops->num_breakpoints * sizeof (CORE_ADDR));
818 memset (breakaddr, 0, mon_ops->num_breakpoints * sizeof (CORE_ADDR));
821 /* Remove all breakpoints */
823 if (mon_ops->clr_all_break)
825 monitor_printf (mon_ops->clr_all_break);
826 monitor_expect_prompt (NULL, 0);
830 printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name);
832 push_target (targ_ops);
834 inferior_pid = 42000; /* Make run command think we are busy... */
836 /* Give monitor_wait something to read */
838 monitor_printf (current_monitor->line_term);
843 /* Close out all files and local state before this target loses
847 monitor_close (int quitting)
850 SERIAL_CLOSE (monitor_desc);
852 /* Free breakpoint memory */
853 if (breakaddr != NULL)
862 /* Terminate the open connection to the remote debugger. Use this
863 when you want to detach and do something else with your gdb. */
866 monitor_detach (char *args, int from_tty)
868 pop_target (); /* calls monitor_close to do the real work */
870 printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
873 /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */
876 monitor_supply_register (int regno, char *valstr)
879 unsigned char regbuf[MAX_REGISTER_RAW_SIZE];
884 while (p && *p != '\0')
886 if (*p == '\r' || *p == '\n')
897 if (!isxdigit (*p) && *p != 'x')
903 val += fromhex (*p++);
905 monitor_debug ("Supplying Register %d %s\n", regno, valstr);
908 error ("monitor_supply_register (%d): bad value from monitor: %s.",
911 /* supply register stores in target byte order, so swap here */
913 store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val);
915 supply_register (regno, regbuf);
920 /* Tell the remote machine to resume. */
923 monitor_resume (int pid, int step, enum target_signal sig)
925 /* Some monitors require a different command when starting a program */
926 monitor_debug ("MON resume\n");
927 if (current_monitor->flags & MO_RUN_FIRST_TIME && first_time == 1)
930 monitor_printf ("run\r");
931 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
936 monitor_printf (current_monitor->step);
939 if (current_monitor->continue_hook)
940 (*current_monitor->continue_hook) ();
942 monitor_printf (current_monitor->cont);
943 if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT)
948 /* Parse the output of a register dump command. A monitor specific
949 regexp is used to extract individual register descriptions of the
950 form REG=VAL. Each description is split up into a name and a value
951 string which are passed down to monitor specific code. */
954 parse_register_dump (char *buf, int len)
956 monitor_debug ("MON Parsing register dump\n");
959 int regnamelen, vallen;
961 /* Element 0 points to start of register name, and element 1
962 points to the start of the register value. */
963 struct re_registers register_strings;
965 memset (®ister_strings, 0, sizeof (struct re_registers));
967 if (re_search (®ister_pattern, buf, len, 0, len,
968 ®ister_strings) == -1)
971 regnamelen = register_strings.end[1] - register_strings.start[1];
972 regname = buf + register_strings.start[1];
973 vallen = register_strings.end[2] - register_strings.start[2];
974 val = buf + register_strings.start[2];
976 current_monitor->supply_register (regname, regnamelen, val, vallen);
978 buf += register_strings.end[0];
979 len -= register_strings.end[0];
983 /* Send ^C to target to halt it. Target will respond, and send us a
987 monitor_interrupt (int signo)
989 /* If this doesn't work, try more severe steps. */
990 signal (signo, monitor_interrupt_twice);
992 if (monitor_debug_p || remote_debug)
993 fprintf_unfiltered (gdb_stdlog, "monitor_interrupt called\n");
998 /* The user typed ^C twice. */
1001 monitor_interrupt_twice (int signo)
1003 signal (signo, ofunc);
1005 monitor_interrupt_query ();
1007 signal (signo, monitor_interrupt);
1010 /* Ask the user what to do when an interrupt is received. */
1013 monitor_interrupt_query (void)
1015 target_terminal_ours ();
1017 if (query ("Interrupted while waiting for the program.\n\
1018 Give up (and stop debugging it)? "))
1020 target_mourn_inferior ();
1021 return_to_top_level (RETURN_QUIT);
1024 target_terminal_inferior ();
1028 monitor_wait_cleanup (void *old_timeout)
1030 timeout = *(int *) old_timeout;
1031 signal (SIGINT, ofunc);
1032 in_monitor_wait = 0;
1038 monitor_wait_filter (char *buf,
1041 struct target_waitstatus *status
1047 resp_len = monitor_expect_prompt (buf, bufmax);
1048 *ext_resp_len = resp_len;
1051 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
1053 while (resp_len < 0);
1055 /* Print any output characters that were preceded by ^O. */
1056 /* FIXME - This would be great as a user settabgle flag */
1057 if (monitor_debug_p || remote_debug
1058 || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT)
1062 for (i = 0; i < resp_len - 1; i++)
1064 putchar_unfiltered (buf[++i]);
1070 /* Wait until the remote machine stops, then return, storing status in
1071 status just as `wait' would. */
1074 monitor_wait (int pid, struct target_waitstatus *status)
1076 int old_timeout = timeout;
1077 char buf[TARGET_BUF_SIZE];
1079 struct cleanup *old_chain;
1081 status->kind = TARGET_WAITKIND_EXITED;
1082 status->value.integer = 0;
1084 old_chain = make_cleanup (monitor_wait_cleanup, &old_timeout);
1085 monitor_debug ("MON wait\n");
1088 /* This is somthing other than a maintenance command */
1089 in_monitor_wait = 1;
1090 timeout = watchdog > 0 ? watchdog : -1;
1092 timeout = -1; /* Don't time out -- user program is running. */
1095 ofunc = (void (*)()) signal (SIGINT, monitor_interrupt);
1097 if (current_monitor->wait_filter)
1098 (*current_monitor->wait_filter) (buf, sizeof (buf), &resp_len, status);
1100 monitor_wait_filter (buf, sizeof (buf), &resp_len, status);
1102 #if 0 /* Transferred to monitor wait filter */
1105 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1108 fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf);
1110 while (resp_len < 0);
1112 /* Print any output characters that were preceded by ^O. */
1113 /* FIXME - This would be great as a user settabgle flag */
1114 if (monitor_debug_p || remote_debug
1115 || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT)
1119 for (i = 0; i < resp_len - 1; i++)
1121 putchar_unfiltered (buf[++i]);
1125 signal (SIGINT, ofunc);
1127 timeout = old_timeout;
1129 if (dump_reg_flag && current_monitor->dump_registers)
1132 monitor_printf (current_monitor->dump_registers);
1133 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1136 if (current_monitor->register_pattern)
1137 parse_register_dump (buf, resp_len);
1139 monitor_debug ("Wait fetching registers after stop\n");
1140 monitor_dump_regs ();
1143 status->kind = TARGET_WAITKIND_STOPPED;
1144 status->value.sig = TARGET_SIGNAL_TRAP;
1146 discard_cleanups (old_chain);
1148 in_monitor_wait = 0;
1150 return inferior_pid;
1153 /* Fetch register REGNO, or all registers if REGNO is -1. Returns
1157 monitor_fetch_register (int regno)
1164 regbuf = alloca (MAX_REGISTER_RAW_SIZE * 2 + 1);
1165 zerobuf = alloca (MAX_REGISTER_RAW_SIZE);
1166 memset (zerobuf, 0, MAX_REGISTER_RAW_SIZE);
1168 name = current_monitor->regnames[regno];
1169 monitor_debug ("MON fetchreg %d '%s'\n", regno, name ? name : "(null name)");
1171 if (!name || (*name == '\0'))
1173 monitor_debug ("No register known for %d\n", regno);
1174 supply_register (regno, zerobuf);
1178 /* send the register examine command */
1180 monitor_printf (current_monitor->getreg.cmd, name);
1182 /* If RESP_DELIM is specified, we search for that as a leading
1183 delimiter for the register value. Otherwise, we just start
1184 searching from the start of the buf. */
1186 if (current_monitor->getreg.resp_delim)
1188 monitor_debug ("EXP getreg.resp_delim\n");
1189 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
1190 /* Handle case of first 32 registers listed in pairs. */
1191 if (current_monitor->flags & MO_32_REGS_PAIRED
1192 && (regno & 1) != 0 && regno < 32)
1194 monitor_debug ("EXP getreg.resp_delim\n");
1195 monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
1199 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set */
1200 if (current_monitor->flags & MO_HEX_PREFIX)
1203 c = readchar (timeout);
1205 c = readchar (timeout);
1206 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
1209 error ("Bad value returned from monitor while fetching register %x.",
1213 /* Read upto the maximum number of hex digits for this register, skipping
1214 spaces, but stop reading if something else is seen. Some monitors
1215 like to drop leading zeros. */
1217 for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++)
1220 c = readchar (timeout);
1222 c = readchar (timeout);
1230 regbuf[i] = '\000'; /* terminate the number */
1231 monitor_debug ("REGVAL '%s'\n", regbuf);
1233 /* If TERM is present, we wait for that to show up. Also, (if TERM
1234 is present), we will send TERM_CMD if that is present. In any
1235 case, we collect all of the output into buf, and then wait for
1236 the normal prompt. */
1238 if (current_monitor->getreg.term)
1240 monitor_debug ("EXP getreg.term\n");
1241 monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */
1244 if (current_monitor->getreg.term_cmd)
1246 monitor_debug ("EMIT getreg.term.cmd\n");
1247 monitor_printf (current_monitor->getreg.term_cmd);
1249 if (!current_monitor->getreg.term || /* Already expected or */
1250 current_monitor->getreg.term_cmd) /* ack expected */
1251 monitor_expect_prompt (NULL, 0); /* get response */
1253 monitor_supply_register (regno, regbuf);
1256 /* Sometimes, it takes several commands to dump the registers */
1257 /* This is a primitive for use by variations of monitor interfaces in
1258 case they need to compose the operation.
1261 monitor_dump_reg_block (char *block_cmd)
1263 char buf[TARGET_BUF_SIZE];
1265 monitor_printf (block_cmd);
1266 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1267 parse_register_dump (buf, resp_len);
1272 /* Read the remote registers into the block regs. */
1273 /* Call the specific function if it has been provided */
1276 monitor_dump_regs (void)
1278 char buf[TARGET_BUF_SIZE];
1280 if (current_monitor->dumpregs)
1281 (*(current_monitor->dumpregs)) (); /* call supplied function */
1282 else if (current_monitor->dump_registers) /* default version */
1284 monitor_printf (current_monitor->dump_registers);
1285 resp_len = monitor_expect_prompt (buf, sizeof (buf));
1286 parse_register_dump (buf, resp_len);
1289 internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Need some way to read registers */
1293 monitor_fetch_registers (int regno)
1295 monitor_debug ("MON fetchregs\n");
1296 if (current_monitor->getreg.cmd)
1300 monitor_fetch_register (regno);
1304 for (regno = 0; regno < NUM_REGS; regno++)
1305 monitor_fetch_register (regno);
1309 monitor_dump_regs ();
1313 /* Store register REGNO, or all if REGNO == 0. Return errno value. */
1316 monitor_store_register (int regno)
1321 name = current_monitor->regnames[regno];
1322 if (!name || (*name == '\0'))
1324 monitor_debug ("MON Cannot store unknown register\n");
1328 val = read_register (regno);
1329 monitor_debug ("MON storeg %d %s\n", regno,
1330 phex (val, REGISTER_RAW_SIZE (regno)));
1332 /* send the register deposit command */
1334 if (current_monitor->flags & MO_REGISTER_VALUE_FIRST)
1335 monitor_printf (current_monitor->setreg.cmd, val, name);
1336 else if (current_monitor->flags & MO_SETREG_INTERACTIVE)
1337 monitor_printf (current_monitor->setreg.cmd, name);
1339 monitor_printf (current_monitor->setreg.cmd, name, val);
1341 if (current_monitor->setreg.term)
1343 monitor_debug ("EXP setreg.term\n");
1344 monitor_expect (current_monitor->setreg.term, NULL, 0);
1345 if (current_monitor->flags & MO_SETREG_INTERACTIVE)
1346 monitor_printf ("%s\r", paddr_nz (val));
1347 monitor_expect_prompt (NULL, 0);
1350 monitor_expect_prompt (NULL, 0);
1351 if (current_monitor->setreg.term_cmd) /* Mode exit required */
1353 monitor_debug ("EXP setreg_termcmd\n");
1354 monitor_printf ("%s", current_monitor->setreg.term_cmd);
1355 monitor_expect_prompt (NULL, 0);
1357 } /* monitor_store_register */
1359 /* Store the remote registers. */
1362 monitor_store_registers (int regno)
1366 monitor_store_register (regno);
1370 for (regno = 0; regno < NUM_REGS; regno++)
1371 monitor_store_register (regno);
1374 /* Get ready to modify the registers array. On machines which store
1375 individual registers, this doesn't need to do anything. On machines
1376 which store all the registers in one fell swoop, this makes sure
1377 that registers contains all the registers from the program being
1381 monitor_prepare_to_store (void)
1383 /* Do nothing, since we can store individual regs */
1387 monitor_files_info (struct target_ops *ops)
1389 printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate);
1393 monitor_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
1395 unsigned int val, hostval;
1399 monitor_debug ("MON write %d %s\n", len, paddr (memaddr));
1401 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
1402 memaddr = ADDR_BITS_REMOVE (memaddr);
1404 /* Use memory fill command for leading 0 bytes. */
1406 if (current_monitor->fill)
1408 for (i = 0; i < len; i++)
1412 if (i > 4) /* More than 4 zeros is worth doing */
1414 monitor_debug ("MON FILL %d\n", i);
1415 if (current_monitor->flags & MO_FILL_USES_ADDR)
1416 monitor_printf (current_monitor->fill, memaddr, (memaddr + i) - 1, 0);
1418 monitor_printf (current_monitor->fill, memaddr, i, 0);
1420 monitor_expect_prompt (NULL, 0);
1427 /* Can't actually use long longs if VAL is an int (nice idea, though). */
1428 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll)
1431 cmd = current_monitor->setmem.cmdll;
1435 if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl)
1438 cmd = current_monitor->setmem.cmdl;
1440 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw)
1443 cmd = current_monitor->setmem.cmdw;
1448 cmd = current_monitor->setmem.cmdb;
1451 val = extract_unsigned_integer (myaddr, len);
1455 hostval = *(unsigned int *) myaddr;
1456 monitor_debug ("Hostval(%08x) val(%08x)\n", hostval, val);
1460 if (current_monitor->flags & MO_NO_ECHO_ON_SETMEM)
1461 monitor_printf_noecho (cmd, memaddr, val);
1462 else if (current_monitor->flags & MO_SETMEM_INTERACTIVE)
1465 monitor_printf_noecho (cmd, memaddr);
1467 if (current_monitor->setmem.term)
1469 monitor_debug ("EXP setmem.term");
1470 monitor_expect (current_monitor->setmem.term, NULL, 0);
1471 monitor_printf ("%x\r", val);
1473 if (current_monitor->setmem.term_cmd)
1474 { /* Emit this to get out of the memory editing state */
1475 monitor_printf ("%s", current_monitor->setmem.term_cmd);
1476 /* Drop through to expecting a prompt */
1480 monitor_printf (cmd, memaddr, val);
1482 monitor_expect_prompt (NULL, 0);
1489 monitor_write_even_block (CORE_ADDR memaddr, char *myaddr, int len)
1493 /* Enter the sub mode */
1494 monitor_printf (current_monitor->setmem.cmdl, memaddr);
1495 monitor_expect_prompt (NULL, 0);
1499 val = extract_unsigned_integer (myaddr, 4); /* REALLY */
1500 monitor_printf ("%x\r", val);
1504 monitor_debug (" @ %s\n", paddr (memaddr));
1505 /* If we wanted to, here we could validate the address */
1506 monitor_expect_prompt (NULL, 0);
1508 /* Now exit the sub mode */
1509 monitor_printf (current_monitor->getreg.term_cmd);
1510 monitor_expect_prompt (NULL, 0);
1516 monitor_write_memory_bytes (CORE_ADDR memaddr, char *myaddr, int len)
1522 /* Enter the sub mode */
1523 monitor_printf (current_monitor->setmem.cmdb, memaddr);
1524 monitor_expect_prompt (NULL, 0);
1528 monitor_printf ("%x\r", val);
1532 /* If we wanted to, here we could validate the address */
1533 monitor_expect_prompt (NULL, 0);
1536 /* Now exit the sub mode */
1537 monitor_printf (current_monitor->getreg.term_cmd);
1538 monitor_expect_prompt (NULL, 0);
1544 longlongendswap (unsigned char *a)
1553 *(a + i) = *(a + j);
1558 /* Format 32 chars of long long value, advance the pointer */
1559 static char *hexlate = "0123456789abcdef";
1561 longlong_hexchars (unsigned long long value,
1571 static unsigned char disbuf[8]; /* disassembly buffer */
1572 unsigned char *scan, *limit; /* loop controls */
1573 unsigned char c, nib;
1578 unsigned long long *dp;
1579 dp = (unsigned long long *) scan;
1582 longlongendswap (disbuf); /* FIXME: ONly on big endian hosts */
1583 while (scan < limit)
1585 c = *scan++; /* a byte of our long long value */
1591 leadzero = 0; /* henceforth we print even zeroes */
1593 nib = c >> 4; /* high nibble bits */
1594 *outbuff++ = hexlate[nib];
1595 nib = c & 0x0f; /* low nibble bits */
1596 *outbuff++ = hexlate[nib];
1600 } /* longlong_hexchars */
1604 /* I am only going to call this when writing virtual byte streams.
1605 Which possably entails endian conversions
1608 monitor_write_memory_longlongs (CORE_ADDR memaddr, char *myaddr, int len)
1610 static char hexstage[20]; /* At least 16 digits required, plus null */
1615 llptr = (unsigned long long *) myaddr;
1618 monitor_printf (current_monitor->setmem.cmdll, memaddr);
1619 monitor_expect_prompt (NULL, 0);
1623 endstring = longlong_hexchars (*llptr, hexstage);
1624 *endstring = '\0'; /* NUll terminate for printf */
1625 monitor_printf ("%s\r", hexstage);
1629 /* If we wanted to, here we could validate the address */
1630 monitor_expect_prompt (NULL, 0);
1633 /* Now exit the sub mode */
1634 monitor_printf (current_monitor->getreg.term_cmd);
1635 monitor_expect_prompt (NULL, 0);
1641 /* ----- MONITOR_WRITE_MEMORY_BLOCK ---------------------------- */
1642 /* This is for the large blocks of memory which may occur in downloading.
1643 And for monitors which use interactive entry,
1644 And for monitors which do not have other downloading methods.
1645 Without this, we will end up calling monitor_write_memory many times
1646 and do the entry and exit of the sub mode many times
1647 This currently assumes...
1648 MO_SETMEM_INTERACTIVE
1649 ! MO_NO_ECHO_ON_SETMEM
1650 To use this, the you have to patch the monitor_cmds block with
1651 this function. Otherwise, its not tuned up for use by all
1656 monitor_write_memory_block (CORE_ADDR memaddr, char *myaddr, int len)
1660 /* FIXME: This would be a good place to put the zero test */
1662 if ((len > 8) && (((len & 0x07)) == 0) && current_monitor->setmem.cmdll)
1664 return monitor_write_memory_longlongs (memaddr, myaddr, len);
1671 written = monitor_write_even_block (memaddr, myaddr, len);
1672 /* Adjust calling parameters by written amount */
1678 written = monitor_write_memory_bytes (memaddr, myaddr, len);
1682 /* This is an alternate form of monitor_read_memory which is used for monitors
1683 which can only read a single byte/word/etc. at a time. */
1686 monitor_read_memory_single (CORE_ADDR memaddr, char *myaddr, int len)
1689 char membuf[sizeof (int) * 2 + 1];
1694 monitor_debug ("MON read single\n");
1696 /* Can't actually use long longs (nice idea, though). In fact, the
1697 call to strtoul below will fail if it tries to convert a value
1698 that's too big to fit in a long. */
1699 if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll)
1702 cmd = current_monitor->getmem.cmdll;
1706 if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl)
1709 cmd = current_monitor->getmem.cmdl;
1711 else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw)
1714 cmd = current_monitor->getmem.cmdw;
1719 cmd = current_monitor->getmem.cmdb;
1722 /* Send the examine command. */
1724 monitor_printf (cmd, memaddr);
1726 /* If RESP_DELIM is specified, we search for that as a leading
1727 delimiter for the memory value. Otherwise, we just start
1728 searching from the start of the buf. */
1730 if (current_monitor->getmem.resp_delim)
1732 monitor_debug ("EXP getmem.resp_delim\n");
1733 monitor_expect_regexp (&getmem_resp_delim_pattern, NULL, 0);
1736 /* Now, read the appropriate number of hex digits for this loc,
1739 /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set. */
1740 if (current_monitor->flags & MO_HEX_PREFIX)
1744 c = readchar (timeout);
1746 c = readchar (timeout);
1747 if ((c == '0') && ((c = readchar (timeout)) == 'x'))
1750 monitor_error ("monitor_read_memory_single",
1751 "bad response from monitor",
1752 memaddr, i, membuf, c);
1754 for (i = 0; i < len * 2; i++)
1760 c = readchar (timeout);
1766 monitor_error ("monitor_read_memory_single",
1767 "bad response from monitor",
1768 memaddr, i, membuf, c);
1774 membuf[i] = '\000'; /* terminate the number */
1776 /* If TERM is present, we wait for that to show up. Also, (if TERM is
1777 present), we will send TERM_CMD if that is present. In any case, we collect
1778 all of the output into buf, and then wait for the normal prompt. */
1780 if (current_monitor->getmem.term)
1782 monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */
1784 if (current_monitor->getmem.term_cmd)
1786 monitor_printf (current_monitor->getmem.term_cmd);
1787 monitor_expect_prompt (NULL, 0);
1791 monitor_expect_prompt (NULL, 0); /* get response */
1794 val = strtoul (membuf, &p, 16);
1796 if (val == 0 && membuf == p)
1797 monitor_error ("monitor_read_memory_single",
1798 "bad value from monitor",
1799 memaddr, 0, membuf, 0);
1801 /* supply register stores in target byte order, so swap here */
1803 store_unsigned_integer (myaddr, len, val);
1808 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
1809 memory at MEMADDR. Returns length moved. Currently, we do no more
1810 than 16 bytes at a time. */
1813 monitor_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
1824 monitor_debug ("Zero length call to monitor_read_memory\n");
1828 monitor_debug ("MON read block ta(%s) ha(%lx) %d\n",
1829 paddr_nz (memaddr), (long) myaddr, len);
1831 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
1832 memaddr = ADDR_BITS_REMOVE (memaddr);
1834 if (current_monitor->flags & MO_GETMEM_READ_SINGLE)
1835 return monitor_read_memory_single (memaddr, myaddr, len);
1837 len = min (len, 16);
1839 /* Some dumpers align the first data with the preceeding 16
1840 byte boundary. Some print blanks and start at the
1841 requested boundary. EXACT_DUMPADDR
1844 dumpaddr = (current_monitor->flags & MO_EXACT_DUMPADDR)
1845 ? memaddr : memaddr & ~0x0f;
1847 /* See if xfer would cross a 16 byte boundary. If so, clip it. */
1848 if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0)
1849 len = ((memaddr + len) & ~0xf) - memaddr;
1851 /* send the memory examine command */
1853 if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE)
1854 monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len);
1855 else if (current_monitor->flags & MO_GETMEM_16_BOUNDARY)
1856 monitor_printf (current_monitor->getmem.cmdb, dumpaddr);
1858 monitor_printf (current_monitor->getmem.cmdb, memaddr, len);
1860 /* If TERM is present, we wait for that to show up. Also, (if TERM
1861 is present), we will send TERM_CMD if that is present. In any
1862 case, we collect all of the output into buf, and then wait for
1863 the normal prompt. */
1865 if (current_monitor->getmem.term)
1867 resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
1870 monitor_error ("monitor_read_memory",
1871 "excessive response from monitor",
1872 memaddr, resp_len, buf, 0);
1874 if (current_monitor->getmem.term_cmd)
1876 SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd,
1877 strlen (current_monitor->getmem.term_cmd));
1878 monitor_expect_prompt (NULL, 0);
1882 resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
1886 /* If RESP_DELIM is specified, we search for that as a leading
1887 delimiter for the values. Otherwise, we just start searching
1888 from the start of the buf. */
1890 if (current_monitor->getmem.resp_delim)
1893 struct re_registers resp_strings;
1894 monitor_debug ("MON getmem.resp_delim %s\n", current_monitor->getmem.resp_delim);
1896 memset (&resp_strings, 0, sizeof (struct re_registers));
1898 retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp,
1902 monitor_error ("monitor_read_memory",
1903 "bad response from monitor",
1904 memaddr, resp_len, buf, 0);
1906 p += resp_strings.end[0];
1908 p = strstr (p, current_monitor->getmem.resp_delim);
1910 monitor_error ("monitor_read_memory",
1911 "bad response from monitor",
1912 memaddr, resp_len, buf, 0);
1913 p += strlen (current_monitor->getmem.resp_delim);
1916 monitor_debug ("MON scanning %d ,%lx '%s'\n", len, (long) p, p);
1917 if (current_monitor->flags & MO_GETMEM_16_BOUNDARY)
1925 while (!(c == '\000' || c == '\n' || c == '\r') && i > 0)
1929 if ((dumpaddr >= memaddr) && (i > 0))
1931 val = fromhex (c) * 16 + fromhex (*(p + 1));
1933 if (monitor_debug_p || remote_debug)
1934 fprintf_unfiltered (gdb_stdlog, "[%02x]", val);
1941 ++p; /* skip a blank or other non hex char */
1945 error ("Failed to read via monitor");
1946 if (monitor_debug_p || remote_debug)
1947 fprintf_unfiltered (gdb_stdlog, "\n");
1948 return fetched; /* Return the number of bytes actually read */
1950 monitor_debug ("MON scanning bytes\n");
1952 for (i = len; i > 0; i--)
1954 /* Skip non-hex chars, but bomb on end of string and newlines */
1961 if (*p == '\000' || *p == '\n' || *p == '\r')
1962 monitor_error ("monitor_read_memory",
1963 "badly terminated response from monitor",
1964 memaddr, resp_len, buf, 0);
1968 val = strtoul (p, &p1, 16);
1970 if (val == 0 && p == p1)
1971 monitor_error ("monitor_read_memory",
1972 "bad value from monitor",
1973 memaddr, resp_len, buf, 0);
1986 /* Transfer LEN bytes between target address MEMADDR and GDB address
1987 MYADDR. Returns 0 for success, errno code for failure. TARGET is
1991 monitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1992 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
1993 struct target_ops *target ATTRIBUTE_UNUSED)
1999 if (current_monitor->flags & MO_HAS_BLOCKWRITES)
2000 res = monitor_write_memory_block(memaddr, myaddr, len);
2002 res = monitor_write_memory(memaddr, myaddr, len);
2006 res = monitor_read_memory(memaddr, myaddr, len);
2015 return; /* ignore attempts to kill target system */
2018 /* All we actually do is set the PC to the start address of exec_bfd, and start
2019 the program at that point. */
2022 monitor_create_inferior (char *exec_file, char *args, char **env)
2024 if (args && (*args != '\000'))
2025 error ("Args are not supported by the monitor.");
2028 clear_proceed_status ();
2029 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
2032 /* Clean up when a program exits.
2033 The program actually lives on in the remote processor's RAM, and may be
2034 run again without a download. Don't leave it full of breakpoint
2038 monitor_mourn_inferior (void)
2040 unpush_target (targ_ops);
2041 generic_mourn_inferior (); /* Do all the proper things now */
2044 /* Tell the monitor to add a breakpoint. */
2047 monitor_insert_breakpoint (CORE_ADDR addr, char *shadow)
2053 monitor_debug ("MON inst bkpt %s\n", paddr (addr));
2054 if (current_monitor->set_break == NULL)
2055 error ("No set_break defined for this monitor");
2057 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
2058 addr = ADDR_BITS_REMOVE (addr);
2060 /* Determine appropriate breakpoint size for this address. */
2061 bp = memory_breakpoint_from_pc (&addr, &bplen);
2063 for (i = 0; i < current_monitor->num_breakpoints; i++)
2065 if (breakaddr[i] == 0)
2067 breakaddr[i] = addr;
2068 monitor_read_memory (addr, shadow, bplen);
2069 monitor_printf (current_monitor->set_break, addr);
2070 monitor_expect_prompt (NULL, 0);
2075 error ("Too many breakpoints (> %d) for monitor.", current_monitor->num_breakpoints);
2078 /* Tell the monitor to remove a breakpoint. */
2081 monitor_remove_breakpoint (CORE_ADDR addr, char *shadow)
2085 monitor_debug ("MON rmbkpt %s\n", paddr (addr));
2086 if (current_monitor->clr_break == NULL)
2087 error ("No clr_break defined for this monitor");
2089 if (current_monitor->flags & MO_ADDR_BITS_REMOVE)
2090 addr = ADDR_BITS_REMOVE (addr);
2092 for (i = 0; i < current_monitor->num_breakpoints; i++)
2094 if (breakaddr[i] == addr)
2097 /* some monitors remove breakpoints based on the address */
2098 if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR)
2099 monitor_printf (current_monitor->clr_break, addr);
2100 else if (current_monitor->flags & MO_CLR_BREAK_1_BASED)
2101 monitor_printf (current_monitor->clr_break, i + 1);
2103 monitor_printf (current_monitor->clr_break, i);
2104 monitor_expect_prompt (NULL, 0);
2108 fprintf_unfiltered (gdb_stderr,
2109 "Can't find breakpoint associated with 0x%s\n",
2114 /* monitor_wait_srec_ack -- wait for the target to send an acknowledgement for
2115 an S-record. Return non-zero if the ACK is received properly. */
2118 monitor_wait_srec_ack (void)
2122 if (current_monitor->flags & MO_SREC_ACK_PLUS)
2124 return (readchar (timeout) == '+');
2126 else if (current_monitor->flags & MO_SREC_ACK_ROTATE)
2128 /* Eat two backspaces, a "rotating" char (|/-\), and a space. */
2129 if ((ch = readchar (1)) < 0)
2131 if ((ch = readchar (1)) < 0)
2133 if ((ch = readchar (1)) < 0)
2135 if ((ch = readchar (1)) < 0)
2141 /* monitor_load -- download a file. */
2144 monitor_load (char *file, int from_tty)
2146 monitor_debug ("MON load\n");
2148 if (current_monitor->load_routine)
2149 current_monitor->load_routine (monitor_desc, file, hashmark);
2151 { /* The default is ascii S-records */
2153 unsigned long load_offset;
2156 /* enable user to specify address for downloading as 2nd arg to load */
2157 n = sscanf (file, "%s 0x%lx", buf, &load_offset);
2163 monitor_printf (current_monitor->load);
2164 if (current_monitor->loadresp)
2165 monitor_expect (current_monitor->loadresp, NULL, 0);
2167 load_srec (monitor_desc, file, (bfd_vma) load_offset,
2168 32, SREC_ALL, hashmark,
2169 current_monitor->flags & MO_SREC_ACK ?
2170 monitor_wait_srec_ack : NULL);
2172 monitor_expect_prompt (NULL, 0);
2175 /* Finally, make the PC point at the start address */
2178 write_pc (bfd_get_start_address (exec_bfd));
2180 inferior_pid = 0; /* No process now */
2182 /* This is necessary because many things were based on the PC at the time that
2183 we attached to the monitor, which is no longer valid now that we have loaded
2184 new code (and just changed the PC). Another way to do this might be to call
2185 normal_stop, except that the stack may not be valid, and things would get
2186 horribly confused... */
2188 clear_symtab_users ();
2194 monitor_debug ("MON stop\n");
2195 if ((current_monitor->flags & MO_SEND_BREAK_ON_STOP) != 0)
2196 SERIAL_SEND_BREAK (monitor_desc);
2197 if (current_monitor->stop)
2198 monitor_printf_noecho (current_monitor->stop);
2201 /* Put a COMMAND string out to MONITOR. Output from MONITOR is placed
2202 in OUTPUT until the prompt is seen. FIXME: We read the characters
2203 ourseleves here cause of a nasty echo. */
2206 monitor_rcmd (char *command,
2207 struct ui_file *outbuf)
2213 if (monitor_desc == NULL)
2214 error ("monitor target not open.");
2216 p = current_monitor->prompt;
2218 /* Send the command. Note that if no args were supplied, then we're
2219 just sending the monitor a newline, which is sometimes useful. */
2221 monitor_printf ("%s\r", (command ? command : ""));
2223 resp_len = monitor_expect_prompt (buf, sizeof buf);
2225 fputs_unfiltered (buf, outbuf); /* Output the response */
2228 /* Convert hex digit A to a number. */
2234 if (a >= '0' && a <= '9')
2236 if (a >= 'a' && a <= 'f')
2237 return a - 'a' + 10;
2238 if (a >= 'A' && a <= 'F')
2239 return a - 'A' + 10;
2241 error ("Reply contains invalid hex digit 0x%x", a);
2246 monitor_get_dev_name (void)
2251 static struct target_ops monitor_ops;
2254 init_base_monitor_ops (void)
2256 monitor_ops.to_shortname = NULL;
2257 monitor_ops.to_longname = NULL;
2258 monitor_ops.to_doc = NULL;
2259 monitor_ops.to_open = NULL;
2260 monitor_ops.to_close = monitor_close;
2261 monitor_ops.to_attach = NULL;
2262 monitor_ops.to_post_attach = NULL;
2263 monitor_ops.to_require_attach = NULL;
2264 monitor_ops.to_detach = monitor_detach;
2265 monitor_ops.to_require_detach = NULL;
2266 monitor_ops.to_resume = monitor_resume;
2267 monitor_ops.to_wait = monitor_wait;
2268 monitor_ops.to_post_wait = NULL;
2269 monitor_ops.to_fetch_registers = monitor_fetch_registers;
2270 monitor_ops.to_store_registers = monitor_store_registers;
2271 monitor_ops.to_prepare_to_store = monitor_prepare_to_store;
2272 monitor_ops.to_xfer_memory = monitor_xfer_memory;
2273 monitor_ops.to_files_info = monitor_files_info;
2274 monitor_ops.to_insert_breakpoint = monitor_insert_breakpoint;
2275 monitor_ops.to_remove_breakpoint = monitor_remove_breakpoint;
2276 monitor_ops.to_terminal_init = 0;
2277 monitor_ops.to_terminal_inferior = 0;
2278 monitor_ops.to_terminal_ours_for_output = 0;
2279 monitor_ops.to_terminal_ours = 0;
2280 monitor_ops.to_terminal_info = 0;
2281 monitor_ops.to_kill = monitor_kill;
2282 monitor_ops.to_load = monitor_load;
2283 monitor_ops.to_lookup_symbol = 0;
2284 monitor_ops.to_create_inferior = monitor_create_inferior;
2285 monitor_ops.to_post_startup_inferior = NULL;
2286 monitor_ops.to_acknowledge_created_inferior = NULL;
2287 monitor_ops.to_clone_and_follow_inferior = NULL;
2288 monitor_ops.to_post_follow_inferior_by_clone = NULL;
2289 monitor_ops.to_insert_fork_catchpoint = NULL;
2290 monitor_ops.to_remove_fork_catchpoint = NULL;
2291 monitor_ops.to_insert_vfork_catchpoint = NULL;
2292 monitor_ops.to_remove_vfork_catchpoint = NULL;
2293 monitor_ops.to_has_forked = NULL;
2294 monitor_ops.to_has_vforked = NULL;
2295 monitor_ops.to_can_follow_vfork_prior_to_exec = NULL;
2296 monitor_ops.to_post_follow_vfork = NULL;
2297 monitor_ops.to_insert_exec_catchpoint = NULL;
2298 monitor_ops.to_remove_exec_catchpoint = NULL;
2299 monitor_ops.to_has_execd = NULL;
2300 monitor_ops.to_reported_exec_events_per_exec_call = NULL;
2301 monitor_ops.to_has_exited = NULL;
2302 monitor_ops.to_mourn_inferior = monitor_mourn_inferior;
2303 monitor_ops.to_can_run = 0;
2304 monitor_ops.to_notice_signals = 0;
2305 monitor_ops.to_thread_alive = 0;
2306 monitor_ops.to_stop = monitor_stop;
2307 monitor_ops.to_rcmd = monitor_rcmd;
2308 monitor_ops.to_pid_to_exec_file = NULL;
2309 monitor_ops.to_core_file_to_sym_file = NULL;
2310 monitor_ops.to_stratum = process_stratum;
2311 monitor_ops.DONT_USE = 0;
2312 monitor_ops.to_has_all_memory = 1;
2313 monitor_ops.to_has_memory = 1;
2314 monitor_ops.to_has_stack = 1;
2315 monitor_ops.to_has_registers = 1;
2316 monitor_ops.to_has_execution = 1;
2317 monitor_ops.to_sections = 0;
2318 monitor_ops.to_sections_end = 0;
2319 monitor_ops.to_magic = OPS_MAGIC;
2320 } /* init_base_monitor_ops */
2322 /* Init the target_ops structure pointed at by OPS */
2325 init_monitor_ops (struct target_ops *ops)
2327 if (monitor_ops.to_magic != OPS_MAGIC)
2328 init_base_monitor_ops ();
2330 memcpy (ops, &monitor_ops, sizeof monitor_ops);
2333 /* Define additional commands that are usually only used by monitors. */
2336 _initialize_remote_monitors (void)
2338 init_base_monitor_ops ();
2339 add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
2341 "Set display of activity while downloading a file.\n\
2342 When enabled, a hashmark \'#\' is displayed.",
2347 (add_set_cmd ("monitor", no_class, var_zinteger,
2348 (char *) &monitor_debug_p,
2349 "Set debugging of remote monitor communication.\n\
2350 When enabled, communication between GDB and the remote monitor\n\
2351 is displayed.", &setdebuglist),