在反汇编视图中进入 P/Invoke 调用

发布于 2024-08-07 07:23:18 字数 705 浏览 11 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(5

眸中客 2024-08-14 07:23:18

可能会帮助您解决问题:
(作者:Graviton)

CallingConvention = CallingConvention.Cdecl

还提到当跨越边界时,您需要分离托管调试器并重新附加非托管调试器。您可能需要从 MSDN 检查混合调试器的功能及其首选项。

最后,使用 Ed多尔的回答:

在“工具.选项”对话框下,选择
调试类别,并确保
“仅启用我的代码”设置是
未经检查。来自项目
属性,选择“调试”选项卡,然后
然后确保“启用非托管
代码调试”被选中。

一旦你把这些摆好,
你应该使用混合模式
调试支持工作。

此外,如果您使用“Debug.Attach To
处理”,一定要点击
“附加到”中的“选择...”按钮
处理”对话框,然后选择两者
托管和本机调试支持。

This may help you solve the problem:
(by Graviton)

CallingConvention = CallingConvention.Cdecl

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:

Under Tools.Options dialog, select the
Debugging category, and make sure the
"Enable Just My Code" setting is
unchecked. From the Project
properties, select the Debug tab, and
then ensure that "Enable unmanaged
code debugging" is checked.

Once you've got these squared away,
you should get the mixed mode
debugging support working.

Also, if you use "Debug.Attach To
Process" , be sure to hit the
"Select..." button in the "Attach To
Process" dialog, and select both
Managed and Native debugging support.

梦醒灬来后我 2024-08-14 07:23:18

我会尝试的一件事是从 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.

安静被遗忘 2024-08-14 07:23:18

在 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.

素手挽清风 2024-08-14 07:23:18

我遇到了类似的问题,我正在调试一个 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.

污味仙女 2024-08-14 07:23:18

今天有同样的问题。
对我来说有帮助的是以管理员身份运行 Visual Studio。

我的设置:

  • Visual Studio 2019
  • Windows 10

Had same issue today.
What helped in my case was to run Visual Studio as administrator.

My setup:

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