1 /* Remote debugging interface for MIPS remote debugging protocol.
2 Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
33 /* Prototypes for local functions. */
36 mips_readchar
PARAMS ((int timeout
));
39 mips_receive_header
PARAMS ((unsigned char *hdr
, int *pgarbage
, int ch
,
43 mips_receive_trailer
PARAMS ((unsigned char *trlr
, int *pgarbage
, int *pch
,
46 static int mips_cksum
PARAMS ((const unsigned char *hdr
,
47 const unsigned char *data
,
51 mips_send_packet
PARAMS ((const char *s
, int get_ack
));
54 mips_receive_packet
PARAMS ((char *buff
));
57 mips_request
PARAMS ((char cmd
, unsigned int addr
, unsigned int data
,
61 mips_initialize
PARAMS ((void));
64 mips_open
PARAMS ((char *name
, int from_tty
));
67 mips_close
PARAMS ((int quitting
));
70 mips_detach
PARAMS ((char *args
, int from_tty
));
73 mips_resume
PARAMS ((int step
, int siggnal
));
76 mips_wait
PARAMS ((WAITTYPE
*status
));
79 mips_map_regno
PARAMS ((int regno
));
82 mips_fetch_registers
PARAMS ((int regno
));
85 mips_prepare_to_store
PARAMS ((void));
88 mips_store_registers
PARAMS ((int regno
));
91 mips_fetch_word
PARAMS ((CORE_ADDR addr
));
94 mips_store_word
PARAMS ((CORE_ADDR addr
, int value
));
97 mips_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
,
98 int write
, struct target_ops
*ignore
));
101 mips_files_info
PARAMS ((struct target_ops
*ignore
));
104 mips_load
PARAMS ((char *args
, int from_tty
));
107 mips_create_inferior
PARAMS ((char *execfile
, char *args
, char **env
));
110 mips_mourn_inferior
PARAMS ((void));
112 /* A forward declaration. */
113 extern struct target_ops mips_ops
;
115 /* The MIPS remote debugging interface is built on top of a simple
116 packet protocol. Each packet is organized as follows:
118 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
119 may not appear anywhere else in the packet. Any time a SYN is
120 seen, a new packet should be assumed to have begun.
123 This byte contains the upper five bits of the logical length
124 of the data section, plus a single bit indicating whether this
125 is a data packet or an acknowledgement. The documentation
126 indicates that this bit is 1 for a data packet, but the actual
127 board uses 1 for an acknowledgement. The value of the byte is
128 0x40 + (ack ? 0x20 : 0) + (len >> 6)
129 (we always have 0 <= len < 1024). Acknowledgement packets do
130 not carry data, and must have a data length of 0.
132 LEN1 This byte contains the lower six bits of the logical length of
133 the data section. The value is
136 SEQ This byte contains the six bit sequence number of the packet.
139 An acknowlegment packet contains the sequence number of the
140 packet being acknowledged plus 1 module 64. Data packets are
141 transmitted in sequence. There may only be one outstanding
142 unacknowledged data packet at a time. The sequence numbers
143 are independent in each direction. If an acknowledgement for
144 the previous packet is received (i.e., an acknowledgement with
145 the sequence number of the packet just sent) the packet just
146 sent should be retransmitted. If no acknowledgement is
147 received within a timeout period, the packet should be
148 retransmitted. This has an unfortunate failure condition on a
149 high-latency line, as a delayed acknowledgement may lead to an
150 endless series of duplicate packets.
152 DATA The actual data bytes follow. The following characters are
153 escaped inline with DLE (ASCII 020, or ^P):
159 The additional DLE characters are not counted in the logical
160 length stored in the TYPE_LEN and LEN1 bytes.
165 These bytes contain an 18 bit checksum of the complete
166 contents of the packet excluding the SEQ byte and the
167 CSUM[123] bytes. The checksum is simply the twos complement
168 addition of all the bytes treated as unsigned characters. The
169 values of the checksum bytes are:
170 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
171 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
172 CSUM3: 0x40 + (cksum & 0x3f)
174 It happens that the MIPS remote debugging protocol always
175 communicates with ASCII strings. Because of this, this
176 implementation doesn't bother to handle the DLE quoting mechanism,
177 since it will never be required. */
179 /* The SYN character which starts each packet. */
182 /* The 0x40 used to offset each packet (this value ensures that all of
183 the header and trailer bytes, other than SYN, are printable ASCII
185 #define HDR_OFFSET 0x40
187 /* The indices of the bytes in the packet header. */
188 #define HDR_INDX_SYN 0
189 #define HDR_INDX_TYPE_LEN 1
190 #define HDR_INDX_LEN1 2
191 #define HDR_INDX_SEQ 3
194 /* The data/ack bit in the TYPE_LEN header byte. */
195 #define TYPE_LEN_DA_BIT 0x20
196 #define TYPE_LEN_DATA 0
197 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
199 /* How to compute the header bytes. */
200 #define HDR_SET_SYN(data, len, seq) (SYN)
201 #define HDR_SET_TYPE_LEN(data, len, seq) \
203 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
204 + (((len) >> 6) & 0x1f))
205 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
206 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
208 /* Check that a header byte is reasonable. */
209 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
211 /* Get data from the header. These macros evaluate their argument
213 #define HDR_IS_DATA(hdr) \
214 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
215 #define HDR_GET_LEN(hdr) \
216 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
217 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
219 /* The maximum data length. */
220 #define DATA_MAXLEN 1023
222 /* The trailer offset. */
223 #define TRLR_OFFSET HDR_OFFSET
225 /* The indices of the bytes in the packet trailer. */
226 #define TRLR_INDX_CSUM1 0
227 #define TRLR_INDX_CSUM2 1
228 #define TRLR_INDX_CSUM3 2
229 #define TRLR_LENGTH 3
231 /* How to compute the trailer bytes. */
232 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
233 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
234 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
236 /* Check that a trailer byte is reasonable. */
237 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
239 /* Get data from the trailer. This evaluates its argument multiple
241 #define TRLR_GET_CKSUM(trlr) \
242 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
243 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
244 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
246 /* The sequence number modulos. */
247 #define SEQ_MODULOS (64)
249 /* Set to 1 if the target is open. */
250 static int mips_is_open
;
252 /* Set to 1 while the connection is being initialized. */
253 static int mips_initializing
;
255 /* The next sequence number to send. */
256 static int mips_send_seq
;
258 /* The next sequence number we expect to receive. */
259 static int mips_receive_seq
;
261 /* The time to wait before retransmitting a packet, in seconds. */
262 static int mips_retransmit_wait
= 3;
264 /* The number of times to try retransmitting a packet before giving up. */
265 static int mips_send_retries
= 10;
267 /* The number of garbage characters to accept when looking for an
268 SYN for the next packet. */
269 static int mips_syn_garbage
= 1050;
271 /* The time to wait for a packet, in seconds. */
272 static int mips_receive_wait
= 5;
274 /* Set if we have sent a packet to the board but have not yet received
276 static int mips_need_reply
= 0;
278 /* This can be set to get debugging with ``set remotedebug''. */
279 static int mips_debug
= 0;
281 /* Handle used to access serial I/O stream. */
282 static serial_t mips_desc
;
284 /* Read a character from the remote, aborting on error. Returns -2 on
285 timeout (since that's what serial_readchar returns). FIXME: If we
286 see the string "<IDT>" from the board, then we are debugging on the
287 main console port, and we have somehow dropped out of remote
288 debugging mode. In this case, we automatically go back in to
289 remote debugging mode. This is a hack, put in because I can't find
290 any way for a program running on the remote board to terminate
291 without also ending remote debugging mode. I assume users won't
292 have any trouble with this; for one thing, the IDT documentation
293 generally assumes that the remote debugging port is not the console
294 port. This is, however, very convenient for DejaGnu when you only
295 have one connected serial port. */
298 mips_readchar (timeout
)
302 static int state
= 0;
303 static char nextstate
[5] = { '<', 'I', 'D', 'T', '>' };
305 ch
= SERIAL_READCHAR (mips_desc
, timeout
);
306 if (ch
== SERIAL_EOF
)
307 error ("End of file from remote");
308 if (ch
== SERIAL_ERROR
)
309 error ("Error reading from remote: %s", safe_strerror (errno
));
312 if (ch
!= SERIAL_TIMEOUT
)
313 printf_filtered ("Read '%c' %d 0x%x\n", ch
, ch
, ch
);
315 printf_filtered ("Timed out in read\n");
318 /* If we have seen <IDT> and we either time out, or we see a @
319 (which was echoed from a packet we sent), reset the board as
320 described above. The first character in a packet after the SYN
321 (which is not echoed) is always an @ unless the packet is more
322 than 64 characters long, which ours never are. */
323 if ((ch
== SERIAL_TIMEOUT
|| ch
== '@')
325 && ! mips_initializing
)
328 printf_filtered ("Reinitializing MIPS debugging mode\n");
329 SERIAL_WRITE (mips_desc
, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
337 error ("Remote board reset");
340 if (ch
== nextstate
[state
])
348 /* Get a packet header, putting the data in the supplied buffer.
349 PGARBAGE is a pointer to the number of garbage characters received
350 so far. CH is the last character received. Returns 0 for success,
351 or -1 for timeout. */
354 mips_receive_header (hdr
, pgarbage
, ch
, timeout
)
364 /* Wait for a SYN. mips_syn_garbage is intended to prevent
365 sitting here indefinitely if the board sends us one garbage
366 character per second. ch may already have a value from the
367 last time through the loop. */
370 ch
= mips_readchar (timeout
);
375 /* Printing the character here lets the user of gdb see
376 what the program is outputting, if the debugging is
377 being done on the console port. FIXME: Perhaps this
378 should be filtered? */
379 if (! mips_initializing
|| mips_debug
> 0)
386 if (*pgarbage
> mips_syn_garbage
)
387 error ("Remote debugging protocol failure");
391 /* Get the packet header following the SYN. */
392 for (i
= 1; i
< HDR_LENGTH
; i
++)
394 ch
= mips_readchar (timeout
);
398 /* Make sure this is a header byte. */
399 if (ch
== SYN
|| ! HDR_CHECK (ch
))
405 /* If we got the complete header, we can return. Otherwise we
406 loop around and keep looking for SYN. */
412 /* Get a packet header, putting the data in the supplied buffer.
413 PGARBAGE is a pointer to the number of garbage characters received
414 so far. The last character read is returned in *PCH. Returns 0
415 for success, -1 for timeout, -2 for error. */
418 mips_receive_trailer (trlr
, pgarbage
, pch
, timeout
)
427 for (i
= 0; i
< TRLR_LENGTH
; i
++)
429 ch
= mips_readchar (timeout
);
433 if (! TRLR_CHECK (ch
))
440 /* Get the checksum of a packet. HDR points to the packet header.
441 DATA points to the packet data. LEN is the length of DATA. */
444 mips_cksum (hdr
, data
, len
)
445 const unsigned char *hdr
;
446 const unsigned char *data
;
449 register const unsigned char *p
;
455 /* The initial SYN is not included in the checksum. */
469 /* Send a packet containing the given ASCII string. */
472 mips_send_packet (s
, get_ack
)
477 unsigned char *packet
;
482 if (len
> DATA_MAXLEN
)
483 error ("MIPS protocol data packet too long: %s", s
);
485 packet
= (unsigned char *) alloca (HDR_LENGTH
+ len
+ TRLR_LENGTH
+ 1);
487 packet
[HDR_INDX_SYN
] = HDR_SET_SYN (1, len
, mips_send_seq
);
488 packet
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (1, len
, mips_send_seq
);
489 packet
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (1, len
, mips_send_seq
);
490 packet
[HDR_INDX_SEQ
] = HDR_SET_SEQ (1, len
, mips_send_seq
);
492 memcpy (packet
+ HDR_LENGTH
, s
, len
);
494 cksum
= mips_cksum (packet
, packet
+ HDR_LENGTH
, len
);
495 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
496 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
497 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
499 /* Increment the sequence number. This will set mips_send_seq to
500 the sequence number we expect in the acknowledgement. */
501 mips_send_seq
= (mips_send_seq
+ 1) % SEQ_MODULOS
;
506 /* We can only have one outstanding data packet, so we just wait for
507 the acknowledgement here. Keep retransmitting the packet until
508 we get one, or until we've tried too many times. */
509 for (try = 0; try < mips_send_retries
; try++)
516 packet
[HDR_LENGTH
+ len
+ TRLR_LENGTH
] = '\0';
517 printf_filtered ("Writing \"%s\"\n", packet
+ 1);
520 if (SERIAL_WRITE (mips_desc
, packet
, HDR_LENGTH
+ len
+ TRLR_LENGTH
))
521 error ("write to target failed: %s", safe_strerror (errno
));
527 unsigned char hdr
[HDR_LENGTH
+ 1];
528 unsigned char trlr
[TRLR_LENGTH
+ 1];
532 /* Get the packet header. If we time out, resend the data
534 err
= mips_receive_header (hdr
, &garbage
, ch
, mips_retransmit_wait
);
540 /* If we get a data packet, assume it is a duplicate and
541 ignore it. FIXME: If the acknowledgement is lost, this
542 data packet may be the packet the remote sends after the
544 if (HDR_IS_DATA (hdr
))
547 /* If the length is not 0, this is a garbled packet. */
548 if (HDR_GET_LEN (hdr
) != 0)
551 /* Get the packet trailer. */
552 err
= mips_receive_trailer (trlr
, &garbage
, &ch
,
553 mips_retransmit_wait
);
555 /* If we timed out, resend the data packet. */
559 /* If we got a bad character, reread the header. */
563 /* If the checksum does not match the trailer checksum, this
564 is a bad packet; ignore it. */
565 if (mips_cksum (hdr
, (unsigned char *) NULL
, 0)
566 != TRLR_GET_CKSUM (trlr
))
571 hdr
[HDR_LENGTH
] = '\0';
572 trlr
[TRLR_LENGTH
] = '\0';
573 printf_filtered ("Got ack %d \"%s%s\"\n",
574 HDR_GET_SEQ (hdr
), hdr
, trlr
);
577 /* If this ack is for the current packet, we're done. */
578 seq
= HDR_GET_SEQ (hdr
);
579 if (seq
== mips_send_seq
)
582 /* If this ack is for the last packet, resend the current
584 if ((seq
+ 1) % SEQ_MODULOS
== mips_send_seq
)
587 /* Otherwise this is a bad ack; ignore it. Increment the
588 garbage count to ensure that we do not stay in this loop
594 error ("Remote did not acknowledge packet");
597 /* Receive and acknowledge a packet, returning the data in BUFF (which
598 should be DATA_MAXLEN + 1 bytes). The protocol documentation
599 implies that only the sender retransmits packets, so this code just
600 waits silently for a packet. It returns the length of the received
604 mips_receive_packet (buff
)
610 unsigned char ack
[HDR_LENGTH
+ TRLR_LENGTH
+ 1];
617 unsigned char hdr
[HDR_LENGTH
];
618 unsigned char trlr
[TRLR_LENGTH
];
622 if (mips_receive_header (hdr
, &garbage
, ch
, mips_receive_wait
) != 0)
623 error ("Timed out waiting for remote packet");
627 /* An acknowledgement is probably a duplicate; ignore it. */
628 if (! HDR_IS_DATA (hdr
))
631 printf_filtered ("Ignoring unexpected ACK\n");
635 /* If this is the wrong sequence number, ignore it. */
636 if (HDR_GET_SEQ (hdr
) != mips_receive_seq
)
639 printf_filtered ("Ignoring sequence number %d (want %d)\n",
640 HDR_GET_SEQ (hdr
), mips_receive_seq
);
644 len
= HDR_GET_LEN (hdr
);
646 for (i
= 0; i
< len
; i
++)
650 rch
= mips_readchar (mips_receive_wait
);
657 error ("Timed out waiting for remote packet");
664 printf_filtered ("Got new SYN after %d chars (wanted %d)\n",
669 err
= mips_receive_trailer (trlr
, &garbage
, &ch
, mips_receive_wait
);
671 error ("Timed out waiting for packet");
675 printf_filtered ("Got SYN when wanted trailer\n");
679 if (mips_cksum (hdr
, buff
, len
) == TRLR_GET_CKSUM (trlr
))
683 printf_filtered ("Bad checksum; data %d, trailer %d\n",
684 mips_cksum (hdr
, buff
, len
),
685 TRLR_GET_CKSUM (trlr
));
687 /* The checksum failed. Send an acknowledgement for the
688 previous packet to tell the remote to resend the packet. */
689 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
690 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
691 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
692 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
694 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
696 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
697 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
698 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
702 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
703 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
707 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
))
708 error ("write to target failed: %s", safe_strerror (errno
));
714 printf_filtered ("Got packet \"%s\"\n", buff
);
717 /* We got the packet. Send an acknowledgement. */
718 mips_receive_seq
= (mips_receive_seq
+ 1) % SEQ_MODULOS
;
720 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
721 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
722 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
723 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
725 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
727 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
728 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
729 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
733 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
734 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
738 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
))
739 error ("write to target failed: %s", safe_strerror (errno
));
744 /* Optionally send a request to the remote system and optionally wait
745 for the reply. This implements the remote debugging protocol,
746 which is built on top of the packet protocol defined above. Each
747 request has an ADDR argument and a DATA argument. The following
748 requests are defined:
750 \0 don't send a request; just wait for a reply
751 i read word from instruction space at ADDR
752 d read word from data space at ADDR
753 I write DATA to instruction space at ADDR
754 D write DATA to data space at ADDR
755 r read register number ADDR
756 R set register number ADDR to value DATA
757 c continue execution (if ADDR != 1, set pc to ADDR)
758 s single step (if ADDR != 1, set pc to ADDR)
760 The read requests return the value requested. The write requests
761 return the previous value in the changed location. The execution
762 requests return a UNIX wait value (the approximate signal which
763 caused execution to stop is in the upper eight bits).
765 If PERR is not NULL, this function waits for a reply. If an error
766 occurs, it sets *PERR to 1 and sets errno according to what the
767 target board reports. */
770 mips_request (cmd
, addr
, data
, perr
)
776 char buff
[DATA_MAXLEN
+ 1];
786 fatal ("mips_request: Trying to send command before reply");
787 sprintf (buff
, "0x0 %c 0x%x 0x%x", cmd
, addr
, data
);
788 mips_send_packet (buff
, 1);
792 if (perr
== (int *) NULL
)
795 if (! mips_need_reply
)
796 fatal ("mips_request: Trying to get reply before command");
800 len
= mips_receive_packet (buff
);
803 if (sscanf (buff
, "0x%x %c 0x%x 0x%x",
804 &rpid
, &rcmd
, &rerrflg
, &rresponse
) != 4
806 || (cmd
!= '\0' && rcmd
!= cmd
))
807 error ("Bad response from remote board");
813 /* FIXME: This will returns MIPS errno numbers, which may or may
814 not be the same as errno values used on other systems. If
815 they stick to common errno values, they will be the same, but
816 if they don't, they must be translated. */
826 /* Initialize a new connection to the MIPS board, and make sure we are
835 char buff
[DATA_MAXLEN
+ 1];
838 if (mips_initializing
)
841 mips_initializing
= 1;
844 mips_receive_seq
= 0;
846 /* The board seems to want to send us a packet. I don't know what
847 it means. The packet seems to be triggered by a carriage return
848 character, although perhaps any character would do. */
850 SERIAL_WRITE (mip_desc
, &cr
, 1);
852 hold_wait
= mips_receive_wait
;
853 mips_receive_wait
= 3;
856 while (catch_errors (mips_receive_packet
, buff
, (char *) NULL
) == 0)
861 error ("Could not connect to target");
864 /* We did not receive the packet we expected; try resetting the
865 board and trying again. */
866 printf_filtered ("Failed to initialize; trying to reset board\n");
868 SERIAL_WRITE (mips_desc
, &cc
, 1);
870 SERIAL_WRITE (mips_desc
, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
873 SERIAL_WRITE (mips_desc
, &cr
, 1);
876 mips_receive_wait
= hold_wait
;
877 mips_initializing
= 0;
879 /* If this doesn't call error, we have connected; we don't care if
880 the request itself succeeds or fails. */
881 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err
);
884 /* Open a connection to the remote board. */
887 mips_open (name
, from_tty
)
893 "To open a MIPS remote debugging connection, you need to specify what serial\n\
894 device is attached to the target board (e.g., /dev/ttya).");
896 target_preopen (from_tty
);
899 unpush_target (&mips_ops
);
901 mips_desc
= SERIAL_OPEN (name
);
904 perror_with_name (name
);
906 SERIAL_RAW (mips_desc
);
913 printf ("Remote MIPS debugging using %s\n", name
);
914 push_target (&mips_ops
); /* Switch to using remote target now */
916 /* FIXME: Should we call start_remote here? */
919 /* Close a connection to the remote board. */
922 mips_close (quitting
)
931 /* Get the board out of remote debugging mode. */
932 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err
);
934 SERIAL_CLOSE (mips_desc
);
938 /* Detach from the remote board. */
941 mips_detach (args
, from_tty
)
946 error ("Argument given to \"detach\" when remotely debugging.");
950 printf ("Ending remote MIPS debugging.\n");
953 /* Tell the target board to resume. This does not wait for a reply
957 mips_resume (step
, siggnal
)
961 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
964 mips_request (step
? 's' : 'c',
970 /* Wait until the remote stops, and return a wait status. */
979 /* If we have not sent a single step or continue command, then the
980 board is waiting for us to do something. Return a status
981 indicating that it is stopped. */
982 if (! mips_need_reply
)
984 WSETSTOP (*status
, SIGTRAP
);
988 rstatus
= mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err
);
990 error ("Remote failure: %s", safe_strerror (errno
));
992 /* FIXME: The target board uses numeric signal values which are
993 those used on MIPS systems. If the host uses different signal
994 values, we need to translate here. I believe all Unix systems
995 use the same values for the signals the board can return, which
996 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
998 /* FIXME: The target board uses a standard Unix wait status int. If
999 the host system does not, we must translate here. */
1006 /* We have to map between the register numbers used by gdb and the
1007 register numbers used by the debugging protocol. This function
1008 assumes that we are using tm-mips.h. */
1010 #define REGNO_OFFSET 96
1013 mips_map_regno (regno
)
1018 if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
1019 return regno
- FP0_REGNUM
+ 32;
1023 return REGNO_OFFSET
+ 0;
1025 return REGNO_OFFSET
+ 1;
1027 return REGNO_OFFSET
+ 2;
1029 return REGNO_OFFSET
+ 3;
1031 return REGNO_OFFSET
+ 4;
1033 return REGNO_OFFSET
+ 5;
1035 /* FIXME: Is there a way to get the status register? */
1040 /* Fetch the remote registers. */
1043 mips_fetch_registers (regno
)
1051 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1052 mips_fetch_registers (regno
);
1056 val
= mips_request ('r', (unsigned int) mips_map_regno (regno
),
1057 (unsigned int) 0, &err
);
1059 error ("Can't read register %d: %s", regno
, safe_strerror (errno
));
1061 /* We got the number the register holds, but gdb expects to see a
1062 value in the target byte ordering. */
1063 SWAP_TARGET_AND_HOST (val
, sizeof (REGISTER_TYPE
));
1064 supply_register (regno
, (char *) &val
);
1067 /* Prepare to store registers. The MIPS protocol can store individual
1068 registers, so this function doesn't have to do anything. */
1071 mips_prepare_to_store ()
1075 /* Store remote register(s). */
1078 mips_store_registers (regno
)
1085 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1086 mips_store_registers (regno
);
1090 mips_request ('R', (unsigned int) mips_map_regno (regno
),
1091 (unsigned int) read_register (regno
),
1094 error ("Can't write register %d: %s", regno
, safe_strerror (errno
));
1097 /* Fetch a word from the target board. */
1100 mips_fetch_word (addr
)
1106 val
= mips_request ('d', (unsigned int) addr
, (unsigned int) 0, &err
);
1109 /* Data space failed; try instruction space. */
1110 val
= mips_request ('i', (unsigned int) addr
, (unsigned int) 0, &err
);
1112 error ("Can't read address 0x%x: %s", addr
, safe_strerror (errno
));
1117 /* Store a word to the target board. */
1120 mips_store_word (addr
, val
)
1126 mips_request ('D', (unsigned int) addr
, (unsigned int) val
, &err
);
1129 /* Data space failed; try instruction space. */
1130 mips_request ('I', (unsigned int) addr
, (unsigned int) val
, &err
);
1132 error ("Can't write address 0x%x: %s", addr
, safe_strerror (errno
));
1136 /* Read or write LEN bytes from inferior memory at MEMADDR,
1137 transferring to or from debugger address MYADDR. Write to inferior
1138 if SHOULD_WRITE is nonzero. Returns length of data written or
1139 read; 0 for error. Note that protocol gives us the correct value
1140 for a longword, since it transfers values in ASCII. We want the
1141 byte values, so we have to swap the longword values. */
1144 mips_xfer_memory (memaddr
, myaddr
, len
, write
, ignore
)
1149 struct target_ops
*ignore
;
1152 /* Round starting address down to longword boundary. */
1153 register CORE_ADDR addr
= memaddr
&~ 3;
1154 /* Round ending address up; get number of longwords that makes. */
1155 register int count
= (((memaddr
+ len
) - addr
) + 3) / 4;
1156 /* Allocate buffer of that many longwords. */
1157 register unsigned int *buffer
= (unsigned int *) alloca (count
* 4);
1161 /* Fill start and end extra bytes of buffer with existing data. */
1162 if (addr
!= memaddr
|| len
< 4)
1164 /* Need part of initial word -- fetch it. */
1165 buffer
[0] = mips_fetch_word (addr
);
1166 SWAP_TARGET_AND_HOST (buffer
, 4);
1169 if (count
> 1) /* FIXME, avoid if even boundary */
1171 buffer
[count
- 1] = mips_fetch_word (addr
+ (count
- 1) * 4);
1172 SWAP_TARGET_AND_HOST (buffer
+ (count
- 1) * 4, 4);
1175 /* Copy data to be written over corresponding part of buffer */
1177 memcpy ((char *) buffer
+ (memaddr
& 3), myaddr
, len
);
1179 /* Write the entire buffer. */
1181 for (i
= 0; i
< count
; i
++, addr
+= 4)
1183 SWAP_TARGET_AND_HOST (buffer
+ i
, 4);
1184 mips_store_word (addr
, buffer
[i
]);
1189 /* Read all the longwords */
1190 for (i
= 0; i
< count
; i
++, addr
+= 4)
1192 buffer
[i
] = mips_fetch_word (addr
);
1193 SWAP_TARGET_AND_HOST (buffer
+ i
, 4);
1197 /* Copy appropriate bytes out of the buffer. */
1198 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
1203 /* Print info on this target. */
1206 mips_files_info (ignore
)
1207 struct target_ops
*ignore
;
1209 printf ("Debugging a MIPS board over a serial line.\n");
1212 /* Kill the process running on the board. This will actually only
1213 work if we are doing remote debugging over the console input. I
1214 think that if IDT/sim had the remote debug interrupt enabled on the
1215 right port, we could interrupt the process with a break signal. */
1227 SERIAL_WRITE (mips_desc
, &cc
, 1);
1229 target_mourn_inferior ();
1234 /* Load an executable onto the board. */
1237 mips_load (args
, from_tty
)
1246 abfd
= bfd_openr (args
, 0);
1247 if (abfd
== (bfd
*) NULL
)
1248 error ("Unable to open file %s", args
);
1250 if (bfd_check_format (abfd
, bfd_object
) == 0)
1251 error ("%s: Not an object file", args
);
1254 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1256 if ((s
->flags
& SEC_LOAD
) != 0)
1260 size
= bfd_get_section_size_before_reloc (s
);
1264 struct cleanup
*old_chain
;
1267 buffer
= xmalloc (size
);
1268 old_chain
= make_cleanup (free
, buffer
);
1270 vma
= bfd_get_section_vma (abfd
, s
);
1271 printf_filtered ("Loading section %s, size 0x%x vma 0x%x\n",
1272 bfd_get_section_name (abfd
, s
), size
, vma
);
1273 bfd_get_section_contents (abfd
, s
, buffer
, 0, size
);
1274 mips_xfer_memory (vma
, buffer
, size
, 1, &mips_ops
);
1276 do_cleanups (old_chain
);
1278 if ((bfd_get_section_flags (abfd
, s
) & SEC_CODE
) != 0
1285 mips_request ('R', (unsigned int) mips_map_regno (PC_REGNUM
),
1286 (unsigned int) abfd
->start_address
,
1289 error ("Can't write PC register: %s", safe_strerror (errno
));
1293 /* FIXME: Should we call symbol_file_add here? The local variable
1294 text exists just for this call. Making the call seems to confuse
1295 gdb if more than one file is loaded in. Perhaps passing MAINLINE
1296 as 1 would fix this, but it's not clear that that is correct
1297 either since it is possible to load several files onto the board.
1299 symbol_file_add (args, from_tty, text, 0, 0, 0); */
1302 /* Start running on the target board. */
1305 mips_create_inferior (execfile
, args
, env
)
1313 error ("Can't pass arguments to remote MIPS board.");
1315 if (execfile
== 0 || exec_bfd
== 0)
1316 error ("No exec file specified");
1318 entry_pt
= (CORE_ADDR
) bfd_get_start_address (exec_bfd
);
1320 init_wait_for_inferior ();
1322 /* FIXME: Should we set inferior_pid here? */
1324 proceed (entry_pt
, -1, 0);
1327 /* Clean up after a process. Actually nothing to do. */
1330 mips_mourn_inferior ()
1332 generic_mourn_inferior ();
1335 /* The target vector. */
1337 struct target_ops mips_ops
=
1339 "mips", /* to_shortname */
1340 "Remote MIPS debugging over serial line", /* to_longname */
1341 "Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1342 Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */
1343 mips_open
, /* to_open */
1344 mips_close
, /* to_close */
1345 NULL
, /* to_attach */
1346 mips_detach
, /* to_detach */
1347 mips_resume
, /* to_resume */
1348 mips_wait
, /* to_wait */
1349 mips_fetch_registers
, /* to_fetch_registers */
1350 mips_store_registers
, /* to_store_registers */
1351 mips_prepare_to_store
, /* to_prepare_to_store */
1352 mips_xfer_memory
, /* to_xfer_memory */
1353 mips_files_info
, /* to_files_info */
1354 NULL
, /* to_insert_breakpoint */
1355 NULL
, /* to_remove_breakpoint */
1356 NULL
, /* to_terminal_init */
1357 NULL
, /* to_terminal_inferior */
1358 NULL
, /* to_terminal_ours_for_output */
1359 NULL
, /* to_terminal_ours */
1360 NULL
, /* to_terminal_info */
1361 mips_kill
, /* to_kill */
1362 mips_load
, /* to_load */
1363 NULL
, /* to_lookup_symbol */
1364 mips_create_inferior
, /* to_create_inferior */
1365 mips_mourn_inferior
, /* to_mourn_inferior */
1366 NULL
, /* to_can_run */
1367 NULL
, /* to_notice_signals */
1368 process_stratum
, /* to_stratum */
1370 1, /* to_has_all_memory */
1371 1, /* to_has_memory */
1372 1, /* to_has_stack */
1373 1, /* to_has_registers */
1374 1, /* to_has_execution */
1375 NULL
, /* sections */
1376 NULL
, /* sections_end */
1377 OPS_MAGIC
/* to_magic */
1381 _initialize_remote_mips ()
1383 add_target (&mips_ops
);
1386 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *) &mips_debug
,
1387 "Set debugging of remote MIPS serial I/O.\n\
1388 When non-zero, each packet sent or received with the remote target\n\
1389 is displayed. Higher numbers produce more debugging.", &setlist
),
This page took 0.138283 seconds and 4 git commands to generate.