1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998-2000, 2002-2003, 2005-2012 Free Software
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 3 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, see <http://www.gnu.org/licenses/>. */
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
54 #if HAVE_NETINET_TCP_H
55 #include <netinet/tcp.h>
67 #ifndef HAVE_SOCKLEN_T
68 typedef int socklen_t
;
71 /* Sort of a hack... */
74 /* Version information, from version.c. */
75 extern const char version
[];
76 extern const char host_name
[];
78 static int remote_desc
;
83 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
86 #define errno (GetLastError ())
89 strerror (DWORD error
)
91 static char buf
[1024];
93 DWORD lasterr
= GetLastError ();
94 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
95 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
98 0, /* Default language */
104 /* If there is an \r\n appended, zap it. */
106 && msgbuf
[chars
- 2] == '\r'
107 && msgbuf
[chars
- 1] == '\n')
113 if (chars
> ((COUNTOF (buf
)) - 1))
115 chars
= COUNTOF (buf
) - 1;
119 wcstombs (buf
, msgbuf
, chars
+ 1);
123 sprintf (buf
, "unknown win32 error (%ld)", error
);
125 SetLastError (lasterr
);
129 #endif /* __MINGW32CE__ */
131 /* Print the system error message for errno, and also mention STRING
132 as the file name for which the error was encountered.
133 Then return to command level. */
136 perror_with_name (const char *string
)
144 err
= strerror (errno
);
146 err
= "unknown error";
148 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
149 strcpy (combined
, string
);
150 strcat (combined
, ": ");
151 strcat (combined
, err
);
152 fprintf (stderr
, "\n%s.\n", combined
);
158 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
160 fprintf (stderr
, "\n%s\n", desc
);
161 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
162 ftell (fp
), expect
, got
);
168 remote_error (const char *desc
)
170 fprintf (stderr
, "\n%s\n", desc
);
179 closesocket (remote_desc
);
185 /* Open a connection to a remote debugger.
186 NAME is the filename used for communication. */
189 remote_open (char *name
)
191 if (!strchr (name
, ':'))
193 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
200 static int winsock_initialized
;
204 struct sockaddr_in sockaddr
;
208 port_str
= strchr (name
, ':');
210 port
= atoi (port_str
+ 1);
213 if (!winsock_initialized
)
217 WSAStartup (MAKEWORD (1, 0), &wsad
);
218 winsock_initialized
= 1;
222 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
224 perror_with_name ("Can't open socket");
226 /* Allow rapid reuse of this port. */
228 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
231 sockaddr
.sin_family
= PF_INET
;
232 sockaddr
.sin_port
= htons (port
);
233 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
235 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
236 || listen (tmp_desc
, 1))
237 perror_with_name ("Can't bind address");
239 tmp
= sizeof (sockaddr
);
240 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
241 if (remote_desc
== -1)
242 perror_with_name ("Accept failed");
244 /* Enable TCP keep alive process. */
246 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
247 (char *) &tmp
, sizeof (tmp
));
249 /* Tell TCP not to delay small packets. This greatly speeds up
250 interactive response. */
252 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
253 (char *) &tmp
, sizeof (tmp
));
256 close (tmp_desc
); /* No longer need this */
258 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
259 gdbreplay simply exits when
260 the remote side dies. */
262 closesocket (tmp_desc
); /* No longer need this */
266 #if defined(F_SETFL) && defined (FASYNC)
267 fcntl (remote_desc
, F_SETFL
, FASYNC
);
270 fprintf (stderr
, "Replay logfile using %s\n", name
);
277 if (ch
>= '0' && ch
<= '9')
281 if (ch
>= 'A' && ch
<= 'F')
283 return (ch
- 'A' + 10);
285 if (ch
>= 'a' && ch
<= 'f')
287 return (ch
- 'a' + 10);
289 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
338 ch
= tohex (ch2
) << 4;
345 /* Treat any other char as just itself */
357 unsigned char fromgdb
;
359 if (read (desc
, &fromgdb
, 1) != 1)
365 /* Accept input from gdb and match with chars from fp (after skipping one
366 blank) up until a \n is read from fp (which is not matched) */
374 if ((fromlog
= logchar (fp
)) != ' ')
376 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
381 fromlog
= logchar (fp
);
384 fromgdb
= gdbchar (remote_desc
);
386 remote_error ("Error during read from gdb");
388 while (fromlog
== fromgdb
);
392 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
397 /* Play data back to gdb from fp (after skipping leading blank) up until a
398 \n is read from fp (which is discarded and not sent to gdb). */
406 if ((fromlog
= logchar (fp
)) != ' ')
408 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
411 while ((fromlog
= logchar (fp
)) != EOL
)
414 if (write (remote_desc
, &ch
, 1) != 1)
415 remote_error ("Error during write to gdb");
420 gdbreplay_version (void)
422 printf ("GNU gdbreplay %s%s\n"
423 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
424 "gdbreplay is free software, covered by "
425 "the GNU General Public License.\n"
426 "This gdbreplay was configured as \"%s\"\n",
427 PKGVERSION
, version
, host_name
);
431 gdbreplay_usage (FILE *stream
)
433 fprintf (stream
, "Usage:\tgdbreplay <logfile> <host:port>\n");
434 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
435 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
439 main (int argc
, char *argv
[])
444 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
446 gdbreplay_version ();
449 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
451 gdbreplay_usage (stdout
);
457 gdbreplay_usage (stderr
);
460 fp
= fopen (argv
[1], "r");
463 perror_with_name (argv
[1]);
465 remote_open (argv
[2]);
466 while ((ch
= logchar (fp
)) != EOF
)
471 /* data sent from gdb to gdbreplay, accept and match it */
475 /* data sent from gdbreplay to gdb, play it */
479 /* Command executed by gdb */
480 while ((ch
= logchar (fp
)) != EOL
);