| 1 | #include "attach.h" |
| 2 | #include <stdio.h> |
| 3 | #include <sys/wait.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | volatile int release_child_flag = 0; |
| 7 | |
| 8 | int main(int argc, char const *argv[]) |
| 9 | { |
| 10 | pid_t child = fork(); |
| 11 | if (child == -1) |
| 12 | { |
| 13 | perror(s: "fork" ); |
| 14 | return 1; |
| 15 | } |
| 16 | |
| 17 | if (child > 0) |
| 18 | { // parent |
| 19 | if (argc < 2) |
| 20 | { |
| 21 | fprintf(stderr, format: "Need pid filename.\n" ); |
| 22 | return 2; |
| 23 | } |
| 24 | |
| 25 | // Let the test suite know the child's pid. |
| 26 | FILE *pid_file = fopen(filename: argv[1], modes: "w" ); |
| 27 | if (pid_file == NULL) |
| 28 | { |
| 29 | perror(s: "fopen" ); |
| 30 | return 3; |
| 31 | } |
| 32 | |
| 33 | fprintf(stream: pid_file, format: "%d\n" , child); |
| 34 | if (fclose(stream: pid_file) == EOF) |
| 35 | { |
| 36 | perror(s: "fclose" ); |
| 37 | return 4; |
| 38 | } |
| 39 | |
| 40 | // And wait for the child to finish it's work. |
| 41 | int status = 0; |
| 42 | pid_t wpid = wait(stat_loc: &status); |
| 43 | if (wpid == -1) |
| 44 | { |
| 45 | perror(s: "wait" ); |
| 46 | return 5; |
| 47 | } |
| 48 | if (wpid != child) |
| 49 | { |
| 50 | fprintf(stderr, format: "wait() waited for wrong child\n" ); |
| 51 | return 6; |
| 52 | } |
| 53 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 54 | { |
| 55 | fprintf(stderr, format: "child did not exit correctly\n" ); |
| 56 | return 7; |
| 57 | } |
| 58 | } |
| 59 | else |
| 60 | { // child |
| 61 | lldb_enable_attach(); |
| 62 | |
| 63 | while (! release_child_flag) // Wait for debugger to attach |
| 64 | sleep(seconds: 1); |
| 65 | |
| 66 | printf(format: "Child's previous process group is: %d\n" , getpgid(pid: 0)); |
| 67 | setpgid(pid: 0, pgid: 0); // Set breakpoint here |
| 68 | printf(format: "Child's process group set to: %d\n" , getpgid(pid: 0)); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |