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