remote.c 23 KB

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