/* Remote debugging interface for MIPS remote debugging protocol.
- Copyright 1993 Free Software Foundation, Inc.
+ Copyright 1993, 1994 Free Software Foundation, Inc.
Contributed by Cygnus Support. Written by Ian Lance Taylor
<ian@cygnus.com>.
#include "remote-utils.h"
#include <signal.h>
+#include <varargs.h>
\f
/* Prototypes for local functions. */
static void
mips_send_packet PARAMS ((const char *s, int get_ack));
-static int
-mips_receive_packet PARAMS ((char *buff));
+static int mips_receive_packet PARAMS ((char *buff, int throw_error,
+ int timeout));
static int
mips_request PARAMS ((char cmd, unsigned int addr, unsigned int data,
- int *perr));
+ int *perr, int timeout));
static void
mips_initialize PARAMS ((void));
static void
mips_detach PARAMS ((char *args, int from_tty));
-static void
-mips_resume PARAMS ((int pid, int step, int siggnal));
+static void mips_resume PARAMS ((int pid, int step,
+ enum target_signal siggnal));
static int
-mips_wait PARAMS ((int pid, WAITTYPE *status));
+mips_wait PARAMS ((int pid, struct target_waitstatus *status));
static int
mips_map_regno PARAMS ((int regno));
fprintf_filtered (gdb_stderr, "\n");
va_end (args);
- /* We probably should print "ending remote debugging" here, but that would
- appear to be a problem for mips_initialize and its catch_errors. */
+ /* Clean up in such a way that mips_close won't try to talk to the
+ board (it almost surely won't work since we weren't able to talk to
+ it). */
+ mips_is_open = 0;
+ SERIAL_CLOSE (mips_desc);
+
+ printf_unfiltered ("Ending remote MIPS debugging.\n");
target_mourn_inferior ();
return_to_top_level (RETURN_ERROR);
should be DATA_MAXLEN + 1 bytes). The protocol documentation
implies that only the sender retransmits packets, so this code just
waits silently for a packet. It returns the length of the received
- packet. */
+ packet. If THROW_ERROR is nonzero, call error() on errors. If not,
+ don't print an error message and return -1. */
static int
-mips_receive_packet (buff)
+mips_receive_packet (buff, throw_error, timeout)
char *buff;
+ int throw_error;
+ int timeout;
{
int ch;
int garbage;
int i;
int err;
- if (mips_receive_header (hdr, &garbage, ch, mips_receive_wait) != 0)
- mips_error ("Timed out waiting for remote packet");
+ if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
+ {
+ if (throw_error)
+ mips_error ("Timed out waiting for remote packet");
+ else
+ return -1;
+ }
ch = 0;
{
int rch;
- rch = mips_readchar (mips_receive_wait);
+ rch = mips_readchar (timeout);
if (rch == SYN)
{
ch = SYN;
break;
}
if (rch == SERIAL_TIMEOUT)
- mips_error ("Timed out waiting for remote packet");
+ {
+ if (throw_error)
+ mips_error ("Timed out waiting for remote packet");
+ else
+ return -1;
+ }
buff[i] = rch;
}
continue;
}
- err = mips_receive_trailer (trlr, &garbage, &ch, mips_receive_wait);
+ err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
if (err == -1)
- mips_error ("Timed out waiting for packet");
+ {
+ if (throw_error)
+ mips_error ("Timed out waiting for packet");
+ else
+ return -1;
+ }
if (err == -2)
{
if (sr_get_debug () > 0)
}
if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
- mips_error ("write to target failed: %s", safe_strerror (errno));
+ {
+ if (throw_error)
+ mips_error ("write to target failed: %s", safe_strerror (errno));
+ else
+ return -1;
+ }
}
if (sr_get_debug () > 0)
}
if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
- mips_error ("write to target failed: %s", safe_strerror (errno));
+ {
+ if (throw_error)
+ mips_error ("write to target failed: %s", safe_strerror (errno));
+ else
+ return -1;
+ }
return len;
}
target board reports. */
static int
-mips_request (cmd, addr, data, perr)
+mips_request (cmd, addr, data, perr, timeout)
char cmd;
unsigned int addr;
unsigned int data;
int *perr;
+ int timeout;
{
char buff[DATA_MAXLEN + 1];
int len;
mips_need_reply = 0;
- len = mips_receive_packet (buff);
+ len = mips_receive_packet (buff, 1, timeout);
buff[len] = '\0';
if (sscanf (buff, "0x%x %c 0x%x 0x%x",
return rresponse;
}
+static void
+mips_initialize_cleanups (arg)
+ PTR arg;
+{
+ mips_initializing = 0;
+}
+
/* Initialize a new connection to the MIPS board, and make sure we are
really connected. */
mips_initialize ()
{
char cr;
- int hold_wait;
- int tries;
char buff[DATA_MAXLEN + 1];
int err;
+ struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
+ /* What is this code doing here? I don't see any way it can happen, and
+ it might mean mips_initializing didn't get cleared properly.
+ So I'll make it a warning. */
if (mips_initializing)
- return;
+ {
+ warning ("internal error: mips_initialize called twice");
+ return;
+ }
mips_initializing = 1;
cr = '\r';
SERIAL_WRITE (mips_desc, &cr, 1);
- hold_wait = mips_receive_wait;
- mips_receive_wait = 3;
-
- tries = 0;
- while (catch_errors (mips_receive_packet, buff, (char *) NULL,
- RETURN_MASK_ALL)
- == 0)
+ if (mips_receive_packet (buff, 0, 3) < 0)
{
char cc;
- if (tries > 0)
- mips_error ("Could not connect to target");
- ++tries;
-
/* We did not receive the packet we expected; try resetting the
board and trying again. */
printf_filtered ("Failed to initialize; trying to reset board\n");
cr = '\r';
SERIAL_WRITE (mips_desc, &cr, 1);
}
+ mips_receive_packet (buff, 1, 3);
- mips_receive_wait = hold_wait;
- mips_initializing = 0;
+ do_cleanups (old_cleanups);
/* If this doesn't call error, we have connected; we don't care if
the request itself succeeds or fails. */
- mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err);
+ mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
+ mips_receive_wait);
}
/* Open a connection to the remote board. */
mips_is_open = 0;
/* Get the board out of remote debugging mode. */
- mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err);
+ mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
+ mips_receive_wait);
SERIAL_CLOSE (mips_desc);
}
static void
mips_resume (pid, step, siggnal)
- int pid, step, siggnal;
+ int pid, step;
+ enum target_signal siggnal;
{
- if (siggnal)
- mips_error ("Can't send signals to a remote system. Try `handle %d ignore'.",
- siggnal);
+ if (siggnal != TARGET_SIGNAL_0)
+ warning
+ ("Can't send signals to a remote system. Try `handle %s ignore'.",
+ target_signal_to_name (siggnal));
mips_request (step ? 's' : 'c',
(unsigned int) 1,
(unsigned int) 0,
- (int *) NULL);
+ (int *) NULL,
+ mips_receive_wait);
+}
+
+/* Return the signal corresponding to SIG, where SIG is the number which
+ the MIPS protocol uses for the signal. */
+enum target_signal
+mips_signal_from_protocol (sig)
+ int sig;
+{
+ /* We allow a few more signals than the IDT board actually returns, on
+ the theory that there is at least *some* hope that perhaps the numbering
+ for these signals is widely agreed upon. */
+ if (sig <= 0
+ || sig > 31)
+ return TARGET_SIGNAL_UNKNOWN;
+
+ /* Don't want to use target_signal_from_host because we are converting
+ from MIPS signal numbers, not host ones. Our internal numbers
+ match the MIPS numbers for the signals the board can return, which
+ are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
+ return (enum target_signal) sig;
}
/* Wait until the remote stops, and return a wait status. */
static int
mips_wait (pid, status)
int pid;
- WAITTYPE *status;
+ struct target_waitstatus *status;
{
int rstatus;
int err;
indicating that it is stopped. */
if (! mips_need_reply)
{
- WSETSTOP (*status, SIGTRAP);
+ status->kind = TARGET_WAITKIND_STOPPED;
+ status->value.sig = TARGET_SIGNAL_TRAP;
return 0;
}
- rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err);
+ /* No timeout; we sit here as long as the program continues to execute. */
+ rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err, -1);
if (err)
mips_error ("Remote failure: %s", safe_strerror (errno));
- /* FIXME: The target board uses numeric signal values which are
- those used on MIPS systems. If the host uses different signal
- values, we need to translate here. I believe all Unix systems
- use the same values for the signals the board can return, which
- are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
-
- /* FIXME: The target board uses a standard Unix wait status int. If
- the host system does not, we must translate here. */
-
- *status = rstatus;
+ /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
+ and so on, because the constants we want here are determined by the
+ MIPS protocol and have nothing to do with what host we are running on. */
+ if ((rstatus & 0377) == 0)
+ {
+ status->kind = TARGET_WAITKIND_EXITED;
+ status->value.integer = (((rstatus) >> 8) & 0377);
+ }
+ else if ((rstatus & 0377) == 0177)
+ {
+ status->kind = TARGET_WAITKIND_STOPPED;
+ status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
+ }
+ else
+ {
+ status->kind = TARGET_WAITKIND_SIGNALLED;
+ status->value.sig = mips_signal_from_protocol (rstatus & 0177);
+ }
return 0;
}
}
val = mips_request ('r', (unsigned int) mips_map_regno (regno),
- (unsigned int) 0, &err);
+ (unsigned int) 0, &err, mips_receive_wait);
if (err)
mips_error ("Can't read register %d: %s", regno, safe_strerror (errno));
mips_request ('R', (unsigned int) mips_map_regno (regno),
(unsigned int) read_register (regno),
- &err);
+ &err, mips_receive_wait);
if (err)
mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
}
int val;
int err;
- val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err);
+ val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
+ mips_receive_wait);
if (err)
{
/* Data space failed; try instruction space. */
- val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err);
+ val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
+ mips_receive_wait);
if (err)
mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
}
{
int err;
- mips_request ('D', (unsigned int) addr, (unsigned int) val, &err);
+ mips_request ('D', (unsigned int) addr, (unsigned int) val, &err,
+ mips_receive_wait);
if (err)
{
/* Data space failed; try instruction space. */
- mips_request ('I', (unsigned int) addr, (unsigned int) val, &err);
+ mips_request ('I', (unsigned int) addr, (unsigned int) val, &err,
+ mips_receive_wait);
if (err)
mips_error ("Can't write address 0x%x: %s", addr, safe_strerror (errno));
}
/* FIXME: Should we set inferior_pid here? */
- proceed (entry_pt, -1, 0);
+ proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
}
/* Clean up after a process. Actually nothing to do. */
{
"mips", /* to_shortname */
"Remote MIPS debugging over serial line", /* to_longname */
- "Debug a board using the MIPS remote debugging protocol over a serial line.\n\
-Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */
+ "\
+Debug a board using the MIPS remote debugging protocol over a serial line.\n\
+The argument is the device it is connected to or, if it contains a colon,\n\
+HOST:PORT to access a board over a network", /* to_doc */
mips_open, /* to_open */
mips_close, /* to_close */
NULL, /* to_attach */