1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2014 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"
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
48 #if HAVE_NETINET_TCP_H
49 #include <netinet/tcp.h>
58 #ifndef HAVE_SOCKLEN_T
59 typedef int socklen_t
;
62 /* Sort of a hack... */
65 static int remote_desc
;
70 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
73 #define errno (GetLastError ())
76 strerror (DWORD error
)
78 static char buf
[1024];
80 DWORD lasterr
= GetLastError ();
81 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
82 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
85 0, /* Default language */
91 /* If there is an \r\n appended, zap it. */
93 && msgbuf
[chars
- 2] == '\r'
94 && msgbuf
[chars
- 1] == '\n')
100 if (chars
> ((COUNTOF (buf
)) - 1))
102 chars
= COUNTOF (buf
) - 1;
106 wcstombs (buf
, msgbuf
, chars
+ 1);
110 sprintf (buf
, "unknown win32 error (%ld)", error
);
112 SetLastError (lasterr
);
116 #endif /* __MINGW32CE__ */
118 /* Print the system error message for errno, and also mention STRING
119 as the file name for which the error was encountered.
120 Then return to command level. */
123 perror_with_name (const char *string
)
131 err
= strerror (errno
);
133 err
= "unknown error";
135 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
136 strcpy (combined
, string
);
137 strcat (combined
, ": ");
138 strcat (combined
, err
);
139 fprintf (stderr
, "\n%s.\n", combined
);
145 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
147 fprintf (stderr
, "\n%s\n", desc
);
148 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
149 ftell (fp
), expect
, got
);
155 remote_error (const char *desc
)
157 fprintf (stderr
, "\n%s\n", desc
);
166 closesocket (remote_desc
);
172 /* Open a connection to a remote debugger.
173 NAME is the filename used for communication. */
176 remote_open (char *name
)
178 if (!strchr (name
, ':'))
180 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
187 static int winsock_initialized
;
191 struct sockaddr_in sockaddr
;
195 port_str
= strchr (name
, ':');
197 port
= atoi (port_str
+ 1);
200 if (!winsock_initialized
)
204 WSAStartup (MAKEWORD (1, 0), &wsad
);
205 winsock_initialized
= 1;
209 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
211 perror_with_name ("Can't open socket");
213 /* Allow rapid reuse of this port. */
215 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
218 sockaddr
.sin_family
= PF_INET
;
219 sockaddr
.sin_port
= htons (port
);
220 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
222 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
223 || listen (tmp_desc
, 1))
224 perror_with_name ("Can't bind address");
226 tmp
= sizeof (sockaddr
);
227 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
228 if (remote_desc
== -1)
229 perror_with_name ("Accept failed");
231 /* Enable TCP keep alive process. */
233 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
234 (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
246 gdbreplay simply exits when
247 the remote side dies. */
249 closesocket (tmp_desc
); /* No longer need this */
253 #if defined(F_SETFL) && defined (FASYNC)
254 fcntl (remote_desc
, F_SETFL
, FASYNC
);
257 fprintf (stderr
, "Replay logfile using %s\n", name
);
264 if (ch
>= '0' && ch
<= '9')
268 if (ch
>= 'A' && ch
<= 'F')
270 return (ch
- 'A' + 10);
272 if (ch
>= 'a' && ch
<= 'f')
274 return (ch
- 'a' + 10);
276 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
325 ch
= tohex (ch2
) << 4;
332 /* Treat any other char as just itself */
344 unsigned char fromgdb
;
346 if (read (desc
, &fromgdb
, 1) != 1)
352 /* Accept input from gdb and match with chars from fp (after skipping one
353 blank) up until a \n is read from fp (which is not matched) */
361 if ((fromlog
= logchar (fp
)) != ' ')
363 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
368 fromlog
= logchar (fp
);
371 fromgdb
= gdbchar (remote_desc
);
373 remote_error ("Error during read from gdb");
375 while (fromlog
== fromgdb
);
379 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
384 /* Play data back to gdb from fp (after skipping leading blank) up until a
385 \n is read from fp (which is discarded and not sent to gdb). */
393 if ((fromlog
= logchar (fp
)) != ' ')
395 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
398 while ((fromlog
= logchar (fp
)) != EOL
)
401 if (write (remote_desc
, &ch
, 1) != 1)
402 remote_error ("Error during write to gdb");
407 gdbreplay_version (void)
409 printf ("GNU gdbreplay %s%s\n"
410 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
411 "gdbreplay is free software, covered by "
412 "the GNU General Public License.\n"
413 "This gdbreplay was configured as \"%s\"\n",
414 PKGVERSION
, version
, host_name
);
418 gdbreplay_usage (FILE *stream
)
420 fprintf (stream
, "Usage:\tgdbreplay <logfile> <host:port>\n");
421 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
422 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
426 main (int argc
, char *argv
[])
431 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
433 gdbreplay_version ();
436 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
438 gdbreplay_usage (stdout
);
444 gdbreplay_usage (stderr
);
447 fp
= fopen (argv
[1], "r");
450 perror_with_name (argv
[1]);
452 remote_open (argv
[2]);
453 while ((ch
= logchar (fp
)) != EOF
)
458 /* data sent from gdb to gdbreplay, accept and match it */
462 /* data sent from gdbreplay to gdb, play it */
466 /* Command executed by gdb */
467 while ((ch
= logchar (fp
)) != EOL
);