【问题标题】:gdb catch throw, this threadgdb catch throw,这个线程
【发布时间】:2021-10-31 19:10:36
【问题描述】:

我的二进制文件(从 C++ 生成)有两个线程。当我关心异常时,我关心的是其中一个(worker)抛出的异常,而不是另一个。

有没有办法告诉gdb在使用catch throw时只关注其中一个线程? gdb 手册(texinfo 文档)和谷歌搜索向我建议这是不可能的,尽管我认为我可以请求捕获特定类型的异常,希望只有一个线程会抛出,使用 catch throw REGEXP。

【问题讨论】:

    标签: c++ gdb c++14


    【解决方案1】:

    有没有办法告诉 gdb 只关注其中一个线程

    catch throw 实际上只是在__cxxabiv1::__cxa_throw(或类似的)上设置断点的一种奇特方式,您可以根据线程号设置断点,从而获得等效的结果。

    例子:

    #include <pthread.h>
    #include <unistd.h>
    
    void *fn(void *)
    {
      while(true) {
        try {
          throw 1;
        } catch (...) {}
        sleep(1);
      }
      return nullptr;
    }
    
    int main() {
      pthread_t tid;
      pthread_create(&tid, nullptr, fn, nullptr);
      fn(nullptr);
      return 0;
    }
    
    g++ -g -pthread t.cc
    

    使用catch throw,您将在main 和第二个线程上触发断点。但是使用break ... thread 2 你只会得到你关心的一个断点:

    gdb -q a.out
    Reading symbols from a.out...
    
    (gdb) start
    Temporary breakpoint 1 at 0x11d6: file t.cc, line 17.
    Starting program: /tmp/a.out
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    
    Temporary breakpoint 1, main () at t.cc:17
    17        pthread_create(&tid, nullptr, fn, nullptr);
    (gdb) n
    [New Thread 0x7ffff7a4c640 (LWP 1225199)]
    
      ## Note: GDB refuses to set thread-specific breakpoint until the thread actually exists.
    
    18        fn(nullptr);
    (gdb) break __cxxabiv1::__cxa_throw thread 2
    Breakpoint 2 at 0x7ffff7e40370: file ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc, line 77.
    (gdb) c
    Continuing.
    [Switching to Thread 0x7ffff7a4c640 (LWP 1225199)]
    
    Thread 2 "a.out" hit Breakpoint 2, __cxxabiv1::__cxa_throw (obj=0x7ffff0000be0, tinfo=0x555555557dc8 <typeinfo for int@CXXABI_1.3>, dest=0x0) at ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc:77
    77      ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc: No such file or directory.
    (gdb) bt
    #0  __cxxabiv1::__cxa_throw (obj=0x7ffff0000be0, tinfo=0x555555557dc8 <typeinfo for int@CXXABI_1.3>, dest=0x0) at ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc:77
    #1  0x00005555555551b5 in fn () at t.cc:8
    #2  0x00007ffff7d7eeae in start_thread (arg=0x7ffff7a4c640) at pthread_create.c:463
    #3  0x00007ffff7caea5f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
    (gdb) c
    Continuing.
    
    Thread 2 "a.out" hit Breakpoint 2, __cxxabiv1::__cxa_throw (obj=0x7ffff0000be0, tinfo=0x555555557dc8 <typeinfo for int@CXXABI_1.3>, dest=0x0) at ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc:77
    77      in ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc
    (gdb) bt
    #0  __cxxabiv1::__cxa_throw (obj=0x7ffff0000be0, tinfo=0x555555557dc8 <typeinfo for int@CXXABI_1.3>, dest=0x0) at ../../../../src/libstdc++-v3/libsupc++/eh_throw.cc:77
    #1  0x00005555555551b5 in fn () at t.cc:8
    #2  0x00007ffff7d7eeae in start_thread (arg=0x7ffff7a4c640) at pthread_create.c:463
    #3  0x00007ffff7caea5f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
    

    瞧——线程特定的 catch throw 等效项。

    【讨论】:

    • 在我的情况下,它只有在没有范围的情况下才有效,b __cxa_throw thread 2,但非常棒。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2012-06-04
    • 2010-12-14
    • 2013-10-07
    • 2013-11-10
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多