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>
51 #ifndef HAVE_SOCKLEN_T
52 typedef int socklen_t;
55 /* Sort of a hack... */
58 static int remote_desc;
63 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
66 #define errno (GetLastError ())
69 strerror (DWORD error)
71 static char buf[1024];
73 DWORD lasterr = GetLastError ();
74 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
75 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
78 0, /* Default language */
84 /* If there is an \r\n appended, zap it. */
86 && msgbuf[chars - 2] == '\r'
87 && msgbuf[chars - 1] == '\n')
93 if (chars > ((COUNTOF (buf)) - 1))
95 chars = COUNTOF (buf) - 1;
99 wcstombs (buf, msgbuf, chars + 1);
103 sprintf (buf, "unknown win32 error (%ld)", error);
105 SetLastError (lasterr);
109 #endif /* __MINGW32CE__ */
112 sync_error (FILE *fp, const char *desc, int expect, int got)
114 fprintf (stderr, "\n%s\n", desc);
115 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
116 ftell (fp), expect, got);
122 remote_error (const char *desc)
124 fprintf (stderr, "\n%s\n", desc);
133 closesocket (remote_desc);
139 /* Open a connection to a remote debugger.
140 NAME is the filename used for communication. */
143 remote_open (char *name)
145 if (!strchr (name, ':'))
147 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
154 static int winsock_initialized;
158 struct sockaddr_in sockaddr;
162 port_str = strchr (name, ':');
164 port = atoi (port_str + 1);
167 if (!winsock_initialized)
171 WSAStartup (MAKEWORD (1, 0), &wsad);
172 winsock_initialized = 1;
176 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
178 perror_with_name ("Can't open socket");
180 /* Allow rapid reuse of this port. */
182 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
185 sockaddr.sin_family = PF_INET;
186 sockaddr.sin_port = htons (port);
187 sockaddr.sin_addr.s_addr = INADDR_ANY;
189 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
190 || listen (tmp_desc, 1))
191 perror_with_name ("Can't bind address");
193 tmp = sizeof (sockaddr);
194 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
195 if (remote_desc == -1)
196 perror_with_name ("Accept failed");
198 /* Enable TCP keep alive process. */
200 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
201 (char *) &tmp, sizeof (tmp));
203 /* Tell TCP not to delay small packets. This greatly speeds up
204 interactive response. */
206 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
207 (char *) &tmp, sizeof (tmp));
210 close (tmp_desc); /* No longer need this */
212 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
213 gdbreplay simply exits when
214 the remote side dies. */
216 closesocket (tmp_desc); /* No longer need this */
220 #if defined(F_SETFL) && defined (FASYNC)
221 fcntl (remote_desc, F_SETFL, FASYNC);
224 fprintf (stderr, "Replay logfile using %s\n", name);
231 if (ch >= '0' && ch <= '9')
235 if (ch >= 'A' && ch <= 'F')
237 return (ch - 'A' + 10);
239 if (ch >= 'a' && ch <= 'f')
241 return (ch - 'a' + 10);
243 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
292 ch = fromhex (ch2) << 4;
299 /* Treat any other char as just itself */
311 unsigned char fromgdb;
313 if (read (desc, &fromgdb, 1) != 1)
319 /* Accept input from gdb and match with chars from fp (after skipping one
320 blank) up until a \n is read from fp (which is not matched) */
328 if ((fromlog = logchar (fp)) != ' ')
330 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
335 fromlog = logchar (fp);
338 fromgdb = gdbchar (remote_desc);
340 remote_error ("Error during read from gdb");
342 while (fromlog == fromgdb);
346 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
351 /* Play data back to gdb from fp (after skipping leading blank) up until a
352 \n is read from fp (which is discarded and not sent to gdb). */
360 if ((fromlog = logchar (fp)) != ' ')
362 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
365 while ((fromlog = logchar (fp)) != EOL)
368 if (write (remote_desc, &ch, 1) != 1)
369 remote_error ("Error during write to gdb");
374 gdbreplay_version (void)
376 printf ("GNU gdbreplay %s%s\n"
377 "Copyright (C) 2018 Free Software Foundation, Inc.\n"
378 "gdbreplay is free software, covered by "
379 "the GNU General Public License.\n"
380 "This gdbreplay was configured as \"%s\"\n",
381 PKGVERSION, version, host_name);
385 gdbreplay_usage (FILE *stream)
387 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
388 if (REPORT_BUGS_TO[0] && stream == stdout)
389 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
392 /* Main function. This is called by the real "main" function,
393 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
395 static void ATTRIBUTE_NORETURN
396 captured_main (int argc, char *argv[])
401 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
403 gdbreplay_version ();
406 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
408 gdbreplay_usage (stdout);
414 gdbreplay_usage (stderr);
417 fp = fopen (argv[1], "r");
420 perror_with_name (argv[1]);
422 remote_open (argv[2]);
423 while ((ch = logchar (fp)) != EOF)
428 /* data sent from gdb to gdbreplay, accept and match it */
432 /* data sent from gdbreplay to gdb, play it */
436 /* Command executed by gdb */
437 while ((ch = logchar (fp)) != EOL);
446 main (int argc, char *argv[])
450 captured_main (argc, argv);
452 CATCH (exception, RETURN_MASK_ALL)
454 if (exception.reason == RETURN_ERROR)
457 fprintf (stderr, "%s\n", exception.message);
464 gdb_assert_not_reached ("captured_main should never return");