Issue 127952
Summary [lldb] How lldb debugs multiple processes generated by fork()
Labels new issue
Assignees
Reporter MoonYoonMee
    In gdb, "set detach-on-fork off" can debug the parent process and the fork child process at the same time, use the "inferior id" command to switch to the process corresponding to the ID.
For example, debug the following code:

```
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
    pid_t pid = fork();
    if(pid == 0) { // child
        printf("I am child, my pid = %d, my parent pid = %d\n", getpid(), getppid());
        while(true){
            sleep(1);
        };
    } else if(pid > 0) { // parent
        printf("I am parent, my pid = %d, my child pid = %d\n", getpid(), pid);
        while(true) {
            sleep(1);
        };
        wait(NULL); 
    } else {
       perror("fork error!\n");
       return -1;
    }
    return 0;
}
```

```
(gdb) break ttt.cpp:8
Breakpoint 1 at 0x11bb: file ttt.cpp, line 8.
(gdb) break ttt.cpp:9
Breakpoint 2 at 0x11c1: file ttt.cpp, line 9.
(gdb) break ttt.cpp:13
Breakpoint 3 at 0x11ee: file ttt.cpp, line 13.
(gdb) set detach-on-fork off
(gdb) r
Starting program: /home/muyongmei/testDemo/ttt
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New inferior 2 (process 1432275)]
Reading symbols from /usr/lib/debug/.build-id/69/389d485a9793dbe873f0ea2c93e02efaa9aa3d.debug...
Reading symbols from /usr/lib/debug/.build-id/61/ef896a699bb1c2e4e231642b2e1688b2f1a61e.debug...
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Thread 1.1 "ttt" hit Breakpoint 1, main () at ttt.cpp:8
8           if(pid == 0) { // child
(gdb) c
Continuing.

Thread 1.1 "ttt" hit Breakpoint 3, main () at ttt.cpp:13
13          } else if(pid > 0) { // parent
(gdb) n
14              printf("I am parent, my pid = %d, my child pid = %d\n", getpid(), pid);
(gdb) info inferiors
  Num  Description       Connection           Executable
* 1    process 1432242   1 (native)           /home/muyongmei/testDemo/ttt
  2    process 1432275   1 (native)           /home/muyongmei/testDemo/ttt
(gdb) inferior 2
[Switching to inferior 2 [process 1432275] (/home/muyongmei/testDemo/ttt)]
[Switching to thread 2.1 (Thread 0x7ffff7d82740 (LWP 1432275))]
#0  arch_fork (ctid=0x7ffff7d82a10) at ../sysdeps/unix/sysv/linux/arch-fork.h:52
52      ../sysdeps/unix/sysv/linux/arch-fork.h: No such file or directory.
(gdb) c
Continuing.

Thread 2.1 "ttt" hit Breakpoint 1, main () at ttt.cpp:8
8           if(pid == 0) { // child
(gdb) n

Thread 2.1 "ttt" hit Breakpoint 2, main () at ttt.cpp:9
9               printf("I am child, my pid = %d, my parent pid = %d\n", getpid(), getppid());
```

How to do it in lldb?
If this kind of multi-process cannot be debugged in lldb, what is the reason?
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to