Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations
[deliverable/binutils-gdb.git] / gdb / gdbserver / gdbreplay.c
1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2015 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 "config.h"
21 #include "build-gnulib-gdbserver/config.h"
22 #include "version.h"
23
24 #include <stdio.h>
25 #if HAVE_SYS_FILE_H
26 #include <sys/file.h>
27 #endif
28 #if HAVE_SIGNAL_H
29 #include <signal.h>
30 #endif
31 #include <ctype.h>
32 #if HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #if HAVE_NETDB_H
40 #include <netdb.h>
41 #endif
42 #if HAVE_NETINET_TCP_H
43 #include <netinet/tcp.h>
44 #endif
45 #include "gdb_socket.h"
46
47 #include <alloca.h>
48
49
50 #ifndef HAVE_SOCKLEN_T
51 typedef int socklen_t;
52 #endif
53
54 /* Sort of a hack... */
55 #define EOL (EOF - 1)
56
57 static int remote_desc;
58
59 #ifdef __MINGW32CE__
60
61 #ifndef COUNTOF
62 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
63 #endif
64
65 #define errno (GetLastError ())
66
67 char *
68 strerror (DWORD error)
69 {
70 static char buf[1024];
71 WCHAR *msgbuf;
72 DWORD lasterr = GetLastError ();
73 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
74 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
75 NULL,
76 error,
77 0, /* Default language */
78 (LPVOID)&msgbuf,
79 0,
80 NULL);
81 if (chars != 0)
82 {
83 /* If there is an \r\n appended, zap it. */
84 if (chars >= 2
85 && msgbuf[chars - 2] == '\r'
86 && msgbuf[chars - 1] == '\n')
87 {
88 chars -= 2;
89 msgbuf[chars] = 0;
90 }
91
92 if (chars > ((COUNTOF (buf)) - 1))
93 {
94 chars = COUNTOF (buf) - 1;
95 msgbuf [chars] = 0;
96 }
97
98 wcstombs (buf, msgbuf, chars + 1);
99 LocalFree (msgbuf);
100 }
101 else
102 sprintf (buf, "unknown win32 error (%ld)", error);
103
104 SetLastError (lasterr);
105 return buf;
106 }
107
108 #endif /* __MINGW32CE__ */
109
110 /* Print the system error message for errno, and also mention STRING
111 as the file name for which the error was encountered.
112 Then return to command level. */
113
114 static void
115 perror_with_name (const char *string)
116 {
117 #ifndef STDC_HEADERS
118 extern int errno;
119 #endif
120 const char *err;
121 char *combined;
122
123 err = strerror (errno);
124 if (err == NULL)
125 err = "unknown error";
126
127 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
128 strcpy (combined, string);
129 strcat (combined, ": ");
130 strcat (combined, err);
131 fprintf (stderr, "\n%s.\n", combined);
132 fflush (stderr);
133 exit (1);
134 }
135
136 static void
137 sync_error (FILE *fp, char *desc, int expect, int got)
138 {
139 fprintf (stderr, "\n%s\n", desc);
140 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
141 ftell (fp), expect, got);
142 fflush (stderr);
143 exit (1);
144 }
145
146 static void
147 remote_error (const char *desc)
148 {
149 fprintf (stderr, "\n%s\n", desc);
150 fflush (stderr);
151 exit (1);
152 }
153
154 static void
155 remote_close (void)
156 {
157 #ifdef USE_WIN32API
158 closesocket (remote_desc);
159 #else
160 close (remote_desc);
161 #endif
162 }
163
164 /* Open a connection to a remote debugger.
165 NAME is the filename used for communication. */
166
167 static void
168 remote_open (char *name)
169 {
170 if (!strchr (name, ':'))
171 {
172 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
173 fflush (stderr);
174 exit (1);
175 }
176 else
177 {
178 #ifdef USE_WIN32API
179 static int winsock_initialized;
180 #endif
181 char *port_str;
182 int port;
183 union gdb_sockaddr_u sockaddr;
184 socklen_t tmp;
185 int tmp_desc;
186
187 port_str = strchr (name, ':');
188
189 port = atoi (port_str + 1);
190
191 #ifdef USE_WIN32API
192 if (!winsock_initialized)
193 {
194 WSADATA wsad;
195
196 WSAStartup (MAKEWORD (1, 0), &wsad);
197 winsock_initialized = 1;
198 }
199 #endif
200
201 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
202 if (tmp_desc == -1)
203 perror_with_name ("Can't open socket");
204
205 /* Allow rapid reuse of this port. */
206 tmp = 1;
207 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
208 sizeof (tmp));
209
210 sockaddr.sa_in.sin_family = PF_INET;
211 sockaddr.sa_in.sin_port = htons (port);
212 sockaddr.sa_in.sin_addr.s_addr = INADDR_ANY;
213
214 if (bind (tmp_desc, &sockaddr.sa, sizeof (sockaddr.sa_in))
215 || listen (tmp_desc, 1))
216 perror_with_name ("Can't bind address");
217
218 tmp = sizeof (sockaddr.sa_in);
219 remote_desc = accept (tmp_desc, &sockaddr.sa, &tmp);
220 if (remote_desc == -1)
221 perror_with_name ("Accept failed");
222
223 /* Enable TCP keep alive process. */
224 tmp = 1;
225 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
226 (char *) &tmp, sizeof (tmp));
227
228 /* Tell TCP not to delay small packets. This greatly speeds up
229 interactive response. */
230 tmp = 1;
231 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
232 (char *) &tmp, sizeof (tmp));
233
234 #ifndef USE_WIN32API
235 close (tmp_desc); /* No longer need this */
236
237 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
238 gdbreplay simply exits when
239 the remote side dies. */
240 #else
241 closesocket (tmp_desc); /* No longer need this */
242 #endif
243 }
244
245 #if defined(F_SETFL) && defined (FASYNC)
246 fcntl (remote_desc, F_SETFL, FASYNC);
247 #endif
248
249 fprintf (stderr, "Replay logfile using %s\n", name);
250 fflush (stderr);
251 }
252
253 static int
254 tohex (int ch)
255 {
256 if (ch >= '0' && ch <= '9')
257 {
258 return (ch - '0');
259 }
260 if (ch >= 'A' && ch <= 'F')
261 {
262 return (ch - 'A' + 10);
263 }
264 if (ch >= 'a' && ch <= 'f')
265 {
266 return (ch - 'a' + 10);
267 }
268 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
269 fflush (stderr);
270 exit (1);
271 }
272
273 static int
274 logchar (FILE *fp)
275 {
276 int ch;
277 int ch2;
278
279 ch = fgetc (fp);
280 fputc (ch, stdout);
281 fflush (stdout);
282 switch (ch)
283 {
284 case '\n':
285 ch = EOL;
286 break;
287 case '\\':
288 ch = fgetc (fp);
289 fputc (ch, stdout);
290 fflush (stdout);
291 switch (ch)
292 {
293 case '\\':
294 break;
295 case 'b':
296 ch = '\b';
297 break;
298 case 'f':
299 ch = '\f';
300 break;
301 case 'n':
302 ch = '\n';
303 break;
304 case 'r':
305 ch = '\r';
306 break;
307 case 't':
308 ch = '\t';
309 break;
310 case 'v':
311 ch = '\v';
312 break;
313 case 'x':
314 ch2 = fgetc (fp);
315 fputc (ch2, stdout);
316 fflush (stdout);
317 ch = tohex (ch2) << 4;
318 ch2 = fgetc (fp);
319 fputc (ch2, stdout);
320 fflush (stdout);
321 ch |= tohex (ch2);
322 break;
323 default:
324 /* Treat any other char as just itself */
325 break;
326 }
327 default:
328 break;
329 }
330 return (ch);
331 }
332
333 static int
334 gdbchar (int desc)
335 {
336 unsigned char fromgdb;
337
338 if (read (desc, &fromgdb, 1) != 1)
339 return -1;
340 else
341 return fromgdb;
342 }
343
344 /* Accept input from gdb and match with chars from fp (after skipping one
345 blank) up until a \n is read from fp (which is not matched) */
346
347 static void
348 expect (FILE *fp)
349 {
350 int fromlog;
351 int fromgdb;
352
353 if ((fromlog = logchar (fp)) != ' ')
354 {
355 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
356 fromlog);
357 }
358 do
359 {
360 fromlog = logchar (fp);
361 if (fromlog == EOL)
362 break;
363 fromgdb = gdbchar (remote_desc);
364 if (fromgdb < 0)
365 remote_error ("Error during read from gdb");
366 }
367 while (fromlog == fromgdb);
368
369 if (fromlog != EOL)
370 {
371 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
372 fromgdb);
373 }
374 }
375
376 /* Play data back to gdb from fp (after skipping leading blank) up until a
377 \n is read from fp (which is discarded and not sent to gdb). */
378
379 static void
380 play (FILE *fp)
381 {
382 int fromlog;
383 char ch;
384
385 if ((fromlog = logchar (fp)) != ' ')
386 {
387 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
388 fromlog);
389 }
390 while ((fromlog = logchar (fp)) != EOL)
391 {
392 ch = fromlog;
393 if (write (remote_desc, &ch, 1) != 1)
394 remote_error ("Error during write to gdb");
395 }
396 }
397
398 static void
399 gdbreplay_version (void)
400 {
401 printf ("GNU gdbreplay %s%s\n"
402 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
403 "gdbreplay is free software, covered by "
404 "the GNU General Public License.\n"
405 "This gdbreplay was configured as \"%s\"\n",
406 PKGVERSION, version, host_name);
407 }
408
409 static void
410 gdbreplay_usage (FILE *stream)
411 {
412 fprintf (stream, "Usage:\tgdbreplay <logfile> <host:port>\n");
413 if (REPORT_BUGS_TO[0] && stream == stdout)
414 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
415 }
416
417 int
418 main (int argc, char *argv[])
419 {
420 FILE *fp;
421 int ch;
422
423 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
424 {
425 gdbreplay_version ();
426 exit (0);
427 }
428 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
429 {
430 gdbreplay_usage (stdout);
431 exit (0);
432 }
433
434 if (argc < 3)
435 {
436 gdbreplay_usage (stderr);
437 exit (1);
438 }
439 fp = fopen (argv[1], "r");
440 if (fp == NULL)
441 {
442 perror_with_name (argv[1]);
443 }
444 remote_open (argv[2]);
445 while ((ch = logchar (fp)) != EOF)
446 {
447 switch (ch)
448 {
449 case 'w':
450 /* data sent from gdb to gdbreplay, accept and match it */
451 expect (fp);
452 break;
453 case 'r':
454 /* data sent from gdbreplay to gdb, play it */
455 play (fp);
456 break;
457 case 'c':
458 /* Command executed by gdb */
459 while ((ch = logchar (fp)) != EOL);
460 break;
461 }
462 }
463 remote_close ();
464 exit (0);
465 }
This page took 0.039079 seconds and 5 git commands to generate.