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 void mips_set_processor_type_command
PARAMS ((char *, int));
43 /* Prototypes for local functions. */
45 static int mips_readchar
PARAMS ((int timeout
));
47 static int mips_receive_header
PARAMS ((unsigned char *hdr
, int *pgarbage
,
48 int ch
, int timeout
));
50 static int mips_receive_trailer
PARAMS ((unsigned char *trlr
, int *pgarbage
,
51 int *pch
, int timeout
));
53 static int mips_cksum
PARAMS ((const unsigned char *hdr
,
54 const unsigned char *data
,
57 static void mips_send_packet
PARAMS ((const char *s
, int get_ack
));
59 static void mips_send_command
PARAMS ((const char *cmd
, int prompt
));
61 static int mips_receive_packet
PARAMS ((char *buff
, int throw_error
,
64 static CORE_ADDR mips_request
PARAMS ((int cmd
, CORE_ADDR addr
,
65 CORE_ADDR 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 pmon_open
PARAMS ((char *name
, int from_tty
));
74 static void ddb_open
PARAMS ((char *name
, int from_tty
));
76 static void mips_close
PARAMS ((int quitting
));
78 static void mips_detach
PARAMS ((char *args
, int from_tty
));
80 static void mips_resume
PARAMS ((int pid
, int step
,
81 enum target_signal siggnal
));
83 static int mips_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
85 static int pmon_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
87 static int mips_map_regno
PARAMS ((int regno
));
89 static void mips_fetch_registers
PARAMS ((int regno
));
91 static void mips_prepare_to_store
PARAMS ((void));
93 static void mips_store_registers
PARAMS ((int regno
));
95 static unsigned int mips_fetch_word
PARAMS ((CORE_ADDR addr
));
97 static int mips_store_word
PARAMS ((CORE_ADDR addr
, unsigned int value
,
100 static int mips_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
,
101 int write
, struct target_ops
*ignore
));
103 static void mips_files_info
PARAMS ((struct target_ops
*ignore
));
105 static void mips_create_inferior
PARAMS ((char *execfile
, char *args
,
108 static void mips_mourn_inferior
PARAMS ((void));
110 static int pmon_makeb64
PARAMS ((unsigned long v
, char *p
, int n
, int *chksum
));
112 static int pmon_zeroset
PARAMS ((int recsize
, char **buff
, int *amount
,
113 unsigned int *chksum
));
115 static int pmon_checkset
PARAMS ((int recsize
, char **buff
, int *value
));
117 static void pmon_make_fastrec
PARAMS ((char **outbuf
, unsigned char *inbuf
,
118 int *inptr
, int inamount
, int *recsize
,
119 unsigned int *csum
, unsigned int *zerofill
));
121 static int pmon_check_ack
PARAMS ((void));
123 static void pmon_load_fast
PARAMS ((char *file
));
125 static void mips_load
PARAMS ((char *file
, int from_tty
));
127 static int mips_make_srec
PARAMS ((char *buffer
, int type
, CORE_ADDR memaddr
,
128 unsigned char *myaddr
, int len
));
130 static int common_breakpoint
PARAMS ((int cmd
, CORE_ADDR addr
, CORE_ADDR mask
,
133 static void common_open
PARAMS ((struct target_ops
*ops
, char *name
,
135 /* Forward declarations. */
136 extern struct target_ops mips_ops
;
137 extern struct target_ops pmon_ops
;
138 extern struct target_ops ddb_ops
;
140 /* The MIPS remote debugging interface is built on top of a simple
141 packet protocol. Each packet is organized as follows:
143 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
144 may not appear anywhere else in the packet. Any time a SYN is
145 seen, a new packet should be assumed to have begun.
148 This byte contains the upper five bits of the logical length
149 of the data section, plus a single bit indicating whether this
150 is a data packet or an acknowledgement. The documentation
151 indicates that this bit is 1 for a data packet, but the actual
152 board uses 1 for an acknowledgement. The value of the byte is
153 0x40 + (ack ? 0x20 : 0) + (len >> 6)
154 (we always have 0 <= len < 1024). Acknowledgement packets do
155 not carry data, and must have a data length of 0.
157 LEN1 This byte contains the lower six bits of the logical length of
158 the data section. The value is
161 SEQ This byte contains the six bit sequence number of the packet.
164 An acknowlegment packet contains the sequence number of the
165 packet being acknowledged plus 1 modulo 64. Data packets are
166 transmitted in sequence. There may only be one outstanding
167 unacknowledged data packet at a time. The sequence numbers
168 are independent in each direction. If an acknowledgement for
169 the previous packet is received (i.e., an acknowledgement with
170 the sequence number of the packet just sent) the packet just
171 sent should be retransmitted. If no acknowledgement is
172 received within a timeout period, the packet should be
173 retransmitted. This has an unfortunate failure condition on a
174 high-latency line, as a delayed acknowledgement may lead to an
175 endless series of duplicate packets.
177 DATA The actual data bytes follow. The following characters are
178 escaped inline with DLE (ASCII 020, or ^P):
184 The additional DLE characters are not counted in the logical
185 length stored in the TYPE_LEN and LEN1 bytes.
190 These bytes contain an 18 bit checksum of the complete
191 contents of the packet excluding the SEQ byte and the
192 CSUM[123] bytes. The checksum is simply the twos complement
193 addition of all the bytes treated as unsigned characters. The
194 values of the checksum bytes are:
195 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
196 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
197 CSUM3: 0x40 + (cksum & 0x3f)
199 It happens that the MIPS remote debugging protocol always
200 communicates with ASCII strings. Because of this, this
201 implementation doesn't bother to handle the DLE quoting mechanism,
202 since it will never be required. */
204 /* The SYN character which starts each packet. */
207 /* The 0x40 used to offset each packet (this value ensures that all of
208 the header and trailer bytes, other than SYN, are printable ASCII
210 #define HDR_OFFSET 0x40
212 /* The indices of the bytes in the packet header. */
213 #define HDR_INDX_SYN 0
214 #define HDR_INDX_TYPE_LEN 1
215 #define HDR_INDX_LEN1 2
216 #define HDR_INDX_SEQ 3
219 /* The data/ack bit in the TYPE_LEN header byte. */
220 #define TYPE_LEN_DA_BIT 0x20
221 #define TYPE_LEN_DATA 0
222 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
224 /* How to compute the header bytes. */
225 #define HDR_SET_SYN(data, len, seq) (SYN)
226 #define HDR_SET_TYPE_LEN(data, len, seq) \
228 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
229 + (((len) >> 6) & 0x1f))
230 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
231 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
233 /* Check that a header byte is reasonable. */
234 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
236 /* Get data from the header. These macros evaluate their argument
238 #define HDR_IS_DATA(hdr) \
239 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
240 #define HDR_GET_LEN(hdr) \
241 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
242 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
244 /* The maximum data length. */
245 #define DATA_MAXLEN 1023
247 /* The trailer offset. */
248 #define TRLR_OFFSET HDR_OFFSET
250 /* The indices of the bytes in the packet trailer. */
251 #define TRLR_INDX_CSUM1 0
252 #define TRLR_INDX_CSUM2 1
253 #define TRLR_INDX_CSUM3 2
254 #define TRLR_LENGTH 3
256 /* How to compute the trailer bytes. */
257 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
258 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
259 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
261 /* Check that a trailer byte is reasonable. */
262 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
264 /* Get data from the trailer. This evaluates its argument multiple
266 #define TRLR_GET_CKSUM(trlr) \
267 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
268 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
269 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
271 /* The sequence number modulos. */
272 #define SEQ_MODULOS (64)
274 enum mips_monitor_type
{
275 /* IDT/SIM monitor being used: */
277 /* PMON monitor being used: */
278 MON_PMON
, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */
279 MON_DDB
, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */
280 /* Last and unused value, for sizing vectors, etc. */
283 static enum mips_monitor_type mips_monitor
= MON_LAST
;
285 /* The default monitor prompt text: */
286 static char *mips_monitor_prompt
= TARGET_MONITOR_PROMPT
;
287 /* For the Cogent PMON world this is still not ideal. The default
288 prompt is "PMON> ", unfortunately the user can change the prompt
289 and the new prompt will survive over a power-cycle (EEPROM). This
290 means that the code should really force the monitor prompt to a
291 known value as the very first action, and that the
292 "mips_monitor_prompt" support is not needed... since the prompt
293 could be explicitly set to TARGET_MONITOR_PROMPT (even though it
294 may be the prompt for a different monitor). However, this will
295 require changing the mips_initialize reset sequence. (TODO) */
297 /* Set to 1 if the target is open. */
298 static int mips_is_open
;
300 /* Currently active target description (if mips_is_open == 1) */
301 static struct target_ops
*current_ops
;
303 /* Set to 1 while the connection is being initialized. */
304 static int mips_initializing
;
306 /* The next sequence number to send. */
307 static unsigned int mips_send_seq
;
309 /* The next sequence number we expect to receive. */
310 static unsigned int mips_receive_seq
;
312 /* The time to wait before retransmitting a packet, in seconds. */
313 static int mips_retransmit_wait
= 3;
315 /* The number of times to try retransmitting a packet before giving up. */
316 static int mips_send_retries
= 10;
318 /* The number of garbage characters to accept when looking for an
319 SYN for the next packet. */
320 static int mips_syn_garbage
= 1050;
322 /* The time to wait for a packet, in seconds. */
323 static int mips_receive_wait
= 5;
325 /* Set if we have sent a packet to the board but have not yet received
327 static int mips_need_reply
= 0;
329 /* Handle used to access serial I/O stream. */
330 static serial_t mips_desc
;
332 /* Counts the number of times the user tried to interrupt the target (usually
334 static int interrupt_count
;
336 /* If non-zero, means that the target is running. */
337 static int mips_wait_flag
= 0;
339 /* If non-zero, monitor supports breakpoint commands. */
340 static monitor_supports_breakpoints
= 0;
342 /* Data cache header. */
344 static DCACHE
*mips_dcache
;
346 /* Non-zero means that we've just hit a read or write watchpoint */
347 static int hit_watchpoint
;
349 /* Handle low-level error that we can't recover from. Note that just
350 error()ing out from target_wait or some such low-level place will cause
351 all hell to break loose--the rest of GDB will tend to get left in an
352 inconsistent state. */
355 #ifdef ANSI_PROTOTYPES
356 mips_error (char *string
, ...)
358 mips_error (va_alist
)
364 #ifdef ANSI_PROTOTYPES
365 va_start (args
, string
);
369 string
= va_arg (args
, char *);
372 target_terminal_ours ();
373 wrap_here(""); /* Force out any buffered output */
374 gdb_flush (gdb_stdout
);
376 fprintf_filtered (gdb_stderr
, error_pre_print
);
377 vfprintf_filtered (gdb_stderr
, string
, args
);
378 fprintf_filtered (gdb_stderr
, "\n");
380 gdb_flush (gdb_stderr
);
382 /* Clean up in such a way that mips_close won't try to talk to the
383 board (it almost surely won't work since we weren't able to talk to
386 SERIAL_CLOSE (mips_desc
);
388 printf_unfiltered ("Ending remote MIPS debugging.\n");
389 target_mourn_inferior ();
391 return_to_top_level (RETURN_ERROR
);
394 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
408 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
409 were waiting for the mips_monitor_prompt... */
411 c
= SERIAL_READCHAR (mips_desc
, 2);
413 if (c
== SERIAL_TIMEOUT
)
433 /* Read the required number of characters into the given buffer (which
434 is assumed to be large enough). The only failure is a timeout. */
436 mips_getstring (string
, n
)
446 c
= SERIAL_READCHAR (mips_desc
, 2);
448 if (c
== SERIAL_TIMEOUT
) {
449 fprintf_unfiltered (stderr
, "Failed to read %d characters from target (TIMEOUT)\n", n
);
460 /* Read a character from the remote, aborting on error. Returns
461 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
462 returns). FIXME: If we see the string mips_monitor_prompt from
463 the board, then we are debugging on the main console port, and we
464 have somehow dropped out of remote debugging mode. In this case,
465 we automatically go back in to remote debugging mode. This is a
466 hack, put in because I can't find any way for a program running on
467 the remote board to terminate without also ending remote debugging
468 mode. I assume users won't have any trouble with this; for one
469 thing, the IDT documentation generally assumes that the remote
470 debugging port is not the console port. This is, however, very
471 convenient for DejaGnu when you only have one connected serial
475 mips_readchar (timeout
)
479 static int state
= 0;
480 static int mips_monitor_prompt_len
= -1;
482 /* NASTY, since we assume that the prompt does not change after the
483 first mips_readchar call: */
484 if (mips_monitor_prompt_len
= -1)
485 mips_monitor_prompt_len
= strlen(mips_monitor_prompt
);
487 #ifdef MAINTENANCE_CMDS
492 if (i
== -1 && watchdog
> 0)
497 if (state
== mips_monitor_prompt_len
)
499 ch
= SERIAL_READCHAR (mips_desc
, timeout
);
500 #ifdef MAINTENANCE_CMDS
501 if (ch
== SERIAL_TIMEOUT
&& timeout
== -1) /* Watchdog went off */
503 target_mourn_inferior ();
504 error ("Watchdog has expired. Target detached.\n");
507 if (ch
== SERIAL_EOF
)
508 mips_error ("End of file from remote");
509 if (ch
== SERIAL_ERROR
)
510 mips_error ("Error reading from remote: %s", safe_strerror (errno
));
511 if (remote_debug
> 1)
513 /* Don't use _filtered; we can't deal with a QUIT out of
514 target_wait, and I think this might be called from there. */
515 if (ch
!= SERIAL_TIMEOUT
)
516 printf_unfiltered ("Read '%c' %d 0x%x\n", ch
, ch
, ch
);
518 printf_unfiltered ("Timed out in read\n");
521 /* If we have seen mips_monitor_prompt and we either time out, or
522 we see a @ (which was echoed from a packet we sent), reset the
523 board as described above. The first character in a packet after
524 the SYN (which is not echoed) is always an @ unless the packet is
525 more than 64 characters long, which ours never are. */
526 if ((ch
== SERIAL_TIMEOUT
|| ch
== '@')
527 && state
== mips_monitor_prompt_len
528 && ! mips_initializing
)
530 if (remote_debug
> 0)
531 /* Don't use _filtered; we can't deal with a QUIT out of
532 target_wait, and I think this might be called from there. */
533 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
540 /* At this point, about the only thing we can do is abort the command
541 in progress and get back to command level as quickly as possible. */
543 error ("Remote board reset, debug protocol re-initialized.");
546 if (ch
== mips_monitor_prompt
[state
])
554 /* Get a packet header, putting the data in the supplied buffer.
555 PGARBAGE is a pointer to the number of garbage characters received
556 so far. CH is the last character received. Returns 0 for success,
557 or -1 for timeout. */
560 mips_receive_header (hdr
, pgarbage
, ch
, timeout
)
570 /* Wait for a SYN. mips_syn_garbage is intended to prevent
571 sitting here indefinitely if the board sends us one garbage
572 character per second. ch may already have a value from the
573 last time through the loop. */
576 ch
= mips_readchar (timeout
);
577 if (ch
== SERIAL_TIMEOUT
)
581 /* Printing the character here lets the user of gdb see
582 what the program is outputting, if the debugging is
583 being done on the console port. Don't use _filtered;
584 we can't deal with a QUIT out of target_wait. */
585 if (! mips_initializing
|| remote_debug
> 0)
587 /* Note that the host's idea of newline may not
588 correspond to the target's idea, so recognize
589 newline by its actual ASCII code, but write it
590 out using the \n notation. */
591 if (ch
< 0x20 && ch
!= '\012')
593 putchar_unfiltered ('^');
594 putchar_unfiltered (ch
+ 0x40);
596 else if (ch
== '\012')
597 putchar_unfiltered ('\n');
599 putchar_unfiltered (ch
);
600 gdb_flush (gdb_stdout
);
604 if (mips_syn_garbage
> 0
605 && *pgarbage
> mips_syn_garbage
)
606 mips_error ("Debug protocol failure: more than %d characters before a sync.",
611 /* Get the packet header following the SYN. */
612 for (i
= 1; i
< HDR_LENGTH
; i
++)
614 ch
= mips_readchar (timeout
);
615 if (ch
== SERIAL_TIMEOUT
)
617 /* Make sure this is a header byte. */
618 if (ch
== SYN
|| ! HDR_CHECK (ch
))
624 /* If we got the complete header, we can return. Otherwise we
625 loop around and keep looking for SYN. */
631 /* Get a packet header, putting the data in the supplied buffer.
632 PGARBAGE is a pointer to the number of garbage characters received
633 so far. The last character read is returned in *PCH. Returns 0
634 for success, -1 for timeout, -2 for error. */
637 mips_receive_trailer (trlr
, pgarbage
, pch
, timeout
)
646 for (i
= 0; i
< TRLR_LENGTH
; i
++)
648 ch
= mips_readchar (timeout
);
650 if (ch
== SERIAL_TIMEOUT
)
652 if (! TRLR_CHECK (ch
))
659 /* Get the checksum of a packet. HDR points to the packet header.
660 DATA points to the packet data. LEN is the length of DATA. */
663 mips_cksum (hdr
, data
, len
)
664 const unsigned char *hdr
;
665 const unsigned char *data
;
668 register const unsigned char *p
;
674 /* The initial SYN is not included in the checksum. */
688 /* Send a packet containing the given ASCII string. */
691 mips_send_packet (s
, get_ack
)
696 unsigned char *packet
;
701 if (len
> DATA_MAXLEN
)
702 mips_error ("MIPS protocol data packet too long: %s", s
);
704 packet
= (unsigned char *) alloca (HDR_LENGTH
+ len
+ TRLR_LENGTH
+ 1);
706 packet
[HDR_INDX_SYN
] = HDR_SET_SYN (1, len
, mips_send_seq
);
707 packet
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (1, len
, mips_send_seq
);
708 packet
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (1, len
, mips_send_seq
);
709 packet
[HDR_INDX_SEQ
] = HDR_SET_SEQ (1, len
, mips_send_seq
);
711 memcpy (packet
+ HDR_LENGTH
, s
, len
);
713 cksum
= mips_cksum (packet
, packet
+ HDR_LENGTH
, len
);
714 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
715 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
716 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
718 /* Increment the sequence number. This will set mips_send_seq to
719 the sequence number we expect in the acknowledgement. */
720 mips_send_seq
= (mips_send_seq
+ 1) % SEQ_MODULOS
;
722 /* We can only have one outstanding data packet, so we just wait for
723 the acknowledgement here. Keep retransmitting the packet until
724 we get one, or until we've tried too many times. */
725 for (try = 0; try < mips_send_retries
; try++)
730 if (remote_debug
> 0)
732 /* Don't use _filtered; we can't deal with a QUIT out of
733 target_wait, and I think this might be called from there. */
734 packet
[HDR_LENGTH
+ len
+ TRLR_LENGTH
] = '\0';
735 printf_unfiltered ("Writing \"%s\"\n", packet
+ 1);
738 if (SERIAL_WRITE (mips_desc
, packet
,
739 HDR_LENGTH
+ len
+ TRLR_LENGTH
) != 0)
740 mips_error ("write to target failed: %s", safe_strerror (errno
));
749 unsigned char hdr
[HDR_LENGTH
+ 1];
750 unsigned char trlr
[TRLR_LENGTH
+ 1];
754 /* Get the packet header. If we time out, resend the data
756 err
= mips_receive_header (hdr
, &garbage
, ch
, mips_retransmit_wait
);
762 /* If we get a data packet, assume it is a duplicate and
763 ignore it. FIXME: If the acknowledgement is lost, this
764 data packet may be the packet the remote sends after the
766 if (HDR_IS_DATA (hdr
)) {
769 /* Ignore any errors raised whilst attempting to ignore
772 len
= HDR_GET_LEN (hdr
);
774 for (i
= 0; i
< len
; i
++)
778 rch
= mips_readchar (2);
784 if (rch
== SERIAL_TIMEOUT
)
786 /* ignore the character */
790 (void) mips_receive_trailer (trlr
, &garbage
, &ch
, 2);
792 /* We don't bother checking the checksum, or providing an
793 ACK to the packet. */
797 /* If the length is not 0, this is a garbled packet. */
798 if (HDR_GET_LEN (hdr
) != 0)
801 /* Get the packet trailer. */
802 err
= mips_receive_trailer (trlr
, &garbage
, &ch
,
803 mips_retransmit_wait
);
805 /* If we timed out, resend the data packet. */
809 /* If we got a bad character, reread the header. */
813 /* If the checksum does not match the trailer checksum, this
814 is a bad packet; ignore it. */
815 if (mips_cksum (hdr
, (unsigned char *) NULL
, 0)
816 != TRLR_GET_CKSUM (trlr
))
819 if (remote_debug
> 0)
821 hdr
[HDR_LENGTH
] = '\0';
822 trlr
[TRLR_LENGTH
] = '\0';
823 /* Don't use _filtered; we can't deal with a QUIT out of
824 target_wait, and I think this might be called from there. */
825 printf_unfiltered ("Got ack %d \"%s%s\"\n",
826 HDR_GET_SEQ (hdr
), hdr
+ 1, trlr
);
829 /* If this ack is for the current packet, we're done. */
830 seq
= HDR_GET_SEQ (hdr
);
831 if (seq
== mips_send_seq
)
834 /* If this ack is for the last packet, resend the current
836 if ((seq
+ 1) % SEQ_MODULOS
== mips_send_seq
)
839 /* Otherwise this is a bad ack; ignore it. Increment the
840 garbage count to ensure that we do not stay in this loop
846 mips_error ("Remote did not acknowledge packet");
849 /* Receive and acknowledge a packet, returning the data in BUFF (which
850 should be DATA_MAXLEN + 1 bytes). The protocol documentation
851 implies that only the sender retransmits packets, so this code just
852 waits silently for a packet. It returns the length of the received
853 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
854 don't print an error message and return -1. */
857 mips_receive_packet (buff
, throw_error
, timeout
)
865 unsigned char ack
[HDR_LENGTH
+ TRLR_LENGTH
+ 1];
872 unsigned char hdr
[HDR_LENGTH
];
873 unsigned char trlr
[TRLR_LENGTH
];
877 if (mips_receive_header (hdr
, &garbage
, ch
, timeout
) != 0)
880 mips_error ("Timed out waiting for remote packet");
887 /* An acknowledgement is probably a duplicate; ignore it. */
888 if (! HDR_IS_DATA (hdr
))
890 len
= HDR_GET_LEN (hdr
);
891 /* Check if the length is valid for an ACK, we may aswell
892 try and read the remainder of the packet: */
895 /* Ignore the error condition, since we are going to
896 ignore the packet anyway. */
897 (void) mips_receive_trailer (trlr
, &garbage
, &ch
, timeout
);
899 /* Don't use _filtered; we can't deal with a QUIT out of
900 target_wait, and I think this might be called from there. */
901 if (remote_debug
> 0)
902 printf_unfiltered ("Ignoring unexpected ACK\n");
906 len
= HDR_GET_LEN (hdr
);
907 for (i
= 0; i
< len
; i
++)
911 rch
= mips_readchar (timeout
);
917 if (rch
== SERIAL_TIMEOUT
)
920 mips_error ("Timed out waiting for remote packet");
929 /* Don't use _filtered; we can't deal with a QUIT out of
930 target_wait, and I think this might be called from there. */
931 if (remote_debug
> 0)
932 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
937 err
= mips_receive_trailer (trlr
, &garbage
, &ch
, timeout
);
941 mips_error ("Timed out waiting for packet");
947 /* Don't use _filtered; we can't deal with a QUIT out of
948 target_wait, and I think this might be called from there. */
949 if (remote_debug
> 0)
950 printf_unfiltered ("Got SYN when wanted trailer\n");
954 /* If this is the wrong sequence number, ignore it. */
955 if (HDR_GET_SEQ (hdr
) != mips_receive_seq
)
957 /* Don't use _filtered; we can't deal with a QUIT out of
958 target_wait, and I think this might be called from there. */
959 if (remote_debug
> 0)
960 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
961 HDR_GET_SEQ (hdr
), mips_receive_seq
);
965 if (mips_cksum (hdr
, buff
, len
) == TRLR_GET_CKSUM (trlr
))
968 if (remote_debug
> 0)
969 /* Don't use _filtered; we can't deal with a QUIT out of
970 target_wait, and I think this might be called from there. */
971 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
972 mips_cksum (hdr
, buff
, len
),
973 TRLR_GET_CKSUM (trlr
));
975 /* The checksum failed. Send an acknowledgement for the
976 previous packet to tell the remote to resend the packet. */
977 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
978 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
979 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
980 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
982 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
984 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
985 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
986 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
988 if (remote_debug
> 0)
990 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
991 /* Don't use _filtered; we can't deal with a QUIT out of
992 target_wait, and I think this might be called from there. */
993 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
997 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
) != 0)
1000 mips_error ("write to target failed: %s", safe_strerror (errno
));
1006 if (remote_debug
> 0)
1009 /* Don't use _filtered; we can't deal with a QUIT out of
1010 target_wait, and I think this might be called from there. */
1011 printf_unfiltered ("Got packet \"%s\"\n", buff
);
1014 /* We got the packet. Send an acknowledgement. */
1015 mips_receive_seq
= (mips_receive_seq
+ 1) % SEQ_MODULOS
;
1017 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
1018 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
1019 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
1020 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
1022 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
1024 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
1025 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
1026 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
1028 if (remote_debug
> 0)
1030 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
1031 /* Don't use _filtered; we can't deal with a QUIT out of
1032 target_wait, and I think this might be called from there. */
1033 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
1037 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
) != 0)
1040 mips_error ("write to target failed: %s", safe_strerror (errno
));
1048 /* Optionally send a request to the remote system and optionally wait
1049 for the reply. This implements the remote debugging protocol,
1050 which is built on top of the packet protocol defined above. Each
1051 request has an ADDR argument and a DATA argument. The following
1052 requests are defined:
1054 \0 don't send a request; just wait for a reply
1055 i read word from instruction space at ADDR
1056 d read word from data space at ADDR
1057 I write DATA to instruction space at ADDR
1058 D write DATA to data space at ADDR
1059 r read register number ADDR
1060 R set register number ADDR to value DATA
1061 c continue execution (if ADDR != 1, set pc to ADDR)
1062 s single step (if ADDR != 1, set pc to ADDR)
1064 The read requests return the value requested. The write requests
1065 return the previous value in the changed location. The execution
1066 requests return a UNIX wait value (the approximate signal which
1067 caused execution to stop is in the upper eight bits).
1069 If PERR is not NULL, this function waits for a reply. If an error
1070 occurs, it sets *PERR to 1 and sets errno according to what the
1071 target board reports. */
1074 mips_request (cmd
, addr
, data
, perr
, timeout
, buff
)
1082 char myBuff
[DATA_MAXLEN
+ 1];
1089 if (buff
== (char *) NULL
)
1094 if (mips_need_reply
)
1095 fatal ("mips_request: Trying to send command before reply");
1096 sprintf (buff
, "0x0 %c 0x%s 0x%s", cmd
, paddr(addr
), paddr(data
));
1097 mips_send_packet (buff
, 1);
1098 mips_need_reply
= 1;
1101 if (perr
== (int *) NULL
)
1104 if (! mips_need_reply
)
1105 fatal ("mips_request: Trying to get reply before command");
1107 mips_need_reply
= 0;
1109 len
= mips_receive_packet (buff
, 1, timeout
);
1112 if (sscanf (buff
, "0x%x %c 0x%x 0x%x",
1113 &rpid
, &rcmd
, &rerrflg
, &rresponse
) != 4
1114 || (cmd
!= '\0' && rcmd
!= cmd
))
1115 mips_error ("Bad response from remote board");
1121 /* FIXME: This will returns MIPS errno numbers, which may or may
1122 not be the same as errno values used on other systems. If
1123 they stick to common errno values, they will be the same, but
1124 if they don't, they must be translated. */
1135 mips_initialize_cleanups (arg
)
1138 mips_initializing
= 0;
1142 mips_send_command (cmd
, prompt
)
1146 SERIAL_WRITE (mips_desc
, cmd
, strlen(cmd
));
1148 mips_expect ("\012");
1150 mips_expect (mips_monitor_prompt
);
1153 /* Enter remote (dbx) debug mode: */
1157 /* Reset the sequence numbers, ready for the new debug sequence: */
1159 mips_receive_seq
= 0;
1161 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
1162 mips_send_command ("debug\015", 0);
1163 else /* assume IDT monitor by default */
1164 mips_send_command ("db tty0\015", 0);
1166 SERIAL_WRITE (mips_desc
, "\015", sizeof "\015" - 1);
1168 /* We don't need to absorb any spurious characters here, since the
1169 mips_receive_header will eat up a reasonable number of characters
1170 whilst looking for the SYN, however this avoids the "garbage"
1171 being displayed to the user. */
1172 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
1173 mips_expect ("\015");
1176 char buff
[DATA_MAXLEN
+ 1];
1177 if (mips_receive_packet (buff
, 1, 3) < 0)
1178 mips_error ("Failed to initialize (didn't receive packet).");
1182 /* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1188 if (mips_monitor
== MON_DDB
)
1190 /* The Ddb version of PMON exits immediately, so we do not get
1191 a reply to this command: */
1192 mips_request ('x', (unsigned int) 0, (unsigned int) 0, NULL
,
1193 mips_receive_wait
, NULL
);
1194 mips_need_reply
= 0;
1195 if (!mips_expect (" break!"))
1199 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err
,
1200 mips_receive_wait
, NULL
);
1202 if (mips_monitor
== MON_PMON
&& !mips_expect ("Exiting remote debug mode"))
1205 if (mips_monitor
== MON_DDB
)
1207 if (!mips_expect ("\012"))
1211 if (!mips_expect ("\015\012"))
1214 if (!mips_expect (mips_monitor_prompt
))
1220 /* Initialize a new connection to the MIPS board, and make sure we are
1221 really connected. */
1227 struct cleanup
*old_cleanups
= make_cleanup (mips_initialize_cleanups
, NULL
);
1230 /* What is this code doing here? I don't see any way it can happen, and
1231 it might mean mips_initializing didn't get cleared properly.
1232 So I'll make it a warning. */
1234 if (mips_initializing
)
1236 warning ("internal error: mips_initialize called twice");
1241 mips_initializing
= 1;
1243 /* At this point, the packit protocol isn't responding. We'll try getting
1244 into the monitor, and restarting the protocol. */
1246 /* Force the system into the monitor. After this we *should* be at
1247 the mips_monitor_prompt. */
1248 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
1249 j
= 0; /* start by checking if we are already at the prompt */
1251 j
= 1; /* start by sending a break */
1256 case 0: /* First, try sending a CR */
1257 SERIAL_FLUSH_INPUT (mips_desc
);
1258 SERIAL_WRITE (mips_desc
, "\015", 1);
1260 case 1: /* First, try sending a break */
1261 SERIAL_SEND_BREAK (mips_desc
);
1263 case 2: /* Then, try a ^C */
1264 SERIAL_WRITE (mips_desc
, "\003", 1);
1266 case 3: /* Then, try escaping from download */
1268 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
1272 /* We shouldn't need to send multiple termination
1273 sequences, since the target performs line (or
1274 block) reads, and then processes those
1275 packets. In-case we were downloading a large packet
1276 we flush the output buffer before inserting a
1277 termination sequence. */
1278 SERIAL_FLUSH_OUTPUT (mips_desc
);
1279 sprintf (tbuff
, "\015/E/E\015");
1280 SERIAL_WRITE (mips_desc
, tbuff
, 6);
1287 /* We are possibly in binary download mode, having
1288 aborted in the middle of an S-record. ^C won't
1289 work because of binary mode. The only reliable way
1290 out is to send enough termination packets (8 bytes)
1291 to fill up and then overflow the largest size
1292 S-record (255 bytes in this case). This amounts to
1296 mips_make_srec (srec
, '7', 0, NULL
, 0);
1298 for (i
= 1; i
<= 33; i
++)
1300 SERIAL_WRITE (mips_desc
, srec
, 8);
1302 if (SERIAL_READCHAR (mips_desc
, 0) >= 0)
1303 break; /* Break immediatly if we get something from
1310 mips_error ("Failed to initialize.");
1313 if (mips_expect (mips_monitor_prompt
))
1317 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
1319 /* Ensure the correct target state: */
1320 mips_send_command ("set regsize 64\015", -1);
1321 mips_send_command ("set hostport tty0\015", -1);
1322 mips_send_command ("set brkcmd \"\"\015", -1);
1323 /* Delete all the current breakpoints: */
1324 mips_send_command ("db *\015", -1);
1325 /* NOTE: PMON does not have breakpoint support through the
1326 "debug" mode, only at the monitor command-line. */
1329 mips_enter_debug ();
1331 /* Clear all breakpoints: */
1332 if (common_breakpoint ('b', -1, 0, NULL
))
1333 monitor_supports_breakpoints
= 0;
1335 monitor_supports_breakpoints
= 1;
1337 do_cleanups (old_cleanups
);
1339 /* If this doesn't call error, we have connected; we don't care if
1340 the request itself succeeds or fails. */
1342 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err
,
1343 mips_receive_wait
, NULL
);
1344 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1345 select_frame (get_current_frame (), 0);
1348 /* Open a connection to the remote board. */
1350 common_open (ops
, name
, from_tty
)
1351 struct target_ops
*ops
;
1359 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1360 device is attached to the target board (e.g., /dev/ttya).");
1362 target_preopen (from_tty
);
1365 unpush_target (current_ops
);
1367 mips_desc
= SERIAL_OPEN (name
);
1368 if (mips_desc
== (serial_t
) NULL
)
1369 perror_with_name (name
);
1371 if (baud_rate
!= -1)
1373 if (SERIAL_SETBAUDRATE (mips_desc
, baud_rate
))
1375 SERIAL_CLOSE (mips_desc
);
1376 perror_with_name (name
);
1380 SERIAL_RAW (mips_desc
);
1388 printf_unfiltered ("Remote MIPS debugging using %s\n", name
);
1390 /* Switch to using remote target now. */
1393 /* FIXME: Should we call start_remote here? */
1395 /* Try to figure out the processor model if possible. */
1396 ptype
= mips_read_processor_type ();
1398 mips_set_processor_type_command (strsave (ptype
), 0);
1400 /* This is really the job of start_remote however, that makes an assumption
1401 that the target is about to print out a status message of some sort. That
1402 doesn't happen here (in fact, it may not be possible to get the monitor to
1403 send the appropriate packet). */
1405 flush_cached_frames ();
1406 registers_changed ();
1407 stop_pc
= read_pc ();
1408 set_current_frame (create_new_frame (read_fp (), stop_pc
));
1409 select_frame (get_current_frame (), 0);
1410 print_stack_frame (selected_frame
, -1, 1);
1414 mips_open (name
, from_tty
)
1418 mips_monitor
= MON_IDT
;
1419 common_open (&mips_ops
, name
, from_tty
);
1423 pmon_open (name
, from_tty
)
1427 /* The PMON monitor has a prompt different from the default
1428 "TARGET_MONITOR_PROMPT": */
1429 mips_monitor_prompt
= "PMON> ";
1430 mips_monitor
= MON_PMON
;
1431 common_open (&pmon_ops
, name
, from_tty
);
1435 ddb_open (name
, from_tty
)
1439 /* The PMON monitor has a prompt different from the default
1440 "TARGET_MONITOR_PROMPT": */
1441 mips_monitor_prompt
= "NEC010>";
1442 mips_monitor
= MON_DDB
;
1443 common_open (&ddb_ops
, name
, from_tty
);
1446 /* Close a connection to the remote board. */
1449 mips_close (quitting
)
1458 /* Get the board out of remote debugging mode. */
1459 (void) mips_exit_debug ();
1461 SERIAL_CLOSE (mips_desc
);
1465 /* Detach from the remote board. */
1468 mips_detach (args
, from_tty
)
1473 error ("Argument given to \"detach\" when remotely debugging.");
1480 printf_unfiltered ("Ending remote MIPS debugging.\n");
1483 /* Tell the target board to resume. This does not wait for a reply
1487 mips_resume (pid
, step
, siggnal
)
1489 enum target_signal siggnal
;
1492 /* start-sanitize-gm */
1493 #ifndef GENERAL_MAGIC
1494 if (siggnal
!= TARGET_SIGNAL_0
)
1496 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1497 target_signal_to_name (siggnal
));
1498 #endif /* GENERAL_MAGIC */
1499 /* end-sanitize-gm */
1501 mips_request (step
? 's' : 'c',
1503 (unsigned int) siggnal
,
1505 mips_receive_wait
, NULL
);
1508 /* Return the signal corresponding to SIG, where SIG is the number which
1509 the MIPS protocol uses for the signal. */
1511 mips_signal_from_protocol (sig
)
1514 /* We allow a few more signals than the IDT board actually returns, on
1515 the theory that there is at least *some* hope that perhaps the numbering
1516 for these signals is widely agreed upon. */
1519 return TARGET_SIGNAL_UNKNOWN
;
1521 /* Don't want to use target_signal_from_host because we are converting
1522 from MIPS signal numbers, not host ones. Our internal numbers
1523 match the MIPS numbers for the signals the board can return, which
1524 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1525 return (enum target_signal
) sig
;
1528 /* Wait until the remote stops, and return a wait status. */
1531 mips_wait (pid
, status
)
1533 struct target_waitstatus
*status
;
1537 char buff
[DATA_MAXLEN
];
1542 interrupt_count
= 0;
1545 /* If we have not sent a single step or continue command, then the
1546 board is waiting for us to do something. Return a status
1547 indicating that it is stopped. */
1548 if (! mips_need_reply
)
1550 status
->kind
= TARGET_WAITKIND_STOPPED
;
1551 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1555 /* No timeout; we sit here as long as the program continues to execute. */
1557 rstatus
= mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err
, -1,
1561 mips_error ("Remote failure: %s", safe_strerror (errno
));
1563 nfields
= sscanf (buff
, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1564 &rpc
, &rfp
, &rsp
, flags
);
1566 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1568 if (nfields
== 7 || nfields
== 9)
1570 char buf
[MAX_REGISTER_RAW_SIZE
];
1572 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (PC_REGNUM
), rpc
);
1573 supply_register (PC_REGNUM
, buf
);
1575 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (PC_REGNUM
), rfp
);
1576 supply_register (30, buf
); /* This register they are avoiding and so it is unnamed */
1578 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (SP_REGNUM
), rsp
);
1579 supply_register (SP_REGNUM
, buf
);
1581 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (FP_REGNUM
), 0);
1582 supply_register (FP_REGNUM
, buf
);
1588 for (i
= 0; i
<= 2; i
++)
1589 if (flags
[i
] == 'r' || flags
[i
] == 'w')
1591 else if (flags
[i
] == '\000')
1596 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1597 and so on, because the constants we want here are determined by the
1598 MIPS protocol and have nothing to do with what host we are running on. */
1599 if ((rstatus
& 0377) == 0)
1601 status
->kind
= TARGET_WAITKIND_EXITED
;
1602 status
->value
.integer
= (((rstatus
) >> 8) & 0377);
1604 else if ((rstatus
& 0377) == 0177)
1606 status
->kind
= TARGET_WAITKIND_STOPPED
;
1607 status
->value
.sig
= mips_signal_from_protocol (((rstatus
) >> 8) & 0377);
1611 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1612 status
->value
.sig
= mips_signal_from_protocol (rstatus
& 0177);
1619 pmon_wait (pid
, status
)
1621 struct target_waitstatus
*status
;
1625 char buff
[DATA_MAXLEN
];
1627 interrupt_count
= 0;
1630 /* If we have not sent a single step or continue command, then the
1631 board is waiting for us to do something. Return a status
1632 indicating that it is stopped. */
1633 if (! mips_need_reply
)
1635 status
->kind
= TARGET_WAITKIND_STOPPED
;
1636 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1640 /* Sit, polling the serial until the target decides to talk to
1641 us. NOTE: the timeout value we use is used not just for the
1642 first character, but for all the characters. */
1644 rstatus
= mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err
, -1,
1648 mips_error ("Remote failure: %s", safe_strerror (errno
));
1650 /* NOTE: The following (sig) numbers are defined by PMON:
1651 SPP_SIGTRAP 5 breakpoint
1659 /* On returning from a continue, the PMON monitor seems to start
1660 echoing back the messages we send prior to sending back the
1661 ACK. The code can cope with this, but to try and avoid the
1662 unnecessary serial traffic, and "spurious" characters displayed
1663 to the user, we cheat and reset the debug protocol. The problems
1664 seems to be caused by a check on the number of arguments, and the
1665 command length, within the monitor causing it to echo the command
1667 if (mips_monitor
!= MON_DDB
)
1670 mips_enter_debug ();
1673 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1674 and so on, because the constants we want here are determined by the
1675 MIPS protocol and have nothing to do with what host we are running on. */
1676 if ((rstatus
& 0377) == 0)
1678 status
->kind
= TARGET_WAITKIND_EXITED
;
1679 status
->value
.integer
= (((rstatus
) >> 8) & 0377);
1681 else if ((rstatus
& 0377) == 0177)
1683 status
->kind
= TARGET_WAITKIND_STOPPED
;
1684 status
->value
.sig
= mips_signal_from_protocol (((rstatus
) >> 8) & 0377);
1688 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1689 status
->value
.sig
= mips_signal_from_protocol (rstatus
& 0177);
1695 /* We have to map between the register numbers used by gdb and the
1696 register numbers used by the debugging protocol. This function
1697 assumes that we are using tm-mips.h. */
1699 #define REGNO_OFFSET 96
1702 mips_map_regno (regno
)
1707 if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
1708 return regno
- FP0_REGNUM
+ 32;
1712 return REGNO_OFFSET
+ 0;
1714 return REGNO_OFFSET
+ 1;
1716 return REGNO_OFFSET
+ 2;
1718 return REGNO_OFFSET
+ 3;
1720 return REGNO_OFFSET
+ 4;
1722 return REGNO_OFFSET
+ 5;
1724 /* FIXME: Is there a way to get the status register? */
1729 /* Fetch the remote registers. */
1732 mips_fetch_registers (regno
)
1740 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1741 mips_fetch_registers (regno
);
1745 if (regno
== FP_REGNUM
|| regno
== ZERO_REGNUM
)
1746 /* FP_REGNUM on the mips is a hack which is just supposed to read
1747 zero (see also mips-nat.c). */
1751 /* Unfortunately the PMON version in the Vr4300 board has been
1752 compiled without the 64bit register access commands. This
1753 means we cannot get hold of the full register width. */
1754 if (mips_monitor
== MON_DDB
)
1755 val
= (unsigned)mips_request ('t', (unsigned int) mips_map_regno (regno
),
1756 (unsigned int) 0, &err
, mips_receive_wait
, NULL
);
1758 val
= mips_request ('r', (unsigned int) mips_map_regno (regno
),
1759 (unsigned int) 0, &err
, mips_receive_wait
, NULL
);
1761 mips_error ("Can't read register %d: %s", regno
,
1762 safe_strerror (errno
));
1766 char buf
[MAX_REGISTER_RAW_SIZE
];
1768 /* We got the number the register holds, but gdb expects to see a
1769 value in the target byte ordering. */
1770 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (regno
), val
);
1771 supply_register (regno
, buf
);
1775 /* Prepare to store registers. The MIPS protocol can store individual
1776 registers, so this function doesn't have to do anything. */
1779 mips_prepare_to_store ()
1783 /* Store remote register(s). */
1786 mips_store_registers (regno
)
1793 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1794 mips_store_registers (regno
);
1798 mips_request ('R', (unsigned int) mips_map_regno (regno
),
1799 read_register (regno
),
1800 &err
, mips_receive_wait
, NULL
);
1802 mips_error ("Can't write register %d: %s", regno
, safe_strerror (errno
));
1805 /* Fetch a word from the target board. */
1808 mips_fetch_word (addr
)
1814 /* FIXME! addr was cast to uint! */
1815 val
= mips_request ('d', addr
, (unsigned int) 0, &err
,
1816 mips_receive_wait
, NULL
);
1819 /* Data space failed; try instruction space. */
1820 /* FIXME! addr was cast to uint! */
1821 val
= mips_request ('i', addr
, (unsigned int) 0, &err
,
1822 mips_receive_wait
, NULL
);
1824 mips_error ("Can't read address 0x%s: %s",
1825 paddr(addr
), safe_strerror (errno
));
1830 /* Store a word to the target board. Returns errno code or zero for
1831 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1832 memory location there. */
1834 /* FIXME! make sure only 32-bit quantities get stored! */
1836 mips_store_word (addr
, val
, old_contents
)
1842 unsigned int oldcontents
;
1844 oldcontents
= mips_request ('D', addr
, (unsigned int) val
,
1846 mips_receive_wait
, NULL
);
1849 /* Data space failed; try instruction space. */
1850 oldcontents
= mips_request ('I', addr
,
1851 (unsigned int) val
, &err
,
1852 mips_receive_wait
, NULL
);
1856 if (old_contents
!= NULL
)
1857 store_unsigned_integer (old_contents
, 4, oldcontents
);
1861 /* Read or write LEN bytes from inferior memory at MEMADDR,
1862 transferring to or from debugger address MYADDR. Write to inferior
1863 if SHOULD_WRITE is nonzero. Returns length of data written or
1864 read; 0 for error. Note that protocol gives us the correct value
1865 for a longword, since it transfers values in ASCII. We want the
1866 byte values, so we have to swap the longword values. */
1869 mips_xfer_memory (memaddr
, myaddr
, len
, write
, ignore
)
1874 struct target_ops
*ignore
;
1877 /* Round starting address down to longword boundary. */
1878 register CORE_ADDR addr
= memaddr
&~ 3;
1879 /* Round ending address up; get number of longwords that makes. */
1880 register int count
= (((memaddr
+ len
) - addr
) + 3) / 4;
1881 /* Allocate buffer of that many longwords. */
1882 register char *buffer
= alloca (count
* 4);
1888 /* Fill start and end extra bytes of buffer with existing data. */
1889 if (addr
!= memaddr
|| len
< 4)
1891 /* Need part of initial word -- fetch it. */
1892 store_unsigned_integer (&buffer
[0], 4, mips_fetch_word (addr
));
1897 /* Need part of last word -- fetch it. FIXME: we do this even
1898 if we don't need it. */
1899 store_unsigned_integer (&buffer
[(count
- 1) * 4], 4,
1900 mips_fetch_word (addr
+ (count
- 1) * 4));
1903 /* Copy data to be written over corresponding part of buffer */
1905 memcpy ((char *) buffer
+ (memaddr
& 3), myaddr
, len
);
1907 /* Write the entire buffer. */
1909 for (i
= 0; i
< count
; i
++, addr
+= 4)
1911 status
= mips_store_word (addr
,
1912 extract_unsigned_integer (&buffer
[i
*4], 4),
1914 /* Report each kilobyte (we download 32-bit words at a time) */
1917 printf_unfiltered ("*");
1925 /* FIXME: Do we want a QUIT here? */
1928 printf_unfiltered ("\n");
1932 /* Read all the longwords */
1933 for (i
= 0; i
< count
; i
++, addr
+= 4)
1935 store_unsigned_integer (&buffer
[i
*4], 4, mips_fetch_word (addr
));
1939 /* Copy appropriate bytes out of the buffer. */
1940 memcpy (myaddr
, buffer
+ (memaddr
& 3), len
);
1945 /* Print info on this target. */
1948 mips_files_info (ignore
)
1949 struct target_ops
*ignore
;
1951 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1954 /* Kill the process running on the board. This will actually only
1955 work if we are doing remote debugging over the console input. I
1956 think that if IDT/sim had the remote debug interrupt enabled on the
1957 right port, we could interrupt the process with a break signal. */
1962 if (!mips_wait_flag
)
1967 if (interrupt_count
>= 2)
1969 interrupt_count
= 0;
1971 target_terminal_ours ();
1973 if (query ("Interrupted while waiting for the program.\n\
1974 Give up (and stop debugging it)? "))
1976 /* Clean up in such a way that mips_close won't try to talk to the
1977 board (it almost surely won't work since we weren't able to talk to
1981 SERIAL_CLOSE (mips_desc
);
1983 printf_unfiltered ("Ending remote MIPS debugging.\n");
1984 target_mourn_inferior ();
1986 return_to_top_level (RETURN_QUIT
);
1989 target_terminal_inferior ();
1992 if (remote_debug
> 0)
1993 printf_unfiltered ("Sending break\n");
1995 SERIAL_SEND_BREAK (mips_desc
);
2004 SERIAL_WRITE (mips_desc
, &cc
, 1);
2006 target_mourn_inferior ();
2011 /* Start running on the target board. */
2014 mips_create_inferior (execfile
, args
, env
)
2024 Can't pass arguments to remote MIPS board; arguments ignored.");
2025 /* And don't try to use them on the next "run" command. */
2026 execute_command ("set args", 0);
2029 if (execfile
== 0 || exec_bfd
== 0)
2030 error ("No executable file specified");
2032 entry_pt
= (CORE_ADDR
) bfd_get_start_address (exec_bfd
);
2034 init_wait_for_inferior ();
2036 /* FIXME: Should we set inferior_pid here? */
2038 /* start-sanitize-gm */
2039 #ifdef GENERAL_MAGIC
2040 magic_create_inferior_hook ();
2041 proceed (entry_pt
, TARGET_SIGNAL_PWR
, 0);
2043 /* end-sanitize-gm */
2044 proceed (entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
2045 /* start-sanitize-gm */
2046 #endif /* GENERAL_MAGIC */
2047 /* end-sanitize-gm */
2050 /* Clean up after a process. Actually nothing to do. */
2053 mips_mourn_inferior ()
2055 if (current_ops
!= NULL
)
2056 unpush_target (current_ops
);
2057 generic_mourn_inferior ();
2060 /* We can write a breakpoint and read the shadow contents in one
2063 /* The IDT board uses an unusual breakpoint value, and sometimes gets
2064 confused when it sees the usual MIPS breakpoint instruction. */
2066 #define BREAK_INSN (0x00000a0d)
2067 #define BREAK_INSN_SIZE (4)
2069 /* Insert a breakpoint on targets that don't have any better breakpoint
2070 support. We read the contents of the target location and stash it,
2071 then overwrite it with a breakpoint instruction. ADDR is the target
2072 location in the target machine. CONTENTS_CACHE is a pointer to
2073 memory allocated for saving the target contents. It is guaranteed
2074 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2075 is accomplished via BREAKPOINT_MAX). */
2078 mips_insert_breakpoint (addr
, contents_cache
)
2080 char *contents_cache
;
2084 if (monitor_supports_breakpoints
)
2085 return common_breakpoint ('B', addr
, 0x3, "f");
2087 return mips_store_word (addr
, BREAK_INSN
, contents_cache
);
2091 mips_remove_breakpoint (addr
, contents_cache
)
2093 char *contents_cache
;
2095 if (monitor_supports_breakpoints
)
2096 return common_breakpoint ('b', addr
, 0, NULL
);
2098 return target_write_memory (addr
, contents_cache
, BREAK_INSN_SIZE
);
2101 #if 0 /* currently not used */
2102 /* PMON does not currently provide support for the debug mode 'b'
2103 commands to manipulate breakpoints. However, if we wanted to use
2104 the monitor breakpoints (rather than the GDB BREAK_INSN version)
2105 then this code performs the work needed to leave debug mode,
2106 set/clear the breakpoint, and then return to debug mode. */
2108 #define PMON_MAX_BP (33) /* 32 SW, 1 HW */
2109 static CORE_ADDR mips_pmon_bp_info
[PMON_MAX_BP
];
2110 /* NOTE: The code relies on this vector being zero-initialised by the system */
2113 pmon_insert_breakpoint (addr
, contents_cache
)
2115 char *contents_cache
;
2119 if (monitor_supports_breakpoints
)
2121 char tbuff
[12]; /* space for breakpoint command */
2125 /* PMON does not support debug level breakpoint set/remove: */
2126 if (mips_exit_debug ())
2127 mips_error ("Failed to exit debug mode");
2129 sprintf (tbuff
, "b %08x\015", addr
);
2130 mips_send_command (tbuff
, 0);
2132 mips_expect ("Bpt ");
2134 if (!mips_getstring (tbuff
, 2))
2136 tbuff
[2] = '\0'; /* terminate the string */
2137 if (sscanf (tbuff
, "%d", &bpnum
) != 1)
2139 fprintf_unfiltered (stderr
, "Invalid decimal breakpoint number from target: %s\n", tbuff
);
2143 mips_expect (" = ");
2145 /* Lead in the hex number we are expecting: */
2149 /* FIXME!! only 8 bytes! need to expand for Bfd64;
2150 which targets return 64-bit addresses? PMON returns only 32! */
2151 if (!mips_getstring (&tbuff
[2], 8))
2153 tbuff
[10] = '\0'; /* terminate the string */
2155 if (sscanf (tbuff
, "0x%08x", &bpaddr
) != 1)
2157 fprintf_unfiltered (stderr
, "Invalid hex address from target: %s\n", tbuff
);
2161 if (bpnum
>= PMON_MAX_BP
)
2163 fprintf_unfiltered (stderr
, "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n",
2164 bpnum
, PMON_MAX_BP
- 1);
2169 fprintf_unfiltered (stderr
, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr
, bpaddr
);
2171 mips_pmon_bp_info
[bpnum
] = bpaddr
;
2173 mips_expect ("\015\012");
2174 mips_expect (mips_monitor_prompt
);
2176 mips_enter_debug ();
2181 return mips_store_word (addr
, BREAK_INSN
, contents_cache
);
2185 pmon_remove_breakpoint (addr
, contents_cache
)
2187 char *contents_cache
;
2189 if (monitor_supports_breakpoints
)
2192 char tbuff
[7]; /* enough for delete breakpoint command */
2194 for (bpnum
= 0; bpnum
< PMON_MAX_BP
; bpnum
++)
2195 if (mips_pmon_bp_info
[bpnum
] == addr
)
2198 if (bpnum
>= PMON_MAX_BP
)
2200 fprintf_unfiltered (stderr
, "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n", paddr(addr
));
2204 if (mips_exit_debug ())
2205 mips_error ("Failed to exit debug mode");
2207 sprintf (tbuff
, "db %02d\015", bpnum
);
2209 mips_send_command (tbuff
, -1);
2210 /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not
2211 set" message will be returned. */
2213 mips_enter_debug ();
2218 return target_write_memory (addr
, contents_cache
, BREAK_INSN_SIZE
);
2222 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2223 This is used for memory ref breakpoints. */
2225 static unsigned long
2226 calculate_mask (addr
, len
)
2233 mask
= addr
^ (addr
+ len
- 1);
2235 for (i
= 32; i
>= 0; i
--)
2241 mask
= (unsigned long) 0xffffffff >> i
;
2246 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
2247 for a read watchpoint, or 2 for a read/write watchpoint. */
2250 remote_mips_set_watchpoint (addr
, len
, type
)
2255 CORE_ADDR first_addr
;
2259 mask
= calculate_mask (addr
, len
);
2261 first_addr
= addr
& ~mask
;
2271 case 2: /* read/write */
2278 if (common_breakpoint ('B', first_addr
, mask
, flags
))
2285 remote_mips_remove_watchpoint (addr
, len
, type
)
2290 CORE_ADDR first_addr
;
2293 mask
= calculate_mask (addr
, len
);
2295 first_addr
= addr
& ~mask
;
2297 if (common_breakpoint ('b', first_addr
, 0, NULL
))
2304 remote_mips_stopped_by_watchpoint ()
2306 return hit_watchpoint
;
2309 /* This routine generates the a breakpoint command of the form:
2311 0x0 <CMD> <ADDR> <MASK> <FLAGS>
2313 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
2314 the address of the breakpoint. <MASK> is a don't care mask for addresses.
2315 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
2318 common_breakpoint (cmd
, addr
, mask
, flags
)
2325 char buf
[DATA_MAXLEN
+ 1];
2327 int rpid
, rerrflg
, rresponse
;
2331 sprintf (buf
, "0x0 %c 0x%x 0x%x %s", cmd
, addr
, mask
, flags
);
2333 sprintf (buf
, "0x0 %c 0x%x", cmd
, addr
);
2335 mips_send_packet (buf
, 1);
2337 len
= mips_receive_packet (buf
, 1, mips_receive_wait
);
2340 nfields
= sscanf (buf
, "0x%x %c 0x%x 0x%x", &rpid
, &rcmd
, &rerrflg
, &rresponse
);
2344 mips_error ("common_breakpoint: Bad response from remote board: %s", buf
);
2348 /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2349 Cogent returns "0x0 b 0xffffffff 0x16\000": */
2350 if (mips_monitor
== MON_DDB
)
2351 rresponse
= rerrflg
;
2352 if (rresponse
!= 22) /* invalid argument */
2353 fprintf_unfiltered (stderr
, "common_breakpoint (0x%s): Got error: 0x%x\n",
2354 paddr(addr
), rresponse
);
2362 send_srec (srec
, len
, addr
)
2371 SERIAL_WRITE (mips_desc
, srec
, len
);
2373 ch
= mips_readchar (2);
2377 case SERIAL_TIMEOUT
:
2378 error ("Timeout during download.");
2382 case 0x15: /* NACK */
2383 fprintf_unfiltered (gdb_stderr
, "Download got a NACK at byte %d! Retrying.\n", addr
);
2386 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch
);
2391 /* Download a binary file by converting it to S records. */
2394 mips_load_srec (args
)
2399 char *buffer
, srec
[1024];
2401 int srec_frame
= 200;
2403 static int hashmark
= 1;
2405 buffer
= alloca (srec_frame
* 2 + 256);
2407 abfd
= bfd_openr (args
, 0);
2410 printf_filtered ("Unable to open file %s\n", args
);
2414 if (bfd_check_format (abfd
, bfd_object
) == 0)
2416 printf_filtered ("File is not an object file\n");
2420 /* This actually causes a download in the IDT binary format: */
2421 #define LOAD_CMD "load -b -s tty0\015"
2422 mips_send_command (LOAD_CMD
, 0);
2424 for (s
= abfd
->sections
; s
; s
= s
->next
)
2426 if (s
->flags
& SEC_LOAD
)
2430 /* FIXME! vma too small?? */
2431 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s
->name
, s
->vma
,
2432 s
->vma
+ s
->_raw_size
);
2433 gdb_flush (gdb_stdout
);
2435 for (i
= 0; i
< s
->_raw_size
; i
+= numbytes
)
2437 numbytes
= min (srec_frame
, s
->_raw_size
- i
);
2439 bfd_get_section_contents (abfd
, s
, buffer
, i
, numbytes
);
2441 reclen
= mips_make_srec (srec
, '3', s
->vma
+ i
, buffer
, numbytes
);
2442 send_srec (srec
, reclen
, s
->vma
+ i
);
2446 putchar_unfiltered ('#');
2447 gdb_flush (gdb_stdout
);
2450 } /* Per-packet (or S-record) loop */
2452 putchar_unfiltered ('\n');
2453 } /* Loadable sections */
2456 putchar_unfiltered ('\n');
2458 /* Write a type 7 terminator record. no data for a type 7, and there
2459 is no data, so len is 0. */
2461 reclen
= mips_make_srec (srec
, '7', abfd
->start_address
, NULL
, 0);
2463 send_srec (srec
, reclen
, abfd
->start_address
);
2465 SERIAL_FLUSH_INPUT (mips_desc
);
2469 * mips_make_srec -- make an srecord. This writes each line, one at a
2470 * time, each with it's own header and trailer line.
2471 * An srecord looks like this:
2473 * byte count-+ address
2474 * start ---+ | | data +- checksum
2476 * S01000006F6B692D746573742E73726563E4
2477 * S315000448600000000000000000FC00005900000000E9
2478 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2479 * S30B0004485A0000000000004E
2482 * S<type><length><address><data><checksum>
2486 * is the number of bytes following upto the checksum. Note that
2487 * this is not the number of chars following, since it takes two
2488 * chars to represent a byte.
2492 * 1) two byte address data record
2493 * 2) three byte address data record
2494 * 3) four byte address data record
2495 * 7) four byte address termination record
2496 * 8) three byte address termination record
2497 * 9) two byte address termination record
2500 * is the start address of the data following, or in the case of
2501 * a termination record, the start address of the image
2505 * is the sum of all the raw byte data in the record, from the length
2506 * upwards, modulo 256 and subtracted from 255.
2508 * This routine returns the length of the S-record.
2513 mips_make_srec (buf
, type
, memaddr
, myaddr
, len
)
2517 unsigned char *myaddr
;
2520 unsigned char checksum
;
2523 /* Create the header for the srec. addr_size is the number of bytes in the address,
2524 and 1 is the number of bytes in the count. */
2526 /* FIXME!! bigger buf required for 64-bit! */
2529 buf
[2] = len
+ 4 + 1; /* len + 4 byte address + 1 byte checksum */
2530 /* This assumes S3 style downloads (4byte addresses). There should
2531 probably be a check, or the code changed to make it more
2533 buf
[3] = memaddr
>> 24;
2534 buf
[4] = memaddr
>> 16;
2535 buf
[5] = memaddr
>> 8;
2537 memcpy (&buf
[7], myaddr
, len
);
2539 /* Note that the checksum is calculated on the raw data, not the
2540 hexified data. It includes the length, address and the data
2541 portions of the packet. */
2543 buf
+= 2; /* Point at length byte */
2544 for (i
= 0; i
< len
+ 4 + 1; i
++)
2552 /* The following manifest controls whether we enable the simple flow
2553 control support provided by the monitor. If enabled the code will
2554 wait for an affirmative ACK between transmitting packets. */
2555 #define DOETXACK (1)
2557 /* The PMON fast-download uses an encoded packet format constructed of
2558 3byte data packets (encoded as 4 printable ASCII characters), and
2559 escape sequences (preceded by a '/'):
2562 'C' compare checksum (12bit value, not included in checksum calculation)
2563 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary
2564 'Z' zero fill multiple of 3bytes
2565 'B' byte (12bit encoded value, of 8bit data)
2566 'A' address (36bit encoded value)
2567 'E' define entry as original address, and exit load
2569 The packets are processed in 4 character chunks, so the escape
2570 sequences that do not have any data (or variable length data)
2571 should be padded to a 4 character boundary. The decoder will give
2572 an error if the complete message block size is not a multiple of
2573 4bytes (size of record).
2575 The encoding of numbers is done in 6bit fields. The 6bit value is
2576 used to index into this string to get the specific character
2577 encoding for the value: */
2578 static char encoding
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
2580 /* Convert the number of bits required into an encoded number, 6bits
2581 at a time (range 0..63). Keep a checksum if required (passed
2582 pointer non-NULL). The function returns the number of encoded
2583 characters written into the buffer. */
2585 pmon_makeb64 (v
, p
, n
, chksum
)
2591 int count
= (n
/ 6);
2593 if ((n
% 12) != 0) {
2594 fprintf_unfiltered(stderr
,"Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n",n
,(n
== 1)?"":"s");
2598 fprintf_unfiltered(stderr
,"Fast encoding cannot process more than 36bits at the moment: %dbits\n",n
);
2602 /* Deal with the checksum: */
2603 if (chksum
!= NULL
) {
2605 case 36: *chksum
+= ((v
>> 24) & 0xFFF);
2606 case 24: *chksum
+= ((v
>> 12) & 0xFFF);
2607 case 12: *chksum
+= ((v
>> 0) & 0xFFF);
2613 *p
++ = encoding
[(v
>> n
) & 0x3F];
2619 /* Shorthand function (that could be in-lined) to output the zero-fill
2620 escape sequence into the data stream. */
2622 pmon_zeroset (recsize
, buff
, amount
, chksum
)
2626 unsigned int *chksum
;
2630 sprintf(*buff
,"/Z");
2631 count
= pmon_makeb64 (*amount
, (*buff
+ 2), 12, chksum
);
2632 *buff
+= (count
+ 2);
2634 return(recsize
+ count
+ 2);
2638 pmon_checkset (recsize
, buff
, value
)
2645 /* Add the checksum (without updating the value): */
2646 sprintf (*buff
, "/C");
2647 count
= pmon_makeb64 (*value
, (*buff
+ 2), 12, NULL
);
2648 *buff
+= (count
+ 2);
2649 sprintf (*buff
, "\015");
2650 *buff
+= 2; /* include zero terminator */
2651 /* Forcing a checksum validation clears the sum: */
2653 return(recsize
+ count
+ 3);
2656 /* Amount of padding we leave after at the end of the output buffer,
2657 for the checksum and line termination characters: */
2658 #define CHECKSIZE (4 + 4 + 4 + 2)
2659 /* zero-fill, checksum, transfer end and line termination space. */
2661 /* The amount of binary data loaded from the object file in a single
2663 #define BINCHUNK (1024)
2665 /* Maximum line of data accepted by the monitor: */
2666 #define MAXRECSIZE (550)
2667 /* NOTE: This constant depends on the monitor being used. This value
2668 is for PMON 5.x on the Cogent Vr4300 board. */
2671 pmon_make_fastrec (outbuf
, inbuf
, inptr
, inamount
, recsize
, csum
, zerofill
)
2673 unsigned char *inbuf
;
2678 unsigned int *zerofill
;
2683 /* This is a simple check to ensure that our data will fit within
2684 the maximum allowable record size. Each record output is 4bytes
2685 in length. We must allow space for a pending zero fill command,
2686 the record, and a checksum record. */
2687 while ((*recsize
< (MAXRECSIZE
- CHECKSIZE
)) && ((inamount
- *inptr
) > 0)) {
2688 /* Process the binary data: */
2689 if ((inamount
- *inptr
) < 3) {
2691 *recsize
= pmon_zeroset (*recsize
, &p
, zerofill
, csum
);
2693 count
= pmon_makeb64 (inbuf
[*inptr
], &p
[2], 12, csum
);
2695 *recsize
+= (2 + count
);
2698 unsigned int value
= ((inbuf
[*inptr
+ 0] << 16) | (inbuf
[*inptr
+ 1] << 8) | inbuf
[*inptr
+ 2]);
2699 /* Simple check for zero data. TODO: A better check would be
2700 to check the last, and then the middle byte for being zero
2701 (if the first byte is not). We could then check for
2702 following runs of zeros, and if above a certain size it is
2703 worth the 4 or 8 character hit of the byte insertions used
2704 to pad to the start of the zeroes. NOTE: This also depends
2705 on the alignment at the end of the zero run. */
2706 if (value
== 0x00000000) {
2708 if (*zerofill
== 0xFFF) /* 12bit counter */
2709 *recsize
= pmon_zeroset (*recsize
, &p
, zerofill
, csum
);
2712 *recsize
= pmon_zeroset (*recsize
, &p
, zerofill
, csum
);
2713 count
= pmon_makeb64 (value
, p
, 24, csum
);
2725 #if defined(DOETXACK)
2729 int c
= SERIAL_READCHAR (mips_desc
, 2);
2730 if ((c
== SERIAL_TIMEOUT
) || (c
!= 0x06)) {
2731 fprintf_unfiltered (gdb_stderr
, "Failed to receive valid ACK\n");
2732 return(-1); /* terminate the download */
2736 #endif /* DOETXACK */
2739 pmon_load_fast (file
)
2744 unsigned char *binbuf
;
2747 unsigned int csum
= 0;
2748 static int hashmark
= 1;
2753 buffer
= (char *)xmalloc(MAXRECSIZE
+ 1);
2754 binbuf
= (unsigned char *)xmalloc(BINCHUNK
);
2756 abfd
= bfd_openr(file
,0);
2759 printf_filtered ("Unable to open file %s\n",file
);
2763 if (bfd_check_format(abfd
,bfd_object
) == 0)
2765 printf_filtered("File is not an object file\n");
2769 /* Setup the required download state: */
2770 mips_send_command ("set dlproto etxack\015", -1);
2771 mips_send_command ("set dlecho off\015", -1);
2772 /* NOTE: We get a "cannot set variable" message if the variable is
2773 already defined to have the argument we give. The code doesn't
2774 care, since it just scans to the next prompt anyway. */
2775 /* Start the download: */
2776 mips_send_command (LOAD_CMD
, 0);
2777 mips_expect ("Downloading from tty0, ^C to abort\015\012");
2779 /* Zero the checksum */
2780 sprintf(buffer
,"/Kxx\015");
2781 reclen
= strlen(buffer
);
2782 SERIAL_WRITE (mips_desc
, buffer
, reclen
);
2784 #if defined(DOETXACK)
2785 finished
= pmon_check_ack();
2786 #endif /* DOETXACK */
2788 for (s
= abfd
->sections
; s
&& !finished
; s
= s
->next
)
2789 if (s
->flags
& SEC_LOAD
) /* only deal with loadable sections */
2791 bintotal
+= s
->_raw_size
;
2792 final
= (s
->vma
+ s
->_raw_size
);
2794 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s
->name
, (unsigned int)s
->vma
,
2795 (unsigned int)(s
->vma
+ s
->_raw_size
));
2796 gdb_flush (gdb_stdout
);
2798 /* Output the starting address */
2799 sprintf(buffer
,"/A");
2800 reclen
= pmon_makeb64(s
->vma
,&buffer
[2],36,&csum
);
2801 buffer
[2 + reclen
] = '\015';
2802 buffer
[3 + reclen
] = '\0';
2803 reclen
+= 3; /* for the initial escape code and carriage return */
2804 SERIAL_WRITE (mips_desc
, buffer
, reclen
);
2805 #if defined(DOETXACK)
2806 finished
= pmon_check_ack();
2807 #endif /* DOETXACK */
2812 unsigned int zerofill
= 0;
2818 for (i
= 0; ((i
< s
->_raw_size
) && !finished
); i
+= binamount
) {
2821 binamount
= min (BINCHUNK
, s
->_raw_size
- i
);
2823 bfd_get_section_contents (abfd
, s
, binbuf
, i
, binamount
);
2825 /* This keeps a rolling checksum, until we decide to output
2827 for (; ((binamount
- binptr
) > 0);) {
2828 pmon_make_fastrec (&bp
, binbuf
, &binptr
, binamount
, &reclen
, &csum
, &zerofill
);
2829 if (reclen
>= (MAXRECSIZE
- CHECKSIZE
)) {
2830 reclen
= pmon_checkset (reclen
, &bp
, &csum
);
2831 SERIAL_WRITE (mips_desc
, buffer
, reclen
);
2832 #if defined(DOETXACK)
2833 finished
= pmon_check_ack();
2835 zerofill
= 0; /* do not transmit pending zerofills */
2838 #endif /* DOETXACK */
2841 putchar_unfiltered ('#');
2842 gdb_flush (gdb_stdout
);
2846 reclen
= 0; /* buffer processed */
2851 /* Ensure no out-standing zerofill requests: */
2853 reclen
= pmon_zeroset (reclen
, &bp
, &zerofill
, &csum
);
2855 /* and then flush the line: */
2857 reclen
= pmon_checkset (reclen
, &bp
, &csum
);
2858 /* Currently pmon_checkset outputs the line terminator by
2859 default, so we write out the buffer so far: */
2860 SERIAL_WRITE (mips_desc
, buffer
, reclen
);
2861 #if defined(DOETXACK)
2862 finished
= pmon_check_ack();
2863 #endif /* DOETXACK */
2868 putchar_unfiltered ('\n');
2871 /* Terminate the transfer. We know that we have an empty output
2872 buffer at this point. */
2873 sprintf (buffer
, "/E/E\015"); /* include dummy padding characters */
2874 reclen
= strlen (buffer
);
2875 SERIAL_WRITE (mips_desc
, buffer
, reclen
);
2877 if (finished
) { /* Ignore the termination message: */
2878 SERIAL_FLUSH_INPUT (mips_desc
);
2879 } else { /* Deal with termination message: */
2880 char hexnumber
[9]; /* includes '\0' space */
2881 mips_expect ("Entry Address = ");
2882 sprintf(hexnumber
,"%x",final
);
2883 mips_expect (hexnumber
);
2884 #if defined(DOETXACK)
2885 mips_expect ("\015\012\006\015\012total = 0x");
2886 #else /* normal termination */
2887 mips_expect ("\015\012\015\012total = 0x");
2888 #endif /* !DOETXACK */
2889 sprintf(hexnumber
,"%x",bintotal
);
2890 mips_expect (hexnumber
);
2891 mips_expect (" bytes\015\012");
2897 /* mips_load -- download a file. */
2900 mips_load (file
, from_tty
)
2904 /* Get the board out of remote debugging mode. */
2905 if (mips_exit_debug ())
2906 error ("mips_load: Couldn't get into monitor mode.");
2908 if (mips_monitor
== MON_PMON
|| mips_monitor
== MON_DDB
)
2909 pmon_load_fast (file
);
2911 mips_load_srec (file
);
2915 /* Finally, make the PC point at the start address */
2916 if (mips_monitor
== MON_DDB
)
2918 /* Work around problem where DDB monitor does not update the
2919 PC after a load. The following ensures that the write_pc()
2920 WILL update the PC value: */
2921 register_valid
[PC_REGNUM
] = 0;
2924 write_pc (bfd_get_start_address (exec_bfd
));
2926 inferior_pid
= 0; /* No process now */
2928 /* This is necessary because many things were based on the PC at the time that
2929 we attached to the monitor, which is no longer valid now that we have loaded
2930 new code (and just changed the PC). Another way to do this might be to call
2931 normal_stop, except that the stack may not be valid, and things would get
2932 horribly confused... */
2934 clear_symtab_users ();
2937 /* The target vector. */
2939 struct target_ops mips_ops
=
2941 "mips", /* to_shortname */
2942 "Remote MIPS debugging over serial line", /* to_longname */
2944 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2945 The argument is the device it is connected to or, if it contains a colon,\n\
2946 HOST:PORT to access a board over a network", /* to_doc */
2947 mips_open
, /* to_open */
2948 mips_close
, /* to_close */
2949 NULL
, /* to_attach */
2950 mips_detach
, /* to_detach */
2951 mips_resume
, /* to_resume */
2952 mips_wait
, /* to_wait */
2953 mips_fetch_registers
, /* to_fetch_registers */
2954 mips_store_registers
, /* to_store_registers */
2955 mips_prepare_to_store
, /* to_prepare_to_store */
2956 mips_xfer_memory
, /* to_xfer_memory */
2957 mips_files_info
, /* to_files_info */
2958 mips_insert_breakpoint
, /* to_insert_breakpoint */
2959 mips_remove_breakpoint
, /* to_remove_breakpoint */
2960 NULL
, /* to_terminal_init */
2961 NULL
, /* to_terminal_inferior */
2962 NULL
, /* to_terminal_ours_for_output */
2963 NULL
, /* to_terminal_ours */
2964 NULL
, /* to_terminal_info */
2965 mips_kill
, /* to_kill */
2966 mips_load
, /* to_load */
2967 NULL
, /* to_lookup_symbol */
2968 mips_create_inferior
, /* to_create_inferior */
2969 mips_mourn_inferior
, /* to_mourn_inferior */
2970 NULL
, /* to_can_run */
2971 NULL
, /* to_notice_signals */
2972 0, /* to_thread_alive */
2974 process_stratum
, /* to_stratum */
2976 1, /* to_has_all_memory */
2977 1, /* to_has_memory */
2978 1, /* to_has_stack */
2979 1, /* to_has_registers */
2980 1, /* to_has_execution */
2981 NULL
, /* sections */
2982 NULL
, /* sections_end */
2983 OPS_MAGIC
/* to_magic */
2986 /* An alternative target vector: */
2987 struct target_ops pmon_ops
=
2989 "pmon", /* to_shortname */
2990 "Remote MIPS debugging over serial line", /* to_longname */
2992 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
2993 line. The argument is the device it is connected to or, if it contains a\n\
2994 colon, HOST:PORT to access a board over a network", /* to_doc */
2995 pmon_open
, /* to_open */
2996 mips_close
, /* to_close */
2997 NULL
, /* to_attach */
2998 mips_detach
, /* to_detach */
2999 mips_resume
, /* to_resume */
3000 pmon_wait
, /* to_wait */
3001 mips_fetch_registers
, /* to_fetch_registers */
3002 mips_store_registers
, /* to_store_registers */
3003 mips_prepare_to_store
, /* to_prepare_to_store */
3004 mips_xfer_memory
, /* to_xfer_memory */
3005 mips_files_info
, /* to_files_info */
3006 mips_insert_breakpoint
, /* to_insert_breakpoint */
3007 mips_remove_breakpoint
, /* to_remove_breakpoint */
3008 NULL
, /* to_terminal_init */
3009 NULL
, /* to_terminal_inferior */
3010 NULL
, /* to_terminal_ours_for_output */
3011 NULL
, /* to_terminal_ours */
3012 NULL
, /* to_terminal_info */
3013 mips_kill
, /* to_kill */
3014 mips_load
, /* to_load */
3015 NULL
, /* to_lookup_symbol */
3016 mips_create_inferior
, /* to_create_inferior */
3017 mips_mourn_inferior
, /* to_mourn_inferior */
3018 NULL
, /* to_can_run */
3019 NULL
, /* to_notice_signals */
3020 0, /* to_thread_alive */
3022 process_stratum
, /* to_stratum */
3024 1, /* to_has_all_memory */
3025 1, /* to_has_memory */
3026 1, /* to_has_stack */
3027 1, /* to_has_registers */
3028 1, /* to_has_execution */
3029 NULL
, /* sections */
3030 NULL
, /* sections_end */
3031 OPS_MAGIC
/* to_magic */
3034 /* Another alternative target vector. This is a PMON system, but with
3035 a different monitor prompt, aswell as some other operational
3037 struct target_ops ddb_ops
=
3039 "ddb", /* to_shortname */
3040 "Remote MIPS debugging over serial line", /* to_longname */
3042 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3043 line. The argument is the device it is connected to or, if it contains a\n\
3044 colon, HOST:PORT to access a board over a network", /* to_doc */
3045 ddb_open
, /* to_open */
3046 mips_close
, /* to_close */
3047 NULL
, /* to_attach */
3048 mips_detach
, /* to_detach */
3049 mips_resume
, /* to_resume */
3050 pmon_wait
, /* to_wait */
3051 mips_fetch_registers
, /* to_fetch_registers */
3052 mips_store_registers
, /* to_store_registers */
3053 mips_prepare_to_store
, /* to_prepare_to_store */
3054 mips_xfer_memory
, /* to_xfer_memory */
3055 mips_files_info
, /* to_files_info */
3056 mips_insert_breakpoint
, /* to_insert_breakpoint */
3057 mips_remove_breakpoint
, /* to_remove_breakpoint */
3058 NULL
, /* to_terminal_init */
3059 NULL
, /* to_terminal_inferior */
3060 NULL
, /* to_terminal_ours_for_output */
3061 NULL
, /* to_terminal_ours */
3062 NULL
, /* to_terminal_info */
3063 mips_kill
, /* to_kill */
3064 mips_load
, /* to_load */
3065 NULL
, /* to_lookup_symbol */
3066 mips_create_inferior
, /* to_create_inferior */
3067 mips_mourn_inferior
, /* to_mourn_inferior */
3068 NULL
, /* to_can_run */
3069 NULL
, /* to_notice_signals */
3070 0, /* to_thread_alive */
3072 process_stratum
, /* to_stratum */
3074 1, /* to_has_all_memory */
3075 1, /* to_has_memory */
3076 1, /* to_has_stack */
3077 1, /* to_has_registers */
3078 1, /* to_has_execution */
3079 NULL
, /* sections */
3080 NULL
, /* sections_end */
3081 OPS_MAGIC
/* to_magic */
3085 _initialize_remote_mips ()
3087 add_target (&mips_ops
);
3088 add_target (&pmon_ops
);
3089 add_target (&ddb_ops
);
3092 add_set_cmd ("timeout", no_class
, var_zinteger
,
3093 (char *) &mips_receive_wait
,
3094 "Set timeout in seconds for remote MIPS serial I/O.",
3099 add_set_cmd ("retransmit-timeout", no_class
, var_zinteger
,
3100 (char *) &mips_retransmit_wait
,
3101 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3102 This is the number of seconds to wait for an acknowledgement to a packet\n\
3103 before resending the packet.", &setlist
),
3107 add_set_cmd ("syn-garbage-limit", no_class
, var_zinteger
,
3108 (char *) &mips_syn_garbage
,
3109 "Set the maximum number of characters to ignore when scanning for a SYN.\n\
3110 This is the maximum number of characters GDB will ignore when trying to\n\
3111 synchronize with the remote system. A value of -1 means that there is no limit\n\
3112 (Note that these characters are printed out even though they are ignored.)",