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"
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
54 #if HAVE_NETINET_TCP_H
55 #include <netinet/tcp.h>
67 #ifndef HAVE_SOCKLEN_T
68 typedef int socklen_t;
71 /* Sort of a hack... */
74 static int remote_desc;
79 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
82 #define errno (GetLastError ())
85 strerror (DWORD error)
87 static char buf[1024];
89 DWORD lasterr = GetLastError ();
90 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
91 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
94 0, /* Default language */
100 /* If there is an \r\n appended, zap it. */
102 && msgbuf[chars - 2] == '\r'
103 && msgbuf[chars - 1] == '\n')
109 if (chars > ((COUNTOF (buf)) - 1))
111 chars = COUNTOF (buf) - 1;
115 wcstombs (buf, msgbuf, chars + 1);
119 sprintf (buf, "unknown win32 error (%ld)", error);
121 SetLastError (lasterr);
125 #endif /* __MINGW32CE__ */
127 /* Print the system error message for errno, and also mention STRING
128 as the file name for which the error was encountered.
129 Then return to command level. */
132 perror_with_name (const char *string)
140 err = strerror (errno);
142 err = "unknown error";
144 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
145 strcpy (combined, string);
146 strcat (combined, ": ");
147 strcat (combined, err);
148 fprintf (stderr, "\n%s.\n", combined);
154 sync_error (FILE *fp, char *desc, int expect, int got)
156 fprintf (stderr, "\n%s\n", desc);
157 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
158 ftell (fp), expect, got);
164 remote_error (const char *desc)
166 fprintf (stderr, "\n%s\n", desc);
175 closesocket (remote_desc);
181 /* Open a connection to a remote debugger.
182 NAME is the filename used for communication. */
185 remote_open (char *name)
187 if (!strchr (name, ':'))
189 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
196 static int winsock_initialized;
200 struct sockaddr_in sockaddr;
204 port_str = strchr (name, ':');
206 port = atoi (port_str + 1);
209 if (!winsock_initialized)
213 WSAStartup (MAKEWORD (1, 0), &wsad);
214 winsock_initialized = 1;
218 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
220 perror_with_name ("Can't open socket");
222 /* Allow rapid reuse of this port. */
224 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
227 sockaddr.sin_family = PF_INET;
228 sockaddr.sin_port = htons (port);
229 sockaddr.sin_addr.s_addr = INADDR_ANY;
231 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
232 || listen (tmp_desc, 1))
233 perror_with_name ("Can't bind address");
235 tmp = sizeof (sockaddr);
236 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
237 if (remote_desc == -1)
238 perror_with_name ("Accept failed");
240 /* Enable TCP keep alive process. */
242 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
243 (char *) &tmp, sizeof (tmp));
245 /* Tell TCP not to delay small packets. This greatly speeds up
246 interactive response. */
248 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
249 (char *) &tmp, sizeof (tmp));
252 close (tmp_desc); /* No longer need this */
254 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
255 gdbreplay simply exits when
256 the remote side dies. */
258 closesocket (tmp_desc); /* No longer need this */
262 #if defined(F_SETFL) && defined (FASYNC)
263 fcntl (remote_desc, F_SETFL, FASYNC);
266 fprintf (stderr, "Replay logfile using %s\n", name);
273 if (ch >= '0' && ch <= '9')
277 if (ch >= 'A' && ch <= 'F')
279 return (ch - 'A' + 10);
281 if (ch >= 'a' && ch <= 'f')
283 return (ch - 'a' + 10);
285 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
334 ch = tohex (ch2) << 4;
341 /* Treat any other char as just itself */
353 unsigned char fromgdb;
355 if (read (desc, &fromgdb, 1) != 1)
361 /* Accept input from gdb and match with chars from fp (after skipping one
362 blank) up until a \n is read from fp (which is not matched) */
370 if ((fromlog = logchar (fp)) != ' ')
372 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
377 fromlog = logchar (fp);
380 fromgdb = gdbchar (remote_desc);
382 remote_error ("Error during read from gdb");
384 while (fromlog == fromgdb);
388 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
393 /* Play data back to gdb from fp (after skipping leading blank) up until a
394 \n is read from fp (which is discarded and not sent to gdb). */
402 if ((fromlog = logchar (fp)) != ' ')
404 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
407 while ((fromlog = logchar (fp)) != EOL)
410 if (write (remote_desc, &ch, 1) != 1)
411 remote_error ("Error during write to gdb");
416 gdbreplay_version (void)
418 printf ("GNU gdbreplay %s%s\n"
419 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
420 "gdbreplay is free software, covered by "
421 "the GNU General Public License.\n"
422 "This gdbreplay was configured as \"%s\"\n",
423 PKGVERSION, version, host_name);
427 gdbreplay_usage (FILE *stream)
429 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
430 if (REPORT_BUGS_TO[0] && stream == stdout)
431 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
435 main (int argc, char *argv[])
440 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
442 gdbreplay_version ();
445 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
447 gdbreplay_usage (stdout);
453 gdbreplay_usage (stderr);
456 fp = fopen (argv[1], "r");
459 perror_with_name (argv[1]);
461 remote_open (argv[2]);
462 while ((ch = logchar (fp)) != EOF)
467 /* data sent from gdb to gdbreplay, accept and match it */
471 /* data sent from gdbreplay to gdb, play it */
475 /* Command executed by gdb */
476 while ((ch = logchar (fp)) != EOL);