C++ 为什么gdb不能在分离线程上正常工作?

C++ 为什么gdb不能在分离线程上正常工作?,c++,gdb,C++,Gdb,我使用gdb调试我的程序,但我发现它在分离线程上工作不好,代码如下所示 #include<iostream> #include<unistd.h> #include<string> #include<thread> using namespace std; void fn()throw(){ int i = 0; string str("aaa"); } voi

我使用gdb调试我的程序,但我发现它在分离线程上工作不好,代码如下所示

#include<iostream>
#include<unistd.h>
#include<string>
#include<thread>
using namespace std;

void fn()throw(){
            int i = 0;
                    string str("aaa");
}

void fn2(){
            fn();

}

int main(){
    thread th(fn2);
    th.detach(); //set thread to detach
    return 0;
}

在gdb中,我在fn和fn2处设置断点,然后执行run,但不触发任何断点

(gdb) b fn
Breakpoint 1 at 0x400d86
(gdb) b fn2
Breakpoint 2 at 0x400e0a
(gdb) r
Starting program: /root/project/test/a.out 
warning: File "/usr/local/lib64/libstdc++.so.6.0.28-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.28-gdb.py
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff6ef6700 (LWP 9583)]
Couldn't get registers: No such process.
(gdb) Cannot find user-level thread for LWP 9583: generic error
(gdb) [Thread 0x7ffff6ef6700 (LWP 9583) exited]
[Inferior 1 (process 9559) exited normally]

如果我将
th.detach()
替换为
th.join
,所有断点都将正常触发,为什么?

因为程序在到达任何断点之前退出。您使用的是哪种版本的gdb?我的是Ubuntu上的8.1版本,我没有看到“无法获取寄存器:没有这样的进程”或“一般错误”消息(断点没有被触发,但它们不需要被触发,正如上面的评论所解释的)。你为什么要以root用户身份运行?这不是很安全。@n.“代词m。GNU gdb(Ubuntu7.11.1-0ubuntu1~16.5)7.11.1,g++(Ubuntu5.4.0-6ubuntu1~16.04.12)5.4.0 20160609,系统是Ubuntu16.04.6 LTS\n\l这很古老。gdb一直在进步。
(gdb) b fn
Breakpoint 1 at 0x400d86
(gdb) b fn2
Breakpoint 2 at 0x400e0a
(gdb) r
Starting program: /root/project/test/a.out 
warning: File "/usr/local/lib64/libstdc++.so.6.0.28-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.28-gdb.py
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff6ef6700 (LWP 9583)]
Couldn't get registers: No such process.
(gdb) Cannot find user-level thread for LWP 9583: generic error
(gdb) [Thread 0x7ffff6ef6700 (LWP 9583) exited]
[Inferior 1 (process 9559) exited normally]