如何查看优化的 jitted .NET 代码的反汇编?

发布于 2024-09-13 17:06:15 字数 444 浏览 11 评论 0原文

出于某种原因,我有时发现查看函数的优化编译器输出很有用或很有趣。

对于非托管 C/C++ 代码,我最喜欢的方法是在发布模式下编译,在感兴趣的函数中设置断点,运行并在遇到断点时在 Visual Studio 中查看反汇编结果。

我最近在一个 C# 项目中尝试了这一点,发现该技术不起作用。即使在发布模式下,我看到的反汇编显然也没有优化。我发现并禁用了(在 Visual Studio 2010 中)“调试...选项和设置...调试...常规...抑制模块加载时的 JIT 优化”选项,这可能让我更接近我想要的,只是现在,当我尝试运行它时,它会警告我,然后我无法让它在断点处停止,以便我可以看到反汇编。

那么,如果我想查看某个函数的 CLR (4.0) 抖动的反汇编、优化输出,最好的方法是什么?需要明确的是,我希望看到 x86(或者最好是 x86_64)反汇编,而不仅仅是 IL 反汇编(您可以在 Reflector 中看到)。

For one reason or another, I sometimes find it useful or just interesting to look at the optimised compiler output for a function.

For unmanaged C/C++ code, my favourite way to do this has been to compile in Release mode, stick a breakpoint in the function of interest, run, and view the disassembly in Visual Studio when it hits the breakpoint.

I recently tried this with a C# project and discovered that that technique doesn't work. Even in Release mode, the disassembly I see is obviously not optimised. I found and disabled (in Visual Studio 2010) the "Debug ... Options and Settings ... Debugging ... General ... Suppress JIT optimization on module load" option, which presumeably gets me closer to what I want, only now it warns me when I try to run it, and I then can't get it to stop on a breakpoint so that I can see the disassembly.

So, if I want to see the disassembled, optimised output of the CLR (4.0) jitter for a function, what's the best way to go about that? To be clear, I'd like to see the x86 (or preferably x86_64) disassembly, not just the IL disassembly (which you can see in Reflector).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

攒一口袋星星 2024-09-20 17:06:15

当然,经过半天的寻找这个问题的答案,我在SO上问了5分钟后自己找到了答案。

我很接近;我在问题中唯一缺少的一步是“仅启用我的代码”也必须在选项中取消选中。

完整指南可在此处获取:<链接>

Of course, after half a day of searching for the answer on this one, I find the answer myself 5 minutes after I ask on SO.

I was close; the only missing step from what I had in the question was that "Enable Just My Code" must also be unchecked in the options.

The full guide is available here: <Link>

維他命╮ 2024-09-20 17:06:15

我相信 JIT 知道您何时在调试器下运行,并生成更多“调试器友好”的 x86 代码,这可以解释为什么您看到的 x86 代码未优化。

您可以尝试独立运行应用程序,至少执行一次您感兴趣的代码(因此它可以在不附加调试器的情况下进行 JIT),然后将调试器附加到进程并设置断点。

I believe the JIT knows when you're running under a debugger, and generates more "debugger-friendly" x86 code, which would explain why the x86 code you saw was unoptimized.

You might try running the app standalone, executing the code you're interested in at least once (so it JITs without the debugger attached), then attach the debugger to the process and set your breakpoint.

明月夜 2024-09-20 17:06:15

优化代码中的断点在内联函数中不起作用。如果要查看内联函数的反汇编,可以将命令 System.Diagnostics.Debugger.Break(); 插入源代码中。

Breakpoints in optimized code don't work in functions which are inlined. If you want to view disassembly of an inline function, you can insert command System.Diagnostics.Debugger.Break(); into your source code.

短叹 2024-09-20 17:06:15

您可以尝试检查 NGEN 的程序集,但这会非常困难,因为不存在元数据。但它可以工作:)

You could try inspecting an NGEN'd assembly, but that would be quite hard as no metadata is present. But it could work :)

一曲爱恨情仇 2024-09-20 17:06:15

要在发布版本中查看托管代码的反汇编程序中的优化代码,请在选项/调试/常规中配置以下内容:

  • 禁用“仅启用我的代码”
  • 禁用“自动抑制调试方法的 JIT 优化”
  • 禁用“抑制模块加载时的 JIT 优化” ”

To see optimized code in the disassembler for managed code in a Release build, configure the following in Options/Debugging/General:

  • Disable "Enable Just My Code"
  • Disable "Automatically suppress JIT optimizations for debugged methods"
  • Disable "Suppress JIT optimization on module load"
墨洒年华 2024-09-20 17:06:15

您可以使用 https://sharplab.io/ 作为代码片段。它将显示 IL 以及 JIT 优化的汇编代码。

如果您想在调试时看到这一点,您可以在“调试”|“反汇编和寄存器”窗口中查看。 Windows 菜单选项(仅在调试时可用)。确保您处于“发布”配置,并且“发布”配置已选中“优化代码”。

根据您的情况,您可能需要配置的一些调试选项是:

  • 选中“启用地址级调试”
  • 选中“如果源不可用则显示反汇编”
  • 取消选中“仅启用我的代码”
  • 取消选中“自动抑制 JIT 优化”顺便
  • 说一句,取消选中“抑制模块加载时的 JIT 优化”

,无论您是否处于调试或发布模式,在 VisualStudio 下运行仍将在调试器下运行,即使应用程序代码可能已优化。

You can use https://sharplab.io/ for snippets of code. It will show IL as well as the JIT Optimized Assembly code.

If you want to see this while debugging, you can look in the Disassembly and Registers Windows under the Debug | Windows menu option (only available while debugging). Make sure you are in 'Release' configuration and your 'Release' configuration has 'Optimize Code' checked.

Some Debug Options you may want to configure when de depending on your case, would be:

  • Check 'Enable address-level debugging'
  • Check 'Show disassembly if source is not available'
  • Uncheck 'Enable Just My Code'
  • Uncheck 'Automatically suppress JIT optimizations for debugged methods'
  • Uncheck 'Suppress JIT Optimization on module load'

BTW, running under VisualStudio regardless of whether or not you're in Debug or Release mode will still run under the Debugger even though the application code may be optimized.

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