почему GDB не может нормально работать при отсоединении потока? - PullRequest
3 голосов
/ 12 июля 2020

Я использую 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;
}

скомпилировать и отладить его

g++ main.cpp -O0 -std=c++11 -pthread
gdb a.out

В 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, все точки останова будут срабатывать нормально, почему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...