1 /* Remote debugging interface for MIPS remote debugging protocol.
2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Ian Lance Taylor
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, Boston, MA 02111-1307, USA. */
31 #include "remote-utils.h"
32 #include "gdb_string.h"
35 #include <sys/types.h>
37 #ifdef ANSI_PROTOTYPES
43 extern void mips_set_processor_type_command PARAMS ((char *, int));
46 /* Prototypes for local functions. */
48 static int mips_readchar PARAMS ((int timeout));
50 static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
51 int ch, int timeout));
53 static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
54 int *pch, int timeout));
56 static int mips_cksum PARAMS ((const unsigned char *hdr,
57 const unsigned char *data,
60 static void mips_send_packet PARAMS ((const char *s, int get_ack));
62 static void mips_send_command PARAMS ((const char *cmd, int prompt));
64 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
67 static CORE_ADDR mips_request PARAMS ((int cmd, CORE_ADDR addr,
68 CORE_ADDR data, int *perr, int timeout,
71 static void mips_initialize PARAMS ((void));
73 static void mips_open PARAMS ((char *name, int from_tty));
75 static void pmon_open PARAMS ((char *name, int from_tty));
77 static void ddb_open PARAMS ((char *name, int from_tty));
79 static void mips_close PARAMS ((int quitting));
81 static void mips_detach PARAMS ((char *args, int from_tty));
83 static void mips_resume PARAMS ((int pid, int step,
84 enum target_signal siggnal));
86 static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
88 static int pmon_wait PARAMS ((int pid, struct target_waitstatus *status));
90 static int mips_map_regno PARAMS ((int regno));
92 static void mips_fetch_registers PARAMS ((int regno));
94 static void mips_prepare_to_store PARAMS ((void));
96 static void mips_store_registers PARAMS ((int regno));
98 static unsigned int mips_fetch_word PARAMS ((CORE_ADDR addr));
100 static int mips_store_word PARAMS ((CORE_ADDR addr, unsigned int value,
101 char *old_contents));
103 static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
104 int write, struct target_ops *ignore));
106 static void mips_files_info PARAMS ((struct target_ops *ignore));
108 static void mips_create_inferior PARAMS ((char *execfile, char *args,
111 static void mips_mourn_inferior PARAMS ((void));
113 static int pmon_makeb64 PARAMS ((unsigned long v, char *p, int n, int *chksum));
115 static int pmon_zeroset PARAMS ((int recsize, char **buff, int *amount,
116 unsigned int *chksum));
118 static int pmon_checkset PARAMS ((int recsize, char **buff, int *value));
120 static void pmon_make_fastrec PARAMS ((char **outbuf, unsigned char *inbuf,
121 int *inptr, int inamount, int *recsize,
122 unsigned int *csum, unsigned int *zerofill));
124 static int pmon_check_ack PARAMS ((char *mesg));
126 static void pmon_start_download PARAMS ((void));
128 static void pmon_end_download PARAMS ((int final, int bintotal));
130 static void pmon_download PARAMS ((char *buffer, int length));
132 static void pmon_load_fast PARAMS ((char *file));
134 static void mips_load PARAMS ((char *file, int from_tty));
136 static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
137 unsigned char *myaddr, int len));
139 static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask,
142 static void common_open PARAMS ((struct target_ops *ops, char *name,
144 /* Forward declarations. */
145 extern struct target_ops mips_ops;
146 extern struct target_ops pmon_ops;
147 extern struct target_ops ddb_ops;
149 /* The MIPS remote debugging interface is built on top of a simple
150 packet protocol. Each packet is organized as follows:
152 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
153 may not appear anywhere else in the packet. Any time a SYN is
154 seen, a new packet should be assumed to have begun.
157 This byte contains the upper five bits of the logical length
158 of the data section, plus a single bit indicating whether this
159 is a data packet or an acknowledgement. The documentation
160 indicates that this bit is 1 for a data packet, but the actual
161 board uses 1 for an acknowledgement. The value of the byte is
162 0x40 + (ack ? 0x20 : 0) + (len >> 6)
163 (we always have 0 <= len < 1024). Acknowledgement packets do
164 not carry data, and must have a data length of 0.
166 LEN1 This byte contains the lower six bits of the logical length of
167 the data section. The value is
170 SEQ This byte contains the six bit sequence number of the packet.
173 An acknowlegment packet contains the sequence number of the
174 packet being acknowledged plus 1 modulo 64. Data packets are
175 transmitted in sequence. There may only be one outstanding
176 unacknowledged data packet at a time. The sequence numbers
177 are independent in each direction. If an acknowledgement for
178 the previous packet is received (i.e., an acknowledgement with
179 the sequence number of the packet just sent) the packet just
180 sent should be retransmitted. If no acknowledgement is
181 received within a timeout period, the packet should be
182 retransmitted. This has an unfortunate failure condition on a
183 high-latency line, as a delayed acknowledgement may lead to an
184 endless series of duplicate packets.
186 DATA The actual data bytes follow. The following characters are
187 escaped inline with DLE (ASCII 020, or ^P):
193 The additional DLE characters are not counted in the logical
194 length stored in the TYPE_LEN and LEN1 bytes.
199 These bytes contain an 18 bit checksum of the complete
200 contents of the packet excluding the SEQ byte and the
201 CSUM[123] bytes. The checksum is simply the twos complement
202 addition of all the bytes treated as unsigned characters. The
203 values of the checksum bytes are:
204 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
205 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
206 CSUM3: 0x40 + (cksum & 0x3f)
208 It happens that the MIPS remote debugging protocol always
209 communicates with ASCII strings. Because of this, this
210 implementation doesn't bother to handle the DLE quoting mechanism,
211 since it will never be required. */
213 /* The SYN character which starts each packet. */
216 /* The 0x40 used to offset each packet (this value ensures that all of
217 the header and trailer bytes, other than SYN, are printable ASCII
219 #define HDR_OFFSET 0x40
221 /* The indices of the bytes in the packet header. */
222 #define HDR_INDX_SYN 0
223 #define HDR_INDX_TYPE_LEN 1
224 #define HDR_INDX_LEN1 2
225 #define HDR_INDX_SEQ 3
228 /* The data/ack bit in the TYPE_LEN header byte. */
229 #define TYPE_LEN_DA_BIT 0x20
230 #define TYPE_LEN_DATA 0
231 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
233 /* How to compute the header bytes. */
234 #define HDR_SET_SYN(data, len, seq) (SYN)
235 #define HDR_SET_TYPE_LEN(data, len, seq) \
237 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
238 + (((len) >> 6) & 0x1f))
239 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
240 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
242 /* Check that a header byte is reasonable. */
243 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
245 /* Get data from the header. These macros evaluate their argument
247 #define HDR_IS_DATA(hdr) \
248 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
249 #define HDR_GET_LEN(hdr) \
250 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
251 #define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
253 /* The maximum data length. */
254 #define DATA_MAXLEN 1023
256 /* The trailer offset. */
257 #define TRLR_OFFSET HDR_OFFSET
259 /* The indices of the bytes in the packet trailer. */
260 #define TRLR_INDX_CSUM1 0
261 #define TRLR_INDX_CSUM2 1
262 #define TRLR_INDX_CSUM3 2
263 #define TRLR_LENGTH 3
265 /* How to compute the trailer bytes. */
266 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
267 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
268 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
270 /* Check that a trailer byte is reasonable. */
271 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
273 /* Get data from the trailer. This evaluates its argument multiple
275 #define TRLR_GET_CKSUM(trlr) \
276 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
277 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
278 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
280 /* The sequence number modulos. */
281 #define SEQ_MODULOS (64)
283 /* PMON commands to load from the serial port or UDP socket. */
284 #define LOAD_CMD "load -b -s tty0\r"
285 #define LOAD_CMD_UDP "load -b -s udp\r"
287 enum mips_monitor_type {
288 /* IDT/SIM monitor being used: */
290 /* PMON monitor being used: */
291 MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */
292 MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */
293 /* Last and unused value, for sizing vectors, etc. */
296 static enum mips_monitor_type mips_monitor = MON_LAST;
298 /* The default monitor prompt text: */
299 static char *mips_monitor_prompt = TARGET_MONITOR_PROMPT;
300 /* For the Cogent PMON world this is still not ideal. The default
301 prompt is "PMON> ", unfortunately the user can change the prompt
302 and the new prompt will survive over a power-cycle (EEPROM). This
303 means that the code should really force the monitor prompt to a
304 known value as the very first action, and that the
305 "mips_monitor_prompt" support is not needed... since the prompt
306 could be explicitly set to TARGET_MONITOR_PROMPT (even though it
307 may be the prompt for a different monitor). However, this will
308 require changing the mips_initialize reset sequence. (TODO) */
310 /* Set to 1 if the target is open. */
311 static int mips_is_open;
313 /* Currently active target description (if mips_is_open == 1) */
314 static struct target_ops *current_ops;
316 /* Set to 1 while the connection is being initialized. */
317 static int mips_initializing;
319 /* The next sequence number to send. */
320 static unsigned int mips_send_seq;
322 /* The next sequence number we expect to receive. */
323 static unsigned int mips_receive_seq;
325 /* The time to wait before retransmitting a packet, in seconds. */
326 static int mips_retransmit_wait = 3;
328 /* The number of times to try retransmitting a packet before giving up. */
329 static int mips_send_retries = 10;
331 /* The number of garbage characters to accept when looking for an
332 SYN for the next packet. */
333 static int mips_syn_garbage = 1050;
335 /* The time to wait for a packet, in seconds. */
336 static int mips_receive_wait = 5;
338 /* Set if we have sent a packet to the board but have not yet received
340 static int mips_need_reply = 0;
342 /* Handle used to access serial I/O stream. */
343 static serial_t mips_desc;
345 /* UDP handle used to download files to target. */
346 static serial_t udp_desc;
347 static int udp_in_use;
349 /* TFTP filename used to download files to DDB board, in the form
351 static char *tftp_name; /* host:filename */
352 static char *tftp_localname; /* filename portion of above */
353 static int tftp_in_use;
354 static FILE *tftp_file;
356 /* Counts the number of times the user tried to interrupt the target (usually
358 static int interrupt_count;
360 /* If non-zero, means that the target is running. */
361 static int mips_wait_flag = 0;
363 /* If non-zero, monitor supports breakpoint commands. */
364 static monitor_supports_breakpoints = 0;
366 /* Data cache header. */
368 #if 0 /* not used (yet?) */
369 static DCACHE *mips_dcache;
372 /* Non-zero means that we've just hit a read or write watchpoint */
373 static int hit_watchpoint;
379 SERIAL_CLOSE (mips_desc);
383 SERIAL_CLOSE (udp_desc);
389 /* Handle low-level error that we can't recover from. Note that just
390 error()ing out from target_wait or some such low-level place will cause
391 all hell to break loose--the rest of GDB will tend to get left in an
392 inconsistent state. */
395 #ifdef ANSI_PROTOTYPES
396 mips_error (char *string, ...)
398 mips_error (va_alist)
404 #ifdef ANSI_PROTOTYPES
405 va_start (args, string);
409 string = va_arg (args, char *);
412 target_terminal_ours ();
413 wrap_here(""); /* Force out any buffered output */
414 gdb_flush (gdb_stdout);
416 fprintf_filtered (gdb_stderr, error_pre_print);
417 vfprintf_filtered (gdb_stderr, string, args);
418 fprintf_filtered (gdb_stderr, "\n");
420 gdb_flush (gdb_stderr);
422 /* Clean up in such a way that mips_close won't try to talk to the
423 board (it almost surely won't work since we weren't able to talk to
427 printf_unfiltered ("Ending remote MIPS debugging.\n");
428 target_mourn_inferior ();
430 return_to_top_level (RETURN_ERROR);
433 /* putc_readable - print a character, displaying non-printable chars in
434 ^x notation or in hex. */
441 putchar_unfiltered ('\n');
443 printf_unfiltered ("\\r");
444 else if (ch < 0x20) /* ASCII control character */
445 printf_unfiltered ("^%c", ch + '@');
446 else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */
447 printf_unfiltered ("[%02x]", ch & 0xff);
449 putchar_unfiltered (ch);
453 /* puts_readable - print a string, displaying non-printable chars in
454 ^x notation or in hex. */
457 puts_readable (string)
462 while ((c = *string++) != '\0')
467 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
468 timed out. TIMEOUT specifies timeout value in seconds.
472 mips_expect_timeout (string, timeout)
480 printf_unfiltered ("Expected \"");
481 puts_readable (string);
482 printf_unfiltered ("\", got \"");
490 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
491 were waiting for the mips_monitor_prompt... */
493 c = SERIAL_READCHAR (mips_desc, timeout);
495 if (c == SERIAL_TIMEOUT)
498 printf_unfiltered ("\": FAIL\n");
511 printf_unfiltered ("\": OK\n");
524 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
525 timed out. The timeout value is hard-coded to 2 seconds. Use
526 mips_expect_timeout if a different timeout value is needed.
533 return mips_expect_timeout (string, 2);
536 /* Read the required number of characters into the given buffer (which
537 is assumed to be large enough). The only failure is a timeout. */
539 mips_getstring (string, n)
549 c = SERIAL_READCHAR (mips_desc, 2);
551 if (c == SERIAL_TIMEOUT) {
552 fprintf_unfiltered (stderr, "Failed to read %d characters from target (TIMEOUT)\n", n);
563 /* Read a character from the remote, aborting on error. Returns
564 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
565 returns). FIXME: If we see the string mips_monitor_prompt from
566 the board, then we are debugging on the main console port, and we
567 have somehow dropped out of remote debugging mode. In this case,
568 we automatically go back in to remote debugging mode. This is a
569 hack, put in because I can't find any way for a program running on
570 the remote board to terminate without also ending remote debugging
571 mode. I assume users won't have any trouble with this; for one
572 thing, the IDT documentation generally assumes that the remote
573 debugging port is not the console port. This is, however, very
574 convenient for DejaGnu when you only have one connected serial
578 mips_readchar (timeout)
582 static int state = 0;
583 static int mips_monitor_prompt_len = -1;
585 /* NASTY, since we assume that the prompt does not change after the
586 first mips_readchar call: */
587 if (mips_monitor_prompt_len == -1)
588 mips_monitor_prompt_len = strlen(mips_monitor_prompt);
590 #ifdef MAINTENANCE_CMDS
595 if (i == -1 && watchdog > 0)
600 if (state == mips_monitor_prompt_len)
602 ch = SERIAL_READCHAR (mips_desc, timeout);
603 #ifdef MAINTENANCE_CMDS
604 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
606 target_mourn_inferior ();
607 error ("Watchdog has expired. Target detached.\n");
610 if (ch == SERIAL_EOF)
611 mips_error ("End of file from remote");
612 if (ch == SERIAL_ERROR)
613 mips_error ("Error reading from remote: %s", safe_strerror (errno));
614 if (remote_debug > 1)
616 /* Don't use _filtered; we can't deal with a QUIT out of
617 target_wait, and I think this might be called from there. */
618 if (ch != SERIAL_TIMEOUT)
619 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
621 printf_unfiltered ("Timed out in read\n");
624 /* If we have seen mips_monitor_prompt and we either time out, or
625 we see a @ (which was echoed from a packet we sent), reset the
626 board as described above. The first character in a packet after
627 the SYN (which is not echoed) is always an @ unless the packet is
628 more than 64 characters long, which ours never are. */
629 if ((ch == SERIAL_TIMEOUT || ch == '@')
630 && state == mips_monitor_prompt_len
631 && ! mips_initializing)
633 if (remote_debug > 0)
634 /* Don't use _filtered; we can't deal with a QUIT out of
635 target_wait, and I think this might be called from there. */
636 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
643 /* At this point, about the only thing we can do is abort the command
644 in progress and get back to command level as quickly as possible. */
646 error ("Remote board reset, debug protocol re-initialized.");
649 if (ch == mips_monitor_prompt[state])
657 /* Get a packet header, putting the data in the supplied buffer.
658 PGARBAGE is a pointer to the number of garbage characters received
659 so far. CH is the last character received. Returns 0 for success,
660 or -1 for timeout. */
663 mips_receive_header (hdr, pgarbage, ch, timeout)
673 /* Wait for a SYN. mips_syn_garbage is intended to prevent
674 sitting here indefinitely if the board sends us one garbage
675 character per second. ch may already have a value from the
676 last time through the loop. */
679 ch = mips_readchar (timeout);
680 if (ch == SERIAL_TIMEOUT)
684 /* Printing the character here lets the user of gdb see
685 what the program is outputting, if the debugging is
686 being done on the console port. Don't use _filtered;
687 we can't deal with a QUIT out of target_wait. */
688 if (! mips_initializing || remote_debug > 0)
691 gdb_flush (gdb_stdout);
695 if (mips_syn_garbage > 0
696 && *pgarbage > mips_syn_garbage)
697 mips_error ("Debug protocol failure: more than %d characters before a sync.",
702 /* Get the packet header following the SYN. */
703 for (i = 1; i < HDR_LENGTH; i++)
705 ch = mips_readchar (timeout);
706 if (ch == SERIAL_TIMEOUT)
708 /* Make sure this is a header byte. */
709 if (ch == SYN || ! HDR_CHECK (ch))
715 /* If we got the complete header, we can return. Otherwise we
716 loop around and keep looking for SYN. */
722 /* Get a packet header, putting the data in the supplied buffer.
723 PGARBAGE is a pointer to the number of garbage characters received
724 so far. The last character read is returned in *PCH. Returns 0
725 for success, -1 for timeout, -2 for error. */
728 mips_receive_trailer (trlr, pgarbage, pch, timeout)
737 for (i = 0; i < TRLR_LENGTH; i++)
739 ch = mips_readchar (timeout);
741 if (ch == SERIAL_TIMEOUT)
743 if (! TRLR_CHECK (ch))
750 /* Get the checksum of a packet. HDR points to the packet header.
751 DATA points to the packet data. LEN is the length of DATA. */
754 mips_cksum (hdr, data, len)
755 const unsigned char *hdr;
756 const unsigned char *data;
759 register const unsigned char *p;
765 /* The initial SYN is not included in the checksum. */
779 /* Send a packet containing the given ASCII string. */
782 mips_send_packet (s, get_ack)
786 /* unsigned */ int len;
787 unsigned char *packet;
792 if (len > DATA_MAXLEN)
793 mips_error ("MIPS protocol data packet too long: %s", s);
795 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
797 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
798 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
799 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
800 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
802 memcpy (packet + HDR_LENGTH, s, len);
804 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
805 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
806 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
807 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
809 /* Increment the sequence number. This will set mips_send_seq to
810 the sequence number we expect in the acknowledgement. */
811 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
813 /* We can only have one outstanding data packet, so we just wait for
814 the acknowledgement here. Keep retransmitting the packet until
815 we get one, or until we've tried too many times. */
816 for (try = 0; try < mips_send_retries; try++)
821 if (remote_debug > 0)
823 /* Don't use _filtered; we can't deal with a QUIT out of
824 target_wait, and I think this might be called from there. */
825 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
826 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
829 if (SERIAL_WRITE (mips_desc, packet,
830 HDR_LENGTH + len + TRLR_LENGTH) != 0)
831 mips_error ("write to target failed: %s", safe_strerror (errno));
840 unsigned char hdr[HDR_LENGTH + 1];
841 unsigned char trlr[TRLR_LENGTH + 1];
845 /* Get the packet header. If we time out, resend the data
847 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
853 /* If we get a data packet, assume it is a duplicate and
854 ignore it. FIXME: If the acknowledgement is lost, this
855 data packet may be the packet the remote sends after the
857 if (HDR_IS_DATA (hdr)) {
860 /* Ignore any errors raised whilst attempting to ignore
863 len = HDR_GET_LEN (hdr);
865 for (i = 0; i < len; i++)
869 rch = mips_readchar (2);
875 if (rch == SERIAL_TIMEOUT)
877 /* ignore the character */
881 (void) mips_receive_trailer (trlr, &garbage, &ch, 2);
883 /* We don't bother checking the checksum, or providing an
884 ACK to the packet. */
888 /* If the length is not 0, this is a garbled packet. */
889 if (HDR_GET_LEN (hdr) != 0)
892 /* Get the packet trailer. */
893 err = mips_receive_trailer (trlr, &garbage, &ch,
894 mips_retransmit_wait);
896 /* If we timed out, resend the data packet. */
900 /* If we got a bad character, reread the header. */
904 /* If the checksum does not match the trailer checksum, this
905 is a bad packet; ignore it. */
906 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
907 != TRLR_GET_CKSUM (trlr))
910 if (remote_debug > 0)
912 hdr[HDR_LENGTH] = '\0';
913 trlr[TRLR_LENGTH] = '\0';
914 /* Don't use _filtered; we can't deal with a QUIT out of
915 target_wait, and I think this might be called from there. */
916 printf_unfiltered ("Got ack %d \"%s%s\"\n",
917 HDR_GET_SEQ (hdr), hdr + 1, trlr);
920 /* If this ack is for the current packet, we're done. */
921 seq = HDR_GET_SEQ (hdr);
922 if (seq == mips_send_seq)
925 /* If this ack is for the last packet, resend the current
927 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
930 /* Otherwise this is a bad ack; ignore it. Increment the
931 garbage count to ensure that we do not stay in this loop
937 mips_error ("Remote did not acknowledge packet");
940 /* Receive and acknowledge a packet, returning the data in BUFF (which
941 should be DATA_MAXLEN + 1 bytes). The protocol documentation
942 implies that only the sender retransmits packets, so this code just
943 waits silently for a packet. It returns the length of the received
944 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
945 don't print an error message and return -1. */
948 mips_receive_packet (buff, throw_error, timeout)
956 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
963 unsigned char hdr[HDR_LENGTH];
964 unsigned char trlr[TRLR_LENGTH];
968 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
971 mips_error ("Timed out waiting for remote packet");
978 /* An acknowledgement is probably a duplicate; ignore it. */
979 if (! HDR_IS_DATA (hdr))
981 len = HDR_GET_LEN (hdr);
982 /* Check if the length is valid for an ACK, we may aswell
983 try and read the remainder of the packet: */
986 /* Ignore the error condition, since we are going to
987 ignore the packet anyway. */
988 (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
990 /* Don't use _filtered; we can't deal with a QUIT out of
991 target_wait, and I think this might be called from there. */
992 if (remote_debug > 0)
993 printf_unfiltered ("Ignoring unexpected ACK\n");
997 len = HDR_GET_LEN (hdr);
998 for (i = 0; i < len; i++)
1002 rch = mips_readchar (timeout);
1008 if (rch == SERIAL_TIMEOUT)
1011 mips_error ("Timed out waiting for remote packet");
1020 /* Don't use _filtered; we can't deal with a QUIT out of
1021 target_wait, and I think this might be called from there. */
1022 if (remote_debug > 0)
1023 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
1028 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
1032 mips_error ("Timed out waiting for packet");
1038 /* Don't use _filtered; we can't deal with a QUIT out of
1039 target_wait, and I think this might be called from there. */
1040 if (remote_debug > 0)
1041 printf_unfiltered ("Got SYN when wanted trailer\n");
1045 /* If this is the wrong sequence number, ignore it. */
1046 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
1048 /* Don't use _filtered; we can't deal with a QUIT out of
1049 target_wait, and I think this might be called from there. */
1050 if (remote_debug > 0)
1051 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
1052 HDR_GET_SEQ (hdr), mips_receive_seq);
1056 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
1059 if (remote_debug > 0)
1060 /* Don't use _filtered; we can't deal with a QUIT out of
1061 target_wait, and I think this might be called from there. */
1062 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
1063 mips_cksum (hdr, buff, len),
1064 TRLR_GET_CKSUM (trlr));
1066 /* The checksum failed. Send an acknowledgement for the
1067 previous packet to tell the remote to resend the packet. */
1068 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1069 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1070 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1071 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1073 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1075 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1076 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1077 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1079 if (remote_debug > 0)
1081 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1082 /* Don't use _filtered; we can't deal with a QUIT out of
1083 target_wait, and I think this might be called from there. */
1084 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1088 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1091 mips_error ("write to target failed: %s", safe_strerror (errno));
1097 if (remote_debug > 0)
1100 /* Don't use _filtered; we can't deal with a QUIT out of
1101 target_wait, and I think this might be called from there. */
1102 printf_unfiltered ("Got packet \"%s\"\n", buff);
1105 /* We got the packet. Send an acknowledgement. */
1106 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1108 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1109 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1110 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1111 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1113 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1115 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1116 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1117 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1119 if (remote_debug > 0)
1121 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1122 /* Don't use _filtered; we can't deal with a QUIT out of
1123 target_wait, and I think this might be called from there. */
1124 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1128 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1131 mips_error ("write to target failed: %s", safe_strerror (errno));
1139 /* Optionally send a request to the remote system and optionally wait
1140 for the reply. This implements the remote debugging protocol,
1141 which is built on top of the packet protocol defined above. Each
1142 request has an ADDR argument and a DATA argument. The following
1143 requests are defined:
1145 \0 don't send a request; just wait for a reply
1146 i read word from instruction space at ADDR
1147 d read word from data space at ADDR
1148 I write DATA to instruction space at ADDR
1149 D write DATA to data space at ADDR
1150 r read register number ADDR
1151 R set register number ADDR to value DATA
1152 c continue execution (if ADDR != 1, set pc to ADDR)
1153 s single step (if ADDR != 1, set pc to ADDR)
1155 The read requests return the value requested. The write requests
1156 return the previous value in the changed location. The execution
1157 requests return a UNIX wait value (the approximate signal which
1158 caused execution to stop is in the upper eight bits).
1160 If PERR is not NULL, this function waits for a reply. If an error
1161 occurs, it sets *PERR to 1 and sets errno according to what the
1162 target board reports. */
1165 mips_request (cmd, addr, data, perr, timeout, buff)
1173 char myBuff[DATA_MAXLEN + 1];
1180 if (buff == (char *) NULL)
1185 if (mips_need_reply)
1186 fatal ("mips_request: Trying to send command before reply");
1187 sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
1188 mips_send_packet (buff, 1);
1189 mips_need_reply = 1;
1192 if (perr == (int *) NULL)
1195 if (! mips_need_reply)
1196 fatal ("mips_request: Trying to get reply before command");
1198 mips_need_reply = 0;
1200 len = mips_receive_packet (buff, 1, timeout);
1203 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
1204 &rpid, &rcmd, &rerrflg, &rresponse) != 4
1205 || (cmd != '\0' && rcmd != cmd))
1206 mips_error ("Bad response from remote board");
1212 /* FIXME: This will returns MIPS errno numbers, which may or may
1213 not be the same as errno values used on other systems. If
1214 they stick to common errno values, they will be the same, but
1215 if they don't, they must be translated. */
1226 mips_initialize_cleanups (arg)
1229 mips_initializing = 0;
1233 mips_send_command (cmd, prompt)
1237 SERIAL_WRITE (mips_desc, cmd, strlen(cmd));
1241 mips_expect (mips_monitor_prompt);
1244 /* Enter remote (dbx) debug mode: */
1248 /* Reset the sequence numbers, ready for the new debug sequence: */
1250 mips_receive_seq = 0;
1252 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
1253 mips_send_command ("debug\r", 0);
1254 else /* assume IDT monitor by default */
1255 mips_send_command ("db tty0\r", 0);
1257 SERIAL_WRITE (mips_desc, "\r", sizeof "\r" - 1);
1259 /* We don't need to absorb any spurious characters here, since the
1260 mips_receive_header will eat up a reasonable number of characters
1261 whilst looking for the SYN, however this avoids the "garbage"
1262 being displayed to the user. */
1263 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
1267 char buff[DATA_MAXLEN + 1];
1268 if (mips_receive_packet (buff, 1, 3) < 0)
1269 mips_error ("Failed to initialize (didn't receive packet).");
1273 /* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1279 if (mips_monitor == MON_DDB)
1281 /* The Ddb version of PMON exits immediately, so we do not get
1282 a reply to this command: */
1283 mips_request ('x', (unsigned int) 0, (unsigned int) 0, NULL,
1284 mips_receive_wait, NULL);
1285 mips_need_reply = 0;
1286 if (!mips_expect (" break!"))
1290 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1291 mips_receive_wait, NULL);
1293 if (mips_monitor == MON_PMON && !mips_expect ("Exiting remote debug mode"))
1296 if (mips_monitor == MON_DDB)
1298 if (!mips_expect ("\n"))
1302 if (!mips_expect ("\r\n"))
1305 if (!mips_expect (mips_monitor_prompt))
1311 /* Initialize a new connection to the MIPS board, and make sure we are
1312 really connected. */
1318 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1321 /* What is this code doing here? I don't see any way it can happen, and
1322 it might mean mips_initializing didn't get cleared properly.
1323 So I'll make it a warning. */
1325 if (mips_initializing)
1327 warning ("internal error: mips_initialize called twice");
1332 mips_initializing = 1;
1334 /* At this point, the packit protocol isn't responding. We'll try getting
1335 into the monitor, and restarting the protocol. */
1337 /* Force the system into the monitor. After this we *should* be at
1338 the mips_monitor_prompt. */
1339 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
1340 j = 0; /* start by checking if we are already at the prompt */
1342 j = 1; /* start by sending a break */
1347 case 0: /* First, try sending a CR */
1348 SERIAL_FLUSH_INPUT (mips_desc);
1349 SERIAL_WRITE (mips_desc, "\r", 1);
1351 case 1: /* First, try sending a break */
1352 SERIAL_SEND_BREAK (mips_desc);
1354 case 2: /* Then, try a ^C */
1355 SERIAL_WRITE (mips_desc, "\003", 1);
1357 case 3: /* Then, try escaping from download */
1359 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
1363 /* We shouldn't need to send multiple termination
1364 sequences, since the target performs line (or
1365 block) reads, and then processes those
1366 packets. In-case we were downloading a large packet
1367 we flush the output buffer before inserting a
1368 termination sequence. */
1369 SERIAL_FLUSH_OUTPUT (mips_desc);
1370 sprintf (tbuff, "\r/E/E\r");
1371 SERIAL_WRITE (mips_desc, tbuff, 6);
1378 /* We are possibly in binary download mode, having
1379 aborted in the middle of an S-record. ^C won't
1380 work because of binary mode. The only reliable way
1381 out is to send enough termination packets (8 bytes)
1382 to fill up and then overflow the largest size
1383 S-record (255 bytes in this case). This amounts to
1387 mips_make_srec (srec, '7', 0, NULL, 0);
1389 for (i = 1; i <= 33; i++)
1391 SERIAL_WRITE (mips_desc, srec, 8);
1393 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1394 break; /* Break immediatly if we get something from
1401 mips_error ("Failed to initialize.");
1404 if (mips_expect (mips_monitor_prompt))
1408 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
1410 /* Ensure the correct target state: */
1411 mips_send_command ("set regsize 64\r", -1);
1412 mips_send_command ("set hostport tty0\r", -1);
1413 mips_send_command ("set brkcmd \"\"\r", -1);
1414 /* Delete all the current breakpoints: */
1415 mips_send_command ("db *\r", -1);
1416 /* NOTE: PMON does not have breakpoint support through the
1417 "debug" mode, only at the monitor command-line. */
1420 mips_enter_debug ();
1422 /* Clear all breakpoints: */
1423 if (common_breakpoint ('b', -1, 0, NULL))
1424 monitor_supports_breakpoints = 0;
1426 monitor_supports_breakpoints = 1;
1428 do_cleanups (old_cleanups);
1430 /* If this doesn't call error, we have connected; we don't care if
1431 the request itself succeeds or fails. */
1433 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
1434 mips_receive_wait, NULL);
1435 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1436 select_frame (get_current_frame (), 0);
1439 /* Open a connection to the remote board. */
1441 common_open (ops, name, from_tty)
1442 struct target_ops *ops;
1447 char *serial_port_name;
1448 char *remote_name = 0;
1449 char *local_name = 0;
1454 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1455 device is attached to the target board (e.g., /dev/ttya).\n"
1456 "If you want to use TFTP to download to the board, specify the name of a\n"
1457 "temporary file to be used by GDB for downloads as the second argument.\n"
1458 "This filename must be in the form host:filename, where host is the name\n"
1459 "of the host running the TFTP server, and the file must be readable by the\n"
1460 "world. If the local name of the temporary file differs from the name as\n",
1461 "seen from the board via TFTP, specify that name as the third parameter\n");
1463 /* Parse the serial port name, the optional TFTP name, and the
1464 optional local TFTP name. */
1465 if ((argv = buildargv (name)) == NULL)
1467 make_cleanup (freeargv, (char *) argv);
1469 serial_port_name = strsave (argv[0]);
1470 if (argv[1]) /* remote TFTP name specified? */
1472 remote_name = argv[1];
1473 if (argv[2]) /* local TFTP filename specified? */
1474 local_name = argv[2];
1477 target_preopen (from_tty);
1480 unpush_target (current_ops);
1482 /* Open and initialize the serial port. */
1483 mips_desc = SERIAL_OPEN (serial_port_name);
1484 if (mips_desc == (serial_t) NULL)
1485 perror_with_name (serial_port_name);
1487 if (baud_rate != -1)
1489 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1491 SERIAL_CLOSE (mips_desc);
1492 perror_with_name (serial_port_name);
1496 SERIAL_RAW (mips_desc);
1498 /* Open and initialize the optional download port. If it is in the form
1499 hostname#portnumber, it's a UDP socket. If it is in the form
1500 hostname:filename, assume it's the TFTP filename that must be
1501 passed to the DDB board to tell it where to get the load file. */
1504 if (strchr (remote_name, '#'))
1506 udp_desc = SERIAL_OPEN (remote_name);
1508 perror_with_name ("Unable to open UDP port");
1513 /* Save the remote and local names of the TFTP temp file. If
1514 the user didn't specify a local name, assume it's the same
1515 as the part of the remote name after the "host:". */
1519 free (tftp_localname);
1520 if (local_name == NULL)
1521 if ((local_name = strchr (remote_name, ':')) != NULL)
1522 local_name++; /* skip over the colon */
1523 if (local_name == NULL)
1524 local_name = remote_name; /* local name same as remote name */
1525 tftp_name = strsave (remote_name);
1526 tftp_localname = strsave (local_name);
1537 printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
1539 /* Switch to using remote target now. */
1542 /* FIXME: Should we call start_remote here? */
1544 /* Try to figure out the processor model if possible. */
1545 ptype = mips_read_processor_type ();
1547 mips_set_processor_type_command (strsave (ptype), 0);
1549 /* This is really the job of start_remote however, that makes an assumption
1550 that the target is about to print out a status message of some sort. That
1551 doesn't happen here (in fact, it may not be possible to get the monitor to
1552 send the appropriate packet). */
1554 flush_cached_frames ();
1555 registers_changed ();
1556 stop_pc = read_pc ();
1557 set_current_frame (create_new_frame (read_fp (), stop_pc));
1558 select_frame (get_current_frame (), 0);
1559 print_stack_frame (selected_frame, -1, 1);
1560 free (serial_port_name);
1564 mips_open (name, from_tty)
1568 mips_monitor = MON_IDT;
1569 common_open (&mips_ops, name, from_tty);
1573 pmon_open (name, from_tty)
1577 /* The PMON monitor has a prompt different from the default
1578 "TARGET_MONITOR_PROMPT": */
1579 mips_monitor_prompt = "PMON> ";
1580 mips_monitor = MON_PMON;
1581 common_open (&pmon_ops, name, from_tty);
1585 ddb_open (name, from_tty)
1589 /* The PMON monitor has a prompt different from the default
1590 "TARGET_MONITOR_PROMPT": */
1591 mips_monitor_prompt = "NEC010>";
1592 mips_monitor = MON_DDB;
1593 common_open (&ddb_ops, name, from_tty);
1596 /* Close a connection to the remote board. */
1599 mips_close (quitting)
1604 /* Get the board out of remote debugging mode. */
1605 (void) mips_exit_debug ();
1611 /* Detach from the remote board. */
1614 mips_detach (args, from_tty)
1619 error ("Argument given to \"detach\" when remotely debugging.");
1626 printf_unfiltered ("Ending remote MIPS debugging.\n");
1629 /* Tell the target board to resume. This does not wait for a reply
1633 mips_resume (pid, step, siggnal)
1635 enum target_signal siggnal;
1638 /* start-sanitize-gm */
1639 #ifndef GENERAL_MAGIC
1640 if (siggnal != TARGET_SIGNAL_0)
1642 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1643 target_signal_to_name (siggnal));
1644 #endif /* GENERAL_MAGIC */
1645 /* end-sanitize-gm */
1647 mips_request (step ? 's' : 'c',
1649 (unsigned int) siggnal,
1651 mips_receive_wait, NULL);
1654 /* Return the signal corresponding to SIG, where SIG is the number which
1655 the MIPS protocol uses for the signal. */
1657 mips_signal_from_protocol (sig)
1660 /* We allow a few more signals than the IDT board actually returns, on
1661 the theory that there is at least *some* hope that perhaps the numbering
1662 for these signals is widely agreed upon. */
1665 return TARGET_SIGNAL_UNKNOWN;
1667 /* Don't want to use target_signal_from_host because we are converting
1668 from MIPS signal numbers, not host ones. Our internal numbers
1669 match the MIPS numbers for the signals the board can return, which
1670 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1671 return (enum target_signal) sig;
1674 /* Wait until the remote stops, and return a wait status. */
1677 mips_wait (pid, status)
1679 struct target_waitstatus *status;
1683 char buff[DATA_MAXLEN];
1688 interrupt_count = 0;
1691 /* If we have not sent a single step or continue command, then the
1692 board is waiting for us to do something. Return a status
1693 indicating that it is stopped. */
1694 if (! mips_need_reply)
1696 status->kind = TARGET_WAITKIND_STOPPED;
1697 status->value.sig = TARGET_SIGNAL_TRAP;
1701 /* No timeout; we sit here as long as the program continues to execute. */
1703 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1707 mips_error ("Remote failure: %s", safe_strerror (errno));
1709 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1710 &rpc, &rfp, &rsp, flags);
1712 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1714 if (nfields == 7 || nfields == 9)
1716 char buf[MAX_REGISTER_RAW_SIZE];
1718 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1719 supply_register (PC_REGNUM, buf);
1721 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1722 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1724 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1725 supply_register (SP_REGNUM, buf);
1727 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1728 supply_register (FP_REGNUM, buf);
1734 for (i = 0; i <= 2; i++)
1735 if (flags[i] == 'r' || flags[i] == 'w')
1737 else if (flags[i] == '\000')
1742 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1743 and so on, because the constants we want here are determined by the
1744 MIPS protocol and have nothing to do with what host we are running on. */
1745 if ((rstatus & 0377) == 0)
1747 status->kind = TARGET_WAITKIND_EXITED;
1748 status->value.integer = (((rstatus) >> 8) & 0377);
1750 else if ((rstatus & 0377) == 0177)
1752 status->kind = TARGET_WAITKIND_STOPPED;
1753 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1757 status->kind = TARGET_WAITKIND_SIGNALLED;
1758 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1765 pmon_wait (pid, status)
1767 struct target_waitstatus *status;
1771 char buff[DATA_MAXLEN];
1773 interrupt_count = 0;
1776 /* If we have not sent a single step or continue command, then the
1777 board is waiting for us to do something. Return a status
1778 indicating that it is stopped. */
1779 if (! mips_need_reply)
1781 status->kind = TARGET_WAITKIND_STOPPED;
1782 status->value.sig = TARGET_SIGNAL_TRAP;
1786 /* Sit, polling the serial until the target decides to talk to
1787 us. NOTE: the timeout value we use is used not just for the
1788 first character, but for all the characters. */
1790 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1794 mips_error ("Remote failure: %s", safe_strerror (errno));
1796 /* NOTE: The following (sig) numbers are defined by PMON:
1797 SPP_SIGTRAP 5 breakpoint
1805 /* On returning from a continue, the PMON monitor seems to start
1806 echoing back the messages we send prior to sending back the
1807 ACK. The code can cope with this, but to try and avoid the
1808 unnecessary serial traffic, and "spurious" characters displayed
1809 to the user, we cheat and reset the debug protocol. The problems
1810 seems to be caused by a check on the number of arguments, and the
1811 command length, within the monitor causing it to echo the command
1813 if (mips_monitor != MON_DDB)
1816 mips_enter_debug ();
1819 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1820 and so on, because the constants we want here are determined by the
1821 MIPS protocol and have nothing to do with what host we are running on. */
1822 if ((rstatus & 0377) == 0)
1824 status->kind = TARGET_WAITKIND_EXITED;
1825 status->value.integer = (((rstatus) >> 8) & 0377);
1827 else if ((rstatus & 0377) == 0177)
1829 status->kind = TARGET_WAITKIND_STOPPED;
1830 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1834 status->kind = TARGET_WAITKIND_SIGNALLED;
1835 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1841 /* We have to map between the register numbers used by gdb and the
1842 register numbers used by the debugging protocol. This function
1843 assumes that we are using tm-mips.h. */
1845 #define REGNO_OFFSET 96
1848 mips_map_regno (regno)
1853 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1854 return regno - FP0_REGNUM + 32;
1858 return REGNO_OFFSET + 0;
1860 return REGNO_OFFSET + 1;
1862 return REGNO_OFFSET + 2;
1864 return REGNO_OFFSET + 3;
1866 return REGNO_OFFSET + 4;
1868 return REGNO_OFFSET + 5;
1870 /* FIXME: Is there a way to get the status register? */
1875 /* Fetch the remote registers. */
1878 mips_fetch_registers (regno)
1881 unsigned LONGEST val;
1886 for (regno = 0; regno < NUM_REGS; regno++)
1887 mips_fetch_registers (regno);
1891 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1892 /* FP_REGNUM on the mips is a hack which is just supposed to read
1893 zero (see also mips-nat.c). */
1897 /* Unfortunately the PMON version in the Vr4300 board has been
1898 compiled without the 64bit register access commands. This
1899 means we cannot get hold of the full register width. */
1900 if (mips_monitor == MON_DDB)
1901 val = (unsigned)mips_request ('t', (unsigned int) mips_map_regno (regno),
1902 (unsigned int) 0, &err, mips_receive_wait, NULL);
1904 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1905 (unsigned int) 0, &err, mips_receive_wait, NULL);
1907 mips_error ("Can't read register %d: %s", regno,
1908 safe_strerror (errno));
1912 char buf[MAX_REGISTER_RAW_SIZE];
1914 /* We got the number the register holds, but gdb expects to see a
1915 value in the target byte ordering. */
1916 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1917 supply_register (regno, buf);
1921 /* Prepare to store registers. The MIPS protocol can store individual
1922 registers, so this function doesn't have to do anything. */
1925 mips_prepare_to_store ()
1929 /* Store remote register(s). */
1932 mips_store_registers (regno)
1939 for (regno = 0; regno < NUM_REGS; regno++)
1940 mips_store_registers (regno);
1944 mips_request ('R', (unsigned int) mips_map_regno (regno),
1945 read_register (regno),
1946 &err, mips_receive_wait, NULL);
1948 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1951 /* Fetch a word from the target board. */
1954 mips_fetch_word (addr)
1960 /* FIXME! addr was cast to uint! */
1961 val = mips_request ('d', addr, (unsigned int) 0, &err,
1962 mips_receive_wait, NULL);
1965 /* Data space failed; try instruction space. */
1966 /* FIXME! addr was cast to uint! */
1967 val = mips_request ('i', addr, (unsigned int) 0, &err,
1968 mips_receive_wait, NULL);
1970 mips_error ("Can't read address 0x%s: %s",
1971 paddr_nz (addr), safe_strerror (errno));
1976 /* Store a word to the target board. Returns errno code or zero for
1977 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1978 memory location there. */
1980 /* FIXME! make sure only 32-bit quantities get stored! */
1982 mips_store_word (addr, val, old_contents)
1988 unsigned int oldcontents;
1990 oldcontents = mips_request ('D', addr, (unsigned int) val,
1992 mips_receive_wait, NULL);
1995 /* Data space failed; try instruction space. */
1996 oldcontents = mips_request ('I', addr,
1997 (unsigned int) val, &err,
1998 mips_receive_wait, NULL);
2002 if (old_contents != NULL)
2003 store_unsigned_integer (old_contents, 4, oldcontents);
2007 /* Read or write LEN bytes from inferior memory at MEMADDR,
2008 transferring to or from debugger address MYADDR. Write to inferior
2009 if SHOULD_WRITE is nonzero. Returns length of data written or
2010 read; 0 for error. Note that protocol gives us the correct value
2011 for a longword, since it transfers values in ASCII. We want the
2012 byte values, so we have to swap the longword values. */
2015 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
2020 struct target_ops *ignore;
2023 /* Round starting address down to longword boundary. */
2024 register CORE_ADDR addr = memaddr &~ 3;
2025 /* Round ending address up; get number of longwords that makes. */
2026 register int count = (((memaddr + len) - addr) + 3) / 4;
2027 /* Allocate buffer of that many longwords. */
2028 register char *buffer = alloca (count * 4);
2034 /* Fill start and end extra bytes of buffer with existing data. */
2035 if (addr != memaddr || len < 4)
2037 /* Need part of initial word -- fetch it. */
2038 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
2043 /* Need part of last word -- fetch it. FIXME: we do this even
2044 if we don't need it. */
2045 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2046 mips_fetch_word (addr + (count - 1) * 4));
2049 /* Copy data to be written over corresponding part of buffer */
2051 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2053 /* Write the entire buffer. */
2055 for (i = 0; i < count; i++, addr += 4)
2057 status = mips_store_word (addr,
2058 extract_unsigned_integer (&buffer[i*4], 4),
2060 /* Report each kilobyte (we download 32-bit words at a time) */
2063 printf_unfiltered ("*");
2071 /* FIXME: Do we want a QUIT here? */
2074 printf_unfiltered ("\n");
2078 /* Read all the longwords */
2079 for (i = 0; i < count; i++, addr += 4)
2081 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
2085 /* Copy appropriate bytes out of the buffer. */
2086 memcpy (myaddr, buffer + (memaddr & 3), len);
2091 /* Print info on this target. */
2094 mips_files_info (ignore)
2095 struct target_ops *ignore;
2097 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
2100 /* Kill the process running on the board. This will actually only
2101 work if we are doing remote debugging over the console input. I
2102 think that if IDT/sim had the remote debug interrupt enabled on the
2103 right port, we could interrupt the process with a break signal. */
2108 if (!mips_wait_flag)
2113 if (interrupt_count >= 2)
2115 interrupt_count = 0;
2117 target_terminal_ours ();
2119 if (query ("Interrupted while waiting for the program.\n\
2120 Give up (and stop debugging it)? "))
2122 /* Clean up in such a way that mips_close won't try to talk to the
2123 board (it almost surely won't work since we weren't able to talk to
2128 printf_unfiltered ("Ending remote MIPS debugging.\n");
2129 target_mourn_inferior ();
2131 return_to_top_level (RETURN_QUIT);
2134 target_terminal_inferior ();
2137 if (remote_debug > 0)
2138 printf_unfiltered ("Sending break\n");
2140 SERIAL_SEND_BREAK (mips_desc);
2149 SERIAL_WRITE (mips_desc, &cc, 1);
2151 target_mourn_inferior ();
2156 /* Start running on the target board. */
2159 mips_create_inferior (execfile, args, env)
2169 Can't pass arguments to remote MIPS board; arguments ignored.");
2170 /* And don't try to use them on the next "run" command. */
2171 execute_command ("set args", 0);
2174 if (execfile == 0 || exec_bfd == 0)
2175 error ("No executable file specified");
2177 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2179 init_wait_for_inferior ();
2181 /* FIXME: Should we set inferior_pid here? */
2183 /* start-sanitize-gm */
2184 #ifdef GENERAL_MAGIC
2185 magic_create_inferior_hook ();
2186 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
2188 /* end-sanitize-gm */
2189 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
2190 /* start-sanitize-gm */
2191 #endif /* GENERAL_MAGIC */
2192 /* end-sanitize-gm */
2195 /* Clean up after a process. Actually nothing to do. */
2198 mips_mourn_inferior ()
2200 if (current_ops != NULL)
2201 unpush_target (current_ops);
2202 generic_mourn_inferior ();
2205 /* We can write a breakpoint and read the shadow contents in one
2208 /* The IDT board uses an unusual breakpoint value, and sometimes gets
2209 confused when it sees the usual MIPS breakpoint instruction. */
2211 #define BREAK_INSN (0x00000a0d)
2212 #define BREAK_INSN_SIZE (4)
2214 /* Insert a breakpoint on targets that don't have any better breakpoint
2215 support. We read the contents of the target location and stash it,
2216 then overwrite it with a breakpoint instruction. ADDR is the target
2217 location in the target machine. CONTENTS_CACHE is a pointer to
2218 memory allocated for saving the target contents. It is guaranteed
2219 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2220 is accomplished via BREAKPOINT_MAX). */
2223 mips_insert_breakpoint (addr, contents_cache)
2225 char *contents_cache;
2227 if (monitor_supports_breakpoints)
2228 return common_breakpoint ('B', addr, 0x3, "f");
2230 return mips_store_word (addr, BREAK_INSN, contents_cache);
2234 mips_remove_breakpoint (addr, contents_cache)
2236 char *contents_cache;
2238 if (monitor_supports_breakpoints)
2239 return common_breakpoint ('b', addr, 0, NULL);
2241 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2244 #if 0 /* currently not used */
2245 /* PMON does not currently provide support for the debug mode 'b'
2246 commands to manipulate breakpoints. However, if we wanted to use
2247 the monitor breakpoints (rather than the GDB BREAK_INSN version)
2248 then this code performs the work needed to leave debug mode,
2249 set/clear the breakpoint, and then return to debug mode. */
2251 #define PMON_MAX_BP (33) /* 32 SW, 1 HW */
2252 static CORE_ADDR mips_pmon_bp_info[PMON_MAX_BP];
2253 /* NOTE: The code relies on this vector being zero-initialised by the system */
2256 pmon_insert_breakpoint (addr, contents_cache)
2258 char *contents_cache;
2262 if (monitor_supports_breakpoints)
2264 char tbuff[12]; /* space for breakpoint command */
2268 /* PMON does not support debug level breakpoint set/remove: */
2269 if (mips_exit_debug ())
2270 mips_error ("Failed to exit debug mode");
2272 sprintf (tbuff, "b %08x\r", addr);
2273 mips_send_command (tbuff, 0);
2275 mips_expect ("Bpt ");
2277 if (!mips_getstring (tbuff, 2))
2279 tbuff[2] = '\0'; /* terminate the string */
2280 if (sscanf (tbuff, "%d", &bpnum) != 1)
2282 fprintf_unfiltered (stderr, "Invalid decimal breakpoint number from target: %s\n", tbuff);
2286 mips_expect (" = ");
2288 /* Lead in the hex number we are expecting: */
2292 /* FIXME!! only 8 bytes! need to expand for Bfd64;
2293 which targets return 64-bit addresses? PMON returns only 32! */
2294 if (!mips_getstring (&tbuff[2], 8))
2296 tbuff[10] = '\0'; /* terminate the string */
2298 if (sscanf (tbuff, "0x%08x", &bpaddr) != 1)
2300 fprintf_unfiltered (stderr, "Invalid hex address from target: %s\n", tbuff);
2304 if (bpnum >= PMON_MAX_BP)
2306 fprintf_unfiltered (stderr, "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n",
2307 bpnum, PMON_MAX_BP - 1);
2312 fprintf_unfiltered (stderr, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr, bpaddr);
2314 mips_pmon_bp_info[bpnum] = bpaddr;
2316 mips_expect ("\r\n");
2317 mips_expect (mips_monitor_prompt);
2319 mips_enter_debug ();
2324 return mips_store_word (addr, BREAK_INSN, contents_cache);
2328 pmon_remove_breakpoint (addr, contents_cache)
2330 char *contents_cache;
2332 if (monitor_supports_breakpoints)
2335 char tbuff[7]; /* enough for delete breakpoint command */
2337 for (bpnum = 0; bpnum < PMON_MAX_BP; bpnum++)
2338 if (mips_pmon_bp_info[bpnum] == addr)
2341 if (bpnum >= PMON_MAX_BP)
2343 fprintf_unfiltered (stderr,
2344 "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n",
2349 if (mips_exit_debug ())
2350 mips_error ("Failed to exit debug mode");
2352 sprintf (tbuff, "db %02d\r", bpnum);
2354 mips_send_command (tbuff, -1);
2355 /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not
2356 set" message will be returned. */
2358 mips_enter_debug ();
2363 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2367 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2368 This is used for memory ref breakpoints. */
2370 static unsigned long
2371 calculate_mask (addr, len)
2378 mask = addr ^ (addr + len - 1);
2380 for (i = 32; i >= 0; i--)
2386 mask = (unsigned long) 0xffffffff >> i;
2391 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
2392 for a read watchpoint, or 2 for a read/write watchpoint. */
2395 remote_mips_set_watchpoint (addr, len, type)
2400 CORE_ADDR first_addr;
2404 mask = calculate_mask (addr, len);
2406 first_addr = addr & ~mask;
2416 case 2: /* read/write */
2423 if (common_breakpoint ('B', first_addr, mask, flags))
2430 remote_mips_remove_watchpoint (addr, len, type)
2435 CORE_ADDR first_addr;
2438 mask = calculate_mask (addr, len);
2440 first_addr = addr & ~mask;
2442 if (common_breakpoint ('b', first_addr, 0, NULL))
2449 remote_mips_stopped_by_watchpoint ()
2451 return hit_watchpoint;
2454 /* This routine generates the a breakpoint command of the form:
2456 0x0 <CMD> <ADDR> <MASK> <FLAGS>
2458 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
2459 the address of the breakpoint. <MASK> is a don't care mask for addresses.
2460 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
2463 common_breakpoint (cmd, addr, mask, flags)
2470 char buf[DATA_MAXLEN + 1];
2472 int rpid, rerrflg, rresponse;
2476 sprintf (buf, "0x0 %c 0x%s 0x%s %s", cmd, paddr_nz (addr), paddr_nz (mask),
2479 sprintf (buf, "0x0 %c 0x%s", cmd, paddr_nz (addr));
2481 mips_send_packet (buf, 1);
2483 len = mips_receive_packet (buf, 1, mips_receive_wait);
2486 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
2490 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2494 /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2495 Cogent returns "0x0 b 0xffffffff 0x16\000": */
2496 if (mips_monitor == MON_DDB)
2497 rresponse = rerrflg;
2498 if (rresponse != 22) /* invalid argument */
2499 fprintf_unfiltered (stderr, "common_breakpoint (0x%s): Got error: 0x%x\n",
2500 paddr_nz (addr), rresponse);
2508 send_srec (srec, len, addr)
2517 SERIAL_WRITE (mips_desc, srec, len);
2519 ch = mips_readchar (2);
2523 case SERIAL_TIMEOUT:
2524 error ("Timeout during download.");
2528 case 0x15: /* NACK */
2529 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
2532 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2537 /* Download a binary file by converting it to S records. */
2540 mips_load_srec (args)
2545 char *buffer, srec[1024];
2547 unsigned int srec_frame = 200;
2549 static int hashmark = 1;
2551 buffer = alloca (srec_frame * 2 + 256);
2553 abfd = bfd_openr (args, 0);
2556 printf_filtered ("Unable to open file %s\n", args);
2560 if (bfd_check_format (abfd, bfd_object) == 0)
2562 printf_filtered ("File is not an object file\n");
2566 /* This actually causes a download in the IDT binary format: */
2567 mips_send_command (LOAD_CMD, 0);
2569 for (s = abfd->sections; s; s = s->next)
2571 if (s->flags & SEC_LOAD)
2573 unsigned int numbytes;
2575 /* FIXME! vma too small?? */
2576 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
2577 s->vma + s->_raw_size);
2578 gdb_flush (gdb_stdout);
2580 for (i = 0; i < s->_raw_size; i += numbytes)
2582 numbytes = min (srec_frame, s->_raw_size - i);
2584 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2586 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2587 send_srec (srec, reclen, s->vma + i);
2591 putchar_unfiltered ('#');
2592 gdb_flush (gdb_stdout);
2595 } /* Per-packet (or S-record) loop */
2597 putchar_unfiltered ('\n');
2598 } /* Loadable sections */
2601 putchar_unfiltered ('\n');
2603 /* Write a type 7 terminator record. no data for a type 7, and there
2604 is no data, so len is 0. */
2606 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2608 send_srec (srec, reclen, abfd->start_address);
2610 SERIAL_FLUSH_INPUT (mips_desc);
2614 * mips_make_srec -- make an srecord. This writes each line, one at a
2615 * time, each with it's own header and trailer line.
2616 * An srecord looks like this:
2618 * byte count-+ address
2619 * start ---+ | | data +- checksum
2621 * S01000006F6B692D746573742E73726563E4
2622 * S315000448600000000000000000FC00005900000000E9
2623 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2624 * S30B0004485A0000000000004E
2627 * S<type><length><address><data><checksum>
2631 * is the number of bytes following upto the checksum. Note that
2632 * this is not the number of chars following, since it takes two
2633 * chars to represent a byte.
2637 * 1) two byte address data record
2638 * 2) three byte address data record
2639 * 3) four byte address data record
2640 * 7) four byte address termination record
2641 * 8) three byte address termination record
2642 * 9) two byte address termination record
2645 * is the start address of the data following, or in the case of
2646 * a termination record, the start address of the image
2650 * is the sum of all the raw byte data in the record, from the length
2651 * upwards, modulo 256 and subtracted from 255.
2653 * This routine returns the length of the S-record.
2658 mips_make_srec (buf, type, memaddr, myaddr, len)
2662 unsigned char *myaddr;
2665 unsigned char checksum;
2668 /* Create the header for the srec. addr_size is the number of bytes in the address,
2669 and 1 is the number of bytes in the count. */
2671 /* FIXME!! bigger buf required for 64-bit! */
2674 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2675 /* This assumes S3 style downloads (4byte addresses). There should
2676 probably be a check, or the code changed to make it more
2678 buf[3] = memaddr >> 24;
2679 buf[4] = memaddr >> 16;
2680 buf[5] = memaddr >> 8;
2682 memcpy (&buf[7], myaddr, len);
2684 /* Note that the checksum is calculated on the raw data, not the
2685 hexified data. It includes the length, address and the data
2686 portions of the packet. */
2688 buf += 2; /* Point at length byte */
2689 for (i = 0; i < len + 4 + 1; i++)
2697 /* The following manifest controls whether we enable the simple flow
2698 control support provided by the monitor. If enabled the code will
2699 wait for an affirmative ACK between transmitting packets. */
2700 #define DOETXACK (1)
2702 /* The PMON fast-download uses an encoded packet format constructed of
2703 3byte data packets (encoded as 4 printable ASCII characters), and
2704 escape sequences (preceded by a '/'):
2707 'C' compare checksum (12bit value, not included in checksum calculation)
2708 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary
2709 'Z' zero fill multiple of 3bytes
2710 'B' byte (12bit encoded value, of 8bit data)
2711 'A' address (36bit encoded value)
2712 'E' define entry as original address, and exit load
2714 The packets are processed in 4 character chunks, so the escape
2715 sequences that do not have any data (or variable length data)
2716 should be padded to a 4 character boundary. The decoder will give
2717 an error if the complete message block size is not a multiple of
2718 4bytes (size of record).
2720 The encoding of numbers is done in 6bit fields. The 6bit value is
2721 used to index into this string to get the specific character
2722 encoding for the value: */
2723 static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
2725 /* Convert the number of bits required into an encoded number, 6bits
2726 at a time (range 0..63). Keep a checksum if required (passed
2727 pointer non-NULL). The function returns the number of encoded
2728 characters written into the buffer. */
2730 pmon_makeb64 (v, p, n, chksum)
2736 int count = (n / 6);
2738 if ((n % 12) != 0) {
2739 fprintf_unfiltered(stderr,"Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n",n,(n == 1)?"":"s");
2743 fprintf_unfiltered(stderr,"Fast encoding cannot process more than 36bits at the moment: %dbits\n",n);
2747 /* Deal with the checksum: */
2748 if (chksum != NULL) {
2750 case 36: *chksum += ((v >> 24) & 0xFFF);
2751 case 24: *chksum += ((v >> 12) & 0xFFF);
2752 case 12: *chksum += ((v >> 0) & 0xFFF);
2758 *p++ = encoding[(v >> n) & 0x3F];
2764 /* Shorthand function (that could be in-lined) to output the zero-fill
2765 escape sequence into the data stream. */
2767 pmon_zeroset (recsize, buff, amount, chksum)
2771 unsigned int *chksum;
2775 sprintf(*buff,"/Z");
2776 count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
2777 *buff += (count + 2);
2779 return(recsize + count + 2);
2783 pmon_checkset (recsize, buff, value)
2790 /* Add the checksum (without updating the value): */
2791 sprintf (*buff, "/C");
2792 count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
2793 *buff += (count + 2);
2794 sprintf (*buff, "\n");
2795 *buff += 2; /* include zero terminator */
2796 /* Forcing a checksum validation clears the sum: */
2798 return(recsize + count + 3);
2801 /* Amount of padding we leave after at the end of the output buffer,
2802 for the checksum and line termination characters: */
2803 #define CHECKSIZE (4 + 4 + 4 + 2)
2804 /* zero-fill, checksum, transfer end and line termination space. */
2806 /* The amount of binary data loaded from the object file in a single
2808 #define BINCHUNK (1024)
2810 /* Maximum line of data accepted by the monitor: */
2811 #define MAXRECSIZE (550)
2812 /* NOTE: This constant depends on the monitor being used. This value
2813 is for PMON 5.x on the Cogent Vr4300 board. */
2816 pmon_make_fastrec (outbuf, inbuf, inptr, inamount, recsize, csum, zerofill)
2818 unsigned char *inbuf;
2823 unsigned int *zerofill;
2828 /* This is a simple check to ensure that our data will fit within
2829 the maximum allowable record size. Each record output is 4bytes
2830 in length. We must allow space for a pending zero fill command,
2831 the record, and a checksum record. */
2832 while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0)) {
2833 /* Process the binary data: */
2834 if ((inamount - *inptr) < 3) {
2836 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2838 count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
2840 *recsize += (2 + count);
2843 unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
2844 /* Simple check for zero data. TODO: A better check would be
2845 to check the last, and then the middle byte for being zero
2846 (if the first byte is not). We could then check for
2847 following runs of zeros, and if above a certain size it is
2848 worth the 4 or 8 character hit of the byte insertions used
2849 to pad to the start of the zeroes. NOTE: This also depends
2850 on the alignment at the end of the zero run. */
2851 if (value == 0x00000000) {
2853 if (*zerofill == 0xFFF) /* 12bit counter */
2854 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2857 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2858 count = pmon_makeb64 (value, p, 24, csum);
2871 pmon_check_ack(mesg)
2874 #if defined(DOETXACK)
2879 c = SERIAL_READCHAR (udp_in_use ? udp_desc : mips_desc, 2);
2880 if ((c == SERIAL_TIMEOUT) || (c != 0x06))
2882 fprintf_unfiltered (gdb_stderr,
2883 "Failed to receive valid ACK for %s\n", mesg);
2884 return(-1); /* terminate the download */
2887 #endif /* DOETXACK */
2891 /* pmon_download - Send a sequence of characters to the PMON download port,
2892 which is either a serial port or a UDP socket. */
2895 pmon_start_download ()
2899 /* Create the temporary download file. */
2900 if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
2901 perror_with_name (tftp_localname);
2905 mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
2906 mips_expect ("Downloading from ");
2907 mips_expect (udp_in_use ? "udp" : "tty0");
2908 mips_expect (", ^C to abort\r\n");
2913 mips_expect_download (char *string)
2915 if (!mips_expect (string))
2917 fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
2919 remove (tftp_localname); /* Remove temporary file */
2927 pmon_end_download (final, bintotal)
2931 char hexnumber[9]; /* includes '\0' space */
2935 static char *load_cmd_prefix = "load -b -s ";
2939 /* Close off the temporary file containing the load data. */
2943 /* Make the temporary file readable by the world. */
2944 if (stat (tftp_localname, &stbuf) == 0)
2945 chmod (tftp_localname, stbuf.st_mode | S_IROTH);
2947 /* Must reinitialize the board to prevent PMON from crashing. */
2948 mips_send_command ("initEther\r", -1);
2950 /* Send the load command. */
2951 cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
2952 strcpy (cmd, load_cmd_prefix);
2953 strcat (cmd, tftp_name);
2955 mips_send_command (cmd, 0);
2957 if (!mips_expect_download ("Downloading from "))
2959 if (!mips_expect_download (tftp_name))
2961 if (!mips_expect_download (", ^C to abort\r\n"))
2965 /* Wait for the stuff that PMON prints after the load has completed.
2966 The timeout value for use in the tftp case (15 seconds) was picked
2967 arbitrarily but might be too small for really large downloads. FIXME. */
2968 mips_expect_timeout ("Entry Address = ", tftp_in_use ? 15 : 2);
2969 sprintf (hexnumber,"%x",final);
2970 mips_expect (hexnumber);
2971 mips_expect ("\r\n");
2972 pmon_check_ack ("termination");
2973 mips_expect ("\r\ntotal = 0x");
2974 sprintf (hexnumber,"%x",bintotal);
2975 mips_expect (hexnumber);
2976 if (!mips_expect_download (" bytes\r\n"))
2980 remove (tftp_localname); /* Remove temporary file */
2984 pmon_download (buffer, length)
2989 fwrite (buffer, 1, length, tftp_file);
2991 SERIAL_WRITE (udp_in_use ? udp_desc : mips_desc, buffer, length);
2995 pmon_load_fast (file)
3000 unsigned char *binbuf;
3003 unsigned int csum = 0;
3004 int hashmark = !tftp_in_use;
3009 buffer = (char *)xmalloc(MAXRECSIZE + 1);
3010 binbuf = (unsigned char *)xmalloc(BINCHUNK);
3012 abfd = bfd_openr(file,0);
3015 printf_filtered ("Unable to open file %s\n",file);
3019 if (bfd_check_format(abfd,bfd_object) == 0)
3021 printf_filtered("File is not an object file\n");
3025 /* Setup the required download state: */
3026 mips_send_command ("set dlproto etxack\r", -1);
3027 mips_send_command ("set dlecho off\r", -1);
3028 /* NOTE: We get a "cannot set variable" message if the variable is
3029 already defined to have the argument we give. The code doesn't
3030 care, since it just scans to the next prompt anyway. */
3031 /* Start the download: */
3032 pmon_start_download();
3034 /* Zero the checksum */
3035 sprintf(buffer,"/Kxx\n");
3036 reclen = strlen(buffer);
3037 pmon_download (buffer, reclen);
3038 finished = pmon_check_ack("/Kxx");
3040 for (s = abfd->sections; s && !finished; s = s->next)
3041 if (s->flags & SEC_LOAD) /* only deal with loadable sections */
3043 bintotal += s->_raw_size;
3044 final = (s->vma + s->_raw_size);
3046 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, (unsigned int)s->vma,
3047 (unsigned int)(s->vma + s->_raw_size));
3048 gdb_flush (gdb_stdout);
3050 /* Output the starting address */
3051 sprintf(buffer,"/A");
3052 reclen = pmon_makeb64(s->vma,&buffer[2],36,&csum);
3053 buffer[2 + reclen] = '\n';
3054 buffer[3 + reclen] = '\0';
3055 reclen += 3; /* for the initial escape code and carriage return */
3056 pmon_download (buffer, reclen);
3057 finished = pmon_check_ack("/A");
3061 unsigned int binamount;
3062 unsigned int zerofill = 0;
3068 for (i = 0; ((i < s->_raw_size) && !finished); i += binamount) {
3071 binamount = min (BINCHUNK, s->_raw_size - i);
3073 bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3075 /* This keeps a rolling checksum, until we decide to output
3077 for (; ((binamount - binptr) > 0);) {
3078 pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3079 if (reclen >= (MAXRECSIZE - CHECKSIZE)) {
3080 reclen = pmon_checkset (reclen, &bp, &csum);
3081 pmon_download (buffer, reclen);
3082 finished = pmon_check_ack("data record");
3084 zerofill = 0; /* do not transmit pending zerofills */
3089 putchar_unfiltered ('#');
3090 gdb_flush (gdb_stdout);
3094 reclen = 0; /* buffer processed */
3099 /* Ensure no out-standing zerofill requests: */
3101 reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3103 /* and then flush the line: */
3105 reclen = pmon_checkset (reclen, &bp, &csum);
3106 /* Currently pmon_checkset outputs the line terminator by
3107 default, so we write out the buffer so far: */
3108 pmon_download (buffer, reclen);
3109 finished = pmon_check_ack("record remnant");
3113 putchar_unfiltered ('\n');
3116 /* Terminate the transfer. We know that we have an empty output
3117 buffer at this point. */
3118 sprintf (buffer, "/E/E\n"); /* include dummy padding characters */
3119 reclen = strlen (buffer);
3120 pmon_download (buffer, reclen);
3122 if (finished) { /* Ignore the termination message: */
3123 SERIAL_FLUSH_INPUT (udp_in_use ? udp_desc : mips_desc);
3124 } else { /* Deal with termination message: */
3125 pmon_end_download (final, bintotal);
3131 /* mips_load -- download a file. */
3134 mips_load (file, from_tty)
3138 /* Get the board out of remote debugging mode. */
3139 if (mips_exit_debug ())
3140 error ("mips_load: Couldn't get into monitor mode.");
3142 if (mips_monitor == MON_PMON || mips_monitor == MON_DDB)
3143 pmon_load_fast (file);
3145 mips_load_srec (file);
3149 /* Finally, make the PC point at the start address */
3150 if (mips_monitor == MON_DDB)
3152 /* Work around problem where DDB monitor does not update the
3153 PC after a load. The following ensures that the write_pc()
3154 WILL update the PC value: */
3155 register_valid[PC_REGNUM] = 0;
3158 write_pc (bfd_get_start_address (exec_bfd));
3160 inferior_pid = 0; /* No process now */
3162 /* This is necessary because many things were based on the PC at the time that
3163 we attached to the monitor, which is no longer valid now that we have loaded
3164 new code (and just changed the PC). Another way to do this might be to call
3165 normal_stop, except that the stack may not be valid, and things would get
3166 horribly confused... */
3168 clear_symtab_users ();
3171 /* The target vector. */
3173 struct target_ops mips_ops =
3175 "mips", /* to_shortname */
3176 "Remote MIPS debugging over serial line", /* to_longname */
3178 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
3179 The argument is the device it is connected to or, if it contains a colon,\n\
3180 HOST:PORT to access a board over a network", /* to_doc */
3181 mips_open, /* to_open */
3182 mips_close, /* to_close */
3183 NULL, /* to_attach */
3184 mips_detach, /* to_detach */
3185 mips_resume, /* to_resume */
3186 mips_wait, /* to_wait */
3187 mips_fetch_registers, /* to_fetch_registers */
3188 mips_store_registers, /* to_store_registers */
3189 mips_prepare_to_store, /* to_prepare_to_store */
3190 mips_xfer_memory, /* to_xfer_memory */
3191 mips_files_info, /* to_files_info */
3192 mips_insert_breakpoint, /* to_insert_breakpoint */
3193 mips_remove_breakpoint, /* to_remove_breakpoint */
3194 NULL, /* to_terminal_init */
3195 NULL, /* to_terminal_inferior */
3196 NULL, /* to_terminal_ours_for_output */
3197 NULL, /* to_terminal_ours */
3198 NULL, /* to_terminal_info */
3199 mips_kill, /* to_kill */
3200 mips_load, /* to_load */
3201 NULL, /* to_lookup_symbol */
3202 mips_create_inferior, /* to_create_inferior */
3203 mips_mourn_inferior, /* to_mourn_inferior */
3204 NULL, /* to_can_run */
3205 NULL, /* to_notice_signals */
3206 0, /* to_thread_alive */
3208 process_stratum, /* to_stratum */
3210 1, /* to_has_all_memory */
3211 1, /* to_has_memory */
3212 1, /* to_has_stack */
3213 1, /* to_has_registers */
3214 1, /* to_has_execution */
3215 NULL, /* sections */
3216 NULL, /* sections_end */
3217 OPS_MAGIC /* to_magic */
3220 /* An alternative target vector: */
3221 struct target_ops pmon_ops =
3223 "pmon", /* to_shortname */
3224 "Remote MIPS debugging over serial line", /* to_longname */
3226 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3227 line. The argument is the device it is connected to or, if it contains a\n\
3228 colon, HOST:PORT to access a board over a network", /* to_doc */
3229 pmon_open, /* to_open */
3230 mips_close, /* to_close */
3231 NULL, /* to_attach */
3232 mips_detach, /* to_detach */
3233 mips_resume, /* to_resume */
3234 pmon_wait, /* to_wait */
3235 mips_fetch_registers, /* to_fetch_registers */
3236 mips_store_registers, /* to_store_registers */
3237 mips_prepare_to_store, /* to_prepare_to_store */
3238 mips_xfer_memory, /* to_xfer_memory */
3239 mips_files_info, /* to_files_info */
3240 mips_insert_breakpoint, /* to_insert_breakpoint */
3241 mips_remove_breakpoint, /* to_remove_breakpoint */
3242 NULL, /* to_terminal_init */
3243 NULL, /* to_terminal_inferior */
3244 NULL, /* to_terminal_ours_for_output */
3245 NULL, /* to_terminal_ours */
3246 NULL, /* to_terminal_info */
3247 mips_kill, /* to_kill */
3248 mips_load, /* to_load */
3249 NULL, /* to_lookup_symbol */
3250 mips_create_inferior, /* to_create_inferior */
3251 mips_mourn_inferior, /* to_mourn_inferior */
3252 NULL, /* to_can_run */
3253 NULL, /* to_notice_signals */
3254 0, /* to_thread_alive */
3256 process_stratum, /* to_stratum */
3258 1, /* to_has_all_memory */
3259 1, /* to_has_memory */
3260 1, /* to_has_stack */
3261 1, /* to_has_registers */
3262 1, /* to_has_execution */
3263 NULL, /* sections */
3264 NULL, /* sections_end */
3265 OPS_MAGIC /* to_magic */
3268 /* Another alternative target vector. This is a PMON system, but with
3269 a different monitor prompt, aswell as some other operational
3271 struct target_ops ddb_ops =
3273 "ddb", /* to_shortname */
3274 "Remote MIPS debugging over serial line", /* to_longname */
3276 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3277 line. The first argument is the device it is connected to or, if it contains\n\
3278 a colon, HOST:PORT to access a board over a network. The optional second\n\
3279 parameter is the temporary file in the form HOST:FILENAME to be used for\n\
3280 TFTP downloads to the board. The optional third parameter is the local\n\
3281 of the TFTP temporary file, if it differs from the filename seen by the board",
3283 ddb_open, /* to_open */
3284 mips_close, /* to_close */
3285 NULL, /* to_attach */
3286 mips_detach, /* to_detach */
3287 mips_resume, /* to_resume */
3288 pmon_wait, /* to_wait */
3289 mips_fetch_registers, /* to_fetch_registers */
3290 mips_store_registers, /* to_store_registers */
3291 mips_prepare_to_store, /* to_prepare_to_store */
3292 mips_xfer_memory, /* to_xfer_memory */
3293 mips_files_info, /* to_files_info */
3294 mips_insert_breakpoint, /* to_insert_breakpoint */
3295 mips_remove_breakpoint, /* to_remove_breakpoint */
3296 NULL, /* to_terminal_init */
3297 NULL, /* to_terminal_inferior */
3298 NULL, /* to_terminal_ours_for_output */
3299 NULL, /* to_terminal_ours */
3300 NULL, /* to_terminal_info */
3301 mips_kill, /* to_kill */
3302 mips_load, /* to_load */
3303 NULL, /* to_lookup_symbol */
3304 mips_create_inferior, /* to_create_inferior */
3305 mips_mourn_inferior, /* to_mourn_inferior */
3306 NULL, /* to_can_run */
3307 NULL, /* to_notice_signals */
3308 0, /* to_thread_alive */
3310 process_stratum, /* to_stratum */
3312 1, /* to_has_all_memory */
3313 1, /* to_has_memory */
3314 1, /* to_has_stack */
3315 1, /* to_has_registers */
3316 1, /* to_has_execution */
3317 NULL, /* sections */
3318 NULL, /* sections_end */
3319 OPS_MAGIC /* to_magic */
3323 _initialize_remote_mips ()
3325 add_target (&mips_ops);
3326 add_target (&pmon_ops);
3327 add_target (&ddb_ops);
3330 add_set_cmd ("timeout", no_class, var_zinteger,
3331 (char *) &mips_receive_wait,
3332 "Set timeout in seconds for remote MIPS serial I/O.",
3337 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3338 (char *) &mips_retransmit_wait,
3339 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3340 This is the number of seconds to wait for an acknowledgement to a packet\n\
3341 before resending the packet.", &setlist),
3345 add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3346 (char *) &mips_syn_garbage,
3347 "Set the maximum number of characters to ignore when scanning for a SYN.\n\
3348 This is the maximum number of characters GDB will ignore when trying to\n\
3349 synchronize with the remote system. A value of -1 means that there is no limit\n\
3350 (Note that these characters are printed out even though they are ignored.)",