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"
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
50 #if HAVE_NETINET_TCP_H
51 #include <netinet/tcp.h>
63 #ifndef HAVE_SOCKLEN_T
64 typedef int socklen_t
;
67 /* Sort of a hack... */
70 static int remote_desc
;
75 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
78 #define errno (GetLastError ())
81 strerror (DWORD error
)
83 static char buf
[1024];
85 DWORD lasterr
= GetLastError ();
86 DWORD chars
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
87 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
90 0, /* Default language */
96 /* If there is an \r\n appended, zap it. */
98 && msgbuf
[chars
- 2] == '\r'
99 && msgbuf
[chars
- 1] == '\n')
105 if (chars
> ((COUNTOF (buf
)) - 1))
107 chars
= COUNTOF (buf
) - 1;
111 wcstombs (buf
, msgbuf
, chars
+ 1);
115 sprintf (buf
, "unknown win32 error (%ld)", error
);
117 SetLastError (lasterr
);
121 #endif /* __MINGW32CE__ */
123 /* Print the system error message for errno, and also mention STRING
124 as the file name for which the error was encountered.
125 Then return to command level. */
128 perror_with_name (const char *string
)
136 err
= strerror (errno
);
138 err
= "unknown error";
140 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
141 strcpy (combined
, string
);
142 strcat (combined
, ": ");
143 strcat (combined
, err
);
144 fprintf (stderr
, "\n%s.\n", combined
);
150 sync_error (FILE *fp
, char *desc
, int expect
, int got
)
152 fprintf (stderr
, "\n%s\n", desc
);
153 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
154 ftell (fp
), expect
, got
);
160 remote_error (const char *desc
)
162 fprintf (stderr
, "\n%s\n", desc
);
171 closesocket (remote_desc
);
177 /* Open a connection to a remote debugger.
178 NAME is the filename used for communication. */
181 remote_open (char *name
)
183 if (!strchr (name
, ':'))
185 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
192 static int winsock_initialized
;
196 struct sockaddr_in sockaddr
;
200 port_str
= strchr (name
, ':');
202 port
= atoi (port_str
+ 1);
205 if (!winsock_initialized
)
209 WSAStartup (MAKEWORD (1, 0), &wsad
);
210 winsock_initialized
= 1;
214 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
216 perror_with_name ("Can't open socket");
218 /* Allow rapid reuse of this port. */
220 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
223 sockaddr
.sin_family
= PF_INET
;
224 sockaddr
.sin_port
= htons (port
);
225 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
227 if (bind (tmp_desc
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
))
228 || listen (tmp_desc
, 1))
229 perror_with_name ("Can't bind address");
231 tmp
= sizeof (sockaddr
);
232 remote_desc
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
, &tmp
);
233 if (remote_desc
== -1)
234 perror_with_name ("Accept failed");
236 /* Enable TCP keep alive process. */
238 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
239 (char *) &tmp
, sizeof (tmp
));
241 /* Tell TCP not to delay small packets. This greatly speeds up
242 interactive response. */
244 setsockopt (remote_desc
, IPPROTO_TCP
, TCP_NODELAY
,
245 (char *) &tmp
, sizeof (tmp
));
248 close (tmp_desc
); /* No longer need this */
250 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
251 gdbreplay simply exits when
252 the remote side dies. */
254 closesocket (tmp_desc
); /* No longer need this */
258 #if defined(F_SETFL) && defined (FASYNC)
259 fcntl (remote_desc
, F_SETFL
, FASYNC
);
262 fprintf (stderr
, "Replay logfile using %s\n", name
);
269 if (ch
>= '0' && ch
<= '9')
273 if (ch
>= 'A' && ch
<= 'F')
275 return (ch
- 'A' + 10);
277 if (ch
>= 'a' && ch
<= 'f')
279 return (ch
- 'a' + 10);
281 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
330 ch
= tohex (ch2
) << 4;
337 /* Treat any other char as just itself */
349 unsigned char fromgdb
;
351 if (read (desc
, &fromgdb
, 1) != 1)
357 /* Accept input from gdb and match with chars from fp (after skipping one
358 blank) up until a \n is read from fp (which is not matched) */
366 if ((fromlog
= logchar (fp
)) != ' ')
368 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
373 fromlog
= logchar (fp
);
376 fromgdb
= gdbchar (remote_desc
);
378 remote_error ("Error during read from gdb");
380 while (fromlog
== fromgdb
);
384 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
389 /* Play data back to gdb from fp (after skipping leading blank) up until a
390 \n is read from fp (which is discarded and not sent to gdb). */
398 if ((fromlog
= logchar (fp
)) != ' ')
400 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
403 while ((fromlog
= logchar (fp
)) != EOL
)
406 if (write (remote_desc
, &ch
, 1) != 1)
407 remote_error ("Error during write to gdb");
412 gdbreplay_version (void)
414 printf ("GNU gdbreplay %s%s\n"
415 "Copyright (C) 2014 Free Software Foundation, Inc.\n"
416 "gdbreplay is free software, covered by "
417 "the GNU General Public License.\n"
418 "This gdbreplay was configured as \"%s\"\n",
419 PKGVERSION
, version
, host_name
);
423 gdbreplay_usage (FILE *stream
)
425 fprintf (stream
, "Usage:\tgdbreplay <logfile> <host:port>\n");
426 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
427 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
431 main (int argc
, char *argv
[])
436 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
438 gdbreplay_version ();
441 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
443 gdbreplay_usage (stdout
);
449 gdbreplay_usage (stderr
);
452 fp
= fopen (argv
[1], "r");
455 perror_with_name (argv
[1]);
457 remote_open (argv
[2]);
458 while ((ch
= logchar (fp
)) != EOF
)
463 /* data sent from gdb to gdbreplay, accept and match it */
467 /* data sent from gdbreplay to gdb, play it */
471 /* Command executed by gdb */
472 while ((ch
= logchar (fp
)) != EOL
);