x86: infer operand count of templates
[deliverable/binutils-gdb.git] / gdbserver / gdbreplay.cc
1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
3 Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "gdbsupport/common-defs.h"
21
22 #undef PACKAGE
23 #undef PACKAGE_NAME
24 #undef PACKAGE_VERSION
25 #undef PACKAGE_STRING
26 #undef PACKAGE_TARNAME
27
28 #include <config.h>
29 #include "gdbsupport/version.h"
30
31 #if HAVE_SYS_FILE_H
32 #include <sys/file.h>
33 #endif
34 #if HAVE_SIGNAL_H
35 #include <signal.h>
36 #endif
37 #include <ctype.h>
38 #if HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
41 #include <unistd.h>
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #if HAVE_NETDB_H
49 #include <netdb.h>
50 #endif
51 #if HAVE_NETINET_TCP_H
52 #include <netinet/tcp.h>
53 #endif
54
55 #if USE_WIN32API
56 #include <ws2tcpip.h>
57 #endif
58
59 #include "gdbsupport/netstuff.h"
60 #include "gdbsupport/rsp-low.h"
61
62 #ifndef HAVE_SOCKLEN_T
63 typedef int socklen_t;
64 #endif
65
66 /* Sort of a hack... */
67 #define EOL (EOF - 1)
68
69 static int remote_desc_in;
70 static int remote_desc_out;
71
72 #ifdef __MINGW32CE__
73
74 #ifndef COUNTOF
75 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
76 #endif
77
78 #define errno (GetLastError ())
79
80 char *
81 strerror (DWORD error)
82 {
83 static char buf[1024];
84 WCHAR *msgbuf;
85 DWORD lasterr = GetLastError ();
86 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
87 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
88 NULL,
89 error,
90 0, /* Default language */
91 (LPVOID)&msgbuf,
92 0,
93 NULL);
94 if (chars != 0)
95 {
96 /* If there is an \r\n appended, zap it. */
97 if (chars >= 2
98 && msgbuf[chars - 2] == '\r'
99 && msgbuf[chars - 1] == '\n')
100 {
101 chars -= 2;
102 msgbuf[chars] = 0;
103 }
104
105 if (chars > ((COUNTOF (buf)) - 1))
106 {
107 chars = COUNTOF (buf) - 1;
108 msgbuf [chars] = 0;
109 }
110
111 wcstombs (buf, msgbuf, chars + 1);
112 LocalFree (msgbuf);
113 }
114 else
115 sprintf (buf, "unknown win32 error (%ld)", error);
116
117 SetLastError (lasterr);
118 return buf;
119 }
120
121 #endif /* __MINGW32CE__ */
122
123 static void
124 sync_error (FILE *fp, const char *desc, int expect, int got)
125 {
126 fprintf (stderr, "\n%s\n", desc);
127 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
128 ftell (fp), expect, got);
129 fflush (stderr);
130 exit (1);
131 }
132
133 static void
134 remote_error (const char *desc)
135 {
136 fprintf (stderr, "\n%s\n", desc);
137 fflush (stderr);
138 exit (1);
139 }
140
141 static void
142 remote_close (void)
143 {
144 #ifdef USE_WIN32API
145 gdb_assert (remote_desc_in == remote_desc_out);
146 closesocket (remote_desc_in);
147 #else
148 close (remote_desc_in);
149 if (remote_desc_in != remote_desc_out)
150 close (remote_desc_out);
151 #endif
152 }
153
154 /* Open a connection to a remote debugger.
155 NAME is the filename used for communication. */
156
157 static void
158 remote_open (const char *name)
159 {
160 #ifndef USE_WIN32API
161 if (strcmp (name, "-") == 0)
162 {
163 remote_desc_in = 0;
164 remote_desc_out = 1;
165 return;
166 }
167 #endif
168
169 const char *last_colon = strrchr (name, ':');
170
171 if (last_colon == NULL)
172 {
173 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
174 fflush (stderr);
175 exit (1);
176 }
177
178 #ifdef USE_WIN32API
179 static int winsock_initialized;
180 #endif
181 int tmp;
182 int tmp_desc;
183 struct addrinfo hint;
184 struct addrinfo *ainfo;
185
186 memset (&hint, 0, sizeof (hint));
187 /* Assume no prefix will be passed, therefore we should use
188 AF_UNSPEC. */
189 hint.ai_family = AF_UNSPEC;
190 hint.ai_socktype = SOCK_STREAM;
191 hint.ai_protocol = IPPROTO_TCP;
192
193 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
194
195 if (parsed.port_str.empty ())
196 error (_("Missing port on hostname '%s'"), name);
197
198 #ifdef USE_WIN32API
199 if (!winsock_initialized)
200 {
201 WSADATA wsad;
202
203 WSAStartup (MAKEWORD (1, 0), &wsad);
204 winsock_initialized = 1;
205 }
206 #endif
207
208 int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
209 &hint, &ainfo);
210
211 if (r != 0)
212 {
213 fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
214 parsed.host_str.c_str (), parsed.port_str.c_str (),
215 gai_strerror (r));
216 fflush (stderr);
217 exit (1);
218 }
219
220 scoped_free_addrinfo free_ainfo (ainfo);
221
222 struct addrinfo *p;
223
224 for (p = ainfo; p != NULL; p = p->ai_next)
225 {
226 tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
227
228 if (tmp_desc >= 0)
229 break;
230 }
231
232 if (p == NULL)
233 perror_with_name ("Cannot open socket");
234
235 /* Allow rapid reuse of this port. */
236 tmp = 1;
237 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
238 sizeof (tmp));
239
240 switch (p->ai_family)
241 {
242 case AF_INET:
243 ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
244 break;
245 case AF_INET6:
246 ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
247 break;
248 default:
249 fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
250 exit (1);
251 }
252
253 if (bind (tmp_desc, p->ai_addr, p->ai_addrlen) != 0)
254 perror_with_name ("Can't bind address");
255
256 if (p->ai_socktype == SOCK_DGRAM)
257 remote_desc_in = tmp_desc;
258 else
259 {
260 struct sockaddr_storage sockaddr;
261 socklen_t sockaddrsize = sizeof (sockaddr);
262 char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
263
264 if (listen (tmp_desc, 1) != 0)
265 perror_with_name ("Can't listen on socket");
266
267 remote_desc_in = accept (tmp_desc, (struct sockaddr *) &sockaddr,
268 &sockaddrsize);
269
270 if (remote_desc_in == -1)
271 perror_with_name ("Accept failed");
272
273 /* Enable TCP keep alive process. */
274 tmp = 1;
275 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
276 (char *) &tmp, sizeof (tmp));
277
278 /* Tell TCP not to delay small packets. This greatly speeds up
279 interactive response. */
280 tmp = 1;
281 setsockopt (remote_desc_in, IPPROTO_TCP, TCP_NODELAY,
282 (char *) &tmp, sizeof (tmp));
283
284 if (getnameinfo ((struct sockaddr *) &sockaddr, sockaddrsize,
285 orig_host, sizeof (orig_host),
286 orig_port, sizeof (orig_port),
287 NI_NUMERICHOST | NI_NUMERICSERV) == 0)
288 {
289 fprintf (stderr, "Remote debugging from host %s, port %s\n",
290 orig_host, orig_port);
291 fflush (stderr);
292 }
293
294 #ifndef USE_WIN32API
295 close (tmp_desc); /* No longer need this */
296
297 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
298 gdbreplay simply exits when
299 the remote side dies. */
300 #else
301 closesocket (tmp_desc); /* No longer need this */
302 #endif
303 }
304
305 #if defined(F_SETFL) && defined (FASYNC)
306 fcntl (remote_desc_in, F_SETFL, FASYNC);
307 #endif
308 remote_desc_out = remote_desc_in;
309
310 fprintf (stderr, "Replay logfile using %s\n", name);
311 fflush (stderr);
312 }
313
314 static int
315 logchar (FILE *fp)
316 {
317 int ch;
318 int ch2;
319
320 ch = fgetc (fp);
321 if (ch != '\r')
322 {
323 fputc (ch, stderr);
324 fflush (stderr);
325 }
326 switch (ch)
327 {
328 /* Treat \r\n as a newline. */
329 case '\r':
330 ch = fgetc (fp);
331 if (ch == '\n')
332 ch = EOL;
333 else
334 {
335 ungetc (ch, fp);
336 ch = '\r';
337 }
338 fputc (ch == EOL ? '\n' : '\r', stderr);
339 fflush (stderr);
340 break;
341 case '\n':
342 ch = EOL;
343 break;
344 case '\\':
345 ch = fgetc (fp);
346 fputc (ch, stderr);
347 fflush (stderr);
348 switch (ch)
349 {
350 case '\\':
351 break;
352 case 'b':
353 ch = '\b';
354 break;
355 case 'f':
356 ch = '\f';
357 break;
358 case 'n':
359 ch = '\n';
360 break;
361 case 'r':
362 ch = '\r';
363 break;
364 case 't':
365 ch = '\t';
366 break;
367 case 'v':
368 ch = '\v';
369 break;
370 case 'x':
371 ch2 = fgetc (fp);
372 fputc (ch2, stderr);
373 fflush (stderr);
374 ch = fromhex (ch2) << 4;
375 ch2 = fgetc (fp);
376 fputc (ch2, stderr);
377 fflush (stderr);
378 ch |= fromhex (ch2);
379 break;
380 default:
381 /* Treat any other char as just itself */
382 break;
383 }
384 default:
385 break;
386 }
387 return (ch);
388 }
389
390 static int
391 gdbchar (int desc)
392 {
393 unsigned char fromgdb;
394
395 if (read (desc, &fromgdb, 1) != 1)
396 return -1;
397 else
398 return fromgdb;
399 }
400
401 /* Accept input from gdb and match with chars from fp (after skipping one
402 blank) up until a \n is read from fp (which is not matched) */
403
404 static void
405 expect (FILE *fp)
406 {
407 int fromlog;
408 int fromgdb;
409
410 if ((fromlog = logchar (fp)) != ' ')
411 {
412 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
413 fromlog);
414 }
415 do
416 {
417 fromlog = logchar (fp);
418 if (fromlog == EOL)
419 break;
420 fromgdb = gdbchar (remote_desc_in);
421 if (fromgdb < 0)
422 remote_error ("Error during read from gdb");
423 }
424 while (fromlog == fromgdb);
425
426 if (fromlog != EOL)
427 {
428 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
429 fromgdb);
430 }
431 }
432
433 /* Play data back to gdb from fp (after skipping leading blank) up until a
434 \n is read from fp (which is discarded and not sent to gdb). */
435
436 static void
437 play (FILE *fp)
438 {
439 int fromlog;
440 char ch;
441
442 if ((fromlog = logchar (fp)) != ' ')
443 {
444 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
445 fromlog);
446 }
447 while ((fromlog = logchar (fp)) != EOL)
448 {
449 ch = fromlog;
450 if (write (remote_desc_out, &ch, 1) != 1)
451 remote_error ("Error during write to gdb");
452 }
453 }
454
455 static void
456 gdbreplay_version (void)
457 {
458 printf ("GNU gdbreplay %s%s\n"
459 "Copyright (C) 2021 Free Software Foundation, Inc.\n"
460 "gdbreplay is free software, covered by "
461 "the GNU General Public License.\n"
462 "This gdbreplay was configured as \"%s\"\n",
463 PKGVERSION, version, host_name);
464 }
465
466 static void
467 gdbreplay_usage (FILE *stream)
468 {
469 fprintf (stream, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
470 if (REPORT_BUGS_TO[0] && stream == stdout)
471 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
472 }
473
474 /* Main function. This is called by the real "main" function,
475 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
476
477 static void ATTRIBUTE_NORETURN
478 captured_main (int argc, char *argv[])
479 {
480 FILE *fp;
481 int ch;
482
483 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
484 {
485 gdbreplay_version ();
486 exit (0);
487 }
488 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
489 {
490 gdbreplay_usage (stdout);
491 exit (0);
492 }
493
494 if (argc < 3)
495 {
496 gdbreplay_usage (stderr);
497 exit (1);
498 }
499 fp = fopen (argv[1], "r");
500 if (fp == NULL)
501 {
502 perror_with_name (argv[1]);
503 }
504 remote_open (argv[2]);
505 while ((ch = logchar (fp)) != EOF)
506 {
507 switch (ch)
508 {
509 case 'w':
510 /* data sent from gdb to gdbreplay, accept and match it */
511 expect (fp);
512 break;
513 case 'r':
514 /* data sent from gdbreplay to gdb, play it */
515 play (fp);
516 break;
517 case 'c':
518 /* Command executed by gdb */
519 while ((ch = logchar (fp)) != EOL);
520 break;
521 }
522 }
523 remote_close ();
524 exit (0);
525 }
526
527 int
528 main (int argc, char *argv[])
529 {
530 try
531 {
532 captured_main (argc, argv);
533 }
534 catch (const gdb_exception &exception)
535 {
536 if (exception.reason == RETURN_ERROR)
537 {
538 fflush (stdout);
539 fprintf (stderr, "%s\n", exception.what ());
540 }
541
542 exit (1);
543 }
544
545 gdb_assert_not_reached ("captured_main should never return");
546 }
This page took 0.068363 seconds and 4 git commands to generate.