remote-utils.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /* Remote utility routines for the remote server for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #if HAVE_TERMIOS_H
  16. #include <termios.h>
  17. #endif
  18. #include "target.h"
  19. #include "gdbthread.h"
  20. #include "tdesc.h"
  21. #include "debug.h"
  22. #include "dll.h"
  23. #include "gdbsupport/rsp-low.h"
  24. #include "gdbsupport/netstuff.h"
  25. #include "gdbsupport/filestuff.h"
  26. #include "gdbsupport/gdb-sigmask.h"
  27. #include <ctype.h>
  28. #if HAVE_SYS_IOCTL_H
  29. #include <sys/ioctl.h>
  30. #endif
  31. #if HAVE_SYS_FILE_H
  32. #include <sys/file.h>
  33. #endif
  34. #if HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #if HAVE_SYS_SOCKET_H
  38. #include <sys/socket.h>
  39. #endif
  40. #if HAVE_NETDB_H
  41. #include <netdb.h>
  42. #endif
  43. #if HAVE_NETINET_TCP_H
  44. #include <netinet/tcp.h>
  45. #endif
  46. #if HAVE_SYS_IOCTL_H
  47. #include <sys/ioctl.h>
  48. #endif
  49. #if HAVE_SIGNAL_H
  50. #include <signal.h>
  51. #endif
  52. #if HAVE_FCNTL_H
  53. #include <fcntl.h>
  54. #endif
  55. #include "gdbsupport/gdb_sys_time.h"
  56. #include <unistd.h>
  57. #if HAVE_ARPA_INET_H
  58. #include <arpa/inet.h>
  59. #endif
  60. #include <sys/stat.h>
  61. #if USE_WIN32API
  62. #include <ws2tcpip.h>
  63. #endif
  64. #ifndef HAVE_SOCKLEN_T
  65. typedef int socklen_t;
  66. #endif
  67. #ifndef IN_PROCESS_AGENT
  68. /* Extra value for readchar_callback. */
  69. enum {
  70. /* The callback is currently not scheduled. */
  71. NOT_SCHEDULED = -1
  72. };
  73. /* Status of the readchar callback.
  74. Either NOT_SCHEDULED or the callback id. */
  75. static int readchar_callback = NOT_SCHEDULED;
  76. static int readchar (void);
  77. static void reset_readchar (void);
  78. static void reschedule (void);
  79. /* A cache entry for a successfully looked-up symbol. */
  80. struct sym_cache
  81. {
  82. char *name;
  83. CORE_ADDR addr;
  84. struct sym_cache *next;
  85. };
  86. static int remote_is_stdio = 0;
  87. static int remote_desc = -1;
  88. static int listen_desc = -1;
  89. #ifdef USE_WIN32API
  90. /* gnulib wraps these as macros, undo them. */
  91. # undef read
  92. # undef write
  93. # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
  94. # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
  95. #endif
  96. int
  97. gdb_connected (void)
  98. {
  99. return remote_desc != -1;
  100. }
  101. /* Return true if the remote connection is over stdio. */
  102. int
  103. remote_connection_is_stdio (void)
  104. {
  105. return remote_is_stdio;
  106. }
  107. static void
  108. enable_async_notification (int fd)
  109. {
  110. #if defined(F_SETFL) && defined (FASYNC)
  111. int save_fcntl_flags;
  112. save_fcntl_flags = fcntl (fd, F_GETFL, 0);
  113. fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
  114. #if defined (F_SETOWN)
  115. fcntl (fd, F_SETOWN, getpid ());
  116. #endif
  117. #endif
  118. }
  119. static void
  120. handle_accept_event (int err, gdb_client_data client_data)
  121. {
  122. struct sockaddr_storage sockaddr;
  123. socklen_t len = sizeof (sockaddr);
  124. threads_debug_printf ("handling possible accept event");
  125. remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &len);
  126. if (remote_desc == -1)
  127. perror_with_name ("Accept failed");
  128. /* Enable TCP keep alive process. */
  129. socklen_t tmp = 1;
  130. setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
  131. (char *) &tmp, sizeof (tmp));
  132. /* Tell TCP not to delay small packets. This greatly speeds up
  133. interactive response. */
  134. tmp = 1;
  135. setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
  136. (char *) &tmp, sizeof (tmp));
  137. #ifndef USE_WIN32API
  138. signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
  139. exits when the remote side dies. */
  140. #endif
  141. if (run_once)
  142. {
  143. #ifndef USE_WIN32API
  144. close (listen_desc); /* No longer need this */
  145. #else
  146. closesocket (listen_desc); /* No longer need this */
  147. #endif
  148. }
  149. /* Even if !RUN_ONCE no longer notice new connections. Still keep the
  150. descriptor open for add_file_handler to wait for a new connection. */
  151. delete_file_handler (listen_desc);
  152. /* Convert IP address to string. */
  153. char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
  154. int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
  155. orig_host, sizeof (orig_host),
  156. orig_port, sizeof (orig_port),
  157. NI_NUMERICHOST | NI_NUMERICSERV);
  158. if (r != 0)
  159. fprintf (stderr, _("Could not obtain remote address: %s\n"),
  160. gai_strerror (r));
  161. else
  162. fprintf (stderr, _("Remote debugging from host %s, port %s\n"),
  163. orig_host, orig_port);
  164. enable_async_notification (remote_desc);
  165. /* Register the event loop handler. */
  166. add_file_handler (remote_desc, handle_serial_event, NULL, "remote-net");
  167. /* We have a new GDB connection now. If we were disconnected
  168. tracing, there's a window where the target could report a stop
  169. event to the event loop, and since we have a connection now, we'd
  170. try to send vStopped notifications to GDB. But, don't do that
  171. until GDB as selected all-stop/non-stop, and has queried the
  172. threads' status ('?'). */
  173. target_async (0);
  174. }
  175. /* Prepare for a later connection to a remote debugger.
  176. NAME is the filename used for communication. */
  177. void
  178. remote_prepare (const char *name)
  179. {
  180. client_state &cs = get_client_state ();
  181. #ifdef USE_WIN32API
  182. static int winsock_initialized;
  183. #endif
  184. socklen_t tmp;
  185. remote_is_stdio = 0;
  186. if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
  187. {
  188. /* We need to record fact that we're using stdio sooner than the
  189. call to remote_open so start_inferior knows the connection is
  190. via stdio. */
  191. remote_is_stdio = 1;
  192. cs.transport_is_reliable = 1;
  193. return;
  194. }
  195. struct addrinfo hint;
  196. struct addrinfo *ainfo;
  197. memset (&hint, 0, sizeof (hint));
  198. /* Assume no prefix will be passed, therefore we should use
  199. AF_UNSPEC. */
  200. hint.ai_family = AF_UNSPEC;
  201. hint.ai_socktype = SOCK_STREAM;
  202. hint.ai_protocol = IPPROTO_TCP;
  203. parsed_connection_spec parsed
  204. = parse_connection_spec_without_prefix (name, &hint);
  205. if (parsed.port_str.empty ())
  206. {
  207. cs.transport_is_reliable = 0;
  208. return;
  209. }
  210. #ifdef USE_WIN32API
  211. if (!winsock_initialized)
  212. {
  213. WSADATA wsad;
  214. WSAStartup (MAKEWORD (1, 0), &wsad);
  215. winsock_initialized = 1;
  216. }
  217. #endif
  218. int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
  219. &hint, &ainfo);
  220. if (r != 0)
  221. error (_("%s: cannot resolve name: %s"), name, gai_strerror (r));
  222. scoped_free_addrinfo freeaddrinfo (ainfo);
  223. struct addrinfo *iter;
  224. for (iter = ainfo; iter != NULL; iter = iter->ai_next)
  225. {
  226. listen_desc = gdb_socket_cloexec (iter->ai_family, iter->ai_socktype,
  227. iter->ai_protocol);
  228. if (listen_desc >= 0)
  229. break;
  230. }
  231. if (iter == NULL)
  232. perror_with_name ("Can't open socket");
  233. /* Allow rapid reuse of this port. */
  234. tmp = 1;
  235. setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
  236. sizeof (tmp));
  237. switch (iter->ai_family)
  238. {
  239. case AF_INET:
  240. ((struct sockaddr_in *) iter->ai_addr)->sin_addr.s_addr = INADDR_ANY;
  241. break;
  242. case AF_INET6:
  243. ((struct sockaddr_in6 *) iter->ai_addr)->sin6_addr = in6addr_any;
  244. break;
  245. default:
  246. internal_error (__FILE__, __LINE__,
  247. _("Invalid 'ai_family' %d\n"), iter->ai_family);
  248. }
  249. if (bind (listen_desc, iter->ai_addr, iter->ai_addrlen) != 0)
  250. perror_with_name ("Can't bind address");
  251. if (listen (listen_desc, 1) != 0)
  252. perror_with_name ("Can't listen on socket");
  253. cs.transport_is_reliable = 1;
  254. }
  255. /* Open a connection to a remote debugger.
  256. NAME is the filename used for communication. */
  257. void
  258. remote_open (const char *name)
  259. {
  260. const char *port_str;
  261. port_str = strchr (name, ':');
  262. #ifdef USE_WIN32API
  263. if (port_str == NULL)
  264. error ("Only HOST:PORT is supported on this platform.");
  265. #endif
  266. if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
  267. {
  268. fprintf (stderr, "Remote debugging using stdio\n");
  269. /* Use stdin as the handle of the connection.
  270. We only select on reads, for example. */
  271. remote_desc = fileno (stdin);
  272. enable_async_notification (remote_desc);
  273. /* Register the event loop handler. */
  274. add_file_handler (remote_desc, handle_serial_event, NULL, "remote-stdio");
  275. }
  276. #ifndef USE_WIN32API
  277. else if (port_str == NULL)
  278. {
  279. struct stat statbuf;
  280. if (stat (name, &statbuf) == 0
  281. && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
  282. remote_desc = open (name, O_RDWR);
  283. else
  284. {
  285. errno = EINVAL;
  286. remote_desc = -1;
  287. }
  288. if (remote_desc < 0)
  289. perror_with_name ("Could not open remote device");
  290. #if HAVE_TERMIOS_H
  291. {
  292. struct termios termios;
  293. tcgetattr (remote_desc, &termios);
  294. termios.c_iflag = 0;
  295. termios.c_oflag = 0;
  296. termios.c_lflag = 0;
  297. termios.c_cflag &= ~(CSIZE | PARENB);
  298. termios.c_cflag |= CLOCAL | CS8;
  299. termios.c_cc[VMIN] = 1;
  300. termios.c_cc[VTIME] = 0;
  301. tcsetattr (remote_desc, TCSANOW, &termios);
  302. }
  303. #endif
  304. fprintf (stderr, "Remote debugging using %s\n", name);
  305. enable_async_notification (remote_desc);
  306. /* Register the event loop handler. */
  307. add_file_handler (remote_desc, handle_serial_event, NULL,
  308. "remote-device");
  309. }
  310. #endif /* USE_WIN32API */
  311. else
  312. {
  313. char listen_port[GDB_NI_MAX_PORT];
  314. struct sockaddr_storage sockaddr;
  315. socklen_t len = sizeof (sockaddr);
  316. if (getsockname (listen_desc, (struct sockaddr *) &sockaddr, &len) < 0)
  317. perror_with_name ("Can't determine port");
  318. int r = getnameinfo ((struct sockaddr *) &sockaddr, len,
  319. NULL, 0,
  320. listen_port, sizeof (listen_port),
  321. NI_NUMERICSERV);
  322. if (r != 0)
  323. fprintf (stderr, _("Can't obtain port where we are listening: %s"),
  324. gai_strerror (r));
  325. else
  326. fprintf (stderr, _("Listening on port %s\n"), listen_port);
  327. fflush (stderr);
  328. /* Register the event loop handler. */
  329. add_file_handler (listen_desc, handle_accept_event, NULL,
  330. "remote-listen");
  331. }
  332. }
  333. void
  334. remote_close (void)
  335. {
  336. delete_file_handler (remote_desc);
  337. disable_async_io ();
  338. #ifdef USE_WIN32API
  339. closesocket (remote_desc);
  340. #else
  341. if (! remote_connection_is_stdio ())
  342. close (remote_desc);
  343. #endif
  344. remote_desc = -1;
  345. reset_readchar ();
  346. }
  347. #endif
  348. #ifndef IN_PROCESS_AGENT
  349. void
  350. decode_address (CORE_ADDR *addrp, const char *start, int len)
  351. {
  352. CORE_ADDR addr;
  353. char ch;
  354. int i;
  355. addr = 0;
  356. for (i = 0; i < len; i++)
  357. {
  358. ch = start[i];
  359. addr = addr << 4;
  360. addr = addr | (fromhex (ch) & 0x0f);
  361. }
  362. *addrp = addr;
  363. }
  364. const char *
  365. decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
  366. {
  367. const char *end;
  368. end = start;
  369. while (*end != '\0' && *end != ';')
  370. end++;
  371. decode_address (addrp, start, end - start);
  372. if (*end == ';')
  373. end++;
  374. return end;
  375. }
  376. #endif
  377. #ifndef IN_PROCESS_AGENT
  378. /* Look for a sequence of characters which can be run-length encoded.
  379. If there are any, update *CSUM and *P. Otherwise, output the
  380. single character. Return the number of characters consumed. */
  381. static int
  382. try_rle (char *buf, int remaining, unsigned char *csum, char **p)
  383. {
  384. int n;
  385. /* Always output the character. */
  386. *csum += buf[0];
  387. *(*p)++ = buf[0];
  388. /* Don't go past '~'. */
  389. if (remaining > 97)
  390. remaining = 97;
  391. for (n = 1; n < remaining; n++)
  392. if (buf[n] != buf[0])
  393. break;
  394. /* N is the index of the first character not the same as buf[0].
  395. buf[0] is counted twice, so by decrementing N, we get the number
  396. of characters the RLE sequence will replace. */
  397. n--;
  398. if (n < 3)
  399. return 1;
  400. /* Skip the frame characters. The manual says to skip '+' and '-'
  401. also, but there's no reason to. Unfortunately these two unusable
  402. characters double the encoded length of a four byte zero
  403. value. */
  404. while (n + 29 == '$' || n + 29 == '#')
  405. n--;
  406. *csum += '*';
  407. *(*p)++ = '*';
  408. *csum += n + 29;
  409. *(*p)++ = n + 29;
  410. return n + 1;
  411. }
  412. #endif
  413. #ifndef IN_PROCESS_AGENT
  414. /* Write a PTID to BUF. Returns BUF+CHARACTERS_WRITTEN. */
  415. char *
  416. write_ptid (char *buf, ptid_t ptid)
  417. {
  418. client_state &cs = get_client_state ();
  419. int pid, tid;
  420. if (cs.multi_process)
  421. {
  422. pid = ptid.pid ();
  423. if (pid < 0)
  424. buf += sprintf (buf, "p-%x.", -pid);
  425. else
  426. buf += sprintf (buf, "p%x.", pid);
  427. }
  428. tid = ptid.lwp ();
  429. if (tid < 0)
  430. buf += sprintf (buf, "-%x", -tid);
  431. else
  432. buf += sprintf (buf, "%x", tid);
  433. return buf;
  434. }
  435. static ULONGEST
  436. hex_or_minus_one (const char *buf, const char **obuf)
  437. {
  438. ULONGEST ret;
  439. if (startswith (buf, "-1"))
  440. {
  441. ret = (ULONGEST) -1;
  442. buf += 2;
  443. }
  444. else
  445. buf = unpack_varlen_hex (buf, &ret);
  446. if (obuf)
  447. *obuf = buf;
  448. return ret;
  449. }
  450. /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
  451. passed the last parsed char. Returns null_ptid on error. */
  452. ptid_t
  453. read_ptid (const char *buf, const char **obuf)
  454. {
  455. const char *p = buf;
  456. const char *pp;
  457. ULONGEST pid = 0, tid = 0;
  458. if (*p == 'p')
  459. {
  460. /* Multi-process ptid. */
  461. pp = unpack_varlen_hex (p + 1, &pid);
  462. if (*pp != '.')
  463. error ("invalid remote ptid: %s\n", p);
  464. p = pp + 1;
  465. tid = hex_or_minus_one (p, &pp);
  466. if (obuf)
  467. *obuf = pp;
  468. return ptid_t (pid, tid);
  469. }
  470. /* No multi-process. Just a tid. */
  471. tid = hex_or_minus_one (p, &pp);
  472. /* Since GDB is not sending a process id (multi-process extensions
  473. are off), then there's only one process. Default to the first in
  474. the list. */
  475. pid = pid_of (get_first_process ());
  476. if (obuf)
  477. *obuf = pp;
  478. return ptid_t (pid, tid);
  479. }
  480. /* Write COUNT bytes in BUF to the client.
  481. The result is the number of bytes written or -1 if error.
  482. This may return less than COUNT. */
  483. static int
  484. write_prim (const void *buf, int count)
  485. {
  486. if (remote_connection_is_stdio ())
  487. return write (fileno (stdout), buf, count);
  488. else
  489. return write (remote_desc, buf, count);
  490. }
  491. /* Read COUNT bytes from the client and store in BUF.
  492. The result is the number of bytes read or -1 if error.
  493. This may return less than COUNT. */
  494. static int
  495. read_prim (void *buf, int count)
  496. {
  497. if (remote_connection_is_stdio ())
  498. return read (fileno (stdin), buf, count);
  499. else
  500. return read (remote_desc, buf, count);
  501. }
  502. /* Send a packet to the remote machine, with error checking.
  503. The data of the packet is in BUF, and the length of the
  504. packet is in CNT. Returns >= 0 on success, -1 otherwise. */
  505. static int
  506. putpkt_binary_1 (char *buf, int cnt, int is_notif)
  507. {
  508. client_state &cs = get_client_state ();
  509. int i;
  510. unsigned char csum = 0;
  511. char *buf2;
  512. char *p;
  513. int cc;
  514. buf2 = (char *) xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);
  515. /* Copy the packet into buffer BUF2, encapsulating it
  516. and giving it a checksum. */
  517. p = buf2;
  518. if (is_notif)
  519. *p++ = '%';
  520. else
  521. *p++ = '$';
  522. for (i = 0; i < cnt;)
  523. i += try_rle (buf + i, cnt - i, &csum, &p);
  524. *p++ = '#';
  525. *p++ = tohex ((csum >> 4) & 0xf);
  526. *p++ = tohex (csum & 0xf);
  527. *p = '\0';
  528. /* Send it over and over until we get a positive ack. */
  529. do
  530. {
  531. if (write_prim (buf2, p - buf2) != p - buf2)
  532. {
  533. perror ("putpkt(write)");
  534. free (buf2);
  535. return -1;
  536. }
  537. if (cs.noack_mode || is_notif)
  538. {
  539. /* Don't expect an ack then. */
  540. if (is_notif)
  541. remote_debug_printf ("putpkt (\"%s\"); [notif]", buf2);
  542. else
  543. remote_debug_printf ("putpkt (\"%s\"); [noack mode]", buf2);
  544. break;
  545. }
  546. remote_debug_printf ("putpkt (\"%s\"); [looking for ack]", buf2);
  547. cc = readchar ();
  548. if (cc < 0)
  549. {
  550. free (buf2);
  551. return -1;
  552. }
  553. remote_debug_printf ("[received '%c' (0x%x)]", cc, cc);
  554. /* Check for an input interrupt while we're here. */
  555. if (cc == '\003' && current_thread != NULL)
  556. the_target->request_interrupt ();
  557. }
  558. while (cc != '+');
  559. free (buf2);
  560. return 1; /* Success! */
  561. }
  562. int
  563. putpkt_binary (char *buf, int cnt)
  564. {
  565. return putpkt_binary_1 (buf, cnt, 0);
  566. }
  567. /* Send a packet to the remote machine, with error checking. The data
  568. of the packet is in BUF, and the packet should be a NUL-terminated
  569. string. Returns >= 0 on success, -1 otherwise. */
  570. int
  571. putpkt (char *buf)
  572. {
  573. return putpkt_binary (buf, strlen (buf));
  574. }
  575. int
  576. putpkt_notif (char *buf)
  577. {
  578. return putpkt_binary_1 (buf, strlen (buf), 1);
  579. }
  580. /* Come here when we get an input interrupt from the remote side. This
  581. interrupt should only be active while we are waiting for the child to do
  582. something. Thus this assumes readchar:bufcnt is 0.
  583. About the only thing that should come through is a ^C, which
  584. will cause us to request child interruption. */
  585. static void
  586. input_interrupt (int unused)
  587. {
  588. fd_set readset;
  589. struct timeval immediate = { 0, 0 };
  590. /* Protect against spurious interrupts. This has been observed to
  591. be a problem under NetBSD 1.4 and 1.5. */
  592. FD_ZERO (&readset);
  593. FD_SET (remote_desc, &readset);
  594. if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
  595. {
  596. int cc;
  597. char c = 0;
  598. cc = read_prim (&c, 1);
  599. if (cc == 0)
  600. {
  601. fprintf (stderr, "client connection closed\n");
  602. return;
  603. }
  604. else if (cc != 1 || c != '\003')
  605. {
  606. fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
  607. if (isprint (c))
  608. fprintf (stderr, "('%c')\n", c);
  609. else
  610. fprintf (stderr, "('\\x%02x')\n", c & 0xff);
  611. return;
  612. }
  613. the_target->request_interrupt ();
  614. }
  615. }
  616. /* Check if the remote side sent us an interrupt request (^C). */
  617. void
  618. check_remote_input_interrupt_request (void)
  619. {
  620. /* This function may be called before establishing communications,
  621. therefore we need to validate the remote descriptor. */
  622. if (remote_desc == -1)
  623. return;
  624. input_interrupt (0);
  625. }
  626. /* Asynchronous I/O support. SIGIO must be unblocked when waiting,
  627. in order to accept Control-C from the client, and must be blocked
  628. when talking to the client. */
  629. static void
  630. block_unblock_async_io (int block)
  631. {
  632. #ifndef USE_WIN32API
  633. sigset_t sigio_set;
  634. sigemptyset (&sigio_set);
  635. sigaddset (&sigio_set, SIGIO);
  636. gdb_sigmask (block ? SIG_BLOCK : SIG_UNBLOCK, &sigio_set, NULL);
  637. #endif
  638. }
  639. /* Current state of asynchronous I/O. */
  640. static int async_io_enabled;
  641. /* Enable asynchronous I/O. */
  642. void
  643. enable_async_io (void)
  644. {
  645. if (async_io_enabled)
  646. return;
  647. block_unblock_async_io (0);
  648. async_io_enabled = 1;
  649. }
  650. /* Disable asynchronous I/O. */
  651. void
  652. disable_async_io (void)
  653. {
  654. if (!async_io_enabled)
  655. return;
  656. block_unblock_async_io (1);
  657. async_io_enabled = 0;
  658. }
  659. void
  660. initialize_async_io (void)
  661. {
  662. /* Make sure that async I/O starts blocked. */
  663. async_io_enabled = 1;
  664. disable_async_io ();
  665. /* Install the signal handler. */
  666. #ifndef USE_WIN32API
  667. signal (SIGIO, input_interrupt);
  668. #endif
  669. }
  670. /* Internal buffer used by readchar.
  671. These are global to readchar because reschedule_remote needs to be
  672. able to tell whether the buffer is empty. */
  673. static unsigned char readchar_buf[BUFSIZ];
  674. static int readchar_bufcnt = 0;
  675. static unsigned char *readchar_bufp;
  676. /* Returns next char from remote GDB. -1 if error. */
  677. static int
  678. readchar (void)
  679. {
  680. int ch;
  681. if (readchar_bufcnt == 0)
  682. {
  683. readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));
  684. if (readchar_bufcnt <= 0)
  685. {
  686. if (readchar_bufcnt == 0)
  687. {
  688. remote_debug_printf ("readchar: Got EOF");
  689. }
  690. else
  691. perror ("readchar");
  692. return -1;
  693. }
  694. readchar_bufp = readchar_buf;
  695. }
  696. readchar_bufcnt--;
  697. ch = *readchar_bufp++;
  698. reschedule ();
  699. return ch;
  700. }
  701. /* Reset the readchar state machine. */
  702. static void
  703. reset_readchar (void)
  704. {
  705. readchar_bufcnt = 0;
  706. if (readchar_callback != NOT_SCHEDULED)
  707. {
  708. delete_timer (readchar_callback);
  709. readchar_callback = NOT_SCHEDULED;
  710. }
  711. }
  712. /* Process remaining data in readchar_buf. */
  713. static void
  714. process_remaining (void *context)
  715. {
  716. /* This is a one-shot event. */
  717. readchar_callback = NOT_SCHEDULED;
  718. if (readchar_bufcnt > 0)
  719. handle_serial_event (0, NULL);
  720. }
  721. /* If there is still data in the buffer, queue another event to process it,
  722. we can't sleep in select yet. */
  723. static void
  724. reschedule (void)
  725. {
  726. if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
  727. readchar_callback = create_timer (0, process_remaining, NULL);
  728. }
  729. /* Read a packet from the remote machine, with error checking,
  730. and store it in BUF. Returns length of packet, or negative if error. */
  731. int
  732. getpkt (char *buf)
  733. {
  734. client_state &cs = get_client_state ();
  735. char *bp;
  736. unsigned char csum, c1, c2;
  737. int c;
  738. while (1)
  739. {
  740. csum = 0;
  741. while (1)
  742. {
  743. c = readchar ();
  744. /* The '\003' may appear before or after each packet, so
  745. check for an input interrupt. */
  746. if (c == '\003')
  747. {
  748. the_target->request_interrupt ();
  749. continue;
  750. }
  751. if (c == '$')
  752. break;
  753. remote_debug_printf ("[getpkt: discarding char '%c']", c);
  754. if (c < 0)
  755. return -1;
  756. }
  757. bp = buf;
  758. while (1)
  759. {
  760. c = readchar ();
  761. if (c < 0)
  762. return -1;
  763. if (c == '#')
  764. break;
  765. *bp++ = c;
  766. csum += c;
  767. }
  768. *bp = 0;
  769. c1 = fromhex (readchar ());
  770. c2 = fromhex (readchar ());
  771. if (csum == (c1 << 4) + c2)
  772. break;
  773. if (cs.noack_mode)
  774. {
  775. fprintf (stderr,
  776. "Bad checksum, sentsum=0x%x, csum=0x%x, "
  777. "buf=%s [no-ack-mode, Bad medium?]\n",
  778. (c1 << 4) + c2, csum, buf);
  779. /* Not much we can do, GDB wasn't expecting an ack/nac. */
  780. break;
  781. }
  782. fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  783. (c1 << 4) + c2, csum, buf);
  784. if (write_prim ("-", 1) != 1)
  785. return -1;
  786. }
  787. if (!cs.noack_mode)
  788. {
  789. remote_debug_printf ("getpkt (\"%s\"); [sending ack]", buf);
  790. if (write_prim ("+", 1) != 1)
  791. return -1;
  792. remote_debug_printf ("[sent ack]");
  793. }
  794. else
  795. remote_debug_printf ("getpkt (\"%s\"); [no ack sent]", buf);
  796. /* The readchar above may have already read a '\003' out of the socket
  797. and moved it to the local buffer. For example, when GDB sends
  798. vCont;c immediately followed by interrupt (see
  799. gdb.base/interrupt-noterm.exp). As soon as we see the vCont;c, we'll
  800. resume the inferior and wait. Since we've already moved the '\003'
  801. to the local buffer, SIGIO won't help. In that case, if we don't
  802. check for interrupt after the vCont;c packet, the interrupt character
  803. would stay in the buffer unattended until after the next (unrelated)
  804. stop. */
  805. while (readchar_bufcnt > 0 && *readchar_bufp == '\003')
  806. {
  807. /* Consume the interrupt character in the buffer. */
  808. readchar ();
  809. the_target->request_interrupt ();
  810. }
  811. return bp - buf;
  812. }
  813. void
  814. write_ok (char *buf)
  815. {
  816. buf[0] = 'O';
  817. buf[1] = 'K';
  818. buf[2] = '\0';
  819. }
  820. void
  821. write_enn (char *buf)
  822. {
  823. /* Some day, we should define the meanings of the error codes... */
  824. buf[0] = 'E';
  825. buf[1] = '0';
  826. buf[2] = '1';
  827. buf[3] = '\0';
  828. }
  829. #endif
  830. #ifndef IN_PROCESS_AGENT
  831. static char *
  832. outreg (struct regcache *regcache, int regno, char *buf)
  833. {
  834. if ((regno >> 12) != 0)
  835. *buf++ = tohex ((regno >> 12) & 0xf);
  836. if ((regno >> 8) != 0)
  837. *buf++ = tohex ((regno >> 8) & 0xf);
  838. *buf++ = tohex ((regno >> 4) & 0xf);
  839. *buf++ = tohex (regno & 0xf);
  840. *buf++ = ':';
  841. collect_register_as_string (regcache, regno, buf);
  842. buf += 2 * register_size (regcache->tdesc, regno);
  843. *buf++ = ';';
  844. return buf;
  845. }
  846. void
  847. prepare_resume_reply (char *buf, ptid_t ptid, const target_waitstatus &status)
  848. {
  849. client_state &cs = get_client_state ();
  850. threads_debug_printf ("Writing resume reply for %s:%d",
  851. target_pid_to_str (ptid).c_str (), status.kind ());
  852. switch (status.kind ())
  853. {
  854. case TARGET_WAITKIND_STOPPED:
  855. case TARGET_WAITKIND_FORKED:
  856. case TARGET_WAITKIND_VFORKED:
  857. case TARGET_WAITKIND_VFORK_DONE:
  858. case TARGET_WAITKIND_EXECD:
  859. case TARGET_WAITKIND_THREAD_CREATED:
  860. case TARGET_WAITKIND_SYSCALL_ENTRY:
  861. case TARGET_WAITKIND_SYSCALL_RETURN:
  862. {
  863. const char **regp;
  864. struct regcache *regcache;
  865. char *buf_start = buf;
  866. if ((status.kind () == TARGET_WAITKIND_FORKED && cs.report_fork_events)
  867. || (status.kind () == TARGET_WAITKIND_VFORKED
  868. && cs.report_vfork_events))
  869. {
  870. enum gdb_signal signal = GDB_SIGNAL_TRAP;
  871. const char *event = (status.kind () == TARGET_WAITKIND_FORKED
  872. ? "fork" : "vfork");
  873. sprintf (buf, "T%02x%s:", signal, event);
  874. buf += strlen (buf);
  875. buf = write_ptid (buf, status.child_ptid ());
  876. strcat (buf, ";");
  877. }
  878. else if (status.kind () == TARGET_WAITKIND_VFORK_DONE
  879. && cs.report_vfork_events)
  880. {
  881. enum gdb_signal signal = GDB_SIGNAL_TRAP;
  882. sprintf (buf, "T%02xvforkdone:;", signal);
  883. }
  884. else if (status.kind () == TARGET_WAITKIND_EXECD && cs.report_exec_events)
  885. {
  886. enum gdb_signal signal = GDB_SIGNAL_TRAP;
  887. const char *event = "exec";
  888. char hexified_pathname[PATH_MAX * 2];
  889. sprintf (buf, "T%02x%s:", signal, event);
  890. buf += strlen (buf);
  891. /* Encode pathname to hexified format. */
  892. bin2hex ((const gdb_byte *) status.execd_pathname (),
  893. hexified_pathname,
  894. strlen (status.execd_pathname ()));
  895. sprintf (buf, "%s;", hexified_pathname);
  896. buf += strlen (buf);
  897. }
  898. else if (status.kind () == TARGET_WAITKIND_THREAD_CREATED
  899. && cs.report_thread_events)
  900. {
  901. enum gdb_signal signal = GDB_SIGNAL_TRAP;
  902. sprintf (buf, "T%02xcreate:;", signal);
  903. }
  904. else if (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
  905. || status.kind () == TARGET_WAITKIND_SYSCALL_RETURN)
  906. {
  907. enum gdb_signal signal = GDB_SIGNAL_TRAP;
  908. const char *event = (status.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
  909. ? "syscall_entry" : "syscall_return");
  910. sprintf (buf, "T%02x%s:%x;", signal, event,
  911. status.syscall_number ());
  912. }
  913. else
  914. sprintf (buf, "T%02x", status.sig ());
  915. if (disable_packet_T)
  916. {
  917. /* This is a bit (OK, a lot) of a kludge, however, this isn't
  918. really a user feature, but exists only so GDB can use the
  919. gdbserver to test handling of the 'S' stop reply packet, so
  920. we would rather this code be as simple as possible.
  921. By this point we've started to build the 'T' stop packet,
  922. and it should look like 'Txx....' where 'x' is a hex digit.
  923. An 'S' stop packet always looks like 'Sxx', so all we do
  924. here is convert the buffer from a T packet to an S packet
  925. and the avoid adding any extra content by breaking out. */
  926. gdb_assert (buf_start[0] == 'T');
  927. gdb_assert (isxdigit (buf_start[1]));
  928. gdb_assert (isxdigit (buf_start[2]));
  929. buf_start[0] = 'S';
  930. buf_start[3] = '\0';
  931. break;
  932. }
  933. buf += strlen (buf);
  934. scoped_restore_current_thread restore_thread;
  935. switch_to_thread (the_target, ptid);
  936. regp = current_target_desc ()->expedite_regs;
  937. regcache = get_thread_regcache (current_thread, 1);
  938. if (the_target->stopped_by_watchpoint ())
  939. {
  940. CORE_ADDR addr;
  941. int i;
  942. memcpy (buf, "watch:", 6);
  943. buf += 6;
  944. addr = the_target->stopped_data_address ();
  945. /* Convert each byte of the address into two hexadecimal
  946. chars. Note that we take sizeof (void *) instead of
  947. sizeof (addr); this is to avoid sending a 64-bit
  948. address to a 32-bit GDB. */
  949. for (i = sizeof (void *) * 2; i > 0; i--)
  950. *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
  951. *buf++ = ';';
  952. }
  953. else if (cs.swbreak_feature && target_stopped_by_sw_breakpoint ())
  954. {
  955. sprintf (buf, "swbreak:;");
  956. buf += strlen (buf);
  957. }
  958. else if (cs.hwbreak_feature && target_stopped_by_hw_breakpoint ())
  959. {
  960. sprintf (buf, "hwbreak:;");
  961. buf += strlen (buf);
  962. }
  963. while (*regp)
  964. {
  965. buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
  966. regp ++;
  967. }
  968. *buf = '\0';
  969. /* Formerly, if the debugger had not used any thread features
  970. we would not burden it with a thread status response. This
  971. was for the benefit of GDB 4.13 and older. However, in
  972. recent GDB versions the check (``if (cont_thread != 0)'')
  973. does not have the desired effect because of sillyness in
  974. the way that the remote protocol handles specifying a
  975. thread. Since thread support relies on qSymbol support
  976. anyway, assume GDB can handle threads. */
  977. if (using_threads && !disable_packet_Tthread)
  978. {
  979. /* This if (1) ought to be unnecessary. But remote_wait
  980. in GDB will claim this event belongs to inferior_ptid
  981. if we do not specify a thread, and there's no way for
  982. gdbserver to know what inferior_ptid is. */
  983. if (1 || cs.general_thread != ptid)
  984. {
  985. int core = -1;
  986. /* In non-stop, don't change the general thread behind
  987. GDB's back. */
  988. if (!non_stop)
  989. cs.general_thread = ptid;
  990. sprintf (buf, "thread:");
  991. buf += strlen (buf);
  992. buf = write_ptid (buf, ptid);
  993. strcat (buf, ";");
  994. buf += strlen (buf);
  995. core = target_core_of_thread (ptid);
  996. if (core != -1)
  997. {
  998. sprintf (buf, "core:");
  999. buf += strlen (buf);
  1000. sprintf (buf, "%x", core);
  1001. strcat (buf, ";");
  1002. buf += strlen (buf);
  1003. }
  1004. }
  1005. }
  1006. if (current_process ()->dlls_changed)
  1007. {
  1008. strcpy (buf, "library:;");
  1009. buf += strlen (buf);
  1010. current_process ()->dlls_changed = false;
  1011. }
  1012. }
  1013. break;
  1014. case TARGET_WAITKIND_EXITED:
  1015. if (cs.multi_process)
  1016. sprintf (buf, "W%x;process:%x",
  1017. status.exit_status (), ptid.pid ());
  1018. else
  1019. sprintf (buf, "W%02x", status.exit_status ());
  1020. break;
  1021. case TARGET_WAITKIND_SIGNALLED:
  1022. if (cs.multi_process)
  1023. sprintf (buf, "X%x;process:%x",
  1024. status.sig (), ptid.pid ());
  1025. else
  1026. sprintf (buf, "X%02x", status.sig ());
  1027. break;
  1028. case TARGET_WAITKIND_THREAD_EXITED:
  1029. sprintf (buf, "w%x;", status.exit_status ());
  1030. buf += strlen (buf);
  1031. buf = write_ptid (buf, ptid);
  1032. break;
  1033. case TARGET_WAITKIND_NO_RESUMED:
  1034. sprintf (buf, "N");
  1035. break;
  1036. default:
  1037. error ("unhandled waitkind");
  1038. break;
  1039. }
  1040. }
  1041. /* See remote-utils.h. */
  1042. const char *
  1043. decode_m_packet_params (const char *from, CORE_ADDR *mem_addr_ptr,
  1044. unsigned int *len_ptr, const char end_marker)
  1045. {
  1046. int i = 0;
  1047. char ch;
  1048. *mem_addr_ptr = *len_ptr = 0;
  1049. while ((ch = from[i++]) != ',')
  1050. {
  1051. *mem_addr_ptr = *mem_addr_ptr << 4;
  1052. *mem_addr_ptr |= fromhex (ch) & 0x0f;
  1053. }
  1054. while ((ch = from[i++]) != end_marker)
  1055. {
  1056. *len_ptr = *len_ptr << 4;
  1057. *len_ptr |= fromhex (ch) & 0x0f;
  1058. }
  1059. return from + i;
  1060. }
  1061. void
  1062. decode_m_packet (const char *from, CORE_ADDR *mem_addr_ptr,
  1063. unsigned int *len_ptr)
  1064. {
  1065. decode_m_packet_params (from, mem_addr_ptr, len_ptr, '\0');
  1066. }
  1067. void
  1068. decode_M_packet (const char *from, CORE_ADDR *mem_addr_ptr,
  1069. unsigned int *len_ptr, unsigned char **to_p)
  1070. {
  1071. from = decode_m_packet_params (from, mem_addr_ptr, len_ptr, ':');
  1072. if (*to_p == NULL)
  1073. *to_p = (unsigned char *) xmalloc (*len_ptr);
  1074. hex2bin (from, *to_p, *len_ptr);
  1075. }
  1076. int
  1077. decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
  1078. unsigned int *len_ptr, unsigned char **to_p)
  1079. {
  1080. int i = 0;
  1081. char ch;
  1082. *mem_addr_ptr = *len_ptr = 0;
  1083. while ((ch = from[i++]) != ',')
  1084. {
  1085. *mem_addr_ptr = *mem_addr_ptr << 4;
  1086. *mem_addr_ptr |= fromhex (ch) & 0x0f;
  1087. }
  1088. while ((ch = from[i++]) != ':')
  1089. {
  1090. *len_ptr = *len_ptr << 4;
  1091. *len_ptr |= fromhex (ch) & 0x0f;
  1092. }
  1093. if (*to_p == NULL)
  1094. *to_p = (unsigned char *) xmalloc (*len_ptr);
  1095. if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
  1096. *to_p, *len_ptr) != *len_ptr)
  1097. return -1;
  1098. return 0;
  1099. }
  1100. /* Decode a qXfer write request. */
  1101. int
  1102. decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
  1103. unsigned int *len, unsigned char *data)
  1104. {
  1105. char ch;
  1106. char *b = buf;
  1107. /* Extract the offset. */
  1108. *offset = 0;
  1109. while ((ch = *buf++) != ':')
  1110. {
  1111. *offset = *offset << 4;
  1112. *offset |= fromhex (ch) & 0x0f;
  1113. }
  1114. /* Get encoded data. */
  1115. packet_len -= buf - b;
  1116. *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
  1117. data, packet_len);
  1118. return 0;
  1119. }
  1120. /* Decode the parameters of a qSearch:memory packet. */
  1121. int
  1122. decode_search_memory_packet (const char *buf, int packet_len,
  1123. CORE_ADDR *start_addrp,
  1124. CORE_ADDR *search_space_lenp,
  1125. gdb_byte *pattern, unsigned int *pattern_lenp)
  1126. {
  1127. const char *p = buf;
  1128. p = decode_address_to_semicolon (start_addrp, p);
  1129. p = decode_address_to_semicolon (search_space_lenp, p);
  1130. packet_len -= p - buf;
  1131. *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
  1132. pattern, packet_len);
  1133. return 0;
  1134. }
  1135. static void
  1136. free_sym_cache (struct sym_cache *sym)
  1137. {
  1138. if (sym != NULL)
  1139. {
  1140. free (sym->name);
  1141. free (sym);
  1142. }
  1143. }
  1144. void
  1145. clear_symbol_cache (struct sym_cache **symcache_p)
  1146. {
  1147. struct sym_cache *sym, *next;
  1148. /* Check the cache first. */
  1149. for (sym = *symcache_p; sym; sym = next)
  1150. {
  1151. next = sym->next;
  1152. free_sym_cache (sym);
  1153. }
  1154. *symcache_p = NULL;
  1155. }
  1156. /* Get the address of NAME, and return it in ADDRP if found. if
  1157. MAY_ASK_GDB is false, assume symbol cache misses are failures.
  1158. Returns 1 if the symbol is found, 0 if it is not, -1 on error. */
  1159. int
  1160. look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
  1161. {
  1162. client_state &cs = get_client_state ();
  1163. char *p, *q;
  1164. int len;
  1165. struct sym_cache *sym;
  1166. struct process_info *proc;
  1167. proc = current_process ();
  1168. /* Check the cache first. */
  1169. for (sym = proc->symbol_cache; sym; sym = sym->next)
  1170. if (strcmp (name, sym->name) == 0)
  1171. {
  1172. *addrp = sym->addr;
  1173. return 1;
  1174. }
  1175. /* It might not be an appropriate time to look up a symbol,
  1176. e.g. while we're trying to fetch registers. */
  1177. if (!may_ask_gdb)
  1178. return 0;
  1179. /* Send the request. */
  1180. strcpy (cs.own_buf, "qSymbol:");
  1181. bin2hex ((const gdb_byte *) name, cs.own_buf + strlen ("qSymbol:"),
  1182. strlen (name));
  1183. if (putpkt (cs.own_buf) < 0)
  1184. return -1;
  1185. /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
  1186. len = getpkt (cs.own_buf);
  1187. if (len < 0)
  1188. return -1;
  1189. /* We ought to handle pretty much any packet at this point while we
  1190. wait for the qSymbol "response". That requires re-entering the
  1191. main loop. For now, this is an adequate approximation; allow
  1192. GDB to read from memory and handle 'v' packets (for vFile transfers)
  1193. while it figures out the address of the symbol. */
  1194. while (1)
  1195. {
  1196. if (cs.own_buf[0] == 'm')
  1197. {
  1198. CORE_ADDR mem_addr;
  1199. unsigned char *mem_buf;
  1200. unsigned int mem_len;
  1201. decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
  1202. mem_buf = (unsigned char *) xmalloc (mem_len);
  1203. if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
  1204. bin2hex (mem_buf, cs.own_buf, mem_len);
  1205. else
  1206. write_enn (cs.own_buf);
  1207. free (mem_buf);
  1208. if (putpkt (cs.own_buf) < 0)
  1209. return -1;
  1210. }
  1211. else if (cs.own_buf[0] == 'v')
  1212. {
  1213. int new_len = -1;
  1214. handle_v_requests (cs.own_buf, len, &new_len);
  1215. if (new_len != -1)
  1216. putpkt_binary (cs.own_buf, new_len);
  1217. else
  1218. putpkt (cs.own_buf);
  1219. }
  1220. else
  1221. break;
  1222. len = getpkt (cs.own_buf);
  1223. if (len < 0)
  1224. return -1;
  1225. }
  1226. if (!startswith (cs.own_buf, "qSymbol:"))
  1227. {
  1228. warning ("Malformed response to qSymbol, ignoring: %s", cs.own_buf);
  1229. return -1;
  1230. }
  1231. p = cs.own_buf + strlen ("qSymbol:");
  1232. q = p;
  1233. while (*q && *q != ':')
  1234. q++;
  1235. /* Make sure we found a value for the symbol. */
  1236. if (p == q || *q == '\0')
  1237. return 0;
  1238. decode_address (addrp, p, q - p);
  1239. /* Save the symbol in our cache. */
  1240. sym = XNEW (struct sym_cache);
  1241. sym->name = xstrdup (name);
  1242. sym->addr = *addrp;
  1243. sym->next = proc->symbol_cache;
  1244. proc->symbol_cache = sym;
  1245. return 1;
  1246. }
  1247. /* Relocate an instruction to execute at a different address. OLDLOC
  1248. is the address in the inferior memory where the instruction to
  1249. relocate is currently at. On input, TO points to the destination
  1250. where we want the instruction to be copied (and possibly adjusted)
  1251. to. On output, it points to one past the end of the resulting
  1252. instruction(s). The effect of executing the instruction at TO
  1253. shall be the same as if executing it at OLDLOC. For example, call
  1254. instructions that implicitly push the return address on the stack
  1255. should be adjusted to return to the instruction after OLDLOC;
  1256. relative branches, and other PC-relative instructions need the
  1257. offset adjusted; etc. Returns 0 on success, -1 on failure. */
  1258. int
  1259. relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
  1260. {
  1261. client_state &cs = get_client_state ();
  1262. int len;
  1263. ULONGEST written = 0;
  1264. /* Send the request. */
  1265. sprintf (cs.own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
  1266. paddress (*to));
  1267. if (putpkt (cs.own_buf) < 0)
  1268. return -1;
  1269. /* FIXME: Eventually add buffer overflow checking (to getpkt?) */
  1270. len = getpkt (cs.own_buf);
  1271. if (len < 0)
  1272. return -1;
  1273. /* We ought to handle pretty much any packet at this point while we
  1274. wait for the qRelocInsn "response". That requires re-entering
  1275. the main loop. For now, this is an adequate approximation; allow
  1276. GDB to access memory. */
  1277. while (cs.own_buf[0] == 'm' || cs.own_buf[0] == 'M' || cs.own_buf[0] == 'X')
  1278. {
  1279. CORE_ADDR mem_addr;
  1280. unsigned char *mem_buf = NULL;
  1281. unsigned int mem_len;
  1282. if (cs.own_buf[0] == 'm')
  1283. {
  1284. decode_m_packet (&cs.own_buf[1], &mem_addr, &mem_len);
  1285. mem_buf = (unsigned char *) xmalloc (mem_len);
  1286. if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
  1287. bin2hex (mem_buf, cs.own_buf, mem_len);
  1288. else
  1289. write_enn (cs.own_buf);
  1290. }
  1291. else if (cs.own_buf[0] == 'X')
  1292. {
  1293. if (decode_X_packet (&cs.own_buf[1], len - 1, &mem_addr,
  1294. &mem_len, &mem_buf) < 0
  1295. || target_write_memory (mem_addr, mem_buf, mem_len) != 0)
  1296. write_enn (cs.own_buf);
  1297. else
  1298. write_ok (cs.own_buf);
  1299. }
  1300. else
  1301. {
  1302. decode_M_packet (&cs.own_buf[1], &mem_addr, &mem_len, &mem_buf);
  1303. if (target_write_memory (mem_addr, mem_buf, mem_len) == 0)
  1304. write_ok (cs.own_buf);
  1305. else
  1306. write_enn (cs.own_buf);
  1307. }
  1308. free (mem_buf);
  1309. if (putpkt (cs.own_buf) < 0)
  1310. return -1;
  1311. len = getpkt (cs.own_buf);
  1312. if (len < 0)
  1313. return -1;
  1314. }
  1315. if (cs.own_buf[0] == 'E')
  1316. {
  1317. warning ("An error occurred while relocating an instruction: %s",
  1318. cs.own_buf);
  1319. return -1;
  1320. }
  1321. if (!startswith (cs.own_buf, "qRelocInsn:"))
  1322. {
  1323. warning ("Malformed response to qRelocInsn, ignoring: %s",
  1324. cs.own_buf);
  1325. return -1;
  1326. }
  1327. unpack_varlen_hex (cs.own_buf + strlen ("qRelocInsn:"), &written);
  1328. *to += written;
  1329. return 0;
  1330. }
  1331. void
  1332. monitor_output (const char *msg)
  1333. {
  1334. int len = strlen (msg);
  1335. char *buf = (char *) xmalloc (len * 2 + 2);
  1336. buf[0] = 'O';
  1337. bin2hex ((const gdb_byte *) msg, buf + 1, len);
  1338. putpkt (buf);
  1339. free (buf);
  1340. }
  1341. #endif