如何使用GDB获得有关异常的更多信息?

时间:2018-06-23 19:35:29

标签: c++ linux debugging gdb

我有一个多线程程序,其中某些线程导致异常。我在gdb中运行它,在发生异常并终止程序后,将提供以下输出:

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

Thread 46 "MyProgram" received signal SIGABRT, Aborted.
[Switching to Thread 0x7fff3bfff700 (LWP 24491)]
0x00007ffff48f2428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) where
#0  0x00007ffff48f2428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff48f402a in __GI_abort () at abort.c:89
#2  0x00007ffff514d0d5 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff514acc6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff514ad11 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff517719e in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff4c8e6ba in start_thread (arg=0x7fff3bfff700) at pthread_create.c:333
#7  0x00007ffff49c441d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

where的输出并不能真正帮助找出导致问题的线程。

是否有办法从这种情况中获取更多信息?

1 个答案:

答案 0 :(得分:2)

当您看到此输出时,因为程序已终止,现在找出哪个线程引发了异常为时已晚。我猜您需要在std::out_of_range异常上设置catchpoint并继续运行程序,直到引发此异常为止:

(gdb) catch throw out_of_range

引发异常时,gdb应该停止,并且您可以打印导致异常的线程的回溯。