arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / gdbserver / gdbreplay.cc
bloba2c77ce958850a235336051add678d8b48a33d17
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 /* 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). */
408 static void
409 play (FILE *fp)
411 int fromlog;
412 char ch;
414 if ((fromlog = logchar (fp, false)) != ' ')
416 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
417 fromlog);
419 while ((fromlog = logchar (fp, false)) != EOL)
421 ch = fromlog;
422 if (write (remote_desc_out, &ch, 1) != 1)
423 remote_error ("Error during write to gdb");
427 static void
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);
438 static void
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[])
452 FILE *fp;
453 int ch, optc;
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)
465 switch (optc)
467 case OPT_VERSION:
468 gdbreplay_version ();
469 exit (0);
470 case OPT_HELP:
471 gdbreplay_usage (stdout);
472 exit (0);
473 case OPT_LOGGING:
474 debug_logging = true;
475 break;
479 if (optind + 2 != argc)
481 gdbreplay_usage (stderr);
482 exit (1);
484 fp = fopen (argv[optind], "r");
485 if (fp == NULL)
487 perror_with_name (argv[optind]);
489 remote_open (argv[optind + 1]);
490 while ((ch = logchar (fp, false)) != EOF)
492 switch (ch)
494 case 'w':
495 /* data sent from gdb to gdbreplay, accept and match it */
496 expect (fp);
497 break;
498 case 'r':
499 /* data sent from gdbreplay to gdb, play it */
500 play (fp);
501 break;
502 case 'c':
503 /* We want to always print the command executed by GDB. */
504 if (!debug_logging)
506 fprintf (stderr, "\n");
507 fprintf (stderr, "Command expected from GDB:\n");
509 while ((ch = logchar (fp, true)) != EOL);
510 break;
511 case 'E':
512 if (!debug_logging)
513 fprintf (stderr, "E");
514 while ((ch = logchar (fp, true)) != EOL);
515 break;
518 remote_close ();
519 exit (0);
523 main (int argc, char *argv[])
527 captured_main (argc, argv);
529 catch (const gdb_exception &exception)
531 if (exception.reason == RETURN_ERROR)
533 fflush (stdout);
534 fprintf (stderr, "%s\n", exception.what ());
537 exit (1);
540 gdb_assert_not_reached ("captured_main should never return");