开发者

How to obtain the native stacktrace from native exceptions caught in managed code

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.


Obtaining a native stack trace is quite difficult. By the time it passes through the .NET/native translation layer, the native stack trace has already been lost.

So, you need to capture it while still in native code, and this is also quite difficult. Take a look at John Robbins' work for proper native stack tracing; the latest publicly-available version of his SUPERASSERT that I could find is from MSJ, Feb 1999.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜