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"
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
;
71 /* When true all packets are printed to stderr as they are handled by
73 bool debug_logging
= false;
76 sync_error (FILE *fp
, const char *desc
, int expect
, int got
)
78 fprintf (stderr
, "\n%s\n", desc
);
79 fprintf (stderr
, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
80 ftell (fp
), expect
, got
);
86 remote_error (const char *desc
)
88 fprintf (stderr
, "\n%s\n", desc
);
97 gdb_assert (remote_desc_in
== remote_desc_out
);
98 closesocket (remote_desc_in
);
100 close (remote_desc_in
);
101 if (remote_desc_in
!= remote_desc_out
)
102 close (remote_desc_out
);
106 /* Open a connection to a remote debugger.
107 NAME is the filename used for communication. */
110 remote_open (const char *name
)
113 if (strcmp (name
, "-") == 0)
121 const char *last_colon
= strrchr (name
, ':');
123 if (last_colon
== NULL
)
125 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
131 static int winsock_initialized
;
135 struct addrinfo hint
;
136 struct addrinfo
*ainfo
;
138 memset (&hint
, 0, sizeof (hint
));
139 /* Assume no prefix will be passed, therefore we should use
141 hint
.ai_family
= AF_UNSPEC
;
142 hint
.ai_socktype
= SOCK_STREAM
;
143 hint
.ai_protocol
= IPPROTO_TCP
;
145 parsed_connection_spec parsed
= parse_connection_spec (name
, &hint
);
147 if (parsed
.port_str
.empty ())
148 error (_("Missing port on hostname '%s'"), name
);
151 if (!winsock_initialized
)
155 WSAStartup (MAKEWORD (1, 0), &wsad
);
156 winsock_initialized
= 1;
160 int r
= getaddrinfo (parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
165 fprintf (stderr
, "%s:%s: cannot resolve name: %s\n",
166 parsed
.host_str
.c_str (), parsed
.port_str
.c_str (),
172 scoped_free_addrinfo
free_ainfo (ainfo
);
176 for (p
= ainfo
; p
!= NULL
; p
= p
->ai_next
)
178 tmp_desc
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
185 perror_with_name ("Cannot open socket");
187 /* Allow rapid reuse of this port. */
189 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
,
192 switch (p
->ai_family
)
195 ((struct sockaddr_in
*) p
->ai_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
198 ((struct sockaddr_in6
*) p
->ai_addr
)->sin6_addr
= in6addr_any
;
201 fprintf (stderr
, "Invalid 'ai_family' %d\n", p
->ai_family
);
205 if (bind (tmp_desc
, p
->ai_addr
, p
->ai_addrlen
) != 0)
206 perror_with_name ("Can't bind address");
208 if (p
->ai_socktype
== SOCK_DGRAM
)
209 remote_desc_in
= tmp_desc
;
212 struct sockaddr_storage sockaddr
;
213 socklen_t sockaddrsize
= sizeof (sockaddr
);
214 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
216 if (listen (tmp_desc
, 1) != 0)
217 perror_with_name ("Can't listen on socket");
219 remote_desc_in
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
,
222 if (remote_desc_in
== -1)
223 perror_with_name ("Accept failed");
225 /* Enable TCP keep alive process. */
227 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
228 (char *) &tmp
, sizeof (tmp
));
230 /* Tell TCP not to delay small packets. This greatly speeds up
231 interactive response. */
233 setsockopt (remote_desc_in
, IPPROTO_TCP
, TCP_NODELAY
,
234 (char *) &tmp
, sizeof (tmp
));
236 if (getnameinfo ((struct sockaddr
*) &sockaddr
, sockaddrsize
,
237 orig_host
, sizeof (orig_host
),
238 orig_port
, sizeof (orig_port
),
239 NI_NUMERICHOST
| NI_NUMERICSERV
) == 0)
241 fprintf (stderr
, "Remote debugging from host %s, port %s\n",
242 orig_host
, orig_port
);
247 close (tmp_desc
); /* No longer need this */
249 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
250 gdbreplay simply exits when
251 the remote side dies. */
253 closesocket (tmp_desc
); /* No longer need this */
257 #if defined(F_SETFL) && defined (FASYNC)
258 fcntl (remote_desc_in
, F_SETFL
, FASYNC
);
260 remote_desc_out
= remote_desc_in
;
262 fprintf (stderr
, "Replay logfile using %s\n", name
);
267 logchar (FILE *fp
, bool print
)
273 if (ch
!= '\r' && (print
|| debug_logging
))
280 /* Treat \r\n as a newline. */
290 if (print
|| debug_logging
)
292 fputc (ch
== EOL
? '\n' : '\r', stderr
);
301 if (print
|| debug_logging
)
330 if (print
|| debug_logging
)
335 ch
= fromhex (ch2
) << 4;
337 if (print
|| debug_logging
)
353 /* Treat any other char as just itself */
365 unsigned char fromgdb
;
367 if (read (desc
, &fromgdb
, 1) != 1)
373 /* Accept input from gdb and match with chars from fp (after skipping one
374 blank) up until a \n is read from fp (which is not matched) */
382 if ((fromlog
= logchar (fp
, false)) != ' ')
384 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
389 fromlog
= logchar (fp
, false);
392 fromgdb
= gdbchar (remote_desc_in
);
394 remote_error ("Error during read from gdb");
396 while (fromlog
== fromgdb
);
400 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
405 /* Play data back to gdb from fp (after skipping leading blank) up until a
406 \n is read from fp (which is discarded and not sent to gdb). */
414 if ((fromlog
= logchar (fp
, false)) != ' ')
416 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
419 while ((fromlog
= logchar (fp
, false)) != EOL
)
422 if (write (remote_desc_out
, &ch
, 1) != 1)
423 remote_error ("Error during write to gdb");
428 gdbreplay_version (void)
430 printf ("GNU gdbreplay %s%s\n"
431 "Copyright (C) 2024 Free Software Foundation, Inc.\n"
432 "gdbreplay is free software, covered by "
433 "the GNU General Public License.\n"
434 "This gdbreplay was configured as \"%s\"\n",
435 PKGVERSION
, version
, host_name
);
439 gdbreplay_usage (FILE *stream
)
441 fprintf (stream
, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
442 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
443 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
446 /* Main function. This is called by the real "main" function,
447 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
449 [[noreturn
]] static void
450 captured_main (int argc
, char *argv
[])
454 enum opts
{ OPT_VERSION
= 1, OPT_HELP
, OPT_LOGGING
};
455 static struct option longopts
[] =
457 {"version", no_argument
, nullptr, OPT_VERSION
},
458 {"help", no_argument
, nullptr, OPT_HELP
},
459 {"debug-logging", no_argument
, nullptr, OPT_LOGGING
},
460 {nullptr, no_argument
, nullptr, 0}
463 while ((optc
= getopt_long (argc
, argv
, "", longopts
, nullptr)) != -1)
468 gdbreplay_version ();
471 gdbreplay_usage (stdout
);
474 debug_logging
= true;
479 if (optind
+ 2 != argc
)
481 gdbreplay_usage (stderr
);
484 fp
= fopen (argv
[optind
], "r");
487 perror_with_name (argv
[optind
]);
489 remote_open (argv
[optind
+ 1]);
490 while ((ch
= logchar (fp
, false)) != EOF
)
495 /* data sent from gdb to gdbreplay, accept and match it */
499 /* data sent from gdbreplay to gdb, play it */
503 /* We want to always print the command executed by GDB. */
506 fprintf (stderr
, "\n");
507 fprintf (stderr
, "Command expected from GDB:\n");
509 while ((ch
= logchar (fp
, true)) != EOL
);
513 fprintf (stderr
, "E");
514 while ((ch
= logchar (fp
, true)) != EOL
);
523 main (int argc
, char *argv
[])
527 captured_main (argc
, argv
);
529 catch (const gdb_exception
&exception
)
531 if (exception
.reason
== RETURN_ERROR
)
534 fprintf (stderr
, "%s\n", exception
.what ());
540 gdb_assert_not_reached ("captured_main should never return");