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