从未捕获的异常中获取堆栈跟踪?

发布于 2024-08-12 13:03:41 字数 214 浏览 3 评论 0原文

我意识到这将是特定于平台的:有没有办法从未捕获的 C++ 异常中获取堆栈跟踪,但从引发异常的点开始?

我有一个 Windows 结构化异常处理程序来捕获访问冲突等并生成小型转储。但当然,如果由于未捕获的 C++ 异常而终止,则不会调用该方法,因此不存在故障转储。

我目前正在寻找 Windows 解决方案(无论多么肮脏!),但如果可能的话,希望了解其他平台。

谢谢。

I realise this will be platform specific: is there any way to get a stack trace from an uncaught C++ exception, but from the point at which the exception is thrown?

I have a Windows Structured Exception Handler to catch access violations, etc. and generate a minidump. But of course that won't get called in the event of termination due to an uncaught C++ exception, and so there is no crash dump.

I'm looking for a Windows solution at the moment (no matter how dirty!), but would like to hear about other platforms if possible.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗失的美好 2024-08-19 13:03:41

我们在上一篇文章中使用以下站点中的信息实现了针对未处理异常的 MiniDump:

http://beefchunk .com/documentation/sys-programming/os-win32/debug/www.debuginfo.com/articles/effminidumps.html

要捕获 Windows 上未处理的异常,请查看:

SetUnhandledExceptionFilter (http://msdn.microsoft.com/en-us/library/ms680634 %28VS.85%29.aspx)。

作为一名助手,我们花了很多时间尝试不同级别的小型转储,直到我们确定了一个。事实证明,这在现实世界的崩溃中没有真正的用处,因为我们不知道在实施小型转储时它们会是什么。它是特定于应用程序的,也是特定于崩溃的,所以我的建议是尽早添加小型转储处理程序,它将随着项目和质量检查而增长,并且在某个时候它将成为救星(希望在现实世界中)也)。

We implemented MiniDumps for unhandled exceptions in our last title using the information from this site:

http://beefchunk.com/documentation/sys-programming/os-win32/debug/www.debuginfo.com/articles/effminidumps.html

And to catch the unhandled exceptions on windows have a look at:

SetUnhandledExceptionFilter (http://msdn.microsoft.com/en-us/library/ms680634%28VS.85%29.aspx).

As an aisde, we spent a lot of time experimenting with the different levels of minidump until we settled on one. This proved to be of no real use in real world crashes as we had no idea what they would be at the time the minidumps were implemented. It's very application specific, and also crash specific, so my recommendation is to add the minidump handler as early as possible, it will grow with the project and through QA and it will be a life saver at somepoint (and hopefully out in the real world too).

雨夜星沙 2024-08-19 13:03:41

您可以使用 try- except 语句将 C++ 异常“转换”为结构化异常(然后您可以从中获得不错的堆栈跟踪)。考虑一下:

// Your function to get a backtrace from a CONTEXT
const char *readBacktrace( CONTEXT &ctx );

extern "C"
static DWORD exceptFilter( struct _EXCEPTION_POINTERS* exInf )
{
    OutputDebugStringA( readBacktrace( *exInf->ContextRecord ) );
    return EXCEPTION_EXECUTE_HANDLER;
}

try {
  // your C++ code which might yield exceptions
} catch ( ... ) {
  // In case a C++ exception occurs, raise a structured exception and catch it    immediately
  // so that we get a CONTEXT object which we can use to generate a stack trace.
  __try {
    RaiseException( 1, 0, 0, NULL );
  } __except( exceptFilter( GetExceptionInformation() ) ) {
  }
}

这有点笨拙,但好处是您可以将 __try { } __ except() { } 部分放入通用 dumpStackTrace() 函数中。然后,您可以根据需要从程序中的任何点生成堆栈跟踪。

You can use the try-except Statement to "convert" a C++ exception to a structured exception (out of which you can then get a nice stack trace). Consider this:

// Your function to get a backtrace from a CONTEXT
const char *readBacktrace( CONTEXT &ctx );

extern "C"
static DWORD exceptFilter( struct _EXCEPTION_POINTERS* exInf )
{
    OutputDebugStringA( readBacktrace( *exInf->ContextRecord ) );
    return EXCEPTION_EXECUTE_HANDLER;
}

try {
  // your C++ code which might yield exceptions
} catch ( ... ) {
  // In case a C++ exception occurs, raise a structured exception and catch it    immediately
  // so that we get a CONTEXT object which we can use to generate a stack trace.
  __try {
    RaiseException( 1, 0, 0, NULL );
  } __except( exceptFilter( GetExceptionInformation() ) ) {
  }
}

This is a little clumsy, but the nice thing is that you can put the __try { } __except() { } part into a general purpose dumpStackTrace() function. You can then yield stack traces from any point in your program, as you like.

哽咽笑 2024-08-19 13:03:41

尝试使用 set_terminate 安装终止处理程序。并在其中使用迷你转储函数抓取堆栈跟踪。也许会起作用。

Try using set_terminate to install terminate handler. And in it grab stack trace using mini dump funcitons. Maybe it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文