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"
34 #ifdef ANSI_PROTOTYPES
40 extern char *mips_read_processor_type PARAMS ((void));
42 extern void mips_set_processor_type_command PARAMS ((char *, int));
45 /* Prototypes for local functions. */
47 static int mips_readchar PARAMS ((int timeout));
49 static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
50 int ch, int timeout));
52 static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
53 int *pch, int timeout));
55 static int mips_cksum PARAMS ((const unsigned char *hdr,
56 const unsigned char *data,
59 static void mips_send_packet PARAMS ((const char *s, int get_ack));
61 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
64 static int mips_request PARAMS ((int cmd, unsigned int addr,
65 unsigned int data, int *perr, int timeout,
68 static void mips_initialize PARAMS ((void));
70 static void mips_open PARAMS ((char *name, int from_tty));
72 static void mips_close PARAMS ((int quitting));
74 static void mips_detach PARAMS ((char *args, int from_tty));
76 static void mips_resume PARAMS ((int pid, int step,
77 enum target_signal siggnal));
79 static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
81 static int mips_map_regno PARAMS ((int regno));
83 static void mips_fetch_registers PARAMS ((int regno));
85 static void mips_prepare_to_store PARAMS ((void));
87 static void mips_store_registers PARAMS ((int regno));
89 static int mips_fetch_word PARAMS ((CORE_ADDR addr));
91 static int mips_store_word PARAMS ((CORE_ADDR addr, int value,
94 static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
95 int write, struct target_ops *ignore));
97 static void mips_files_info PARAMS ((struct target_ops *ignore));
99 static void mips_create_inferior PARAMS ((char *execfile, char *args,
102 static void mips_mourn_inferior PARAMS ((void));
104 static void mips_load PARAMS ((char *file, int from_tty));
106 static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
107 unsigned char *myaddr, int len));
109 static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask,
111 /* A forward declaration. */
112 extern struct target_ops mips_ops;
114 /* The MIPS remote debugging interface is built on top of a simple
115 packet protocol. Each packet is organized as follows:
117 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
118 may not appear anywhere else in the packet. Any time a SYN is
119 seen, a new packet should be assumed to have begun.
122 This byte contains the upper five bits of the logical length
123 of the data section, plus a single bit indicating whether this
124 is a data packet or an acknowledgement. The documentation
125 indicates that this bit is 1 for a data packet, but the actual
126 board uses 1 for an acknowledgement. The value of the byte is
127 0x40 + (ack ? 0x20 : 0) + (len >> 6)
128 (we always have 0 <= len < 1024). Acknowledgement packets do
129 not carry data, and must have a data length of 0.
131 LEN1 This byte contains the lower six bits of the logical length of
132 the data section. The value is
135 SEQ This byte contains the six bit sequence number of the packet.
138 An acknowlegment packet contains the sequence number of the
139 packet being acknowledged plus 1 modulo 64. Data packets are
140 transmitted in sequence. There may only be one outstanding
141 unacknowledged data packet at a time. The sequence numbers
142 are independent in each direction. If an acknowledgement for
143 the previous packet is received (i.e., an acknowledgement with
144 the sequence number of the packet just sent) the packet just
145 sent should be retransmitted. If no acknowledgement is
146 received within a timeout period, the packet should be
147 retransmitted. This has an unfortunate failure condition on a
148 high-latency line, as a delayed acknowledgement may lead to an
149 endless series of duplicate packets.
151 DATA The actual data bytes follow. The following characters are
152 escaped inline with DLE (ASCII 020, or ^P):
158 The additional DLE characters are not counted in the logical
159 length stored in the TYPE_LEN and LEN1 bytes.
164 These bytes contain an 18 bit checksum of the complete
165 contents of the packet excluding the SEQ byte and the
166 CSUM[123] bytes. The checksum is simply the twos complement
167 addition of all the bytes treated as unsigned characters. The
168 values of the checksum bytes are:
169 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
170 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
171 CSUM3: 0x40 + (cksum & 0x3f)
173 It happens that the MIPS remote debugging protocol always
174 communicates with ASCII strings. Because of this, this
175 implementation doesn't bother to handle the DLE quoting mechanism,
176 since it will never be required. */
178 /* The SYN character which starts each packet. */
181 /* The 0x40 used to offset each packet (this value ensures that all of
182 the header and trailer bytes, other than SYN, are printable ASCII
184 #define HDR_OFFSET 0x40
186 /* The indices of the bytes in the packet header. */
187 #define HDR_INDX_SYN 0
188 #define HDR_INDX_TYPE_LEN 1
189 #define HDR_INDX_LEN1 2
190 #define HDR_INDX_SEQ 3
193 /* The data/ack bit in the TYPE_LEN header byte. */
194 #define TYPE_LEN_DA_BIT 0x20
195 #define TYPE_LEN_DATA 0
196 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
198 /* How to compute the header bytes. */
199 #define HDR_SET_SYN(data, len, seq) (SYN)
200 #define HDR_SET_TYPE_LEN(data, len, seq) \
202 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
203 + (((len) >> 6) & 0x1f))
204 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
205 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
207 /* Check that a header byte is reasonable. */
208 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
210 /* Get data from the header. These macros evaluate their argument
212 #define HDR_IS_DATA(hdr) \
213 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
214 #define HDR_GET_LEN(hdr) \
215 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
216 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
218 /* The maximum data length. */
219 #define DATA_MAXLEN 1023
221 /* The trailer offset. */
222 #define TRLR_OFFSET HDR_OFFSET
224 /* The indices of the bytes in the packet trailer. */
225 #define TRLR_INDX_CSUM1 0
226 #define TRLR_INDX_CSUM2 1
227 #define TRLR_INDX_CSUM3 2
228 #define TRLR_LENGTH 3
230 /* How to compute the trailer bytes. */
231 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
232 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
233 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
235 /* Check that a trailer byte is reasonable. */
236 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
238 /* Get data from the trailer. This evaluates its argument multiple
240 #define TRLR_GET_CKSUM(trlr) \
241 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
242 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
243 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
245 /* The sequence number modulos. */
246 #define SEQ_MODULOS (64)
248 /* Set to 1 if the target is open. */
249 static int mips_is_open;
251 /* Set to 1 while the connection is being initialized. */
252 static int mips_initializing;
254 /* The next sequence number to send. */
255 static int mips_send_seq;
257 /* The next sequence number we expect to receive. */
258 static int mips_receive_seq;
260 /* The time to wait before retransmitting a packet, in seconds. */
261 static int mips_retransmit_wait = 3;
263 /* The number of times to try retransmitting a packet before giving up. */
264 static int mips_send_retries = 10;
266 /* The number of garbage characters to accept when looking for an
267 SYN for the next packet. */
268 static int mips_syn_garbage = 1050;
270 /* The time to wait for a packet, in seconds. */
271 static int mips_receive_wait = 5;
273 /* Set if we have sent a packet to the board but have not yet received
275 static int mips_need_reply = 0;
277 /* Handle used to access serial I/O stream. */
278 static serial_t mips_desc;
280 /* Counts the number of times the user tried to interrupt the target (usually
282 static int interrupt_count;
284 /* If non-zero, means that the target is running. */
285 static int mips_wait_flag = 0;
287 /* If non-zero, monitor supports breakpoint commands. */
288 static monitor_supports_breakpoints = 0;
290 /* Data cache header. */
292 static DCACHE *mips_dcache;
294 /* Non-zero means that we've just hit a read or write watchpoint */
295 static int hit_watchpoint;
297 /* Handle low-level error that we can't recover from. Note that just
298 error()ing out from target_wait or some such low-level place will cause
299 all hell to break loose--the rest of GDB will tend to get left in an
300 inconsistent state. */
303 #ifdef ANSI_PROTOTYPES
304 mips_error (char *string, ...)
306 mips_error (va_alist)
312 #ifdef ANSI_PROTOTYPES
313 va_start (args, string);
317 string = va_arg (args, char *);
320 target_terminal_ours ();
321 wrap_here(""); /* Force out any buffered output */
322 gdb_flush (gdb_stdout);
324 fprintf_filtered (gdb_stderr, error_pre_print);
325 vfprintf_filtered (gdb_stderr, string, args);
326 fprintf_filtered (gdb_stderr, "\n");
328 gdb_flush (gdb_stderr);
330 /* Clean up in such a way that mips_close won't try to talk to the
331 board (it almost surely won't work since we weren't able to talk to
334 SERIAL_CLOSE (mips_desc);
336 printf_unfiltered ("Ending remote MIPS debugging.\n");
337 target_mourn_inferior ();
339 return_to_top_level (RETURN_ERROR);
342 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
356 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
357 were waiting for the TARGET_MONITOR_PROMPT... */
359 c = SERIAL_READCHAR (mips_desc, 2);
361 if (c == SERIAL_TIMEOUT)
382 /* Read a character from the remote, aborting on error. Returns
383 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
384 returns). FIXME: If we see the string TARGET_MONITOR_PROMPT from
385 the board, then we are debugging on the main console port, and we
386 have somehow dropped out of remote debugging mode. In this case,
387 we automatically go back in to remote debugging mode. This is a
388 hack, put in because I can't find any way for a program running on
389 the remote board to terminate without also ending remote debugging
390 mode. I assume users won't have any trouble with this; for one
391 thing, the IDT documentation generally assumes that the remote
392 debugging port is not the console port. This is, however, very
393 convenient for DejaGnu when you only have one connected serial
397 mips_readchar (timeout)
401 static int state = 0;
402 static char nextstate[] = TARGET_MONITOR_PROMPT;
403 #ifdef MAINTENANCE_CMDS
407 if (i == -1 && watchdog > 0)
411 if (state == (sizeof(nextstate) / sizeof(char)))
413 ch = SERIAL_READCHAR (mips_desc, timeout);
414 #ifdef MAINTENANCE_CMDS
415 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
417 target_mourn_inferior ();
418 error ("Watchdog has expired. Target detached.\n");
421 if (ch == SERIAL_EOF)
422 mips_error ("End of file from remote");
423 if (ch == SERIAL_ERROR)
424 mips_error ("Error reading from remote: %s", safe_strerror (errno));
425 if (remote_debug > 1)
427 /* Don't use _filtered; we can't deal with a QUIT out of
428 target_wait, and I think this might be called from there. */
429 if (ch != SERIAL_TIMEOUT)
430 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
432 printf_unfiltered ("Timed out in read\n");
435 /* If we have seen TARGET_MONITOR_PROMPT and we either time out, or
436 we see a @ (which was echoed from a packet we sent), reset the
437 board as described above. The first character in a packet after
438 the SYN (which is not echoed) is always an @ unless the packet is
439 more than 64 characters long, which ours never are. */
440 if ((ch == SERIAL_TIMEOUT || ch == '@')
441 && state == (sizeof(nextstate) / sizeof(char))
442 && ! mips_initializing)
444 if (remote_debug > 0)
445 /* Don't use _filtered; we can't deal with a QUIT out of
446 target_wait, and I think this might be called from there. */
447 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
454 /* At this point, about the only thing we can do is abort the command
455 in progress and get back to command level as quickly as possible. */
457 error ("Remote board reset, debug protocol re-initialized.");
460 if (ch == nextstate[state])
468 /* Get a packet header, putting the data in the supplied buffer.
469 PGARBAGE is a pointer to the number of garbage characters received
470 so far. CH is the last character received. Returns 0 for success,
471 or -1 for timeout. */
474 mips_receive_header (hdr, pgarbage, ch, timeout)
484 /* Wait for a SYN. mips_syn_garbage is intended to prevent
485 sitting here indefinitely if the board sends us one garbage
486 character per second. ch may already have a value from the
487 last time through the loop. */
490 ch = mips_readchar (timeout);
491 if (ch == SERIAL_TIMEOUT)
495 /* Printing the character here lets the user of gdb see
496 what the program is outputting, if the debugging is
497 being done on the console port. Don't use _filtered;
498 we can't deal with a QUIT out of target_wait. */
499 if (! mips_initializing || remote_debug > 0)
501 if (ch < 0x20 && ch != '\n')
503 putchar_unfiltered ('^');
504 putchar_unfiltered (ch + 0x40);
507 putchar_unfiltered (ch);
508 gdb_flush (gdb_stdout);
512 if (*pgarbage > mips_syn_garbage)
513 mips_error ("Debug protocol failure: more than %d characters before a sync.",
518 /* Get the packet header following the SYN. */
519 for (i = 1; i < HDR_LENGTH; i++)
521 ch = mips_readchar (timeout);
522 if (ch == SERIAL_TIMEOUT)
525 /* Make sure this is a header byte. */
526 if (ch == SYN || ! HDR_CHECK (ch))
532 /* If we got the complete header, we can return. Otherwise we
533 loop around and keep looking for SYN. */
539 /* Get a packet header, putting the data in the supplied buffer.
540 PGARBAGE is a pointer to the number of garbage characters received
541 so far. The last character read is returned in *PCH. Returns 0
542 for success, -1 for timeout, -2 for error. */
545 mips_receive_trailer (trlr, pgarbage, pch, timeout)
554 for (i = 0; i < TRLR_LENGTH; i++)
556 ch = mips_readchar (timeout);
558 if (ch == SERIAL_TIMEOUT)
560 if (! TRLR_CHECK (ch))
567 /* Get the checksum of a packet. HDR points to the packet header.
568 DATA points to the packet data. LEN is the length of DATA. */
571 mips_cksum (hdr, data, len)
572 const unsigned char *hdr;
573 const unsigned char *data;
576 register const unsigned char *p;
582 /* The initial SYN is not included in the checksum. */
596 /* Send a packet containing the given ASCII string. */
599 mips_send_packet (s, get_ack)
604 unsigned char *packet;
609 if (len > DATA_MAXLEN)
610 mips_error ("MIPS protocol data packet too long: %s", s);
612 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
614 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
615 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
616 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
617 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
619 memcpy (packet + HDR_LENGTH, s, len);
621 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
622 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
623 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
624 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
626 /* Increment the sequence number. This will set mips_send_seq to
627 the sequence number we expect in the acknowledgement. */
628 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
630 /* We can only have one outstanding data packet, so we just wait for
631 the acknowledgement here. Keep retransmitting the packet until
632 we get one, or until we've tried too many times. */
633 for (try = 0; try < mips_send_retries; try++)
638 if (remote_debug > 0)
640 /* Don't use _filtered; we can't deal with a QUIT out of
641 target_wait, and I think this might be called from there. */
642 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
643 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
646 if (SERIAL_WRITE (mips_desc, packet,
647 HDR_LENGTH + len + TRLR_LENGTH) != 0)
648 mips_error ("write to target failed: %s", safe_strerror (errno));
657 unsigned char hdr[HDR_LENGTH + 1];
658 unsigned char trlr[TRLR_LENGTH + 1];
662 /* Get the packet header. If we time out, resend the data
664 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
670 /* If we get a data packet, assume it is a duplicate and
671 ignore it. FIXME: If the acknowledgement is lost, this
672 data packet may be the packet the remote sends after the
674 if (HDR_IS_DATA (hdr))
677 /* If the length is not 0, this is a garbled packet. */
678 if (HDR_GET_LEN (hdr) != 0)
681 /* Get the packet trailer. */
682 err = mips_receive_trailer (trlr, &garbage, &ch,
683 mips_retransmit_wait);
685 /* If we timed out, resend the data packet. */
689 /* If we got a bad character, reread the header. */
693 /* If the checksum does not match the trailer checksum, this
694 is a bad packet; ignore it. */
695 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
696 != TRLR_GET_CKSUM (trlr))
699 if (remote_debug > 0)
701 hdr[HDR_LENGTH] = '\0';
702 trlr[TRLR_LENGTH] = '\0';
703 /* Don't use _filtered; we can't deal with a QUIT out of
704 target_wait, and I think this might be called from there. */
705 printf_unfiltered ("Got ack %d \"%s%s\"\n",
706 HDR_GET_SEQ (hdr), hdr + 1, trlr);
709 /* If this ack is for the current packet, we're done. */
710 seq = HDR_GET_SEQ (hdr);
711 if (seq == mips_send_seq)
714 /* If this ack is for the last packet, resend the current
716 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
719 /* Otherwise this is a bad ack; ignore it. Increment the
720 garbage count to ensure that we do not stay in this loop
726 mips_error ("Remote did not acknowledge packet");
729 /* Receive and acknowledge a packet, returning the data in BUFF (which
730 should be DATA_MAXLEN + 1 bytes). The protocol documentation
731 implies that only the sender retransmits packets, so this code just
732 waits silently for a packet. It returns the length of the received
733 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
734 don't print an error message and return -1. */
737 mips_receive_packet (buff, throw_error, timeout)
745 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
752 unsigned char hdr[HDR_LENGTH];
753 unsigned char trlr[TRLR_LENGTH];
757 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
760 mips_error ("Timed out waiting for remote packet");
767 /* An acknowledgement is probably a duplicate; ignore it. */
768 if (! HDR_IS_DATA (hdr))
770 /* Don't use _filtered; we can't deal with a QUIT out of
771 target_wait, and I think this might be called from there. */
772 if (remote_debug > 0)
773 printf_unfiltered ("Ignoring unexpected ACK\n");
777 /* If this is the wrong sequence number, ignore it. */
778 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
780 /* Don't use _filtered; we can't deal with a QUIT out of
781 target_wait, and I think this might be called from there. */
782 if (remote_debug > 0)
783 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
784 HDR_GET_SEQ (hdr), mips_receive_seq);
788 len = HDR_GET_LEN (hdr);
790 for (i = 0; i < len; i++)
794 rch = mips_readchar (timeout);
800 if (rch == SERIAL_TIMEOUT)
803 mips_error ("Timed out waiting for remote packet");
812 /* Don't use _filtered; we can't deal with a QUIT out of
813 target_wait, and I think this might be called from there. */
814 if (remote_debug > 0)
815 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
820 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
824 mips_error ("Timed out waiting for packet");
830 /* Don't use _filtered; we can't deal with a QUIT out of
831 target_wait, and I think this might be called from there. */
832 if (remote_debug > 0)
833 printf_unfiltered ("Got SYN when wanted trailer\n");
837 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
840 if (remote_debug > 0)
841 /* Don't use _filtered; we can't deal with a QUIT out of
842 target_wait, and I think this might be called from there. */
843 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
844 mips_cksum (hdr, buff, len),
845 TRLR_GET_CKSUM (trlr));
847 /* The checksum failed. Send an acknowledgement for the
848 previous packet to tell the remote to resend the packet. */
849 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
850 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
851 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
852 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
854 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
856 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
857 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
858 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
860 if (remote_debug > 0)
862 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
863 /* Don't use _filtered; we can't deal with a QUIT out of
864 target_wait, and I think this might be called from there. */
865 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
869 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
872 mips_error ("write to target failed: %s", safe_strerror (errno));
878 if (remote_debug > 0)
881 /* Don't use _filtered; we can't deal with a QUIT out of
882 target_wait, and I think this might be called from there. */
883 printf_unfiltered ("Got packet \"%s\"\n", buff);
886 /* We got the packet. Send an acknowledgement. */
887 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
889 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
890 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
891 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
892 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
894 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
896 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
897 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
898 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
900 if (remote_debug > 0)
902 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
903 /* Don't use _filtered; we can't deal with a QUIT out of
904 target_wait, and I think this might be called from there. */
905 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
909 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
912 mips_error ("write to target failed: %s", safe_strerror (errno));
920 /* Optionally send a request to the remote system and optionally wait
921 for the reply. This implements the remote debugging protocol,
922 which is built on top of the packet protocol defined above. Each
923 request has an ADDR argument and a DATA argument. The following
924 requests are defined:
926 \0 don't send a request; just wait for a reply
927 i read word from instruction space at ADDR
928 d read word from data space at ADDR
929 I write DATA to instruction space at ADDR
930 D write DATA to data space at ADDR
931 r read register number ADDR
932 R set register number ADDR to value DATA
933 c continue execution (if ADDR != 1, set pc to ADDR)
934 s single step (if ADDR != 1, set pc to ADDR)
936 The read requests return the value requested. The write requests
937 return the previous value in the changed location. The execution
938 requests return a UNIX wait value (the approximate signal which
939 caused execution to stop is in the upper eight bits).
941 If PERR is not NULL, this function waits for a reply. If an error
942 occurs, it sets *PERR to 1 and sets errno according to what the
943 target board reports. */
946 mips_request (cmd, addr, data, perr, timeout, buff)
954 char myBuff[DATA_MAXLEN + 1];
961 if (buff == (char *) NULL)
967 fatal ("mips_request: Trying to send command before reply");
968 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
969 mips_send_packet (buff, 1);
973 if (perr == (int *) NULL)
976 if (! mips_need_reply)
977 fatal ("mips_request: Trying to get reply before command");
981 len = mips_receive_packet (buff, 1, timeout);
984 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
985 &rpid, &rcmd, &rerrflg, &rresponse) != 4
986 || (cmd != '\0' && rcmd != cmd))
987 mips_error ("Bad response from remote board");
993 /* FIXME: This will returns MIPS errno numbers, which may or may
994 not be the same as errno values used on other systems. If
995 they stick to common errno values, they will be the same, but
996 if they don't, they must be translated. */
1007 mips_initialize_cleanups (arg)
1010 mips_initializing = 0;
1013 /* Initialize a new connection to the MIPS board, and make sure we are
1014 really connected. */
1019 char buff[DATA_MAXLEN + 1];
1021 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1024 /* What is this code doing here? I don't see any way it can happen, and
1025 it might mean mips_initializing didn't get cleared properly.
1026 So I'll make it a warning. */
1028 if (mips_initializing)
1030 warning ("internal error: mips_initialize called twice");
1035 mips_initializing = 1;
1038 mips_receive_seq = 0;
1040 /* At this point, the packit protocol isn't responding. We'll try getting
1041 into the monitor, and restarting the protocol. */
1043 /* Force the system into the IDT monitor. After this we *should* be at the
1046 for (j = 1; j <= 4; j++)
1050 case 1: /* First, try sending a break */
1051 SERIAL_SEND_BREAK (mips_desc);
1053 case 2: /* Then, try a ^C */
1054 SERIAL_WRITE (mips_desc, "\003", 1);
1056 case 3: /* Then, try escaping from download */
1061 /* We are possibly in binary download mode, having aborted in the
1062 middle of an S-record. ^C won't work because of binary mode.
1063 The only reliable way out is to send enough termination packets
1064 (8 bytes) to fill up and then overflow the largest size S-record
1065 (255 bytes in this case). This amounts to 256/8 + 1 packets.
1068 mips_make_srec (srec, '7', 0, NULL, 0);
1070 for (i = 1; i <= 33; i++)
1072 SERIAL_WRITE (mips_desc, srec, 8);
1074 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1075 break; /* Break immediatly if we get something from
1081 mips_error ("Failed to initialize.");
1084 if (mips_expect (TARGET_MONITOR_PROMPT))
1087 SERIAL_WRITE (mips_desc, "db tty0\015", sizeof "db tty0\015" - 1);
1088 mips_expect ("db tty0\015\012"); /* Eat the echo */
1090 SERIAL_WRITE (mips_desc, "\015", sizeof "\015" - 1);
1092 if (mips_receive_packet (buff, 1, 3) < 0)
1093 mips_error ("Failed to initialize (didn't receive packet).");
1095 if (common_breakpoint ('b', -1, 0, NULL)) /* Clear all breakpoints */
1096 monitor_supports_breakpoints = 0; /* Failed, don't use it anymore */
1098 monitor_supports_breakpoints = 1;
1100 do_cleanups (old_cleanups);
1102 /* If this doesn't call error, we have connected; we don't care if
1103 the request itself succeeds or fails. */
1105 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
1106 mips_receive_wait, NULL);
1107 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1108 select_frame (get_current_frame (), 0);
1111 /* Open a connection to the remote board. */
1114 mips_open (name, from_tty)
1122 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1123 device is attached to the target board (e.g., /dev/ttya).");
1125 target_preopen (from_tty);
1128 unpush_target (&mips_ops);
1130 mips_desc = SERIAL_OPEN (name);
1131 if (mips_desc == (serial_t) NULL)
1132 perror_with_name (name);
1134 if (baud_rate != -1)
1136 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1138 SERIAL_CLOSE (mips_desc);
1139 perror_with_name (name);
1143 SERIAL_RAW (mips_desc);
1150 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
1152 /* Switch to using remote target now. */
1153 push_target (&mips_ops);
1155 /* FIXME: Should we call start_remote here? */
1157 /* Try to figure out the processor model if possible. */
1158 ptype = mips_read_processor_type ();
1160 mips_set_processor_type_command (strsave (ptype), 0);
1162 /* This is really the job of start_remote however, that makes an assumption
1163 that the target is about to print out a status message of some sort. That
1164 doesn't happen here (in fact, it may not be possible to get the monitor to
1165 send the appropriate packet). */
1167 flush_cached_frames ();
1168 registers_changed ();
1169 stop_pc = read_pc ();
1170 set_current_frame (create_new_frame (read_fp (), stop_pc));
1171 select_frame (get_current_frame (), 0);
1172 print_stack_frame (selected_frame, -1, 1);
1175 /* Close a connection to the remote board. */
1178 mips_close (quitting)
1187 /* Get the board out of remote debugging mode. */
1188 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1189 mips_receive_wait, NULL);
1191 SERIAL_CLOSE (mips_desc);
1195 /* Detach from the remote board. */
1198 mips_detach (args, from_tty)
1203 error ("Argument given to \"detach\" when remotely debugging.");
1210 printf_unfiltered ("Ending remote MIPS debugging.\n");
1213 /* Tell the target board to resume. This does not wait for a reply
1217 mips_resume (pid, step, siggnal)
1219 enum target_signal siggnal;
1222 /* start-sanitize-gm */
1223 #ifndef GENERAL_MAGIC
1224 if (siggnal != TARGET_SIGNAL_0)
1226 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1227 target_signal_to_name (siggnal));
1228 #endif /* GENERAL_MAGIC */
1229 /* end-sanitize-gm */
1231 mips_request (step ? 's' : 'c',
1233 (unsigned int) siggnal,
1235 mips_receive_wait, NULL);
1238 /* Return the signal corresponding to SIG, where SIG is the number which
1239 the MIPS protocol uses for the signal. */
1241 mips_signal_from_protocol (sig)
1244 /* We allow a few more signals than the IDT board actually returns, on
1245 the theory that there is at least *some* hope that perhaps the numbering
1246 for these signals is widely agreed upon. */
1249 return TARGET_SIGNAL_UNKNOWN;
1251 /* Don't want to use target_signal_from_host because we are converting
1252 from MIPS signal numbers, not host ones. Our internal numbers
1253 match the MIPS numbers for the signals the board can return, which
1254 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1255 return (enum target_signal) sig;
1258 /* Wait until the remote stops, and return a wait status. */
1261 mips_wait (pid, status)
1263 struct target_waitstatus *status;
1267 char buff[DATA_MAXLEN];
1272 interrupt_count = 0;
1275 /* If we have not sent a single step or continue command, then the
1276 board is waiting for us to do something. Return a status
1277 indicating that it is stopped. */
1278 if (! mips_need_reply)
1280 status->kind = TARGET_WAITKIND_STOPPED;
1281 status->value.sig = TARGET_SIGNAL_TRAP;
1285 /* No timeout; we sit here as long as the program continues to execute. */
1287 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1291 mips_error ("Remote failure: %s", safe_strerror (errno));
1293 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1294 &rpc, &rfp, &rsp, flags);
1296 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1298 if (nfields == 7 || nfields == 9)
1300 char buf[MAX_REGISTER_RAW_SIZE];
1302 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1303 supply_register (PC_REGNUM, buf);
1305 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1306 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1308 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1309 supply_register (SP_REGNUM, buf);
1311 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1312 supply_register (FP_REGNUM, buf);
1318 for (i = 0; i <= 2; i++)
1319 if (flags[i] == 'r' || flags[i] == 'w')
1321 else if (flags[i] == '\000')
1326 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1327 and so on, because the constants we want here are determined by the
1328 MIPS protocol and have nothing to do with what host we are running on. */
1329 if ((rstatus & 0377) == 0)
1331 status->kind = TARGET_WAITKIND_EXITED;
1332 status->value.integer = (((rstatus) >> 8) & 0377);
1334 else if ((rstatus & 0377) == 0177)
1336 status->kind = TARGET_WAITKIND_STOPPED;
1337 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1341 status->kind = TARGET_WAITKIND_SIGNALLED;
1342 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1348 /* We have to map between the register numbers used by gdb and the
1349 register numbers used by the debugging protocol. This function
1350 assumes that we are using tm-mips.h. */
1352 #define REGNO_OFFSET 96
1355 mips_map_regno (regno)
1360 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1361 return regno - FP0_REGNUM + 32;
1365 return REGNO_OFFSET + 0;
1367 return REGNO_OFFSET + 1;
1369 return REGNO_OFFSET + 2;
1371 return REGNO_OFFSET + 3;
1373 return REGNO_OFFSET + 4;
1375 return REGNO_OFFSET + 5;
1377 /* FIXME: Is there a way to get the status register? */
1382 /* Fetch the remote registers. */
1385 mips_fetch_registers (regno)
1388 unsigned LONGEST val;
1393 for (regno = 0; regno < NUM_REGS; regno++)
1394 mips_fetch_registers (regno);
1398 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1399 /* FP_REGNUM on the mips is a hack which is just supposed to read
1400 zero (see also mips-nat.c). */
1404 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1405 (unsigned int) 0, &err, mips_receive_wait, NULL);
1407 mips_error ("Can't read register %d: %s", regno,
1408 safe_strerror (errno));
1412 char buf[MAX_REGISTER_RAW_SIZE];
1414 /* We got the number the register holds, but gdb expects to see a
1415 value in the target byte ordering. */
1416 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1417 supply_register (regno, buf);
1421 /* Prepare to store registers. The MIPS protocol can store individual
1422 registers, so this function doesn't have to do anything. */
1425 mips_prepare_to_store ()
1429 /* Store remote register(s). */
1432 mips_store_registers (regno)
1439 for (regno = 0; regno < NUM_REGS; regno++)
1440 mips_store_registers (regno);
1444 mips_request ('R', (unsigned int) mips_map_regno (regno),
1445 (unsigned int) read_register (regno),
1446 &err, mips_receive_wait, NULL);
1448 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1451 /* Fetch a word from the target board. */
1454 mips_fetch_word (addr)
1460 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
1461 mips_receive_wait, NULL);
1464 /* Data space failed; try instruction space. */
1465 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
1466 mips_receive_wait, NULL);
1468 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1473 /* Store a word to the target board. Returns errno code or zero for
1474 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1475 memory location there. */
1478 mips_store_word (addr, val, old_contents)
1484 unsigned int oldcontents;
1486 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1488 mips_receive_wait, NULL);
1491 /* Data space failed; try instruction space. */
1492 oldcontents = mips_request ('I', (unsigned int) addr,
1493 (unsigned int) val, &err,
1494 mips_receive_wait, NULL);
1498 if (old_contents != NULL)
1499 store_unsigned_integer (old_contents, 4, oldcontents);
1503 /* Read or write LEN bytes from inferior memory at MEMADDR,
1504 transferring to or from debugger address MYADDR. Write to inferior
1505 if SHOULD_WRITE is nonzero. Returns length of data written or
1506 read; 0 for error. Note that protocol gives us the correct value
1507 for a longword, since it transfers values in ASCII. We want the
1508 byte values, so we have to swap the longword values. */
1511 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1516 struct target_ops *ignore;
1519 /* Round starting address down to longword boundary. */
1520 register CORE_ADDR addr = memaddr &~ 3;
1521 /* Round ending address up; get number of longwords that makes. */
1522 register int count = (((memaddr + len) - addr) + 3) / 4;
1523 /* Allocate buffer of that many longwords. */
1524 register char *buffer = alloca (count * 4);
1530 /* Fill start and end extra bytes of buffer with existing data. */
1531 if (addr != memaddr || len < 4)
1533 /* Need part of initial word -- fetch it. */
1534 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1539 /* Need part of last word -- fetch it. FIXME: we do this even
1540 if we don't need it. */
1541 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1542 mips_fetch_word (addr + (count - 1) * 4));
1545 /* Copy data to be written over corresponding part of buffer */
1547 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1549 /* Write the entire buffer. */
1551 for (i = 0; i < count; i++, addr += 4)
1553 status = mips_store_word (addr,
1554 extract_unsigned_integer (&buffer[i*4], 4),
1556 /* Report each kilobyte (we download 32-bit words at a time) */
1559 printf_unfiltered ("*");
1567 /* FIXME: Do we want a QUIT here? */
1570 printf_unfiltered ("\n");
1574 /* Read all the longwords */
1575 for (i = 0; i < count; i++, addr += 4)
1577 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1581 /* Copy appropriate bytes out of the buffer. */
1582 memcpy (myaddr, buffer + (memaddr & 3), len);
1587 /* Print info on this target. */
1590 mips_files_info (ignore)
1591 struct target_ops *ignore;
1593 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1596 /* Kill the process running on the board. This will actually only
1597 work if we are doing remote debugging over the console input. I
1598 think that if IDT/sim had the remote debug interrupt enabled on the
1599 right port, we could interrupt the process with a break signal. */
1604 if (!mips_wait_flag)
1609 if (interrupt_count >= 2)
1611 interrupt_count = 0;
1613 target_terminal_ours ();
1615 if (query ("Interrupted while waiting for the program.\n\
1616 Give up (and stop debugging it)? "))
1618 /* Clean up in such a way that mips_close won't try to talk to the
1619 board (it almost surely won't work since we weren't able to talk to
1623 SERIAL_CLOSE (mips_desc);
1625 printf_unfiltered ("Ending remote MIPS debugging.\n");
1626 target_mourn_inferior ();
1628 return_to_top_level (RETURN_QUIT);
1631 target_terminal_inferior ();
1634 if (remote_debug > 0)
1635 printf_unfiltered ("Sending break\n");
1637 SERIAL_SEND_BREAK (mips_desc);
1646 SERIAL_WRITE (mips_desc, &cc, 1);
1648 target_mourn_inferior ();
1653 /* Start running on the target board. */
1656 mips_create_inferior (execfile, args, env)
1666 Can't pass arguments to remote MIPS board; arguments ignored.");
1667 /* And don't try to use them on the next "run" command. */
1668 execute_command ("set args", 0);
1671 if (execfile == 0 || exec_bfd == 0)
1672 error ("No executable file specified");
1674 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1676 init_wait_for_inferior ();
1678 /* FIXME: Should we set inferior_pid here? */
1680 /* start-sanitize-gm */
1681 #ifdef GENERAL_MAGIC
1682 magic_create_inferior_hook ();
1683 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
1685 /* end-sanitize-gm */
1686 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1687 /* start-sanitize-gm */
1688 #endif /* GENERAL_MAGIC */
1689 /* end-sanitize-gm */
1692 /* Clean up after a process. Actually nothing to do. */
1695 mips_mourn_inferior ()
1697 unpush_target (&mips_ops);
1698 generic_mourn_inferior ();
1701 /* We can write a breakpoint and read the shadow contents in one
1704 /* The IDT board uses an unusual breakpoint value, and sometimes gets
1705 confused when it sees the usual MIPS breakpoint instruction. */
1707 #define BREAK_INSN (0x00000a0d)
1708 #define BREAK_INSN_SIZE (4)
1710 /* Insert a breakpoint on targets that don't have any better breakpoint
1711 support. We read the contents of the target location and stash it,
1712 then overwrite it with a breakpoint instruction. ADDR is the target
1713 location in the target machine. CONTENTS_CACHE is a pointer to
1714 memory allocated for saving the target contents. It is guaranteed
1715 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1716 is accomplished via BREAKPOINT_MAX). */
1719 mips_insert_breakpoint (addr, contents_cache)
1721 char *contents_cache;
1725 if (monitor_supports_breakpoints)
1726 return common_breakpoint ('B', addr, 0x3, "f");
1728 return mips_store_word (addr, BREAK_INSN, contents_cache);
1732 mips_remove_breakpoint (addr, contents_cache)
1734 char *contents_cache;
1736 if (monitor_supports_breakpoints)
1737 return common_breakpoint ('b', addr, 0, NULL);
1739 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
1742 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
1743 This is used for memory ref breakpoints. */
1745 static unsigned long
1746 calculate_mask (addr, len)
1753 mask = addr ^ (addr + len - 1);
1755 for (i = 32; i >= 0; i--)
1761 mask = (unsigned long) 0xffffffff >> i;
1766 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
1767 for a read watchpoint, or 2 for a read/write watchpoint. */
1770 remote_mips_set_watchpoint (addr, len, type)
1775 CORE_ADDR first_addr;
1779 mask = calculate_mask (addr, len);
1781 first_addr = addr & ~mask;
1791 case 2: /* read/write */
1798 if (common_breakpoint ('B', first_addr, mask, flags))
1805 remote_mips_remove_watchpoint (addr, len, type)
1810 CORE_ADDR first_addr;
1813 mask = calculate_mask (addr, len);
1815 first_addr = addr & ~mask;
1817 if (common_breakpoint ('b', first_addr, 0, NULL))
1824 remote_mips_stopped_by_watchpoint ()
1826 return hit_watchpoint;
1829 /* This routine generates the a breakpoint command of the form:
1831 0x0 <CMD> <ADDR> <MASK> <FLAGS>
1833 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
1834 the address of the breakpoint. <MASK> is a don't care mask for addresses.
1835 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
1838 common_breakpoint (cmd, addr, mask, flags)
1845 char buf[DATA_MAXLEN + 1];
1847 int rpid, rerrflg, rresponse;
1851 sprintf (buf, "0x0 %c 0x%x 0x%x %s", cmd, addr, mask, flags);
1853 sprintf (buf, "0x0 %c 0x%x", cmd, addr);
1855 mips_send_packet (buf, 1);
1857 len = mips_receive_packet (buf, 1, mips_receive_wait);
1859 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
1863 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
1867 if (rresponse != EINVAL)
1868 fprintf_unfiltered (stderr, "common_breakpoint (0x%x): Got error: 0x%x\n",
1877 send_srec (srec, len, addr)
1886 SERIAL_WRITE (mips_desc, srec, len);
1888 ch = mips_readchar (2);
1892 case SERIAL_TIMEOUT:
1893 error ("Timeout during download.");
1897 case 0x15: /* NACK */
1898 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
1901 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
1906 /* Download a binary file by converting it to S records. */
1909 mips_load_srec (args)
1914 char *buffer, srec[1024];
1916 int srec_frame = 200;
1918 static int hashmark = 1;
1920 buffer = alloca (srec_frame * 2 + 256);
1922 abfd = bfd_openr (args, 0);
1925 printf_filtered ("Unable to open file %s\n", args);
1929 if (bfd_check_format (abfd, bfd_object) == 0)
1931 printf_filtered ("File is not an object file\n");
1935 #define LOAD_CMD "load -b -s tty0\015"
1937 SERIAL_WRITE (mips_desc, LOAD_CMD, sizeof LOAD_CMD - 1);
1939 mips_expect (LOAD_CMD);
1940 mips_expect ("\012");
1942 for (s = abfd->sections; s; s = s->next)
1944 if (s->flags & SEC_LOAD)
1948 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
1949 s->vma + s->_raw_size);
1950 gdb_flush (gdb_stdout);
1952 for (i = 0; i < s->_raw_size; i += numbytes)
1954 numbytes = min (srec_frame, s->_raw_size - i);
1956 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1958 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
1959 send_srec (srec, reclen, s->vma + i);
1963 putchar_unfiltered ('#');
1964 gdb_flush (gdb_stdout);
1967 } /* Per-packet (or S-record) loop */
1969 putchar_unfiltered ('\n');
1970 } /* Loadable sections */
1973 putchar_unfiltered ('\n');
1975 /* Write a type 7 terminator record. no data for a type 7, and there
1976 is no data, so len is 0. */
1978 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
1980 send_srec (srec, reclen, abfd->start_address);
1982 SERIAL_FLUSH_INPUT (mips_desc);
1986 * mips_make_srec -- make an srecord. This writes each line, one at a
1987 * time, each with it's own header and trailer line.
1988 * An srecord looks like this:
1990 * byte count-+ address
1991 * start ---+ | | data +- checksum
1993 * S01000006F6B692D746573742E73726563E4
1994 * S315000448600000000000000000FC00005900000000E9
1995 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1996 * S30B0004485A0000000000004E
1999 * S<type><length><address><data><checksum>
2003 * is the number of bytes following upto the checksum. Note that
2004 * this is not the number of chars following, since it takes two
2005 * chars to represent a byte.
2009 * 1) two byte address data record
2010 * 2) three byte address data record
2011 * 3) four byte address data record
2012 * 7) four byte address termination record
2013 * 8) three byte address termination record
2014 * 9) two byte address termination record
2017 * is the start address of the data following, or in the case of
2018 * a termination record, the start address of the image
2022 * is the sum of all the raw byte data in the record, from the length
2023 * upwards, modulo 256 and subtracted from 255.
2025 * This routine returns the length of the S-record.
2030 mips_make_srec (buf, type, memaddr, myaddr, len)
2034 unsigned char *myaddr;
2037 unsigned char checksum;
2040 /* Create the header for the srec. addr_size is the number of bytes in the address,
2041 and 1 is the number of bytes in the count. */
2045 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2046 buf[3] = memaddr >> 24;
2047 buf[4] = memaddr >> 16;
2048 buf[5] = memaddr >> 8;
2050 memcpy (&buf[7], myaddr, len);
2052 /* Note that the checksum is calculated on the raw data, not the hexified
2053 data. It includes the length, address and the data portions of the
2057 buf += 2; /* Point at length byte */
2058 for (i = 0; i < len + 4 + 1; i++)
2066 /* mips_load -- download a file. */
2069 mips_load (file, from_tty)
2075 /* Get the board out of remote debugging mode. */
2077 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
2078 mips_receive_wait, NULL);
2080 if (!mips_expect ("\015\012") || !mips_expect (TARGET_MONITOR_PROMPT))
2081 error ("mips_load: Couldn't get into monitor mode.");
2083 mips_load_srec (file);
2087 /* Finally, make the PC point at the start address */
2090 write_pc (bfd_get_start_address (exec_bfd));
2092 inferior_pid = 0; /* No process now */
2094 /* This is necessary because many things were based on the PC at the time that
2095 we attached to the monitor, which is no longer valid now that we have loaded
2096 new code (and just changed the PC). Another way to do this might be to call
2097 normal_stop, except that the stack may not be valid, and things would get
2098 horribly confused... */
2100 clear_symtab_users ();
2103 /* The target vector. */
2105 struct target_ops mips_ops =
2107 "mips", /* to_shortname */
2108 "Remote MIPS debugging over serial line", /* to_longname */
2110 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2111 The argument is the device it is connected to or, if it contains a colon,\n\
2112 HOST:PORT to access a board over a network", /* to_doc */
2113 mips_open, /* to_open */
2114 mips_close, /* to_close */
2115 NULL, /* to_attach */
2116 mips_detach, /* to_detach */
2117 mips_resume, /* to_resume */
2118 mips_wait, /* to_wait */
2119 mips_fetch_registers, /* to_fetch_registers */
2120 mips_store_registers, /* to_store_registers */
2121 mips_prepare_to_store, /* to_prepare_to_store */
2122 mips_xfer_memory, /* to_xfer_memory */
2123 mips_files_info, /* to_files_info */
2124 mips_insert_breakpoint, /* to_insert_breakpoint */
2125 mips_remove_breakpoint, /* to_remove_breakpoint */
2126 NULL, /* to_terminal_init */
2127 NULL, /* to_terminal_inferior */
2128 NULL, /* to_terminal_ours_for_output */
2129 NULL, /* to_terminal_ours */
2130 NULL, /* to_terminal_info */
2131 mips_kill, /* to_kill */
2132 mips_load, /* to_load */
2133 NULL, /* to_lookup_symbol */
2134 mips_create_inferior, /* to_create_inferior */
2135 mips_mourn_inferior, /* to_mourn_inferior */
2136 NULL, /* to_can_run */
2137 NULL, /* to_notice_signals */
2138 0, /* to_thread_alive */
2140 process_stratum, /* to_stratum */
2142 1, /* to_has_all_memory */
2143 1, /* to_has_memory */
2144 1, /* to_has_stack */
2145 1, /* to_has_registers */
2146 1, /* to_has_execution */
2147 NULL, /* sections */
2148 NULL, /* sections_end */
2149 OPS_MAGIC /* to_magic */
2153 _initialize_remote_mips ()
2155 add_target (&mips_ops);
2158 add_set_cmd ("timeout", no_class, var_zinteger,
2159 (char *) &mips_receive_wait,
2160 "Set timeout in seconds for remote MIPS serial I/O.",
2165 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
2166 (char *) &mips_retransmit_wait,
2167 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
2168 This is the number of seconds to wait for an acknowledgement to a packet\n\
2169 before resending the packet.", &setlist),