Automatic date update in version.in
[binutils-gdb.git] / gdbserver / gdbreplay.cc
blob0219e865ec73c07380abf70f998a6f5d9cb10bfd
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 #ifndef HAVE_SOCKLEN_T
61 typedef int socklen_t;
62 #endif
64 /* Sort of a hack... */
65 #define EOL (EOF - 1)
67 static int remote_desc_in;
68 static int remote_desc_out;
70 static void
71 sync_error (FILE *fp, const char *desc, int expect, int got)
73 fprintf (stderr, "\n%s\n", desc);
74 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
75 ftell (fp), expect, got);
76 fflush (stderr);
77 exit (1);
80 static void
81 remote_error (const char *desc)
83 fprintf (stderr, "\n%s\n", desc);
84 fflush (stderr);
85 exit (1);
88 static void
89 remote_close (void)
91 #ifdef USE_WIN32API
92 gdb_assert (remote_desc_in == remote_desc_out);
93 closesocket (remote_desc_in);
94 #else
95 close (remote_desc_in);
96 if (remote_desc_in != remote_desc_out)
97 close (remote_desc_out);
98 #endif
101 /* Open a connection to a remote debugger.
102 NAME is the filename used for communication. */
104 static void
105 remote_open (const char *name)
107 #ifndef USE_WIN32API
108 if (strcmp (name, "-") == 0)
110 remote_desc_in = 0;
111 remote_desc_out = 1;
112 return;
114 #endif
116 const char *last_colon = strrchr (name, ':');
118 if (last_colon == NULL)
120 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
121 fflush (stderr);
122 exit (1);
125 #ifdef USE_WIN32API
126 static int winsock_initialized;
127 #endif
128 int tmp;
129 int tmp_desc;
130 struct addrinfo hint;
131 struct addrinfo *ainfo;
133 memset (&hint, 0, sizeof (hint));
134 /* Assume no prefix will be passed, therefore we should use
135 AF_UNSPEC. */
136 hint.ai_family = AF_UNSPEC;
137 hint.ai_socktype = SOCK_STREAM;
138 hint.ai_protocol = IPPROTO_TCP;
140 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
142 if (parsed.port_str.empty ())
143 error (_("Missing port on hostname '%s'"), name);
145 #ifdef USE_WIN32API
146 if (!winsock_initialized)
148 WSADATA wsad;
150 WSAStartup (MAKEWORD (1, 0), &wsad);
151 winsock_initialized = 1;
153 #endif
155 int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
156 &hint, &ainfo);
158 if (r != 0)
160 fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
161 parsed.host_str.c_str (), parsed.port_str.c_str (),
162 gai_strerror (r));
163 fflush (stderr);
164 exit (1);
167 scoped_free_addrinfo free_ainfo (ainfo);
169 struct addrinfo *p;
171 for (p = ainfo; p != NULL; p = p->ai_next)
173 tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
175 if (tmp_desc >= 0)
176 break;
179 if (p == NULL)
180 perror_with_name ("Cannot open socket");
182 /* Allow rapid reuse of this port. */
183 tmp = 1;
184 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
185 sizeof (tmp));
187 switch (p->ai_family)
189 case AF_INET:
190 ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
191 break;
192 case AF_INET6:
193 ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
194 break;
195 default:
196 fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
197 exit (1);
200 if (bind (tmp_desc, p->ai_addr, p->ai_addrlen) != 0)
201 perror_with_name ("Can't bind address");
203 if (p->ai_socktype == SOCK_DGRAM)
204 remote_desc_in = tmp_desc;
205 else
207 struct sockaddr_storage sockaddr;
208 socklen_t sockaddrsize = sizeof (sockaddr);
209 char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
211 if (listen (tmp_desc, 1) != 0)
212 perror_with_name ("Can't listen on socket");
214 remote_desc_in = accept (tmp_desc, (struct sockaddr *) &sockaddr,
215 &sockaddrsize);
217 if (remote_desc_in == -1)
218 perror_with_name ("Accept failed");
220 /* Enable TCP keep alive process. */
221 tmp = 1;
222 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
223 (char *) &tmp, sizeof (tmp));
225 /* Tell TCP not to delay small packets. This greatly speeds up
226 interactive response. */
227 tmp = 1;
228 setsockopt (remote_desc_in, IPPROTO_TCP, TCP_NODELAY,
229 (char *) &tmp, sizeof (tmp));
231 if (getnameinfo ((struct sockaddr *) &sockaddr, sockaddrsize,
232 orig_host, sizeof (orig_host),
233 orig_port, sizeof (orig_port),
234 NI_NUMERICHOST | NI_NUMERICSERV) == 0)
236 fprintf (stderr, "Remote debugging from host %s, port %s\n",
237 orig_host, orig_port);
238 fflush (stderr);
241 #ifndef USE_WIN32API
242 close (tmp_desc); /* No longer need this */
244 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
245 gdbreplay simply exits when
246 the remote side dies. */
247 #else
248 closesocket (tmp_desc); /* No longer need this */
249 #endif
252 #if defined(F_SETFL) && defined (FASYNC)
253 fcntl (remote_desc_in, F_SETFL, FASYNC);
254 #endif
255 remote_desc_out = remote_desc_in;
257 fprintf (stderr, "Replay logfile using %s\n", name);
258 fflush (stderr);
261 static int
262 logchar (FILE *fp)
264 int ch;
265 int ch2;
267 ch = fgetc (fp);
268 if (ch != '\r')
270 fputc (ch, stderr);
271 fflush (stderr);
273 switch (ch)
275 /* Treat \r\n as a newline. */
276 case '\r':
277 ch = fgetc (fp);
278 if (ch == '\n')
279 ch = EOL;
280 else
282 ungetc (ch, fp);
283 ch = '\r';
285 fputc (ch == EOL ? '\n' : '\r', stderr);
286 fflush (stderr);
287 break;
288 case '\n':
289 ch = EOL;
290 break;
291 case '\\':
292 ch = fgetc (fp);
293 fputc (ch, stderr);
294 fflush (stderr);
295 switch (ch)
297 case '\\':
298 break;
299 case 'b':
300 ch = '\b';
301 break;
302 case 'f':
303 ch = '\f';
304 break;
305 case 'n':
306 ch = '\n';
307 break;
308 case 'r':
309 ch = '\r';
310 break;
311 case 't':
312 ch = '\t';
313 break;
314 case 'v':
315 ch = '\v';
316 break;
317 case 'x':
318 ch2 = fgetc (fp);
319 fputc (ch2, stderr);
320 fflush (stderr);
321 ch = fromhex (ch2) << 4;
322 ch2 = fgetc (fp);
323 fputc (ch2, stderr);
324 fflush (stderr);
325 ch |= fromhex (ch2);
326 break;
327 default:
328 /* Treat any other char as just itself */
329 break;
331 default:
332 break;
334 return (ch);
337 static int
338 gdbchar (int desc)
340 unsigned char fromgdb;
342 if (read (desc, &fromgdb, 1) != 1)
343 return -1;
344 else
345 return fromgdb;
348 /* Accept input from gdb and match with chars from fp (after skipping one
349 blank) up until a \n is read from fp (which is not matched) */
351 static void
352 expect (FILE *fp)
354 int fromlog;
355 int fromgdb;
357 if ((fromlog = logchar (fp)) != ' ')
359 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
360 fromlog);
364 fromlog = logchar (fp);
365 if (fromlog == EOL)
366 break;
367 fromgdb = gdbchar (remote_desc_in);
368 if (fromgdb < 0)
369 remote_error ("Error during read from gdb");
371 while (fromlog == fromgdb);
373 if (fromlog != EOL)
375 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
376 fromgdb);
380 /* Play data back to gdb from fp (after skipping leading blank) up until a
381 \n is read from fp (which is discarded and not sent to gdb). */
383 static void
384 play (FILE *fp)
386 int fromlog;
387 char ch;
389 if ((fromlog = logchar (fp)) != ' ')
391 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
392 fromlog);
394 while ((fromlog = logchar (fp)) != EOL)
396 ch = fromlog;
397 if (write (remote_desc_out, &ch, 1) != 1)
398 remote_error ("Error during write to gdb");
402 static void
403 gdbreplay_version (void)
405 printf ("GNU gdbreplay %s%s\n"
406 "Copyright (C) 2024 Free Software Foundation, Inc.\n"
407 "gdbreplay is free software, covered by "
408 "the GNU General Public License.\n"
409 "This gdbreplay was configured as \"%s\"\n",
410 PKGVERSION, version, host_name);
413 static void
414 gdbreplay_usage (FILE *stream)
416 fprintf (stream, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
417 if (REPORT_BUGS_TO[0] && stream == stdout)
418 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
421 /* Main function. This is called by the real "main" function,
422 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
424 static void ATTRIBUTE_NORETURN
425 captured_main (int argc, char *argv[])
427 FILE *fp;
428 int ch;
430 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
432 gdbreplay_version ();
433 exit (0);
435 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
437 gdbreplay_usage (stdout);
438 exit (0);
441 if (argc < 3)
443 gdbreplay_usage (stderr);
444 exit (1);
446 fp = fopen (argv[1], "r");
447 if (fp == NULL)
449 perror_with_name (argv[1]);
451 remote_open (argv[2]);
452 while ((ch = logchar (fp)) != EOF)
454 switch (ch)
456 case 'w':
457 /* data sent from gdb to gdbreplay, accept and match it */
458 expect (fp);
459 break;
460 case 'r':
461 /* data sent from gdbreplay to gdb, play it */
462 play (fp);
463 break;
464 case 'c':
465 /* Command executed by gdb */
466 while ((ch = logchar (fp)) != EOL);
467 break;
470 remote_close ();
471 exit (0);
475 main (int argc, char *argv[])
479 captured_main (argc, argv);
481 catch (const gdb_exception &exception)
483 if (exception.reason == RETURN_ERROR)
485 fflush (stdout);
486 fprintf (stderr, "%s\n", exception.what ());
489 exit (1);
492 gdb_assert_not_reached ("captured_main should never return");