Move the ``set mask-address'' command to remote-mips.c. Disable
[deliverable/binutils-gdb.git] / gdb / remote-mips.c
1 /* Remote debugging interface for MIPS remote debugging protocol.
2 Copyright 1993, 1994, 1995, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Ian Lance Taylor
4 <ian@cygnus.com>.
5
6 This file is part of GDB.
7
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.
12
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.
17
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,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "gdb_wait.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "serial.h"
31 #include "target.h"
32 #include "remote-utils.h"
33 #include "gdb_string.h"
34
35 #include <signal.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include <ctype.h>
40
41 /* Microsoft C's stat.h doesn't define all the POSIX file modes. */
42 #ifndef S_IROTH
43 #define S_IROTH S_IREAD
44 #endif
45
46 \f
47
48 /* Breakpoint types. Values 0, 1, and 2 must agree with the watch
49 types passed by breakpoint.c to target_insert_watchpoint.
50 Value 3 is our own invention, and is used for ordinary instruction
51 breakpoints. Value 4 is used to mark an unused watchpoint in tables. */
52 enum break_type
53 {
54 BREAK_WRITE, /* 0 */
55 BREAK_READ, /* 1 */
56 BREAK_ACCESS, /* 2 */
57 BREAK_FETCH, /* 3 */
58 BREAK_UNUSED /* 4 */
59 };
60
61 /* Prototypes for local functions. */
62
63 static int mips_readchar (int timeout);
64
65 static int mips_receive_header (unsigned char *hdr, int *pgarbage,
66 int ch, int timeout);
67
68 static int mips_receive_trailer (unsigned char *trlr, int *pgarbage,
69 int *pch, int timeout);
70
71 static int mips_cksum (const unsigned char *hdr,
72 const unsigned char *data, int len);
73
74 static void mips_send_packet (const char *s, int get_ack);
75
76 static void mips_send_command (const char *cmd, int prompt);
77
78 static int mips_receive_packet (char *buff, int throw_error, int timeout);
79
80 static ULONGEST mips_request (int cmd, ULONGEST addr, ULONGEST data,
81 int *perr, int timeout, char *buff);
82
83 static void mips_initialize (void);
84
85 static void mips_open (char *name, int from_tty);
86
87 static void pmon_open (char *name, int from_tty);
88
89 static void ddb_open (char *name, int from_tty);
90
91 static void lsi_open (char *name, int from_tty);
92
93 static void mips_close (int quitting);
94
95 static void mips_detach (char *args, int from_tty);
96
97 static void mips_resume (int pid, int step, enum target_signal siggnal);
98
99 static int mips_wait (int pid, struct target_waitstatus *status);
100
101 static int mips_map_regno (int regno);
102
103 static void mips_fetch_registers (int regno);
104
105 static void mips_prepare_to_store (void);
106
107 static void mips_store_registers (int regno);
108
109 static unsigned int mips_fetch_word (CORE_ADDR addr);
110
111 static int mips_store_word (CORE_ADDR addr, unsigned int value,
112 char *old_contents);
113
114 static int mips_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
115 int write, struct target_ops *ignore);
116
117 static void mips_files_info (struct target_ops *ignore);
118
119 static void mips_create_inferior (char *execfile, char *args, char **env);
120
121 static void mips_mourn_inferior (void);
122
123 static int pmon_makeb64 (unsigned long v, char *p, int n, int *chksum);
124
125 static int pmon_zeroset (int recsize, char **buff, int *amount,
126 unsigned int *chksum);
127
128 static int pmon_checkset (int recsize, char **buff, int *value);
129
130 static void pmon_make_fastrec (char **outbuf, unsigned char *inbuf,
131 int *inptr, int inamount, int *recsize,
132 unsigned int *csum, unsigned int *zerofill);
133
134 static int pmon_check_ack (char *mesg);
135
136 static void pmon_start_download (void);
137
138 static void pmon_end_download (int final, int bintotal);
139
140 static void pmon_download (char *buffer, int length);
141
142 static void pmon_load_fast (char *file);
143
144 static void mips_load (char *file, int from_tty);
145
146 static int mips_make_srec (char *buffer, int type, CORE_ADDR memaddr,
147 unsigned char *myaddr, int len);
148
149 static int set_breakpoint (CORE_ADDR addr, int len, enum break_type type);
150
151 static int clear_breakpoint (CORE_ADDR addr, int len, enum break_type type);
152
153 static int common_breakpoint (int set, CORE_ADDR addr, int len,
154 enum break_type type);
155
156 /* Forward declarations. */
157 extern struct target_ops mips_ops;
158 extern struct target_ops pmon_ops;
159 extern struct target_ops ddb_ops;
160 \f/* *INDENT-OFF* */
161 /* The MIPS remote debugging interface is built on top of a simple
162 packet protocol. Each packet is organized as follows:
163
164 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
165 may not appear anywhere else in the packet. Any time a SYN is
166 seen, a new packet should be assumed to have begun.
167
168 TYPE_LEN
169 This byte contains the upper five bits of the logical length
170 of the data section, plus a single bit indicating whether this
171 is a data packet or an acknowledgement. The documentation
172 indicates that this bit is 1 for a data packet, but the actual
173 board uses 1 for an acknowledgement. The value of the byte is
174 0x40 + (ack ? 0x20 : 0) + (len >> 6)
175 (we always have 0 <= len < 1024). Acknowledgement packets do
176 not carry data, and must have a data length of 0.
177
178 LEN1 This byte contains the lower six bits of the logical length of
179 the data section. The value is
180 0x40 + (len & 0x3f)
181
182 SEQ This byte contains the six bit sequence number of the packet.
183 The value is
184 0x40 + seq
185 An acknowlegment packet contains the sequence number of the
186 packet being acknowledged plus 1 modulo 64. Data packets are
187 transmitted in sequence. There may only be one outstanding
188 unacknowledged data packet at a time. The sequence numbers
189 are independent in each direction. If an acknowledgement for
190 the previous packet is received (i.e., an acknowledgement with
191 the sequence number of the packet just sent) the packet just
192 sent should be retransmitted. If no acknowledgement is
193 received within a timeout period, the packet should be
194 retransmitted. This has an unfortunate failure condition on a
195 high-latency line, as a delayed acknowledgement may lead to an
196 endless series of duplicate packets.
197
198 DATA The actual data bytes follow. The following characters are
199 escaped inline with DLE (ASCII 020, or ^P):
200 SYN (026) DLE S
201 DLE (020) DLE D
202 ^C (003) DLE C
203 ^S (023) DLE s
204 ^Q (021) DLE q
205 The additional DLE characters are not counted in the logical
206 length stored in the TYPE_LEN and LEN1 bytes.
207
208 CSUM1
209 CSUM2
210 CSUM3
211 These bytes contain an 18 bit checksum of the complete
212 contents of the packet excluding the SEQ byte and the
213 CSUM[123] bytes. The checksum is simply the twos complement
214 addition of all the bytes treated as unsigned characters. The
215 values of the checksum bytes are:
216 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
217 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
218 CSUM3: 0x40 + (cksum & 0x3f)
219
220 It happens that the MIPS remote debugging protocol always
221 communicates with ASCII strings. Because of this, this
222 implementation doesn't bother to handle the DLE quoting mechanism,
223 since it will never be required. */
224 /* *INDENT-ON* */
225
226
227 /* The SYN character which starts each packet. */
228 #define SYN '\026'
229
230 /* The 0x40 used to offset each packet (this value ensures that all of
231 the header and trailer bytes, other than SYN, are printable ASCII
232 characters). */
233 #define HDR_OFFSET 0x40
234
235 /* The indices of the bytes in the packet header. */
236 #define HDR_INDX_SYN 0
237 #define HDR_INDX_TYPE_LEN 1
238 #define HDR_INDX_LEN1 2
239 #define HDR_INDX_SEQ 3
240 #define HDR_LENGTH 4
241
242 /* The data/ack bit in the TYPE_LEN header byte. */
243 #define TYPE_LEN_DA_BIT 0x20
244 #define TYPE_LEN_DATA 0
245 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
246
247 /* How to compute the header bytes. */
248 #define HDR_SET_SYN(data, len, seq) (SYN)
249 #define HDR_SET_TYPE_LEN(data, len, seq) \
250 (HDR_OFFSET \
251 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
252 + (((len) >> 6) & 0x1f))
253 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
254 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
255
256 /* Check that a header byte is reasonable. */
257 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
258
259 /* Get data from the header. These macros evaluate their argument
260 multiple times. */
261 #define HDR_IS_DATA(hdr) \
262 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
263 #define HDR_GET_LEN(hdr) \
264 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
265 #define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
266
267 /* The maximum data length. */
268 #define DATA_MAXLEN 1023
269
270 /* The trailer offset. */
271 #define TRLR_OFFSET HDR_OFFSET
272
273 /* The indices of the bytes in the packet trailer. */
274 #define TRLR_INDX_CSUM1 0
275 #define TRLR_INDX_CSUM2 1
276 #define TRLR_INDX_CSUM3 2
277 #define TRLR_LENGTH 3
278
279 /* How to compute the trailer bytes. */
280 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
281 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
282 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
283
284 /* Check that a trailer byte is reasonable. */
285 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
286
287 /* Get data from the trailer. This evaluates its argument multiple
288 times. */
289 #define TRLR_GET_CKSUM(trlr) \
290 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
291 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
292 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
293
294 /* The sequence number modulos. */
295 #define SEQ_MODULOS (64)
296
297 /* PMON commands to load from the serial port or UDP socket. */
298 #define LOAD_CMD "load -b -s tty0\r"
299 #define LOAD_CMD_UDP "load -b -s udp\r"
300
301 /* The target vectors for the four different remote MIPS targets.
302 These are initialized with code in _initialize_remote_mips instead
303 of static initializers, to make it easier to extend the target_ops
304 vector later. */
305 struct target_ops mips_ops, pmon_ops, ddb_ops, lsi_ops;
306
307 enum mips_monitor_type
308 {
309 /* IDT/SIM monitor being used: */
310 MON_IDT,
311 /* PMON monitor being used: */
312 MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */
313 MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */
314 MON_LSI, /* 4.3.12 [EB,FP], LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
315 /* Last and unused value, for sizing vectors, etc. */
316 MON_LAST
317 };
318 static enum mips_monitor_type mips_monitor = MON_LAST;
319
320 /* The monitor prompt text. If the user sets the PMON prompt
321 to some new value, the GDB `set monitor-prompt' command must also
322 be used to inform GDB about the expected prompt. Otherwise, GDB
323 will not be able to connect to PMON in mips_initialize().
324 If the `set monitor-prompt' command is not used, the expected
325 default prompt will be set according the target:
326 target prompt
327 ----- -----
328 pmon PMON>
329 ddb NEC010>
330 lsi PMON>
331 */
332 static char *mips_monitor_prompt;
333
334 /* Set to 1 if the target is open. */
335 static int mips_is_open;
336
337 /* Currently active target description (if mips_is_open == 1) */
338 static struct target_ops *current_ops;
339
340 /* Set to 1 while the connection is being initialized. */
341 static int mips_initializing;
342
343 /* Set to 1 while the connection is being brought down. */
344 static int mips_exiting;
345
346 /* The next sequence number to send. */
347 static unsigned int mips_send_seq;
348
349 /* The next sequence number we expect to receive. */
350 static unsigned int mips_receive_seq;
351
352 /* The time to wait before retransmitting a packet, in seconds. */
353 static int mips_retransmit_wait = 3;
354
355 /* The number of times to try retransmitting a packet before giving up. */
356 static int mips_send_retries = 10;
357
358 /* The number of garbage characters to accept when looking for an
359 SYN for the next packet. */
360 static int mips_syn_garbage = 10;
361
362 /* The time to wait for a packet, in seconds. */
363 static int mips_receive_wait = 5;
364
365 /* Set if we have sent a packet to the board but have not yet received
366 a reply. */
367 static int mips_need_reply = 0;
368
369 /* Handle used to access serial I/O stream. */
370 static serial_t mips_desc;
371
372 /* UDP handle used to download files to target. */
373 static serial_t udp_desc;
374 static int udp_in_use;
375
376 /* TFTP filename used to download files to DDB board, in the form
377 host:filename. */
378 static char *tftp_name; /* host:filename */
379 static char *tftp_localname; /* filename portion of above */
380 static int tftp_in_use;
381 static FILE *tftp_file;
382
383 /* Counts the number of times the user tried to interrupt the target (usually
384 via ^C. */
385 static int interrupt_count;
386
387 /* If non-zero, means that the target is running. */
388 static int mips_wait_flag = 0;
389
390 /* If non-zero, monitor supports breakpoint commands. */
391 static int monitor_supports_breakpoints = 0;
392
393 /* Data cache header. */
394
395 #if 0 /* not used (yet?) */
396 static DCACHE *mips_dcache;
397 #endif
398
399 /* Non-zero means that we've just hit a read or write watchpoint */
400 static int hit_watchpoint;
401
402 /* Table of breakpoints/watchpoints (used only on LSI PMON target).
403 The table is indexed by a breakpoint number, which is an integer
404 from 0 to 255 returned by the LSI PMON when a breakpoint is set.
405 */
406 #define MAX_LSI_BREAKPOINTS 256
407 struct lsi_breakpoint_info
408 {
409 enum break_type type; /* type of breakpoint */
410 CORE_ADDR addr; /* address of breakpoint */
411 int len; /* length of region being watched */
412 unsigned long value; /* value to watch */
413 }
414 lsi_breakpoints[MAX_LSI_BREAKPOINTS];
415
416 /* Error/warning codes returned by LSI PMON for breakpoint commands.
417 Warning values may be ORed together; error values may not. */
418 #define W_WARN 0x100 /* This bit is set if the error code is a warning */
419 #define W_MSK 0x101 /* warning: Range feature is supported via mask */
420 #define W_VAL 0x102 /* warning: Value check is not supported in hardware */
421 #define W_QAL 0x104 /* warning: Requested qualifiers are not supported in hardware */
422
423 #define E_ERR 0x200 /* This bit is set if the error code is an error */
424 #define E_BPT 0x200 /* error: No such breakpoint number */
425 #define E_RGE 0x201 /* error: Range is not supported */
426 #define E_QAL 0x202 /* error: The requested qualifiers can not be used */
427 #define E_OUT 0x203 /* error: Out of hardware resources */
428 #define E_NON 0x204 /* error: Hardware breakpoint not supported */
429
430 struct lsi_error
431 {
432 int code; /* error code */
433 char *string; /* string associated with this code */
434 };
435
436 struct lsi_error lsi_warning_table[] =
437 {
438 {W_MSK, "Range feature is supported via mask"},
439 {W_VAL, "Value check is not supported in hardware"},
440 {W_QAL, "Requested qualifiers are not supported in hardware"},
441 {0, NULL}
442 };
443
444 struct lsi_error lsi_error_table[] =
445 {
446 {E_BPT, "No such breakpoint number"},
447 {E_RGE, "Range is not supported"},
448 {E_QAL, "The requested qualifiers can not be used"},
449 {E_OUT, "Out of hardware resources"},
450 {E_NON, "Hardware breakpoint not supported"},
451 {0, NULL}
452 };
453
454 /* Set to 1 with the 'set monitor-warnings' command to enable printing
455 of warnings returned by PMON when hardware breakpoints are used. */
456 static int monitor_warnings;
457
458
459 static void
460 close_ports ()
461 {
462 mips_is_open = 0;
463 SERIAL_CLOSE (mips_desc);
464
465 if (udp_in_use)
466 {
467 SERIAL_CLOSE (udp_desc);
468 udp_in_use = 0;
469 }
470 tftp_in_use = 0;
471 }
472
473 /* Handle low-level error that we can't recover from. Note that just
474 error()ing out from target_wait or some such low-level place will cause
475 all hell to break loose--the rest of GDB will tend to get left in an
476 inconsistent state. */
477
478 static NORETURN void
479 mips_error (char *string,...)
480 {
481 va_list args;
482
483 va_start (args, string);
484
485 target_terminal_ours ();
486 wrap_here (""); /* Force out any buffered output */
487 gdb_flush (gdb_stdout);
488 if (error_pre_print)
489 fprintf_filtered (gdb_stderr, error_pre_print);
490 vfprintf_filtered (gdb_stderr, string, args);
491 fprintf_filtered (gdb_stderr, "\n");
492 va_end (args);
493 gdb_flush (gdb_stderr);
494
495 /* Clean up in such a way that mips_close won't try to talk to the
496 board (it almost surely won't work since we weren't able to talk to
497 it). */
498 close_ports ();
499
500 printf_unfiltered ("Ending remote MIPS debugging.\n");
501 target_mourn_inferior ();
502
503 return_to_top_level (RETURN_ERROR);
504 }
505
506 /* putc_readable - print a character, displaying non-printable chars in
507 ^x notation or in hex. */
508
509 static void
510 fputc_readable (ch, file)
511 int ch;
512 struct ui_file *file;
513 {
514 if (ch == '\n')
515 fputc_unfiltered ('\n', file);
516 else if (ch == '\r')
517 fprintf_unfiltered (file, "\\r");
518 else if (ch < 0x20) /* ASCII control character */
519 fprintf_unfiltered (file, "^%c", ch + '@');
520 else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */
521 fprintf_unfiltered (file, "[%02x]", ch & 0xff);
522 else
523 fputc_unfiltered (ch, file);
524 }
525
526
527 /* puts_readable - print a string, displaying non-printable chars in
528 ^x notation or in hex. */
529
530 static void
531 fputs_readable (string, file)
532 char *string;
533 struct ui_file *file;
534 {
535 int c;
536
537 while ((c = *string++) != '\0')
538 fputc_readable (c, file);
539 }
540
541
542 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
543 timed out. TIMEOUT specifies timeout value in seconds.
544 */
545
546 int
547 mips_expect_timeout (string, timeout)
548 char *string;
549 int timeout;
550 {
551 char *p = string;
552
553 if (remote_debug)
554 {
555 fprintf_unfiltered (gdb_stdlog, "Expected \"");
556 fputs_readable (string, gdb_stdlog);
557 fprintf_unfiltered (gdb_stdlog, "\", got \"");
558 }
559
560 immediate_quit = 1;
561 while (1)
562 {
563 int c;
564
565 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
566 were waiting for the mips_monitor_prompt... */
567
568 c = SERIAL_READCHAR (mips_desc, timeout);
569
570 if (c == SERIAL_TIMEOUT)
571 {
572 if (remote_debug)
573 fprintf_unfiltered (gdb_stdlog, "\": FAIL\n");
574 return 0;
575 }
576
577 if (remote_debug)
578 fputc_readable (c, gdb_stdlog);
579
580 if (c == *p++)
581 {
582 if (*p == '\0')
583 {
584 immediate_quit = 0;
585 if (remote_debug)
586 fprintf_unfiltered (gdb_stdlog, "\": OK\n");
587 return 1;
588 }
589 }
590 else
591 {
592 p = string;
593 if (c == *p)
594 p++;
595 }
596 }
597 }
598
599 /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
600 timed out. The timeout value is hard-coded to 2 seconds. Use
601 mips_expect_timeout if a different timeout value is needed.
602 */
603
604 int
605 mips_expect (string)
606 char *string;
607 {
608 return mips_expect_timeout (string, 2);
609 }
610
611 /* Read the required number of characters into the given buffer (which
612 is assumed to be large enough). The only failure is a timeout. */
613 int
614 mips_getstring (string, n)
615 char *string;
616 int n;
617 {
618 char *p = string;
619 int c;
620
621 immediate_quit = 1;
622 while (n > 0)
623 {
624 c = SERIAL_READCHAR (mips_desc, 2);
625
626 if (c == SERIAL_TIMEOUT)
627 {
628 fprintf_unfiltered (gdb_stderr,
629 "Failed to read %d characters from target (TIMEOUT)\n", n);
630 return 0;
631 }
632
633 *p++ = c;
634 n--;
635 }
636
637 return 1;
638 }
639
640 /* Read a character from the remote, aborting on error. Returns
641 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
642 returns). FIXME: If we see the string mips_monitor_prompt from
643 the board, then we are debugging on the main console port, and we
644 have somehow dropped out of remote debugging mode. In this case,
645 we automatically go back in to remote debugging mode. This is a
646 hack, put in because I can't find any way for a program running on
647 the remote board to terminate without also ending remote debugging
648 mode. I assume users won't have any trouble with this; for one
649 thing, the IDT documentation generally assumes that the remote
650 debugging port is not the console port. This is, however, very
651 convenient for DejaGnu when you only have one connected serial
652 port. */
653
654 static int
655 mips_readchar (timeout)
656 int timeout;
657 {
658 int ch;
659 static int state = 0;
660 int mips_monitor_prompt_len = strlen (mips_monitor_prompt);
661
662 {
663 int i;
664
665 i = timeout;
666 if (i == -1 && watchdog > 0)
667 i = watchdog;
668 }
669
670 if (state == mips_monitor_prompt_len)
671 timeout = 1;
672 ch = SERIAL_READCHAR (mips_desc, timeout);
673
674 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
675 {
676 target_mourn_inferior ();
677 error ("Watchdog has expired. Target detached.\n");
678 }
679
680 if (ch == SERIAL_EOF)
681 mips_error ("End of file from remote");
682 if (ch == SERIAL_ERROR)
683 mips_error ("Error reading from remote: %s", safe_strerror (errno));
684 if (remote_debug > 1)
685 {
686 /* Don't use _filtered; we can't deal with a QUIT out of
687 target_wait, and I think this might be called from there. */
688 if (ch != SERIAL_TIMEOUT)
689 fprintf_unfiltered (gdb_stdlog, "Read '%c' %d 0x%x\n", ch, ch, ch);
690 else
691 fprintf_unfiltered (gdb_stdlog, "Timed out in read\n");
692 }
693
694 /* If we have seen mips_monitor_prompt and we either time out, or
695 we see a @ (which was echoed from a packet we sent), reset the
696 board as described above. The first character in a packet after
697 the SYN (which is not echoed) is always an @ unless the packet is
698 more than 64 characters long, which ours never are. */
699 if ((ch == SERIAL_TIMEOUT || ch == '@')
700 && state == mips_monitor_prompt_len
701 && !mips_initializing
702 && !mips_exiting)
703 {
704 if (remote_debug > 0)
705 /* Don't use _filtered; we can't deal with a QUIT out of
706 target_wait, and I think this might be called from there. */
707 fprintf_unfiltered (gdb_stdlog, "Reinitializing MIPS debugging mode\n");
708
709 mips_need_reply = 0;
710 mips_initialize ();
711
712 state = 0;
713
714 /* At this point, about the only thing we can do is abort the command
715 in progress and get back to command level as quickly as possible. */
716
717 error ("Remote board reset, debug protocol re-initialized.");
718 }
719
720 if (ch == mips_monitor_prompt[state])
721 ++state;
722 else
723 state = 0;
724
725 return ch;
726 }
727
728 /* Get a packet header, putting the data in the supplied buffer.
729 PGARBAGE is a pointer to the number of garbage characters received
730 so far. CH is the last character received. Returns 0 for success,
731 or -1 for timeout. */
732
733 static int
734 mips_receive_header (hdr, pgarbage, ch, timeout)
735 unsigned char *hdr;
736 int *pgarbage;
737 int ch;
738 int timeout;
739 {
740 int i;
741
742 while (1)
743 {
744 /* Wait for a SYN. mips_syn_garbage is intended to prevent
745 sitting here indefinitely if the board sends us one garbage
746 character per second. ch may already have a value from the
747 last time through the loop. */
748 while (ch != SYN)
749 {
750 ch = mips_readchar (timeout);
751 if (ch == SERIAL_TIMEOUT)
752 return -1;
753 if (ch != SYN)
754 {
755 /* Printing the character here lets the user of gdb see
756 what the program is outputting, if the debugging is
757 being done on the console port. Don't use _filtered:
758 we can't deal with a QUIT out of target_wait and
759 buffered target output confuses the user. */
760 if (!mips_initializing || remote_debug > 0)
761 {
762 if (isprint (ch) || isspace (ch))
763 {
764 fputc_unfiltered (ch, gdb_stdtarg);
765 }
766 else
767 {
768 fputc_readable (ch, gdb_stdtarg);
769 }
770 gdb_flush (gdb_stdtarg);
771 }
772
773 /* Only count unprintable characters. */
774 if (! (isprint (ch) || isspace (ch)))
775 (*pgarbage) += 1;
776
777 if (mips_syn_garbage > 0
778 && *pgarbage > mips_syn_garbage)
779 mips_error ("Debug protocol failure: more than %d characters before a sync.",
780 mips_syn_garbage);
781 }
782 }
783
784 /* Get the packet header following the SYN. */
785 for (i = 1; i < HDR_LENGTH; i++)
786 {
787 ch = mips_readchar (timeout);
788 if (ch == SERIAL_TIMEOUT)
789 return -1;
790 /* Make sure this is a header byte. */
791 if (ch == SYN || !HDR_CHECK (ch))
792 break;
793
794 hdr[i] = ch;
795 }
796
797 /* If we got the complete header, we can return. Otherwise we
798 loop around and keep looking for SYN. */
799 if (i >= HDR_LENGTH)
800 return 0;
801 }
802 }
803
804 /* Get a packet header, putting the data in the supplied buffer.
805 PGARBAGE is a pointer to the number of garbage characters received
806 so far. The last character read is returned in *PCH. Returns 0
807 for success, -1 for timeout, -2 for error. */
808
809 static int
810 mips_receive_trailer (trlr, pgarbage, pch, timeout)
811 unsigned char *trlr;
812 int *pgarbage;
813 int *pch;
814 int timeout;
815 {
816 int i;
817 int ch;
818
819 for (i = 0; i < TRLR_LENGTH; i++)
820 {
821 ch = mips_readchar (timeout);
822 *pch = ch;
823 if (ch == SERIAL_TIMEOUT)
824 return -1;
825 if (!TRLR_CHECK (ch))
826 return -2;
827 trlr[i] = ch;
828 }
829 return 0;
830 }
831
832 /* Get the checksum of a packet. HDR points to the packet header.
833 DATA points to the packet data. LEN is the length of DATA. */
834
835 static int
836 mips_cksum (hdr, data, len)
837 const unsigned char *hdr;
838 const unsigned char *data;
839 int len;
840 {
841 register const unsigned char *p;
842 register int c;
843 register int cksum;
844
845 cksum = 0;
846
847 /* The initial SYN is not included in the checksum. */
848 c = HDR_LENGTH - 1;
849 p = hdr + 1;
850 while (c-- != 0)
851 cksum += *p++;
852
853 c = len;
854 p = data;
855 while (c-- != 0)
856 cksum += *p++;
857
858 return cksum;
859 }
860
861 /* Send a packet containing the given ASCII string. */
862
863 static void
864 mips_send_packet (s, get_ack)
865 const char *s;
866 int get_ack;
867 {
868 /* unsigned */ int len;
869 unsigned char *packet;
870 register int cksum;
871 int try;
872
873 len = strlen (s);
874 if (len > DATA_MAXLEN)
875 mips_error ("MIPS protocol data packet too long: %s", s);
876
877 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
878
879 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
880 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
881 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
882 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
883
884 memcpy (packet + HDR_LENGTH, s, len);
885
886 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
887 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
888 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
889 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
890
891 /* Increment the sequence number. This will set mips_send_seq to
892 the sequence number we expect in the acknowledgement. */
893 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
894
895 /* We can only have one outstanding data packet, so we just wait for
896 the acknowledgement here. Keep retransmitting the packet until
897 we get one, or until we've tried too many times. */
898 for (try = 0; try < mips_send_retries; try++)
899 {
900 int garbage;
901 int ch;
902
903 if (remote_debug > 0)
904 {
905 /* Don't use _filtered; we can't deal with a QUIT out of
906 target_wait, and I think this might be called from there. */
907 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
908 fprintf_unfiltered (gdb_stdlog, "Writing \"%s\"\n", packet + 1);
909 }
910
911 if (SERIAL_WRITE (mips_desc, packet,
912 HDR_LENGTH + len + TRLR_LENGTH) != 0)
913 mips_error ("write to target failed: %s", safe_strerror (errno));
914
915 if (!get_ack)
916 return;
917
918 garbage = 0;
919 ch = 0;
920 while (1)
921 {
922 unsigned char hdr[HDR_LENGTH + 1];
923 unsigned char trlr[TRLR_LENGTH + 1];
924 int err;
925 unsigned int seq;
926
927 /* Get the packet header. If we time out, resend the data
928 packet. */
929 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
930 if (err != 0)
931 break;
932
933 ch = 0;
934
935 /* If we get a data packet, assume it is a duplicate and
936 ignore it. FIXME: If the acknowledgement is lost, this
937 data packet may be the packet the remote sends after the
938 acknowledgement. */
939 if (HDR_IS_DATA (hdr))
940 {
941 int i;
942
943 /* Ignore any errors raised whilst attempting to ignore
944 packet. */
945
946 len = HDR_GET_LEN (hdr);
947
948 for (i = 0; i < len; i++)
949 {
950 int rch;
951
952 rch = mips_readchar (2);
953 if (rch == SYN)
954 {
955 ch = SYN;
956 break;
957 }
958 if (rch == SERIAL_TIMEOUT)
959 break;
960 /* ignore the character */
961 }
962
963 if (i == len)
964 (void) mips_receive_trailer (trlr, &garbage, &ch, 2);
965
966 /* We don't bother checking the checksum, or providing an
967 ACK to the packet. */
968 continue;
969 }
970
971 /* If the length is not 0, this is a garbled packet. */
972 if (HDR_GET_LEN (hdr) != 0)
973 continue;
974
975 /* Get the packet trailer. */
976 err = mips_receive_trailer (trlr, &garbage, &ch,
977 mips_retransmit_wait);
978
979 /* If we timed out, resend the data packet. */
980 if (err == -1)
981 break;
982
983 /* If we got a bad character, reread the header. */
984 if (err != 0)
985 continue;
986
987 /* If the checksum does not match the trailer checksum, this
988 is a bad packet; ignore it. */
989 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
990 != TRLR_GET_CKSUM (trlr))
991 continue;
992
993 if (remote_debug > 0)
994 {
995 hdr[HDR_LENGTH] = '\0';
996 trlr[TRLR_LENGTH] = '\0';
997 /* Don't use _filtered; we can't deal with a QUIT out of
998 target_wait, and I think this might be called from there. */
999 fprintf_unfiltered (gdb_stdlog, "Got ack %d \"%s%s\"\n",
1000 HDR_GET_SEQ (hdr), hdr + 1, trlr);
1001 }
1002
1003 /* If this ack is for the current packet, we're done. */
1004 seq = HDR_GET_SEQ (hdr);
1005 if (seq == mips_send_seq)
1006 return;
1007
1008 /* If this ack is for the last packet, resend the current
1009 packet. */
1010 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
1011 break;
1012
1013 /* Otherwise this is a bad ack; ignore it. Increment the
1014 garbage count to ensure that we do not stay in this loop
1015 forever. */
1016 ++garbage;
1017 }
1018 }
1019
1020 mips_error ("Remote did not acknowledge packet");
1021 }
1022
1023 /* Receive and acknowledge a packet, returning the data in BUFF (which
1024 should be DATA_MAXLEN + 1 bytes). The protocol documentation
1025 implies that only the sender retransmits packets, so this code just
1026 waits silently for a packet. It returns the length of the received
1027 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
1028 don't print an error message and return -1. */
1029
1030 static int
1031 mips_receive_packet (buff, throw_error, timeout)
1032 char *buff;
1033 int throw_error;
1034 int timeout;
1035 {
1036 int ch;
1037 int garbage;
1038 int len;
1039 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
1040 int cksum;
1041
1042 ch = 0;
1043 garbage = 0;
1044 while (1)
1045 {
1046 unsigned char hdr[HDR_LENGTH];
1047 unsigned char trlr[TRLR_LENGTH];
1048 int i;
1049 int err;
1050
1051 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
1052 {
1053 if (throw_error)
1054 mips_error ("Timed out waiting for remote packet");
1055 else
1056 return -1;
1057 }
1058
1059 ch = 0;
1060
1061 /* An acknowledgement is probably a duplicate; ignore it. */
1062 if (!HDR_IS_DATA (hdr))
1063 {
1064 len = HDR_GET_LEN (hdr);
1065 /* Check if the length is valid for an ACK, we may aswell
1066 try and read the remainder of the packet: */
1067 if (len == 0)
1068 {
1069 /* Ignore the error condition, since we are going to
1070 ignore the packet anyway. */
1071 (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
1072 }
1073 /* Don't use _filtered; we can't deal with a QUIT out of
1074 target_wait, and I think this might be called from there. */
1075 if (remote_debug > 0)
1076 fprintf_unfiltered (gdb_stdlog, "Ignoring unexpected ACK\n");
1077 continue;
1078 }
1079
1080 len = HDR_GET_LEN (hdr);
1081 for (i = 0; i < len; i++)
1082 {
1083 int rch;
1084
1085 rch = mips_readchar (timeout);
1086 if (rch == SYN)
1087 {
1088 ch = SYN;
1089 break;
1090 }
1091 if (rch == SERIAL_TIMEOUT)
1092 {
1093 if (throw_error)
1094 mips_error ("Timed out waiting for remote packet");
1095 else
1096 return -1;
1097 }
1098 buff[i] = rch;
1099 }
1100
1101 if (i < len)
1102 {
1103 /* Don't use _filtered; we can't deal with a QUIT out of
1104 target_wait, and I think this might be called from there. */
1105 if (remote_debug > 0)
1106 fprintf_unfiltered (gdb_stdlog,
1107 "Got new SYN after %d chars (wanted %d)\n",
1108 i, len);
1109 continue;
1110 }
1111
1112 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
1113 if (err == -1)
1114 {
1115 if (throw_error)
1116 mips_error ("Timed out waiting for packet");
1117 else
1118 return -1;
1119 }
1120 if (err == -2)
1121 {
1122 /* Don't use _filtered; we can't deal with a QUIT out of
1123 target_wait, and I think this might be called from there. */
1124 if (remote_debug > 0)
1125 fprintf_unfiltered (gdb_stdlog, "Got SYN when wanted trailer\n");
1126 continue;
1127 }
1128
1129 /* If this is the wrong sequence number, ignore it. */
1130 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
1131 {
1132 /* Don't use _filtered; we can't deal with a QUIT out of
1133 target_wait, and I think this might be called from there. */
1134 if (remote_debug > 0)
1135 fprintf_unfiltered (gdb_stdlog,
1136 "Ignoring sequence number %d (want %d)\n",
1137 HDR_GET_SEQ (hdr), mips_receive_seq);
1138 continue;
1139 }
1140
1141 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
1142 break;
1143
1144 if (remote_debug > 0)
1145 /* Don't use _filtered; we can't deal with a QUIT out of
1146 target_wait, and I think this might be called from there. */
1147 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
1148 mips_cksum (hdr, buff, len),
1149 TRLR_GET_CKSUM (trlr));
1150
1151 /* The checksum failed. Send an acknowledgement for the
1152 previous packet to tell the remote to resend the packet. */
1153 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1154 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1155 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1156 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1157
1158 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1159
1160 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1161 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1162 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1163
1164 if (remote_debug > 0)
1165 {
1166 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1167 /* Don't use _filtered; we can't deal with a QUIT out of
1168 target_wait, and I think this might be called from there. */
1169 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1170 ack + 1);
1171 }
1172
1173 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1174 {
1175 if (throw_error)
1176 mips_error ("write to target failed: %s", safe_strerror (errno));
1177 else
1178 return -1;
1179 }
1180 }
1181
1182 if (remote_debug > 0)
1183 {
1184 buff[len] = '\0';
1185 /* Don't use _filtered; we can't deal with a QUIT out of
1186 target_wait, and I think this might be called from there. */
1187 printf_unfiltered ("Got packet \"%s\"\n", buff);
1188 }
1189
1190 /* We got the packet. Send an acknowledgement. */
1191 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1192
1193 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1194 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1195 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1196 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1197
1198 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1199
1200 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1201 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1202 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1203
1204 if (remote_debug > 0)
1205 {
1206 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
1207 /* Don't use _filtered; we can't deal with a QUIT out of
1208 target_wait, and I think this might be called from there. */
1209 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
1210 ack + 1);
1211 }
1212
1213 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
1214 {
1215 if (throw_error)
1216 mips_error ("write to target failed: %s", safe_strerror (errno));
1217 else
1218 return -1;
1219 }
1220
1221 return len;
1222 }
1223 \f
1224 /* Optionally send a request to the remote system and optionally wait
1225 for the reply. This implements the remote debugging protocol,
1226 which is built on top of the packet protocol defined above. Each
1227 request has an ADDR argument and a DATA argument. The following
1228 requests are defined:
1229
1230 \0 don't send a request; just wait for a reply
1231 i read word from instruction space at ADDR
1232 d read word from data space at ADDR
1233 I write DATA to instruction space at ADDR
1234 D write DATA to data space at ADDR
1235 r read register number ADDR
1236 R set register number ADDR to value DATA
1237 c continue execution (if ADDR != 1, set pc to ADDR)
1238 s single step (if ADDR != 1, set pc to ADDR)
1239
1240 The read requests return the value requested. The write requests
1241 return the previous value in the changed location. The execution
1242 requests return a UNIX wait value (the approximate signal which
1243 caused execution to stop is in the upper eight bits).
1244
1245 If PERR is not NULL, this function waits for a reply. If an error
1246 occurs, it sets *PERR to 1 and sets errno according to what the
1247 target board reports. */
1248
1249 static ULONGEST
1250 mips_request (int cmd,
1251 ULONGEST addr,
1252 ULONGEST data,
1253 int *perr,
1254 int timeout,
1255 char *buff)
1256 {
1257 char myBuff[DATA_MAXLEN + 1];
1258 int len;
1259 int rpid;
1260 char rcmd;
1261 int rerrflg;
1262 unsigned long rresponse;
1263
1264 if (buff == (char *) NULL)
1265 buff = myBuff;
1266
1267 if (cmd != '\0')
1268 {
1269 if (mips_need_reply)
1270 internal_error ("mips_request: Trying to send command before reply");
1271 sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
1272 mips_send_packet (buff, 1);
1273 mips_need_reply = 1;
1274 }
1275
1276 if (perr == (int *) NULL)
1277 return 0;
1278
1279 if (!mips_need_reply)
1280 internal_error ("mips_request: Trying to get reply before command");
1281
1282 mips_need_reply = 0;
1283
1284 len = mips_receive_packet (buff, 1, timeout);
1285 buff[len] = '\0';
1286
1287 if (sscanf (buff, "0x%x %c 0x%x 0x%lx",
1288 &rpid, &rcmd, &rerrflg, &rresponse) != 4
1289 || (cmd != '\0' && rcmd != cmd))
1290 mips_error ("Bad response from remote board");
1291
1292 if (rerrflg != 0)
1293 {
1294 *perr = 1;
1295
1296 /* FIXME: This will returns MIPS errno numbers, which may or may
1297 not be the same as errno values used on other systems. If
1298 they stick to common errno values, they will be the same, but
1299 if they don't, they must be translated. */
1300 errno = rresponse;
1301
1302 return 0;
1303 }
1304
1305 *perr = 0;
1306 return rresponse;
1307 }
1308
1309 static void
1310 mips_initialize_cleanups (arg)
1311 PTR arg;
1312 {
1313 mips_initializing = 0;
1314 }
1315
1316 static void
1317 mips_exit_cleanups (arg)
1318 PTR arg;
1319 {
1320 mips_exiting = 0;
1321 }
1322
1323 static void
1324 mips_send_command (cmd, prompt)
1325 const char *cmd;
1326 int prompt;
1327 {
1328 SERIAL_WRITE (mips_desc, cmd, strlen (cmd));
1329 mips_expect (cmd);
1330 mips_expect ("\n");
1331 if (prompt)
1332 mips_expect (mips_monitor_prompt);
1333 }
1334
1335 /* Enter remote (dbx) debug mode: */
1336 static void
1337 mips_enter_debug ()
1338 {
1339 /* Reset the sequence numbers, ready for the new debug sequence: */
1340 mips_send_seq = 0;
1341 mips_receive_seq = 0;
1342
1343 if (mips_monitor != MON_IDT)
1344 mips_send_command ("debug\r", 0);
1345 else /* assume IDT monitor by default */
1346 mips_send_command ("db tty0\r", 0);
1347
1348 sleep (1);
1349 SERIAL_WRITE (mips_desc, "\r", sizeof "\r" - 1);
1350
1351 /* We don't need to absorb any spurious characters here, since the
1352 mips_receive_header will eat up a reasonable number of characters
1353 whilst looking for the SYN, however this avoids the "garbage"
1354 being displayed to the user. */
1355 if (mips_monitor != MON_IDT)
1356 mips_expect ("\r");
1357
1358 {
1359 char buff[DATA_MAXLEN + 1];
1360 if (mips_receive_packet (buff, 1, 3) < 0)
1361 mips_error ("Failed to initialize (didn't receive packet).");
1362 }
1363 }
1364
1365 /* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1366 static int
1367 mips_exit_debug ()
1368 {
1369 int err;
1370 struct cleanup *old_cleanups = make_cleanup (mips_exit_cleanups, NULL);
1371
1372 mips_exiting = 1;
1373
1374 if (mips_monitor != MON_IDT)
1375 {
1376 /* The DDB (NEC) and MiniRISC (LSI) versions of PMON exit immediately,
1377 so we do not get a reply to this command: */
1378 mips_request ('x', 0, 0, NULL, mips_receive_wait, NULL);
1379 mips_need_reply = 0;
1380 if (!mips_expect (" break!"))
1381 return -1;
1382 }
1383 else
1384 mips_request ('x', 0, 0, &err, mips_receive_wait, NULL);
1385
1386 if (!mips_expect (mips_monitor_prompt))
1387 return -1;
1388
1389 do_cleanups (old_cleanups);
1390
1391 return 0;
1392 }
1393
1394 /* Initialize a new connection to the MIPS board, and make sure we are
1395 really connected. */
1396
1397 static void
1398 mips_initialize ()
1399 {
1400 int err;
1401 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1402 int j;
1403
1404 /* What is this code doing here? I don't see any way it can happen, and
1405 it might mean mips_initializing didn't get cleared properly.
1406 So I'll make it a warning. */
1407
1408 if (mips_initializing)
1409 {
1410 warning ("internal error: mips_initialize called twice");
1411 return;
1412 }
1413
1414 mips_wait_flag = 0;
1415 mips_initializing = 1;
1416
1417 /* At this point, the packit protocol isn't responding. We'll try getting
1418 into the monitor, and restarting the protocol. */
1419
1420 /* Force the system into the monitor. After this we *should* be at
1421 the mips_monitor_prompt. */
1422 if (mips_monitor != MON_IDT)
1423 j = 0; /* start by checking if we are already at the prompt */
1424 else
1425 j = 1; /* start by sending a break */
1426 for (; j <= 4; j++)
1427 {
1428 switch (j)
1429 {
1430 case 0: /* First, try sending a CR */
1431 SERIAL_FLUSH_INPUT (mips_desc);
1432 SERIAL_WRITE (mips_desc, "\r", 1);
1433 break;
1434 case 1: /* First, try sending a break */
1435 SERIAL_SEND_BREAK (mips_desc);
1436 break;
1437 case 2: /* Then, try a ^C */
1438 SERIAL_WRITE (mips_desc, "\003", 1);
1439 break;
1440 case 3: /* Then, try escaping from download */
1441 {
1442 if (mips_monitor != MON_IDT)
1443 {
1444 char tbuff[7];
1445
1446 /* We shouldn't need to send multiple termination
1447 sequences, since the target performs line (or
1448 block) reads, and then processes those
1449 packets. In-case we were downloading a large packet
1450 we flush the output buffer before inserting a
1451 termination sequence. */
1452 SERIAL_FLUSH_OUTPUT (mips_desc);
1453 sprintf (tbuff, "\r/E/E\r");
1454 SERIAL_WRITE (mips_desc, tbuff, 6);
1455 }
1456 else
1457 {
1458 char srec[10];
1459 int i;
1460
1461 /* We are possibly in binary download mode, having
1462 aborted in the middle of an S-record. ^C won't
1463 work because of binary mode. The only reliable way
1464 out is to send enough termination packets (8 bytes)
1465 to fill up and then overflow the largest size
1466 S-record (255 bytes in this case). This amounts to
1467 256/8 + 1 packets.
1468 */
1469
1470 mips_make_srec (srec, '7', 0, NULL, 0);
1471
1472 for (i = 1; i <= 33; i++)
1473 {
1474 SERIAL_WRITE (mips_desc, srec, 8);
1475
1476 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1477 break; /* Break immediatly if we get something from
1478 the board. */
1479 }
1480 }
1481 }
1482 break;
1483 case 4:
1484 mips_error ("Failed to initialize.");
1485 }
1486
1487 if (mips_expect (mips_monitor_prompt))
1488 break;
1489 }
1490
1491 if (mips_monitor != MON_IDT)
1492 {
1493 /* Sometimes PMON ignores the first few characters in the first
1494 command sent after a load. Sending a blank command gets
1495 around that. */
1496 mips_send_command ("\r", -1);
1497
1498 /* Ensure the correct target state: */
1499 if (mips_monitor != MON_LSI)
1500 mips_send_command ("set regsize 64\r", -1);
1501 mips_send_command ("set hostport tty0\r", -1);
1502 mips_send_command ("set brkcmd \"\"\r", -1);
1503 /* Delete all the current breakpoints: */
1504 mips_send_command ("db *\r", -1);
1505 /* NOTE: PMON does not have breakpoint support through the
1506 "debug" mode, only at the monitor command-line. */
1507 }
1508
1509 mips_enter_debug ();
1510
1511 /* Clear all breakpoints: */
1512 if ((mips_monitor == MON_IDT
1513 && clear_breakpoint (-1, 0, BREAK_UNUSED) == 0)
1514 || mips_monitor == MON_LSI)
1515 monitor_supports_breakpoints = 1;
1516 else
1517 monitor_supports_breakpoints = 0;
1518
1519 do_cleanups (old_cleanups);
1520
1521 /* If this doesn't call error, we have connected; we don't care if
1522 the request itself succeeds or fails. */
1523
1524 mips_request ('r', 0, 0, &err, mips_receive_wait, NULL);
1525 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1526 select_frame (get_current_frame (), 0);
1527 }
1528
1529 /* Open a connection to the remote board. */
1530 static void
1531 common_open (ops, name, from_tty, new_monitor, new_monitor_prompt)
1532 struct target_ops *ops;
1533 char *name;
1534 int from_tty;
1535 enum mips_monitor_type new_monitor;
1536 char *new_monitor_prompt;
1537 {
1538 char *ptype;
1539 char *serial_port_name;
1540 char *remote_name = 0;
1541 char *local_name = 0;
1542 char **argv;
1543
1544 if (name == 0)
1545 error (
1546 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1547 device is attached to the target board (e.g., /dev/ttya).\n"
1548 "If you want to use TFTP to download to the board, specify the name of a\n"
1549 "temporary file to be used by GDB for downloads as the second argument.\n"
1550 "This filename must be in the form host:filename, where host is the name\n"
1551 "of the host running the TFTP server, and the file must be readable by the\n"
1552 "world. If the local name of the temporary file differs from the name as\n"
1553 "seen from the board via TFTP, specify that name as the third parameter.\n");
1554
1555 /* Parse the serial port name, the optional TFTP name, and the
1556 optional local TFTP name. */
1557 if ((argv = buildargv (name)) == NULL)
1558 nomem (0);
1559 make_cleanup_freeargv (argv);
1560
1561 serial_port_name = strsave (argv[0]);
1562 if (argv[1]) /* remote TFTP name specified? */
1563 {
1564 remote_name = argv[1];
1565 if (argv[2]) /* local TFTP filename specified? */
1566 local_name = argv[2];
1567 }
1568
1569 target_preopen (from_tty);
1570
1571 if (mips_is_open)
1572 unpush_target (current_ops);
1573
1574 /* Open and initialize the serial port. */
1575 mips_desc = SERIAL_OPEN (serial_port_name);
1576 if (mips_desc == (serial_t) NULL)
1577 perror_with_name (serial_port_name);
1578
1579 if (baud_rate != -1)
1580 {
1581 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1582 {
1583 SERIAL_CLOSE (mips_desc);
1584 perror_with_name (serial_port_name);
1585 }
1586 }
1587
1588 SERIAL_RAW (mips_desc);
1589
1590 /* Open and initialize the optional download port. If it is in the form
1591 hostname#portnumber, it's a UDP socket. If it is in the form
1592 hostname:filename, assume it's the TFTP filename that must be
1593 passed to the DDB board to tell it where to get the load file. */
1594 if (remote_name)
1595 {
1596 if (strchr (remote_name, '#'))
1597 {
1598 udp_desc = SERIAL_OPEN (remote_name);
1599 if (!udp_desc)
1600 perror_with_name ("Unable to open UDP port");
1601 udp_in_use = 1;
1602 }
1603 else
1604 {
1605 /* Save the remote and local names of the TFTP temp file. If
1606 the user didn't specify a local name, assume it's the same
1607 as the part of the remote name after the "host:". */
1608 if (tftp_name)
1609 free (tftp_name);
1610 if (tftp_localname)
1611 free (tftp_localname);
1612 if (local_name == NULL)
1613 if ((local_name = strchr (remote_name, ':')) != NULL)
1614 local_name++; /* skip over the colon */
1615 if (local_name == NULL)
1616 local_name = remote_name; /* local name same as remote name */
1617 tftp_name = strsave (remote_name);
1618 tftp_localname = strsave (local_name);
1619 tftp_in_use = 1;
1620 }
1621 }
1622
1623 current_ops = ops;
1624 mips_is_open = 1;
1625
1626 /* Reset the expected monitor prompt if it's never been set before. */
1627 if (mips_monitor_prompt == NULL)
1628 mips_monitor_prompt = strsave (new_monitor_prompt);
1629 mips_monitor = new_monitor;
1630
1631 mips_initialize ();
1632
1633 if (from_tty)
1634 printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
1635
1636 /* Switch to using remote target now. */
1637 push_target (ops);
1638
1639 /* FIXME: Should we call start_remote here? */
1640
1641 /* Try to figure out the processor model if possible. */
1642 ptype = mips_read_processor_type ();
1643 if (ptype)
1644 mips_set_processor_type_command (strsave (ptype), 0);
1645
1646 /* This is really the job of start_remote however, that makes an assumption
1647 that the target is about to print out a status message of some sort. That
1648 doesn't happen here (in fact, it may not be possible to get the monitor to
1649 send the appropriate packet). */
1650
1651 flush_cached_frames ();
1652 registers_changed ();
1653 stop_pc = read_pc ();
1654 set_current_frame (create_new_frame (read_fp (), stop_pc));
1655 select_frame (get_current_frame (), 0);
1656 print_stack_frame (selected_frame, -1, 1);
1657 free (serial_port_name);
1658 }
1659
1660 static void
1661 mips_open (name, from_tty)
1662 char *name;
1663 int from_tty;
1664 {
1665 const char *monitor_prompt = NULL;
1666 if (TARGET_ARCHITECTURE != NULL
1667 && TARGET_ARCHITECTURE->arch == bfd_arch_mips)
1668 {
1669 switch (TARGET_ARCHITECTURE->mach)
1670 {
1671 case bfd_mach_mips4100:
1672 case bfd_mach_mips4300:
1673 case bfd_mach_mips4600:
1674 case bfd_mach_mips4650:
1675 case bfd_mach_mips5000:
1676 monitor_prompt = "<RISQ> ";
1677 break;
1678 }
1679 }
1680 if (monitor_prompt == NULL)
1681 monitor_prompt = "<IDT>";
1682 common_open (&mips_ops, name, from_tty, MON_IDT, monitor_prompt);
1683 }
1684
1685 static void
1686 pmon_open (name, from_tty)
1687 char *name;
1688 int from_tty;
1689 {
1690 common_open (&pmon_ops, name, from_tty, MON_PMON, "PMON> ");
1691 }
1692
1693 static void
1694 ddb_open (name, from_tty)
1695 char *name;
1696 int from_tty;
1697 {
1698 common_open (&ddb_ops, name, from_tty, MON_DDB, "NEC010>");
1699 }
1700
1701 static void
1702 lsi_open (name, from_tty)
1703 char *name;
1704 int from_tty;
1705 {
1706 int i;
1707
1708 /* Clear the LSI breakpoint table. */
1709 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1710 lsi_breakpoints[i].type = BREAK_UNUSED;
1711
1712 common_open (&lsi_ops, name, from_tty, MON_LSI, "PMON> ");
1713 }
1714
1715 /* Close a connection to the remote board. */
1716
1717 static void
1718 mips_close (quitting)
1719 int quitting;
1720 {
1721 if (mips_is_open)
1722 {
1723 /* Get the board out of remote debugging mode. */
1724 (void) mips_exit_debug ();
1725
1726 close_ports ();
1727 }
1728 }
1729
1730 /* Detach from the remote board. */
1731
1732 static void
1733 mips_detach (args, from_tty)
1734 char *args;
1735 int from_tty;
1736 {
1737 if (args)
1738 error ("Argument given to \"detach\" when remotely debugging.");
1739
1740 pop_target ();
1741
1742 mips_close (1);
1743
1744 if (from_tty)
1745 printf_unfiltered ("Ending remote MIPS debugging.\n");
1746 }
1747
1748 /* Tell the target board to resume. This does not wait for a reply
1749 from the board, except in the case of single-stepping on LSI boards,
1750 where PMON does return a reply. */
1751
1752 static void
1753 mips_resume (pid, step, siggnal)
1754 int pid, step;
1755 enum target_signal siggnal;
1756 {
1757 int err;
1758
1759 /* LSI PMON requires returns a reply packet "0x1 s 0x0 0x57f" after
1760 a single step, so we wait for that. */
1761 mips_request (step ? 's' : 'c', 1, siggnal,
1762 mips_monitor == MON_LSI && step ? &err : (int *) NULL,
1763 mips_receive_wait, NULL);
1764 }
1765
1766 /* Return the signal corresponding to SIG, where SIG is the number which
1767 the MIPS protocol uses for the signal. */
1768 enum target_signal
1769 mips_signal_from_protocol (sig)
1770 int sig;
1771 {
1772 /* We allow a few more signals than the IDT board actually returns, on
1773 the theory that there is at least *some* hope that perhaps the numbering
1774 for these signals is widely agreed upon. */
1775 if (sig <= 0
1776 || sig > 31)
1777 return TARGET_SIGNAL_UNKNOWN;
1778
1779 /* Don't want to use target_signal_from_host because we are converting
1780 from MIPS signal numbers, not host ones. Our internal numbers
1781 match the MIPS numbers for the signals the board can return, which
1782 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1783 return (enum target_signal) sig;
1784 }
1785
1786 /* Wait until the remote stops, and return a wait status. */
1787
1788 static int
1789 mips_wait (pid, status)
1790 int pid;
1791 struct target_waitstatus *status;
1792 {
1793 int rstatus;
1794 int err;
1795 char buff[DATA_MAXLEN];
1796 int rpc, rfp, rsp;
1797 char flags[20];
1798 int nfields;
1799 int i;
1800
1801 interrupt_count = 0;
1802 hit_watchpoint = 0;
1803
1804 /* If we have not sent a single step or continue command, then the
1805 board is waiting for us to do something. Return a status
1806 indicating that it is stopped. */
1807 if (!mips_need_reply)
1808 {
1809 status->kind = TARGET_WAITKIND_STOPPED;
1810 status->value.sig = TARGET_SIGNAL_TRAP;
1811 return 0;
1812 }
1813
1814 /* No timeout; we sit here as long as the program continues to execute. */
1815 mips_wait_flag = 1;
1816 rstatus = mips_request ('\000', 0, 0, &err, -1, buff);
1817 mips_wait_flag = 0;
1818 if (err)
1819 mips_error ("Remote failure: %s", safe_strerror (errno));
1820
1821 /* On returning from a continue, the PMON monitor seems to start
1822 echoing back the messages we send prior to sending back the
1823 ACK. The code can cope with this, but to try and avoid the
1824 unnecessary serial traffic, and "spurious" characters displayed
1825 to the user, we cheat and reset the debug protocol. The problems
1826 seems to be caused by a check on the number of arguments, and the
1827 command length, within the monitor causing it to echo the command
1828 as a bad packet. */
1829 if (mips_monitor == MON_PMON)
1830 {
1831 mips_exit_debug ();
1832 mips_enter_debug ();
1833 }
1834
1835 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1836
1837 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1838 &rpc, &rfp, &rsp, flags);
1839 if (nfields >= 3)
1840 {
1841 char buf[MAX_REGISTER_RAW_SIZE];
1842
1843 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1844 supply_register (PC_REGNUM, buf);
1845
1846 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1847 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1848
1849 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1850 supply_register (SP_REGNUM, buf);
1851
1852 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1853 supply_register (FP_REGNUM, buf);
1854
1855 if (nfields == 9)
1856 {
1857 int i;
1858
1859 for (i = 0; i <= 2; i++)
1860 if (flags[i] == 'r' || flags[i] == 'w')
1861 hit_watchpoint = 1;
1862 else if (flags[i] == '\000')
1863 break;
1864 }
1865 }
1866
1867 if (strcmp (target_shortname, "lsi") == 0)
1868 {
1869 #if 0
1870 /* If this is an LSI PMON target, see if we just hit a hardrdware watchpoint.
1871 Right now, PMON doesn't give us enough information to determine which
1872 breakpoint we hit. So we have to look up the PC in our own table
1873 of breakpoints, and if found, assume it's just a normal instruction
1874 fetch breakpoint, not a data watchpoint. FIXME when PMON
1875 provides some way to tell us what type of breakpoint it is. */
1876 int i;
1877 CORE_ADDR pc = read_pc ();
1878
1879 hit_watchpoint = 1;
1880 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1881 {
1882 if (lsi_breakpoints[i].addr == pc
1883 && lsi_breakpoints[i].type == BREAK_FETCH)
1884 {
1885 hit_watchpoint = 0;
1886 break;
1887 }
1888 }
1889 #else
1890 /* If a data breakpoint was hit, PMON returns the following packet:
1891 0x1 c 0x0 0x57f 0x1
1892 The return packet from an ordinary breakpoint doesn't have the
1893 extra 0x01 field tacked onto the end. */
1894 if (nfields == 1 && rpc == 1)
1895 hit_watchpoint = 1;
1896 #endif
1897 }
1898
1899 /* NOTE: The following (sig) numbers are defined by PMON:
1900 SPP_SIGTRAP 5 breakpoint
1901 SPP_SIGINT 2
1902 SPP_SIGSEGV 11
1903 SPP_SIGBUS 10
1904 SPP_SIGILL 4
1905 SPP_SIGFPE 8
1906 SPP_SIGTERM 15 */
1907
1908 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1909 and so on, because the constants we want here are determined by the
1910 MIPS protocol and have nothing to do with what host we are running on. */
1911 if ((rstatus & 0xff) == 0)
1912 {
1913 status->kind = TARGET_WAITKIND_EXITED;
1914 status->value.integer = (((rstatus) >> 8) & 0xff);
1915 }
1916 else if ((rstatus & 0xff) == 0x7f)
1917 {
1918 status->kind = TARGET_WAITKIND_STOPPED;
1919 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0xff);
1920
1921 /* If the stop PC is in the _exit function, assume
1922 we hit the 'break 0x3ff' instruction in _exit, so this
1923 is not a normal breakpoint. */
1924 if (strcmp (target_shortname, "lsi") == 0)
1925 {
1926 char *func_name;
1927 CORE_ADDR func_start;
1928 CORE_ADDR pc = read_pc ();
1929
1930 find_pc_partial_function (pc, &func_name, &func_start, NULL);
1931 if (func_name != NULL && strcmp (func_name, "_exit") == 0
1932 && func_start == pc)
1933 status->kind = TARGET_WAITKIND_EXITED;
1934 }
1935 }
1936 else
1937 {
1938 status->kind = TARGET_WAITKIND_SIGNALLED;
1939 status->value.sig = mips_signal_from_protocol (rstatus & 0x7f);
1940 }
1941
1942 return 0;
1943 }
1944
1945 /* We have to map between the register numbers used by gdb and the
1946 register numbers used by the debugging protocol. This function
1947 assumes that we are using tm-mips.h. */
1948
1949 #define REGNO_OFFSET 96
1950
1951 static int
1952 mips_map_regno (regno)
1953 int regno;
1954 {
1955 if (regno < 32)
1956 return regno;
1957 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1958 return regno - FP0_REGNUM + 32;
1959 switch (regno)
1960 {
1961 case PC_REGNUM:
1962 return REGNO_OFFSET + 0;
1963 case CAUSE_REGNUM:
1964 return REGNO_OFFSET + 1;
1965 case HI_REGNUM:
1966 return REGNO_OFFSET + 2;
1967 case LO_REGNUM:
1968 return REGNO_OFFSET + 3;
1969 case FCRCS_REGNUM:
1970 return REGNO_OFFSET + 4;
1971 case FCRIR_REGNUM:
1972 return REGNO_OFFSET + 5;
1973 default:
1974 /* FIXME: Is there a way to get the status register? */
1975 return 0;
1976 }
1977 }
1978
1979 /* Fetch the remote registers. */
1980
1981 static void
1982 mips_fetch_registers (regno)
1983 int regno;
1984 {
1985 unsigned LONGEST val;
1986 int err;
1987
1988 if (regno == -1)
1989 {
1990 for (regno = 0; regno < NUM_REGS; regno++)
1991 mips_fetch_registers (regno);
1992 return;
1993 }
1994
1995 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1996 /* FP_REGNUM on the mips is a hack which is just supposed to read
1997 zero (see also mips-nat.c). */
1998 val = 0;
1999 else
2000 {
2001 /* If PMON doesn't support this register, don't waste serial
2002 bandwidth trying to read it. */
2003 int pmon_reg = mips_map_regno (regno);
2004 if (regno != 0 && pmon_reg == 0)
2005 val = 0;
2006 else
2007 {
2008 /* Unfortunately the PMON version in the Vr4300 board has been
2009 compiled without the 64bit register access commands. This
2010 means we cannot get hold of the full register width. */
2011 if (mips_monitor == MON_DDB)
2012 val = (unsigned) mips_request ('t', pmon_reg, 0,
2013 &err, mips_receive_wait, NULL);
2014 else
2015 val = mips_request ('r', pmon_reg, 0,
2016 &err, mips_receive_wait, NULL);
2017 if (err)
2018 mips_error ("Can't read register %d: %s", regno,
2019 safe_strerror (errno));
2020 }
2021 }
2022
2023 {
2024 char buf[MAX_REGISTER_RAW_SIZE];
2025
2026 /* We got the number the register holds, but gdb expects to see a
2027 value in the target byte ordering. */
2028 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
2029 supply_register (regno, buf);
2030 }
2031 }
2032
2033 /* Prepare to store registers. The MIPS protocol can store individual
2034 registers, so this function doesn't have to do anything. */
2035
2036 static void
2037 mips_prepare_to_store ()
2038 {
2039 }
2040
2041 /* Store remote register(s). */
2042
2043 static void
2044 mips_store_registers (regno)
2045 int regno;
2046 {
2047 int err;
2048
2049 if (regno == -1)
2050 {
2051 for (regno = 0; regno < NUM_REGS; regno++)
2052 mips_store_registers (regno);
2053 return;
2054 }
2055
2056 mips_request ('R', mips_map_regno (regno),
2057 read_register (regno),
2058 &err, mips_receive_wait, NULL);
2059 if (err)
2060 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
2061 }
2062
2063 /* Fetch a word from the target board. */
2064
2065 static unsigned int
2066 mips_fetch_word (addr)
2067 CORE_ADDR addr;
2068 {
2069 unsigned int val;
2070 int err;
2071
2072 val = mips_request ('d', addr, 0, &err, mips_receive_wait, NULL);
2073 if (err)
2074 {
2075 /* Data space failed; try instruction space. */
2076 val = mips_request ('i', addr, 0, &err,
2077 mips_receive_wait, NULL);
2078 if (err)
2079 mips_error ("Can't read address 0x%s: %s",
2080 paddr_nz (addr), safe_strerror (errno));
2081 }
2082 return val;
2083 }
2084
2085 /* Store a word to the target board. Returns errno code or zero for
2086 success. If OLD_CONTENTS is non-NULL, put the old contents of that
2087 memory location there. */
2088
2089 /* FIXME! make sure only 32-bit quantities get stored! */
2090 static int
2091 mips_store_word (addr, val, old_contents)
2092 CORE_ADDR addr;
2093 unsigned int val;
2094 char *old_contents;
2095 {
2096 int err;
2097 unsigned int oldcontents;
2098
2099 oldcontents = mips_request ('D', addr, val, &err,
2100 mips_receive_wait, NULL);
2101 if (err)
2102 {
2103 /* Data space failed; try instruction space. */
2104 oldcontents = mips_request ('I', addr, val, &err,
2105 mips_receive_wait, NULL);
2106 if (err)
2107 return errno;
2108 }
2109 if (old_contents != NULL)
2110 store_unsigned_integer (old_contents, 4, oldcontents);
2111 return 0;
2112 }
2113
2114 /* Read or write LEN bytes from inferior memory at MEMADDR,
2115 transferring to or from debugger address MYADDR. Write to inferior
2116 if SHOULD_WRITE is nonzero. Returns length of data written or
2117 read; 0 for error. Note that protocol gives us the correct value
2118 for a longword, since it transfers values in ASCII. We want the
2119 byte values, so we have to swap the longword values. */
2120
2121 static int mask_address_p = 1;
2122
2123 static int
2124 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
2125 CORE_ADDR memaddr;
2126 char *myaddr;
2127 int len;
2128 int write;
2129 struct target_ops *ignore;
2130 {
2131 int i;
2132 CORE_ADDR addr;
2133 int count;
2134 char *buffer;
2135 int status;
2136
2137 /* PMON targets do not cope well with 64 bit addresses. Mask the
2138 value down to 32 bits. */
2139 if (mask_address_p)
2140 memaddr &= (CORE_ADDR) 0xffffffff;
2141
2142 /* Round starting address down to longword boundary. */
2143 addr = memaddr & ~3;
2144 /* Round ending address up; get number of longwords that makes. */
2145 count = (((memaddr + len) - addr) + 3) / 4;
2146 /* Allocate buffer of that many longwords. */
2147 buffer = alloca (count * 4);
2148
2149 if (write)
2150 {
2151 /* Fill start and end extra bytes of buffer with existing data. */
2152 if (addr != memaddr || len < 4)
2153 {
2154 /* Need part of initial word -- fetch it. */
2155 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
2156 }
2157
2158 if (count > 1)
2159 {
2160 /* Need part of last word -- fetch it. FIXME: we do this even
2161 if we don't need it. */
2162 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2163 mips_fetch_word (addr + (count - 1) * 4));
2164 }
2165
2166 /* Copy data to be written over corresponding part of buffer */
2167
2168 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2169
2170 /* Write the entire buffer. */
2171
2172 for (i = 0; i < count; i++, addr += 4)
2173 {
2174 status = mips_store_word (addr,
2175 extract_unsigned_integer (&buffer[i * 4], 4),
2176 NULL);
2177 /* Report each kilobyte (we download 32-bit words at a time) */
2178 if (i % 256 == 255)
2179 {
2180 printf_unfiltered ("*");
2181 gdb_flush (gdb_stdout);
2182 }
2183 if (status)
2184 {
2185 errno = status;
2186 return 0;
2187 }
2188 /* FIXME: Do we want a QUIT here? */
2189 }
2190 if (count >= 256)
2191 printf_unfiltered ("\n");
2192 }
2193 else
2194 {
2195 /* Read all the longwords */
2196 for (i = 0; i < count; i++, addr += 4)
2197 {
2198 store_unsigned_integer (&buffer[i * 4], 4, mips_fetch_word (addr));
2199 QUIT;
2200 }
2201
2202 /* Copy appropriate bytes out of the buffer. */
2203 memcpy (myaddr, buffer + (memaddr & 3), len);
2204 }
2205 return len;
2206 }
2207
2208 /* Print info on this target. */
2209
2210 static void
2211 mips_files_info (ignore)
2212 struct target_ops *ignore;
2213 {
2214 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
2215 }
2216
2217 /* Kill the process running on the board. This will actually only
2218 work if we are doing remote debugging over the console input. I
2219 think that if IDT/sim had the remote debug interrupt enabled on the
2220 right port, we could interrupt the process with a break signal. */
2221
2222 static void
2223 mips_kill ()
2224 {
2225 if (!mips_wait_flag)
2226 return;
2227
2228 interrupt_count++;
2229
2230 if (interrupt_count >= 2)
2231 {
2232 interrupt_count = 0;
2233
2234 target_terminal_ours ();
2235
2236 if (query ("Interrupted while waiting for the program.\n\
2237 Give up (and stop debugging it)? "))
2238 {
2239 /* Clean up in such a way that mips_close won't try to talk to the
2240 board (it almost surely won't work since we weren't able to talk to
2241 it). */
2242 mips_wait_flag = 0;
2243 close_ports ();
2244
2245 printf_unfiltered ("Ending remote MIPS debugging.\n");
2246 target_mourn_inferior ();
2247
2248 return_to_top_level (RETURN_QUIT);
2249 }
2250
2251 target_terminal_inferior ();
2252 }
2253
2254 if (remote_debug > 0)
2255 printf_unfiltered ("Sending break\n");
2256
2257 SERIAL_SEND_BREAK (mips_desc);
2258
2259 #if 0
2260 if (mips_is_open)
2261 {
2262 char cc;
2263
2264 /* Send a ^C. */
2265 cc = '\003';
2266 SERIAL_WRITE (mips_desc, &cc, 1);
2267 sleep (1);
2268 target_mourn_inferior ();
2269 }
2270 #endif
2271 }
2272
2273 /* Start running on the target board. */
2274
2275 static void
2276 mips_create_inferior (execfile, args, env)
2277 char *execfile;
2278 char *args;
2279 char **env;
2280 {
2281 CORE_ADDR entry_pt;
2282
2283 if (args && *args)
2284 {
2285 warning ("\
2286 Can't pass arguments to remote MIPS board; arguments ignored.");
2287 /* And don't try to use them on the next "run" command. */
2288 execute_command ("set args", 0);
2289 }
2290
2291 if (execfile == 0 || exec_bfd == 0)
2292 error ("No executable file specified");
2293
2294 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2295
2296 init_wait_for_inferior ();
2297
2298 /* FIXME: Should we set inferior_pid here? */
2299
2300 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
2301 }
2302
2303 /* Clean up after a process. Actually nothing to do. */
2304
2305 static void
2306 mips_mourn_inferior ()
2307 {
2308 if (current_ops != NULL)
2309 unpush_target (current_ops);
2310 generic_mourn_inferior ();
2311 }
2312 \f
2313 /* We can write a breakpoint and read the shadow contents in one
2314 operation. */
2315
2316 /* Insert a breakpoint. On targets that don't have built-in breakpoint
2317 support, we read the contents of the target location and stash it,
2318 then overwrite it with a breakpoint instruction. ADDR is the target
2319 location in the target machine. CONTENTS_CACHE is a pointer to
2320 memory allocated for saving the target contents. It is guaranteed
2321 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2322 is accomplished via BREAKPOINT_MAX). */
2323
2324 static int
2325 mips_insert_breakpoint (addr, contents_cache)
2326 CORE_ADDR addr;
2327 char *contents_cache;
2328 {
2329 if (monitor_supports_breakpoints)
2330 return set_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2331 else
2332 return memory_insert_breakpoint (addr, contents_cache);
2333 }
2334
2335 static int
2336 mips_remove_breakpoint (addr, contents_cache)
2337 CORE_ADDR addr;
2338 char *contents_cache;
2339 {
2340 if (monitor_supports_breakpoints)
2341 return clear_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2342 else
2343 return memory_remove_breakpoint (addr, contents_cache);
2344 }
2345
2346 #if 0 /* currently not used */
2347 /* PMON does not currently provide support for the debug mode 'b'
2348 commands to manipulate breakpoints. However, if we wanted to use
2349 the monitor breakpoints (rather than the GDB BREAK_INSN version)
2350 then this code performs the work needed to leave debug mode,
2351 set/clear the breakpoint, and then return to debug mode. */
2352
2353 #define PMON_MAX_BP (33) /* 32 SW, 1 HW */
2354 static CORE_ADDR mips_pmon_bp_info[PMON_MAX_BP];
2355 /* NOTE: The code relies on this vector being zero-initialised by the system */
2356
2357 static int
2358 pmon_insert_breakpoint (addr, contents_cache)
2359 CORE_ADDR addr;
2360 char *contents_cache;
2361 {
2362 int status;
2363
2364 if (monitor_supports_breakpoints)
2365 {
2366 char tbuff[12]; /* space for breakpoint command */
2367 int bpnum;
2368 CORE_ADDR bpaddr;
2369
2370 /* PMON does not support debug level breakpoint set/remove: */
2371 if (mips_exit_debug ())
2372 mips_error ("Failed to exit debug mode");
2373
2374 sprintf (tbuff, "b %08x\r", addr);
2375 mips_send_command (tbuff, 0);
2376
2377 mips_expect ("Bpt ");
2378
2379 if (!mips_getstring (tbuff, 2))
2380 return 1;
2381 tbuff[2] = '\0'; /* terminate the string */
2382 if (sscanf (tbuff, "%d", &bpnum) != 1)
2383 {
2384 fprintf_unfiltered (gdb_stderr,
2385 "Invalid decimal breakpoint number from target: %s\n", tbuff);
2386 return 1;
2387 }
2388
2389 mips_expect (" = ");
2390
2391 /* Lead in the hex number we are expecting: */
2392 tbuff[0] = '0';
2393 tbuff[1] = 'x';
2394
2395 /* FIXME!! only 8 bytes! need to expand for Bfd64;
2396 which targets return 64-bit addresses? PMON returns only 32! */
2397 if (!mips_getstring (&tbuff[2], 8))
2398 return 1;
2399 tbuff[10] = '\0'; /* terminate the string */
2400
2401 if (sscanf (tbuff, "0x%08x", &bpaddr) != 1)
2402 {
2403 fprintf_unfiltered (gdb_stderr,
2404 "Invalid hex address from target: %s\n", tbuff);
2405 return 1;
2406 }
2407
2408 if (bpnum >= PMON_MAX_BP)
2409 {
2410 fprintf_unfiltered (gdb_stderr,
2411 "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n",
2412 bpnum, PMON_MAX_BP - 1);
2413 return 1;
2414 }
2415
2416 if (bpaddr != addr)
2417 fprintf_unfiltered (gdb_stderr, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr, bpaddr);
2418
2419 mips_pmon_bp_info[bpnum] = bpaddr;
2420
2421 mips_expect ("\r\n");
2422 mips_expect (mips_monitor_prompt);
2423
2424 mips_enter_debug ();
2425
2426 return 0;
2427 }
2428
2429 return mips_store_word (addr, BREAK_INSN, contents_cache);
2430 }
2431
2432 static int
2433 pmon_remove_breakpoint (addr, contents_cache)
2434 CORE_ADDR addr;
2435 char *contents_cache;
2436 {
2437 if (monitor_supports_breakpoints)
2438 {
2439 int bpnum;
2440 char tbuff[7]; /* enough for delete breakpoint command */
2441
2442 for (bpnum = 0; bpnum < PMON_MAX_BP; bpnum++)
2443 if (mips_pmon_bp_info[bpnum] == addr)
2444 break;
2445
2446 if (bpnum >= PMON_MAX_BP)
2447 {
2448 fprintf_unfiltered (gdb_stderr,
2449 "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n",
2450 paddr_nz (addr));
2451 return 1;
2452 }
2453
2454 if (mips_exit_debug ())
2455 mips_error ("Failed to exit debug mode");
2456
2457 sprintf (tbuff, "db %02d\r", bpnum);
2458
2459 mips_send_command (tbuff, -1);
2460 /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not
2461 set" message will be returned. */
2462
2463 mips_enter_debug ();
2464
2465 return 0;
2466 }
2467
2468 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2469 }
2470 #endif
2471
2472
2473 /* Tell whether this target can support a hardware breakpoint. CNT
2474 is the number of hardware breakpoints already installed. This
2475 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
2476
2477 int
2478 remote_mips_can_use_hardware_watchpoint (cnt)
2479 int cnt;
2480 {
2481 return cnt < MAX_LSI_BREAKPOINTS && strcmp (target_shortname, "lsi") == 0;
2482 }
2483
2484
2485 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2486 This is used for memory ref breakpoints. */
2487
2488 static unsigned long
2489 calculate_mask (addr, len)
2490 CORE_ADDR addr;
2491 int len;
2492 {
2493 unsigned long mask;
2494 int i;
2495
2496 mask = addr ^ (addr + len - 1);
2497
2498 for (i = 32; i >= 0; i--)
2499 if (mask == 0)
2500 break;
2501 else
2502 mask >>= 1;
2503
2504 mask = (unsigned long) 0xffffffff >> i;
2505
2506 return mask;
2507 }
2508
2509
2510 /* Insert a hardware breakpoint. This works only on LSI targets, which
2511 implement ordinary breakpoints using hardware facilities. */
2512
2513 int
2514 remote_mips_insert_hw_breakpoint (addr, contents_cache)
2515 CORE_ADDR addr;
2516 char *contents_cache;
2517 {
2518 if (strcmp (target_shortname, "lsi") == 0)
2519 return mips_insert_breakpoint (addr, contents_cache);
2520 else
2521 return -1;
2522 }
2523
2524
2525 /* Remove a hardware breakpoint. This works only on LSI targets, which
2526 implement ordinary breakpoints using hardware facilities. */
2527
2528 int
2529 remote_mips_remove_hw_breakpoint (addr, contents_cache)
2530 CORE_ADDR addr;
2531 char *contents_cache;
2532 {
2533 if (strcmp (target_shortname, "lsi") == 0)
2534 return mips_remove_breakpoint (addr, contents_cache);
2535 else
2536 return -1;
2537 }
2538
2539 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
2540 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
2541 watchpoint. */
2542
2543 int
2544 remote_mips_set_watchpoint (addr, len, type)
2545 CORE_ADDR addr;
2546 int len;
2547 int type;
2548 {
2549 if (set_breakpoint (addr, len, type))
2550 return -1;
2551
2552 return 0;
2553 }
2554
2555 int
2556 remote_mips_remove_watchpoint (addr, len, type)
2557 CORE_ADDR addr;
2558 int len;
2559 int type;
2560 {
2561 if (clear_breakpoint (addr, len, type))
2562 return -1;
2563
2564 return 0;
2565 }
2566
2567 int
2568 remote_mips_stopped_by_watchpoint (void)
2569 {
2570 return hit_watchpoint;
2571 }
2572
2573
2574 /* Insert a breakpoint. */
2575
2576 static int
2577 set_breakpoint (addr, len, type)
2578 CORE_ADDR addr;
2579 int len;
2580 enum break_type type;
2581 {
2582 return common_breakpoint (1, addr, len, type);
2583 }
2584
2585
2586 /* Clear a breakpoint. */
2587
2588 static int
2589 clear_breakpoint (addr, len, type)
2590 CORE_ADDR addr;
2591 int len;
2592 enum break_type type;
2593 {
2594 return common_breakpoint (0, addr, len, type);
2595 }
2596
2597
2598 /* Check the error code from the return packet for an LSI breakpoint
2599 command. If there's no error, just return 0. If it's a warning,
2600 print the warning text and return 0. If it's an error, print
2601 the error text and return 1. <ADDR> is the address of the breakpoint
2602 that was being set. <RERRFLG> is the error code returned by PMON.
2603 This is a helper function for common_breakpoint. */
2604
2605 static int
2606 check_lsi_error (addr, rerrflg)
2607 CORE_ADDR addr;
2608 int rerrflg;
2609 {
2610 struct lsi_error *err;
2611 char *saddr = paddr_nz (addr); /* printable address string */
2612
2613 if (rerrflg == 0) /* no error */
2614 return 0;
2615
2616 /* Warnings can be ORed together, so check them all. */
2617 if (rerrflg & W_WARN)
2618 {
2619 if (monitor_warnings)
2620 {
2621 int found = 0;
2622 for (err = lsi_warning_table; err->code != 0; err++)
2623 {
2624 if ((err->code & rerrflg) == err->code)
2625 {
2626 found = 1;
2627 fprintf_unfiltered (gdb_stderr,
2628 "common_breakpoint (0x%s): Warning: %s\n",
2629 saddr,
2630 err->string);
2631 }
2632 }
2633 if (!found)
2634 fprintf_unfiltered (gdb_stderr,
2635 "common_breakpoint (0x%s): Unknown warning: 0x%x\n",
2636 saddr,
2637 rerrflg);
2638 }
2639 return 0;
2640 }
2641
2642 /* Errors are unique, i.e. can't be ORed together. */
2643 for (err = lsi_error_table; err->code != 0; err++)
2644 {
2645 if ((err->code & rerrflg) == err->code)
2646 {
2647 fprintf_unfiltered (gdb_stderr,
2648 "common_breakpoint (0x%s): Error: %s\n",
2649 saddr,
2650 err->string);
2651 return 1;
2652 }
2653 }
2654 fprintf_unfiltered (gdb_stderr,
2655 "common_breakpoint (0x%s): Unknown error: 0x%x\n",
2656 saddr,
2657 rerrflg);
2658 return 1;
2659 }
2660
2661
2662 /* This routine sends a breakpoint command to the remote target.
2663
2664 <SET> is 1 if setting a breakpoint, or 0 if clearing a breakpoint.
2665 <ADDR> is the address of the breakpoint.
2666 <LEN> the length of the region to break on.
2667 <TYPE> is the type of breakpoint:
2668 0 = write (BREAK_WRITE)
2669 1 = read (BREAK_READ)
2670 2 = read/write (BREAK_ACCESS)
2671 3 = instruction fetch (BREAK_FETCH)
2672
2673 Return 0 if successful; otherwise 1. */
2674
2675 static int
2676 common_breakpoint (set, addr, len, type)
2677 int set;
2678 CORE_ADDR addr;
2679 int len;
2680 enum break_type type;
2681 {
2682 char buf[DATA_MAXLEN + 1];
2683 char cmd, rcmd;
2684 int rpid, rerrflg, rresponse, rlen;
2685 int nfields;
2686
2687 addr = ADDR_BITS_REMOVE (addr);
2688
2689 if (mips_monitor == MON_LSI)
2690 {
2691 if (set == 0) /* clear breakpoint */
2692 {
2693 /* The LSI PMON "clear breakpoint" has this form:
2694 <pid> 'b' <bptn> 0x0
2695 reply:
2696 <pid> 'b' 0x0 <code>
2697
2698 <bptn> is a breakpoint number returned by an earlier 'B' command.
2699 Possible return codes: OK, E_BPT. */
2700
2701 int i;
2702
2703 /* Search for the breakpoint in the table. */
2704 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
2705 if (lsi_breakpoints[i].type == type
2706 && lsi_breakpoints[i].addr == addr
2707 && lsi_breakpoints[i].len == len)
2708 break;
2709
2710 /* Clear the table entry and tell PMON to clear the breakpoint. */
2711 if (i == MAX_LSI_BREAKPOINTS)
2712 {
2713 warning ("common_breakpoint: Attempt to clear bogus breakpoint at %s\n",
2714 paddr_nz (addr));
2715 return 1;
2716 }
2717
2718 lsi_breakpoints[i].type = BREAK_UNUSED;
2719 sprintf (buf, "0x0 b 0x%x 0x0", i);
2720 mips_send_packet (buf, 1);
2721
2722 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2723 buf[rlen] = '\0';
2724
2725 nfields = sscanf (buf, "0x%x b 0x0 0x%x", &rpid, &rerrflg);
2726 if (nfields != 2)
2727 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2728
2729 return (check_lsi_error (addr, rerrflg));
2730 }
2731 else
2732 /* set a breakpoint */
2733 {
2734 /* The LSI PMON "set breakpoint" command has this form:
2735 <pid> 'B' <addr> 0x0
2736 reply:
2737 <pid> 'B' <bptn> <code>
2738
2739 The "set data breakpoint" command has this form:
2740
2741 <pid> 'A' <addr1> <type> [<addr2> [<value>]]
2742
2743 where: type= "0x1" = read
2744 "0x2" = write
2745 "0x3" = access (read or write)
2746
2747 The reply returns two values:
2748 bptn - a breakpoint number, which is a small integer with
2749 possible values of zero through 255.
2750 code - an error return code, a value of zero indicates a
2751 succesful completion, other values indicate various
2752 errors and warnings.
2753
2754 Possible return codes: OK, W_QAL, E_QAL, E_OUT, E_NON.
2755
2756 */
2757
2758 if (type == BREAK_FETCH) /* instruction breakpoint */
2759 {
2760 cmd = 'B';
2761 sprintf (buf, "0x0 B 0x%s 0x0", paddr_nz (addr));
2762 }
2763 else
2764 /* watchpoint */
2765 {
2766 cmd = 'A';
2767 sprintf (buf, "0x0 A 0x%s 0x%x 0x%s", paddr_nz (addr),
2768 type == BREAK_READ ? 1 : (type == BREAK_WRITE ? 2 : 3),
2769 paddr_nz (addr + len - 1));
2770 }
2771 mips_send_packet (buf, 1);
2772
2773 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2774 buf[rlen] = '\0';
2775
2776 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2777 &rpid, &rcmd, &rresponse, &rerrflg);
2778 if (nfields != 4 || rcmd != cmd || rresponse > 255)
2779 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2780
2781 if (rerrflg != 0)
2782 if (check_lsi_error (addr, rerrflg))
2783 return 1;
2784
2785 /* rresponse contains PMON's breakpoint number. Record the
2786 information for this breakpoint so we can clear it later. */
2787 lsi_breakpoints[rresponse].type = type;
2788 lsi_breakpoints[rresponse].addr = addr;
2789 lsi_breakpoints[rresponse].len = len;
2790
2791 return 0;
2792 }
2793 }
2794 else
2795 {
2796 /* On non-LSI targets, the breakpoint command has this form:
2797 0x0 <CMD> <ADDR> <MASK> <FLAGS>
2798 <MASK> is a don't care mask for addresses.
2799 <FLAGS> is any combination of `r', `w', or `f' for read/write/fetch.
2800 */
2801 unsigned long mask;
2802
2803 mask = calculate_mask (addr, len);
2804 addr &= ~mask;
2805
2806 if (set) /* set a breakpoint */
2807 {
2808 char *flags;
2809 switch (type)
2810 {
2811 case BREAK_WRITE: /* write */
2812 flags = "w";
2813 break;
2814 case BREAK_READ: /* read */
2815 flags = "r";
2816 break;
2817 case BREAK_ACCESS: /* read/write */
2818 flags = "rw";
2819 break;
2820 case BREAK_FETCH: /* fetch */
2821 flags = "f";
2822 break;
2823 default:
2824 abort ();
2825 }
2826
2827 cmd = 'B';
2828 sprintf (buf, "0x0 B 0x%s 0x%s %s", paddr_nz (addr),
2829 paddr_nz (mask), flags);
2830 }
2831 else
2832 {
2833 cmd = 'b';
2834 sprintf (buf, "0x0 b 0x%s", paddr_nz (addr));
2835 }
2836
2837 mips_send_packet (buf, 1);
2838
2839 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2840 buf[rlen] = '\0';
2841
2842 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2843 &rpid, &rcmd, &rerrflg, &rresponse);
2844
2845 if (nfields != 4 || rcmd != cmd)
2846 mips_error ("common_breakpoint: Bad response from remote board: %s",
2847 buf);
2848
2849 if (rerrflg != 0)
2850 {
2851 /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2852 Cogent returns "0x0 b 0xffffffff 0x16\000": */
2853 if (mips_monitor == MON_DDB)
2854 rresponse = rerrflg;
2855 if (rresponse != 22) /* invalid argument */
2856 fprintf_unfiltered (gdb_stderr,
2857 "common_breakpoint (0x%s): Got error: 0x%x\n",
2858 paddr_nz (addr), rresponse);
2859 return 1;
2860 }
2861 }
2862 return 0;
2863 }
2864 \f
2865 static void
2866 send_srec (srec, len, addr)
2867 char *srec;
2868 int len;
2869 CORE_ADDR addr;
2870 {
2871 while (1)
2872 {
2873 int ch;
2874
2875 SERIAL_WRITE (mips_desc, srec, len);
2876
2877 ch = mips_readchar (2);
2878
2879 switch (ch)
2880 {
2881 case SERIAL_TIMEOUT:
2882 error ("Timeout during download.");
2883 break;
2884 case 0x6: /* ACK */
2885 return;
2886 case 0x15: /* NACK */
2887 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %s! Retrying.\n", paddr_u (addr));
2888 continue;
2889 default:
2890 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2891 }
2892 }
2893 }
2894
2895 /* Download a binary file by converting it to S records. */
2896
2897 static void
2898 mips_load_srec (args)
2899 char *args;
2900 {
2901 bfd *abfd;
2902 asection *s;
2903 char *buffer, srec[1024];
2904 unsigned int i;
2905 unsigned int srec_frame = 200;
2906 int reclen;
2907 static int hashmark = 1;
2908
2909 buffer = alloca (srec_frame * 2 + 256);
2910
2911 abfd = bfd_openr (args, 0);
2912 if (!abfd)
2913 {
2914 printf_filtered ("Unable to open file %s\n", args);
2915 return;
2916 }
2917
2918 if (bfd_check_format (abfd, bfd_object) == 0)
2919 {
2920 printf_filtered ("File is not an object file\n");
2921 return;
2922 }
2923
2924 /* This actually causes a download in the IDT binary format: */
2925 mips_send_command (LOAD_CMD, 0);
2926
2927 for (s = abfd->sections; s; s = s->next)
2928 {
2929 if (s->flags & SEC_LOAD)
2930 {
2931 unsigned int numbytes;
2932
2933 /* FIXME! vma too small????? */
2934 printf_filtered ("%s\t: 0x%4lx .. 0x%4lx ", s->name,
2935 (long) s->vma,
2936 (long) (s->vma + s->_raw_size));
2937 gdb_flush (gdb_stdout);
2938
2939 for (i = 0; i < s->_raw_size; i += numbytes)
2940 {
2941 numbytes = min (srec_frame, s->_raw_size - i);
2942
2943 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2944
2945 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2946 send_srec (srec, reclen, s->vma + i);
2947
2948 if (hashmark)
2949 {
2950 putchar_unfiltered ('#');
2951 gdb_flush (gdb_stdout);
2952 }
2953
2954 } /* Per-packet (or S-record) loop */
2955
2956 putchar_unfiltered ('\n');
2957 } /* Loadable sections */
2958 }
2959 if (hashmark)
2960 putchar_unfiltered ('\n');
2961
2962 /* Write a type 7 terminator record. no data for a type 7, and there
2963 is no data, so len is 0. */
2964
2965 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2966
2967 send_srec (srec, reclen, abfd->start_address);
2968
2969 SERIAL_FLUSH_INPUT (mips_desc);
2970 }
2971
2972 /*
2973 * mips_make_srec -- make an srecord. This writes each line, one at a
2974 * time, each with it's own header and trailer line.
2975 * An srecord looks like this:
2976 *
2977 * byte count-+ address
2978 * start ---+ | | data +- checksum
2979 * | | | |
2980 * S01000006F6B692D746573742E73726563E4
2981 * S315000448600000000000000000FC00005900000000E9
2982 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2983 * S30B0004485A0000000000004E
2984 * S70500040000F6
2985 *
2986 * S<type><length><address><data><checksum>
2987 *
2988 * Where
2989 * - length
2990 * is the number of bytes following upto the checksum. Note that
2991 * this is not the number of chars following, since it takes two
2992 * chars to represent a byte.
2993 * - type
2994 * is one of:
2995 * 0) header record
2996 * 1) two byte address data record
2997 * 2) three byte address data record
2998 * 3) four byte address data record
2999 * 7) four byte address termination record
3000 * 8) three byte address termination record
3001 * 9) two byte address termination record
3002 *
3003 * - address
3004 * is the start address of the data following, or in the case of
3005 * a termination record, the start address of the image
3006 * - data
3007 * is the data.
3008 * - checksum
3009 * is the sum of all the raw byte data in the record, from the length
3010 * upwards, modulo 256 and subtracted from 255.
3011 *
3012 * This routine returns the length of the S-record.
3013 *
3014 */
3015
3016 static int
3017 mips_make_srec (buf, type, memaddr, myaddr, len)
3018 char *buf;
3019 int type;
3020 CORE_ADDR memaddr;
3021 unsigned char *myaddr;
3022 int len;
3023 {
3024 unsigned char checksum;
3025 int i;
3026
3027 /* Create the header for the srec. addr_size is the number of bytes in the address,
3028 and 1 is the number of bytes in the count. */
3029
3030 /* FIXME!! bigger buf required for 64-bit! */
3031 buf[0] = 'S';
3032 buf[1] = type;
3033 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
3034 /* This assumes S3 style downloads (4byte addresses). There should
3035 probably be a check, or the code changed to make it more
3036 explicit. */
3037 buf[3] = memaddr >> 24;
3038 buf[4] = memaddr >> 16;
3039 buf[5] = memaddr >> 8;
3040 buf[6] = memaddr;
3041 memcpy (&buf[7], myaddr, len);
3042
3043 /* Note that the checksum is calculated on the raw data, not the
3044 hexified data. It includes the length, address and the data
3045 portions of the packet. */
3046 checksum = 0;
3047 buf += 2; /* Point at length byte */
3048 for (i = 0; i < len + 4 + 1; i++)
3049 checksum += *buf++;
3050
3051 *buf = ~checksum;
3052
3053 return len + 8;
3054 }
3055
3056 /* The following manifest controls whether we enable the simple flow
3057 control support provided by the monitor. If enabled the code will
3058 wait for an affirmative ACK between transmitting packets. */
3059 #define DOETXACK (1)
3060
3061 /* The PMON fast-download uses an encoded packet format constructed of
3062 3byte data packets (encoded as 4 printable ASCII characters), and
3063 escape sequences (preceded by a '/'):
3064
3065 'K' clear checksum
3066 'C' compare checksum (12bit value, not included in checksum calculation)
3067 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary
3068 'Z' zero fill multiple of 3bytes
3069 'B' byte (12bit encoded value, of 8bit data)
3070 'A' address (36bit encoded value)
3071 'E' define entry as original address, and exit load
3072
3073 The packets are processed in 4 character chunks, so the escape
3074 sequences that do not have any data (or variable length data)
3075 should be padded to a 4 character boundary. The decoder will give
3076 an error if the complete message block size is not a multiple of
3077 4bytes (size of record).
3078
3079 The encoding of numbers is done in 6bit fields. The 6bit value is
3080 used to index into this string to get the specific character
3081 encoding for the value: */
3082 static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
3083
3084 /* Convert the number of bits required into an encoded number, 6bits
3085 at a time (range 0..63). Keep a checksum if required (passed
3086 pointer non-NULL). The function returns the number of encoded
3087 characters written into the buffer. */
3088 static int
3089 pmon_makeb64 (v, p, n, chksum)
3090 unsigned long v;
3091 char *p;
3092 int n;
3093 int *chksum;
3094 {
3095 int count = (n / 6);
3096
3097 if ((n % 12) != 0)
3098 {
3099 fprintf_unfiltered (gdb_stderr,
3100 "Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n", n, (n == 1) ? "" : "s");
3101 return (0);
3102 }
3103 if (n > 36)
3104 {
3105 fprintf_unfiltered (gdb_stderr,
3106 "Fast encoding cannot process more than 36bits at the moment: %dbits\n", n);
3107 return (0);
3108 }
3109
3110 /* Deal with the checksum: */
3111 if (chksum != NULL)
3112 {
3113 switch (n)
3114 {
3115 case 36:
3116 *chksum += ((v >> 24) & 0xFFF);
3117 case 24:
3118 *chksum += ((v >> 12) & 0xFFF);
3119 case 12:
3120 *chksum += ((v >> 0) & 0xFFF);
3121 }
3122 }
3123
3124 do
3125 {
3126 n -= 6;
3127 *p++ = encoding[(v >> n) & 0x3F];
3128 }
3129 while (n > 0);
3130
3131 return (count);
3132 }
3133
3134 /* Shorthand function (that could be in-lined) to output the zero-fill
3135 escape sequence into the data stream. */
3136 static int
3137 pmon_zeroset (recsize, buff, amount, chksum)
3138 int recsize;
3139 char **buff;
3140 int *amount;
3141 unsigned int *chksum;
3142 {
3143 int count;
3144
3145 sprintf (*buff, "/Z");
3146 count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
3147 *buff += (count + 2);
3148 *amount = 0;
3149 return (recsize + count + 2);
3150 }
3151
3152 static int
3153 pmon_checkset (recsize, buff, value)
3154 int recsize;
3155 char **buff;
3156 int *value;
3157 {
3158 int count;
3159
3160 /* Add the checksum (without updating the value): */
3161 sprintf (*buff, "/C");
3162 count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
3163 *buff += (count + 2);
3164 sprintf (*buff, "\n");
3165 *buff += 2; /* include zero terminator */
3166 /* Forcing a checksum validation clears the sum: */
3167 *value = 0;
3168 return (recsize + count + 3);
3169 }
3170
3171 /* Amount of padding we leave after at the end of the output buffer,
3172 for the checksum and line termination characters: */
3173 #define CHECKSIZE (4 + 4 + 4 + 2)
3174 /* zero-fill, checksum, transfer end and line termination space. */
3175
3176 /* The amount of binary data loaded from the object file in a single
3177 operation: */
3178 #define BINCHUNK (1024)
3179
3180 /* Maximum line of data accepted by the monitor: */
3181 #define MAXRECSIZE (550)
3182 /* NOTE: This constant depends on the monitor being used. This value
3183 is for PMON 5.x on the Cogent Vr4300 board. */
3184
3185 static void
3186 pmon_make_fastrec (outbuf, inbuf, inptr, inamount, recsize, csum, zerofill)
3187 char **outbuf;
3188 unsigned char *inbuf;
3189 int *inptr;
3190 int inamount;
3191 int *recsize;
3192 unsigned int *csum;
3193 unsigned int *zerofill;
3194 {
3195 int count = 0;
3196 char *p = *outbuf;
3197
3198 /* This is a simple check to ensure that our data will fit within
3199 the maximum allowable record size. Each record output is 4bytes
3200 in length. We must allow space for a pending zero fill command,
3201 the record, and a checksum record. */
3202 while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0))
3203 {
3204 /* Process the binary data: */
3205 if ((inamount - *inptr) < 3)
3206 {
3207 if (*zerofill != 0)
3208 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3209 sprintf (p, "/B");
3210 count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
3211 p += (2 + count);
3212 *recsize += (2 + count);
3213 (*inptr)++;
3214 }
3215 else
3216 {
3217 unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
3218 /* Simple check for zero data. TODO: A better check would be
3219 to check the last, and then the middle byte for being zero
3220 (if the first byte is not). We could then check for
3221 following runs of zeros, and if above a certain size it is
3222 worth the 4 or 8 character hit of the byte insertions used
3223 to pad to the start of the zeroes. NOTE: This also depends
3224 on the alignment at the end of the zero run. */
3225 if (value == 0x00000000)
3226 {
3227 (*zerofill)++;
3228 if (*zerofill == 0xFFF) /* 12bit counter */
3229 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3230 }
3231 else
3232 {
3233 if (*zerofill != 0)
3234 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3235 count = pmon_makeb64 (value, p, 24, csum);
3236 p += count;
3237 *recsize += count;
3238 }
3239 *inptr += 3;
3240 }
3241 }
3242
3243 *outbuf = p;
3244 return;
3245 }
3246
3247 static int
3248 pmon_check_ack (mesg)
3249 char *mesg;
3250 {
3251 #if defined(DOETXACK)
3252 int c;
3253
3254 if (!tftp_in_use)
3255 {
3256 c = SERIAL_READCHAR (udp_in_use ? udp_desc : mips_desc, 2);
3257 if ((c == SERIAL_TIMEOUT) || (c != 0x06))
3258 {
3259 fprintf_unfiltered (gdb_stderr,
3260 "Failed to receive valid ACK for %s\n", mesg);
3261 return (-1); /* terminate the download */
3262 }
3263 }
3264 #endif /* DOETXACK */
3265 return (0);
3266 }
3267
3268 /* pmon_download - Send a sequence of characters to the PMON download port,
3269 which is either a serial port or a UDP socket. */
3270
3271 static void
3272 pmon_start_download ()
3273 {
3274 if (tftp_in_use)
3275 {
3276 /* Create the temporary download file. */
3277 if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
3278 perror_with_name (tftp_localname);
3279 }
3280 else
3281 {
3282 mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
3283 mips_expect ("Downloading from ");
3284 mips_expect (udp_in_use ? "udp" : "tty0");
3285 mips_expect (", ^C to abort\r\n");
3286 }
3287 }
3288
3289 static int
3290 mips_expect_download (char *string)
3291 {
3292 if (!mips_expect (string))
3293 {
3294 fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
3295 if (tftp_in_use)
3296 remove (tftp_localname); /* Remove temporary file */
3297 return 0;
3298 }
3299 else
3300 return 1;
3301 }
3302
3303 static void
3304 pmon_end_download (final, bintotal)
3305 int final;
3306 int bintotal;
3307 {
3308 char hexnumber[9]; /* includes '\0' space */
3309
3310 if (tftp_in_use)
3311 {
3312 static char *load_cmd_prefix = "load -b -s ";
3313 char *cmd;
3314 struct stat stbuf;
3315
3316 /* Close off the temporary file containing the load data. */
3317 fclose (tftp_file);
3318 tftp_file = NULL;
3319
3320 /* Make the temporary file readable by the world. */
3321 if (stat (tftp_localname, &stbuf) == 0)
3322 chmod (tftp_localname, stbuf.st_mode | S_IROTH);
3323
3324 /* Must reinitialize the board to prevent PMON from crashing. */
3325 mips_send_command ("initEther\r", -1);
3326
3327 /* Send the load command. */
3328 cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
3329 strcpy (cmd, load_cmd_prefix);
3330 strcat (cmd, tftp_name);
3331 strcat (cmd, "\r");
3332 mips_send_command (cmd, 0);
3333 free (cmd);
3334 if (!mips_expect_download ("Downloading from "))
3335 return;
3336 if (!mips_expect_download (tftp_name))
3337 return;
3338 if (!mips_expect_download (", ^C to abort\r\n"))
3339 return;
3340 }
3341
3342 /* Wait for the stuff that PMON prints after the load has completed.
3343 The timeout value for use in the tftp case (15 seconds) was picked
3344 arbitrarily but might be too small for really large downloads. FIXME. */
3345 if (mips_monitor == MON_LSI)
3346 {
3347 pmon_check_ack ("termination");
3348 mips_expect_timeout ("Entry address is ", tftp_in_use ? 15 : 2);
3349 }
3350 else
3351 mips_expect_timeout ("Entry Address = ", tftp_in_use ? 15 : 2);
3352
3353 sprintf (hexnumber, "%x", final);
3354 mips_expect (hexnumber);
3355 mips_expect ("\r\n");
3356 if (mips_monitor != MON_LSI)
3357 pmon_check_ack ("termination");
3358 mips_expect ("\r\ntotal = 0x");
3359 sprintf (hexnumber, "%x", bintotal);
3360 mips_expect (hexnumber);
3361 if (!mips_expect_download (" bytes\r\n"))
3362 return;
3363
3364 if (tftp_in_use)
3365 remove (tftp_localname); /* Remove temporary file */
3366 }
3367
3368 static void
3369 pmon_download (buffer, length)
3370 char *buffer;
3371 int length;
3372 {
3373 if (tftp_in_use)
3374 fwrite (buffer, 1, length, tftp_file);
3375 else
3376 SERIAL_WRITE (udp_in_use ? udp_desc : mips_desc, buffer, length);
3377 }
3378
3379 static void
3380 pmon_load_fast (file)
3381 char *file;
3382 {
3383 bfd *abfd;
3384 asection *s;
3385 unsigned char *binbuf;
3386 char *buffer;
3387 int reclen;
3388 unsigned int csum = 0;
3389 int hashmark = !tftp_in_use;
3390 int bintotal = 0;
3391 int final = 0;
3392 int finished = 0;
3393
3394 buffer = (char *) xmalloc (MAXRECSIZE + 1);
3395 binbuf = (unsigned char *) xmalloc (BINCHUNK);
3396
3397 abfd = bfd_openr (file, 0);
3398 if (!abfd)
3399 {
3400 printf_filtered ("Unable to open file %s\n", file);
3401 return;
3402 }
3403
3404 if (bfd_check_format (abfd, bfd_object) == 0)
3405 {
3406 printf_filtered ("File is not an object file\n");
3407 return;
3408 }
3409
3410 /* Setup the required download state: */
3411 mips_send_command ("set dlproto etxack\r", -1);
3412 mips_send_command ("set dlecho off\r", -1);
3413 /* NOTE: We get a "cannot set variable" message if the variable is
3414 already defined to have the argument we give. The code doesn't
3415 care, since it just scans to the next prompt anyway. */
3416 /* Start the download: */
3417 pmon_start_download ();
3418
3419 /* Zero the checksum */
3420 sprintf (buffer, "/Kxx\n");
3421 reclen = strlen (buffer);
3422 pmon_download (buffer, reclen);
3423 finished = pmon_check_ack ("/Kxx");
3424
3425 for (s = abfd->sections; s && !finished; s = s->next)
3426 if (s->flags & SEC_LOAD) /* only deal with loadable sections */
3427 {
3428 bintotal += s->_raw_size;
3429 final = (s->vma + s->_raw_size);
3430
3431 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, (unsigned int) s->vma,
3432 (unsigned int) (s->vma + s->_raw_size));
3433 gdb_flush (gdb_stdout);
3434
3435 /* Output the starting address */
3436 sprintf (buffer, "/A");
3437 reclen = pmon_makeb64 (s->vma, &buffer[2], 36, &csum);
3438 buffer[2 + reclen] = '\n';
3439 buffer[3 + reclen] = '\0';
3440 reclen += 3; /* for the initial escape code and carriage return */
3441 pmon_download (buffer, reclen);
3442 finished = pmon_check_ack ("/A");
3443
3444 if (!finished)
3445 {
3446 unsigned int binamount;
3447 unsigned int zerofill = 0;
3448 char *bp = buffer;
3449 unsigned int i;
3450
3451 reclen = 0;
3452
3453 for (i = 0; ((i < s->_raw_size) && !finished); i += binamount)
3454 {
3455 int binptr = 0;
3456
3457 binamount = min (BINCHUNK, s->_raw_size - i);
3458
3459 bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3460
3461 /* This keeps a rolling checksum, until we decide to output
3462 the line: */
3463 for (; ((binamount - binptr) > 0);)
3464 {
3465 pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3466 if (reclen >= (MAXRECSIZE - CHECKSIZE))
3467 {
3468 reclen = pmon_checkset (reclen, &bp, &csum);
3469 pmon_download (buffer, reclen);
3470 finished = pmon_check_ack ("data record");
3471 if (finished)
3472 {
3473 zerofill = 0; /* do not transmit pending zerofills */
3474 break;
3475 }
3476
3477 if (hashmark)
3478 {
3479 putchar_unfiltered ('#');
3480 gdb_flush (gdb_stdout);
3481 }
3482
3483 bp = buffer;
3484 reclen = 0; /* buffer processed */
3485 }
3486 }
3487 }
3488
3489 /* Ensure no out-standing zerofill requests: */
3490 if (zerofill != 0)
3491 reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3492
3493 /* and then flush the line: */
3494 if (reclen > 0)
3495 {
3496 reclen = pmon_checkset (reclen, &bp, &csum);
3497 /* Currently pmon_checkset outputs the line terminator by
3498 default, so we write out the buffer so far: */
3499 pmon_download (buffer, reclen);
3500 finished = pmon_check_ack ("record remnant");
3501 }
3502 }
3503
3504 putchar_unfiltered ('\n');
3505 }
3506
3507 /* Terminate the transfer. We know that we have an empty output
3508 buffer at this point. */
3509 sprintf (buffer, "/E/E\n"); /* include dummy padding characters */
3510 reclen = strlen (buffer);
3511 pmon_download (buffer, reclen);
3512
3513 if (finished)
3514 { /* Ignore the termination message: */
3515 SERIAL_FLUSH_INPUT (udp_in_use ? udp_desc : mips_desc);
3516 }
3517 else
3518 { /* Deal with termination message: */
3519 pmon_end_download (final, bintotal);
3520 }
3521
3522 return;
3523 }
3524
3525 /* mips_load -- download a file. */
3526
3527 static void
3528 mips_load (file, from_tty)
3529 char *file;
3530 int from_tty;
3531 {
3532 /* Get the board out of remote debugging mode. */
3533 if (mips_exit_debug ())
3534 error ("mips_load: Couldn't get into monitor mode.");
3535
3536 if (mips_monitor != MON_IDT)
3537 pmon_load_fast (file);
3538 else
3539 mips_load_srec (file);
3540
3541 mips_initialize ();
3542
3543 /* Finally, make the PC point at the start address */
3544 if (mips_monitor != MON_IDT)
3545 {
3546 /* Work around problem where PMON monitor updates the PC after a load
3547 to a different value than GDB thinks it has. The following ensures
3548 that the write_pc() WILL update the PC value: */
3549 register_valid[PC_REGNUM] = 0;
3550 }
3551 if (exec_bfd)
3552 write_pc (bfd_get_start_address (exec_bfd));
3553
3554 inferior_pid = 0; /* No process now */
3555
3556 /* This is necessary because many things were based on the PC at the time that
3557 we attached to the monitor, which is no longer valid now that we have loaded
3558 new code (and just changed the PC). Another way to do this might be to call
3559 normal_stop, except that the stack may not be valid, and things would get
3560 horribly confused... */
3561
3562 clear_symtab_users ();
3563 }
3564
3565
3566 /* Pass the command argument as a packet to PMON verbatim. */
3567
3568 static void
3569 pmon_command (args, from_tty)
3570 char *args;
3571 int from_tty;
3572 {
3573 char buf[DATA_MAXLEN + 1];
3574 int rlen;
3575
3576 sprintf (buf, "0x0 %s", args);
3577 mips_send_packet (buf, 1);
3578 printf_filtered ("Send packet: %s\n", buf);
3579
3580 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
3581 buf[rlen] = '\0';
3582 printf_filtered ("Received packet: %s\n", buf);
3583 }
3584 \f
3585 void
3586 _initialize_remote_mips ()
3587 {
3588 /* Initialize the fields in mips_ops that are common to all four targets. */
3589 mips_ops.to_longname = "Remote MIPS debugging over serial line";
3590 mips_ops.to_close = mips_close;
3591 mips_ops.to_detach = mips_detach;
3592 mips_ops.to_resume = mips_resume;
3593 mips_ops.to_fetch_registers = mips_fetch_registers;
3594 mips_ops.to_store_registers = mips_store_registers;
3595 mips_ops.to_prepare_to_store = mips_prepare_to_store;
3596 mips_ops.to_xfer_memory = mips_xfer_memory;
3597 mips_ops.to_files_info = mips_files_info;
3598 mips_ops.to_insert_breakpoint = mips_insert_breakpoint;
3599 mips_ops.to_remove_breakpoint = mips_remove_breakpoint;
3600 mips_ops.to_kill = mips_kill;
3601 mips_ops.to_load = mips_load;
3602 mips_ops.to_create_inferior = mips_create_inferior;
3603 mips_ops.to_mourn_inferior = mips_mourn_inferior;
3604 mips_ops.to_stratum = process_stratum;
3605 mips_ops.to_has_all_memory = 1;
3606 mips_ops.to_has_memory = 1;
3607 mips_ops.to_has_stack = 1;
3608 mips_ops.to_has_registers = 1;
3609 mips_ops.to_has_execution = 1;
3610 mips_ops.to_magic = OPS_MAGIC;
3611
3612 /* Copy the common fields to all four target vectors. */
3613 pmon_ops = ddb_ops = lsi_ops = mips_ops;
3614
3615 /* Initialize target-specific fields in the target vectors. */
3616 mips_ops.to_shortname = "mips";
3617 mips_ops.to_doc = "\
3618 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
3619 The argument is the device it is connected to or, if it contains a colon,\n\
3620 HOST:PORT to access a board over a network";
3621 mips_ops.to_open = mips_open;
3622 mips_ops.to_wait = mips_wait;
3623
3624 pmon_ops.to_shortname = "pmon";
3625 pmon_ops.to_doc = "\
3626 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3627 line. The argument is the device it is connected to or, if it contains a\n\
3628 colon, HOST:PORT to access a board over a network";
3629 pmon_ops.to_open = pmon_open;
3630 pmon_ops.to_wait = mips_wait;
3631
3632 ddb_ops.to_shortname = "ddb";
3633 ddb_ops.to_doc = "\
3634 Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3635 line. The first argument is the device it is connected to or, if it contains\n\
3636 a colon, HOST:PORT to access a board over a network. The optional second\n\
3637 parameter is the temporary file in the form HOST:FILENAME to be used for\n\
3638 TFTP downloads to the board. The optional third parameter is the local name\n\
3639 of the TFTP temporary file, if it differs from the filename seen by the board.";
3640 ddb_ops.to_open = ddb_open;
3641 ddb_ops.to_wait = mips_wait;
3642
3643 lsi_ops.to_shortname = "lsi";
3644 lsi_ops.to_doc = pmon_ops.to_doc;
3645 lsi_ops.to_open = lsi_open;
3646 lsi_ops.to_wait = mips_wait;
3647
3648 /* Add the targets. */
3649 add_target (&mips_ops);
3650 add_target (&pmon_ops);
3651 add_target (&ddb_ops);
3652 add_target (&lsi_ops);
3653
3654 add_show_from_set (
3655 add_set_cmd ("timeout", no_class, var_zinteger,
3656 (char *) &mips_receive_wait,
3657 "Set timeout in seconds for remote MIPS serial I/O.",
3658 &setlist),
3659 &showlist);
3660
3661 add_show_from_set (
3662 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3663 (char *) &mips_retransmit_wait,
3664 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3665 This is the number of seconds to wait for an acknowledgement to a packet\n\
3666 before resending the packet.", &setlist),
3667 &showlist);
3668
3669 add_show_from_set (
3670 add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3671 (char *) &mips_syn_garbage,
3672 "Set the maximum number of characters to ignore when scanning for a SYN.\n\
3673 This is the maximum number of characters GDB will ignore when trying to\n\
3674 synchronize with the remote system. A value of -1 means that there is no limit\n\
3675 (Note that these characters are printed out even though they are ignored.)",
3676 &setlist),
3677 &showlist);
3678
3679 add_show_from_set
3680 (add_set_cmd ("monitor-prompt", class_obscure, var_string,
3681 (char *) &mips_monitor_prompt,
3682 "Set the prompt that GDB expects from the monitor.",
3683 &setlist),
3684 &showlist);
3685
3686 add_show_from_set (
3687 add_set_cmd ("monitor-warnings", class_obscure, var_zinteger,
3688 (char *) &monitor_warnings,
3689 "Set printing of monitor warnings.\n"
3690 "When enabled, monitor warnings about hardware breakpoints "
3691 "will be displayed.",
3692 &setlist),
3693 &showlist);
3694
3695 add_com ("pmon <command>", class_obscure, pmon_command,
3696 "Send a packet to PMON (must be in debug mode).");
3697
3698 add_show_from_set (add_set_cmd ("mask-address", no_class,
3699 var_boolean, &mask_address_p,
3700 "Set zeroing of upper 32 bits of 64-bit addresses when talking to PMON targets.\n\
3701 Use \"on\" to enable the masking and \"off\" to disable it.\n",
3702 &setlist),
3703 &showlist);
3704 }
This page took 0.102285 seconds and 5 git commands to generate.