简体   繁体   English

gdb catch throw,这个线程

[英]gdb catch throw, this thread

My binary (generated from C++) has two threads.我的二进制文件(从 C++ 生成)有两个线程。 When I care about exceptions, I care about exceptions thrown in one of them (the worker) but not the other.当我关心异常时,我关心的是其中一个(worker)抛出的异常,而不是另一个。

Is there a way to tell gdb only to pay attention to one of the threads when using catch throw ?有没有办法告诉 gdb 在使用catch throw时只注意其中一个线程? The gdb manual (texinfo document) and googling suggest to me that this isn't possible, although I think I could request catching a specific type of exception, which hopefully only one of the threads would throw, using catch throw REGEXP . gdb 手册(texinfo 文档)和谷歌搜索告诉我这是不可能的,尽管我认为我可以请求捕获特定类型的异常,希望只有一个线程会抛出,使用catch throw REGEXP

Is there a way to tell gdb only to pay attention to one of the threads有没有办法告诉 gdb 只关注其中一个线程

The catch throw is really just a fancy way to set a breakpoint on __cxxabiv1::__cxa_throw (or similar), and you can make a breakpoint conditional on thread number, achieving the equivalent result. catch throw实际上只是在__cxxabiv1::__cxa_throw (或类似的)上设置断点的一种奇特方式,您可以根据线程数设置断点,从而获得等效的结果。

Example:例子:

#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

Using catch throw , you would get a breakpoint firing on the main and the second thread.使用catch throw ,你会在main线程和第二个线程上触发一个断点。 But using break ... thread 2 you would only get the one breakpoint you care about:但是使用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

Voilà -- thread-specific catch throw equivalent. Voilà - 线程特定的catch throw等效项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM