1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2021 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/>. */
20 #include "gdbsupport/common-defs.h"
24 #undef PACKAGE_VERSION
26 #undef PACKAGE_TARNAME
29 #include "gdbsupport/version.h"
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
51 #if HAVE_NETINET_TCP_H
52 #include <netinet/tcp.h>
59 #include "gdbsupport/netstuff.h"
60 #include "gdbsupport/rsp-low.h"
62 #ifndef HAVE_SOCKLEN_T
63 typedef int socklen_t
;
66 /* Sort of a hack... */
69 static int remote_desc_in
;
70 static int remote_desc_out
;
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__ */
124 sync_error (FILE *fp
, const char *desc
, int expect
, int got
)
126 fprintf (stderr
, "\n%s\n", desc
);
127 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
128 ftell (fp
), expect
, got
);
134 remote_error (const char *desc
)
136 fprintf (stderr
, "\n%s\n", desc
);
145 gdb_assert (remote_desc_in
== remote_desc_out
);
146 closesocket (remote_desc_in
);
148 close (remote_desc_in
);
149 if (remote_desc_in
!= remote_desc_out
)
150 close (remote_desc_out
);
154 /* Open a connection to a remote debugger.
155 NAME is the filename used for communication. */
158 remote_open (const char *name
)
161 if (strcmp (name
, "-") == 0)
169 const char *last_colon
= strrchr (name
, ':');
171 if (last_colon
== NULL
)
173 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
179 static int winsock_initialized
;
183 struct addrinfo hint
;
184 struct addrinfo
*ainfo
;
186 memset (&hint
, 0, sizeof (hint
));
187 /* Assume no prefix will be passed, therefore we should use
189 hint
.ai_family
= AF_UNSPEC
;
190 hint
.ai_socktype
= SOCK_STREAM
;
191 hint
.ai_protocol
= IPPROTO_TCP
;
193 parsed_connection_spec parsed
= parse_connection_spec (name
, &hint
);
195 if (parsed
.port_str
.empty ())
196 error (_("Missing port on hostname '%s'"), name
);
199 if (!winsock_initialized
)
203 WSAStartup (MAKEWORD (1, 0), &wsad
);
204 winsock_initialized
= 1;
208 int r
= getaddrinfo (parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
213 fprintf (stderr
, "%s:%s: cannot resolve name: %s\n",
214 parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
220 scoped_free_addrinfo
free_ainfo (ainfo
);
224 for (p
= ainfo
; p
!= NULL
; p
= p
->ai_next
)
226 tmp_desc
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
233 perror_with_name ("Cannot open socket");
235 /* Allow rapid reuse of this port. */
237 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
240 switch (p
->ai_family
)
243 ((struct sockaddr_in
*) p
->ai_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
246 ((struct sockaddr_in6
*) p
->ai_addr
)->sin6_addr
= in6addr_any
;
249 fprintf (stderr
, "Invalid 'ai_family' %d\n", p
->ai_family
);
253 if (bind (tmp_desc
, p
->ai_addr
, p
->ai_addrlen
) != 0)
254 perror_with_name ("Can't bind address");
256 if (p
->ai_socktype
== SOCK_DGRAM
)
257 remote_desc_in
= tmp_desc
;
260 struct sockaddr_storage sockaddr
;
261 socklen_t sockaddrsize
= sizeof (sockaddr
);
262 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
264 if (listen (tmp_desc
, 1) != 0)
265 perror_with_name ("Can't listen on socket");
267 remote_desc_in
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
,
270 if (remote_desc_in
== -1)
271 perror_with_name ("Accept failed");
273 /* Enable TCP keep alive process. */
275 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
276 (char *) &tmp
, sizeof (tmp
));
278 /* Tell TCP not to delay small packets. This greatly speeds up
279 interactive response. */
281 setsockopt (remote_desc_in
, IPPROTO_TCP
, TCP_NODELAY
,
282 (char *) &tmp
, sizeof (tmp
));
284 if (getnameinfo ((struct sockaddr
*) &sockaddr
, sockaddrsize
,
285 orig_host
, sizeof (orig_host
),
286 orig_port
, sizeof (orig_port
),
287 NI_NUMERICHOST
| NI_NUMERICSERV
) == 0)
289 fprintf (stderr
, "Remote debugging from host %s, port %s\n",
290 orig_host
, orig_port
);
295 close (tmp_desc
); /* No longer need this */
297 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
298 gdbreplay simply exits when
299 the remote side dies. */
301 closesocket (tmp_desc
); /* No longer need this */
305 #if defined(F_SETFL) && defined (FASYNC)
306 fcntl (remote_desc_in
, F_SETFL
, FASYNC
);
308 remote_desc_out
= remote_desc_in
;
310 fprintf (stderr
, "Replay logfile using %s\n", name
);
328 /* Treat \r\n as a newline. */
338 fputc (ch
== EOL
? '\n' : '\r', stderr
);
374 ch
= fromhex (ch2
) << 4;
381 /* Treat any other char as just itself */
393 unsigned char fromgdb
;
395 if (read (desc
, &fromgdb
, 1) != 1)
401 /* Accept input from gdb and match with chars from fp (after skipping one
402 blank) up until a \n is read from fp (which is not matched) */
410 if ((fromlog
= logchar (fp
)) != ' ')
412 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
417 fromlog
= logchar (fp
);
420 fromgdb
= gdbchar (remote_desc_in
);
422 remote_error ("Error during read from gdb");
424 while (fromlog
== fromgdb
);
428 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
433 /* Play data back to gdb from fp (after skipping leading blank) up until a
434 \n is read from fp (which is discarded and not sent to gdb). */
442 if ((fromlog
= logchar (fp
)) != ' ')
444 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
447 while ((fromlog
= logchar (fp
)) != EOL
)
450 if (write (remote_desc_out
, &ch
, 1) != 1)
451 remote_error ("Error during write to gdb");
456 gdbreplay_version (void)
458 printf ("GNU gdbreplay %s%s\n"
459 "Copyright (C) 2021 Free Software Foundation, Inc.\n"
460 "gdbreplay is free software, covered by "
461 "the GNU General Public License.\n"
462 "This gdbreplay was configured as \"%s\"\n",
463 PKGVERSION
, version
, host_name
);
467 gdbreplay_usage (FILE *stream
)
469 fprintf (stream
, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
470 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
471 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
474 /* Main function. This is called by the real "main" function,
475 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
477 static void ATTRIBUTE_NORETURN
478 captured_main (int argc
, char *argv
[])
483 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
485 gdbreplay_version ();
488 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
490 gdbreplay_usage (stdout
);
496 gdbreplay_usage (stderr
);
499 fp
= fopen (argv
[1], "r");
502 perror_with_name (argv
[1]);
504 remote_open (argv
[2]);
505 while ((ch
= logchar (fp
)) != EOF
)
510 /* data sent from gdb to gdbreplay, accept and match it */
514 /* data sent from gdbreplay to gdb, play it */
518 /* Command executed by gdb */
519 while ((ch
= logchar (fp
)) != EOL
);
528 main (int argc
, char *argv
[])
532 captured_main (argc
, argv
);
534 catch (const gdb_exception
&exception
)
536 if (exception
.reason
== RETURN_ERROR
)
539 fprintf (stderr
, "%s\n", exception
.what ());
545 gdb_assert_not_reached ("captured_main should never return");