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. */
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
56 #if HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
67 #ifndef HAVE_SOCKLEN_T
68 typedef int socklen_t
;
71 /* Sort of a hack... */
74 static int remote_desc
;
79 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
82 #define errno (GetLastError ())
85 strerror (DWORD error
)
87 static char buf
[1024];
89 DWORD lasterr
= GetLastError ();
90 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
91 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
94 0, /* Default language */
100 /* If there is an \r\n appended, zap it. */
102 && msgbuf
[chars
- 2] == '\r'
103 && msgbuf
[chars
- 1] == '\n')
109 if (chars
> ((COUNTOF (buf
)) - 1))
111 chars
= COUNTOF (buf
) - 1;
115 wcstombs (buf
, msgbuf
, chars
+ 1);
119 sprintf (buf
, "unknown win32 error (%ld)", error
);
121 SetLastError (lasterr
);
125 #endif /* __MINGW32CE__ */
127 /* Print the system error message for errno, and also mention STRING
128 as the file name for which the error was encountered.
129 Then return to command level. */
132 perror_with_name (char *string
)
140 err
= strerror (errno
);
142 err
= "unknown error";
144 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
145 strcpy (combined
, string
);
146 strcat (combined
, ": ");
147 strcat (combined
, err
);
148 fprintf (stderr
, "\n%s.\n", combined
);
154 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
156 fprintf (stderr
, "\n%s\n", desc
);
157 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
158 ftell (fp
), expect
, got
);
167 closesocket (remote_desc
);
173 /* Open a connection to a remote debugger.
174 NAME is the filename used for communication. */
177 remote_open (char *name
)
179 if (!strchr (name
, ':'))
181 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
188 static int winsock_initialized
;
192 struct sockaddr_in sockaddr
;
196 port_str
= strchr (name
, ':');
198 port
= atoi (port_str
+ 1);
201 if (!winsock_initialized
)
205 WSAStartup (MAKEWORD (1, 0), &wsad
);
206 winsock_initialized
= 1;
210 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
212 perror_with_name ("Can't open socket");
214 /* Allow rapid reuse of this port. */
216 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
219 sockaddr
.sin_family
= PF_INET
;
220 sockaddr
.sin_port
= htons (port
);
221 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
223 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
224 || listen (tmp_desc
, 1))
225 perror_with_name ("Can't bind address");
227 tmp
= sizeof (sockaddr
);
228 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
229 if (remote_desc
== -1)
230 perror_with_name ("Accept failed");
232 /* Enable TCP keep alive process. */
234 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
236 /* Tell TCP not to delay small packets. This greatly speeds up
237 interactive response. */
239 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
240 (char *) &tmp
, sizeof (tmp
));
243 close (tmp_desc
); /* No longer need this */
245 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbreplay simply
246 exits when the remote side dies. */
248 closesocket (tmp_desc
); /* No longer need this */
252 #if defined(F_SETFL) && defined (FASYNC)
253 fcntl (remote_desc
, F_SETFL
, FASYNC
);
256 fprintf (stderr
, "Replay logfile using %s\n", name
);
263 if (ch
>= '0' && ch
<= '9')
267 if (ch
>= 'A' && ch
<= 'F')
269 return (ch
- 'A' + 10);
271 if (ch
>= 'a' && ch
<= 'f')
273 return (ch
- 'a' + 10);
275 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
324 ch
= tohex (ch2
) << 4;
331 /* Treat any other char as just itself */
340 /* Accept input from gdb and match with chars from fp (after skipping one
341 blank) up until a \n is read from fp (which is not matched) */
347 unsigned char fromgdb
;
349 if ((fromlog
= logchar (fp
)) != ' ')
351 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
356 fromlog
= logchar (fp
);
361 read (remote_desc
, &fromgdb
, 1);
363 while (fromlog
== fromgdb
);
366 sync_error (fp
, "Sync error during read of gdb packet", fromlog
,
371 /* Play data back to gdb from fp (after skipping leading blank) up until a
372 \n is read from fp (which is discarded and not sent to gdb). */
380 if ((fromlog
= logchar (fp
)) != ' ')
382 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
385 while ((fromlog
= logchar (fp
)) != EOL
)
388 write (remote_desc
, &ch
, 1);
393 main (int argc
, char *argv
[])
400 fprintf (stderr
, "Usage: gdbreplay <logfile> <host:port>\n");
404 fp
= fopen (argv
[1], "r");
407 perror_with_name (argv
[1]);
409 remote_open (argv
[2]);
410 while ((ch
= logchar (fp
)) != EOF
)
415 /* data sent from gdb to gdbreplay, accept and match it */
419 /* data sent from gdbreplay to gdb, play it */
423 /* Command executed by gdb */
424 while ((ch
= logchar (fp
)) != EOL
);