在反汇编视图中进入 P/Invoke 调用
我的 C# 代码正在通过 P/Invoke 调用非托管第三方库函数,并且非托管函数有一些奇怪的副作用。我想调试它并看看它在做什么。
如果我调试 C# 代码,并尝试“单步执行”P/Invoke 调用,它会跳过。这并不奇怪——这是我预料到的;它没有这个 DLL 的源代码,而且我也没有告诉它我可以查看反汇编视图。
因此,我将调试器切换到反汇编视图(“调试”>“Windows”>“反汇编”)。现在我在 JITted 代码中看到了单独的 x86 指令。我再次尝试进入 P/Invoke 调用。同样,它还是单步执行——尽管我明确告诉它单步执行 x86 CALL 指令。进入 x86 CALL 有多难?
到目前为止,我的谷歌搜索已经向我展示了几个可能影响此问题的选项,并且我已经设置了它们:
- 在“工具”>“选项>调试>一般情况下,“仅启用我的代码”未选中。
- 在项目中>属性>调试选项卡中,选中“启用非托管代码调试”。
不好。 Visual Studio 仍然拒绝介入。
我没有第三方 DLL 的 PDB,但这应该不重要。我不关心源代码或符号信息。 (嗯,实际上它们真的很好,但我已经知道我不会得到它们。)Visual Studio 可以进行 x86 调试(这就是反汇编视图的用途),并且所有我想做的是单步执行x86代码。
我还需要做什么才能让 VS 允许我单步执行 P/Invoke 调用中的 x86 指令?
My C# code is calling an unmanaged third-party library function via P/Invoke, and the unmanaged function is having some strange side effects. I want to debug into it and see what it's doing.
If I debug my C# code, and try to "Step Into" the P/Invoke call, it steps over instead. No surprise there -- I expected that; it doesn't have the source for this DLL, and I didn't tell it I was okay with seeing the disassembly view.
So I switch the debugger to disassembly view (Debug > Windows > Disassembly). Now I see the individual x86 instructions in my JITted code. Again I try to step into the P/Invoke call. Again, it steps over instead -- even though I clearly told it to Step Into an x86 CALL instruction. How hard is it to step into an x86 CALL?
My Googling thus far has shown me a couple of options that can affect this, and I've already set them:
- In Tools > Options > Debugging > General, "Enable Just My Code" is unchecked.
- In Project > Properties > Debug tab, "Enable unmanaged code debugging" is checked.
No good. Visual Studio still refuses to step in.
I don't have a PDB for the third-party DLL, but that shouldn't matter. I don't care about source code or symbol information. (Well, actually they'd be really nice, but I already know I'm not going to get them.) Visual Studio can do x86 debugging (that's what the Disassembly view is for), and all I want to do is step into the x86 code.
What else do I need to do to get VS to allow me to step into the x86 instructions inside a P/Invoke call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这可能会帮助您解决问题:
(作者:Graviton)
这还提到当跨越边界时,您需要分离托管调试器并重新附加非托管调试器。您可能需要从 MSDN 检查混合调试器的功能及其首选项。
最后,使用 Ed多尔的回答:
This may help you solve the problem:
(by Graviton)
Also this mentions that you need to detach managed debugger and re-attach unmanaged when crossing the boundaries. You might need to check the capabilities of mixed debugger and it's preferences from MSDN.
And finally, using Ed Dore' answer:
我会尝试的一件事是从 C# 转到 C++/CLI 代码,然后从 C++ 转到第三方代码。一旦您使用 C++(并且摆脱了 P/Invoke 基础结构),您可能会更幸运地使用反汇编视图。
One thing I would try is going from C# to C++/CLI code, and then from C++ to the third-party code. Once you're in C++ (and free of the P/Invoke infrastructure), you might have better luck with the disassembly view.
在 C# 项目属性的“调试”选项卡中,选中“启用本机代码调试”。在 VS 2012 中为我工作。
感谢 billb。
另外,由于它是第三方库,请确保在“选项”>“仅启用我的代码”中取消选中。调试。
In your C# project properties, in the Debug tab, check Enable native code debugging. Worked for me in VS 2012.
Credit goes to billb.
Also, since it's a third party library, ensure Enable Just My Code is unchecked in Options > Debugging.
我遇到了类似的问题,我正在调试一个 C# exe,它通过 PInvoke 调用我自己的 C++ dll,所有这些都是同一解决方案的一部分。在我的 C# 项目中启用本机代码调试使我能够调试我的 C++ 代码。
I had a similar issue where I was debugging a C# exe that called my own C++ dll via PInvoke, all part of the same solution. Enabling native code debugging in my c# project allowed me to debug my C++ code.
今天有同样的问题。
对我来说有帮助的是以管理员身份运行 Visual Studio。
我的设置:
Had same issue today.
What helped in my case was to run Visual Studio as administrator.
My setup: