Add translations for various sub-directories
[binutils-gdb.git] / gdbserver / gdbreplay.cc
blob5c7821aa3a695426c4e66e7c7b312c8c365ece5b
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/>. */
20 #undef PACKAGE
21 #undef PACKAGE_NAME
22 #undef PACKAGE_VERSION
23 #undef PACKAGE_STRING
24 #undef PACKAGE_TARNAME
26 #include <config.h>
27 #include "gdbsupport/version.h"
29 #if HAVE_SYS_FILE_H
30 #include <sys/file.h>
31 #endif
32 #if HAVE_SIGNAL_H
33 #include <signal.h>
34 #endif
35 #include <ctype.h>
36 #if HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #include <unistd.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
45 #endif
46 #if HAVE_NETDB_H
47 #include <netdb.h>
48 #endif
49 #if HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
51 #endif
53 #if USE_WIN32API
54 #include <ws2tcpip.h>
55 #endif
57 #include "gdbsupport/netstuff.h"
58 #include "gdbsupport/rsp-low.h"
60 #include "getopt.h"
62 #ifndef HAVE_SOCKLEN_T
63 typedef int socklen_t;
64 #endif
66 /* Sort of a hack... */
67 #define EOL (EOF - 1)
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
72 gdbreplay. */
73 bool debug_logging = false;
75 static void
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);
81 fflush (stderr);
82 exit (1);
85 static void
86 remote_error (const char *desc)
88 fprintf (stderr, "\n%s\n", desc);
89 fflush (stderr);
90 exit (1);
93 static void
94 remote_close (void)
96 #ifdef USE_WIN32API
97 gdb_assert (remote_desc_in == remote_desc_out);
98 closesocket (remote_desc_in);
99 #else
100 close (remote_desc_in);
101 if (remote_desc_in != remote_desc_out)
102 close (remote_desc_out);
103 #endif
106 /* Open a connection to a remote debugger.
107 NAME is the filename used for communication. */
109 static void
110 remote_open (const char *name)
112 #ifndef USE_WIN32API
113 if (strcmp (name, "-") == 0)
115 remote_desc_in = 0;
116 remote_desc_out = 1;
117 return;
119 #endif
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);
126 fflush (stderr);
127 exit (1);
130 #ifdef USE_WIN32API
131 static int winsock_initialized;
132 #endif
133 int tmp;
134 int tmp_desc;
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
140 AF_UNSPEC. */
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);
150 #ifdef USE_WIN32API
151 if (!winsock_initialized)
153 WSADATA wsad;
155 WSAStartup (MAKEWORD (1, 0), &wsad);
156 winsock_initialized = 1;
158 #endif
160 int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
161 &hint, &ainfo);
163 if (r != 0)
165 fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
166 parsed.host_str.c_str (), parsed.port_str.c_str (),
167 gai_strerror (r));
168 fflush (stderr);
169 exit (1);
172 scoped_free_addrinfo free_ainfo (ainfo);
174 struct addrinfo *p;
176 for (p = ainfo; p != NULL; p = p->ai_next)
178 tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
180 if (tmp_desc >= 0)
181 break;
184 if (p == NULL)
185 perror_with_name ("Cannot open socket");
187 /* Allow rapid reuse of this port. */
188 tmp = 1;
189 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
190 sizeof (tmp));
192 switch (p->ai_family)
194 case AF_INET:
195 ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
196 break;
197 case AF_INET6:
198 ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
199 break;
200 default:
201 fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
202 exit (1);
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;
210 else
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,
220 &sockaddrsize);
222 if (remote_desc_in == -1)
223 perror_with_name ("Accept failed");
225 /* Enable TCP keep alive process. */
226 tmp = 1;
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. */
232 tmp = 1;
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);
243 fflush (stderr);
246 #ifndef USE_WIN32API
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. */
252 #else
253 closesocket (tmp_desc); /* No longer need this */
254 #endif
257 #if defined(F_SETFL) && defined (FASYNC)
258 fcntl (remote_desc_in, F_SETFL, FASYNC);
259 #endif
260 remote_desc_out = remote_desc_in;
262 fprintf (stderr, "Replay logfile using %s\n", name);
263 fflush (stderr);
266 static int
267 logchar (FILE *fp, bool print)
269 int ch;
270 int ch2;
272 ch = fgetc (fp);
273 if (ch != '\r' && (print || debug_logging))
275 fputc (ch, stderr);
276 fflush (stderr);
278 switch (ch)
280 /* Treat \r\n as a newline. */
281 case '\r':
282 ch = fgetc (fp);
283 if (ch == '\n')
284 ch = EOL;
285 else
287 ungetc (ch, fp);
288 ch = '\r';
290 if (print || debug_logging)
292 fputc (ch == EOL ? '\n' : '\r', stderr);
293 fflush (stderr);
295 break;
296 case '\n':
297 ch = EOL;
298 break;
299 case '\\':
300 ch = fgetc (fp);
301 if (print || debug_logging)
303 fputc (ch, stderr);
304 fflush (stderr);
306 switch (ch)
308 case '\\':
309 break;
310 case 'b':
311 ch = '\b';
312 break;
313 case 'f':
314 ch = '\f';
315 break;
316 case 'n':
317 ch = '\n';
318 break;
319 case 'r':
320 ch = '\r';
321 break;
322 case 't':
323 ch = '\t';
324 break;
325 case 'v':
326 ch = '\v';
327 break;
328 case 'x':
329 ch2 = fgetc (fp);
330 if (print || debug_logging)
332 fputc (ch2, stderr);
333 fflush (stderr);
335 ch = fromhex (ch2) << 4;
336 ch2 = fgetc (fp);
337 if (print || debug_logging)
339 fputc (ch2, stderr);
340 fflush (stderr);
342 ch |= fromhex (ch2);
343 break;
344 case 'c':
345 fputc (ch, stderr);
346 fflush (stderr);
347 break;
348 case 'E':
349 fputc (ch, stderr);
350 fflush (stderr);
351 break;
352 default:
353 /* Treat any other char as just itself */
354 break;
356 default:
357 break;
359 return (ch);
362 static int
363 gdbchar (int desc)
365 unsigned char fromgdb;
367 if (read (desc, &fromgdb, 1) != 1)
368 return -1;
369 else
370 return fromgdb;
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) */
376 static void
377 expect (FILE *fp)
379 int fromlog;
380 int fromgdb;
382 if ((fromlog = logchar (fp, false)) != ' ')
384 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
385 fromlog);
389 fromlog = logchar (fp, false);
390 if (fromlog == EOL)
391 break;
392 fromgdb = gdbchar (remote_desc_in);
393 if (fromgdb < 0)
394 remote_error ("Error during read from gdb");
396 while (fromlog == fromgdb);
398 if (fromlog != EOL)
400 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
401 fromgdb);
405 /* Calculate checksum for the packet stored in buffer buf. Store
406 the checksum in a hexadecimal format in a checksum_hex variable. */
407 static void
408 recalculate_csum (const std::string &buf, int off, unsigned char *checksum_hex)
410 unsigned char csum = 0;
412 int len = buf.length ();
413 for (int i = off; i < len; ++i)
414 csum += buf[i];
416 checksum_hex[0] = tohex ((csum >> 4) & 0xf);
417 checksum_hex[1] = tohex (csum & 0xf);
420 /* Play data back to gdb from fp (after skipping leading blank) up until a
421 \n is read from fp (which is discarded and not sent to gdb). */
423 static void
424 play (FILE *fp)
426 int fromlog;
427 int where_csum = 0, offset = 1;
428 unsigned char checksum[2] = {0, 0};
429 std::string line;
432 if ((fromlog = logchar (fp, false)) != ' ')
434 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
435 fromlog);
437 while ((fromlog = logchar (fp, false)) != EOL)
439 if (fromlog == '#')
440 where_csum = line.length ();
441 line.push_back (fromlog);
444 /* Packet starts with '+$' or '$', we don't want to calculate those
445 to the checksum, substract the offset to adjust the line length.
446 If the line starts with '$', the offset remains set to 1. */
447 if (line[0] == '+')
448 offset = 2;
450 if (where_csum > 0)
451 line.resize (where_csum);
452 recalculate_csum (line, offset, checksum);
454 line.push_back ('#');
455 line.push_back (checksum[0]);
456 line.push_back (checksum[1]);
458 if (write (remote_desc_out, line.data (), line.size ()) != line.size ())
459 remote_error ("Error during write to gdb");
462 static void
463 gdbreplay_version (void)
465 printf ("GNU gdbreplay %s%s\n"
466 "Copyright (C) 2024 Free Software Foundation, Inc.\n"
467 "gdbreplay is free software, covered by "
468 "the GNU General Public License.\n"
469 "This gdbreplay was configured as \"%s\"\n",
470 PKGVERSION, version, host_name);
473 static void
474 gdbreplay_usage (FILE *stream)
476 fprintf (stream, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
479 static void
480 gdbreplay_help ()
482 gdbreplay_usage (stdout);
484 printf ("\n");
485 printf ("LOGFILE is a file generated by 'set remotelogfile' in gdb.\n");
486 printf ("COMM may either be a tty device (for serial debugging),\n");
487 printf ("HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use\n");
488 printf ("stdin/stdout of gdbserver.\n");
489 printf ("\n");
491 printf ("Options:\n\n");
492 printf (" --debug-logging Show packets as they are processed.\n");
493 printf (" --help Print this message and then exit.\n");
494 printf (" --version Display version information and then exit.\n");
495 if (REPORT_BUGS_TO[0])
497 printf ("\n");
498 printf ("Report bugs to \"%s\".\n", REPORT_BUGS_TO);
502 /* Main function. This is called by the real "main" function,
503 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
505 [[noreturn]] static void
506 captured_main (int argc, char *argv[])
508 FILE *fp;
509 int ch, optc;
510 enum opts { OPT_VERSION = 1, OPT_HELP, OPT_LOGGING };
511 static struct option longopts[] =
513 {"version", no_argument, nullptr, OPT_VERSION},
514 {"help", no_argument, nullptr, OPT_HELP},
515 {"debug-logging", no_argument, nullptr, OPT_LOGGING},
516 {nullptr, no_argument, nullptr, 0}
519 while ((optc = getopt_long (argc, argv, "", longopts, nullptr)) != -1)
521 switch (optc)
523 case OPT_VERSION:
524 gdbreplay_version ();
525 exit (0);
526 case OPT_HELP:
527 gdbreplay_help ();
528 exit (0);
529 case OPT_LOGGING:
530 debug_logging = true;
531 break;
533 case '?':
534 fprintf (stderr,
535 "Use 'gdbreplay --help' for a complete list of options.\n");
536 exit (1);
540 if (optind + 2 != argc)
542 gdbreplay_usage (stderr);
543 exit (1);
545 fp = fopen (argv[optind], "r");
546 if (fp == NULL)
548 perror_with_name (argv[optind]);
550 remote_open (argv[optind + 1]);
551 while ((ch = logchar (fp, false)) != EOF)
553 switch (ch)
555 case 'w':
556 /* data sent from gdb to gdbreplay, accept and match it */
557 expect (fp);
558 break;
559 case 'r':
560 /* data sent from gdbreplay to gdb, play it */
561 play (fp);
562 break;
563 case 'c':
564 /* We want to always print the command executed by GDB. */
565 if (!debug_logging)
567 fprintf (stderr, "\n");
568 fprintf (stderr, "Command expected from GDB:\n");
570 while ((ch = logchar (fp, true)) != EOL);
571 break;
572 case 'E':
573 if (!debug_logging)
574 fprintf (stderr, "E");
575 while ((ch = logchar (fp, true)) != EOL);
576 break;
579 remote_close ();
580 exit (0);
584 main (int argc, char *argv[])
588 captured_main (argc, argv);
590 catch (const gdb_exception &exception)
592 if (exception.reason == RETURN_ERROR)
594 fflush (stdout);
595 fprintf (stderr, "%s\n", exception.what ());
598 exit (1);
601 gdb_assert_not_reached ("captured_main should never return");