1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2023 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
;
73 sync_error (FILE *fp
, const char *desc
, int expect
, int got
)
75 fprintf (stderr
, "\n%s\n", desc
);
76 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
77 ftell (fp
), expect
, got
);
83 remote_error (const char *desc
)
85 fprintf (stderr
, "\n%s\n", desc
);
94 gdb_assert (remote_desc_in
== remote_desc_out
);
95 closesocket (remote_desc_in
);
97 close (remote_desc_in
);
98 if (remote_desc_in
!= remote_desc_out
)
99 close (remote_desc_out
);
103 /* Open a connection to a remote debugger.
104 NAME is the filename used for communication. */
107 remote_open (const char *name
)
110 if (strcmp (name
, "-") == 0)
118 const char *last_colon
= strrchr (name
, ':');
120 if (last_colon
== NULL
)
122 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
128 static int winsock_initialized
;
132 struct addrinfo hint
;
133 struct addrinfo
*ainfo
;
135 memset (&hint
, 0, sizeof (hint
));
136 /* Assume no prefix will be passed, therefore we should use
138 hint
.ai_family
= AF_UNSPEC
;
139 hint
.ai_socktype
= SOCK_STREAM
;
140 hint
.ai_protocol
= IPPROTO_TCP
;
142 parsed_connection_spec parsed
= parse_connection_spec (name
, &hint
);
144 if (parsed
.port_str
.empty ())
145 error (_("Missing port on hostname '%s'"), name
);
148 if (!winsock_initialized
)
152 WSAStartup (MAKEWORD (1, 0), &wsad
);
153 winsock_initialized
= 1;
157 int r
= getaddrinfo (parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
162 fprintf (stderr
, "%s:%s: cannot resolve name: %s\n",
163 parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
169 scoped_free_addrinfo
free_ainfo (ainfo
);
173 for (p
= ainfo
; p
!= NULL
; p
= p
->ai_next
)
175 tmp_desc
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
182 perror_with_name ("Cannot open socket");
184 /* Allow rapid reuse of this port. */
186 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
189 switch (p
->ai_family
)
192 ((struct sockaddr_in
*) p
->ai_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
195 ((struct sockaddr_in6
*) p
->ai_addr
)->sin6_addr
= in6addr_any
;
198 fprintf (stderr
, "Invalid 'ai_family' %d\n", p
->ai_family
);
202 if (bind (tmp_desc
, p
->ai_addr
, p
->ai_addrlen
) != 0)
203 perror_with_name ("Can't bind address");
205 if (p
->ai_socktype
== SOCK_DGRAM
)
206 remote_desc_in
= tmp_desc
;
209 struct sockaddr_storage sockaddr
;
210 socklen_t sockaddrsize
= sizeof (sockaddr
);
211 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
213 if (listen (tmp_desc
, 1) != 0)
214 perror_with_name ("Can't listen on socket");
216 remote_desc_in
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
,
219 if (remote_desc_in
== -1)
220 perror_with_name ("Accept failed");
222 /* Enable TCP keep alive process. */
224 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
225 (char *) &tmp
, sizeof (tmp
));
227 /* Tell TCP not to delay small packets. This greatly speeds up
228 interactive response. */
230 setsockopt (remote_desc_in
, IPPROTO_TCP
, TCP_NODELAY
,
231 (char *) &tmp
, sizeof (tmp
));
233 if (getnameinfo ((struct sockaddr
*) &sockaddr
, sockaddrsize
,
234 orig_host
, sizeof (orig_host
),
235 orig_port
, sizeof (orig_port
),
236 NI_NUMERICHOST
| NI_NUMERICSERV
) == 0)
238 fprintf (stderr
, "Remote debugging from host %s, port %s\n",
239 orig_host
, orig_port
);
244 close (tmp_desc
); /* No longer need this */
246 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
247 gdbreplay simply exits when
248 the remote side dies. */
250 closesocket (tmp_desc
); /* No longer need this */
254 #if defined(F_SETFL) && defined (FASYNC)
255 fcntl (remote_desc_in
, F_SETFL
, FASYNC
);
257 remote_desc_out
= remote_desc_in
;
259 fprintf (stderr
, "Replay logfile using %s\n", name
);
277 /* Treat \r\n as a newline. */
287 fputc (ch
== EOL
? '\n' : '\r', stderr
);
323 ch
= fromhex (ch2
) << 4;
330 /* Treat any other char as just itself */
342 unsigned char fromgdb
;
344 if (read (desc
, &fromgdb
, 1) != 1)
350 /* Accept input from gdb and match with chars from fp (after skipping one
351 blank) up until a \n is read from fp (which is not matched) */
359 if ((fromlog
= logchar (fp
)) != ' ')
361 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
366 fromlog
= logchar (fp
);
369 fromgdb
= gdbchar (remote_desc_in
);
371 remote_error ("Error during read from gdb");
373 while (fromlog
== fromgdb
);
377 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
382 /* Play data back to gdb from fp (after skipping leading blank) up until a
383 \n is read from fp (which is discarded and not sent to gdb). */
391 if ((fromlog
= logchar (fp
)) != ' ')
393 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
396 while ((fromlog
= logchar (fp
)) != EOL
)
399 if (write (remote_desc_out
, &ch
, 1) != 1)
400 remote_error ("Error during write to gdb");
405 gdbreplay_version (void)
407 printf ("GNU gdbreplay %s%s\n"
408 "Copyright (C) 2023 Free Software Foundation, Inc.\n"
409 "gdbreplay is free software, covered by "
410 "the GNU General Public License.\n"
411 "This gdbreplay was configured as \"%s\"\n",
412 PKGVERSION
, version
, host_name
);
416 gdbreplay_usage (FILE *stream
)
418 fprintf (stream
, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
419 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
420 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
423 /* Main function. This is called by the real "main" function,
424 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
426 static void ATTRIBUTE_NORETURN
427 captured_main (int argc
, char *argv
[])
432 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
434 gdbreplay_version ();
437 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
439 gdbreplay_usage (stdout
);
445 gdbreplay_usage (stderr
);
448 fp
= fopen (argv
[1], "r");
451 perror_with_name (argv
[1]);
453 remote_open (argv
[2]);
454 while ((ch
= logchar (fp
)) != EOF
)
459 /* data sent from gdb to gdbreplay, accept and match it */
463 /* data sent from gdbreplay to gdb, play it */
467 /* Command executed by gdb */
468 while ((ch
= logchar (fp
)) != EOL
);
477 main (int argc
, char *argv
[])
481 captured_main (argc
, argv
);
483 catch (const gdb_exception
&exception
)
485 if (exception
.reason
== RETURN_ERROR
)
488 fprintf (stderr
, "%s\n", exception
.what ());
494 gdb_assert_not_reached ("captured_main should never return");