看板 LinuxDev 關於我們 聯絡資訊
小弟目前還正在學linux程式 目前在debug上有點小問題 問題是 目前寫的程式我會用到fork() 但是在設定 breakpoint 在parnet process 執行的程式碼時gdb不會跳進去中斷點 ex. myfork.cpp 1 #include <iostream> 2 #include <sys/types.h> 3 #include <unistd.h> 4 5 int main(){ 6 7 pid_t child_pid; 8 9 std::cout << "the mani program pid is " << (int)getpid() << std::endl; 10 11 child_pid = fork(); 12 if(child_pid == 0) 13 { 14 std::cout << " In the Child process : " << (int)child_pid << std::endl; 15 }else 16 { 17 std::cout << " This parent process : " << (int) child_pid << std::endl; 18 } 19 } ~ 譬如我在 14行和17行設定中斷點 但是用gdb看的話 他程式會只有中斷17行 而不會去中斷 child process 的中斷點 請問我該如何使程式跳入child process的中斷點呢? 感謝各位前輩指教! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.189.231 ※ 編輯: fuha 來自: 59.120.189.231 (11/28 16:50)
eva19452002:會不會child_pid一直都非零? 11/29 08:06
eva19452002:你可以在gdb trace到12行時,把child pid用gdb設成非0 11/29 08:09
eva19452002: 設成0才對 11/29 08:09
eva19452002:這樣就會跳到14行了 11/29 08:10
antontw:man fork, return value 不是有寫的很清楚嗎? 11/29 17:31
antontw:簡單的說:用 gdb 抓 child pid 再 attach 11/29 17:39