1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998, 1999, 2000, 2002, 2003, 2005, 2006, 2007
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
49 #if HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
57 #ifndef HAVE_SOCKLEN_T
58 typedef int socklen_t;
61 /* Sort of a hack... */
64 static int remote_desc;
66 /* Print the system error message for errno, and also mention STRING
67 as the file name for which the error was encountered.
68 Then return to command level. */
71 perror_with_name (char *string)
79 err = strerror (errno);
81 err = "unknown error";
83 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
84 strcpy (combined, string);
85 strcat (combined, ": ");
86 strcat (combined, err);
87 fprintf (stderr, "\n%s.\n", combined);
93 sync_error (FILE *fp, char *desc, int expect, int got)
95 fprintf (stderr, "\n%s\n", desc);
96 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
97 ftell (fp), expect, got);
106 closesocket (remote_desc);
112 /* Open a connection to a remote debugger.
113 NAME is the filename used for communication. */
116 remote_open (char *name)
118 if (!strchr (name, ':'))
120 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
127 static int winsock_initialized;
131 struct sockaddr_in sockaddr;
135 port_str = strchr (name, ':');
137 port = atoi (port_str + 1);
140 if (!winsock_initialized)
144 WSAStartup (MAKEWORD (1, 0), &wsad);
145 winsock_initialized = 1;
149 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
151 perror_with_name ("Can't open socket");
153 /* Allow rapid reuse of this port. */
155 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
158 sockaddr.sin_family = PF_INET;
159 sockaddr.sin_port = htons (port);
160 sockaddr.sin_addr.s_addr = INADDR_ANY;
162 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
163 || listen (tmp_desc, 1))
164 perror_with_name ("Can't bind address");
166 tmp = sizeof (sockaddr);
167 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
168 if (remote_desc == -1)
169 perror_with_name ("Accept failed");
171 /* Enable TCP keep alive process. */
173 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
175 /* Tell TCP not to delay small packets. This greatly speeds up
176 interactive response. */
178 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
179 (char *) &tmp, sizeof (tmp));
181 close (tmp_desc); /* No longer need this */
184 close (tmp_desc); /* No longer need this */
186 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbreplay simply
187 exits when the remote side dies. */
189 closesocket (tmp_desc); /* No longer need this */
193 #if defined(F_SETFL) && defined (FASYNC)
194 fcntl (remote_desc, F_SETFL, FASYNC);
197 fprintf (stderr, "Replay logfile using %s\n", name);
204 if (ch >= '0' && ch <= '9')
208 if (ch >= 'A' && ch <= 'F')
210 return (ch - 'A' + 10);
212 if (ch >= 'a' && ch <= 'f')
214 return (ch - 'a' + 10);
216 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
265 ch = tohex (ch2) << 4;
272 /* Treat any other char as just itself */
281 /* Accept input from gdb and match with chars from fp (after skipping one
282 blank) up until a \n is read from fp (which is not matched) */
288 unsigned char fromgdb;
290 if ((fromlog = logchar (fp)) != ' ')
292 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
297 fromlog = logchar (fp);
302 read (remote_desc, &fromgdb, 1);
304 while (fromlog == fromgdb);
307 sync_error (fp, "Sync error during read of gdb packet", fromlog,
312 /* Play data back to gdb from fp (after skipping leading blank) up until a
313 \n is read from fp (which is discarded and not sent to gdb). */
321 if ((fromlog = logchar (fp)) != ' ')
323 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
326 while ((fromlog = logchar (fp)) != EOL)
329 write (remote_desc, &ch, 1);
334 main (int argc, char *argv[])
341 fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
345 fp = fopen (argv[1], "r");
348 perror_with_name (argv[1]);
350 remote_open (argv[2]);
351 while ((ch = logchar (fp)) != EOF)
356 /* data sent from gdb to gdbreplay, accept and match it */
360 /* data sent from gdbreplay to gdb, play it */
364 /* Command executed by gdb */
365 while ((ch = logchar (fp)) != EOL);