如何附加调试器以从托管 (C#) 包装器进入本机 (C++) 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to attach debugger to step into native (C++) code from a managed (C#) wrapper?
提问by Nikhil
I have a wrapper around a C++ function call which I call from C# code. How do I attach a debugger in Visual Studio to step into the native C++ code?
我有一个 C++ 函数调用的包装器,我从 C# 代码调用它。如何在 Visual Studio 中附加调试器以进入本机 C++ 代码?
This is the wrapper that I have which calls GetData() defined in a C++ file:
这是我调用在 C++ 文件中定义的 GetData() 的包装器:
[DllImport("Unmanaged.dll", CallingConvention=CallingConvention.Cdecl,
EntryPoint = "GetData", BestFitMapping = false)]
public static extern String GetData(String url);
The code is crashing and I want to investigate the root cause.
代码崩溃了,我想调查根本原因。
Thanks, Nikhil
谢谢,尼基尔
采纳答案by Lou
Check the Debug tab on your project's properties page. There should be an "Enable unmanaged code debugging" checkbox. This worked for me when we developed a new .NET UI for our old c++ DLLs.
检查项目属性页面上的调试选项卡。应该有一个“启用非托管代码调试”复选框。当我们为旧的 c++ DLL 开发新的 .NET UI 时,这对我有用。
If your unmanaged DLL is being built from another project (for a while ours were being built using VS6) just make sure you have the DLL's pdb file handy for the debugging.
如果您的非托管 DLL 是从另一个项目构建的(有一段时间我们是使用 VS6 构建的),请确保您有 DLL 的 pdb 文件便于调试。
The other approach is to use the C# exe as the target exe to run from the DLL project, you can then debug your DLL normally.
另一种方法是使用 C# exe 作为目标 exe 从 DLL 项目中运行,然后您可以正常调试您的 DLL。
回答by Rob Walker
in addition to Lou's advise for starting the debugger, you can select which debug engines are used when attaching to an existing process by clicking on 'Select...' in the 'attach to process' dialog and choosing both 'managed code' and 'native code'.
除了 Lou 对启动调试器的建议之外,您还可以通过单击“附加到进程”对话框中的“选择...”并同时选择“托管代码”和“本机代码'。
Debugging in this way is called mixed mode debugging. See this blog postfor some tips.
这种方式的调试称为混合模式调试。有关一些提示,请参阅此博客文章。
I believe this isn't supported for 64 bit processes ... though would love to be wrong on that point.
我相信这不支持 64 位进程......尽管在这一点上很可能是错误的。
回答by Rob Walker
To anyone using WinDbg:
对于任何使用 WinDbg 的人:
1>Setup symbols
1>设置符号
Look at these commands. (Help: in console .hh < command> )
看看这些命令。(帮助:在控制台 .hh < command> )
.sympath
.sympath+
.symfix
2>Set up source path
2>设置源路径
.srcpath
3>Load SOS extention to debug managed / mixed mode programs.
3>加载 SOS 扩展以调试托管/混合模式程序。
(Make sure you have extention path setup correctly)
(确保正确设置了扩展路径)
Add Microsoft.NET\Framework\v2.0.50727 for x86 using-
添加 Microsoft.NET\Framework\v2.0.50727 for x86 using-
.extpath
Set a breakpoint for the clr to load.
为要加载的 clr 设置断点。
sxe ld:mscorwks
(F5 / g) (Wait for ModLoad BP on mscorwks.dll)
(F5 / g)(等待 mscorwks.dll 上的 ModLoad BP)
Make sure you dont have a duplicate sos extention already loaded. See:
确保您没有加载重复的 sos 扩展。看:
.chain
Now we're ready to load the sos extention. :)
现在我们准备加载 sos 扩展。:)
.loadby sos mscorwks
4> Reload all the symbols..
4>重新加载所有符号..
.reload
Now you're all set :)
现在你已经准备好了:)
(YMMV)
(YMMV)
回答by Rob Walker
Mixed debugging is not supported in 64bit mode (as of Visual Studio 2008).
64 位模式不支持混合调试(从 Visual Studio 2008 开始)。