如何从托管代码中捕获的本机异常获取本机堆栈跟踪 [英] How to obtain the native stacktrace from native exceptions caught in managed code

查看:211
本文介绍了如何从托管代码中捕获的本机异常获取本机堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些托管代码调用一些本机DLL中的方法(我有适当的符号文件)。

有时,该本机方法会引发我在托管代码中捕获的异常。但是,当我从我捕获的异常打印stacktrace时,我只看到托管代码(最后一帧是调用本机代码..但是在本机代码中看不到stacktrack)。



如何获取本机callstack?

*当我调试代码时,我可以进入本机代码,并查看实际调用堆栈。

解决方案

当您捕获异常时,这不像显示本机调用堆栈那么光滑,但如果您尝试追踪用户计算机上的某个特定问题,并且用户相当精明,您可以让他们在WinDbg下运行您的应用程序。当抛出本机异常并且可以查看调用堆栈时,它将中断。



另一种可能性是使用stackwalker。这是免费的,可以在这里:
http://www.codeproject.com /KB/threads/StackWalker.aspx
如果您知道顶级本地调用,则可以使用__try / __ catch来包装,并使用stackwalker将堆栈转储到日志文件。大概你也可以捕获异常,使用stackwalker获取调用堆栈,将callstack添加到异常(作为字符串),然后将异常重新抛出到您的.NET代码中。然后,.NET代码可以从您的异常中获取调用堆栈。


I have some managed code that calls to a method inside some native DLL(i have the appropriate symbol files).
Sometimes, that native method throws an exception which I catch in my managed code. However, when i print the stacktrace from my caught exception, I see only managed code (the last frame is the call to the native code .. but it don't see the stacktrack within the native code).

How can I obtain the native callstack as well?
*When i'm debugging the code, i am able to step into the native code, and see the actuall call stack.

解决方案

This isn't as slick as displaying the native call stack when you catch the exception, but if you are trying to track down a specific problem on a user's computer and the user is reasonably savvy you can have them run your app under WinDbg. It will break when the native exception is thrown and the call stack can be viewed.

Another possibility is to use stackwalker. It's free and is available here: http://www.codeproject.com/KB/threads/StackWalker.aspx If you know the top-level native call, you can wrap that with a __try/__catch and use stackwalker to dump the stack to a log file. Presumably you could also catch the exception, get the call stack using stackwalker, add the callstack to the exception (as a string), and then rethrow the exception to your .NET code. The .NET code could then get the callstack out of your exception.

这篇关于如何从托管代码中捕获的本机异常获取本机堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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