remote.c 24 KB

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