xref: /OK3568_Linux_fs/u-boot/tools/gdb/remote.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * taken from gdb/remote.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * I am only interested in the write to memory stuff - everything else
5*4882a593Smuzhiyun  * has been ripped out
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * all the copyright notices etc have been left in
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* enough so that it will compile */
11*4882a593Smuzhiyun #include <stdio.h>
12*4882a593Smuzhiyun #include <stdlib.h>
13*4882a593Smuzhiyun #include <string.h>
14*4882a593Smuzhiyun #include <errno.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /*nicked from gcc..*/
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #ifndef alloca
19*4882a593Smuzhiyun #ifdef __GNUC__
20*4882a593Smuzhiyun #define alloca __builtin_alloca
21*4882a593Smuzhiyun #else /* not GNU C.  */
22*4882a593Smuzhiyun #if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
23*4882a593Smuzhiyun #include <alloca.h>
24*4882a593Smuzhiyun #else /* not sparc */
25*4882a593Smuzhiyun #if defined (MSDOS) && !defined (__TURBOC__)
26*4882a593Smuzhiyun #include <malloc.h>
27*4882a593Smuzhiyun #else /* not MSDOS, or __TURBOC__ */
28*4882a593Smuzhiyun #if defined(_AIX)
29*4882a593Smuzhiyun #include <malloc.h>
30*4882a593Smuzhiyun  #pragma alloca
31*4882a593Smuzhiyun #else /* not MSDOS, __TURBOC__, or _AIX */
32*4882a593Smuzhiyun #ifdef __hpux
33*4882a593Smuzhiyun #endif /* __hpux */
34*4882a593Smuzhiyun #endif /* not _AIX */
35*4882a593Smuzhiyun #endif /* not MSDOS, or __TURBOC__ */
36*4882a593Smuzhiyun #endif /* not sparc.  */
37*4882a593Smuzhiyun #endif /* not GNU C.  */
38*4882a593Smuzhiyun #ifdef __cplusplus
39*4882a593Smuzhiyun extern "C" {
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun     void* alloca(size_t);
42*4882a593Smuzhiyun #ifdef __cplusplus
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun #endif
45*4882a593Smuzhiyun #endif /* alloca not defined.  */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include "serial.h"
49*4882a593Smuzhiyun #include "error.h"
50*4882a593Smuzhiyun #include "remote.h"
51*4882a593Smuzhiyun #define REGISTER_BYTES 0
52*4882a593Smuzhiyun #define fprintf_unfiltered fprintf
53*4882a593Smuzhiyun #define fprintf_filtered fprintf
54*4882a593Smuzhiyun #define fputs_unfiltered fputs
55*4882a593Smuzhiyun #define fputs_filtered fputs
56*4882a593Smuzhiyun #define fputc_unfiltered fputc
57*4882a593Smuzhiyun #define fputc_filtered fputc
58*4882a593Smuzhiyun #define printf_unfiltered printf
59*4882a593Smuzhiyun #define printf_filtered printf
60*4882a593Smuzhiyun #define puts_unfiltered puts
61*4882a593Smuzhiyun #define puts_filtered puts
62*4882a593Smuzhiyun #define putchar_unfiltered putchar
63*4882a593Smuzhiyun #define putchar_filtered putchar
64*4882a593Smuzhiyun #define fputstr_unfiltered(a,b,c) fputs((a), (c))
65*4882a593Smuzhiyun #define gdb_stdlog stderr
66*4882a593Smuzhiyun #define SERIAL_READCHAR(fd,timo)	serialreadchar((fd), (timo))
67*4882a593Smuzhiyun #define SERIAL_WRITE(fd, addr, len)	serialwrite((fd), (addr), (len))
68*4882a593Smuzhiyun #define error Error
69*4882a593Smuzhiyun #define perror_with_name Perror
70*4882a593Smuzhiyun #define gdb_flush fflush
71*4882a593Smuzhiyun #define max(a,b) (((a)>(b))?(a):(b))
72*4882a593Smuzhiyun #define min(a,b) (((a)<(b))?(a):(b))
73*4882a593Smuzhiyun #define target_mourn_inferior() {}
74*4882a593Smuzhiyun #define ULONGEST unsigned long
75*4882a593Smuzhiyun #define CORE_ADDR unsigned long
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun static int putpkt (char *);
78*4882a593Smuzhiyun static int putpkt_binary(char *, int);
79*4882a593Smuzhiyun static void getpkt (char *, int);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun static int remote_debug = 0, remote_register_buf_size = 0, watchdog = 0;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun int remote_desc = -1, remote_timeout = 10;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static void
fputstrn_unfiltered(char * s,int n,int x,FILE * fp)86*4882a593Smuzhiyun fputstrn_unfiltered(char *s, int n, int x, FILE *fp)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun     while (n-- > 0)
89*4882a593Smuzhiyun 	fputc(*s++, fp);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun void
remote_reset(void)93*4882a593Smuzhiyun remote_reset(void)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun     SERIAL_WRITE(remote_desc, "+", 1);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun void
remote_continue(void)99*4882a593Smuzhiyun remote_continue(void)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun     putpkt("c");
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* Remote target communications for serial-line targets in custom GDB protocol
105*4882a593Smuzhiyun    Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 98, 1999
106*4882a593Smuzhiyun    Free Software Foundation, Inc.
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun    This file is part of GDB.
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun /* *INDENT-OFF* */
113*4882a593Smuzhiyun /* Remote communication protocol.
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun    A debug packet whose contents are <data>
116*4882a593Smuzhiyun    is encapsulated for transmission in the form:
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	$ <data> # CSUM1 CSUM2
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	<data> must be ASCII alphanumeric and cannot include characters
121*4882a593Smuzhiyun 	'$' or '#'.  If <data> starts with two characters followed by
122*4882a593Smuzhiyun 	':', then the existing stubs interpret this as a sequence number.
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	CSUM1 and CSUM2 are ascii hex representation of an 8-bit
125*4882a593Smuzhiyun 	checksum of <data>, the most significant nibble is sent first.
126*4882a593Smuzhiyun 	the hex digits 0-9,a-f are used.
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun    Receiver responds with:
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	+	- if CSUM is correct and ready for next packet
131*4882a593Smuzhiyun 	-	- if CSUM is incorrect
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun    <data> is as follows:
134*4882a593Smuzhiyun    Most values are encoded in ascii hex digits.  Signal numbers are according
135*4882a593Smuzhiyun    to the numbering in target.h.
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	Request		Packet
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	set thread	Hct...		Set thread for subsequent operations.
140*4882a593Smuzhiyun 					c = 'c' for thread used in step and
141*4882a593Smuzhiyun 					continue; t... can be -1 for all
142*4882a593Smuzhiyun 					threads.
143*4882a593Smuzhiyun 					c = 'g' for thread used in other
144*4882a593Smuzhiyun 					operations.  If zero, pick a thread,
145*4882a593Smuzhiyun 					any thread.
146*4882a593Smuzhiyun 	reply		OK		for success
147*4882a593Smuzhiyun 			ENN		for an error.
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	read registers  g
150*4882a593Smuzhiyun 	reply		XX....X		Each byte of register data
151*4882a593Smuzhiyun 					is described by two hex digits.
152*4882a593Smuzhiyun 					Registers are in the internal order
153*4882a593Smuzhiyun 					for GDB, and the bytes in a register
154*4882a593Smuzhiyun 					are in the same order the machine uses.
155*4882a593Smuzhiyun 			or ENN		for an error.
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	write regs	GXX..XX		Each byte of register data
158*4882a593Smuzhiyun 					is described by two hex digits.
159*4882a593Smuzhiyun 	reply		OK		for success
160*4882a593Smuzhiyun 			ENN		for an error
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	write reg	Pn...=r...	Write register n... with value r...,
163*4882a593Smuzhiyun 					which contains two hex digits for each
164*4882a593Smuzhiyun 					byte in the register (target byte
165*4882a593Smuzhiyun 					order).
166*4882a593Smuzhiyun 	reply		OK		for success
167*4882a593Smuzhiyun 			ENN		for an error
168*4882a593Smuzhiyun 	(not supported by all stubs).
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	read mem	mAA..AA,LLLL	AA..AA is address, LLLL is length.
171*4882a593Smuzhiyun 	reply		XX..XX		XX..XX is mem contents
172*4882a593Smuzhiyun 					Can be fewer bytes than requested
173*4882a593Smuzhiyun 					if able to read only part of the data.
174*4882a593Smuzhiyun 			or ENN		NN is errno
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	write mem	MAA..AA,LLLL:XX..XX
177*4882a593Smuzhiyun 					AA..AA is address,
178*4882a593Smuzhiyun 					LLLL is number of bytes,
179*4882a593Smuzhiyun 					XX..XX is data
180*4882a593Smuzhiyun 	reply		OK		for success
181*4882a593Smuzhiyun 			ENN		for an error (this includes the case
182*4882a593Smuzhiyun 					where only part of the data was
183*4882a593Smuzhiyun 					written).
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	write mem       XAA..AA,LLLL:XX..XX
186*4882a593Smuzhiyun 	 (binary)                       AA..AA is address,
187*4882a593Smuzhiyun 					LLLL is number of bytes,
188*4882a593Smuzhiyun 					XX..XX is binary data
189*4882a593Smuzhiyun 	reply           OK              for success
190*4882a593Smuzhiyun 			ENN             for an error
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	continue	cAA..AA		AA..AA is address to resume
193*4882a593Smuzhiyun 					If AA..AA is omitted,
194*4882a593Smuzhiyun 					resume at same address.
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	step		sAA..AA		AA..AA is address to resume
197*4882a593Smuzhiyun 					If AA..AA is omitted,
198*4882a593Smuzhiyun 					resume at same address.
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	continue with	Csig;AA..AA	Continue with signal sig (hex signal
201*4882a593Smuzhiyun 	signal				number).  If ;AA..AA is omitted,
202*4882a593Smuzhiyun 					resume at same address.
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	step with	Ssig;AA..AA	Like 'C' but step not continue.
205*4882a593Smuzhiyun 	signal
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	last signal     ?               Reply the current reason for stopping.
208*4882a593Smuzhiyun 					This is the same reply as is generated
209*4882a593Smuzhiyun 					for step or cont : SAA where AA is the
210*4882a593Smuzhiyun 					signal number.
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	detach          D               Reply OK.
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	There is no immediate reply to step or cont.
215*4882a593Smuzhiyun 	The reply comes when the machine stops.
216*4882a593Smuzhiyun 	It is		SAA		AA is the signal number.
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	or...		TAAn...:r...;n...:r...;n...:r...;
219*4882a593Smuzhiyun 					AA = signal number
220*4882a593Smuzhiyun 					n... = register number (hex)
221*4882a593Smuzhiyun 					  r... = register contents
222*4882a593Smuzhiyun 					n... = `thread'
223*4882a593Smuzhiyun 					  r... = thread process ID.  This is
224*4882a593Smuzhiyun 						 a hex integer.
225*4882a593Smuzhiyun 					n... = other string not starting
226*4882a593Smuzhiyun 					    with valid hex digit.
227*4882a593Smuzhiyun 					  gdb should ignore this n,r pair
228*4882a593Smuzhiyun 					  and go on to the next.  This way
229*4882a593Smuzhiyun 					  we can extend the protocol.
230*4882a593Smuzhiyun 	or...		WAA		The process exited, and AA is
231*4882a593Smuzhiyun 					the exit status.  This is only
232*4882a593Smuzhiyun 					applicable for certains sorts of
233*4882a593Smuzhiyun 					targets.
234*4882a593Smuzhiyun 	or...		XAA		The process terminated with signal
235*4882a593Smuzhiyun 					AA.
236*4882a593Smuzhiyun 	or (obsolete)	NAA;tttttttt;dddddddd;bbbbbbbb
237*4882a593Smuzhiyun 					AA = signal number
238*4882a593Smuzhiyun 					tttttttt = address of symbol "_start"
239*4882a593Smuzhiyun 					dddddddd = base of data section
240*4882a593Smuzhiyun 					bbbbbbbb = base of bss  section.
241*4882a593Smuzhiyun 					Note: only used by Cisco Systems
242*4882a593Smuzhiyun 					targets.  The difference between this
243*4882a593Smuzhiyun 					reply and the "qOffsets" query is that
244*4882a593Smuzhiyun 					the 'N' packet may arrive spontaneously
245*4882a593Smuzhiyun 					whereas the 'qOffsets' is a query
246*4882a593Smuzhiyun 					initiated by the host debugger.
247*4882a593Smuzhiyun 	or...           OXX..XX	XX..XX  is hex encoding of ASCII data. This
248*4882a593Smuzhiyun 					can happen at any time while the
249*4882a593Smuzhiyun 					program is running and the debugger
250*4882a593Smuzhiyun 					should continue to wait for
251*4882a593Smuzhiyun 					'W', 'T', etc.
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	thread alive	TXX		Find out if the thread XX is alive.
254*4882a593Smuzhiyun 	reply		OK		thread is still alive
255*4882a593Smuzhiyun 			ENN		thread is dead
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	remote restart	RXX		Restart the remote server
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	extended ops	!		Use the extended remote protocol.
260*4882a593Smuzhiyun 					Sticky -- only needs to be set once.
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	kill request	k
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	toggle debug	d		toggle debug flag (see 386 & 68k stubs)
265*4882a593Smuzhiyun 	reset		r		reset -- see sparc stub.
266*4882a593Smuzhiyun 	reserved	<other>		On other requests, the stub should
267*4882a593Smuzhiyun 					ignore the request and send an empty
268*4882a593Smuzhiyun 					response ($#<checksum>).  This way
269*4882a593Smuzhiyun 					we can extend the protocol and GDB
270*4882a593Smuzhiyun 					can tell whether the stub it is
271*4882a593Smuzhiyun 					talking to uses the old or the new.
272*4882a593Smuzhiyun 	search		tAA:PP,MM	Search backwards starting at address
273*4882a593Smuzhiyun 					AA for a match with pattern PP and
274*4882a593Smuzhiyun 					mask MM.  PP and MM are 4 bytes.
275*4882a593Smuzhiyun 					Not supported by all stubs.
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	general query	qXXXX		Request info about XXXX.
278*4882a593Smuzhiyun 	general set	QXXXX=yyyy	Set value of XXXX to yyyy.
279*4882a593Smuzhiyun 	query sect offs	qOffsets	Get section offsets.  Reply is
280*4882a593Smuzhiyun 					Text=xxx;Data=yyy;Bss=zzz
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	Responses can be run-length encoded to save space.  A '*' means that
283*4882a593Smuzhiyun 	the next character is an ASCII encoding giving a repeat count which
284*4882a593Smuzhiyun 	stands for that many repititions of the character preceding the '*'.
285*4882a593Smuzhiyun 	The encoding is n+29, yielding a printable character where n >=3
286*4882a593Smuzhiyun 	(which is where rle starts to win).  Don't use an n > 126.
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	So
289*4882a593Smuzhiyun 	"0* " means the same as "0000".  */
290*4882a593Smuzhiyun /* *INDENT-ON* */
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /* This variable (available to the user via "set remotebinarydownload")
293*4882a593Smuzhiyun    dictates whether downloads are sent in binary (via the 'X' packet).
294*4882a593Smuzhiyun    We assume that the stub can, and attempt to do it. This will be cleared if
295*4882a593Smuzhiyun    the stub does not understand it. This switch is still needed, though
296*4882a593Smuzhiyun    in cases when the packet is supported in the stub, but the connection
297*4882a593Smuzhiyun    does not allow it (i.e., 7-bit serial connection only). */
298*4882a593Smuzhiyun static int remote_binary_download = 1;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /* Have we already checked whether binary downloads work? */
301*4882a593Smuzhiyun static int remote_binary_checked;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /* Maximum number of bytes to read/write at once.  The value here
304*4882a593Smuzhiyun    is chosen to fill up a packet (the headers account for the 32).  */
305*4882a593Smuzhiyun #define MAXBUFBYTES(N) (((N)-32)/2)
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
308*4882a593Smuzhiyun    and i386-stub.c.  Normally, no one would notice because it only matters
309*4882a593Smuzhiyun    for writing large chunks of memory (e.g. in downloads).  Also, this needs
310*4882a593Smuzhiyun    to be more than 400 if required to hold the registers (see below, where
311*4882a593Smuzhiyun    we round it up based on REGISTER_BYTES).  */
312*4882a593Smuzhiyun /* Round up PBUFSIZ to hold all the registers, at least.  */
313*4882a593Smuzhiyun #define	PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
314*4882a593Smuzhiyun 		 ? (REGISTER_BYTES * 2 + 32) \
315*4882a593Smuzhiyun 		 : 400)
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /* This variable sets the number of bytes to be written to the target
319*4882a593Smuzhiyun    in a single packet.  Normally PBUFSIZ is satisfactory, but some
320*4882a593Smuzhiyun    targets need smaller values (perhaps because the receiving end
321*4882a593Smuzhiyun    is slow).  */
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun static int remote_write_size = 0x7fffffff;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /* This variable sets the number of bits in an address that are to be
326*4882a593Smuzhiyun    sent in a memory ("M" or "m") packet.  Normally, after stripping
327*4882a593Smuzhiyun    leading zeros, the entire address would be sent. This variable
328*4882a593Smuzhiyun    restricts the address to REMOTE_ADDRESS_SIZE bits.  HISTORY: The
329*4882a593Smuzhiyun    initial implementation of remote.c restricted the address sent in
330*4882a593Smuzhiyun    memory packets to ``host::sizeof long'' bytes - (typically 32
331*4882a593Smuzhiyun    bits).  Consequently, for 64 bit targets, the upper 32 bits of an
332*4882a593Smuzhiyun    address was never sent.  Since fixing this bug may cause a break in
333*4882a593Smuzhiyun    some remote targets this variable is principly provided to
334*4882a593Smuzhiyun    facilitate backward compatibility. */
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun static int remote_address_size;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /* Convert hex digit A to a number.  */
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun static int
fromhex(int a)341*4882a593Smuzhiyun fromhex (int a)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun   if (a >= '0' && a <= '9')
344*4882a593Smuzhiyun     return a - '0';
345*4882a593Smuzhiyun   else if (a >= 'a' && a <= 'f')
346*4882a593Smuzhiyun     return a - 'a' + 10;
347*4882a593Smuzhiyun   else if (a >= 'A' && a <= 'F')
348*4882a593Smuzhiyun     return a - 'A' + 10;
349*4882a593Smuzhiyun   else {
350*4882a593Smuzhiyun     error ("Reply contains invalid hex digit %d", a);
351*4882a593Smuzhiyun     return -1;
352*4882a593Smuzhiyun   }
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun /* Convert number NIB to a hex digit.  */
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun static int
tohex(int nib)358*4882a593Smuzhiyun tohex (int nib)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun   if (nib < 10)
361*4882a593Smuzhiyun     return '0' + nib;
362*4882a593Smuzhiyun   else
363*4882a593Smuzhiyun     return 'a' + nib - 10;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /* Return the number of hex digits in num.  */
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun static int
hexnumlen(ULONGEST num)369*4882a593Smuzhiyun hexnumlen (ULONGEST num)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun   int i;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun   for (i = 0; num != 0; i++)
374*4882a593Smuzhiyun     num >>= 4;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun   return max (i, 1);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun /* Set BUF to the hex digits representing NUM.  */
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun static int
hexnumstr(char * buf,ULONGEST num)382*4882a593Smuzhiyun hexnumstr (char *buf, ULONGEST num)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun   int i;
385*4882a593Smuzhiyun   int len = hexnumlen (num);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun   buf[len] = '\0';
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun   for (i = len - 1; i >= 0; i--)
390*4882a593Smuzhiyun     {
391*4882a593Smuzhiyun       buf[i] = "0123456789abcdef"[(num & 0xf)];
392*4882a593Smuzhiyun       num >>= 4;
393*4882a593Smuzhiyun     }
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun   return len;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun static CORE_ADDR
remote_address_masked(CORE_ADDR addr)401*4882a593Smuzhiyun remote_address_masked (CORE_ADDR addr)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun   if (remote_address_size > 0
404*4882a593Smuzhiyun       && remote_address_size < (sizeof (ULONGEST) * 8))
405*4882a593Smuzhiyun     {
406*4882a593Smuzhiyun       /* Only create a mask when that mask can safely be constructed
407*4882a593Smuzhiyun 	 in a ULONGEST variable. */
408*4882a593Smuzhiyun       ULONGEST mask = 1;
409*4882a593Smuzhiyun       mask = (mask << remote_address_size) - 1;
410*4882a593Smuzhiyun       addr &= mask;
411*4882a593Smuzhiyun     }
412*4882a593Smuzhiyun   return addr;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun /* Determine whether the remote target supports binary downloading.
416*4882a593Smuzhiyun    This is accomplished by sending a no-op memory write of zero length
417*4882a593Smuzhiyun    to the target at the specified address. It does not suffice to send
418*4882a593Smuzhiyun    the whole packet, since many stubs strip the eighth bit and subsequently
419*4882a593Smuzhiyun    compute a wrong checksum, which causes real havoc with remote_write_bytes.
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun    NOTE: This can still lose if the serial line is not eight-bit clean. In
422*4882a593Smuzhiyun    cases like this, the user should clear "remotebinarydownload". */
423*4882a593Smuzhiyun static void
check_binary_download(CORE_ADDR addr)424*4882a593Smuzhiyun check_binary_download (CORE_ADDR addr)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun   if (remote_binary_download && !remote_binary_checked)
427*4882a593Smuzhiyun     {
428*4882a593Smuzhiyun       char *buf = alloca (PBUFSIZ);
429*4882a593Smuzhiyun       char *p;
430*4882a593Smuzhiyun       remote_binary_checked = 1;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun       p = buf;
433*4882a593Smuzhiyun       *p++ = 'X';
434*4882a593Smuzhiyun       p += hexnumstr (p, (ULONGEST) addr);
435*4882a593Smuzhiyun       *p++ = ',';
436*4882a593Smuzhiyun       p += hexnumstr (p, (ULONGEST) 0);
437*4882a593Smuzhiyun       *p++ = ':';
438*4882a593Smuzhiyun       *p = '\0';
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun       putpkt_binary (buf, (int) (p - buf));
441*4882a593Smuzhiyun       getpkt (buf, 0);
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun       if (buf[0] == '\0')
444*4882a593Smuzhiyun 	remote_binary_download = 0;
445*4882a593Smuzhiyun     }
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun   if (remote_debug)
448*4882a593Smuzhiyun     {
449*4882a593Smuzhiyun       if (remote_binary_download)
450*4882a593Smuzhiyun 	fprintf_unfiltered (gdb_stdlog,
451*4882a593Smuzhiyun 			    "binary downloading suppported by target\n");
452*4882a593Smuzhiyun       else
453*4882a593Smuzhiyun 	fprintf_unfiltered (gdb_stdlog,
454*4882a593Smuzhiyun 			    "binary downloading NOT suppported by target\n");
455*4882a593Smuzhiyun     }
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /* Write memory data directly to the remote machine.
459*4882a593Smuzhiyun    This does not inform the data cache; the data cache uses this.
460*4882a593Smuzhiyun    MEMADDR is the address in the remote memory space.
461*4882a593Smuzhiyun    MYADDR is the address of the buffer in our space.
462*4882a593Smuzhiyun    LEN is the number of bytes.
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun    Returns number of bytes transferred, or 0 for error.  */
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun int
remote_write_bytes(memaddr,myaddr,len)467*4882a593Smuzhiyun remote_write_bytes (memaddr, myaddr, len)
468*4882a593Smuzhiyun      CORE_ADDR memaddr;
469*4882a593Smuzhiyun      char *myaddr;
470*4882a593Smuzhiyun      int len;
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun   unsigned char *buf = alloca (PBUFSIZ);
473*4882a593Smuzhiyun   int max_buf_size;		/* Max size of packet output buffer */
474*4882a593Smuzhiyun   int origlen;
475*4882a593Smuzhiyun   extern int verbose;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun   /* Verify that the target can support a binary download */
478*4882a593Smuzhiyun   check_binary_download (memaddr);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun   /* Chop the transfer down if necessary */
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun   max_buf_size = min (remote_write_size, PBUFSIZ);
483*4882a593Smuzhiyun   if (remote_register_buf_size != 0)
484*4882a593Smuzhiyun     max_buf_size = min (max_buf_size, remote_register_buf_size);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun   /* Subtract header overhead from max payload size -  $M<memaddr>,<len>:#nn */
487*4882a593Smuzhiyun   max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun   origlen = len;
490*4882a593Smuzhiyun   while (len > 0)
491*4882a593Smuzhiyun     {
492*4882a593Smuzhiyun       unsigned char *p, *plen;
493*4882a593Smuzhiyun       int todo;
494*4882a593Smuzhiyun       int i;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun       /* construct "M"<memaddr>","<len>":" */
497*4882a593Smuzhiyun       /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
498*4882a593Smuzhiyun       memaddr = remote_address_masked (memaddr);
499*4882a593Smuzhiyun       p = buf;
500*4882a593Smuzhiyun       if (remote_binary_download)
501*4882a593Smuzhiyun 	{
502*4882a593Smuzhiyun 	  *p++ = 'X';
503*4882a593Smuzhiyun 	  todo = min (len, max_buf_size);
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun       else
506*4882a593Smuzhiyun 	{
507*4882a593Smuzhiyun 	  *p++ = 'M';
508*4882a593Smuzhiyun 	  todo = min (len, max_buf_size / 2);	/* num bytes that will fit */
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun       p += hexnumstr ((char *)p, (ULONGEST) memaddr);
512*4882a593Smuzhiyun       *p++ = ',';
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun       plen = p;			/* remember where len field goes */
515*4882a593Smuzhiyun       p += hexnumstr ((char *)p, (ULONGEST) todo);
516*4882a593Smuzhiyun       *p++ = ':';
517*4882a593Smuzhiyun       *p = '\0';
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun       /* We send target system values byte by byte, in increasing byte
520*4882a593Smuzhiyun 	 addresses, each byte encoded as two hex characters (or one
521*4882a593Smuzhiyun 	 binary character).  */
522*4882a593Smuzhiyun       if (remote_binary_download)
523*4882a593Smuzhiyun 	{
524*4882a593Smuzhiyun 	  int escaped = 0;
525*4882a593Smuzhiyun 	  for (i = 0;
526*4882a593Smuzhiyun 	       (i < todo) && (i + escaped) < (max_buf_size - 2);
527*4882a593Smuzhiyun 	       i++)
528*4882a593Smuzhiyun 	    {
529*4882a593Smuzhiyun 	      switch (myaddr[i] & 0xff)
530*4882a593Smuzhiyun 		{
531*4882a593Smuzhiyun 		case '$':
532*4882a593Smuzhiyun 		case '#':
533*4882a593Smuzhiyun 		case 0x7d:
534*4882a593Smuzhiyun 		  /* These must be escaped */
535*4882a593Smuzhiyun 		  escaped++;
536*4882a593Smuzhiyun 		  *p++ = 0x7d;
537*4882a593Smuzhiyun 		  *p++ = (myaddr[i] & 0xff) ^ 0x20;
538*4882a593Smuzhiyun 		  break;
539*4882a593Smuzhiyun 		default:
540*4882a593Smuzhiyun 		  *p++ = myaddr[i] & 0xff;
541*4882a593Smuzhiyun 		  break;
542*4882a593Smuzhiyun 		}
543*4882a593Smuzhiyun 	    }
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	  if (i < todo)
546*4882a593Smuzhiyun 	    {
547*4882a593Smuzhiyun 	      /* Escape chars have filled up the buffer prematurely,
548*4882a593Smuzhiyun 		 and we have actually sent fewer bytes than planned.
549*4882a593Smuzhiyun 		 Fix-up the length field of the packet.  */
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	      /* FIXME: will fail if new len is a shorter string than
552*4882a593Smuzhiyun 		 old len.  */
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	      plen += hexnumstr ((char *)plen, (ULONGEST) i);
555*4882a593Smuzhiyun 	      *plen++ = ':';
556*4882a593Smuzhiyun 	    }
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun       else
559*4882a593Smuzhiyun 	{
560*4882a593Smuzhiyun 	  for (i = 0; i < todo; i++)
561*4882a593Smuzhiyun 	    {
562*4882a593Smuzhiyun 	      *p++ = tohex ((myaddr[i] >> 4) & 0xf);
563*4882a593Smuzhiyun 	      *p++ = tohex (myaddr[i] & 0xf);
564*4882a593Smuzhiyun 	    }
565*4882a593Smuzhiyun 	  *p = '\0';
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun       putpkt_binary ((char *)buf, (int) (p - buf));
569*4882a593Smuzhiyun       getpkt ((char *)buf, 0);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun       if (buf[0] == 'E')
572*4882a593Smuzhiyun 	{
573*4882a593Smuzhiyun 	  /* There is no correspondance between what the remote protocol uses
574*4882a593Smuzhiyun 	     for errors and errno codes.  We would like a cleaner way of
575*4882a593Smuzhiyun 	     representing errors (big enough to include errno codes, bfd_error
576*4882a593Smuzhiyun 	     codes, and others).  But for now just return EIO.  */
577*4882a593Smuzhiyun 	  errno = EIO;
578*4882a593Smuzhiyun 	  return 0;
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun       /* Increment by i, not by todo, in case escape chars
582*4882a593Smuzhiyun 	 caused us to send fewer bytes than we'd planned.  */
583*4882a593Smuzhiyun       myaddr += i;
584*4882a593Smuzhiyun       memaddr += i;
585*4882a593Smuzhiyun       len -= i;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun       if (verbose)
588*4882a593Smuzhiyun 	putc('.', stderr);
589*4882a593Smuzhiyun     }
590*4882a593Smuzhiyun   return origlen;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /* Stuff for dealing with the packets which are part of this protocol.
594*4882a593Smuzhiyun    See comment at top of file for details.  */
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun /* Read a single character from the remote end, masking it down to 7 bits. */
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun static int
readchar(int timeout)599*4882a593Smuzhiyun readchar (int timeout)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun   int ch;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun   ch = SERIAL_READCHAR (remote_desc, timeout);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun   switch (ch)
606*4882a593Smuzhiyun     {
607*4882a593Smuzhiyun     case SERIAL_EOF:
608*4882a593Smuzhiyun       error ("Remote connection closed");
609*4882a593Smuzhiyun     case SERIAL_ERROR:
610*4882a593Smuzhiyun       perror_with_name ("Remote communication error");
611*4882a593Smuzhiyun     case SERIAL_TIMEOUT:
612*4882a593Smuzhiyun       return ch;
613*4882a593Smuzhiyun     default:
614*4882a593Smuzhiyun       return ch & 0x7f;
615*4882a593Smuzhiyun     }
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun static int
putpkt(buf)619*4882a593Smuzhiyun putpkt (buf)
620*4882a593Smuzhiyun      char *buf;
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun   return putpkt_binary (buf, strlen (buf));
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun /* Send a packet to the remote machine, with error checking.  The data
626*4882a593Smuzhiyun    of the packet is in BUF.  The string in BUF can be at most  PBUFSIZ - 5
627*4882a593Smuzhiyun    to account for the $, # and checksum, and for a possible /0 if we are
628*4882a593Smuzhiyun    debugging (remote_debug) and want to print the sent packet as a string */
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun static int
putpkt_binary(buf,cnt)631*4882a593Smuzhiyun putpkt_binary (buf, cnt)
632*4882a593Smuzhiyun      char *buf;
633*4882a593Smuzhiyun      int cnt;
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun   int i;
636*4882a593Smuzhiyun   unsigned char csum = 0;
637*4882a593Smuzhiyun   char *buf2 = alloca (PBUFSIZ);
638*4882a593Smuzhiyun   char *junkbuf = alloca (PBUFSIZ);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun   int ch;
641*4882a593Smuzhiyun   int tcount = 0;
642*4882a593Smuzhiyun   char *p;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun   /* Copy the packet into buffer BUF2, encapsulating it
645*4882a593Smuzhiyun      and giving it a checksum.  */
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun   if (cnt > BUFSIZ - 5)		/* Prosanity check */
648*4882a593Smuzhiyun     abort ();
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun   p = buf2;
651*4882a593Smuzhiyun   *p++ = '$';
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun   for (i = 0; i < cnt; i++)
654*4882a593Smuzhiyun     {
655*4882a593Smuzhiyun       csum += buf[i];
656*4882a593Smuzhiyun       *p++ = buf[i];
657*4882a593Smuzhiyun     }
658*4882a593Smuzhiyun   *p++ = '#';
659*4882a593Smuzhiyun   *p++ = tohex ((csum >> 4) & 0xf);
660*4882a593Smuzhiyun   *p++ = tohex (csum & 0xf);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun   /* Send it over and over until we get a positive ack.  */
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun   while (1)
665*4882a593Smuzhiyun     {
666*4882a593Smuzhiyun       int started_error_output = 0;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun       if (remote_debug)
669*4882a593Smuzhiyun 	{
670*4882a593Smuzhiyun 	  *p = '\0';
671*4882a593Smuzhiyun 	  fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
672*4882a593Smuzhiyun 	  fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
673*4882a593Smuzhiyun 	  fprintf_unfiltered (gdb_stdlog, "...");
674*4882a593Smuzhiyun 	  gdb_flush (gdb_stdlog);
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
677*4882a593Smuzhiyun 	perror_with_name ("putpkt: write failed");
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun       /* read until either a timeout occurs (-2) or '+' is read */
680*4882a593Smuzhiyun       while (1)
681*4882a593Smuzhiyun 	{
682*4882a593Smuzhiyun 	  ch = readchar (remote_timeout);
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	  if (remote_debug)
685*4882a593Smuzhiyun 	    {
686*4882a593Smuzhiyun 	      switch (ch)
687*4882a593Smuzhiyun 		{
688*4882a593Smuzhiyun 		case '+':
689*4882a593Smuzhiyun 		case SERIAL_TIMEOUT:
690*4882a593Smuzhiyun 		case '$':
691*4882a593Smuzhiyun 		  if (started_error_output)
692*4882a593Smuzhiyun 		    {
693*4882a593Smuzhiyun 		      putchar_unfiltered ('\n');
694*4882a593Smuzhiyun 		      started_error_output = 0;
695*4882a593Smuzhiyun 		    }
696*4882a593Smuzhiyun 		}
697*4882a593Smuzhiyun 	    }
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	  switch (ch)
700*4882a593Smuzhiyun 	    {
701*4882a593Smuzhiyun 	    case '+':
702*4882a593Smuzhiyun 	      if (remote_debug)
703*4882a593Smuzhiyun 		fprintf_unfiltered (gdb_stdlog, "Ack\n");
704*4882a593Smuzhiyun 	      return 1;
705*4882a593Smuzhiyun 	    case SERIAL_TIMEOUT:
706*4882a593Smuzhiyun 	      tcount++;
707*4882a593Smuzhiyun 	      if (tcount > 3)
708*4882a593Smuzhiyun 		return 0;
709*4882a593Smuzhiyun 	      break;		/* Retransmit buffer */
710*4882a593Smuzhiyun 	    case '$':
711*4882a593Smuzhiyun 	      {
712*4882a593Smuzhiyun 		/* It's probably an old response, and we're out of sync.
713*4882a593Smuzhiyun 		   Just gobble up the packet and ignore it.  */
714*4882a593Smuzhiyun 		getpkt (junkbuf, 0);
715*4882a593Smuzhiyun 		continue;	/* Now, go look for + */
716*4882a593Smuzhiyun 	      }
717*4882a593Smuzhiyun 	    default:
718*4882a593Smuzhiyun 	      if (remote_debug)
719*4882a593Smuzhiyun 		{
720*4882a593Smuzhiyun 		  if (!started_error_output)
721*4882a593Smuzhiyun 		    {
722*4882a593Smuzhiyun 		      started_error_output = 1;
723*4882a593Smuzhiyun 		      fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
724*4882a593Smuzhiyun 		    }
725*4882a593Smuzhiyun 		  fputc_unfiltered (ch & 0177, gdb_stdlog);
726*4882a593Smuzhiyun 		}
727*4882a593Smuzhiyun 	      continue;
728*4882a593Smuzhiyun 	    }
729*4882a593Smuzhiyun 	  break;		/* Here to retransmit */
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun #if 0
733*4882a593Smuzhiyun       /* This is wrong.  If doing a long backtrace, the user should be
734*4882a593Smuzhiyun 	 able to get out next time we call QUIT, without anything as
735*4882a593Smuzhiyun 	 violent as interrupt_query.  If we want to provide a way out of
736*4882a593Smuzhiyun 	 here without getting to the next QUIT, it should be based on
737*4882a593Smuzhiyun 	 hitting ^C twice as in remote_wait.  */
738*4882a593Smuzhiyun       if (quit_flag)
739*4882a593Smuzhiyun 	{
740*4882a593Smuzhiyun 	  quit_flag = 0;
741*4882a593Smuzhiyun 	  interrupt_query ();
742*4882a593Smuzhiyun 	}
743*4882a593Smuzhiyun #endif
744*4882a593Smuzhiyun     }
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun /* Come here after finding the start of the frame.  Collect the rest
748*4882a593Smuzhiyun    into BUF, verifying the checksum, length, and handling run-length
749*4882a593Smuzhiyun    compression.  Returns 0 on any error, 1 on success.  */
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun static int
read_frame(char * buf)752*4882a593Smuzhiyun read_frame (char *buf)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun   unsigned char csum;
755*4882a593Smuzhiyun   char *bp;
756*4882a593Smuzhiyun   int c;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun   csum = 0;
759*4882a593Smuzhiyun   bp = buf;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun   while (1)
762*4882a593Smuzhiyun     {
763*4882a593Smuzhiyun       c = readchar (remote_timeout);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun       switch (c)
766*4882a593Smuzhiyun 	{
767*4882a593Smuzhiyun 	case SERIAL_TIMEOUT:
768*4882a593Smuzhiyun 	  if (remote_debug)
769*4882a593Smuzhiyun 	    fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
770*4882a593Smuzhiyun 	  return 0;
771*4882a593Smuzhiyun 	case '$':
772*4882a593Smuzhiyun 	  if (remote_debug)
773*4882a593Smuzhiyun 	    fputs_filtered ("Saw new packet start in middle of old one\n",
774*4882a593Smuzhiyun 			    gdb_stdlog);
775*4882a593Smuzhiyun 	  return 0;		/* Start a new packet, count retries */
776*4882a593Smuzhiyun 	case '#':
777*4882a593Smuzhiyun 	  {
778*4882a593Smuzhiyun 	    unsigned char pktcsum;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	    *bp = '\000';
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	    pktcsum = fromhex (readchar (remote_timeout)) << 4;
783*4882a593Smuzhiyun 	    pktcsum |= fromhex (readchar (remote_timeout));
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	    if (csum == pktcsum)
786*4882a593Smuzhiyun 	      {
787*4882a593Smuzhiyun 		return 1;
788*4882a593Smuzhiyun 	      }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	    if (remote_debug)
791*4882a593Smuzhiyun 	      {
792*4882a593Smuzhiyun 		fprintf_filtered (gdb_stdlog,
793*4882a593Smuzhiyun 			      "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
794*4882a593Smuzhiyun 				  pktcsum, csum);
795*4882a593Smuzhiyun 		fputs_filtered (buf, gdb_stdlog);
796*4882a593Smuzhiyun 		fputs_filtered ("\n", gdb_stdlog);
797*4882a593Smuzhiyun 	      }
798*4882a593Smuzhiyun 	    return 0;
799*4882a593Smuzhiyun 	  }
800*4882a593Smuzhiyun 	case '*':		/* Run length encoding */
801*4882a593Smuzhiyun 	  csum += c;
802*4882a593Smuzhiyun 	  c = readchar (remote_timeout);
803*4882a593Smuzhiyun 	  csum += c;
804*4882a593Smuzhiyun 	  c = c - ' ' + 3;	/* Compute repeat count */
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	  if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
807*4882a593Smuzhiyun 	    {
808*4882a593Smuzhiyun 	      memset (bp, *(bp - 1), c);
809*4882a593Smuzhiyun 	      bp += c;
810*4882a593Smuzhiyun 	      continue;
811*4882a593Smuzhiyun 	    }
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	  *bp = '\0';
814*4882a593Smuzhiyun 	  printf_filtered ("Repeat count %d too large for buffer: ", c);
815*4882a593Smuzhiyun 	  puts_filtered (buf);
816*4882a593Smuzhiyun 	  puts_filtered ("\n");
817*4882a593Smuzhiyun 	  return 0;
818*4882a593Smuzhiyun 	default:
819*4882a593Smuzhiyun 	  if (bp < buf + PBUFSIZ - 1)
820*4882a593Smuzhiyun 	    {
821*4882a593Smuzhiyun 	      *bp++ = c;
822*4882a593Smuzhiyun 	      csum += c;
823*4882a593Smuzhiyun 	      continue;
824*4882a593Smuzhiyun 	    }
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	  *bp = '\0';
827*4882a593Smuzhiyun 	  puts_filtered ("Remote packet too long: ");
828*4882a593Smuzhiyun 	  puts_filtered (buf);
829*4882a593Smuzhiyun 	  puts_filtered ("\n");
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	  return 0;
832*4882a593Smuzhiyun 	}
833*4882a593Smuzhiyun     }
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /* Read a packet from the remote machine, with error checking, and
837*4882a593Smuzhiyun    store it in BUF.  BUF is expected to be of size PBUFSIZ.  If
838*4882a593Smuzhiyun    FOREVER, wait forever rather than timing out; this is used while
839*4882a593Smuzhiyun    the target is executing user code.  */
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun static void
getpkt(buf,forever)842*4882a593Smuzhiyun getpkt (buf, forever)
843*4882a593Smuzhiyun      char *buf;
844*4882a593Smuzhiyun      int forever;
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun   int c;
847*4882a593Smuzhiyun   int tries;
848*4882a593Smuzhiyun   int timeout;
849*4882a593Smuzhiyun   int val;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun   strcpy (buf, "timeout");
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun   if (forever)
854*4882a593Smuzhiyun     {
855*4882a593Smuzhiyun       timeout = watchdog > 0 ? watchdog : -1;
856*4882a593Smuzhiyun     }
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun   else
859*4882a593Smuzhiyun     timeout = remote_timeout;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun #define MAX_TRIES 3
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun   for (tries = 1; tries <= MAX_TRIES; tries++)
864*4882a593Smuzhiyun     {
865*4882a593Smuzhiyun       /* This can loop forever if the remote side sends us characters
866*4882a593Smuzhiyun 	 continuously, but if it pauses, we'll get a zero from readchar
867*4882a593Smuzhiyun 	 because of timeout.  Then we'll count that as a retry.  */
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun       /* Note that we will only wait forever prior to the start of a packet.
870*4882a593Smuzhiyun 	 After that, we expect characters to arrive at a brisk pace.  They
871*4882a593Smuzhiyun 	 should show up within remote_timeout intervals.  */
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun       do
874*4882a593Smuzhiyun 	{
875*4882a593Smuzhiyun 	  c = readchar (timeout);
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	  if (c == SERIAL_TIMEOUT)
878*4882a593Smuzhiyun 	    {
879*4882a593Smuzhiyun 	      if (forever)	/* Watchdog went off.  Kill the target. */
880*4882a593Smuzhiyun 		{
881*4882a593Smuzhiyun 		  target_mourn_inferior ();
882*4882a593Smuzhiyun 		  error ("Watchdog has expired.  Target detached.\n");
883*4882a593Smuzhiyun 		}
884*4882a593Smuzhiyun 	      if (remote_debug)
885*4882a593Smuzhiyun 		fputs_filtered ("Timed out.\n", gdb_stdlog);
886*4882a593Smuzhiyun 	      goto retry;
887*4882a593Smuzhiyun 	    }
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun       while (c != '$');
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun       /* We've found the start of a packet, now collect the data.  */
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun       val = read_frame (buf);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun       if (val == 1)
896*4882a593Smuzhiyun 	{
897*4882a593Smuzhiyun 	  if (remote_debug)
898*4882a593Smuzhiyun 	    {
899*4882a593Smuzhiyun 	      fprintf_unfiltered (gdb_stdlog, "Packet received: ");
900*4882a593Smuzhiyun 	      fputstr_unfiltered (buf, 0, gdb_stdlog);
901*4882a593Smuzhiyun 	      fprintf_unfiltered (gdb_stdlog, "\n");
902*4882a593Smuzhiyun 	    }
903*4882a593Smuzhiyun 	  SERIAL_WRITE (remote_desc, "+", 1);
904*4882a593Smuzhiyun 	  return;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun       /* Try the whole thing again.  */
908*4882a593Smuzhiyun     retry:
909*4882a593Smuzhiyun       SERIAL_WRITE (remote_desc, "-", 1);
910*4882a593Smuzhiyun     }
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun   /* We have tried hard enough, and just can't receive the packet.  Give up. */
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun   printf_unfiltered ("Ignoring packet error, continuing...\n");
915*4882a593Smuzhiyun   SERIAL_WRITE (remote_desc, "+", 1);
916*4882a593Smuzhiyun }
917