当异常被捕获时,gdb可以用来回溯吗? [英] Can gdb be used to backtrace when exceptions are caught?

查看:766
本文介绍了当异常被捕获时,gdb可以用来回溯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用c ++异常,想要得到它。我想到的是在捕获异常时生成某种回溯信息。最初,我有类似于 C ++中的异常调用栈的想法,但最终得出结论

I have just started using c++ exceptions and want to get it right. What I have in mind is to generate some sort of backtrace information when exceptions are caught. Initially I had ideas similar to Call-stack for exceptions in C++ but eventually figured out that's not quite good.

我还阅读了如何在我的gcc C ++应用程序崩溃时生成stacktrace,但不想为我当前的项目添加更多的复杂性。因为,我只需要在调试模式下的回溯,我希望我可以使用gdb的目的。

I have also read How to generate a stacktrace when my gcc C++ app crashes but do not want to add more complexity to my current project. Since, I only need the backtracing when in debug mode, I was hoping I could be using gdb for that purpose.

我的策略是插入断点在 catch 阻塞,然后通过调用堆栈来精确找出为什么抛出异常(或是什么原因)?不幸的是,我似乎不能这样做,因为当gdb到达断点时,它清除调用堆栈,我只能看到main(这是我捕获)。这是应该发生还是我在这里做错了?

My strategy has been to insert breakpoint in the catch block and then go up through the call stack to exactly pinpoint why the exception was thrown in the first place (or what caused it)? Unfortunatly, I cannot seem to be able to do this since when gdb reaches the breakpoint, it clears the call stack and I can only see main (that's where I catch). Is this supposed to happen or am I doing something wrong here?

编辑:
我只是想总结这里的方法其他人:

I just like to summarize the methods here for other folks:

第一个方法(由paulsm4)。通过 catch throw 捕获catch或 catch catch 设置捕获点的捕获点!然后调用 backtrace

1st Method (by paulsm4). Set a catchpoint via catch throw for catching on throw or catch catch for catching on catch! Then call backtrace

第二个方法(由aschepler设置) __ cxa_throw 然后 backtrace

2nd Method (by aschepler) Set a breakpoint on __cxa_throw and then backtrace

第三方法(在Qt Creator中 - 如果你碰巧使用)

3rd Method (in Qt Creator -- if you happen to use) You can easily set a breakpoint on throw or catch!

Edit_2:使用Qt Creator调试器,似乎在<$ c上设置断点$ c> __ cxa_begin_catch 也等效于 catch catch :)

Edit_2: Using Qt Creator debugger, it seems that setting a breakpoint on __cxa_begin_catch is also an equivalent to catch catch :)

推荐答案

这是:


http://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html

您可以使用catchpoint使调试器停止某些
种程序事件,例如C ++异常或加载
共享库。使用catch命令设置一个接收点。

You can use catchpoints to cause the debugger to stop for certain kinds of program events, such as C++ exceptions or the loading of a shared library. Use the catch command to set a catchpoint.

所以答案应该是yes,它应该避免两个问题

So the answer should be "yes", and it should avoid the problems with the two links you cited.

如果有帮助,请发回!就个人而言,我从来没有尝试过这个GDB功能:)

Please post back if it helped! Personally, I've never tried this GDB feature myself :)

这篇关于当异常被捕获时,gdb可以用来回溯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆