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 /* Microsoft C's stat.h doesn't define all the POSIX file modes. */
45 #define S_IROTH S_IREAD
48 extern void mips_set_processor_type_command PARAMS ((char *, int));
51 /* Prototypes for local functions. */
53 static int mips_readchar PARAMS ((int timeout));
55 static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
56 int ch, int timeout));
58 static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
59 int *pch, int timeout));
61 static int mips_cksum PARAMS ((const unsigned char *hdr,
62 const unsigned char *data,
65 static void mips_send_packet PARAMS ((const char *s, int get_ack));
67 static void mips_send_command PARAMS ((const char *cmd, int prompt));
69 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
72 static CORE_ADDR mips_request PARAMS ((int cmd, CORE_ADDR addr,
73 CORE_ADDR data, int *perr, int timeout,
76 static void mips_initialize PARAMS ((void));
78 static void mips_open PARAMS ((char *name, int from_tty));
80 static void pmon_open PARAMS ((char *name, int from_tty));
82 static void ddb_open PARAMS ((char *name, int from_tty));
84 static void lsi_open PARAMS ((char *name, int from_tty));
86 static void mips_close PARAMS ((int quitting));
88 static void mips_detach PARAMS ((char *args, int from_tty));
90 static void mips_resume PARAMS ((int pid, int step,
91 enum target_signal siggnal));
93 static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
95 static int pmon_wait PARAMS ((int pid, struct target_waitstatus *status));
97 static int mips_map_regno PARAMS ((int regno));
99 static void mips_fetch_registers PARAMS ((int regno));
101 static void mips_prepare_to_store PARAMS ((void));
103 static void mips_store_registers PARAMS ((int regno));
105 static unsigned int mips_fetch_word PARAMS ((CORE_ADDR addr));
107 static int mips_store_word PARAMS ((CORE_ADDR addr, unsigned int value,
108 char *old_contents));
110 static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
111 int write, struct target_ops *ignore));
113 static void mips_files_info PARAMS ((struct target_ops *ignore));
115 static void mips_create_inferior PARAMS ((char *execfile, char *args,
118 static void mips_mourn_inferior PARAMS ((void));
120 static int pmon_makeb64 PARAMS ((unsigned long v, char *p, int n, int *chksum));
122 static int pmon_zeroset PARAMS ((int recsize, char **buff, int *amount,
123 unsigned int *chksum));
125 static int pmon_checkset PARAMS ((int recsize, char **buff, int *value));
127 static void pmon_make_fastrec PARAMS ((char **outbuf, unsigned char *inbuf,
128 int *inptr, int inamount, int *recsize,
129 unsigned int *csum, unsigned int *zerofill));
131 static int pmon_check_ack PARAMS ((char *mesg));
133 static void pmon_start_download PARAMS ((void));
135 static void pmon_end_download PARAMS ((int final, int bintotal));
137 static void pmon_download PARAMS ((char *buffer, int length));
139 static void pmon_load_fast PARAMS ((char *file));
141 static void mips_load PARAMS ((char *file, int from_tty));
143 static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
144 unsigned char *myaddr, int len));
146 static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask,
149 static void common_open PARAMS ((struct target_ops *ops, char *name,
151 /* Forward declarations. */
152 extern struct target_ops mips_ops;
153 extern struct target_ops pmon_ops;
154 extern struct target_ops ddb_ops;
156 /* The MIPS remote debugging interface is built on top of a simple
157 packet protocol. Each packet is organized as follows:
159 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
160 may not appear anywhere else in the packet. Any time a SYN is
161 seen, a new packet should be assumed to have begun.
164 This byte contains the upper five bits of the logical length
165 of the data section, plus a single bit indicating whether this
166 is a data packet or an acknowledgement. The documentation
167 indicates that this bit is 1 for a data packet, but the actual
168 board uses 1 for an acknowledgement. The value of the byte is
169 0x40 + (ack ? 0x20 : 0) + (len >> 6)
170 (we always have 0 <= len < 1024). Acknowledgement packets do
171 not carry data, and must have a data length of 0.
173 LEN1 This byte contains the lower six bits of the logical length of
174 the data section. The value is
177 SEQ This byte contains the six bit sequence number of the packet.
180 An acknowlegment packet contains the sequence number of the
181 packet being acknowledged plus 1 modulo 64. Data packets are
182 transmitted in sequence. There may only be one outstanding
183 unacknowledged data packet at a time. The sequence numbers
184 are independent in each direction. If an acknowledgement for
185 the previous packet is received (i.e., an acknowledgement with
186 the sequence number of the packet just sent) the packet just
187 sent should be retransmitted. If no acknowledgement is
188 received within a timeout period, the packet should be
189 retransmitted. This has an unfortunate failure condition on a
190 high-latency line, as a delayed acknowledgement may lead to an
191 endless series of duplicate packets.
193 DATA The actual data bytes follow. The following characters are
194 escaped inline with DLE (ASCII 020, or ^P):
200 The additional DLE characters are not counted in the logical
201 length stored in the TYPE_LEN and LEN1 bytes.
206 These bytes contain an 18 bit checksum of the complete
207 contents of the packet excluding the SEQ byte and the
208 CSUM[123] bytes. The checksum is simply the twos complement
209 addition of all the bytes treated as unsigned characters. The
210 values of the checksum bytes are:
211 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
212 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
213 CSUM3: 0x40 + (cksum & 0x3f)
215 It happens that the MIPS remote debugging protocol always
216 communicates with ASCII strings. Because of this, this
217 implementation doesn't bother to handle the DLE quoting mechanism,
218 since it will never be required. */
220 /* The SYN character which starts each packet. */
223 /* The 0x40 used to offset each packet (this value ensures that all of
224 the header and trailer bytes, other than SYN, are printable ASCII
226 #define HDR_OFFSET 0x40
228 /* The indices of the bytes in the packet header. */
229 #define HDR_INDX_SYN 0
230 #define HDR_INDX_TYPE_LEN 1
231 #define HDR_INDX_LEN1 2
232 #define HDR_INDX_SEQ 3
235 /* The data/ack bit in the TYPE_LEN header byte. */
236 #define TYPE_LEN_DA_BIT 0x20
237 #define TYPE_LEN_DATA 0
238 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
240 /* How to compute the header bytes. */
241 #define HDR_SET_SYN(data, len, seq) (SYN)
242 #define HDR_SET_TYPE_LEN(data, len, seq) \
244 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
245 + (((len) >> 6) & 0x1f))
246 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
247 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
249 /* Check that a header byte is reasonable. */
250 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
252 /* Get data from the header. These macros evaluate their argument
254 #define HDR_IS_DATA(hdr) \
255 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
256 #define HDR_GET_LEN(hdr) \
257 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
258 #define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
260 /* The maximum data length. */
261 #define DATA_MAXLEN 1023
263 /* The trailer offset. */
264 #define TRLR_OFFSET HDR_OFFSET
266 /* The indices of the bytes in the packet trailer. */
267 #define TRLR_INDX_CSUM1 0
268 #define TRLR_INDX_CSUM2 1
269 #define TRLR_INDX_CSUM3 2
270 #define TRLR_LENGTH 3
272 /* How to compute the trailer bytes. */
273 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
274 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
275 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
277 /* Check that a trailer byte is reasonable. */
278 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
280 /* Get data from the trailer. This evaluates its argument multiple
282 #define TRLR_GET_CKSUM(trlr) \
283 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
284 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
285 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
287 /* The sequence number modulos. */
288 #define SEQ_MODULOS (64)
290 /* PMON commands to load from the serial port or UDP socket. */
291 #define LOAD_CMD "load -b -s tty0\r"
292 #define LOAD_CMD_UDP "load -b -s udp\r"
294 enum mips_monitor_type {
295 /* IDT/SIM monitor being used: */
297 /* PMON monitor being used: */
298 MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */
299 MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */
300 MON_LSI, /* 4.3.12 [EB,FP], LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
301 /* Last and unused value, for sizing vectors, etc. */
304 static enum mips_monitor_type mips_monitor = MON_LAST;
306 /* The default monitor prompt text: */
307 static char *mips_monitor_prompt = TARGET_MONITOR_PROMPT;
308 /* For the Cogent PMON world this is still not ideal. The default
309 prompt is "PMON> ", unfortunately the user can change the prompt
310 and the new prompt will survive over a power-cycle (EEPROM). This
311 means that the code should really force the monitor prompt to a
312 known value as the very first action, and that the
313 "mips_monitor_prompt" support is not needed... since the prompt
314 could be explicitly set to TARGET_MONITOR_PROMPT (even though it
315 may be the prompt for a different monitor). However, this will
316 require changing the mips_initialize reset sequence. (TODO) */
318 /* Set to 1 if the target is open. */
319 static int mips_is_open;
321 /* Currently active target description (if mips_is_open == 1) */
322 static struct target_ops *current_ops;
324 /* Set to 1 while the connection is being initialized. */
325 static int mips_initializing;
327 /* Set to 1 while the connection is being brought down. */
328 static int mips_exiting;
330 /* The next sequence number to send. */
331 static unsigned int mips_send_seq;
333 /* The next sequence number we expect to receive. */
334 static unsigned int mips_receive_seq;
336 /* The time to wait before retransmitting a packet, in seconds. */
337 static int mips_retransmit_wait = 3;
339 /* The number of times to try retransmitting a packet before giving up. */
340 static int mips_send_retries = 10;
342 /* The number of garbage characters to accept when looking for an
343 SYN for the next packet. */
344 static int mips_syn_garbage = 1050;
346 /* The time to wait for a packet, in seconds. */
347 static int mips_receive_wait = 5;
349 /* Set if we have sent a packet to the board but have not yet received
351 static int mips_need_reply = 0;
353 /* Handle used to access serial I/O stream. */
354 static serial_t mips_desc;
356 /* UDP handle used to download files to target. */
357 static serial_t udp_desc;
358 static int udp_in_use;
360 /* TFTP filename used to download files to DDB board, in the form
362 static char *tftp_name; /* host:filename */
363 static char *tftp_localname; /* filename portion of above */
364 static int tftp_in_use;
365 static FILE *tftp_file;
367 /* Counts the number of times the user tried to interrupt the target (usually
369 static int interrupt_count;
371 /* If non-zero, means that the target is running. */
372 static int mips_wait_flag = 0;
374 /* If non-zero, monitor supports breakpoint commands. */
375 static monitor_supports_breakpoints = 0;
377 /* Data cache header. */
379 #if 0 /* not used (yet?) */
380 static DCACHE *mips_dcache;
383 /* Non-zero means that we've just hit a read or write watchpoint */
384 static int hit_watchpoint;
390 SERIAL_CLOSE (mips_desc);
394 SERIAL_CLOSE (udp_desc);
400 /* Handle low-level error that we can't recover from. Note that just
401 error()ing out from target_wait or some such low-level place will cause
402 all hell to break loose--the rest of GDB will tend to get left in an
403 inconsistent state. */
406 #ifdef ANSI_PROTOTYPES
407 mips_error (char *string, ...)
409 mips_error (va_alist)
415 #ifdef ANSI_PROTOTYPES
416 va_start (args, string);
420 string = va_arg (args, char *);
423 target_terminal_ours ();
424 wrap_here(""); /* Force out any buffered output */
425 gdb_flush (gdb_stdout);
427 fprintf_filtered (gdb_stderr, error_pre_print);
428 vfprintf_filtered (gdb_stderr, string, args);
429 fprintf_filtered (gdb_stderr, "\n");
431 gdb_flush (gdb_stderr);
433 /* Clean up in such a way that mips_close won't try to talk to the
434 board (it almost surely won't work since we weren't able to talk to
438 printf_unfiltered ("Ending remote MIPS debugging.\n");
439 target_mourn_inferior ();
441 return_to_top_level (RETURN_ERROR);
444 /* putc_readable - print a character, displaying non-printable chars in
445 ^x notation or in hex. */
452 putchar_unfiltered ('\n');
454 printf_unfiltered ("\\r");
455 else if (ch < 0x20) /* ASCII control character */
456 printf_unfiltered ("^%c", ch + '@');
457 else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */
458 printf_unfiltered ("[%02x]", ch & 0xff);
460 putchar_unfiltered (ch);
464 /* puts_readable - print a string, displaying non-printable chars in
465 ^x notation or in hex. */
468 puts_readable (string)
473 while ((c = *string++) != '\0')
478 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
479 timed out. TIMEOUT specifies timeout value in seconds.
483 mips_expect_timeout (string, timeout)
491 printf_unfiltered ("Expected \"");
492 puts_readable (string);
493 printf_unfiltered ("\", got \"");
501 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
502 were waiting for the mips_monitor_prompt... */
504 c = SERIAL_READCHAR (mips_desc, timeout);
506 if (c == SERIAL_TIMEOUT)
509 printf_unfiltered ("\": FAIL\n");
522 printf_unfiltered ("\": OK\n");
535 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
536 timed out. The timeout value is hard-coded to 2 seconds. Use
537 mips_expect_timeout if a different timeout value is needed.
544 return mips_expect_timeout (string, 2);
547 /* Read the required number of characters into the given buffer (which
548 is assumed to be large enough). The only failure is a timeout. */
550 mips_getstring (string, n)
560 c = SERIAL_READCHAR (mips_desc, 2);
562 if (c == SERIAL_TIMEOUT) {
563 fprintf_unfiltered (stderr, "Failed to read %d characters from target (TIMEOUT)\n", n);
574 /* Read a character from the remote, aborting on error. Returns
575 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
576 returns). FIXME: If we see the string mips_monitor_prompt from
577 the board, then we are debugging on the main console port, and we
578 have somehow dropped out of remote debugging mode. In this case,
579 we automatically go back in to remote debugging mode. This is a
580 hack, put in because I can't find any way for a program running on
581 the remote board to terminate without also ending remote debugging
582 mode. I assume users won't have any trouble with this; for one
583 thing, the IDT documentation generally assumes that the remote
584 debugging port is not the console port. This is, however, very
585 convenient for DejaGnu when you only have one connected serial
589 mips_readchar (timeout)
593 static int state = 0;
594 static int mips_monitor_prompt_len = -1;
596 /* NASTY, since we assume that the prompt does not change after the
597 first mips_readchar call: */
598 if (mips_monitor_prompt_len == -1)
599 mips_monitor_prompt_len = strlen(mips_monitor_prompt);
601 #ifdef MAINTENANCE_CMDS
606 if (i == -1 && watchdog > 0)
611 if (state == mips_monitor_prompt_len)
613 ch = SERIAL_READCHAR (mips_desc, timeout);
614 #ifdef MAINTENANCE_CMDS
615 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
617 target_mourn_inferior ();
618 error ("Watchdog has expired. Target detached.\n");
621 if (ch == SERIAL_EOF)
622 mips_error ("End of file from remote");
623 if (ch == SERIAL_ERROR)
624 mips_error ("Error reading from remote: %s", safe_strerror (errno));
625 if (remote_debug > 1)
627 /* Don't use _filtered; we can't deal with a QUIT out of
628 target_wait, and I think this might be called from there. */
629 if (ch != SERIAL_TIMEOUT)
630 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
632 printf_unfiltered ("Timed out in read\n");
635 /* If we have seen mips_monitor_prompt and we either time out, or
636 we see a @ (which was echoed from a packet we sent), reset the
637 board as described above. The first character in a packet after
638 the SYN (which is not echoed) is always an @ unless the packet is
639 more than 64 characters long, which ours never are. */
640 if ((ch == SERIAL_TIMEOUT || ch == '@')
641 && state == mips_monitor_prompt_len
642 && ! mips_initializing
645 if (remote_debug > 0)
646 /* Don't use _filtered; we can't deal with a QUIT out of
647 target_wait, and I think this might be called from there. */
648 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
655 /* At this point, about the only thing we can do is abort the command
656 in progress and get back to command level as quickly as possible. */
658 error ("Remote board reset, debug protocol re-initialized.");
661 if (ch == mips_monitor_prompt[state])
669 /* Get a packet header, putting the data in the supplied buffer.
670 PGARBAGE is a pointer to the number of garbage characters received
671 so far. CH is the last character received. Returns 0 for success,
672 or -1 for timeout. */
675 mips_receive_header (hdr, pgarbage, ch, timeout)
685 /* Wait for a SYN. mips_syn_garbage is intended to prevent
686 sitting here indefinitely if the board sends us one garbage
687 character per second. ch may already have a value from the
688 last time through the loop. */
691 ch = mips_readchar (timeout);
692 if (ch == SERIAL_TIMEOUT)
696 /* Printing the character here lets the user of gdb see
697 what the program is outputting, if the debugging is
698 being done on the console port. Don't use _filtered;
699 we can't deal with a QUIT out of target_wait. */
700 if (! mips_initializing || remote_debug > 0)
703 gdb_flush (gdb_stdout);
707 if (mips_syn_garbage > 0
708 && *pgarbage > mips_syn_garbage)
709 mips_error ("Debug protocol failure: more than %d characters before a sync.",
714 /* Get the packet header following the SYN. */
715 for (i = 1; i < HDR_LENGTH; i++)
717 ch = mips_readchar (timeout);
718 if (ch == SERIAL_TIMEOUT)
720 /* Make sure this is a header byte. */
721 if (ch == SYN || ! HDR_CHECK (ch))
727 /* If we got the complete header, we can return. Otherwise we
728 loop around and keep looking for SYN. */
734 /* Get a packet header, putting the data in the supplied buffer.
735 PGARBAGE is a pointer to the number of garbage characters received
736 so far. The last character read is returned in *PCH. Returns 0
737 for success, -1 for timeout, -2 for error. */
740 mips_receive_trailer (trlr, pgarbage, pch, timeout)
749 for (i = 0; i < TRLR_LENGTH; i++)
751 ch = mips_readchar (timeout);
753 if (ch == SERIAL_TIMEOUT)
755 if (! TRLR_CHECK (ch))
762 /* Get the checksum of a packet. HDR points to the packet header.
763 DATA points to the packet data. LEN is the length of DATA. */
766 mips_cksum (hdr, data, len)
767 const unsigned char *hdr;
768 const unsigned char *data;
771 register const unsigned char *p;
777 /* The initial SYN is not included in the checksum. */
791 /* Send a packet containing the given ASCII string. */
794 mips_send_packet (s, get_ack)
798 /* unsigned */ int len;
799 unsigned char *packet;
804 if (len > DATA_MAXLEN)
805 mips_error ("MIPS protocol data packet too long: %s", s);
807 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
809 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
810 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
811 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
812 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
814 memcpy (packet + HDR_LENGTH, s, len);
816 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
817 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
818 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
819 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
821 /* Increment the sequence number. This will set mips_send_seq to
822 the sequence number we expect in the acknowledgement. */
823 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
825 /* We can only have one outstanding data packet, so we just wait for
826 the acknowledgement here. Keep retransmitting the packet until
827 we get one, or until we've tried too many times. */
828 for (try = 0; try < mips_send_retries; try++)
833 if (remote_debug > 0)
835 /* Don't use _filtered; we can't deal with a QUIT out of
836 target_wait, and I think this might be called from there. */
837 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
838 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
841 if (SERIAL_WRITE (mips_desc, packet,
842 HDR_LENGTH + len + TRLR_LENGTH) != 0)
843 mips_error ("write to target failed: %s", safe_strerror (errno));
852 unsigned char hdr[HDR_LENGTH + 1];
853 unsigned char trlr[TRLR_LENGTH + 1];
857 /* Get the packet header. If we time out, resend the data
859 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
865 /* If we get a data packet, assume it is a duplicate and
866 ignore it. FIXME: If the acknowledgement is lost, this
867 data packet may be the packet the remote sends after the
869 if (HDR_IS_DATA (hdr)) {
872 /* Ignore any errors raised whilst attempting to ignore
875 len = HDR_GET_LEN (hdr);
877 for (i = 0; i < len; i++)
881 rch = mips_readchar (2);
887 if (rch == SERIAL_TIMEOUT)
889 /* ignore the character */
893 (void) mips_receive_trailer (trlr, &garbage, &ch, 2);
895 /* We don't bother checking the checksum, or providing an
896 ACK to the packet. */
900 /* If the length is not 0, this is a garbled packet. */
901 if (HDR_GET_LEN (hdr) != 0)
904 /* Get the packet trailer. */
905 err = mips_receive_trailer (trlr, &garbage, &ch,
906 mips_retransmit_wait);
908 /* If we timed out, resend the data packet. */
912 /* If we got a bad character, reread the header. */
916 /* If the checksum does not match the trailer checksum, this
917 is a bad packet; ignore it. */
918 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
919 != TRLR_GET_CKSUM (trlr))
922 if (remote_debug > 0)
924 hdr[HDR_LENGTH] = '\0';
925 trlr[TRLR_LENGTH] = '\0';
926 /* Don't use _filtered; we can't deal with a QUIT out of
927 target_wait, and I think this might be called from there. */
928 printf_unfiltered ("Got ack %d \"%s%s\"\n",
929 HDR_GET_SEQ (hdr), hdr + 1, trlr);
932 /* If this ack is for the current packet, we're done. */
933 seq = HDR_GET_SEQ (hdr);
934 if (seq == mips_send_seq)
937 /* If this ack is for the last packet, resend the current
939 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
942 /* Otherwise this is a bad ack; ignore it. Increment the
943 garbage count to ensure that we do not stay in this loop
949 mips_error ("Remote did not acknowledge packet");
952 /* Receive and acknowledge a packet, returning the data in BUFF (which
953 should be DATA_MAXLEN + 1 bytes). The protocol documentation
954 implies that only the sender retransmits packets, so this code just
955 waits silently for a packet. It returns the length of the received
956 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
957 don't print an error message and return -1. */
960 mips_receive_packet (buff, throw_error, timeout)
968 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
975 unsigned char hdr[HDR_LENGTH];
976 unsigned char trlr[TRLR_LENGTH];
980 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
983 mips_error ("Timed out waiting for remote packet");
990 /* An acknowledgement is probably a duplicate; ignore it. */
991 if (! HDR_IS_DATA (hdr))
993 len = HDR_GET_LEN (hdr);
994 /* Check if the length is valid for an ACK, we may aswell
995 try and read the remainder of the packet: */
998 /* Ignore the error condition, since we are going to
999 ignore the packet anyway. */
1000 (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
1002 /* Don't use _filtered; we can't deal with a QUIT out of
1003 target_wait, and I think this might be called from there. */
1004 if (remote_debug > 0)
1005 printf_unfiltered ("Ignoring unexpected ACK\n");
1009 len = HDR_GET_LEN (hdr);
1010 for (i = 0; i < len; i++)
1014 rch = mips_readchar (timeout);
1020 if (rch == SERIAL_TIMEOUT)
1023 mips_error ("Timed out waiting for remote packet");
1032 /* Don't use _filtered; we can't deal with a QUIT out of
1033 target_wait, and I think this might be called from there. */
1034 if (remote_debug > 0)
1035 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
1040 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
1044 mips_error ("Timed out waiting for packet");
1050 /* Don't use _filtered; we can't deal with a QUIT out of
1051 target_wait, and I think this might be called from there. */
1052 if (remote_debug > 0)
1053 printf_unfiltered ("Got SYN when wanted trailer\n");
1057 /* If this is the wrong sequence number, ignore it. */
1058 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
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 if (remote_debug > 0)
1063 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
1064 HDR_GET_SEQ (hdr), mips_receive_seq);
1068 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
1071 if (remote_debug > 0)
1072 /* Don't use _filtered; we can't deal with a QUIT out of
1073 target_wait, and I think this might be called from there. */
1074 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
1075 mips_cksum (hdr, buff, len),
1076 TRLR_GET_CKSUM (trlr));
1078 /* The checksum failed. Send an acknowledgement for the
1079 previous packet to tell the remote to resend the packet. */
1080 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1081 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1082 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1083 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1085 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1087 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1088 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1089 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1091 if (remote_debug > 0)
1093 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1094 /* Don't use _filtered; we can't deal with a QUIT out of
1095 target_wait, and I think this might be called from there. */
1096 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1100 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1103 mips_error ("write to target failed: %s", safe_strerror (errno));
1109 if (remote_debug > 0)
1112 /* Don't use _filtered; we can't deal with a QUIT out of
1113 target_wait, and I think this might be called from there. */
1114 printf_unfiltered ("Got packet \"%s\"\n", buff);
1117 /* We got the packet. Send an acknowledgement. */
1118 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1120 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1121 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1122 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1123 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1125 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1127 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1128 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1129 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1131 if (remote_debug > 0)
1133 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1134 /* Don't use _filtered; we can't deal with a QUIT out of
1135 target_wait, and I think this might be called from there. */
1136 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1140 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1143 mips_error ("write to target failed: %s", safe_strerror (errno));
1151 /* Optionally send a request to the remote system and optionally wait
1152 for the reply. This implements the remote debugging protocol,
1153 which is built on top of the packet protocol defined above. Each
1154 request has an ADDR argument and a DATA argument. The following
1155 requests are defined:
1157 \0 don't send a request; just wait for a reply
1158 i read word from instruction space at ADDR
1159 d read word from data space at ADDR
1160 I write DATA to instruction space at ADDR
1161 D write DATA to data space at ADDR
1162 r read register number ADDR
1163 R set register number ADDR to value DATA
1164 c continue execution (if ADDR != 1, set pc to ADDR)
1165 s single step (if ADDR != 1, set pc to ADDR)
1167 The read requests return the value requested. The write requests
1168 return the previous value in the changed location. The execution
1169 requests return a UNIX wait value (the approximate signal which
1170 caused execution to stop is in the upper eight bits).
1172 If PERR is not NULL, this function waits for a reply. If an error
1173 occurs, it sets *PERR to 1 and sets errno according to what the
1174 target board reports. */
1177 mips_request (cmd, addr, data, perr, timeout, buff)
1185 char myBuff[DATA_MAXLEN + 1];
1192 if (buff == (char *) NULL)
1197 if (mips_need_reply)
1198 fatal ("mips_request: Trying to send command before reply");
1199 sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
1200 mips_send_packet (buff, 1);
1201 mips_need_reply = 1;
1204 if (perr == (int *) NULL)
1207 if (! mips_need_reply)
1208 fatal ("mips_request: Trying to get reply before command");
1210 mips_need_reply = 0;
1212 len = mips_receive_packet (buff, 1, timeout);
1215 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
1216 &rpid, &rcmd, &rerrflg, &rresponse) != 4
1217 || (cmd != '\0' && rcmd != cmd))
1218 mips_error ("Bad response from remote board");
1224 /* FIXME: This will returns MIPS errno numbers, which may or may
1225 not be the same as errno values used on other systems. If
1226 they stick to common errno values, they will be the same, but
1227 if they don't, they must be translated. */
1238 mips_initialize_cleanups (arg)
1241 mips_initializing = 0;
1245 mips_exit_cleanups (arg)
1252 mips_send_command (cmd, prompt)
1256 SERIAL_WRITE (mips_desc, cmd, strlen(cmd));
1260 mips_expect (mips_monitor_prompt);
1263 /* Enter remote (dbx) debug mode: */
1267 /* Reset the sequence numbers, ready for the new debug sequence: */
1269 mips_receive_seq = 0;
1271 if (mips_monitor != MON_IDT)
1272 mips_send_command ("debug\r", 0);
1273 else /* assume IDT monitor by default */
1274 mips_send_command ("db tty0\r", 0);
1276 SERIAL_WRITE (mips_desc, "\r", sizeof "\r" - 1);
1278 /* We don't need to absorb any spurious characters here, since the
1279 mips_receive_header will eat up a reasonable number of characters
1280 whilst looking for the SYN, however this avoids the "garbage"
1281 being displayed to the user. */
1282 if (mips_monitor != MON_IDT)
1286 char buff[DATA_MAXLEN + 1];
1287 if (mips_receive_packet (buff, 1, 3) < 0)
1288 mips_error ("Failed to initialize (didn't receive packet).");
1292 /* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1297 struct cleanup *old_cleanups = make_cleanup (mips_exit_cleanups, NULL);
1301 if (mips_monitor != MON_IDT)
1303 /* The DDB (NEC) and MiniRISC (LSI) versions of PMON exit immediately,
1304 so we do not get a reply to this command: */
1305 mips_request ('x', (unsigned int) 0, (unsigned int) 0, NULL,
1306 mips_receive_wait, NULL);
1307 mips_need_reply = 0;
1308 if (!mips_expect (" break!"))
1312 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1313 mips_receive_wait, NULL);
1315 if (mips_monitor == MON_IDT && !mips_expect ("Exiting remote debug"))
1318 if (mips_monitor == MON_DDB)
1320 if (!mips_expect ("\n"))
1324 if (!mips_expect ("\r\n"))
1327 if (!mips_expect (mips_monitor_prompt))
1330 do_cleanups (old_cleanups);
1335 /* Initialize a new connection to the MIPS board, and make sure we are
1336 really connected. */
1342 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1345 /* What is this code doing here? I don't see any way it can happen, and
1346 it might mean mips_initializing didn't get cleared properly.
1347 So I'll make it a warning. */
1349 if (mips_initializing)
1351 warning ("internal error: mips_initialize called twice");
1356 mips_initializing = 1;
1358 /* At this point, the packit protocol isn't responding. We'll try getting
1359 into the monitor, and restarting the protocol. */
1361 /* Force the system into the monitor. After this we *should* be at
1362 the mips_monitor_prompt. */
1363 if (mips_monitor != MON_IDT)
1364 j = 0; /* start by checking if we are already at the prompt */
1366 j = 1; /* start by sending a break */
1371 case 0: /* First, try sending a CR */
1372 SERIAL_FLUSH_INPUT (mips_desc);
1373 SERIAL_WRITE (mips_desc, "\r", 1);
1375 case 1: /* First, try sending a break */
1376 SERIAL_SEND_BREAK (mips_desc);
1378 case 2: /* Then, try a ^C */
1379 SERIAL_WRITE (mips_desc, "\003", 1);
1381 case 3: /* Then, try escaping from download */
1383 if (mips_monitor != MON_IDT)
1387 /* We shouldn't need to send multiple termination
1388 sequences, since the target performs line (or
1389 block) reads, and then processes those
1390 packets. In-case we were downloading a large packet
1391 we flush the output buffer before inserting a
1392 termination sequence. */
1393 SERIAL_FLUSH_OUTPUT (mips_desc);
1394 sprintf (tbuff, "\r/E/E\r");
1395 SERIAL_WRITE (mips_desc, tbuff, 6);
1402 /* We are possibly in binary download mode, having
1403 aborted in the middle of an S-record. ^C won't
1404 work because of binary mode. The only reliable way
1405 out is to send enough termination packets (8 bytes)
1406 to fill up and then overflow the largest size
1407 S-record (255 bytes in this case). This amounts to
1411 mips_make_srec (srec, '7', 0, NULL, 0);
1413 for (i = 1; i <= 33; i++)
1415 SERIAL_WRITE (mips_desc, srec, 8);
1417 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1418 break; /* Break immediatly if we get something from
1425 mips_error ("Failed to initialize.");
1428 if (mips_expect (mips_monitor_prompt))
1432 if (mips_monitor != MON_IDT)
1434 /* Ensure the correct target state: */
1435 if (mips_monitor != MON_LSI)
1436 mips_send_command ("set regsize 64\r", -1);
1437 mips_send_command ("set hostport tty0\r", -1);
1438 mips_send_command ("set brkcmd \"\"\r", -1);
1439 /* Delete all the current breakpoints: */
1440 mips_send_command ("db *\r", -1);
1441 /* NOTE: PMON does not have breakpoint support through the
1442 "debug" mode, only at the monitor command-line. */
1445 mips_enter_debug ();
1447 /* Clear all breakpoints: */
1448 if (mips_monitor == MON_IDT && common_breakpoint ('b', -1, 0, NULL) == 0)
1449 monitor_supports_breakpoints = 1;
1451 monitor_supports_breakpoints = 0;
1453 do_cleanups (old_cleanups);
1455 /* If this doesn't call error, we have connected; we don't care if
1456 the request itself succeeds or fails. */
1458 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
1459 mips_receive_wait, NULL);
1460 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1461 select_frame (get_current_frame (), 0);
1464 /* Open a connection to the remote board. */
1466 common_open (ops, name, from_tty)
1467 struct target_ops *ops;
1472 char *serial_port_name;
1473 char *remote_name = 0;
1474 char *local_name = 0;
1479 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1480 device is attached to the target board (e.g., /dev/ttya).\n"
1481 "If you want to use TFTP to download to the board, specify the name of a\n"
1482 "temporary file to be used by GDB for downloads as the second argument.\n"
1483 "This filename must be in the form host:filename, where host is the name\n"
1484 "of the host running the TFTP server, and the file must be readable by the\n"
1485 "world. If the local name of the temporary file differs from the name as\n"
1486 "seen from the board via TFTP, specify that name as the third parameter.\n");
1488 /* Parse the serial port name, the optional TFTP name, and the
1489 optional local TFTP name. */
1490 if ((argv = buildargv (name)) == NULL)
1492 make_cleanup (freeargv, (char *) argv);
1494 serial_port_name = strsave (argv[0]);
1495 if (argv[1]) /* remote TFTP name specified? */
1497 remote_name = argv[1];
1498 if (argv[2]) /* local TFTP filename specified? */
1499 local_name = argv[2];
1502 target_preopen (from_tty);
1505 unpush_target (current_ops);
1507 /* Open and initialize the serial port. */
1508 mips_desc = SERIAL_OPEN (serial_port_name);
1509 if (mips_desc == (serial_t) NULL)
1510 perror_with_name (serial_port_name);
1512 if (baud_rate != -1)
1514 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1516 SERIAL_CLOSE (mips_desc);
1517 perror_with_name (serial_port_name);
1521 SERIAL_RAW (mips_desc);
1523 /* Open and initialize the optional download port. If it is in the form
1524 hostname#portnumber, it's a UDP socket. If it is in the form
1525 hostname:filename, assume it's the TFTP filename that must be
1526 passed to the DDB board to tell it where to get the load file. */
1529 if (strchr (remote_name, '#'))
1531 udp_desc = SERIAL_OPEN (remote_name);
1533 perror_with_name ("Unable to open UDP port");
1538 /* Save the remote and local names of the TFTP temp file. If
1539 the user didn't specify a local name, assume it's the same
1540 as the part of the remote name after the "host:". */
1544 free (tftp_localname);
1545 if (local_name == NULL)
1546 if ((local_name = strchr (remote_name, ':')) != NULL)
1547 local_name++; /* skip over the colon */
1548 if (local_name == NULL)
1549 local_name = remote_name; /* local name same as remote name */
1550 tftp_name = strsave (remote_name);
1551 tftp_localname = strsave (local_name);
1562 printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
1564 /* Switch to using remote target now. */
1567 /* FIXME: Should we call start_remote here? */
1569 /* Try to figure out the processor model if possible. */
1570 ptype = mips_read_processor_type ();
1572 mips_set_processor_type_command (strsave (ptype), 0);
1574 /* This is really the job of start_remote however, that makes an assumption
1575 that the target is about to print out a status message of some sort. That
1576 doesn't happen here (in fact, it may not be possible to get the monitor to
1577 send the appropriate packet). */
1579 flush_cached_frames ();
1580 registers_changed ();
1581 stop_pc = read_pc ();
1582 set_current_frame (create_new_frame (read_fp (), stop_pc));
1583 select_frame (get_current_frame (), 0);
1584 print_stack_frame (selected_frame, -1, 1);
1585 free (serial_port_name);
1589 mips_open (name, from_tty)
1593 mips_monitor = MON_IDT;
1594 common_open (&mips_ops, name, from_tty);
1598 pmon_open (name, from_tty)
1602 /* The PMON monitor has a prompt different from the default
1603 "TARGET_MONITOR_PROMPT": */
1604 mips_monitor_prompt = "PMON> ";
1605 mips_monitor = MON_PMON;
1606 common_open (&pmon_ops, name, from_tty);
1610 ddb_open (name, from_tty)
1614 /* The PMON monitor has a prompt different from the default
1615 "TARGET_MONITOR_PROMPT": */
1616 mips_monitor_prompt = "NEC010>";
1617 mips_monitor = MON_DDB;
1618 common_open (&ddb_ops, name, from_tty);
1622 lsi_open (name, from_tty)
1626 mips_monitor_prompt = "PMON> ";
1627 mips_monitor = MON_LSI;
1628 common_open (&ddb_ops, name, from_tty);
1631 /* Close a connection to the remote board. */
1634 mips_close (quitting)
1639 /* Get the board out of remote debugging mode. */
1640 (void) mips_exit_debug ();
1646 /* Detach from the remote board. */
1649 mips_detach (args, from_tty)
1654 error ("Argument given to \"detach\" when remotely debugging.");
1661 printf_unfiltered ("Ending remote MIPS debugging.\n");
1664 /* Tell the target board to resume. This does not wait for a reply
1668 mips_resume (pid, step, siggnal)
1670 enum target_signal siggnal;
1674 /* start-sanitize-gm */
1675 #ifndef GENERAL_MAGIC
1676 if (siggnal != TARGET_SIGNAL_0)
1678 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1679 target_signal_to_name (siggnal));
1680 #endif /* GENERAL_MAGIC */
1681 /* end-sanitize-gm */
1683 /* LSI PMON requires returns a reply packet "0x1 s 0x0 0x57f" after
1684 a single step, so we wait for that. */
1685 mips_request (step ? 's' : 'c',
1687 (unsigned int) siggnal,
1688 mips_monitor == MON_LSI && step ? &err : (int *) NULL,
1689 mips_receive_wait, NULL);
1692 /* Return the signal corresponding to SIG, where SIG is the number which
1693 the MIPS protocol uses for the signal. */
1695 mips_signal_from_protocol (sig)
1698 /* We allow a few more signals than the IDT board actually returns, on
1699 the theory that there is at least *some* hope that perhaps the numbering
1700 for these signals is widely agreed upon. */
1703 return TARGET_SIGNAL_UNKNOWN;
1705 /* Don't want to use target_signal_from_host because we are converting
1706 from MIPS signal numbers, not host ones. Our internal numbers
1707 match the MIPS numbers for the signals the board can return, which
1708 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1709 return (enum target_signal) sig;
1712 /* Wait until the remote stops, and return a wait status. */
1715 mips_wait (pid, status)
1717 struct target_waitstatus *status;
1721 char buff[DATA_MAXLEN];
1726 interrupt_count = 0;
1729 /* If we have not sent a single step or continue command, then the
1730 board is waiting for us to do something. Return a status
1731 indicating that it is stopped. */
1732 if (! mips_need_reply)
1734 status->kind = TARGET_WAITKIND_STOPPED;
1735 status->value.sig = TARGET_SIGNAL_TRAP;
1739 /* No timeout; we sit here as long as the program continues to execute. */
1741 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1745 mips_error ("Remote failure: %s", safe_strerror (errno));
1747 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1748 &rpc, &rfp, &rsp, flags);
1750 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1752 if (nfields == 7 || nfields == 9)
1754 char buf[MAX_REGISTER_RAW_SIZE];
1756 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1757 supply_register (PC_REGNUM, buf);
1759 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1760 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1762 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1763 supply_register (SP_REGNUM, buf);
1765 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1766 supply_register (FP_REGNUM, buf);
1772 for (i = 0; i <= 2; i++)
1773 if (flags[i] == 'r' || flags[i] == 'w')
1775 else if (flags[i] == '\000')
1780 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1781 and so on, because the constants we want here are determined by the
1782 MIPS protocol and have nothing to do with what host we are running on. */
1783 if ((rstatus & 0377) == 0)
1785 status->kind = TARGET_WAITKIND_EXITED;
1786 status->value.integer = (((rstatus) >> 8) & 0377);
1788 else if ((rstatus & 0377) == 0177)
1790 status->kind = TARGET_WAITKIND_STOPPED;
1791 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1795 status->kind = TARGET_WAITKIND_SIGNALLED;
1796 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1803 pmon_wait (pid, status)
1805 struct target_waitstatus *status;
1809 char buff[DATA_MAXLEN];
1811 interrupt_count = 0;
1814 /* If we have not sent a single step or continue command, then the
1815 board is waiting for us to do something. Return a status
1816 indicating that it is stopped. */
1817 if (! mips_need_reply)
1819 status->kind = TARGET_WAITKIND_STOPPED;
1820 status->value.sig = TARGET_SIGNAL_TRAP;
1824 /* Sit, polling the serial until the target decides to talk to
1825 us. NOTE: the timeout value we use is used not just for the
1826 first character, but for all the characters. */
1828 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1832 mips_error ("Remote failure: %s", safe_strerror (errno));
1834 /* NOTE: The following (sig) numbers are defined by PMON:
1835 SPP_SIGTRAP 5 breakpoint
1843 /* On returning from a continue, the PMON monitor seems to start
1844 echoing back the messages we send prior to sending back the
1845 ACK. The code can cope with this, but to try and avoid the
1846 unnecessary serial traffic, and "spurious" characters displayed
1847 to the user, we cheat and reset the debug protocol. The problems
1848 seems to be caused by a check on the number of arguments, and the
1849 command length, within the monitor causing it to echo the command
1851 if (mips_monitor != MON_DDB && mips_monitor != MON_LSI)
1854 mips_enter_debug ();
1857 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1858 and so on, because the constants we want here are determined by the
1859 MIPS protocol and have nothing to do with what host we are running on. */
1860 if ((rstatus & 0377) == 0)
1862 status->kind = TARGET_WAITKIND_EXITED;
1863 status->value.integer = (((rstatus) >> 8) & 0377);
1865 else if ((rstatus & 0377) == 0177)
1867 status->kind = TARGET_WAITKIND_STOPPED;
1868 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1872 status->kind = TARGET_WAITKIND_SIGNALLED;
1873 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1879 /* We have to map between the register numbers used by gdb and the
1880 register numbers used by the debugging protocol. This function
1881 assumes that we are using tm-mips.h. */
1883 #define REGNO_OFFSET 96
1886 mips_map_regno (regno)
1891 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1892 return regno - FP0_REGNUM + 32;
1896 return REGNO_OFFSET + 0;
1898 return REGNO_OFFSET + 1;
1900 return REGNO_OFFSET + 2;
1902 return REGNO_OFFSET + 3;
1904 return REGNO_OFFSET + 4;
1906 return REGNO_OFFSET + 5;
1908 /* FIXME: Is there a way to get the status register? */
1913 /* Fetch the remote registers. */
1916 mips_fetch_registers (regno)
1919 unsigned LONGEST val;
1924 for (regno = 0; regno < NUM_REGS; regno++)
1925 mips_fetch_registers (regno);
1929 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1930 /* FP_REGNUM on the mips is a hack which is just supposed to read
1931 zero (see also mips-nat.c). */
1935 /* If PMON doesn't support this register, don't waste serial
1936 bandwidth trying to read it. */
1937 int pmon_reg = mips_map_regno (regno);
1938 if (regno != 0 && pmon_reg == 0)
1942 /* Unfortunately the PMON version in the Vr4300 board has been
1943 compiled without the 64bit register access commands. This
1944 means we cannot get hold of the full register width. */
1945 if (mips_monitor == MON_DDB)
1946 val = (unsigned)mips_request ('t', (unsigned int) pmon_reg,
1947 (unsigned int) 0, &err, mips_receive_wait, NULL);
1949 val = mips_request ('r', (unsigned int) pmon_reg,
1950 (unsigned int) 0, &err, mips_receive_wait, NULL);
1952 mips_error ("Can't read register %d: %s", regno,
1953 safe_strerror (errno));
1958 char buf[MAX_REGISTER_RAW_SIZE];
1960 /* We got the number the register holds, but gdb expects to see a
1961 value in the target byte ordering. */
1962 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1963 supply_register (regno, buf);
1967 /* Prepare to store registers. The MIPS protocol can store individual
1968 registers, so this function doesn't have to do anything. */
1971 mips_prepare_to_store ()
1975 /* Store remote register(s). */
1978 mips_store_registers (regno)
1985 for (regno = 0; regno < NUM_REGS; regno++)
1986 mips_store_registers (regno);
1990 mips_request ('R', (unsigned int) mips_map_regno (regno),
1991 read_register (regno),
1992 &err, mips_receive_wait, NULL);
1994 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1997 /* Fetch a word from the target board. */
2000 mips_fetch_word (addr)
2006 /* FIXME! addr was cast to uint! */
2007 val = mips_request ('d', addr, (unsigned int) 0, &err,
2008 mips_receive_wait, NULL);
2011 /* Data space failed; try instruction space. */
2012 /* FIXME! addr was cast to uint! */
2013 val = mips_request ('i', addr, (unsigned int) 0, &err,
2014 mips_receive_wait, NULL);
2016 mips_error ("Can't read address 0x%s: %s",
2017 paddr_nz (addr), safe_strerror (errno));
2022 /* Store a word to the target board. Returns errno code or zero for
2023 success. If OLD_CONTENTS is non-NULL, put the old contents of that
2024 memory location there. */
2026 /* FIXME! make sure only 32-bit quantities get stored! */
2028 mips_store_word (addr, val, old_contents)
2034 unsigned int oldcontents;
2036 oldcontents = mips_request ('D', addr, (unsigned int) val,
2038 mips_receive_wait, NULL);
2041 /* Data space failed; try instruction space. */
2042 oldcontents = mips_request ('I', addr,
2043 (unsigned int) val, &err,
2044 mips_receive_wait, NULL);
2048 if (old_contents != NULL)
2049 store_unsigned_integer (old_contents, 4, oldcontents);
2053 /* Read or write LEN bytes from inferior memory at MEMADDR,
2054 transferring to or from debugger address MYADDR. Write to inferior
2055 if SHOULD_WRITE is nonzero. Returns length of data written or
2056 read; 0 for error. Note that protocol gives us the correct value
2057 for a longword, since it transfers values in ASCII. We want the
2058 byte values, so we have to swap the longword values. */
2061 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
2066 struct target_ops *ignore;
2069 /* Round starting address down to longword boundary. */
2070 register CORE_ADDR addr = memaddr &~ 3;
2071 /* Round ending address up; get number of longwords that makes. */
2072 register int count = (((memaddr + len) - addr) + 3) / 4;
2073 /* Allocate buffer of that many longwords. */
2074 register char *buffer = alloca (count * 4);
2080 /* Fill start and end extra bytes of buffer with existing data. */
2081 if (addr != memaddr || len < 4)
2083 /* Need part of initial word -- fetch it. */
2084 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
2089 /* Need part of last word -- fetch it. FIXME: we do this even
2090 if we don't need it. */
2091 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2092 mips_fetch_word (addr + (count - 1) * 4));
2095 /* Copy data to be written over corresponding part of buffer */
2097 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2099 /* Write the entire buffer. */
2101 for (i = 0; i < count; i++, addr += 4)
2103 status = mips_store_word (addr,
2104 extract_unsigned_integer (&buffer[i*4], 4),
2106 /* Report each kilobyte (we download 32-bit words at a time) */
2109 printf_unfiltered ("*");
2117 /* FIXME: Do we want a QUIT here? */
2120 printf_unfiltered ("\n");
2124 /* Read all the longwords */
2125 for (i = 0; i < count; i++, addr += 4)
2127 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
2131 /* Copy appropriate bytes out of the buffer. */
2132 memcpy (myaddr, buffer + (memaddr & 3), len);
2137 /* Print info on this target. */
2140 mips_files_info (ignore)
2141 struct target_ops *ignore;
2143 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
2146 /* Kill the process running on the board. This will actually only
2147 work if we are doing remote debugging over the console input. I
2148 think that if IDT/sim had the remote debug interrupt enabled on the
2149 right port, we could interrupt the process with a break signal. */
2154 if (!mips_wait_flag)
2159 if (interrupt_count >= 2)
2161 interrupt_count = 0;
2163 target_terminal_ours ();
2165 if (query ("Interrupted while waiting for the program.\n\
2166 Give up (and stop debugging it)? "))
2168 /* Clean up in such a way that mips_close won't try to talk to the
2169 board (it almost surely won't work since we weren't able to talk to
2174 printf_unfiltered ("Ending remote MIPS debugging.\n");
2175 target_mourn_inferior ();
2177 return_to_top_level (RETURN_QUIT);
2180 target_terminal_inferior ();
2183 if (remote_debug > 0)
2184 printf_unfiltered ("Sending break\n");
2186 SERIAL_SEND_BREAK (mips_desc);
2195 SERIAL_WRITE (mips_desc, &cc, 1);
2197 target_mourn_inferior ();
2202 /* Start running on the target board. */
2205 mips_create_inferior (execfile, args, env)
2215 Can't pass arguments to remote MIPS board; arguments ignored.");
2216 /* And don't try to use them on the next "run" command. */
2217 execute_command ("set args", 0);
2220 if (execfile == 0 || exec_bfd == 0)
2221 error ("No executable file specified");
2223 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2225 init_wait_for_inferior ();
2227 /* FIXME: Should we set inferior_pid here? */
2229 /* start-sanitize-gm */
2230 #ifdef GENERAL_MAGIC
2231 magic_create_inferior_hook ();
2232 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
2234 /* end-sanitize-gm */
2235 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
2236 /* start-sanitize-gm */
2237 #endif /* GENERAL_MAGIC */
2238 /* end-sanitize-gm */
2241 /* Clean up after a process. Actually nothing to do. */
2244 mips_mourn_inferior ()
2246 if (current_ops != NULL)
2247 unpush_target (current_ops);
2248 generic_mourn_inferior ();
2251 /* We can write a breakpoint and read the shadow contents in one
2254 /* The IDT board uses an unusual breakpoint value, and sometimes gets
2255 confused when it sees the usual MIPS breakpoint instruction. */
2257 #define BREAK_INSN (0x00000a0d)
2258 #define BREAK_INSN_SIZE (4)
2260 /* Insert a breakpoint on targets that don't have any better breakpoint
2261 support. We read the contents of the target location and stash it,
2262 then overwrite it with a breakpoint instruction. ADDR is the target
2263 location in the target machine. CONTENTS_CACHE is a pointer to
2264 memory allocated for saving the target contents. It is guaranteed
2265 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2266 is accomplished via BREAKPOINT_MAX). */
2269 mips_insert_breakpoint (addr, contents_cache)
2271 char *contents_cache;
2273 if (monitor_supports_breakpoints)
2274 return common_breakpoint ('B', addr, 0x3, "f");
2276 return mips_store_word (addr, BREAK_INSN, contents_cache);
2280 mips_remove_breakpoint (addr, contents_cache)
2282 char *contents_cache;
2284 if (monitor_supports_breakpoints)
2285 return common_breakpoint ('b', addr, 0, NULL);
2287 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2290 #if 0 /* currently not used */
2291 /* PMON does not currently provide support for the debug mode 'b'
2292 commands to manipulate breakpoints. However, if we wanted to use
2293 the monitor breakpoints (rather than the GDB BREAK_INSN version)
2294 then this code performs the work needed to leave debug mode,
2295 set/clear the breakpoint, and then return to debug mode. */
2297 #define PMON_MAX_BP (33) /* 32 SW, 1 HW */
2298 static CORE_ADDR mips_pmon_bp_info[PMON_MAX_BP];
2299 /* NOTE: The code relies on this vector being zero-initialised by the system */
2302 pmon_insert_breakpoint (addr, contents_cache)
2304 char *contents_cache;
2308 if (monitor_supports_breakpoints)
2310 char tbuff[12]; /* space for breakpoint command */
2314 /* PMON does not support debug level breakpoint set/remove: */
2315 if (mips_exit_debug ())
2316 mips_error ("Failed to exit debug mode");
2318 sprintf (tbuff, "b %08x\r", addr);
2319 mips_send_command (tbuff, 0);
2321 mips_expect ("Bpt ");
2323 if (!mips_getstring (tbuff, 2))
2325 tbuff[2] = '\0'; /* terminate the string */
2326 if (sscanf (tbuff, "%d", &bpnum) != 1)
2328 fprintf_unfiltered (stderr, "Invalid decimal breakpoint number from target: %s\n", tbuff);
2332 mips_expect (" = ");
2334 /* Lead in the hex number we are expecting: */
2338 /* FIXME!! only 8 bytes! need to expand for Bfd64;
2339 which targets return 64-bit addresses? PMON returns only 32! */
2340 if (!mips_getstring (&tbuff[2], 8))
2342 tbuff[10] = '\0'; /* terminate the string */
2344 if (sscanf (tbuff, "0x%08x", &bpaddr) != 1)
2346 fprintf_unfiltered (stderr, "Invalid hex address from target: %s\n", tbuff);
2350 if (bpnum >= PMON_MAX_BP)
2352 fprintf_unfiltered (stderr, "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n",
2353 bpnum, PMON_MAX_BP - 1);
2358 fprintf_unfiltered (stderr, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr, bpaddr);
2360 mips_pmon_bp_info[bpnum] = bpaddr;
2362 mips_expect ("\r\n");
2363 mips_expect (mips_monitor_prompt);
2365 mips_enter_debug ();
2370 return mips_store_word (addr, BREAK_INSN, contents_cache);
2374 pmon_remove_breakpoint (addr, contents_cache)
2376 char *contents_cache;
2378 if (monitor_supports_breakpoints)
2381 char tbuff[7]; /* enough for delete breakpoint command */
2383 for (bpnum = 0; bpnum < PMON_MAX_BP; bpnum++)
2384 if (mips_pmon_bp_info[bpnum] == addr)
2387 if (bpnum >= PMON_MAX_BP)
2389 fprintf_unfiltered (stderr,
2390 "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n",
2395 if (mips_exit_debug ())
2396 mips_error ("Failed to exit debug mode");
2398 sprintf (tbuff, "db %02d\r", bpnum);
2400 mips_send_command (tbuff, -1);
2401 /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not
2402 set" message will be returned. */
2404 mips_enter_debug ();
2409 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2413 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2414 This is used for memory ref breakpoints. */
2416 static unsigned long
2417 calculate_mask (addr, len)
2424 mask = addr ^ (addr + len - 1);
2426 for (i = 32; i >= 0; i--)
2432 mask = (unsigned long) 0xffffffff >> i;
2437 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
2438 for a read watchpoint, or 2 for a read/write watchpoint. */
2441 remote_mips_set_watchpoint (addr, len, type)
2446 CORE_ADDR first_addr;
2450 mask = calculate_mask (addr, len);
2452 first_addr = addr & ~mask;
2462 case 2: /* read/write */
2469 if (common_breakpoint ('B', first_addr, mask, flags))
2476 remote_mips_remove_watchpoint (addr, len, type)
2481 CORE_ADDR first_addr;
2484 mask = calculate_mask (addr, len);
2486 first_addr = addr & ~mask;
2488 if (common_breakpoint ('b', first_addr, 0, NULL))
2495 remote_mips_stopped_by_watchpoint ()
2497 return hit_watchpoint;
2500 /* This routine generates the a breakpoint command of the form:
2502 0x0 <CMD> <ADDR> <MASK> <FLAGS>
2504 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
2505 the address of the breakpoint. <MASK> is a don't care mask for addresses.
2506 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch.
2508 Return 0 if successful; otherwise 1. */
2511 common_breakpoint (cmd, addr, mask, flags)
2518 char buf[DATA_MAXLEN + 1];
2520 int rpid, rerrflg, rresponse;
2523 addr = ADDR_BITS_REMOVE (addr);
2525 sprintf (buf, "0x0 %c 0x%s 0x%s %s", cmd, paddr_nz (addr), paddr_nz (mask),
2528 sprintf (buf, "0x0 %c 0x%s", cmd, paddr_nz (addr));
2530 mips_send_packet (buf, 1);
2532 len = mips_receive_packet (buf, 1, mips_receive_wait);
2535 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
2539 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2543 /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2544 Cogent returns "0x0 b 0xffffffff 0x16\000": */
2545 if (mips_monitor == MON_DDB)
2546 rresponse = rerrflg;
2547 if (rresponse != 22) /* invalid argument */
2548 fprintf_unfiltered (stderr, "common_breakpoint (0x%s): Got error: 0x%x\n",
2549 paddr_nz (addr), rresponse);
2557 send_srec (srec, len, addr)
2566 SERIAL_WRITE (mips_desc, srec, len);
2568 ch = mips_readchar (2);
2572 case SERIAL_TIMEOUT:
2573 error ("Timeout during download.");
2577 case 0x15: /* NACK */
2578 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
2581 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2586 /* Download a binary file by converting it to S records. */
2589 mips_load_srec (args)
2594 char *buffer, srec[1024];
2596 unsigned int srec_frame = 200;
2598 static int hashmark = 1;
2600 buffer = alloca (srec_frame * 2 + 256);
2602 abfd = bfd_openr (args, 0);
2605 printf_filtered ("Unable to open file %s\n", args);
2609 if (bfd_check_format (abfd, bfd_object) == 0)
2611 printf_filtered ("File is not an object file\n");
2615 /* This actually causes a download in the IDT binary format: */
2616 mips_send_command (LOAD_CMD, 0);
2618 for (s = abfd->sections; s; s = s->next)
2620 if (s->flags & SEC_LOAD)
2622 unsigned int numbytes;
2624 /* FIXME! vma too small?? */
2625 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
2626 s->vma + s->_raw_size);
2627 gdb_flush (gdb_stdout);
2629 for (i = 0; i < s->_raw_size; i += numbytes)
2631 numbytes = min (srec_frame, s->_raw_size - i);
2633 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2635 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2636 send_srec (srec, reclen, s->vma + i);
2640 putchar_unfiltered ('#');
2641 gdb_flush (gdb_stdout);
2644 } /* Per-packet (or S-record) loop */
2646 putchar_unfiltered ('\n');
2647 } /* Loadable sections */
2650 putchar_unfiltered ('\n');
2652 /* Write a type 7 terminator record. no data for a type 7, and there
2653 is no data, so len is 0. */
2655 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2657 send_srec (srec, reclen, abfd->start_address);
2659 SERIAL_FLUSH_INPUT (mips_desc);
2663 * mips_make_srec -- make an srecord. This writes each line, one at a
2664 * time, each with it's own header and trailer line.
2665 * An srecord looks like this:
2667 * byte count-+ address
2668 * start ---+ | | data +- checksum
2670 * S01000006F6B692D746573742E73726563E4
2671 * S315000448600000000000000000FC00005900000000E9
2672 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2673 * S30B0004485A0000000000004E
2676 * S<type><length><address><data><checksum>
2680 * is the number of bytes following upto the checksum. Note that
2681 * this is not the number of chars following, since it takes two
2682 * chars to represent a byte.
2686 * 1) two byte address data record
2687 * 2) three byte address data record
2688 * 3) four byte address data record
2689 * 7) four byte address termination record
2690 * 8) three byte address termination record
2691 * 9) two byte address termination record
2694 * is the start address of the data following, or in the case of
2695 * a termination record, the start address of the image
2699 * is the sum of all the raw byte data in the record, from the length
2700 * upwards, modulo 256 and subtracted from 255.
2702 * This routine returns the length of the S-record.
2707 mips_make_srec (buf, type, memaddr, myaddr, len)
2711 unsigned char *myaddr;
2714 unsigned char checksum;
2717 /* Create the header for the srec. addr_size is the number of bytes in the address,
2718 and 1 is the number of bytes in the count. */
2720 /* FIXME!! bigger buf required for 64-bit! */
2723 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2724 /* This assumes S3 style downloads (4byte addresses). There should
2725 probably be a check, or the code changed to make it more
2727 buf[3] = memaddr >> 24;
2728 buf[4] = memaddr >> 16;
2729 buf[5] = memaddr >> 8;
2731 memcpy (&buf[7], myaddr, len);
2733 /* Note that the checksum is calculated on the raw data, not the
2734 hexified data. It includes the length, address and the data
2735 portions of the packet. */
2737 buf += 2; /* Point at length byte */
2738 for (i = 0; i < len + 4 + 1; i++)
2746 /* The following manifest controls whether we enable the simple flow
2747 control support provided by the monitor. If enabled the code will
2748 wait for an affirmative ACK between transmitting packets. */
2749 #define DOETXACK (1)
2751 /* The PMON fast-download uses an encoded packet format constructed of
2752 3byte data packets (encoded as 4 printable ASCII characters), and
2753 escape sequences (preceded by a '/'):
2756 'C' compare checksum (12bit value, not included in checksum calculation)
2757 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary
2758 'Z' zero fill multiple of 3bytes
2759 'B' byte (12bit encoded value, of 8bit data)
2760 'A' address (36bit encoded value)
2761 'E' define entry as original address, and exit load
2763 The packets are processed in 4 character chunks, so the escape
2764 sequences that do not have any data (or variable length data)
2765 should be padded to a 4 character boundary. The decoder will give
2766 an error if the complete message block size is not a multiple of
2767 4bytes (size of record).
2769 The encoding of numbers is done in 6bit fields. The 6bit value is
2770 used to index into this string to get the specific character
2771 encoding for the value: */
2772 static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
2774 /* Convert the number of bits required into an encoded number, 6bits
2775 at a time (range 0..63). Keep a checksum if required (passed
2776 pointer non-NULL). The function returns the number of encoded
2777 characters written into the buffer. */
2779 pmon_makeb64 (v, p, n, chksum)
2785 int count = (n / 6);
2787 if ((n % 12) != 0) {
2788 fprintf_unfiltered(stderr,"Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n",n,(n == 1)?"":"s");
2792 fprintf_unfiltered(stderr,"Fast encoding cannot process more than 36bits at the moment: %dbits\n",n);
2796 /* Deal with the checksum: */
2797 if (chksum != NULL) {
2799 case 36: *chksum += ((v >> 24) & 0xFFF);
2800 case 24: *chksum += ((v >> 12) & 0xFFF);
2801 case 12: *chksum += ((v >> 0) & 0xFFF);
2807 *p++ = encoding[(v >> n) & 0x3F];
2813 /* Shorthand function (that could be in-lined) to output the zero-fill
2814 escape sequence into the data stream. */
2816 pmon_zeroset (recsize, buff, amount, chksum)
2820 unsigned int *chksum;
2824 sprintf(*buff,"/Z");
2825 count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
2826 *buff += (count + 2);
2828 return(recsize + count + 2);
2832 pmon_checkset (recsize, buff, value)
2839 /* Add the checksum (without updating the value): */
2840 sprintf (*buff, "/C");
2841 count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
2842 *buff += (count + 2);
2843 sprintf (*buff, "\n");
2844 *buff += 2; /* include zero terminator */
2845 /* Forcing a checksum validation clears the sum: */
2847 return(recsize + count + 3);
2850 /* Amount of padding we leave after at the end of the output buffer,
2851 for the checksum and line termination characters: */
2852 #define CHECKSIZE (4 + 4 + 4 + 2)
2853 /* zero-fill, checksum, transfer end and line termination space. */
2855 /* The amount of binary data loaded from the object file in a single
2857 #define BINCHUNK (1024)
2859 /* Maximum line of data accepted by the monitor: */
2860 #define MAXRECSIZE (550)
2861 /* NOTE: This constant depends on the monitor being used. This value
2862 is for PMON 5.x on the Cogent Vr4300 board. */
2865 pmon_make_fastrec (outbuf, inbuf, inptr, inamount, recsize, csum, zerofill)
2867 unsigned char *inbuf;
2872 unsigned int *zerofill;
2877 /* This is a simple check to ensure that our data will fit within
2878 the maximum allowable record size. Each record output is 4bytes
2879 in length. We must allow space for a pending zero fill command,
2880 the record, and a checksum record. */
2881 while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0)) {
2882 /* Process the binary data: */
2883 if ((inamount - *inptr) < 3) {
2885 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2887 count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
2889 *recsize += (2 + count);
2892 unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
2893 /* Simple check for zero data. TODO: A better check would be
2894 to check the last, and then the middle byte for being zero
2895 (if the first byte is not). We could then check for
2896 following runs of zeros, and if above a certain size it is
2897 worth the 4 or 8 character hit of the byte insertions used
2898 to pad to the start of the zeroes. NOTE: This also depends
2899 on the alignment at the end of the zero run. */
2900 if (value == 0x00000000) {
2902 if (*zerofill == 0xFFF) /* 12bit counter */
2903 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2906 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
2907 count = pmon_makeb64 (value, p, 24, csum);
2920 pmon_check_ack(mesg)
2923 #if defined(DOETXACK)
2928 c = SERIAL_READCHAR (udp_in_use ? udp_desc : mips_desc, 2);
2929 if ((c == SERIAL_TIMEOUT) || (c != 0x06))
2931 fprintf_unfiltered (gdb_stderr,
2932 "Failed to receive valid ACK for %s\n", mesg);
2933 return(-1); /* terminate the download */
2936 #endif /* DOETXACK */
2940 /* pmon_download - Send a sequence of characters to the PMON download port,
2941 which is either a serial port or a UDP socket. */
2944 pmon_start_download ()
2948 /* Create the temporary download file. */
2949 if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
2950 perror_with_name (tftp_localname);
2954 mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
2955 mips_expect ("Downloading from ");
2956 mips_expect (udp_in_use ? "udp" : "tty0");
2957 mips_expect (", ^C to abort\r\n");
2962 mips_expect_download (char *string)
2964 if (!mips_expect (string))
2966 fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
2968 remove (tftp_localname); /* Remove temporary file */
2976 pmon_end_download (final, bintotal)
2980 char hexnumber[9]; /* includes '\0' space */
2984 static char *load_cmd_prefix = "load -b -s ";
2988 /* Close off the temporary file containing the load data. */
2992 /* Make the temporary file readable by the world. */
2993 if (stat (tftp_localname, &stbuf) == 0)
2994 chmod (tftp_localname, stbuf.st_mode | S_IROTH);
2996 /* Must reinitialize the board to prevent PMON from crashing. */
2997 mips_send_command ("initEther\r", -1);
2999 /* Send the load command. */
3000 cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
3001 strcpy (cmd, load_cmd_prefix);
3002 strcat (cmd, tftp_name);
3004 mips_send_command (cmd, 0);
3006 if (!mips_expect_download ("Downloading from "))
3008 if (!mips_expect_download (tftp_name))
3010 if (!mips_expect_download (", ^C to abort\r\n"))
3014 /* Wait for the stuff that PMON prints after the load has completed.
3015 The timeout value for use in the tftp case (15 seconds) was picked
3016 arbitrarily but might be too small for really large downloads. FIXME. */
3017 if (mips_monitor == MON_LSI)
3019 pmon_check_ack ("termination");
3020 mips_expect_timeout ("Entry address is ", tftp_in_use ? 15 : 2);
3023 mips_expect_timeout ("Entry Address = ", tftp_in_use ? 15 : 2);
3025 sprintf (hexnumber,"%x",final);
3026 mips_expect (hexnumber);
3027 mips_expect ("\r\n");
3028 if (mips_monitor != MON_LSI)
3029 pmon_check_ack ("termination");
3030 mips_expect ("\r\ntotal = 0x");
3031 sprintf (hexnumber,"%x",bintotal);
3032 mips_expect (hexnumber);
3033 if (!mips_expect_download (" bytes\r\n"))
3037 remove (tftp_localname); /* Remove temporary file */
3041 pmon_download (buffer, length)
3046 fwrite (buffer, 1, length, tftp_file);
3048 SERIAL_WRITE (udp_in_use ? udp_desc : mips_desc, buffer, length);
3052 pmon_load_fast (file)
3057 unsigned char *binbuf;
3060 unsigned int csum = 0;
3061 int hashmark = !tftp_in_use;
3066 buffer = (char *)xmalloc(MAXRECSIZE + 1);
3067 binbuf = (unsigned char *)xmalloc(BINCHUNK);
3069 abfd = bfd_openr(file,0);
3072 printf_filtered ("Unable to open file %s\n",file);
3076 if (bfd_check_format(abfd,bfd_object) == 0)
3078 printf_filtered("File is not an object file\n");
3082 /* Setup the required download state: */
3083 mips_send_command ("set dlproto etxack\r", -1);
3084 mips_send_command ("set dlecho off\r", -1);
3085 /* NOTE: We get a "cannot set variable" message if the variable is
3086 already defined to have the argument we give. The code doesn't
3087 care, since it just scans to the next prompt anyway. */
3088 /* Start the download: */
3089 pmon_start_download();
3091 /* Zero the checksum */
3092 sprintf(buffer,"/Kxx\n");
3093 reclen = strlen(buffer);
3094 pmon_download (buffer, reclen);
3095 finished = pmon_check_ack("/Kxx");
3097 for (s = abfd->sections; s && !finished; s = s->next)
3098 if (s->flags & SEC_LOAD) /* only deal with loadable sections */
3100 bintotal += s->_raw_size;
3101 final = (s->vma + s->_raw_size);
3103 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, (unsigned int)s->vma,
3104 (unsigned int)(s->vma + s->_raw_size));
3105 gdb_flush (gdb_stdout);
3107 /* Output the starting address */
3108 sprintf(buffer,"/A");
3109 reclen = pmon_makeb64(s->vma,&buffer[2],36,&csum);
3110 buffer[2 + reclen] = '\n';
3111 buffer[3 + reclen] = '\0';
3112 reclen += 3; /* for the initial escape code and carriage return */
3113 pmon_download (buffer, reclen);
3114 finished = pmon_check_ack("/A");
3118 unsigned int binamount;
3119 unsigned int zerofill = 0;
3125 for (i = 0; ((i < s->_raw_size) && !finished); i += binamount) {
3128 binamount = min (BINCHUNK, s->_raw_size - i);
3130 bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3132 /* This keeps a rolling checksum, until we decide to output
3134 for (; ((binamount - binptr) > 0);) {
3135 pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3136 if (reclen >= (MAXRECSIZE - CHECKSIZE)) {
3137 reclen = pmon_checkset (reclen, &bp, &csum);
3138 pmon_download (buffer, reclen);
3139 finished = pmon_check_ack("data record");
3141 zerofill = 0; /* do not transmit pending zerofills */
3146 putchar_unfiltered ('#');
3147 gdb_flush (gdb_stdout);
3151 reclen = 0; /* buffer processed */
3156 /* Ensure no out-standing zerofill requests: */
3158 reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3160 /* and then flush the line: */
3162 reclen = pmon_checkset (reclen, &bp, &csum);
3163 /* Currently pmon_checkset outputs the line terminator by
3164 default, so we write out the buffer so far: */
3165 pmon_download (buffer, reclen);
3166 finished = pmon_check_ack("record remnant");
3170 putchar_unfiltered ('\n');
3173 /* Terminate the transfer. We know that we have an empty output
3174 buffer at this point. */
3175 sprintf (buffer, "/E/E\n"); /* include dummy padding characters */
3176 reclen = strlen (buffer);
3177 pmon_download (buffer, reclen);
3179 if (finished) { /* Ignore the termination message: */
3180 SERIAL_FLUSH_INPUT (udp_in_use ? udp_desc : mips_desc);
3181 } else { /* Deal with termination message: */
3182 pmon_end_download (final, bintotal);
3188 /* mips_load -- download a file. */
3191 mips_load (file, from_tty)
3195 /* Get the board out of remote debugging mode. */
3196 if (mips_exit_debug ())
3197 error ("mips_load: Couldn't get into monitor mode.");
3199 if (mips_monitor != MON_IDT)
3200 pmon_load_fast (file);
3202 mips_load_srec (file);
3206 /* Finally, make the PC point at the start address */
3207 if (mips_monitor == MON_DDB)
3209 /* Work around problem where DDB monitor does not update the
3210 PC after a load. The following ensures that the write_pc()
3211 WILL update the PC value: */
3212 register_valid[PC_REGNUM] = 0;
3215 write_pc (bfd_get_start_address (exec_bfd));
3217 inferior_pid = 0; /* No process now */
3219 /* This is necessary because many things were based on the PC at the time that
3220 we attached to the monitor, which is no longer valid now that we have loaded
3221 new code (and just changed the PC). Another way to do this might be to call
3222 normal_stop, except that the stack may not be valid, and things would get
3223 horribly confused... */
3225 clear_symtab_users ();
3228 /* The target vector. */
3230 struct target_ops mips_ops =
3232 "mips", /* to_shortname */
3233 "Remote MIPS debugging over serial line", /* to_longname */
3235 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
3236 The argument is the device it is connected to or, if it contains a colon,\n\
3237 HOST:PORT to access a board over a network", /* to_doc */
3238 mips_open, /* to_open */
3239 mips_close, /* to_close */
3240 NULL, /* to_attach */
3241 mips_detach, /* to_detach */
3242 mips_resume, /* to_resume */
3243 mips_wait, /* to_wait */
3244 mips_fetch_registers, /* to_fetch_registers */
3245 mips_store_registers, /* to_store_registers */
3246 mips_prepare_to_store, /* to_prepare_to_store */
3247 mips_xfer_memory, /* to_xfer_memory */
3248 mips_files_info, /* to_files_info */
3249 mips_insert_breakpoint, /* to_insert_breakpoint */
3250 mips_remove_breakpoint, /* to_remove_breakpoint */
3251 NULL, /* to_terminal_init */
3252 NULL, /* to_terminal_inferior */
3253 NULL, /* to_terminal_ours_for_output */
3254 NULL, /* to_terminal_ours */
3255 NULL, /* to_terminal_info */
3256 mips_kill, /* to_kill */
3257 mips_load, /* to_load */
3258 NULL, /* to_lookup_symbol */
3259 mips_create_inferior, /* to_create_inferior */
3260 mips_mourn_inferior, /* to_mourn_inferior */
3261 NULL, /* to_can_run */
3262 NULL, /* to_notice_signals */
3263 0, /* to_thread_alive */
3265 process_stratum, /* to_stratum */
3267 1, /* to_has_all_memory */
3268 1, /* to_has_memory */
3269 1, /* to_has_stack */
3270 1, /* to_has_registers */
3271 1, /* to_has_execution */
3272 NULL, /* sections */
3273 NULL, /* sections_end */
3274 OPS_MAGIC /* to_magic */
3277 /* An alternative target vector: */
3278 struct target_ops pmon_ops =
3280 "pmon", /* to_shortname */
3281 "Remote MIPS debugging over serial line", /* to_longname */
3283 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3284 line. The argument is the device it is connected to or, if it contains a\n\
3285 colon, HOST:PORT to access a board over a network", /* to_doc */
3286 pmon_open, /* to_open */
3287 mips_close, /* to_close */
3288 NULL, /* to_attach */
3289 mips_detach, /* to_detach */
3290 mips_resume, /* to_resume */
3291 pmon_wait, /* to_wait */
3292 mips_fetch_registers, /* to_fetch_registers */
3293 mips_store_registers, /* to_store_registers */
3294 mips_prepare_to_store, /* to_prepare_to_store */
3295 mips_xfer_memory, /* to_xfer_memory */
3296 mips_files_info, /* to_files_info */
3297 mips_insert_breakpoint, /* to_insert_breakpoint */
3298 mips_remove_breakpoint, /* to_remove_breakpoint */
3299 NULL, /* to_terminal_init */
3300 NULL, /* to_terminal_inferior */
3301 NULL, /* to_terminal_ours_for_output */
3302 NULL, /* to_terminal_ours */
3303 NULL, /* to_terminal_info */
3304 mips_kill, /* to_kill */
3305 mips_load, /* to_load */
3306 NULL, /* to_lookup_symbol */
3307 mips_create_inferior, /* to_create_inferior */
3308 mips_mourn_inferior, /* to_mourn_inferior */
3309 NULL, /* to_can_run */
3310 NULL, /* to_notice_signals */
3311 0, /* to_thread_alive */
3313 process_stratum, /* to_stratum */
3315 1, /* to_has_all_memory */
3316 1, /* to_has_memory */
3317 1, /* to_has_stack */
3318 1, /* to_has_registers */
3319 1, /* to_has_execution */
3320 NULL, /* sections */
3321 NULL, /* sections_end */
3322 OPS_MAGIC /* to_magic */
3325 /* Another alternative target vector. This is a PMON system, but with
3326 a different monitor prompt, aswell as some other operational
3328 struct target_ops ddb_ops =
3330 "ddb", /* to_shortname */
3331 "Remote MIPS debugging over serial line", /* to_longname */
3333 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3334 line. The first argument is the device it is connected to or, if it contains\n\
3335 a colon, HOST:PORT to access a board over a network. The optional second\n\
3336 parameter is the temporary file in the form HOST:FILENAME to be used for\n\
3337 TFTP downloads to the board. The optional third parameter is the local\n\
3338 of the TFTP temporary file, if it differs from the filename seen by the board",
3340 ddb_open, /* to_open */
3341 mips_close, /* to_close */
3342 NULL, /* to_attach */
3343 mips_detach, /* to_detach */
3344 mips_resume, /* to_resume */
3345 pmon_wait, /* to_wait */
3346 mips_fetch_registers, /* to_fetch_registers */
3347 mips_store_registers, /* to_store_registers */
3348 mips_prepare_to_store, /* to_prepare_to_store */
3349 mips_xfer_memory, /* to_xfer_memory */
3350 mips_files_info, /* to_files_info */
3351 mips_insert_breakpoint, /* to_insert_breakpoint */
3352 mips_remove_breakpoint, /* to_remove_breakpoint */
3353 NULL, /* to_terminal_init */
3354 NULL, /* to_terminal_inferior */
3355 NULL, /* to_terminal_ours_for_output */
3356 NULL, /* to_terminal_ours */
3357 NULL, /* to_terminal_info */
3358 mips_kill, /* to_kill */
3359 mips_load, /* to_load */
3360 NULL, /* to_lookup_symbol */
3361 mips_create_inferior, /* to_create_inferior */
3362 mips_mourn_inferior, /* to_mourn_inferior */
3363 NULL, /* to_can_run */
3364 NULL, /* to_notice_signals */
3365 0, /* to_thread_alive */
3367 process_stratum, /* to_stratum */
3369 1, /* to_has_all_memory */
3370 1, /* to_has_memory */
3371 1, /* to_has_stack */
3372 1, /* to_has_registers */
3373 1, /* to_has_execution */
3374 NULL, /* sections */
3375 NULL, /* sections_end */
3376 OPS_MAGIC /* to_magic */
3379 /* Another alternative target vector for LSI Logic MiniRISC boards.
3380 This is a PMON system, but with some other operational differences. */
3381 struct target_ops lsi_ops =
3383 "lsi", /* to_shortname */
3384 "Remote MIPS debugging over serial line", /* to_longname */
3386 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3387 line. The first argument is the device it is connected to or, if it contains\n\
3388 a colon, HOST:PORT to access a board over a network. The optional second\n\
3389 parameter is the temporary file in the form HOST:FILENAME to be used for\n\
3390 TFTP downloads to the board. The optional third parameter is the local\n\
3391 of the TFTP temporary file, if it differs from the filename seen by the board",
3393 lsi_open, /* to_open */
3394 mips_close, /* to_close */
3395 NULL, /* to_attach */
3396 mips_detach, /* to_detach */
3397 mips_resume, /* to_resume */
3398 pmon_wait, /* to_wait */
3399 mips_fetch_registers, /* to_fetch_registers */
3400 mips_store_registers, /* to_store_registers */
3401 mips_prepare_to_store, /* to_prepare_to_store */
3402 mips_xfer_memory, /* to_xfer_memory */
3403 mips_files_info, /* to_files_info */
3404 mips_insert_breakpoint, /* to_insert_breakpoint */
3405 mips_remove_breakpoint, /* to_remove_breakpoint */
3406 NULL, /* to_terminal_init */
3407 NULL, /* to_terminal_inferior */
3408 NULL, /* to_terminal_ours_for_output */
3409 NULL, /* to_terminal_ours */
3410 NULL, /* to_terminal_info */
3411 mips_kill, /* to_kill */
3412 mips_load, /* to_load */
3413 NULL, /* to_lookup_symbol */
3414 mips_create_inferior, /* to_create_inferior */
3415 mips_mourn_inferior, /* to_mourn_inferior */
3416 NULL, /* to_can_run */
3417 NULL, /* to_notice_signals */
3418 0, /* to_thread_alive */
3420 process_stratum, /* to_stratum */
3422 1, /* to_has_all_memory */
3423 1, /* to_has_memory */
3424 1, /* to_has_stack */
3425 1, /* to_has_registers */
3426 1, /* to_has_execution */
3427 NULL, /* sections */
3428 NULL, /* sections_end */
3429 OPS_MAGIC /* to_magic */
3433 _initialize_remote_mips ()
3435 add_target (&mips_ops);
3436 add_target (&pmon_ops);
3437 add_target (&ddb_ops);
3438 add_target (&lsi_ops);
3441 add_set_cmd ("timeout", no_class, var_zinteger,
3442 (char *) &mips_receive_wait,
3443 "Set timeout in seconds for remote MIPS serial I/O.",
3448 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3449 (char *) &mips_retransmit_wait,
3450 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3451 This is the number of seconds to wait for an acknowledgement to a packet\n\
3452 before resending the packet.", &setlist),
3456 add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3457 (char *) &mips_syn_garbage,
3458 "Set the maximum number of characters to ignore when scanning for a SYN.\n\
3459 This is the maximum number of characters GDB will ignore when trying to\n\
3460 synchronize with the remote system. A value of -1 means that there is no limit\n\
3461 (Note that these characters are printed out even though they are ignored.)",