* Spawn a new GDB process and attach it to the current process. * * @param dumpFile a POSIX file descriptor to write GDB's output to, * it will also be used to write failure messages to. * @param[out] gdbWritePipe a POSIX file descriptor linked to GDB's stdin. * * @return 0 if we failed to spawn a new process, a non-zero process ID if we * success
| 412 | * be unchanged. |
| 413 | */ |
| 414 | static pid_t execGdb(int const dumpFile, int *gdbWritePipe) |
| 415 | { |
| 416 | int gdbPipe[2]; |
| 417 | pid_t pid; |
| 418 | char *gdbArgv[] = { gdbPath, programPath, programPID, nullptr }; |
| 419 | char *gdbEnv[] = { nullptr }; |
| 420 | /* Check if the "bare minimum" is available: GDB and an absolute path |
| 421 | * to our program's binary. |
| 422 | */ |
| 423 | if (!programIsAvailable |
| 424 | || !gdbIsAvailable) |
| 425 | { |
| 426 | writeAll(dumpFile, "No extended backtrace dumped:\n"); |
| 427 | |
| 428 | if (!programIsAvailable) |
| 429 | { |
| 430 | writeAll(dumpFile, "- Program path not available\n"); |
| 431 | } |
| 432 | if (!gdbIsAvailable) |
| 433 | { |
| 434 | writeAll(dumpFile, "- GDB not available\n"); |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | // Create a pipe to use for communication with 'gdb' |
| 441 | if (pipe(gdbPipe) == -1) |
| 442 | { |
| 443 | writeAll(dumpFile, "Pipe failed\n"); |
| 444 | |
| 445 | printf("Pipe failed\n"); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | // Fork a new child process |
| 451 | pid = fork(); |
| 452 | if (pid == -1) |
| 453 | { |
| 454 | writeAll(dumpFile, "Fork failed\n"); |
| 455 | |
| 456 | printf("Fork failed\n"); |
| 457 | |
| 458 | // Clean up our pipe |
| 459 | close(gdbPipe[0]); |
| 460 | close(gdbPipe[1]); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | // Check to see if we're the parent |
| 466 | if (pid != 0) |
| 467 | { |
| 468 | #ifdef WZ_OS_LINUX |
| 469 | // Allow tracing the process, some hardened kernel configurations disallow this. |
| 470 | prctl(PR_SET_PTRACER, pid, 0, 0, 0); |
| 471 | #endif |
no test coverage detected