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