* remote-utils.c (remote_open): Set SO_KEEPALIVE on remote_desc
[deliverable/binutils-gdb.git] / gdb / gdbserver / gdbreplay.c
1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996, 1998, 1999, 2000, 2002, 2003, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "config.h"
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_MALLOC_H
60 #include <malloc.h>
61 #endif
62
63 #if USE_WIN32API
64 #include <winsock.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 (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_close (void)
165 {
166 #ifdef USE_WIN32API
167 closesocket (remote_desc);
168 #else
169 close (remote_desc);
170 #endif
171 }
172
173 /* Open a connection to a remote debugger.
174 NAME is the filename used for communication. */
175
176 static void
177 remote_open (char *name)
178 {
179 if (!strchr (name, ':'))
180 {
181 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
182 fflush (stderr);
183 exit (1);
184 }
185 else
186 {
187 #ifdef USE_WIN32API
188 static int winsock_initialized;
189 #endif
190 char *port_str;
191 int port;
192 struct sockaddr_in sockaddr;
193 socklen_t tmp;
194 int tmp_desc;
195
196 port_str = strchr (name, ':');
197
198 port = atoi (port_str + 1);
199
200 #ifdef USE_WIN32API
201 if (!winsock_initialized)
202 {
203 WSADATA wsad;
204
205 WSAStartup (MAKEWORD (1, 0), &wsad);
206 winsock_initialized = 1;
207 }
208 #endif
209
210 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
211 if (tmp_desc < 0)
212 perror_with_name ("Can't open socket");
213
214 /* Allow rapid reuse of this port. */
215 tmp = 1;
216 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
217 sizeof (tmp));
218
219 sockaddr.sin_family = PF_INET;
220 sockaddr.sin_port = htons (port);
221 sockaddr.sin_addr.s_addr = INADDR_ANY;
222
223 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
224 || listen (tmp_desc, 1))
225 perror_with_name ("Can't bind address");
226
227 tmp = sizeof (sockaddr);
228 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
229 if (remote_desc == -1)
230 perror_with_name ("Accept failed");
231
232 /* Enable TCP keep alive process. */
233 tmp = 1;
234 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
235
236 /* Tell TCP not to delay small packets. This greatly speeds up
237 interactive response. */
238 tmp = 1;
239 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
240 (char *) &tmp, sizeof (tmp));
241
242 #ifndef USE_WIN32API
243 close (tmp_desc); /* No longer need this */
244
245 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbreplay simply
246 exits when the remote side dies. */
247 #else
248 closesocket (tmp_desc); /* No longer need this */
249 #endif
250 }
251
252 #if defined(F_SETFL) && defined (FASYNC)
253 fcntl (remote_desc, F_SETFL, FASYNC);
254 #endif
255
256 fprintf (stderr, "Replay logfile using %s\n", name);
257 fflush (stderr);
258 }
259
260 static int
261 tohex (int ch)
262 {
263 if (ch >= '0' && ch <= '9')
264 {
265 return (ch - '0');
266 }
267 if (ch >= 'A' && ch <= 'F')
268 {
269 return (ch - 'A' + 10);
270 }
271 if (ch >= 'a' && ch <= 'f')
272 {
273 return (ch - 'a' + 10);
274 }
275 fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
276 fflush (stderr);
277 exit (1);
278 }
279
280 static int
281 logchar (FILE *fp)
282 {
283 int ch;
284 int ch2;
285
286 ch = fgetc (fp);
287 fputc (ch, stdout);
288 fflush (stdout);
289 switch (ch)
290 {
291 case '\n':
292 ch = EOL;
293 break;
294 case '\\':
295 ch = fgetc (fp);
296 fputc (ch, stdout);
297 fflush (stdout);
298 switch (ch)
299 {
300 case '\\':
301 break;
302 case 'b':
303 ch = '\b';
304 break;
305 case 'f':
306 ch = '\f';
307 break;
308 case 'n':
309 ch = '\n';
310 break;
311 case 'r':
312 ch = '\r';
313 break;
314 case 't':
315 ch = '\t';
316 break;
317 case 'v':
318 ch = '\v';
319 break;
320 case 'x':
321 ch2 = fgetc (fp);
322 fputc (ch2, stdout);
323 fflush (stdout);
324 ch = tohex (ch2) << 4;
325 ch2 = fgetc (fp);
326 fputc (ch2, stdout);
327 fflush (stdout);
328 ch |= tohex (ch2);
329 break;
330 default:
331 /* Treat any other char as just itself */
332 break;
333 }
334 default:
335 break;
336 }
337 return (ch);
338 }
339
340 /* Accept input from gdb and match with chars from fp (after skipping one
341 blank) up until a \n is read from fp (which is not matched) */
342
343 static void
344 expect (FILE *fp)
345 {
346 int fromlog;
347 unsigned char fromgdb;
348
349 if ((fromlog = logchar (fp)) != ' ')
350 {
351 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
352 fromlog);
353 }
354 do
355 {
356 fromlog = logchar (fp);
357 if (fromlog == EOL)
358 {
359 break;
360 }
361 read (remote_desc, &fromgdb, 1);
362 }
363 while (fromlog == fromgdb);
364 if (fromlog != EOL)
365 {
366 sync_error (fp, "Sync error during read of gdb packet", fromlog,
367 fromgdb);
368 }
369 }
370
371 /* Play data back to gdb from fp (after skipping leading blank) up until a
372 \n is read from fp (which is discarded and not sent to gdb). */
373
374 static void
375 play (FILE *fp)
376 {
377 int fromlog;
378 char ch;
379
380 if ((fromlog = logchar (fp)) != ' ')
381 {
382 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
383 fromlog);
384 }
385 while ((fromlog = logchar (fp)) != EOL)
386 {
387 ch = fromlog;
388 write (remote_desc, &ch, 1);
389 }
390 }
391
392 int
393 main (int argc, char *argv[])
394 {
395 FILE *fp;
396 int ch;
397
398 if (argc < 3)
399 {
400 fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
401 fflush (stderr);
402 exit (1);
403 }
404 fp = fopen (argv[1], "r");
405 if (fp == NULL)
406 {
407 perror_with_name (argv[1]);
408 }
409 remote_open (argv[2]);
410 while ((ch = logchar (fp)) != EOF)
411 {
412 switch (ch)
413 {
414 case 'w':
415 /* data sent from gdb to gdbreplay, accept and match it */
416 expect (fp);
417 break;
418 case 'r':
419 /* data sent from gdbreplay to gdb, play it */
420 play (fp);
421 break;
422 case 'c':
423 /* Command executed by gdb */
424 while ((ch = logchar (fp)) != EOL);
425 break;
426 }
427 }
428 remote_close ();
429 exit (0);
430 }
This page took 0.037702 seconds and 4 git commands to generate.