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