Delete unnecessary code from kill_command
[deliverable/binutils-gdb.git] / gdb / gdbserver / gdbreplay.c
1 /* Replay a remote debug session logfile for GDB.
2 Copyright (C) 1996-2020 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 "gdbsupport/common-defs.h"
21 #include "gdbsupport/version.h"
22
23 #if HAVE_SYS_FILE_H
24 #include <sys/file.h>
25 #endif
26 #if HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29 #include <ctype.h>
30 #if HAVE_FCNTL_H
31 #include <fcntl.h>
32 #endif
33 #include <unistd.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40 #if HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
43 #if HAVE_NETINET_TCP_H
44 #include <netinet/tcp.h>
45 #endif
46
47 #if USE_WIN32API
48 #include <ws2tcpip.h>
49 #endif
50
51 #include "gdbsupport/netstuff.h"
52 #include "gdbsupport/rsp-low.h"
53
54 #ifndef HAVE_SOCKLEN_T
55 typedef int socklen_t;
56 #endif
57
58 /* Sort of a hack... */
59 #define EOL (EOF - 1)
60
61 static int remote_desc;
62
63 #ifdef __MINGW32CE__
64
65 #ifndef COUNTOF
66 #define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
67 #endif
68
69 #define errno (GetLastError ())
70
71 char *
72 strerror (DWORD error)
73 {
74 static char buf[1024];
75 WCHAR *msgbuf;
76 DWORD lasterr = GetLastError ();
77 DWORD chars = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
78 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
79 NULL,
80 error,
81 0, /* Default language */
82 (LPVOID)&msgbuf,
83 0,
84 NULL);
85 if (chars != 0)
86 {
87 /* If there is an \r\n appended, zap it. */
88 if (chars >= 2
89 && msgbuf[chars - 2] == '\r'
90 && msgbuf[chars - 1] == '\n')
91 {
92 chars -= 2;
93 msgbuf[chars] = 0;
94 }
95
96 if (chars > ((COUNTOF (buf)) - 1))
97 {
98 chars = COUNTOF (buf) - 1;
99 msgbuf [chars] = 0;
100 }
101
102 wcstombs (buf, msgbuf, chars + 1);
103 LocalFree (msgbuf);
104 }
105 else
106 sprintf (buf, "unknown win32 error (%ld)", error);
107
108 SetLastError (lasterr);
109 return buf;
110 }
111
112 #endif /* __MINGW32CE__ */
113
114 static void
115 sync_error (FILE *fp, const char *desc, int expect, int got)
116 {
117 fprintf (stderr, "\n%s\n", desc);
118 fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
119 ftell (fp), expect, got);
120 fflush (stderr);
121 exit (1);
122 }
123
124 static void
125 remote_error (const char *desc)
126 {
127 fprintf (stderr, "\n%s\n", desc);
128 fflush (stderr);
129 exit (1);
130 }
131
132 static void
133 remote_close (void)
134 {
135 #ifdef USE_WIN32API
136 closesocket (remote_desc);
137 #else
138 close (remote_desc);
139 #endif
140 }
141
142 /* Open a connection to a remote debugger.
143 NAME is the filename used for communication. */
144
145 static void
146 remote_open (char *name)
147 {
148 char *last_colon = strrchr (name, ':');
149
150 if (last_colon == NULL)
151 {
152 fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
153 fflush (stderr);
154 exit (1);
155 }
156
157 #ifdef USE_WIN32API
158 static int winsock_initialized;
159 #endif
160 int tmp;
161 int tmp_desc;
162 struct addrinfo hint;
163 struct addrinfo *ainfo;
164
165 memset (&hint, 0, sizeof (hint));
166 /* Assume no prefix will be passed, therefore we should use
167 AF_UNSPEC. */
168 hint.ai_family = AF_UNSPEC;
169 hint.ai_socktype = SOCK_STREAM;
170 hint.ai_protocol = IPPROTO_TCP;
171
172 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
173
174 if (parsed.port_str.empty ())
175 error (_("Missing port on hostname '%s'"), name);
176
177 #ifdef USE_WIN32API
178 if (!winsock_initialized)
179 {
180 WSADATA wsad;
181
182 WSAStartup (MAKEWORD (1, 0), &wsad);
183 winsock_initialized = 1;
184 }
185 #endif
186
187 int r = getaddrinfo (parsed.host_str.c_str (), parsed.port_str.c_str (),
188 &hint, &ainfo);
189
190 if (r != 0)
191 {
192 fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
193 parsed.host_str.c_str (), parsed.port_str.c_str (),
194 gai_strerror (r));
195 fflush (stderr);
196 exit (1);
197 }
198
199 scoped_free_addrinfo free_ainfo (ainfo);
200
201 struct addrinfo *p;
202
203 for (p = ainfo; p != NULL; p = p->ai_next)
204 {
205 tmp_desc = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
206
207 if (tmp_desc >= 0)
208 break;
209 }
210
211 if (p == NULL)
212 perror_with_name ("Cannot 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 switch (p->ai_family)
220 {
221 case AF_INET:
222 ((struct sockaddr_in *) p->ai_addr)->sin_addr.s_addr = INADDR_ANY;
223 break;
224 case AF_INET6:
225 ((struct sockaddr_in6 *) p->ai_addr)->sin6_addr = in6addr_any;
226 break;
227 default:
228 fprintf (stderr, "Invalid 'ai_family' %d\n", p->ai_family);
229 exit (1);
230 }
231
232 if (bind (tmp_desc, p->ai_addr, p->ai_addrlen) != 0)
233 perror_with_name ("Can't bind address");
234
235 if (p->ai_socktype == SOCK_DGRAM)
236 remote_desc = tmp_desc;
237 else
238 {
239 struct sockaddr_storage sockaddr;
240 socklen_t sockaddrsize = sizeof (sockaddr);
241 char orig_host[GDB_NI_MAX_ADDR], orig_port[GDB_NI_MAX_PORT];
242
243 if (listen (tmp_desc, 1) != 0)
244 perror_with_name ("Can't listen on socket");
245
246 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr,
247 &sockaddrsize);
248
249 if (remote_desc == -1)
250 perror_with_name ("Accept failed");
251
252 /* Enable TCP keep alive process. */
253 tmp = 1;
254 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE,
255 (char *) &tmp, sizeof (tmp));
256
257 /* Tell TCP not to delay small packets. This greatly speeds up
258 interactive response. */
259 tmp = 1;
260 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
261 (char *) &tmp, sizeof (tmp));
262
263 if (getnameinfo ((struct sockaddr *) &sockaddr, sockaddrsize,
264 orig_host, sizeof (orig_host),
265 orig_port, sizeof (orig_port),
266 NI_NUMERICHOST | NI_NUMERICSERV) == 0)
267 {
268 fprintf (stderr, "Remote debugging from host %s, port %s\n",
269 orig_host, orig_port);
270 fflush (stderr);
271 }
272
273 #ifndef USE_WIN32API
274 close (tmp_desc); /* No longer need this */
275
276 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then
277 gdbreplay simply exits when
278 the remote side dies. */
279 #else
280 closesocket (tmp_desc); /* No longer need this */
281 #endif
282 }
283
284 #if defined(F_SETFL) && defined (FASYNC)
285 fcntl (remote_desc, F_SETFL, FASYNC);
286 #endif
287
288 fprintf (stderr, "Replay logfile using %s\n", name);
289 fflush (stderr);
290 }
291
292 static int
293 logchar (FILE *fp)
294 {
295 int ch;
296 int ch2;
297
298 ch = fgetc (fp);
299 if (ch != '\r')
300 {
301 fputc (ch, stdout);
302 fflush (stdout);
303 }
304 switch (ch)
305 {
306 /* Treat \r\n as a newline. */
307 case '\r':
308 ch = fgetc (fp);
309 if (ch == '\n')
310 ch = EOL;
311 else
312 {
313 ungetc (ch, fp);
314 ch = '\r';
315 }
316 fputc (ch == EOL ? '\n' : '\r', stdout);
317 fflush (stdout);
318 break;
319 case '\n':
320 ch = EOL;
321 break;
322 case '\\':
323 ch = fgetc (fp);
324 fputc (ch, stdout);
325 fflush (stdout);
326 switch (ch)
327 {
328 case '\\':
329 break;
330 case 'b':
331 ch = '\b';
332 break;
333 case 'f':
334 ch = '\f';
335 break;
336 case 'n':
337 ch = '\n';
338 break;
339 case 'r':
340 ch = '\r';
341 break;
342 case 't':
343 ch = '\t';
344 break;
345 case 'v':
346 ch = '\v';
347 break;
348 case 'x':
349 ch2 = fgetc (fp);
350 fputc (ch2, stdout);
351 fflush (stdout);
352 ch = fromhex (ch2) << 4;
353 ch2 = fgetc (fp);
354 fputc (ch2, stdout);
355 fflush (stdout);
356 ch |= fromhex (ch2);
357 break;
358 default:
359 /* Treat any other char as just itself */
360 break;
361 }
362 default:
363 break;
364 }
365 return (ch);
366 }
367
368 static int
369 gdbchar (int desc)
370 {
371 unsigned char fromgdb;
372
373 if (read (desc, &fromgdb, 1) != 1)
374 return -1;
375 else
376 return fromgdb;
377 }
378
379 /* Accept input from gdb and match with chars from fp (after skipping one
380 blank) up until a \n is read from fp (which is not matched) */
381
382 static void
383 expect (FILE *fp)
384 {
385 int fromlog;
386 int fromgdb;
387
388 if ((fromlog = logchar (fp)) != ' ')
389 {
390 sync_error (fp, "Sync error during gdb read of leading blank", ' ',
391 fromlog);
392 }
393 do
394 {
395 fromlog = logchar (fp);
396 if (fromlog == EOL)
397 break;
398 fromgdb = gdbchar (remote_desc);
399 if (fromgdb < 0)
400 remote_error ("Error during read from gdb");
401 }
402 while (fromlog == fromgdb);
403
404 if (fromlog != EOL)
405 {
406 sync_error (fp, "Sync error during read of gdb packet from log", fromlog,
407 fromgdb);
408 }
409 }
410
411 /* Play data back to gdb from fp (after skipping leading blank) up until a
412 \n is read from fp (which is discarded and not sent to gdb). */
413
414 static void
415 play (FILE *fp)
416 {
417 int fromlog;
418 char ch;
419
420 if ((fromlog = logchar (fp)) != ' ')
421 {
422 sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
423 fromlog);
424 }
425 while ((fromlog = logchar (fp)) != EOL)
426 {
427 ch = fromlog;
428 if (write (remote_desc, &ch, 1) != 1)
429 remote_error ("Error during write to gdb");
430 }
431 }
432
433 static void
434 gdbreplay_version (void)
435 {
436 printf ("GNU gdbreplay %s%s\n"
437 "Copyright (C) 2020 Free Software Foundation, Inc.\n"
438 "gdbreplay is free software, covered by "
439 "the GNU General Public License.\n"
440 "This gdbreplay was configured as \"%s\"\n",
441 PKGVERSION, version, host_name);
442 }
443
444 static void
445 gdbreplay_usage (FILE *stream)
446 {
447 fprintf (stream, "Usage:\tgdbreplay LOGFILE HOST:PORT\n");
448 if (REPORT_BUGS_TO[0] && stream == stdout)
449 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
450 }
451
452 /* Main function. This is called by the real "main" function,
453 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
454
455 static void ATTRIBUTE_NORETURN
456 captured_main (int argc, char *argv[])
457 {
458 FILE *fp;
459 int ch;
460
461 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
462 {
463 gdbreplay_version ();
464 exit (0);
465 }
466 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
467 {
468 gdbreplay_usage (stdout);
469 exit (0);
470 }
471
472 if (argc < 3)
473 {
474 gdbreplay_usage (stderr);
475 exit (1);
476 }
477 fp = fopen (argv[1], "r");
478 if (fp == NULL)
479 {
480 perror_with_name (argv[1]);
481 }
482 remote_open (argv[2]);
483 while ((ch = logchar (fp)) != EOF)
484 {
485 switch (ch)
486 {
487 case 'w':
488 /* data sent from gdb to gdbreplay, accept and match it */
489 expect (fp);
490 break;
491 case 'r':
492 /* data sent from gdbreplay to gdb, play it */
493 play (fp);
494 break;
495 case 'c':
496 /* Command executed by gdb */
497 while ((ch = logchar (fp)) != EOL);
498 break;
499 }
500 }
501 remote_close ();
502 exit (0);
503 }
504
505 int
506 main (int argc, char *argv[])
507 {
508 try
509 {
510 captured_main (argc, argv);
511 }
512 catch (const gdb_exception &exception)
513 {
514 if (exception.reason == RETURN_ERROR)
515 {
516 fflush (stdout);
517 fprintf (stderr, "%s\n", exception.what ());
518 }
519
520 exit (1);
521 }
522
523 gdb_assert_not_reached ("captured_main should never return");
524 }
This page took 0.070961 seconds and 4 git commands to generate.