1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998, 1999, 2000, 2002, 2003, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
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/>. */
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>
65 #ifndef HAVE_SOCKLEN_T
66 typedef int socklen_t;
69 /* Sort of a hack... */
72 static int remote_desc;
77 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
80 #define errno (GetLastError ())
83 strerror (DWORD error)
85 static char buf[1024];
87 DWORD lasterr = GetLastError ();
88 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
89 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
92 0, /* Default language */
98 /* If there is an \r\n appended, zap it. */
100 && msgbuf[chars - 2] == '\r'
101 && msgbuf[chars - 1] == '\n')
107 if (chars > ((COUNTOF (buf)) - 1))
109 chars = COUNTOF (buf) - 1;
113 wcstombs (buf, msgbuf, chars + 1);
117 sprintf (buf, "unknown win32 error (%ld)", error);
119 SetLastError (lasterr);
123 #endif /* __MINGW32CE__ */
125 /* Print the system error message for errno, and also mention STRING
126 as the file name for which the error was encountered.
127 Then return to command level. */
130 perror_with_name (char *string)
138 err = strerror (errno);
140 err = "unknown error";
142 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
143 strcpy (combined, string);
144 strcat (combined, ": ");
145 strcat (combined, err);
146 fprintf (stderr, "\n%s.\n", combined);
152 sync_error (FILE *fp, char *desc, int expect, int got)
154 fprintf (stderr, "\n%s\n", desc);
155 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
156 ftell (fp), expect, got);
165 closesocket (remote_desc);
171 /* Open a connection to a remote debugger.
172 NAME is the filename used for communication. */
175 remote_open (char *name)
177 if (!strchr (name, ':'))
179 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
186 static int winsock_initialized;
190 struct sockaddr_in sockaddr;
194 port_str = strchr (name, ':');
196 port = atoi (port_str + 1);
199 if (!winsock_initialized)
203 WSAStartup (MAKEWORD (1, 0), &wsad);
204 winsock_initialized = 1;
208 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
210 perror_with_name ("Can't open socket");
212 /* Allow rapid reuse of this port. */
214 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
217 sockaddr.sin_family = PF_INET;
218 sockaddr.sin_port = htons (port);
219 sockaddr.sin_addr.s_addr = INADDR_ANY;
221 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
222 || listen (tmp_desc, 1))
223 perror_with_name ("Can't bind address");
225 tmp = sizeof (sockaddr);
226 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
227 if (remote_desc == -1)
228 perror_with_name ("Accept failed");
230 /* Enable TCP keep alive process. */
232 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
234 /* Tell TCP not to delay small packets. This greatly speeds up
235 interactive response. */
237 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
238 (char *) &tmp, sizeof (tmp));
241 close (tmp_desc); /* No longer need this */
243 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbreplay simply
244 exits when the remote side dies. */
246 closesocket (tmp_desc); /* No longer need this */
250 #if defined(F_SETFL) && defined (FASYNC)
251 fcntl (remote_desc, F_SETFL, FASYNC);
254 fprintf (stderr, "Replay logfile using %s\n", name);
261 if (ch >= '0' && ch <= '9')
265 if (ch >= 'A' && ch <= 'F')
267 return (ch - 'A' + 10);
269 if (ch >= 'a' && ch <= 'f')
271 return (ch - 'a' + 10);
273 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
322 ch = tohex (ch2) << 4;
329 /* Treat any other char as just itself */
338 /* Accept input from gdb and match with chars from fp (after skipping one
339 blank) up until a \n is read from fp (which is not matched) */
345 unsigned char fromgdb;
347 if ((fromlog = logchar (fp)) != ' ')
349 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
354 fromlog = logchar (fp);
359 read (remote_desc, &fromgdb, 1);
361 while (fromlog == fromgdb);
364 sync_error (fp, "Sync error during read of gdb packet", fromlog,
369 /* Play data back to gdb from fp (after skipping leading blank) up until a
370 \n is read from fp (which is discarded and not sent to gdb). */
378 if ((fromlog = logchar (fp)) != ' ')
380 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
383 while ((fromlog = logchar (fp)) != EOL)
386 write (remote_desc, &ch, 1);
391 main (int argc, char *argv[])
398 fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
402 fp = fopen (argv[1], "r");
405 perror_with_name (argv[1]);
407 remote_open (argv[2]);
408 while ((ch = logchar (fp)) != EOF)
413 /* data sent from gdb to gdbreplay, accept and match it */
417 /* data sent from gdbreplay to gdb, play it */
421 /* Command executed by gdb */
422 while ((ch = logchar (fp)) != EOL);