1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2013 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/>. */
21 #include "build-gnulib-gdbserver/config.h"
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
56 #if HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
69 #ifndef HAVE_SOCKLEN_T
70 typedef int socklen_t;
73 /* Sort of a hack... */
76 static int remote_desc;
81 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
84 #define errno (GetLastError ())
87 strerror (DWORD error)
89 static char buf[1024];
91 DWORD lasterr = GetLastError ();
92 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
93 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
96 0, /* Default language */
102 /* If there is an \r\n appended, zap it. */
104 && msgbuf[chars - 2] == '\r'
105 && msgbuf[chars - 1] == '\n')
111 if (chars > ((COUNTOF (buf)) - 1))
113 chars = COUNTOF (buf) - 1;
117 wcstombs (buf, msgbuf, chars + 1);
121 sprintf (buf, "unknown win32 error (%ld)", error);
123 SetLastError (lasterr);
127 #endif /* __MINGW32CE__ */
129 /* Print the system error message for errno, and also mention STRING
130 as the file name for which the error was encountered.
131 Then return to command level. */
134 perror_with_name (const char *string)
142 err = strerror (errno);
144 err = "unknown error";
146 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
147 strcpy (combined, string);
148 strcat (combined, ": ");
149 strcat (combined, err);
150 fprintf (stderr, "\n%s.\n", combined);
156 sync_error (FILE *fp, char *desc, int expect, int got)
158 fprintf (stderr, "\n%s\n", desc);
159 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
160 ftell (fp), expect, got);
166 remote_error (const char *desc)
168 fprintf (stderr, "\n%s\n", desc);
177 closesocket (remote_desc);
183 /* Open a connection to a remote debugger.
184 NAME is the filename used for communication. */
187 remote_open (char *name)
189 if (!strchr (name, ':'))
191 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
198 static int winsock_initialized;
202 struct sockaddr_in sockaddr;
206 port_str = strchr (name, ':');
208 port = atoi (port_str + 1);
211 if (!winsock_initialized)
215 WSAStartup (MAKEWORD (1, 0), &wsad);
216 winsock_initialized = 1;
220 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
222 perror_with_name ("Can't open socket");
224 /* Allow rapid reuse of this port. */
226 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
229 sockaddr.sin_family = PF_INET;
230 sockaddr.sin_port = htons (port);
231 sockaddr.sin_addr.s_addr = INADDR_ANY;
233 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
234 || listen (tmp_desc, 1))
235 perror_with_name ("Can't bind address");
237 tmp = sizeof (sockaddr);
238 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
239 if (remote_desc == -1)
240 perror_with_name ("Accept failed");
242 /* Enable TCP keep alive process. */
244 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
245 (char *) &tmp, sizeof (tmp));
247 /* Tell TCP not to delay small packets. This greatly speeds up
248 interactive response. */
250 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
251 (char *) &tmp, sizeof (tmp));
254 close (tmp_desc); /* No longer need this */
256 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
257 gdbreplay simply exits when
258 the remote side dies. */
260 closesocket (tmp_desc); /* No longer need this */
264 #if defined(F_SETFL) && defined (FASYNC)
265 fcntl (remote_desc, F_SETFL, FASYNC);
268 fprintf (stderr, "Replay logfile using %s\n", name);
275 if (ch >= '0' && ch <= '9')
279 if (ch >= 'A' && ch <= 'F')
281 return (ch - 'A' + 10);
283 if (ch >= 'a' && ch <= 'f')
285 return (ch - 'a' + 10);
287 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
336 ch = tohex (ch2) << 4;
343 /* Treat any other char as just itself */
355 unsigned char fromgdb;
357 if (read (desc, &fromgdb, 1) != 1)
363 /* Accept input from gdb and match with chars from fp (after skipping one
364 blank) up until a \n is read from fp (which is not matched) */
372 if ((fromlog = logchar (fp)) != ' ')
374 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
379 fromlog = logchar (fp);
382 fromgdb = gdbchar (remote_desc);
384 remote_error ("Error during read from gdb");
386 while (fromlog == fromgdb);
390 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
395 /* Play data back to gdb from fp (after skipping leading blank) up until a
396 \n is read from fp (which is discarded and not sent to gdb). */
404 if ((fromlog = logchar (fp)) != ' ')
406 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
409 while ((fromlog = logchar (fp)) != EOL)
412 if (write (remote_desc, &ch, 1) != 1)
413 remote_error ("Error during write to gdb");
418 gdbreplay_version (void)
420 printf ("GNU gdbreplay %s%s\n"
421 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
422 "gdbreplay is free software, covered by "
423 "the GNU General Public License.\n"
424 "This gdbreplay was configured as \"%s\"\n",
425 PKGVERSION, version, host_name);
429 gdbreplay_usage (FILE *stream)
431 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
432 if (REPORT_BUGS_TO[0] && stream == stdout)
433 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
437 main (int argc, char *argv[])
442 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
444 gdbreplay_version ();
447 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
449 gdbreplay_usage (stdout);
455 gdbreplay_usage (stderr);
458 fp = fopen (argv[1], "r");
461 perror_with_name (argv[1]);
463 remote_open (argv[2]);
464 while ((ch = logchar (fp)) != EOF)
469 /* data sent from gdb to gdbreplay, accept and match it */
473 /* data sent from gdbreplay to gdb, play it */
477 /* Command executed by gdb */
478 while ((ch = logchar (fp)) != EOL);