1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998, 1999, 2000, 2002, 2003, 2005
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. */
26 #include <netinet/in.h>
27 #include <sys/socket.h>
29 #include <netinet/tcp.h>
45 #ifndef HAVE_SOCKLEN_T
46 typedef int socklen_t
;
49 /* Sort of a hack... */
52 static int remote_desc
;
54 /* Print the system error message for errno, and also mention STRING
55 as the file name for which the error was encountered.
56 Then return to command level. */
59 perror_with_name (char *string
)
67 err
= strerror (errno
);
69 err
= "unknown error";
71 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
72 strcpy (combined
, string
);
73 strcat (combined
, ": ");
74 strcat (combined
, err
);
75 fprintf (stderr
, "\n%s.\n", combined
);
81 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
83 fprintf (stderr
, "\n%s\n", desc
);
84 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
85 ftell (fp
), expect
, got
);
96 /* Open a connection to a remote debugger.
97 NAME is the filename used for communication. */
100 remote_open (char *name
)
102 if (!strchr (name
, ':'))
104 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
112 struct sockaddr_in sockaddr
;
116 port_str
= strchr (name
, ':');
118 port
= atoi (port_str
+ 1);
120 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
122 perror_with_name ("Can't open socket");
124 /* Allow rapid reuse of this port. */
126 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
129 sockaddr
.sin_family
= PF_INET
;
130 sockaddr
.sin_port
= htons (port
);
131 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
133 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
134 || listen (tmp_desc
, 1))
135 perror_with_name ("Can't bind address");
137 tmp
= sizeof (sockaddr
);
138 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
139 if (remote_desc
== -1)
140 perror_with_name ("Accept failed");
142 /* Enable TCP keep alive process. */
144 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
146 /* Tell TCP not to delay small packets. This greatly speeds up
147 interactive response. */
149 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
150 (char *) &tmp
, sizeof (tmp
));
152 close (tmp_desc
); /* No longer need this */
154 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbreplay simply
155 exits when the remote side dies. */
158 fcntl (remote_desc
, F_SETFL
, FASYNC
);
160 fprintf (stderr
, "Replay logfile using %s\n", name
);
167 if (ch
>= '0' && ch
<= '9')
171 if (ch
>= 'A' && ch
<= 'F')
173 return (ch
- 'A' + 10);
175 if (ch
>= 'a' && ch
<= 'f')
177 return (ch
- 'a' + 10);
179 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
228 ch
= tohex (ch2
) << 4;
235 /* Treat any other char as just itself */
244 /* Accept input from gdb and match with chars from fp (after skipping one
245 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). */
284 if ((fromlog
= logchar (fp
)) != ' ')
286 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
289 while ((fromlog
= logchar (fp
)) != EOL
)
292 write (remote_desc
, &ch
, 1);
297 main (int argc
, char *argv
[])
304 fprintf (stderr
, "Usage: gdbreplay <logfile> <host:port>\n");
308 fp
= fopen (argv
[1], "r");
311 perror_with_name (argv
[1]);
313 remote_open (argv
[2]);
314 while ((ch
= logchar (fp
)) != EOF
)
319 /* data sent from gdb to gdbreplay, accept and match it */
323 /* data sent from gdbreplay to gdb, play it */
327 /* Command executed by gdb */
328 while ((ch
= logchar (fp
)) != EOL
);