#include "inferior.h"
#include "regex.h"
+static int readchar PARAMS ((int timeout));
+
static void monitor_command PARAMS ((char *args, int fromtty));
static void monitor_load_srec PARAMS ((char *args));
static void monitor_close PARAMS ((int quitting));
static void monitor_detach PARAMS ((char *args, int from_tty));
static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig));
+static void monitor_interrupt PARAMS ((int signo));
+static void monitor_interrupt_twice PARAMS ((int signo));
+static void monitor_interrupt_query PARAMS ((void));
+static void monitor_wait_cleanup PARAMS ((int old_timeout));
+
static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status));
static void monitor_fetch_registers PARAMS ((int regno));
static void monitor_store_registers PARAMS ((int regno));
static int timeout = 30;
+static void (*ofunc)(); /* Old SIGINT signal handler */
+
/* Descriptor for I/O to remote machine. Initialize it to NULL so
that monitor_open knows that we don't have a file open when the
program starts. */
static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when
monitor_wait wakes up. */
-/* monitor_printf -- send data to monitor. Works just like printf. */
+/* monitor_printf_noecho -- Send data to monitor, but don't expect an echo.
+ Works just like printf. */
+
+void
+monitor_printf_noecho (va_alist)
+ va_dcl
+{
+ va_list args;
+ char *pattern;
+ char sndbuf[2000];
+ int len;
+
+ va_start (args);
+
+ pattern = va_arg (args, char *);
+
+ vsprintf (sndbuf, pattern, args);
+
+ if (remote_debug > 0)
+ fputs_unfiltered (sndbuf, gdb_stderr);
+
+ len = strlen (sndbuf);
+
+ if (len + 1 > sizeof sndbuf)
+ abort ();
+
+ if (SERIAL_WRITE(monitor_desc, sndbuf, len))
+ fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
+}
+
+/* monitor_printf -- Send data to monitor and check the echo. Works just like
+ printf. */
void
monitor_printf (va_alist)
{
va_list args;
char *pattern;
- char buf[2000];
+ char sndbuf[2000];
int len;
+ int i, c;
va_start (args);
pattern = va_arg (args, char *);
- vsprintf (buf, pattern, args);
+ vsprintf (sndbuf, pattern, args);
if (remote_debug > 0)
- fputs_unfiltered (buf, gdb_stderr);
+ fputs_unfiltered (sndbuf, gdb_stderr);
- len = strlen (buf);
+ len = strlen (sndbuf);
- if (len + 1 > sizeof buf)
+ if (len + 1 > sizeof sndbuf)
abort ();
- if (SERIAL_WRITE(monitor_desc, buf, len))
+ if (SERIAL_WRITE(monitor_desc, sndbuf, len))
fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
+
+ for (i = 0; i < len; i++)
+ {
+ trycr:
+ c = readchar (timeout);
+
+ if (c != sndbuf[i])
+ {
+#if 0
+ if (sndbuf[i] == '\r'
+ && c == '\n')
+ goto trycr;
+#endif
+ error ("monitor_printf: Bad echo. Sent: \"%s\", Got: \"%.*s%c\".",
+ sndbuf, i, sndbuf, c);
+ }
+ }
}
/* Read a character from the remote system, doing all the fancy
/* See if we can wake up the monitor. First, try sending a stop sequence,
then send the init strings. Last, remove all breakpoints. */
- monitor_stop ();
+ if (current_monitor->stop)
+ {
+ monitor_stop ();
+ monitor_expect_prompt (NULL, 0);
+ }
/* wake up the monitor and see if it's alive */
for (p = mon_ops->init; *p != NULL; p++)
monitor_expect_prompt (NULL, 0);
}
+ SERIAL_FLUSH_INPUT (monitor_desc);
+
/* Remove all breakpoints */
if (mon_ops->clr_all_break)
inferior_pid = 42000; /* Make run command think we are busy... */
- monitor_printf ("\r"); /* Give monitor_wait something to read */
+ /* Give monitor_wait something to read */
+
+ monitor_printf (current_monitor->line_term);
start_remote ();
}
}
}
+/* Send ^C to target to halt it. Target will respond, and send us a
+ packet. */
+
+static void
+monitor_interrupt (signo)
+ int signo;
+{
+ /* If this doesn't work, try more severe steps. */
+ signal (signo, monitor_interrupt_twice);
+
+ if (remote_debug)
+ printf_unfiltered ("monitor_interrupt called\n");
+
+ target_stop ();
+}
+
+/* The user typed ^C twice. */
+
+static void
+monitor_interrupt_twice (signo)
+ int signo;
+{
+ signal (signo, ofunc);
+
+ monitor_interrupt_query ();
+
+ signal (signo, monitor_interrupt);
+}
+
+/* Ask the user what to do when an interrupt is received. */
+
+static void
+monitor_interrupt_query ()
+{
+ target_terminal_ours ();
+
+ if (query ("Interrupted while waiting for the program.\n\
+Give up (and stop debugging it)? "))
+ {
+ target_mourn_inferior ();
+ return_to_top_level (RETURN_QUIT);
+ }
+
+ target_terminal_inferior ();
+}
+
+static void
+monitor_wait_cleanup (old_timeout)
+ int old_timeout;
+{
+ timeout = old_timeout;
+ signal (SIGINT, ofunc);
+}
+
/* Wait until the remote machine stops, then return, storing status in
status just as `wait' would. */
int old_timeout = timeout;
char buf[1024];
int resp_len;
+ struct cleanup *old_chain;
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = 0;
+ old_chain = make_cleanup (monitor_wait_cleanup, old_timeout);
+
timeout = -1; /* Don't time out -- user program is running. */
+ ofunc = (void (*)()) signal (SIGINT, monitor_interrupt);
+
do
{
resp_len = monitor_expect_prompt (buf, sizeof (buf));
}
while (resp_len < 0);
+ signal (SIGINT, ofunc);
+
timeout = old_timeout;
if (dump_reg_flag && current_monitor->dump_registers)
status->kind = TARGET_WAITKIND_STOPPED;
status->value.sig = TARGET_SIGNAL_TRAP;
+ discard_cleanups (old_chain);
+
return inferior_pid;
}
monitor_fetch_register (regno)
int regno;
{
- char buf[200];
- char *p;
char *name;
- int resp_len;
static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0};
+ char regbuf[MAX_REGISTER_RAW_SIZE * 2 + 1];
+ int i;
name = REGNAMES (regno);
monitor_printf (current_monitor->getreg.cmd, name);
+/* If RESP_DELIM is specified, we search for that as a leading delimiter for
+ the register value. Otherwise, we just start searching from the start of
+ the buf. */
+
+ if (current_monitor->getreg.resp_delim)
+ monitor_expect (current_monitor->getreg.resp_delim, NULL, 0);
+
+/* Now, read the appropriate number of hex digits for this register, skipping
+ spaces. */
+
+ for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++)
+ {
+ int c;
+
+ while (1)
+ {
+ c = readchar (timeout);
+ if (isxdigit (c))
+ break;
+ if (c == ' ')
+ continue;
+
+ error ("monitor_fetch_register (%d): bad response from monitor: %.*s%c.",
+ regno, i, regbuf, c);
+ }
+
+ regbuf[i] = c;
+ }
+
+ regbuf[i] = '\000'; /* terminate the number */
+
/* If TERM is present, we wait for that to show up. Also, (if TERM is
present), we will send TERM_CMD if that is present. In any case, we collect
all of the output into buf, and then wait for the normal prompt. */
if (current_monitor->getreg.term)
{
- resp_len = monitor_expect (current_monitor->getreg.term, buf, sizeof buf); /* get response */
-
- if (resp_len <= 0)
- error ("monitor_fetch_register (%d): excessive response from monitor: %.*s.",
- regno, resp_len, buf);
+ monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */
if (current_monitor->getreg.term_cmd)
{
- SERIAL_WRITE (monitor_desc, current_monitor->getreg.term_cmd,
- strlen (current_monitor->getreg.term_cmd));
+ monitor_printf (current_monitor->getreg.term_cmd);
monitor_expect_prompt (NULL, 0);
}
}
else
- resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */
-
-
- /* If RESP_DELIM is specified, we search for that as a leading delimiter for
- the register value. Otherwise, we just start searching from the start of
- the buf. */
-
- if (current_monitor->getreg.resp_delim)
- {
- p = strstr (buf, current_monitor->getreg.resp_delim);
- if (!p)
- error ("monitor_fetch_register (%d): bad response from monitor: %.*s.",
- regno, resp_len, buf);
- p += strlen (current_monitor->getreg.resp_delim);
- }
- else
- p = buf;
+ monitor_expect_prompt (NULL, 0); /* get response */
- monitor_supply_register (regno, p);
+ monitor_supply_register (regno, regbuf);
}
/* Read the remote registers into the block regs. */
return len;
}
+/* This is an alternate form of monitor_read_memory which is used for monitors
+ which can only read a single byte/word/etc. at a time. */
+
+static int
+monitor_read_memory_single (memaddr, myaddr, len)
+ CORE_ADDR memaddr;
+ unsigned char *myaddr;
+ int len;
+{
+ unsigned LONGEST val;
+ char membuf[sizeof(LONGEST) * 2 + 1];
+ char *p;
+ char *cmd;
+ int i;
+
+ if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll)
+ {
+ len = 8;
+ cmd = current_monitor->getmem.cmdll;
+ }
+ else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl)
+ {
+ len = 4;
+ cmd = current_monitor->getmem.cmdl;
+ }
+ else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw)
+ {
+ len = 2;
+ cmd = current_monitor->getmem.cmdw;
+ }
+ else
+ {
+ len = 1;
+ cmd = current_monitor->getmem.cmdb;
+ }
+
+/* Send the examine command. */
+
+ monitor_printf (cmd, memaddr);
+
+/* If RESP_DELIM is specified, we search for that as a leading delimiter for
+ the register value. Otherwise, we just start searching from the start of
+ the buf. */
+
+ if (current_monitor->getmem.resp_delim)
+ monitor_expect (current_monitor->getmem.resp_delim, NULL, 0);
+
+/* Now, read the appropriate number of hex digits for this loc, skipping
+ spaces. */
+
+ for (i = 0; i < len * 2; i++)
+ {
+ int c;
+
+ while (1)
+ {
+ c = readchar (timeout);
+ if (isxdigit (c))
+ break;
+ if (c == ' ')
+ continue;
+
+ error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.",
+ memaddr, i, membuf, c);
+ }
+
+ membuf[i] = c;
+ }
+
+ membuf[i] = '\000'; /* terminate the number */
+
+/* If TERM is present, we wait for that to show up. Also, (if TERM is
+ present), we will send TERM_CMD if that is present. In any case, we collect
+ all of the output into buf, and then wait for the normal prompt. */
+
+ if (current_monitor->getmem.term)
+ {
+ monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */
+
+ if (current_monitor->getmem.term_cmd)
+ {
+ monitor_printf (current_monitor->getmem.term_cmd);
+ monitor_expect_prompt (NULL, 0);
+ }
+ }
+ else
+ monitor_expect_prompt (NULL, 0); /* get response */
+
+ p = membuf;
+ val = strtoul (membuf, &p, 16);
+
+ if (val == 0 && membuf == p)
+ error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.",
+ memaddr, membuf);
+
+ /* supply register stores in target byte order, so swap here */
+
+ store_unsigned_integer (myaddr, len, val);
+
+ return len;
+}
+
/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory
at MEMADDR. Returns length moved. Currently, we only do one byte at a
time. */
int resp_len;
int i;
+ if (current_monitor->flags & MO_GETMEM_READ_SINGLE)
+ return monitor_read_memory_single (memaddr, myaddr, len);
+
len = min (len, 16);
/* See if xfer would cross a 16 byte boundary. If so, clip it. */
p = buf;
- while (*p != '\r') /* Skip command echo and line delim */
- p++;
-
/* If RESP_DELIM is specified, we search for that as a leading delimiter for
the values. Otherwise, we just start searching from the start of the buf.
*/
return len;
}
-/* FIXME-someday! merge these two. */
-
static int
monitor_xfer_memory (memaddr, myaddr, len, write, target)
CORE_ADDR memaddr;
static void
monitor_stop ()
{
- if (!current_monitor->stop)
- return;
-
- monitor_printf(current_monitor->stop);
- monitor_expect_prompt (NULL, 0);
+ if (current_monitor->stop)
+ monitor_printf_noecho (current_monitor->stop);
}
/* Put a command string, in args, out to MONITOR. Output from MONITOR
asection *s;
char *buffer, srec[1024];
int i;
- int srec_frame = 128;
+ int srec_frame = 32;
int reclen;
buffer = alloca (srec_frame * 2 + 256);
reclen = monitor_make_srec (srec, 3, s->vma + i, buffer, numbytes);
- monitor_printf ("%.*s\r", reclen, srec);
+ monitor_printf_noecho ("%.*s\r", reclen, srec);
if (hashmark)
{
reclen = monitor_make_srec (srec, 7, abfd->start_address, NULL, 0);
- monitor_printf ("%.*s\r", reclen, srec);
+ monitor_printf_noecho ("%.*s\r", reclen, srec);
- monitor_printf ("\r\r"); /* Some monitors need these to wake up */
+ monitor_printf_noecho ("\r\r"); /* Some monitors need these to wake up */
monitor_expect_prompt (NULL, 0);
+
+ SERIAL_FLUSH_INPUT (monitor_desc);
}
/*