1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996 Free Software Foundation, Inc.
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>
34 /* Sort of a hack... */
37 static int remote_desc;
39 /* Print the system error message for errno, and also mention STRING
40 as the file name for which the error was encountered.
41 Then return to command level. */
44 perror_with_name (string)
49 extern char *sys_errlist[];
55 err = (errno < sys_nerr) ? sys_errlist[errno] : "unknown error";
56 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
57 strcpy (combined, string);
58 strcat (combined, ": ");
59 strcat (combined, err);
60 fprintf (stderr, "\n%s.\n", combined);
66 sync_error (fp, desc, expect, got)
72 fprintf (stderr, "\n%s\n", desc);
73 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
74 ftell (fp), expect, got);
85 /* Open a connection to a remote debugger.
86 NAME is the filename used for communication. */
92 extern char *strchr ();
94 if (!strchr (name, ':'))
96 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
104 struct sockaddr_in sockaddr;
106 struct protoent *protoent;
109 port_str = strchr (name, ':');
111 port = atoi (port_str + 1);
113 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
115 perror_with_name ("Can't open socket");
117 /* Allow rapid reuse of this port. */
119 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
122 sockaddr.sin_family = PF_INET;
123 sockaddr.sin_port = htons (port);
124 sockaddr.sin_addr.s_addr = INADDR_ANY;
126 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
127 || listen (tmp_desc, 1))
128 perror_with_name ("Can't bind address");
130 tmp = sizeof (sockaddr);
131 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
132 if (remote_desc == -1)
133 perror_with_name ("Accept failed");
135 protoent = getprotobyname ("tcp");
137 perror_with_name ("getprotobyname");
139 /* Enable TCP keep alive process. */
141 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
143 /* Tell TCP not to delay small packets. This greatly speeds up
144 interactive response. */
146 setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY,
147 (char *) &tmp, sizeof (tmp));
149 close (tmp_desc); /* No longer need this */
151 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbreplay simply
152 exits when the remote side dies. */
155 fcntl (remote_desc, F_SETFL, FASYNC);
157 fprintf (stderr, "Replay logfile using %s\n", name);
165 if (ch >= '0' && ch <= '9')
169 if (ch >= 'A' && ch <= 'F')
171 return (ch - 'A' + 10);
173 if (ch >= 'a' && ch <= 'f')
175 return (ch - 'a' + 10);
177 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
227 ch = tohex (ch2) << 4;
234 /* Treat any other char as just itself */
243 /* Accept input from gdb and match with chars from fp (after skipping one
244 blank) up until a \n is read from fp (which is not matched) */
251 unsigned char fromgdb;
253 if ((fromlog = logchar (fp)) != ' ')
255 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
260 fromlog = logchar (fp);
265 read (remote_desc, &fromgdb, 1);
267 while (fromlog == fromgdb);
270 sync_error (fp, "Sync error during read of gdb packet", fromlog,
275 /* Play data back to gdb from fp (after skipping leading blank) up until a
276 \n is read from fp (which is discarded and not sent to gdb). */
285 if ((fromlog = logchar (fp)) != ' ')
287 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
290 while ((fromlog = logchar (fp)) != EOL)
293 write (remote_desc, &ch, 1);
307 fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
311 fp = fopen (argv[1], "r");
314 perror_with_name (argv[1]);
316 remote_open (argv[2]);
317 while ((ch = logchar (fp)) != EOF)
322 /* data sent from gdb to gdbreplay, accept and match it */
326 /* data sent from gdbreplay to gdb, play it */
330 /* Command executed by gdb */
331 while ((ch = logchar (fp)) != EOL);