forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote-mips.c
3770 lines (3134 loc) · 108 KB
/
remote-mips.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Remote debugging interface for MIPS remote debugging protocol.
Copyright (C) 1993-2015 Free Software Foundation, Inc.
Contributed by Cygnus Support. Written by Ian Lance Taylor
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "inferior.h"
#include "infrun.h"
#include "bfd.h"
#include "symfile.h"
#include "gdbcmd.h"
#include "gdbcore.h"
#include "serial.h"
#include "target.h"
#include <sys/stat.h>
#include "gdb_usleep.h"
#include "regcache.h"
#include <ctype.h>
#include "mips-tdep.h"
#include "gdbthread.h"
#include "gdb_bfd.h"
/* Breakpoint types. Values 0, 1, and 2 must agree with the watch
types passed by breakpoint.c to target_insert_watchpoint.
Value 3 is our own invention, and is used for ordinary instruction
breakpoints. Value 4 is used to mark an unused watchpoint in tables. */
enum break_type
{
BREAK_WRITE, /* 0 */
BREAK_READ, /* 1 */
BREAK_ACCESS, /* 2 */
BREAK_FETCH, /* 3 */
BREAK_UNUSED /* 4 */
};
/* Prototypes for local functions. */
static int mips_readchar (int timeout);
static int mips_receive_header (unsigned char *hdr, int *pgarbage,
int ch, int timeout);
static int mips_receive_trailer (unsigned char *trlr, int *pgarbage,
int *pch, int timeout);
static int mips_cksum (const unsigned char *hdr,
const char *data, int len);
static void mips_send_packet (const char *s, int get_ack);
static void mips_send_command (const char *cmd, int prompt);
static int mips_receive_packet (char *buff, int throw_error, int timeout);
static ULONGEST mips_request (int cmd, ULONGEST addr, ULONGEST data,
int *perr, int timeout, char *buff);
static void mips_initialize (void);
static void mips_close (struct target_ops *self);
static int mips_map_regno (struct gdbarch *, int);
static void mips_set_register (int regno, ULONGEST value);
static void mips_prepare_to_store (struct target_ops *self,
struct regcache *regcache);
static int mips_fetch_word (CORE_ADDR addr, unsigned int *valp);
static int mips_store_word (CORE_ADDR addr, unsigned int value,
int *old_contents);
static enum target_xfer_status mips_xfer_memory (gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST memaddr,
ULONGEST len,
ULONGEST *xfered_len);
static void mips_files_info (struct target_ops *ignore);
static void mips_mourn_inferior (struct target_ops *ops);
static int pmon_makeb64 (unsigned long v, char *p, int n, unsigned int *chksum);
static int pmon_zeroset (int recsize, char **buff, unsigned int *amount,
unsigned int *chksum);
static int pmon_checkset (int recsize, char **buff, unsigned int *value);
static void pmon_make_fastrec (char **outbuf, unsigned char *inbuf,
int *inptr, int inamount, int *recsize,
unsigned int *csum, unsigned int *zerofill);
static int pmon_check_ack (char *mesg);
static void pmon_start_download (void);
static void pmon_end_download (int final, int bintotal);
static void pmon_download (char *buffer, int length);
static void mips_load (struct target_ops *self, const char *file, int from_tty);
static int mips_make_srec (char *buffer, int type, CORE_ADDR memaddr,
unsigned char *myaddr, int len);
static int mips_set_breakpoint (CORE_ADDR addr, int len, enum break_type type);
static int mips_clear_breakpoint (CORE_ADDR addr, int len,
enum break_type type);
static int mips_common_breakpoint (int set, CORE_ADDR addr, int len,
enum break_type type);
/* Forward declarations. */
extern struct target_ops mips_ops;
extern struct target_ops pmon_ops;
extern struct target_ops ddb_ops;
extern struct target_ops rockhopper_ops;
/* *INDENT-OFF* */
/* The MIPS remote debugging interface is built on top of a simple
packet protocol. Each packet is organized as follows:
SYN The first character is always a SYN (ASCII 026, or ^V). SYN
may not appear anywhere else in the packet. Any time a SYN is
seen, a new packet should be assumed to have begun.
TYPE_LEN
This byte contains the upper five bits of the logical length
of the data section, plus a single bit indicating whether this
is a data packet or an acknowledgement. The documentation
indicates that this bit is 1 for a data packet, but the actual
board uses 1 for an acknowledgement. The value of the byte is
0x40 + (ack ? 0x20 : 0) + (len >> 6)
(we always have 0 <= len < 1024). Acknowledgement packets do
not carry data, and must have a data length of 0.
LEN1 This byte contains the lower six bits of the logical length of
the data section. The value is
0x40 + (len & 0x3f)
SEQ This byte contains the six bit sequence number of the packet.
The value is
0x40 + seq
An acknowlegment packet contains the sequence number of the
packet being acknowledged plus 1 modulo 64. Data packets are
transmitted in sequence. There may only be one outstanding
unacknowledged data packet at a time. The sequence numbers
are independent in each direction. If an acknowledgement for
the previous packet is received (i.e., an acknowledgement with
the sequence number of the packet just sent) the packet just
sent should be retransmitted. If no acknowledgement is
received within a timeout period, the packet should be
retransmitted. This has an unfortunate failure condition on a
high-latency line, as a delayed acknowledgement may lead to an
endless series of duplicate packets.
DATA The actual data bytes follow. The following characters are
escaped inline with DLE (ASCII 020, or ^P):
SYN (026) DLE S
DLE (020) DLE D
^C (003) DLE C
^S (023) DLE s
^Q (021) DLE q
The additional DLE characters are not counted in the logical
length stored in the TYPE_LEN and LEN1 bytes.
CSUM1
CSUM2
CSUM3
These bytes contain an 18 bit checksum of the complete
contents of the packet excluding the SEQ byte and the
CSUM[123] bytes. The checksum is simply the twos complement
addition of all the bytes treated as unsigned characters. The
values of the checksum bytes are:
CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
CSUM3: 0x40 + (cksum & 0x3f)
It happens that the MIPS remote debugging protocol always
communicates with ASCII strings. Because of this, this
implementation doesn't bother to handle the DLE quoting mechanism,
since it will never be required. */
/* *INDENT-ON* */
/* The SYN character which starts each packet. */
#define SYN '\026'
/* The 0x40 used to offset each packet (this value ensures that all of
the header and trailer bytes, other than SYN, are printable ASCII
characters). */
#define HDR_OFFSET 0x40
/* The indices of the bytes in the packet header. */
#define HDR_INDX_SYN 0
#define HDR_INDX_TYPE_LEN 1
#define HDR_INDX_LEN1 2
#define HDR_INDX_SEQ 3
#define HDR_LENGTH 4
/* The data/ack bit in the TYPE_LEN header byte. */
#define TYPE_LEN_DA_BIT 0x20
#define TYPE_LEN_DATA 0
#define TYPE_LEN_ACK TYPE_LEN_DA_BIT
/* How to compute the header bytes. */
#define HDR_SET_SYN(data, len, seq) (SYN)
#define HDR_SET_TYPE_LEN(data, len, seq) \
(HDR_OFFSET \
+ ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
+ (((len) >> 6) & 0x1f))
#define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
#define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
/* Check that a header byte is reasonable. */
#define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
/* Get data from the header. These macros evaluate their argument
multiple times. */
#define HDR_IS_DATA(hdr) \
(((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
#define HDR_GET_LEN(hdr) \
((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
#define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
/* The maximum data length. */
#define DATA_MAXLEN 1023
/* The trailer offset. */
#define TRLR_OFFSET HDR_OFFSET
/* The indices of the bytes in the packet trailer. */
#define TRLR_INDX_CSUM1 0
#define TRLR_INDX_CSUM2 1
#define TRLR_INDX_CSUM3 2
#define TRLR_LENGTH 3
/* How to compute the trailer bytes. */
#define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
#define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
#define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
/* Check that a trailer byte is reasonable. */
#define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
/* Get data from the trailer. This evaluates its argument multiple
times. */
#define TRLR_GET_CKSUM(trlr) \
((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
+ (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
+ ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
/* The sequence number modulos. */
#define SEQ_MODULOS (64)
/* PMON commands to load from the serial port or UDP socket. */
#define LOAD_CMD "load -b -s tty0\r"
#define LOAD_CMD_UDP "load -b -s udp\r"
/* The target vectors for the four different remote MIPS targets.
These are initialized with code in _initialize_remote_mips instead
of static initializers, to make it easier to extend the target_ops
vector later. */
struct target_ops mips_ops, pmon_ops, ddb_ops, rockhopper_ops, lsi_ops;
enum mips_monitor_type
{
/* IDT/SIM monitor being used: */
MON_IDT,
/* PMON monitor being used: */
MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET]
Algorithmics Ltd. Nov 9 1995 17:19:50 */
MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET]
Risq Modular Systems,
Thu Jun 6 09:28:40 PDT 1996 */
MON_LSI, /* 4.3.12 [EB,FP],
LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
MON_ROCKHOPPER,
/* Last and unused value, for sizing vectors, etc. */
MON_LAST
};
static enum mips_monitor_type mips_monitor = MON_LAST;
/* The monitor prompt text. If the user sets the PMON prompt
to some new value, the GDB `set monitor-prompt' command must also
be used to inform GDB about the expected prompt. Otherwise, GDB
will not be able to connect to PMON in mips_initialize().
If the `set monitor-prompt' command is not used, the expected
default prompt will be set according the target:
target prompt
----- -----
pmon PMON>
ddb NEC010>
lsi PMON>
*/
static char *mips_monitor_prompt;
/* Set to 1 if the target is open. */
static int mips_is_open;
/* Currently active target description (if mips_is_open == 1). */
static struct target_ops *current_ops;
/* Set to 1 while the connection is being initialized. */
static int mips_initializing;
/* Set to 1 while the connection is being brought down. */
static int mips_exiting;
/* The next sequence number to send. */
static unsigned int mips_send_seq;
/* The next sequence number we expect to receive. */
static unsigned int mips_receive_seq;
/* The time to wait before retransmitting a packet, in seconds. */
static int mips_retransmit_wait = 3;
/* The number of times to try retransmitting a packet before giving up. */
static int mips_send_retries = 10;
/* The number of garbage characters to accept when looking for an
SYN for the next packet. */
static int mips_syn_garbage = 10;
/* The time to wait for a packet, in seconds. */
static int mips_receive_wait = 5;
/* Set if we have sent a packet to the board but have not yet received
a reply. */
static int mips_need_reply = 0;
/* Handle used to access serial I/O stream. */
static struct serial *mips_desc;
/* UDP handle used to download files to target. */
static struct serial *udp_desc;
static int udp_in_use;
/* TFTP filename used to download files to DDB board, in the form
host:filename. */
static char *tftp_name; /* host:filename */
static char *tftp_localname; /* filename portion of above */
static int tftp_in_use;
static FILE *tftp_file;
/* Counts the number of times the user tried to interrupt the target (usually
via ^C. */
static int interrupt_count;
/* If non-zero, means that the target is running. */
static int mips_wait_flag = 0;
/* If non-zero, monitor supports breakpoint commands. */
static int monitor_supports_breakpoints = 0;
/* Data cache header. */
#if 0 /* not used (yet?) */
static DCACHE *mips_dcache;
#endif
/* Non-zero means that we've just hit a read or write watchpoint. */
static int hit_watchpoint;
/* Table of breakpoints/watchpoints (used only on LSI PMON target).
The table is indexed by a breakpoint number, which is an integer
from 0 to 255 returned by the LSI PMON when a breakpoint is set. */
#define MAX_LSI_BREAKPOINTS 256
struct lsi_breakpoint_info
{
enum break_type type; /* type of breakpoint */
CORE_ADDR addr; /* address of breakpoint */
int len; /* length of region being watched */
unsigned long value; /* value to watch */
}
lsi_breakpoints[MAX_LSI_BREAKPOINTS];
/* Error/warning codes returned by LSI PMON for breakpoint commands.
Warning values may be ORed together; error values may not. */
#define W_WARN 0x100 /* This bit is set if the error code
is a warning */
#define W_MSK 0x101 /* warning: Range feature is supported
via mask */
#define W_VAL 0x102 /* warning: Value check is not
supported in hardware */
#define W_QAL 0x104 /* warning: Requested qualifiers are
not supported in hardware */
#define E_ERR 0x200 /* This bit is set if the error code
is an error */
#define E_BPT 0x200 /* error: No such breakpoint number */
#define E_RGE 0x201 /* error: Range is not supported */
#define E_QAL 0x202 /* error: The requested qualifiers can
not be used */
#define E_OUT 0x203 /* error: Out of hardware resources */
#define E_NON 0x204 /* error: Hardware breakpoint not supported */
struct lsi_error
{
int code; /* error code */
char *string; /* string associated with this code */
};
struct lsi_error lsi_warning_table[] =
{
{W_MSK, "Range feature is supported via mask"},
{W_VAL, "Value check is not supported in hardware"},
{W_QAL, "Requested qualifiers are not supported in hardware"},
{0, NULL}
};
struct lsi_error lsi_error_table[] =
{
{E_BPT, "No such breakpoint number"},
{E_RGE, "Range is not supported"},
{E_QAL, "The requested qualifiers can not be used"},
{E_OUT, "Out of hardware resources"},
{E_NON, "Hardware breakpoint not supported"},
{0, NULL}
};
/* Set to 1 with the 'set monitor-warnings' command to enable printing
of warnings returned by PMON when hardware breakpoints are used. */
static int monitor_warnings;
/* This is the ptid we use while we're connected to the remote. Its
value is arbitrary, as the remote-mips target doesn't have a notion of
processes or threads, but we need something non-null to place in
inferior_ptid. */
static ptid_t remote_mips_ptid;
/* Close any ports which might be open. Reset certain globals indicating
the state of those ports. */
static void
close_ports (void)
{
mips_is_open = 0;
serial_close (mips_desc);
if (udp_in_use)
{
serial_close (udp_desc);
udp_in_use = 0;
}
tftp_in_use = 0;
}
/* Handle low-level error that we can't recover from. Note that just
error()ing out from target_wait or some such low-level place will cause
all hell to break loose--the rest of GDB will tend to get left in an
inconsistent state. */
static void ATTRIBUTE_NORETURN
mips_error (char *string,...)
{
va_list args;
char *fmt;
target_terminal_ours ();
wrap_here (""); /* Force out any buffered output. */
gdb_flush (gdb_stdout);
gdb_flush (gdb_stderr);
/* Clean up in such a way that mips_close won't try to talk to the
board (it almost surely won't work since we weren't able to talk to
it). */
close_ports ();
if (!ptid_equal (inferior_ptid, null_ptid))
target_mourn_inferior ();
fmt = concat (_("Ending remote MIPS debugging: "),
string, (char *) NULL);
make_cleanup (xfree, fmt);
va_start (args, string);
throw_verror (TARGET_CLOSE_ERROR, fmt, args);
va_end (args);
}
/* putc_readable - print a character, displaying non-printable chars in
^x notation or in hex. */
static void
fputc_readable (int ch, struct ui_file *file)
{
if (ch == '\n')
fputc_unfiltered ('\n', file);
else if (ch == '\r')
fprintf_unfiltered (file, "\\r");
else if (ch < 0x20) /* ASCII control character */
fprintf_unfiltered (file, "^%c", ch + '@');
else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */
fprintf_unfiltered (file, "[%02x]", ch & 0xff);
else
fputc_unfiltered (ch, file);
}
/* puts_readable - print a string, displaying non-printable chars in
^x notation or in hex. */
static void
fputs_readable (const char *string, struct ui_file *file)
{
int c;
while ((c = *string++) != '\0')
fputc_readable (c, file);
}
/* Read P as a hex value. Return true if every character made sense,
storing the result in *RESULT. Leave *RESULT unchanged otherwise. */
static int
read_hex_value (const char *p, ULONGEST *result)
{
ULONGEST retval;
retval = 0;
while (*p != 0)
{
retval <<= 4;
if (*p >= '0' && *p <= '9')
retval |= *p - '0';
else if (*p >= 'A' && *p <= 'F')
retval |= *p - 'A' + 10;
else if (*p >= 'a' && *p <= 'f')
retval |= *p - 'a' + 10;
else
return 0;
p++;
}
*result = retval;
return 1;
}
/* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
timed out. TIMEOUT specifies timeout value in seconds. */
static int
mips_expect_timeout (const char *string, int timeout)
{
const char *p = string;
if (remote_debug)
{
fprintf_unfiltered (gdb_stdlog, "Expected \"");
fputs_readable (string, gdb_stdlog);
fprintf_unfiltered (gdb_stdlog, "\", got \"");
}
immediate_quit++;
QUIT;
while (1)
{
int c;
/* Must use serial_readchar() here cuz mips_readchar would get
confused if we were waiting for the mips_monitor_prompt... */
c = serial_readchar (mips_desc, timeout);
if (c == SERIAL_TIMEOUT)
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "\": FAIL\n");
return 0;
}
if (remote_debug)
fputc_readable (c, gdb_stdlog);
if (c == *p++)
{
if (*p == '\0')
{
immediate_quit--;
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "\": OK\n");
return 1;
}
}
else
{
p = string;
if (c == *p)
p++;
}
}
}
/* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
timed out. The timeout value is hard-coded to 2 seconds. Use
mips_expect_timeout if a different timeout value is needed. */
static int
mips_expect (const char *string)
{
return mips_expect_timeout (string, remote_timeout);
}
/* Read a character from the remote, aborting on error. Returns
SERIAL_TIMEOUT on timeout (since that's what serial_readchar()
returns). FIXME: If we see the string mips_monitor_prompt from the
board, then we are debugging on the main console port, and we have
somehow dropped out of remote debugging mode. In this case, we
automatically go back in to remote debugging mode. This is a hack,
put in because I can't find any way for a program running on the
remote board to terminate without also ending remote debugging
mode. I assume users won't have any trouble with this; for one
thing, the IDT documentation generally assumes that the remote
debugging port is not the console port. This is, however, very
convenient for DejaGnu when you only have one connected serial
port. */
static int
mips_readchar (int timeout)
{
int ch;
static int state = 0;
int mips_monitor_prompt_len = strlen (mips_monitor_prompt);
{ /* FIXME this whole block is dead code! */
int i;
i = timeout;
if (i == -1 && watchdog > 0)
i = watchdog;
}
if (state == mips_monitor_prompt_len)
timeout = 1;
ch = serial_readchar (mips_desc, timeout);
if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off. */
{
target_mourn_inferior ();
error (_("Watchdog has expired. Target detached."));
}
if (ch == SERIAL_EOF)
mips_error (_("End of file from remote"));
if (ch == SERIAL_ERROR)
mips_error (_("Error reading from remote: %s"), safe_strerror (errno));
if (remote_debug > 1)
{
/* Don't use _filtered; we can't deal with a QUIT out of
target_wait, and I think this might be called from there. */
if (ch != SERIAL_TIMEOUT)
fprintf_unfiltered (gdb_stdlog, "Read '%c' %d 0x%x\n", ch, ch, ch);
else
fprintf_unfiltered (gdb_stdlog, "Timed out in read\n");
}
/* If we have seen mips_monitor_prompt and we either time out, or
we see a @ (which was echoed from a packet we sent), reset the
board as described above. The first character in a packet after
the SYN (which is not echoed) is always an @ unless the packet is
more than 64 characters long, which ours never are. */
if ((ch == SERIAL_TIMEOUT || ch == '@')
&& state == mips_monitor_prompt_len
&& !mips_initializing
&& !mips_exiting)
{
if (remote_debug > 0)
/* Don't use _filtered; we can't deal with a QUIT out of
target_wait, and I think this might be called from there. */
fprintf_unfiltered (gdb_stdlog,
"Reinitializing MIPS debugging mode\n");
mips_need_reply = 0;
mips_initialize ();
state = 0;
/* At this point, about the only thing we can do is abort the command
in progress and get back to command level as quickly as possible. */
error (_("Remote board reset, debug protocol re-initialized."));
}
if (ch == mips_monitor_prompt[state])
++state;
else
state = 0;
return ch;
}
/* Get a packet header, putting the data in the supplied buffer.
PGARBAGE is a pointer to the number of garbage characters received
so far. CH is the last character received. Returns 0 for success,
or -1 for timeout. */
static int
mips_receive_header (unsigned char *hdr, int *pgarbage, int ch, int timeout)
{
int i;
while (1)
{
/* Wait for a SYN. mips_syn_garbage is intended to prevent
sitting here indefinitely if the board sends us one garbage
character per second. ch may already have a value from the
last time through the loop. */
while (ch != SYN)
{
ch = mips_readchar (timeout);
if (ch == SERIAL_TIMEOUT)
return -1;
if (ch != SYN)
{
/* Printing the character here lets the user of gdb see
what the program is outputting, if the debugging is
being done on the console port. Don't use _filtered:
we can't deal with a QUIT out of target_wait and
buffered target output confuses the user. */
if (!mips_initializing || remote_debug > 0)
{
if (isprint (ch) || isspace (ch))
{
fputc_unfiltered (ch, gdb_stdtarg);
}
else
{
fputc_readable (ch, gdb_stdtarg);
}
gdb_flush (gdb_stdtarg);
}
/* Only count unprintable characters. */
if (! (isprint (ch) || isspace (ch)))
(*pgarbage) += 1;
if (mips_syn_garbage > 0
&& *pgarbage > mips_syn_garbage)
mips_error (_("Debug protocol failure: more "
"than %d characters before a sync."),
mips_syn_garbage);
}
}
/* Get the packet header following the SYN. */
for (i = 1; i < HDR_LENGTH; i++)
{
ch = mips_readchar (timeout);
if (ch == SERIAL_TIMEOUT)
return -1;
/* Make sure this is a header byte. */
if (ch == SYN || !HDR_CHECK (ch))
break;
hdr[i] = ch;
}
/* If we got the complete header, we can return. Otherwise we
loop around and keep looking for SYN. */
if (i >= HDR_LENGTH)
return 0;
}
}
/* Get a packet header, putting the data in the supplied buffer.
PGARBAGE is a pointer to the number of garbage characters received
so far. The last character read is returned in *PCH. Returns 0
for success, -1 for timeout, -2 for error. */
static int
mips_receive_trailer (unsigned char *trlr, int *pgarbage,
int *pch, int timeout)
{
int i;
int ch;
for (i = 0; i < TRLR_LENGTH; i++)
{
ch = mips_readchar (timeout);
*pch = ch;
if (ch == SERIAL_TIMEOUT)
return -1;
if (!TRLR_CHECK (ch))
return -2;
trlr[i] = ch;
}
return 0;
}
/* Get the checksum of a packet. HDR points to the packet header.
DATASTR points to the packet data. LEN is the length of DATASTR. */
static int
mips_cksum (const unsigned char *hdr, const char *datastr, int len)
{
const unsigned char *p;
const unsigned char *data = (const unsigned char *) datastr;
int c;
int cksum;
cksum = 0;
/* The initial SYN is not included in the checksum. */
c = HDR_LENGTH - 1;
p = hdr + 1;
while (c-- != 0)
cksum += *p++;
c = len;
p = data;
while (c-- != 0)
cksum += *p++;
return cksum;
}
/* Send a packet containing the given ASCII string. */
static void
mips_send_packet (const char *s, int get_ack)
{
/* unsigned */ int len;
unsigned char *packet;
int cksum;
int attempt;
len = strlen (s);
if (len > DATA_MAXLEN)
mips_error (_("MIPS protocol data packet too long: %s"), s);
packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
memcpy (packet + HDR_LENGTH, s, len);
cksum = mips_cksum (packet, (char *) packet + HDR_LENGTH, len);
packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
/* Increment the sequence number. This will set mips_send_seq to
the sequence number we expect in the acknowledgement. */
mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
/* We can only have one outstanding data packet, so we just wait for
the acknowledgement here. Keep retransmitting the packet until
we get one, or until we've tried too many times. */
for (attempt = 0; attempt < mips_send_retries; attempt++)
{
int garbage;
int ch;
if (remote_debug > 0)
{
/* Don't use _filtered; we can't deal with a QUIT out of
target_wait, and I think this might be called from there. */
packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
fprintf_unfiltered (gdb_stdlog, "Writing \"%s\"\n", packet + 1);
}
if (serial_write (mips_desc, packet,
HDR_LENGTH + len + TRLR_LENGTH) != 0)
mips_error (_("write to target failed: %s"), safe_strerror (errno));
if (!get_ack)
return;
garbage = 0;
ch = 0;
while (1)
{
unsigned char hdr[HDR_LENGTH + 1];
unsigned char trlr[TRLR_LENGTH + 1];
int err;
unsigned int seq;
/* Get the packet header. If we time out, resend the data
packet. */
err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
if (err != 0)
break;
ch = 0;
/* If we get a data packet, assume it is a duplicate and
ignore it. FIXME: If the acknowledgement is lost, this
data packet may be the packet the remote sends after the
acknowledgement. */
if (HDR_IS_DATA (hdr))
{
int i;
/* Ignore any errors raised whilst attempting to ignore
packet. */
len = HDR_GET_LEN (hdr);
for (i = 0; i < len; i++)
{
int rch;
rch = mips_readchar (remote_timeout);
if (rch == SYN)
{
ch = SYN;
break;
}
if (rch == SERIAL_TIMEOUT)
break;
/* Ignore the character. */
}
if (i == len)
(void) mips_receive_trailer (trlr, &garbage, &ch,
remote_timeout);
/* We don't bother checking the checksum, or providing an
ACK to the packet. */
continue;
}
/* If the length is not 0, this is a garbled packet. */
if (HDR_GET_LEN (hdr) != 0)
continue;
/* Get the packet trailer. */
err = mips_receive_trailer (trlr, &garbage, &ch,
mips_retransmit_wait);
/* If we timed out, resend the data packet. */
if (err == -1)
break;
/* If we got a bad character, reread the header. */
if (err != 0)
continue;
/* If the checksum does not match the trailer checksum, this
is a bad packet; ignore it. */
if (mips_cksum (hdr, NULL, 0) != TRLR_GET_CKSUM (trlr))
continue;
if (remote_debug > 0)
{
hdr[HDR_LENGTH] = '\0';
trlr[TRLR_LENGTH] = '\0';
/* Don't use _filtered; we can't deal with a QUIT out of
target_wait, and I think this might be called from there. */
fprintf_unfiltered (gdb_stdlog, "Got ack %d \"%s%s\"\n",
HDR_GET_SEQ (hdr), hdr + 1, trlr);
}
/* If this ack is for the current packet, we're done. */
seq = HDR_GET_SEQ (hdr);
if (seq == mips_send_seq)
return;
/* If this ack is for the last packet, resend the current
packet. */
if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
break;
/* Otherwise this is a bad ack; ignore it. Increment the
garbage count to ensure that we do not stay in this loop
forever. */
++garbage;
}
}
mips_error (_("Remote did not acknowledge packet"));
}