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