1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2024 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/>. */
22 #undef PACKAGE_VERSION
24 #undef PACKAGE_TARNAME
27 #include "gdbsupport/version.h"
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
49 #if HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
57 #include "gdbsupport/netstuff.h"
58 #include "gdbsupport/rsp-low.h"
60 #ifndef HAVE_SOCKLEN_T
61 typedef int socklen_t
;
64 /* Sort of a hack... */
67 static int remote_desc_in
;
68 static int remote_desc_out
;
71 sync_error (FILE *fp
, const char *desc
, int expect
, int got
)
73 fprintf (stderr
, "\n%s\n", desc
);
74 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
75 ftell (fp
), expect
, got
);
81 remote_error (const char *desc
)
83 fprintf (stderr
, "\n%s\n", desc
);
92 gdb_assert (remote_desc_in
== remote_desc_out
);
93 closesocket (remote_desc_in
);
95 close (remote_desc_in
);
96 if (remote_desc_in
!= remote_desc_out
)
97 close (remote_desc_out
);
101 /* Open a connection to a remote debugger.
102 NAME is the filename used for communication. */
105 remote_open (const char *name
)
108 if (strcmp (name
, "-") == 0)
116 const char *last_colon
= strrchr (name
, ':');
118 if (last_colon
== NULL
)
120 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
126 static int winsock_initialized
;
130 struct addrinfo hint
;
131 struct addrinfo
*ainfo
;
133 memset (&hint
, 0, sizeof (hint
));
134 /* Assume no prefix will be passed, therefore we should use
136 hint
.ai_family
= AF_UNSPEC
;
137 hint
.ai_socktype
= SOCK_STREAM
;
138 hint
.ai_protocol
= IPPROTO_TCP
;
140 parsed_connection_spec parsed
= parse_connection_spec (name
, &hint
);
142 if (parsed
.port_str
.empty ())
143 error (_("Missing port on hostname '%s'"), name
);
146 if (!winsock_initialized
)
150 WSAStartup (MAKEWORD (1, 0), &wsad
);
151 winsock_initialized
= 1;
155 int r
= getaddrinfo (parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
160 fprintf (stderr
, "%s:%s: cannot resolve name: %s\n",
161 parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
167 scoped_free_addrinfo
free_ainfo (ainfo
);
171 for (p
= ainfo
; p
!= NULL
; p
= p
->ai_next
)
173 tmp_desc
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
180 perror_with_name ("Cannot open socket");
182 /* Allow rapid reuse of this port. */
184 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
187 switch (p
->ai_family
)
190 ((struct sockaddr_in
*) p
->ai_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
193 ((struct sockaddr_in6
*) p
->ai_addr
)->sin6_addr
= in6addr_any
;
196 fprintf (stderr
, "Invalid 'ai_family' %d\n", p
->ai_family
);
200 if (bind (tmp_desc
, p
->ai_addr
, p
->ai_addrlen
) != 0)
201 perror_with_name ("Can't bind address");
203 if (p
->ai_socktype
== SOCK_DGRAM
)
204 remote_desc_in
= tmp_desc
;
207 struct sockaddr_storage sockaddr
;
208 socklen_t sockaddrsize
= sizeof (sockaddr
);
209 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
211 if (listen (tmp_desc
, 1) != 0)
212 perror_with_name ("Can't listen on socket");
214 remote_desc_in
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
,
217 if (remote_desc_in
== -1)
218 perror_with_name ("Accept failed");
220 /* Enable TCP keep alive process. */
222 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
223 (char *) &tmp
, sizeof (tmp
));
225 /* Tell TCP not to delay small packets. This greatly speeds up
226 interactive response. */
228 setsockopt (remote_desc_in
, IPPROTO_TCP
, TCP_NODELAY
,
229 (char *) &tmp
, sizeof (tmp
));
231 if (getnameinfo ((struct sockaddr
*) &sockaddr
, sockaddrsize
,
232 orig_host
, sizeof (orig_host
),
233 orig_port
, sizeof (orig_port
),
234 NI_NUMERICHOST
| NI_NUMERICSERV
) == 0)
236 fprintf (stderr
, "Remote debugging from host %s, port %s\n",
237 orig_host
, orig_port
);
242 close (tmp_desc
); /* No longer need this */
244 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
245 gdbreplay simply exits when
246 the remote side dies. */
248 closesocket (tmp_desc
); /* No longer need this */
252 #if defined(F_SETFL) && defined (FASYNC)
253 fcntl (remote_desc_in
, F_SETFL
, FASYNC
);
255 remote_desc_out
= remote_desc_in
;
257 fprintf (stderr
, "Replay logfile using %s\n", name
);
275 /* Treat \r\n as a newline. */
285 fputc (ch
== EOL
? '\n' : '\r', stderr
);
321 ch
= fromhex (ch2
) << 4;
328 /* Treat any other char as just itself */
340 unsigned char fromgdb
;
342 if (read (desc
, &fromgdb
, 1) != 1)
348 /* Accept input from gdb and match with chars from fp (after skipping one
349 blank) up until a \n is read from fp (which is not matched) */
357 if ((fromlog
= logchar (fp
)) != ' ')
359 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
364 fromlog
= logchar (fp
);
367 fromgdb
= gdbchar (remote_desc_in
);
369 remote_error ("Error during read from gdb");
371 while (fromlog
== fromgdb
);
375 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
380 /* Play data back to gdb from fp (after skipping leading blank) up until a
381 \n is read from fp (which is discarded and not sent to gdb). */
389 if ((fromlog
= logchar (fp
)) != ' ')
391 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
394 while ((fromlog
= logchar (fp
)) != EOL
)
397 if (write (remote_desc_out
, &ch
, 1) != 1)
398 remote_error ("Error during write to gdb");
403 gdbreplay_version (void)
405 printf ("GNU gdbreplay %s%s\n"
406 "Copyright (C) 2024 Free Software Foundation, Inc.\n"
407 "gdbreplay is free software, covered by "
408 "the GNU General Public License.\n"
409 "This gdbreplay was configured as \"%s\"\n",
410 PKGVERSION
, version
, host_name
);
414 gdbreplay_usage (FILE *stream
)
416 fprintf (stream
, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
417 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
418 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
421 /* Main function. This is called by the real "main" function,
422 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
424 [[noreturn
]] static void
425 captured_main (int argc
, char *argv
[])
430 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
432 gdbreplay_version ();
435 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
437 gdbreplay_usage (stdout
);
443 gdbreplay_usage (stderr
);
446 fp
= fopen (argv
[1], "r");
449 perror_with_name (argv
[1]);
451 remote_open (argv
[2]);
452 while ((ch
= logchar (fp
)) != EOF
)
457 /* data sent from gdb to gdbreplay, accept and match it */
461 /* data sent from gdbreplay to gdb, play it */
465 /* Command executed by gdb */
466 while ((ch
= logchar (fp
)) != EOL
);
475 main (int argc
, char *argv
[])
479 captured_main (argc
, argv
);
481 catch (const gdb_exception
&exception
)
483 if (exception
.reason
== RETURN_ERROR
)
486 fprintf (stderr
, "%s\n", exception
.what ());
492 gdb_assert_not_reached ("captured_main should never return");