1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998-2000, 2002-2003, 2005-2012 Free Software
4 Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #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 /* Version information, from version.c. */
77 extern const char version[];
78 extern const char host_name[];
80 static int remote_desc;
85 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
88 #define errno (GetLastError ())
91 strerror (DWORD error)
93 static char buf[1024];
95 DWORD lasterr = GetLastError ();
96 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
97 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
100 0, /* Default language */
106 /* If there is an \r\n appended, zap it. */
108 && msgbuf[chars - 2] == '\r'
109 && msgbuf[chars - 1] == '\n')
115 if (chars > ((COUNTOF (buf)) - 1))
117 chars = COUNTOF (buf) - 1;
121 wcstombs (buf, msgbuf, chars + 1);
125 sprintf (buf, "unknown win32 error (%ld)", error);
127 SetLastError (lasterr);
131 #endif /* __MINGW32CE__ */
133 /* Print the system error message for errno, and also mention STRING
134 as the file name for which the error was encountered.
135 Then return to command level. */
138 perror_with_name (const char *string)
146 err = strerror (errno);
148 err = "unknown error";
150 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
151 strcpy (combined, string);
152 strcat (combined, ": ");
153 strcat (combined, err);
154 fprintf (stderr, "\n%s.\n", combined);
160 sync_error (FILE *fp, char *desc, int expect, int got)
162 fprintf (stderr, "\n%s\n", desc);
163 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
164 ftell (fp), expect, got);
170 remote_error (const char *desc)
172 fprintf (stderr, "\n%s\n", desc);
181 closesocket (remote_desc);
187 /* Open a connection to a remote debugger.
188 NAME is the filename used for communication. */
191 remote_open (char *name)
193 if (!strchr (name, ':'))
195 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
202 static int winsock_initialized;
206 struct sockaddr_in sockaddr;
210 port_str = strchr (name, ':');
212 port = atoi (port_str + 1);
215 if (!winsock_initialized)
219 WSAStartup (MAKEWORD (1, 0), &wsad);
220 winsock_initialized = 1;
224 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
226 perror_with_name ("Can't open socket");
228 /* Allow rapid reuse of this port. */
230 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
233 sockaddr.sin_family = PF_INET;
234 sockaddr.sin_port = htons (port);
235 sockaddr.sin_addr.s_addr = INADDR_ANY;
237 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
238 || listen (tmp_desc, 1))
239 perror_with_name ("Can't bind address");
241 tmp = sizeof (sockaddr);
242 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
243 if (remote_desc == -1)
244 perror_with_name ("Accept failed");
246 /* Enable TCP keep alive process. */
248 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
249 (char *) &tmp, sizeof (tmp));
251 /* Tell TCP not to delay small packets. This greatly speeds up
252 interactive response. */
254 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
255 (char *) &tmp, sizeof (tmp));
258 close (tmp_desc); /* No longer need this */
260 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
261 gdbreplay simply exits when
262 the remote side dies. */
264 closesocket (tmp_desc); /* No longer need this */
268 #if defined(F_SETFL) && defined (FASYNC)
269 fcntl (remote_desc, F_SETFL, FASYNC);
272 fprintf (stderr, "Replay logfile using %s\n", name);
279 if (ch >= '0' && ch <= '9')
283 if (ch >= 'A' && ch <= 'F')
285 return (ch - 'A' + 10);
287 if (ch >= 'a' && ch <= 'f')
289 return (ch - 'a' + 10);
291 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
340 ch = tohex (ch2) << 4;
347 /* Treat any other char as just itself */
359 unsigned char fromgdb;
361 if (read (desc, &fromgdb, 1) != 1)
367 /* Accept input from gdb and match with chars from fp (after skipping one
368 blank) up until a \n is read from fp (which is not matched) */
376 if ((fromlog = logchar (fp)) != ' ')
378 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
383 fromlog = logchar (fp);
386 fromgdb = gdbchar (remote_desc);
388 remote_error ("Error during read from gdb");
390 while (fromlog == fromgdb);
394 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
399 /* Play data back to gdb from fp (after skipping leading blank) up until a
400 \n is read from fp (which is discarded and not sent to gdb). */
408 if ((fromlog = logchar (fp)) != ' ')
410 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
413 while ((fromlog = logchar (fp)) != EOL)
416 if (write (remote_desc, &ch, 1) != 1)
417 remote_error ("Error during write to gdb");
422 gdbreplay_version (void)
424 printf ("GNU gdbreplay %s%s\n"
425 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
426 "gdbreplay is free software, covered by "
427 "the GNU General Public License.\n"
428 "This gdbreplay was configured as \"%s\"\n",
429 PKGVERSION, version, host_name);
433 gdbreplay_usage (FILE *stream)
435 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
436 if (REPORT_BUGS_TO[0] && stream == stdout)
437 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
441 main (int argc, char *argv[])
446 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
448 gdbreplay_version ();
451 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
453 gdbreplay_usage (stdout);
459 gdbreplay_usage (stderr);
462 fp = fopen (argv[1], "r");
465 perror_with_name (argv[1]);
467 remote_open (argv[2]);
468 while ((ch = logchar (fp)) != EOF)
473 /* data sent from gdb to gdbreplay, accept and match it */
477 /* data sent from gdbreplay to gdb, play it */
481 /* Command executed by gdb */
482 while ((ch = logchar (fp)) != EOL);