1 /* Replay a remote debug session logfile for GDB.
2 Copyright 1996, 1998, 1999, 2000 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include <netinet/in.h>
26 #include <sys/socket.h>
28 #include <netinet/tcp.h>
40 /* Sort of a hack... */
43 static int remote_desc;
45 /* Print the system error message for errno, and also mention STRING
46 as the file name for which the error was encountered.
47 Then return to command level. */
50 perror_with_name (char *string)
54 extern char *sys_errlist[];
60 err = (errno < sys_nerr) ? sys_errlist[errno] : "unknown error";
61 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
62 strcpy (combined, string);
63 strcat (combined, ": ");
64 strcat (combined, err);
65 fprintf (stderr, "\n%s.\n", combined);
71 sync_error (FILE *fp, char *desc, int expect, int got)
73 fprintf (stderr, "\n%s\n", desc);
74 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
75 ftell (fp), expect, got);
86 /* Open a connection to a remote debugger.
87 NAME is the filename used for communication. */
90 remote_open (char *name)
92 if (!strchr (name, ':'))
94 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
102 struct sockaddr_in sockaddr;
106 port_str = strchr (name, ':');
108 port = atoi (port_str + 1);
110 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
112 perror_with_name ("Can't open socket");
114 /* Allow rapid reuse of this port. */
116 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
119 sockaddr.sin_family = PF_INET;
120 sockaddr.sin_port = htons (port);
121 sockaddr.sin_addr.s_addr = INADDR_ANY;
123 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
124 || listen (tmp_desc, 1))
125 perror_with_name ("Can't bind address");
127 tmp = sizeof (sockaddr);
128 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
129 if (remote_desc == -1)
130 perror_with_name ("Accept failed");
132 /* Enable TCP keep alive process. */
134 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
136 /* Tell TCP not to delay small packets. This greatly speeds up
137 interactive response. */
139 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
140 (char *) &tmp, sizeof (tmp));
142 close (tmp_desc); /* No longer need this */
144 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbreplay simply
145 exits when the remote side dies. */
148 fcntl (remote_desc, F_SETFL, FASYNC);
150 fprintf (stderr, "Replay logfile using %s\n", name);
157 if (ch >= '0' && ch <= '9')
161 if (ch >= 'A' && ch <= 'F')
163 return (ch - 'A' + 10);
165 if (ch >= 'a' && ch <= 'f')
167 return (ch - 'a' + 10);
169 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
218 ch = tohex (ch2) << 4;
225 /* Treat any other char as just itself */
234 /* Accept input from gdb and match with chars from fp (after skipping one
235 blank) up until a \n is read from fp (which is not matched) */
241 unsigned char fromgdb;
243 if ((fromlog = logchar (fp)) != ' ')
245 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
250 fromlog = logchar (fp);
255 read (remote_desc, &fromgdb, 1);
257 while (fromlog == fromgdb);
260 sync_error (fp, "Sync error during read of gdb packet", fromlog,
265 /* Play data back to gdb from fp (after skipping leading blank) up until a
266 \n is read from fp (which is discarded and not sent to gdb). */
274 if ((fromlog = logchar (fp)) != ' ')
276 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
279 while ((fromlog = logchar (fp)) != EOL)
282 write (remote_desc, &ch, 1);
287 main (int argc, char *argv[])
294 fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
298 fp = fopen (argv[1], "r");
301 perror_with_name (argv[1]);
303 remote_open (argv[2]);
304 while ((ch = logchar (fp)) != EOF)
309 /* data sent from gdb to gdbreplay, accept and match it */
313 /* data sent from gdbreplay to gdb, play it */
317 /* Command executed by gdb */
318 while ((ch = logchar (fp)) != EOL);