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>
61 #ifndef HAVE_SOCKLEN_T
62 typedef int socklen_t
;
65 /* Sort of a hack... */
68 static int remote_desc
;
73 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
76 #define errno (GetLastError ())
79 strerror (DWORD error
)
81 static char buf
[1024];
83 DWORD lasterr
= GetLastError ();
84 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
85 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
88 0, /* Default language */
94 /* If there is an \r\n appended, zap it. */
96 && msgbuf
[chars
- 2] == '\r'
97 && msgbuf
[chars
- 1] == '\n')
103 if (chars
> ((COUNTOF (buf
)) - 1))
105 chars
= COUNTOF (buf
) - 1;
109 wcstombs (buf
, msgbuf
, chars
+ 1);
113 sprintf (buf
, "unknown win32 error (%ld)", error
);
115 SetLastError (lasterr
);
119 #endif /* __MINGW32CE__ */
121 /* Print the system error message for errno, and also mention STRING
122 as the file name for which the error was encountered.
123 Then return to command level. */
126 perror_with_name (const char *string
)
134 err
= strerror (errno
);
136 err
= "unknown error";
138 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
139 strcpy (combined
, string
);
140 strcat (combined
, ": ");
141 strcat (combined
, err
);
142 fprintf (stderr
, "\n%s.\n", combined
);
148 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
150 fprintf (stderr
, "\n%s\n", desc
);
151 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
152 ftell (fp
), expect
, got
);
158 remote_error (const char *desc
)
160 fprintf (stderr
, "\n%s\n", desc
);
169 closesocket (remote_desc
);
175 /* Open a connection to a remote debugger.
176 NAME is the filename used for communication. */
179 remote_open (char *name
)
181 if (!strchr (name
, ':'))
183 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
190 static int winsock_initialized
;
194 struct sockaddr_in sockaddr
;
198 port_str
= strchr (name
, ':');
200 port
= atoi (port_str
+ 1);
203 if (!winsock_initialized
)
207 WSAStartup (MAKEWORD (1, 0), &wsad
);
208 winsock_initialized
= 1;
212 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
214 perror_with_name ("Can't open socket");
216 /* Allow rapid reuse of this port. */
218 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
221 sockaddr
.sin_family
= PF_INET
;
222 sockaddr
.sin_port
= htons (port
);
223 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
225 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
226 || listen (tmp_desc
, 1))
227 perror_with_name ("Can't bind address");
229 tmp
= sizeof (sockaddr
);
230 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
231 if (remote_desc
== -1)
232 perror_with_name ("Accept failed");
234 /* Enable TCP keep alive process. */
236 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
237 (char *) &tmp
, sizeof (tmp
));
239 /* Tell TCP not to delay small packets. This greatly speeds up
240 interactive response. */
242 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
243 (char *) &tmp
, sizeof (tmp
));
246 close (tmp_desc
); /* No longer need this */
248 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
249 gdbreplay simply exits when
250 the remote side dies. */
252 closesocket (tmp_desc
); /* No longer need this */
256 #if defined(F_SETFL) && defined (FASYNC)
257 fcntl (remote_desc
, F_SETFL
, FASYNC
);
260 fprintf (stderr
, "Replay logfile using %s\n", name
);
267 if (ch
>= '0' && ch
<= '9')
271 if (ch
>= 'A' && ch
<= 'F')
273 return (ch
- 'A' + 10);
275 if (ch
>= 'a' && ch
<= 'f')
277 return (ch
- 'a' + 10);
279 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
328 ch
= tohex (ch2
) << 4;
335 /* Treat any other char as just itself */
347 unsigned char fromgdb
;
349 if (read (desc
, &fromgdb
, 1) != 1)
355 /* Accept input from gdb and match with chars from fp (after skipping one
356 blank) up until a \n is read from fp (which is not matched) */
364 if ((fromlog
= logchar (fp
)) != ' ')
366 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
371 fromlog
= logchar (fp
);
374 fromgdb
= gdbchar (remote_desc
);
376 remote_error ("Error during read from gdb");
378 while (fromlog
== fromgdb
);
382 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
387 /* Play data back to gdb from fp (after skipping leading blank) up until a
388 \n is read from fp (which is discarded and not sent to gdb). */
396 if ((fromlog
= logchar (fp
)) != ' ')
398 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
401 while ((fromlog
= logchar (fp
)) != EOL
)
404 if (write (remote_desc
, &ch
, 1) != 1)
405 remote_error ("Error during write to gdb");
410 gdbreplay_version (void)
412 printf ("GNU gdbreplay %s%s\n"
413 "Copyright (C) 2014 Free Software Foundation, Inc.\n"
414 "gdbreplay is free software, covered by "
415 "the GNU General Public License.\n"
416 "This gdbreplay was configured as \"%s\"\n",
417 PKGVERSION
, version
, host_name
);
421 gdbreplay_usage (FILE *stream
)
423 fprintf (stream
, "Usage:\tgdbreplay <logfile> <host:port>\n");
424 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
425 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
429 main (int argc
, char *argv
[])
434 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
436 gdbreplay_version ();
439 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
441 gdbreplay_usage (stdout
);
447 gdbreplay_usage (stderr
);
450 fp
= fopen (argv
[1], "r");
453 perror_with_name (argv
[1]);
455 remote_open (argv
[2]);
456 while ((ch
= logchar (fp
)) != EOF
)
461 /* data sent from gdb to gdbreplay, accept and match it */
465 /* data sent from gdbreplay to gdb, play it */
469 /* Command executed by gdb */
470 while ((ch
= logchar (fp
)) != EOL
);