gdbreplay.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* Replay a remote debug session logfile for GDB.
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "gdbsupport/common-defs.h"
  16. #undef PACKAGE
  17. #undef PACKAGE_NAME
  18. #undef PACKAGE_VERSION
  19. #undef PACKAGE_STRING
  20. #undef PACKAGE_TARNAME
  21. #include <config.h>
  22. #include "gdbsupport/version.h"
  23. #if HAVE_SYS_FILE_H
  24. #include <sys/file.h>
  25. #endif
  26. #if HAVE_SIGNAL_H
  27. #include <signal.h>
  28. #endif
  29. #include <ctype.h>
  30. #if HAVE_FCNTL_H
  31. #include <fcntl.h>
  32. #endif
  33. #include <unistd.h>
  34. #ifdef HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #ifdef 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 USE_WIN32API
  47. #include <ws2tcpip.h>
  48. #endif
  49. #include "gdbsupport/netstuff.h"
  50. #include "gdbsupport/rsp-low.h"
  51. #ifndef HAVE_SOCKLEN_T
  52. typedef int socklen_t;
  53. #endif
  54. /* Sort of a hack... */
  55. #define EOL (EOF - 1)
  56. static int remote_desc_in;
  57. static int remote_desc_out;
  58. static void
  59. sync_error (FILE *fp, const char *desc, int expect, int got)
  60. {
  61. fprintf (stderr, "\n%s\n", desc);
  62. fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
  63. ftell (fp), expect, got);
  64. fflush (stderr);
  65. exit (1);
  66. }
  67. static void
  68. remote_error (const char *desc)
  69. {
  70. fprintf (stderr, "\n%s\n", desc);
  71. fflush (stderr);
  72. exit (1);
  73. }
  74. static void
  75. remote_close (void)
  76. {
  77. #ifdef USE_WIN32API
  78. gdb_assert (remote_desc_in == remote_desc_out);
  79. closesocket (remote_desc_in);
  80. #else
  81. close (remote_desc_in);
  82. if (remote_desc_in != remote_desc_out)
  83. close (remote_desc_out);
  84. #endif
  85. }
  86. /* Open a connection to a remote debugger.
  87. NAME is the filename used for communication. */
  88. static void
  89. remote_open (const char *name)
  90. {
  91. #ifndef USE_WIN32API
  92. if (strcmp (name, "-") == 0)
  93. {
  94. remote_desc_in = 0;
  95. remote_desc_out = 1;
  96. return;
  97. }
  98. #endif
  99. const char *last_colon = strrchr (name, ':');
  100. if (last_colon == NULL)
  101. {
  102. fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
  103. fflush (stderr);
  104. exit (1);
  105. }
  106. #ifdef USE_WIN32API
  107. static int winsock_initialized;
  108. #endif
  109. int tmp;
  110. int tmp_desc;
  111. struct addrinfo hint;
  112. struct addrinfo *ainfo;
  113. memset (&hint, 0, sizeof (hint));
  114. /* Assume no prefix will be passed, therefore we should use
  115. AF_UNSPEC. */
  116. hint.ai_family = AF_UNSPEC;
  117. hint.ai_socktype = SOCK_STREAM;
  118. hint.ai_protocol = IPPROTO_TCP;
  119. parsed_connection_spec parsed = parse_connection_spec (name, &hint);
  120. if (parsed.port_str.empty ())
  121. error (_("Missing port on hostname '%s'"), name);
  122. #ifdef USE_WIN32API
  123. if (!winsock_initialized)
  124. {
  125. WSADATA wsad;
  126. WSAStartup (MAKEWORD (1, 0), &wsad);
  127. winsock_initialized = 1;
  128. }
  129. #endif
  130. int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
  131. &hint, &ainfo);
  132. if (r != 0)
  133. {
  134. fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
  135. parsed.host_str.c_str (), parsed.port_str.c_str (),
  136. gai_strerror (r));
  137. fflush (stderr);
  138. exit (1);
  139. }
  140. scoped_free_addrinfo free_ainfo (ainfo);
  141. struct addrinfo *p;
  142. for (p = ainfo; p != NULL; p = p->ai_next)
  143. {
  144. tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
  145. if (tmp_desc >= 0)
  146. break;
  147. }
  148. if (p == NULL)
  149. perror_with_name ("Cannot open socket");
  150. /* Allow rapid reuse of this port. */
  151. tmp = 1;
  152. setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
  153. sizeof (tmp));
  154. switch (p->ai_family)
  155. {
  156. case AF_INET:
  157. ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
  158. break;
  159. case AF_INET6:
  160. ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
  161. break;
  162. default:
  163. fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
  164. exit (1);
  165. }
  166. if (bind (tmp_desc, p->ai_addr, p->ai_addrlen) != 0)
  167. perror_with_name ("Can't bind address");
  168. if (p->ai_socktype == SOCK_DGRAM)
  169. remote_desc_in = tmp_desc;
  170. else
  171. {
  172. struct sockaddr_storage sockaddr;
  173. socklen_t sockaddrsize = sizeof (sockaddr);
  174. char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
  175. if (listen (tmp_desc, 1) != 0)
  176. perror_with_name ("Can't listen on socket");
  177. remote_desc_in = accept (tmp_desc, (struct sockaddr *) &sockaddr,
  178. &sockaddrsize);
  179. if (remote_desc_in == -1)
  180. perror_with_name ("Accept failed");
  181. /* Enable TCP keep alive process. */
  182. tmp = 1;
  183. setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
  184. (char *) &tmp, sizeof (tmp));
  185. /* Tell TCP not to delay small packets. This greatly speeds up
  186. interactive response. */
  187. tmp = 1;
  188. setsockopt (remote_desc_in, IPPROTO_TCP, TCP_NODELAY,
  189. (char *) &tmp, sizeof (tmp));
  190. if (getnameinfo ((struct sockaddr *) &sockaddr, sockaddrsize,
  191. orig_host, sizeof (orig_host),
  192. orig_port, sizeof (orig_port),
  193. NI_NUMERICHOST | NI_NUMERICSERV) == 0)
  194. {
  195. fprintf (stderr, "Remote debugging from host %s, port %s\n",
  196. orig_host, orig_port);
  197. fflush (stderr);
  198. }
  199. #ifndef USE_WIN32API
  200. close (tmp_desc); /* No longer need this */
  201. signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
  202. gdbreplay simply exits when
  203. the remote side dies. */
  204. #else
  205. closesocket (tmp_desc); /* No longer need this */
  206. #endif
  207. }
  208. #if defined(F_SETFL) && defined (FASYNC)
  209. fcntl (remote_desc_in, F_SETFL, FASYNC);
  210. #endif
  211. remote_desc_out = remote_desc_in;
  212. fprintf (stderr, "Replay logfile using %s\n", name);
  213. fflush (stderr);
  214. }
  215. static int
  216. logchar (FILE *fp)
  217. {
  218. int ch;
  219. int ch2;
  220. ch = fgetc (fp);
  221. if (ch != '\r')
  222. {
  223. fputc (ch, stderr);
  224. fflush (stderr);
  225. }
  226. switch (ch)
  227. {
  228. /* Treat \r\n as a newline. */
  229. case '\r':
  230. ch = fgetc (fp);
  231. if (ch == '\n')
  232. ch = EOL;
  233. else
  234. {
  235. ungetc (ch, fp);
  236. ch = '\r';
  237. }
  238. fputc (ch == EOL ? '\n' : '\r', stderr);
  239. fflush (stderr);
  240. break;
  241. case '\n':
  242. ch = EOL;
  243. break;
  244. case '\\':
  245. ch = fgetc (fp);
  246. fputc (ch, stderr);
  247. fflush (stderr);
  248. switch (ch)
  249. {
  250. case '\\':
  251. break;
  252. case 'b':
  253. ch = '\b';
  254. break;
  255. case 'f':
  256. ch = '\f';
  257. break;
  258. case 'n':
  259. ch = '\n';
  260. break;
  261. case 'r':
  262. ch = '\r';
  263. break;
  264. case 't':
  265. ch = '\t';
  266. break;
  267. case 'v':
  268. ch = '\v';
  269. break;
  270. case 'x':
  271. ch2 = fgetc (fp);
  272. fputc (ch2, stderr);
  273. fflush (stderr);
  274. ch = fromhex (ch2) << 4;
  275. ch2 = fgetc (fp);
  276. fputc (ch2, stderr);
  277. fflush (stderr);
  278. ch |= fromhex (ch2);
  279. break;
  280. default:
  281. /* Treat any other char as just itself */
  282. break;
  283. }
  284. default:
  285. break;
  286. }
  287. return (ch);
  288. }
  289. static int
  290. gdbchar (int desc)
  291. {
  292. unsigned char fromgdb;
  293. if (read (desc, &fromgdb, 1) != 1)
  294. return -1;
  295. else
  296. return fromgdb;
  297. }
  298. /* Accept input from gdb and match with chars from fp (after skipping one
  299. blank) up until a \n is read from fp (which is not matched) */
  300. static void
  301. expect (FILE *fp)
  302. {
  303. int fromlog;
  304. int fromgdb;
  305. if ((fromlog = logchar (fp)) != ' ')
  306. {
  307. sync_error (fp, "Sync error during gdb read of leading blank", ' ',
  308. fromlog);
  309. }
  310. do
  311. {
  312. fromlog = logchar (fp);
  313. if (fromlog == EOL)
  314. break;
  315. fromgdb = gdbchar (remote_desc_in);
  316. if (fromgdb < 0)
  317. remote_error ("Error during read from gdb");
  318. }
  319. while (fromlog == fromgdb);
  320. if (fromlog != EOL)
  321. {
  322. sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
  323. fromgdb);
  324. }
  325. }
  326. /* Play data back to gdb from fp (after skipping leading blank) up until a
  327. \n is read from fp (which is discarded and not sent to gdb). */
  328. static void
  329. play (FILE *fp)
  330. {
  331. int fromlog;
  332. char ch;
  333. if ((fromlog = logchar (fp)) != ' ')
  334. {
  335. sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
  336. fromlog);
  337. }
  338. while ((fromlog = logchar (fp)) != EOL)
  339. {
  340. ch = fromlog;
  341. if (write (remote_desc_out, &ch, 1) != 1)
  342. remote_error ("Error during write to gdb");
  343. }
  344. }
  345. static void
  346. gdbreplay_version (void)
  347. {
  348. printf ("GNU gdbreplay %s%s\n"
  349. "Copyright (C) 2022 Free Software Foundation, Inc.\n"
  350. "gdbreplay is free software, covered by "
  351. "the GNU General Public License.\n"
  352. "This gdbreplay was configured as \"%s\"\n",
  353. PKGVERSION, version, host_name);
  354. }
  355. static void
  356. gdbreplay_usage (FILE *stream)
  357. {
  358. fprintf (stream, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
  359. if (REPORT_BUGS_TO[0] && stream == stdout)
  360. fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
  361. }
  362. /* Main function. This is called by the real "main" function,
  363. wrapped in a TRY_CATCH that handles any uncaught exceptions. */
  364. static void ATTRIBUTE_NORETURN
  365. captured_main (int argc, char *argv[])
  366. {
  367. FILE *fp;
  368. int ch;
  369. if (argc >= 2 && strcmp (argv[1], "--version") == 0)
  370. {
  371. gdbreplay_version ();
  372. exit (0);
  373. }
  374. if (argc >= 2 && strcmp (argv[1], "--help") == 0)
  375. {
  376. gdbreplay_usage (stdout);
  377. exit (0);
  378. }
  379. if (argc < 3)
  380. {
  381. gdbreplay_usage (stderr);
  382. exit (1);
  383. }
  384. fp = fopen (argv[1], "r");
  385. if (fp == NULL)
  386. {
  387. perror_with_name (argv[1]);
  388. }
  389. remote_open (argv[2]);
  390. while ((ch = logchar (fp)) != EOF)
  391. {
  392. switch (ch)
  393. {
  394. case 'w':
  395. /* data sent from gdb to gdbreplay, accept and match it */
  396. expect (fp);
  397. break;
  398. case 'r':
  399. /* data sent from gdbreplay to gdb, play it */
  400. play (fp);
  401. break;
  402. case 'c':
  403. /* Command executed by gdb */
  404. while ((ch = logchar (fp)) != EOL);
  405. break;
  406. }
  407. }
  408. remote_close ();
  409. exit (0);
  410. }
  411. int
  412. main (int argc, char *argv[])
  413. {
  414. try
  415. {
  416. captured_main (argc, argv);
  417. }
  418. catch (const gdb_exception &exception)
  419. {
  420. if (exception.reason == RETURN_ERROR)
  421. {
  422. fflush (stdout);
  423. fprintf (stderr, "%s\n", exception.what ());
  424. }
  425. exit (1);
  426. }
  427. gdb_assert_not_reached ("captured_main should never return");
  428. }