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 fprintf (stderr
, "Replay logfile using %s\n", name
);
210 if (p
->ai_socktype
== SOCK_DGRAM
)
211 remote_desc_in
= tmp_desc
;
214 struct sockaddr_storage sockaddr
;
215 socklen_t sockaddrsize
= sizeof (sockaddr
);
216 char orig_host
[GDB_NI_MAX_ADDR
], orig_port
[GDB_NI_MAX_PORT
];
218 if (listen (tmp_desc
, 1) != 0)
219 perror_with_name ("Can't listen on socket");
221 remote_desc_in
= accept (tmp_desc
, (struct sockaddr
*) &sockaddr
,
224 if (remote_desc_in
== -1)
225 perror_with_name ("Accept failed");
227 /* Enable TCP keep alive process. */
229 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
,
230 (char *) &tmp
, sizeof (tmp
));
232 /* Tell TCP not to delay small packets. This greatly speeds up
233 interactive response. */
235 setsockopt (remote_desc_in
, IPPROTO_TCP
, TCP_NODELAY
,
236 (char *) &tmp
, sizeof (tmp
));
238 if (getnameinfo ((struct sockaddr
*) &sockaddr
, sockaddrsize
,
239 orig_host
, sizeof (orig_host
),
240 orig_port
, sizeof (orig_port
),
241 NI_NUMERICHOST
| NI_NUMERICSERV
) == 0)
243 fprintf (stderr
, "Remote debugging from host %s, port %s\n",
244 orig_host
, orig_port
);
249 close (tmp_desc
); /* No longer need this */
251 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then
252 gdbreplay simply exits when
253 the remote side dies. */
255 closesocket (tmp_desc
); /* No longer need this */
259 #if defined(F_SETFL) && defined (FASYNC)
260 fcntl (remote_desc_in
, F_SETFL
, FASYNC
);
262 remote_desc_out
= remote_desc_in
;
266 logchar (FILE *fp
, bool print
)
272 if (ch
!= '\r' && (print
|| debug_logging
))
279 /* Treat \r\n as a newline. */
289 if (print
|| debug_logging
)
291 fputc (ch
== EOL
? '\n' : '\r', stderr
);
300 if (print
|| debug_logging
)
329 if (print
|| debug_logging
)
334 ch
= fromhex (ch2
) << 4;
336 if (print
|| debug_logging
)
352 /* Treat any other char as just itself */
364 unsigned char fromgdb
;
366 if (read (desc
, &fromgdb
, 1) != 1)
372 /* Accept input from gdb and match with chars from fp (after skipping one
373 blank) up until a \n is read from fp (which is not matched) */
381 if ((fromlog
= logchar (fp
, false)) != ' ')
383 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
388 fromlog
= logchar (fp
, false);
391 fromgdb
= gdbchar (remote_desc_in
);
393 remote_error ("Error during read from gdb");
395 while (fromlog
== fromgdb
);
399 sync_error (fp
, "Sync error during read of gdb packet from log", fromlog
,
404 /* Calculate checksum for the packet stored in buffer buf. Store
405 the checksum in a hexadecimal format in a checksum_hex variable. */
407 recalculate_csum (const std::string
&buf
, int off
, unsigned char *checksum_hex
)
409 unsigned char csum
= 0;
411 int len
= buf
.length ();
412 for (int i
= off
; i
< len
; ++i
)
415 checksum_hex
[0] = tohex ((csum
>> 4) & 0xf);
416 checksum_hex
[1] = tohex (csum
& 0xf);
419 /* Play data back to gdb from fp (after skipping leading blank) up until a
420 \n is read from fp (which is discarded and not sent to gdb). */
426 int where_csum
= 0, offset
= 1;
427 unsigned char checksum
[2] = {0, 0};
431 if ((fromlog
= logchar (fp
, false)) != ' ')
433 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
436 while ((fromlog
= logchar (fp
, false)) != EOL
)
439 where_csum
= line
.length ();
440 line
.push_back (fromlog
);
443 /* Packet starts with '+$' or '$', we don't want to calculate those
444 to the checksum, substract the offset to adjust the line length.
445 If the line starts with '$', the offset remains set to 1. */
450 line
.resize (where_csum
);
451 recalculate_csum (line
, offset
, checksum
);
453 line
.push_back ('#');
454 line
.push_back (checksum
[0]);
455 line
.push_back (checksum
[1]);
457 if (write (remote_desc_out
, line
.data (), line
.size ()) != line
.size ())
458 remote_error ("Error during write to gdb");
462 gdbreplay_version (void)
464 printf ("GNU gdbreplay %s%s\n"
465 "Copyright (C) 2024 Free Software Foundation, Inc.\n"
466 "gdbreplay is free software, covered by "
467 "the GNU General Public License.\n"
468 "This gdbreplay was configured as \"%s\"\n",
469 PKGVERSION
, version
, host_name
);
473 gdbreplay_usage (FILE *stream
)
475 fprintf (stream
, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
481 gdbreplay_usage (stdout
);
484 printf ("LOGFILE is a file generated by 'set remotelogfile' in gdb.\n");
485 printf ("COMM may either be a tty device (for serial debugging),\n");
486 printf ("HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use\n");
487 printf ("stdin/stdout of gdbserver.\n");
490 printf ("Options:\n\n");
491 printf (" --debug-logging Show packets as they are processed.\n");
492 printf (" --help Print this message and then exit.\n");
493 printf (" --version Display version information and then exit.\n");
494 if (REPORT_BUGS_TO
[0])
497 printf ("Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
501 /* Main function. This is called by the real "main" function,
502 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
504 [[noreturn
]] static void
505 captured_main (int argc
, char *argv
[])
509 enum opts
{ OPT_VERSION
= 1, OPT_HELP
, OPT_LOGGING
};
510 static struct option longopts
[] =
512 {"version", no_argument
, nullptr, OPT_VERSION
},
513 {"help", no_argument
, nullptr, OPT_HELP
},
514 {"debug-logging", no_argument
, nullptr, OPT_LOGGING
},
515 {nullptr, no_argument
, nullptr, 0}
518 while ((optc
= getopt_long (argc
, argv
, "", longopts
, nullptr)) != -1)
523 gdbreplay_version ();
529 debug_logging
= true;
534 "Use 'gdbreplay --help' for a complete list of options.\n");
539 if (optind
+ 2 != argc
)
541 gdbreplay_usage (stderr
);
544 fp
= fopen (argv
[optind
], "r");
547 perror_with_name (argv
[optind
]);
549 remote_open (argv
[optind
+ 1]);
550 while ((ch
= logchar (fp
, false)) != EOF
)
555 /* data sent from gdb to gdbreplay, accept and match it */
559 /* data sent from gdbreplay to gdb, play it */
563 /* We want to always print the command executed by GDB. */
566 fprintf (stderr
, "\n");
567 fprintf (stderr
, "Command expected from GDB:\n");
569 while ((ch
= logchar (fp
, true)) != EOL
);
573 fprintf (stderr
, "E");
574 while ((ch
= logchar (fp
, true)) != EOL
);
583 main (int argc
, char *argv
[])
587 captured_main (argc
, argv
);
589 catch (const gdb_exception
&exception
)
591 if (exception
.reason
== RETURN_ERROR
)
594 fprintf (stderr
, "%s\n", exception
.what ());
600 gdb_assert_not_reached ("captured_main should never return");