1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
43 #if HAVE_NETINET_TCP_H
44 #include <netinet/tcp.h>
54 #ifndef HAVE_SOCKLEN_T
55 typedef int socklen_t;
58 /* Sort of a hack... */
61 static int remote_desc;
66 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
69 #define errno (GetLastError ())
72 strerror (DWORD error)
74 static char buf[1024];
76 DWORD lasterr = GetLastError ();
77 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
78 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
81 0, /* Default language */
87 /* If there is an \r\n appended, zap it. */
89 && msgbuf[chars - 2] == '\r'
90 && msgbuf[chars - 1] == '\n')
96 if (chars > ((COUNTOF (buf)) - 1))
98 chars = COUNTOF (buf) - 1;
102 wcstombs (buf, msgbuf, chars + 1);
106 sprintf (buf, "unknown win32 error (%ld)", error);
108 SetLastError (lasterr);
112 #endif /* __MINGW32CE__ */
115 sync_error (FILE *fp, const char *desc, int expect, int got)
117 fprintf (stderr, "\n%s\n", desc);
118 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
119 ftell (fp), expect, got);
125 remote_error (const char *desc)
127 fprintf (stderr, "\n%s\n", desc);
136 closesocket (remote_desc);
142 /* Open a connection to a remote debugger.
143 NAME is the filename used for communication. */
146 remote_open (char *name)
148 char *last_colon = strrchr (name, ':');
150 if (last_colon == NULL)
152 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
158 static int winsock_initialized;
163 struct addrinfo hint;
164 struct addrinfo *ainfo;
166 memset (&hint, 0, sizeof (hint));
167 /* Assume no prefix will be passed, therefore we should use
169 hint.ai_family = AF_UNSPEC;
170 hint.ai_socktype = SOCK_STREAM;
171 hint.ai_protocol = IPPROTO_TCP;
173 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
175 if (parsed.port_str.empty ())
176 error (_("Missing port on hostname '%s'"), name);
179 if (!winsock_initialized)
183 WSAStartup (MAKEWORD (1, 0), &wsad);
184 winsock_initialized = 1;
188 int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
193 fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
194 parsed.host_str.c_str (), parsed.port_str.c_str (),
200 scoped_free_addrinfo free_ainfo (ainfo);
204 for (p = ainfo; p != NULL; p = p->ai_next)
206 tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
213 perror_with_name ("Cannot open socket");
215 /* Allow rapid reuse of this port. */
217 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
220 switch (p->ai_family)
223 ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
226 ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
229 fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
233 if (bind (tmp_desc, p->ai_addr, p->ai_addrlen) != 0)
234 perror_with_name ("Can't bind address");
236 if (p->ai_socktype == SOCK_DGRAM)
237 remote_desc = tmp_desc;
240 struct sockaddr_storage sockaddr;
241 socklen_t sockaddrsize = sizeof (sockaddr);
242 char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
244 if (listen (tmp_desc, 1) != 0)
245 perror_with_name ("Can't listen on socket");
247 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr,
250 if (remote_desc == -1)
251 perror_with_name ("Accept failed");
253 /* Enable TCP keep alive process. */
255 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
256 (char *) &tmp, sizeof (tmp));
258 /* Tell TCP not to delay small packets. This greatly speeds up
259 interactive response. */
261 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
262 (char *) &tmp, sizeof (tmp));
264 if (getnameinfo ((struct sockaddr *) &sockaddr, sockaddrsize,
265 orig_host, sizeof (orig_host),
266 orig_port, sizeof (orig_port),
267 NI_NUMERICHOST | NI_NUMERICSERV) == 0)
269 fprintf (stderr, "Remote debugging from host %s, port %s\n",
270 orig_host, orig_port);
275 close (tmp_desc); /* No longer need this */
277 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
278 gdbreplay simply exits when
279 the remote side dies. */
281 closesocket (tmp_desc); /* No longer need this */
285 #if defined(F_SETFL) && defined (FASYNC)
286 fcntl (remote_desc, F_SETFL, FASYNC);
289 fprintf (stderr, "Replay logfile using %s\n", name);
296 if (ch >= '0' && ch <= '9')
300 if (ch >= 'A' && ch <= 'F')
302 return (ch - 'A' + 10);
304 if (ch >= 'a' && ch <= 'f')
306 return (ch - 'a' + 10);
308 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
357 ch = fromhex (ch2) << 4;
364 /* Treat any other char as just itself */
376 unsigned char fromgdb;
378 if (read (desc, &fromgdb, 1) != 1)
384 /* Accept input from gdb and match with chars from fp (after skipping one
385 blank) up until a \n is read from fp (which is not matched) */
393 if ((fromlog = logchar (fp)) != ' ')
395 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
400 fromlog = logchar (fp);
403 fromgdb = gdbchar (remote_desc);
405 remote_error ("Error during read from gdb");
407 while (fromlog == fromgdb);
411 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
416 /* Play data back to gdb from fp (after skipping leading blank) up until a
417 \n is read from fp (which is discarded and not sent to gdb). */
425 if ((fromlog = logchar (fp)) != ' ')
427 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
430 while ((fromlog = logchar (fp)) != EOL)
433 if (write (remote_desc, &ch, 1) != 1)
434 remote_error ("Error during write to gdb");
439 gdbreplay_version (void)
441 printf ("GNU gdbreplay %s%s\n"
442 "Copyright (C) 2018 Free Software Foundation, Inc.\n"
443 "gdbreplay is free software, covered by "
444 "the GNU General Public License.\n"
445 "This gdbreplay was configured as \"%s\"\n",
446 PKGVERSION, version, host_name);
450 gdbreplay_usage (FILE *stream)
452 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
453 if (REPORT_BUGS_TO[0] && stream == stdout)
454 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
457 /* Main function. This is called by the real "main" function,
458 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
460 static void ATTRIBUTE_NORETURN
461 captured_main (int argc, char *argv[])
466 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
468 gdbreplay_version ();
471 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
473 gdbreplay_usage (stdout);
479 gdbreplay_usage (stderr);
482 fp = fopen (argv[1], "r");
485 perror_with_name (argv[1]);
487 remote_open (argv[2]);
488 while ((ch = logchar (fp)) != EOF)
493 /* data sent from gdb to gdbreplay, accept and match it */
497 /* data sent from gdbreplay to gdb, play it */
501 /* Command executed by gdb */
502 while ((ch = logchar (fp)) != EOL);
511 main (int argc, char *argv[])
515 captured_main (argc, argv);
517 CATCH (exception, RETURN_MASK_ALL)
519 if (exception.reason == RETURN_ERROR)
522 fprintf (stderr, "%s\n", exception.message);
529 gdb_assert_not_reached ("captured_main should never return");