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