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
));
54 mips_receive_packet
PARAMS ((char *buff
));
57 mips_request
PARAMS ((char cmd
, unsigned int addr
, unsigned int data
,
61 mips_open
PARAMS ((char *name
, int from_tty
));
64 mips_close
PARAMS ((int quitting
));
67 mips_detach
PARAMS ((char *args
, int from_tty
));
70 mips_resume
PARAMS ((int step
, int siggnal
));
73 mips_wait
PARAMS ((WAITTYPE
*status
));
76 mips_map_regno
PARAMS ((int regno
));
79 mips_fetch_registers
PARAMS ((int regno
));
82 mips_prepare_to_store
PARAMS ((void));
85 mips_store_registers
PARAMS ((int regno
));
88 mips_fetch_word
PARAMS ((CORE_ADDR addr
));
91 mips_store_word
PARAMS ((CORE_ADDR addr
, int value
));
94 mips_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
,
95 int write
, struct target_ops
*ignore
));
98 mips_files_info
PARAMS ((struct target_ops
*ignore
));
101 mips_load
PARAMS ((char *args
, int from_tty
));
104 mips_create_inferior
PARAMS ((char *execfile
, char *args
, char **env
));
107 mips_mourn_inferior
PARAMS ((void));
109 /* A forward declaration. */
110 extern struct target_ops mips_ops
;
112 /* The MIPS remote debugging interface is built on top of a simple
113 packet protocol. Each packet is organized as follows:
115 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
116 may not appear anywhere else in the packet. Any time a SYN is
117 seen, a new packet should be assumed to have begun.
120 This byte contains the upper five bits of the logical length
121 of the data section, plus a single bit indicating whether this
122 is a data packet or an acknowledgement. The documentation
123 indicates that this bit is 1 for a data packet, but the actual
124 board uses 1 for an acknowledgement. The value of the byte is
125 0x40 + (ack ? 0x20 : 0) + (len >> 6)
126 (we always have 0 <= len < 1024). Acknowledgement packets do
127 not carry data, and must have a data length of 0.
129 LEN1 This byte contains the lower six bits of the logical length of
130 the data section. The value is
133 SEQ This byte contains the six bit sequence number of the packet.
136 An acknowlegment packet contains the sequence number of the
137 packet being acknowledged plus 1 module 64. Data packets are
138 transmitted in sequence. There may only be one outstanding
139 unacknowledged data packet at a time. The sequence numbers
140 are independent in each direction. If an acknowledgement for
141 the previous packet is received (i.e., an acknowledgement with
142 the sequence number of the packet just sent) the packet just
143 sent should be retransmitted. If no acknowledgement is
144 received within a timeout period, the packet should be
145 retransmitted. This has an unfortunate failure condition on a
146 high-latency line, as a delayed acknowledgement may lead to an
147 endless series of duplicate packets.
149 DATA The actual data bytes follow. The following characters are
150 escaped inline with DLE (ASCII 020, or ^P):
156 The additional DLE characters are not counted in the logical
157 length stored in the TYPE_LEN and LEN1 bytes.
162 These bytes contain an 18 bit checksum of the complete
163 contents of the packet excluding the SEQ byte and the
164 CSUM[123] bytes. The checksum is simply the twos complement
165 addition of all the bytes treated as unsigned characters. The
166 values of the checksum bytes are:
167 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
168 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
169 CSUM3: 0x40 + (cksum & 0x3f)
171 It happens that the MIPS remote debugging protocol always
172 communicates with ASCII strings. Because of this, this
173 implementation doesn't bother to handle the DLE quoting mechanism,
174 since it will never be required. */
176 /* The SYN character which starts each packet. */
179 /* The 0x40 used to offset each packet (this value ensures that all of
180 the header and trailer bytes, other than SYN, are printable ASCII
182 #define HDR_OFFSET 0x40
184 /* The indices of the bytes in the packet header. */
185 #define HDR_INDX_SYN 0
186 #define HDR_INDX_TYPE_LEN 1
187 #define HDR_INDX_LEN1 2
188 #define HDR_INDX_SEQ 3
191 /* The data/ack bit in the TYPE_LEN header byte. */
192 #define TYPE_LEN_DA_BIT 0x20
193 #define TYPE_LEN_DATA 0
194 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
196 /* How to compute the header bytes. */
197 #define HDR_SET_SYN(data, len, seq) (SYN)
198 #define HDR_SET_TYPE_LEN(data, len, seq) \
200 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
201 + (((len) >> 6) & 0x1f))
202 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
203 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
205 /* Check that a header byte is reasonable. */
206 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
208 /* Get data from the header. These macros evaluate their argument
210 #define HDR_IS_DATA(hdr) \
211 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
212 #define HDR_GET_LEN(hdr) \
213 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
214 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
216 /* The maximum data length. */
217 #define DATA_MAXLEN 1023
219 /* The trailer offset. */
220 #define TRLR_OFFSET HDR_OFFSET
222 /* The indices of the bytes in the packet trailer. */
223 #define TRLR_INDX_CSUM1 0
224 #define TRLR_INDX_CSUM2 1
225 #define TRLR_INDX_CSUM3 2
226 #define TRLR_LENGTH 3
228 /* How to compute the trailer bytes. */
229 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
230 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
231 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
233 /* Check that a trailer byte is reasonable. */
234 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
236 /* Get data from the trailer. This evaluates its argument multiple
238 #define TRLR_GET_CKSUM(trlr) \
239 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
240 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
241 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
243 /* The sequence number modulos. */
244 #define SEQ_MODULOS (64)
246 /* Set to 1 if the target is open. */
247 static int mips_is_open
;
249 /* The next sequence number to send. */
250 static int mips_send_seq
;
252 /* The next sequence number we expect to receive. */
253 static int mips_receive_seq
;
255 /* The time to wait before retransmitting a packet, in seconds. */
256 static int mips_retransmit_wait
= 3;
258 /* The number of times to try retransmitting a packet before giving up. */
259 static int mips_send_retries
= 10;
261 /* The number of garbage characters to accept when looking for an
262 SYN for the next packet. */
263 static int mips_syn_garbage
= 1050;
265 /* The time to wait for a packet, in seconds. */
266 static int mips_receive_wait
= 30;
268 /* Set if we have sent a packet to the board but have not yet received
270 static int mips_need_reply
= 0;
272 /* This can be set to get debugging with ``set remotedebug''. */
273 static int mips_debug
= 0;
275 /* Read a character from the remote, aborting on error. Returns -2 on
276 timeout (since that's what serial_readchar returns). */
279 mips_readchar (timeout
)
284 ch
= serial_readchar (timeout
);
286 error ("End of file from remote");
288 error ("Error reading from remote: %s", safe_strerror (errno
));
292 printf_filtered ("Read '%c' %d 0x%x\n", ch
, ch
, ch
);
294 printf_filtered ("Timed out in read\n");
299 /* Get a packet header, putting the data in the supplied buffer.
300 PGARBAGE is a pointer to the number of garbage characters received
301 so far. CH is the last character received. Returns 0 for success,
302 or -1 for timeout. */
305 mips_receive_header (hdr
, pgarbage
, ch
, timeout
)
315 /* Wait for a SYN. mips_syn_garbage is intended to prevent
316 sitting here indefinitely if the board sends us one garbage
317 character per second. ch may already have a value from the
318 last time through the loop. */
321 ch
= mips_readchar (timeout
);
326 /* Printing the character here lets the user of gdb see
327 what the program is outputting, if the debugging is
328 being done on the console port. FIXME: Perhaps this
329 should be filtered? */
333 if (*pgarbage
> mips_syn_garbage
)
334 error ("Remote debugging protocol failure");
338 /* Get the packet header following the SYN. */
339 for (i
= 1; i
< HDR_LENGTH
; i
++)
341 ch
= mips_readchar (timeout
);
345 /* Make sure this is a header byte. */
346 if (ch
== SYN
|| ! HDR_CHECK (ch
))
352 /* If we got the complete header, we can return. Otherwise we
353 loop around and keep looking for SYN. */
359 /* Get a packet header, putting the data in the supplied buffer.
360 PGARBAGE is a pointer to the number of garbage characters received
361 so far. The last character read is returned in *PCH. Returns 0
362 for success, -1 for timeout, -2 for error. */
365 mips_receive_trailer (trlr
, pgarbage
, pch
, timeout
)
374 for (i
= 0; i
< TRLR_LENGTH
; i
++)
376 ch
= mips_readchar (timeout
);
380 if (! TRLR_CHECK (ch
))
387 /* Get the checksum of a packet. HDR points to the packet header.
388 DATA points to the packet data. LEN is the length of DATA. */
391 mips_cksum (hdr
, data
, len
)
392 const unsigned char *hdr
;
393 const unsigned char *data
;
396 register const unsigned char *p
;
402 /* The initial SYN is not included in the checksum. */
416 /* Send a packet containing the given ASCII string. */
423 unsigned char *packet
;
428 if (len
> DATA_MAXLEN
)
429 error ("MIPS protocol data packet too long: %s", s
);
431 packet
= (unsigned char *) alloca (HDR_LENGTH
+ len
+ TRLR_LENGTH
+ 1);
433 packet
[HDR_INDX_SYN
] = HDR_SET_SYN (1, len
, mips_send_seq
);
434 packet
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (1, len
, mips_send_seq
);
435 packet
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (1, len
, mips_send_seq
);
436 packet
[HDR_INDX_SEQ
] = HDR_SET_SEQ (1, len
, mips_send_seq
);
438 memcpy (packet
+ HDR_LENGTH
, s
, len
);
440 cksum
= mips_cksum (packet
, packet
+ HDR_LENGTH
, len
);
441 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
442 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
443 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
445 /* Increment the sequence number. This will set mips_send_seq to
446 the sequence number we expect in the acknowledgement. */
447 mips_send_seq
= (mips_send_seq
+ 1) % SEQ_MODULOS
;
449 /* We can only have one outstanding data packet, so we just wait for
450 the acknowledgement here. Keep retransmitting the packet until
451 we get one, or until we've tried too many times. */
452 for (try = 0; try < mips_send_retries
; try++)
459 packet
[HDR_LENGTH
+ len
+ TRLR_LENGTH
] = '\0';
460 printf_filtered ("Writing \"%s\"\n", packet
+ 1);
463 if (serial_write (packet
, HDR_LENGTH
+ len
+ TRLR_LENGTH
) == 0)
464 error ("write to target failed: %s", safe_strerror (errno
));
470 unsigned char hdr
[HDR_LENGTH
+ 1];
471 unsigned char trlr
[TRLR_LENGTH
+ 1];
475 /* Get the packet header. If we time out, resend the data
477 err
= mips_receive_header (hdr
, &garbage
, ch
, mips_retransmit_wait
);
483 /* If we get a data packet, assume it is a duplicate and
484 ignore it. FIXME: If the acknowledgement is lost, this
485 data packet may be the packet the remote sends after the
487 if (HDR_IS_DATA (hdr
))
490 /* If the length is not 0, this is a garbled packet. */
491 if (HDR_GET_LEN (hdr
) != 0)
494 /* Get the packet trailer. */
495 err
= mips_receive_trailer (trlr
, &garbage
, &ch
,
496 mips_retransmit_wait
);
498 /* If we timed out, resend the data packet. */
502 /* If we got a bad character, reread the header. */
506 /* If the checksum does not match the trailer checksum, this
507 is a bad packet; ignore it. */
508 if (mips_cksum (hdr
, (unsigned char *) NULL
, 0)
509 != TRLR_GET_CKSUM (trlr
))
514 hdr
[HDR_LENGTH
] = '\0';
515 trlr
[TRLR_LENGTH
] = '\0';
516 printf_filtered ("Got ack %d \"%s%s\"\n",
517 HDR_GET_SEQ (hdr
), hdr
, trlr
);
520 /* If this ack is for the current packet, we're done. */
521 seq
= HDR_GET_SEQ (hdr
);
522 if (seq
== mips_send_seq
)
525 /* If this ack is for the last packet, resend the current
527 if ((seq
+ 1) % SEQ_MODULOS
== mips_send_seq
)
530 /* Otherwise this is a bad ack; ignore it. Increment the
531 garbage count to ensure that we do not stay in this loop
537 error ("Remote did not acknowledge packet");
540 /* Receive and acknowledge a packet, returning the data in BUFF (which
541 should be DATA_MAXLEN + 1 bytes). The protocol documentation
542 implies that only the sender retransmits packets, so this code just
543 waits silently for a packet. It returns the length of the received
547 mips_receive_packet (buff
)
553 unsigned char ack
[HDR_LENGTH
+ TRLR_LENGTH
+ 1];
560 unsigned char hdr
[HDR_LENGTH
];
561 unsigned char trlr
[TRLR_LENGTH
];
565 if (mips_receive_header (hdr
, &garbage
, ch
, mips_receive_wait
) != 0)
566 error ("Timed out waiting for remote packet");
570 /* An acknowledgement is probably a duplicate; ignore it. */
571 if (! HDR_IS_DATA (hdr
))
574 printf_filtered ("Ignoring unexpected ACK\n");
578 /* If this is the wrong sequence number, ignore it. */
579 if (HDR_GET_SEQ (hdr
) != mips_receive_seq
)
582 printf_filtered ("Ignoring sequence number %d (want %d)\n",
583 HDR_GET_SEQ (hdr
), mips_receive_seq
);
587 len
= HDR_GET_LEN (hdr
);
589 for (i
= 0; i
< len
; i
++)
593 rch
= mips_readchar (mips_receive_wait
);
600 error ("Timed out waiting for remote packet");
607 printf_filtered ("Got new SYN after %d chars (wanted %d)\n",
612 err
= mips_receive_trailer (trlr
, &garbage
, &ch
, mips_receive_wait
);
614 error ("Timed out waiting for packet");
618 printf_filtered ("Got SYN when wanted trailer\n");
622 if (mips_cksum (hdr
, buff
, len
) == TRLR_GET_CKSUM (trlr
))
626 printf_filtered ("Bad checksum; data %d, trailer %d\n",
627 mips_cksum (hdr
, buff
, len
),
628 TRLR_GET_CKSUM (trlr
));
630 /* The checksum failed. Send an acknowledgement for the
631 previous packet to tell the remote to resend the packet. */
632 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
633 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
634 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
635 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
637 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
639 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
640 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
641 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
645 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
646 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
650 if (serial_write (ack
, HDR_LENGTH
+ TRLR_LENGTH
) == 0)
651 error ("write to target failed: %s", safe_strerror (errno
));
657 printf_filtered ("Got packet \"%s\"\n", buff
);
660 /* We got the packet. Send an acknowledgement. */
661 mips_receive_seq
= (mips_receive_seq
+ 1) % SEQ_MODULOS
;
663 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
664 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
665 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
666 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
668 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
670 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
671 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
672 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
676 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
677 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
681 if (serial_write (ack
, HDR_LENGTH
+ TRLR_LENGTH
) == 0)
682 error ("write to target failed: %s", safe_strerror (errno
));
687 /* Optionally send a request to the remote system and optionally wait
688 for the reply. This implements the remote debugging protocol,
689 which is built on top of the packet protocol defined above. Each
690 request has an ADDR argument and a DATA argument. The following
691 requests are defined:
693 \0 don't send a request; just wait for a reply
694 i read word from instruction space at ADDR
695 d read word from data space at ADDR
696 I write DATA to instruction space at ADDR
697 D write DATA to data space at ADDR
698 r read register number ADDR
699 R set register number ADDR to value DATA
700 c continue execution (if ADDR != 1, set pc to ADDR)
701 s single step (if ADDR != 1, set pc to ADDR)
703 The read requests return the value requested. The write requests
704 return the previous value in the changed location. The execution
705 requests return a UNIX wait value (the approximate signal which
706 caused execution to stop is in the upper eight bits).
708 If PERR is not NULL, this function waits for a reply. If an error
709 occurs, it sets *PERR to 1 and sets errno according to what the
710 target board reports. */
713 mips_request (cmd
, addr
, data
, perr
)
719 char buff
[DATA_MAXLEN
+ 1];
729 fatal ("mips_request: Trying to send command before reply");
730 sprintf (buff
, "0x0 %c 0x%x 0x%x", cmd
, addr
, data
);
731 mips_send_packet (buff
);
735 if (perr
== (int *) NULL
)
738 if (! mips_need_reply
)
739 fatal ("mips_request: Trying to get reply before command");
743 len
= mips_receive_packet (buff
);
746 if (sscanf (buff
, "0x%x %c 0x%x 0x%x",
747 &rpid
, &rcmd
, &rerrflg
, &rresponse
) != 4
749 || (cmd
!= '\0' && rcmd
!= cmd
))
750 error ("Bad response from remote board");
756 /* FIXME: This will returns MIPS errno numbers, which may or may
757 not be the same as errno values used on other systems. If
758 they stick to common errno values, they will be the same, but
759 if they don't, they must be translated. */
769 /* Open a connection to the remote board. */
772 mips_open (name
, from_tty
)
778 char buff
[DATA_MAXLEN
+ 1];
782 "To open a MIPS remote debugging connection, you need to specify what serial\n\
783 device is attached to the target board (e.g., /dev/ttya).");
785 target_preopen (from_tty
);
790 if (serial_open (name
) == 0)
791 perror_with_name (name
);
795 /* The board seems to want to send us a packet. I don't know what
798 serial_write (&cr
, 1);
799 mips_receive_packet (buff
);
801 /* If this doesn't call error, we have connected; we don't care if
802 the request itself succeeds or fails. */
803 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err
);
806 printf ("Remote MIPS debugging using %s\n", name
);
807 push_target (&mips_ops
); /* Switch to using remote target now */
809 start_remote (); /* Initialize gdb process mechanisms */
812 /* Close a connection to the remote board. */
815 mips_close (quitting
)
820 /* Get the board out of remote debugging mode. */
821 mips_request ('x', (unsigned int) 0, (unsigned int) 0,
828 /* Detach from the remote board. */
831 mips_detach (args
, from_tty
)
836 error ("Argument given to \"detach\" when remotely debugging.");
840 printf ("Ending remote MIPS debugging.\n");
843 /* Tell the target board to resume. This does not wait for a reply
847 mips_resume (step
, siggnal
)
851 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
854 mips_request (step
? 's' : 'c',
855 (unsigned int) read_register (PC_REGNUM
),
860 /* Wait until the remote stops, and return a wait status. */
869 /* If we have not sent a single step or continue command, then the
870 board is waiting for us to do something. Return a status
871 indicating that it is stopped. */
872 if (! mips_need_reply
)
874 WSETSTOP (*status
, SIGTRAP
);
878 rstatus
= mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err
);
880 error ("Remote failure: %s", safe_strerror (errno
));
882 /* FIXME: The target board uses numeric signal values which are
883 those used on MIPS systems. If the host uses different signal
884 values, we need to translate here. I believe all Unix systems
885 use the same values for the signals the board can return, which
886 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
888 /* FIXME: The target board uses a standard Unix wait status int. If
889 the host system does not, we must translate here. */
896 /* We have to map between the register numbers used by gdb and the
897 register numbers used by the debugging protocol. This function
898 assumes that we are using tm-mips.h. */
900 #define REGNO_OFFSET 96
903 mips_map_regno (regno
)
908 if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
909 return regno
- FP0_REGNUM
+ 32;
913 return REGNO_OFFSET
+ 0;
915 return REGNO_OFFSET
+ 1;
917 return REGNO_OFFSET
+ 2;
919 return REGNO_OFFSET
+ 3;
921 return REGNO_OFFSET
+ 4;
923 return REGNO_OFFSET
+ 5;
925 /* FIXME: Is there a way to get the status register? */
930 /* Fetch the remote registers. */
933 mips_fetch_registers (regno
)
941 for (regno
= 0; regno
< NUM_REGS
; regno
++)
942 mips_fetch_registers (regno
);
946 val
= mips_request ('r', (unsigned int) mips_map_regno (regno
),
947 (unsigned int) 0, &err
);
949 error ("Can't read register %d: %s", regno
, safe_strerror (errno
));
951 /* We got the number the register holds, but gdb expects to see a
952 value in the target byte ordering. */
953 SWAP_TARGET_AND_HOST (val
, sizeof (REGISTER_TYPE
));
954 supply_register (regno
, (char *) &val
);
957 /* Prepare to store registers. The MIPS protocol can store individual
958 registers, so this function doesn't have to do anything. */
961 mips_prepare_to_store ()
965 /* Store remote register(s). */
968 mips_store_registers (regno
)
975 for (regno
= 0; regno
< NUM_REGS
; regno
++)
976 mips_store_registers (regno
);
980 mips_request ('R', (unsigned int) mips_map_regno (regno
),
981 (unsigned int) read_register (regno
),
984 error ("Can't write register %d: %s", regno
, safe_strerror (errno
));
987 /* Fetch a word from the target board. */
990 mips_fetch_word (addr
)
996 val
= mips_request ('d', (unsigned int) addr
, (unsigned int) 0, &err
);
999 /* Data space failed; try instruction space. */
1000 val
= mips_request ('i', (unsigned int) addr
, (unsigned int) 0, &err
);
1002 error ("Can't read address 0x%x: %s", addr
, safe_strerror (errno
));
1007 /* Store a word to the target board. */
1010 mips_store_word (addr
, val
)
1016 mips_request ('D', (unsigned int) addr
, (unsigned int) val
, &err
);
1019 /* Data space failed; try instruction space. */
1020 mips_request ('I', (unsigned int) addr
, (unsigned int) val
, &err
);
1022 error ("Can't write address 0x%x: %s", addr
, safe_strerror (errno
));
1026 /* Read or write LEN bytes from inferior memory at MEMADDR,
1027 transferring to or from debugger address MYADDR. Write to inferior
1028 if SHOULD_WRITE is nonzero. Returns length of data written or
1029 read; 0 for error. Note that protocol gives us the correct value
1030 for a longword, since it transfers values in ASCII. We want the
1031 byte values, so we have to swap the longword values. */
1034 mips_xfer_memory (memaddr
, myaddr
, len
, write
, ignore
)
1039 struct target_ops
*ignore
;
1042 /* Round starting address down to longword boundary. */
1043 register CORE_ADDR addr
= memaddr
&~ 3;
1044 /* Round ending address up; get number of longwords that makes. */
1045 register int count
= (((memaddr
+ len
) - addr
) + 3) / 4;
1046 /* Allocate buffer of that many longwords. */
1047 register unsigned int *buffer
= (unsigned int *) alloca (count
* 4);
1051 /* Fill start and end extra bytes of buffer with existing data. */
1052 if (addr
!= memaddr
|| len
< 4)
1054 /* Need part of initial word -- fetch it. */
1055 buffer
[0] = mips_fetch_word (addr
);
1056 SWAP_TARGET_AND_HOST (buffer
, 4);
1059 if (count
> 1) /* FIXME, avoid if even boundary */
1061 buffer
[count
- 1] = mips_fetch_word (addr
+ (count
- 1) * 4);
1062 SWAP_TARGET_AND_HOST (buffer
+ (count
- 1) * 4, 4);
1065 /* Copy data to be written over corresponding part of buffer */
1067 memcpy ((char *) buffer
+ (memaddr
& 3), myaddr
, len
);
1069 /* Write the entire buffer. */
1071 for (i
= 0; i
< count
; i
++, addr
+= 4)
1073 SWAP_TARGET_AND_HOST (buffer
+ i
, 4);
1074 mips_store_word (addr
, buffer
[i
]);
1079 /* Read all the longwords */
1080 for (i
= 0; i
< count
; i
++, addr
+= 4)
1082 buffer
[i
] = mips_fetch_word (addr
);
1083 SWAP_TARGET_AND_HOST (buffer
+ i
, 4);
1087 /* Copy appropriate bytes out of the buffer. */
1088 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
1093 /* Print info on this target. */
1096 mips_files_info (ignore
)
1097 struct target_ops
*ignore
;
1099 printf ("Debugging a MIPS board over a serial line.\n");
1102 /* Load an executable onto the board. */
1105 mips_load (args
, from_tty
)
1113 abfd
= bfd_openr (args
, 0);
1114 if (abfd
== (bfd
*) NULL
)
1115 error ("Unable to open file %s", args
);
1117 if (bfd_check_format (abfd
, bfd_object
) == 0)
1118 error ("%s: Not an object file", args
);
1120 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1122 if ((s
->flags
& SEC_LOAD
) != 0)
1126 size
= bfd_get_section_size_before_reloc (s
);
1130 struct cleanup
*old_chain
;
1133 buffer
= xmalloc (size
);
1134 old_chain
= make_cleanup (free
, buffer
);
1136 vma
= bfd_get_section_vma (abfd
, s
);
1137 printf_filtered ("Loading section %s, size 0x%x vma 0x%x\n",
1138 bfd_get_section_name (abfd
, s
), size
, vma
);
1139 bfd_get_section_contents (abfd
, s
, buffer
, 0, size
);
1140 mips_xfer_memory (vma
, buffer
, size
, 1, &mips_ops
);
1142 do_cleanups (old_chain
);
1147 mips_request ('R', (unsigned int) mips_map_regno (PC_REGNUM
),
1148 (unsigned int) abfd
->start_address
,
1151 error ("Can't write PC register: %s", safe_strerror (errno
));
1155 /* FIXME: Should we call symbol_file_add here? */
1158 /* Start running on the target board. */
1161 mips_create_inferior (execfile
, args
, env
)
1168 /* FIXME: Actually, we probably could pass arguments. */
1170 error ("Can't pass arguments to remote MIPS board.");
1172 if (execfile
== 0 || exec_bfd
== 0)
1173 error ("No exec file specified");
1175 entry_pt
= (CORE_ADDR
) bfd_get_start_address (exec_bfd
);
1177 init_wait_for_inferior ();
1179 proceed (entry_pt
, -1, 0);
1182 /* Clean up after a process. Actually nothing to do. */
1185 mips_mourn_inferior ()
1187 generic_mourn_inferior ();
1190 /* The target vector. */
1192 struct target_ops mips_ops
=
1194 "mips", /* to_shortname */
1195 "Remote MIPS debugging over serial line", /* to_longname */
1196 "Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1197 Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */
1198 mips_open
, /* to_open */
1199 mips_close
, /* to_close */
1200 NULL
, /* to_attach */
1201 mips_detach
, /* to_detach */
1202 mips_resume
, /* to_resume */
1203 mips_wait
, /* to_wait */
1204 mips_fetch_registers
, /* to_fetch_registers */
1205 mips_store_registers
, /* to_store_registers */
1206 mips_prepare_to_store
, /* to_prepare_to_store */
1207 mips_xfer_memory
, /* to_xfer_memory */
1208 mips_files_info
, /* to_files_info */
1209 NULL
, /* to_insert_breakpoint */
1210 NULL
, /* to_remove_breakpoint */
1211 NULL
, /* to_terminal_init */
1212 NULL
, /* to_terminal_inferior */
1213 NULL
, /* to_terminal_ours_for_output */
1214 NULL
, /* to_terminal_ours */
1215 NULL
, /* to_terminal_info */
1217 mips_load
, /* to_load */
1218 NULL
, /* to_lookup_symbol */
1219 mips_create_inferior
, /* to_create_inferior */
1220 mips_mourn_inferior
, /* to_mourn_inferior */
1221 NULL
, /* to_can_run */
1222 NULL
, /* to_notice_signals */
1223 process_stratum
, /* to_stratum */
1225 1, /* to_has_all_memory */
1226 1, /* to_has_memory */
1227 1, /* to_has_stack */
1228 1, /* to_has_registers */
1229 1, /* to_has_execution */
1230 NULL
, /* sections */
1231 NULL
, /* sections_end */
1232 OPS_MAGIC
/* to_magic */
1236 _initialize_remote_mips ()
1238 add_target (&mips_ops
);
1241 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *) &mips_debug
,
1242 "Set debugging of remote MIPS serial I/O.\n\
1243 When non-zero, each packet sent or received with the remote target\n\
1244 is displayed. Higher numbers produce more debugging.", &setlist
),
This page took 0.055293 seconds and 5 git commands to generate.