c++ - why gdb cannot work good on detach thread?
问题描述
I use gdb to debug my program, but I found it doesn't work good on detach thread, code show as below
#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;
}
compile and debug it
g++ main.cpp -O0 -std=c++11 -pthread
gdb a.out
In gdb, I set breakpoints at fn and fn2 then execute run, but not trigger any breakpoints
(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]
what if I replace th.detach()
with th.join
that all breakpoints will be triggered normally, why?
解决方案
推荐阅读
- firebase - 如何更新 Firestore 中的 ReorderableListView 位置?
- flutter - 如何更改轮廓边框的颜色?
- javascript - React 键盘事件也会触发 SyntheticBaseEvent
- woocommerce - 电子商务 | 为可下载的文件名添加唯一密钥
- reactjs - Nginx 反向代理正则表达式位置
- python - 使用 python pandas 读取多个嵌套的 json 文件
- c - How to find the maximum of multiple 2D arrays in OpenMP
- c# - 所有页面列的名称
- java - webview不重定向url android studio java
- npm - 用替代品替换 npm 包