1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
21 #include "build-gnulib-gdbserver/config.h"
46 #ifdef HAVE_NETINET_IN_H
47 #include <netinet/in.h>
49 #ifdef HAVE_SYS_SOCKET_H
50 #include <sys/socket.h>
55 #if HAVE_NETINET_TCP_H
56 #include <netinet/tcp.h>
68 #ifndef HAVE_SOCKLEN_T
69 typedef int socklen_t
;
72 /* Sort of a hack... */
75 /* Version information, from version.c. */
76 extern const char version
[];
77 extern const char host_name
[];
79 static int remote_desc
;
84 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
87 #define errno (GetLastError ())
90 strerror (DWORD error
)
92 static char buf
[1024];
94 DWORD lasterr
= GetLastError ();
95 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
96 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
99 0, /* Default language */
105 /* If there is an \r\n appended, zap it. */
107 && msgbuf
[chars
- 2] == '\r'
108 && msgbuf
[chars
- 1] == '\n')
114 if (chars
> ((COUNTOF (buf
)) - 1))
116 chars
= COUNTOF (buf
) - 1;
120 wcstombs (buf
, msgbuf
, chars
+ 1);
124 sprintf (buf
, "unknown win32 error (%ld)", error
);
126 SetLastError (lasterr
);
130 #endif /* __MINGW32CE__ */
132 /* Print the system error message for errno, and also mention STRING
133 as the file name for which the error was encountered.
134 Then return to command level. */
137 perror_with_name (const char *string
)
145 err
= strerror (errno
);
147 err
= "unknown error";
149 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
150 strcpy (combined
, string
);
151 strcat (combined
, ": ");
152 strcat (combined
, err
);
153 fprintf (stderr
, "\n%s.\n", combined
);
159 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
161 fprintf (stderr
, "\n%s\n", desc
);
162 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
163 ftell (fp
), expect
, got
);
169 remote_error (const char *desc
)
171 fprintf (stderr
, "\n%s\n", desc
);
180 closesocket (remote_desc
);
186 /* Open a connection to a remote debugger.
187 NAME is the filename used for communication. */
190 remote_open (char *name
)
192 if (!strchr (name
, ':'))
194 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
201 static int winsock_initialized
;
205 struct sockaddr_in sockaddr
;
209 port_str
= strchr (name
, ':');
211 port
= atoi (port_str
+ 1);
214 if (!winsock_initialized
)
218 WSAStartup (MAKEWORD (1, 0), &wsad
);
219 winsock_initialized
= 1;
223 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
225 perror_with_name ("Can't open socket");
227 /* Allow rapid reuse of this port. */
229 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
232 sockaddr
.sin_family
= PF_INET
;
233 sockaddr
.sin_port
= htons (port
);
234 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
236 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
237 || listen (tmp_desc
, 1))
238 perror_with_name ("Can't bind address");
240 tmp
= sizeof (sockaddr
);
241 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
242 if (remote_desc
== -1)
243 perror_with_name ("Accept failed");
245 /* Enable TCP keep alive process. */
247 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
248 (char *) &tmp
, sizeof (tmp
));
250 /* Tell TCP not to delay small packets. This greatly speeds up
251 interactive response. */
253 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
254 (char *) &tmp
, sizeof (tmp
));
257 close (tmp_desc
); /* No longer need this */
259 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
260 gdbreplay simply exits when
261 the remote side dies. */
263 closesocket (tmp_desc
); /* No longer need this */
267 #if defined(F_SETFL) && defined (FASYNC)
268 fcntl (remote_desc
, F_SETFL
, FASYNC
);
271 fprintf (stderr
, "Replay logfile using %s\n", name
);
278 if (ch
>= '0' && ch
<= '9')
282 if (ch
>= 'A' && ch
<= 'F')
284 return (ch
- 'A' + 10);
286 if (ch
>= 'a' && ch
<= 'f')
288 return (ch
- 'a' + 10);
290 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
339 ch
= tohex (ch2
) << 4;
346 /* Treat any other char as just itself */
358 unsigned char fromgdb
;
360 if (read (desc
, &fromgdb
, 1) != 1)
366 /* Accept input from gdb and match with chars from fp (after skipping one
367 blank) up until a \n is read from fp (which is not matched) */
375 if ((fromlog
= logchar (fp
)) != ' ')
377 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
382 fromlog
= logchar (fp
);
385 fromgdb
= gdbchar (remote_desc
);
387 remote_error ("Error during read from gdb");
389 while (fromlog
== fromgdb
);
393 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
398 /* Play data back to gdb from fp (after skipping leading blank) up until a
399 \n is read from fp (which is discarded and not sent to gdb). */
407 if ((fromlog
= logchar (fp
)) != ' ')
409 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
412 while ((fromlog
= logchar (fp
)) != EOL
)
415 if (write (remote_desc
, &ch
, 1) != 1)
416 remote_error ("Error during write to gdb");
421 gdbreplay_version (void)
423 printf ("GNU gdbreplay %s%s\n"
424 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
425 "gdbreplay is free software, covered by "
426 "the GNU General Public License.\n"
427 "This gdbreplay was configured as \"%s\"\n",
428 PKGVERSION
, version
, host_name
);
432 gdbreplay_usage (FILE *stream
)
434 fprintf (stream
, "Usage:\tgdbreplay <logfile> <host:port>\n");
435 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
436 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
440 main (int argc
, char *argv
[])
445 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
447 gdbreplay_version ();
450 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
452 gdbreplay_usage (stdout
);
458 gdbreplay_usage (stderr
);
461 fp
= fopen (argv
[1], "r");
464 perror_with_name (argv
[1]);
466 remote_open (argv
[2]);
467 while ((ch
= logchar (fp
)) != EOF
)
472 /* data sent from gdb to gdbreplay, accept and match it */
476 /* data sent from gdbreplay to gdb, play it */
480 /* Command executed by gdb */
481 while ((ch
= logchar (fp
)) != EOL
);