【问题标题】:How to see JIT-Compiled code in .NET VM (CLR)如何在 .NET VM (CLR) 中查看 JIT 编译的代码
【发布时间】:2011-01-25 11:52:11
【问题描述】:

如何跟踪 JIT 编译器生成的本机代码?

谢谢

【问题讨论】:

  • 您只是想查看 IL 还是真的想在 IL 级别进行调试?
  • 不,我想看原生代码:源代码 => C# 编译器 => IL => JIT => 原生代码

标签: .net clr jit


【解决方案1】:

在 Visual Studio 中,在代码中放置一个断点并开始调试。当它中断时,打开反汇编窗口(调试 > 窗口 > 反汇编或 Alt+Ctrl+D)。

【讨论】:

  • 有没有办法将其转储到文件中?
  • 全选,复制,打开记事本,粘贴并保存。
  • 默认情况下,当您在 Visual Studio 中运行时,JITting 是关闭的。与运行 EXE 文件相比,直接从 Visual Studio(为发布模式构建)运行代码通常存在巨大的性能差异。您可以在工具、选项、调试、“在模块加载期间抑制 JIT 优化”下进行更改。另请参阅此 Q/A:stackoverflow.com/questions/4043821/…
  • 你当然是对的,我已经用草率表达了自己。我的观点是,如果忽略查看优化的代码,您会误以为“真正”会执行什么。
  • @DanByström 感谢您指出这一点。 Guffa 对 Mutel 的回答的评论是错误的,他后来对您关于关闭 JIT 优化的评论的回应是正确的。大多数查看生成的 JIT 代码的人都会对性能感兴趣,在这种情况下,启用优化至关重要。
【解决方案2】:

如果您只是在标准 Debug 或 Release exe 上使用 Debug->Windows->Disassembly,而不修改 Visual Studio 调试选项,您将只会看到 未优化 .NET 代码的版本。

看看这篇文章“How to see the Assembly code generated by the JIT using Visual Studio”。它解释了如何检查生成的 JIT 优化代码。

文章中的一个相关引述:

  1. 在 Visual Studio 中配置调试选项以允许 JIT 生成优化的代码并允许您调试优化的代码 代码。

转到工具 => 选项 => 调试 => 常规 · 确保 标有“在模块加载时抑制 JIT 优化”的框是 未选中。

· 确保标有“仅启用我的代码”的框是 未选中。

【讨论】:

  • 这不正确。在发布模式下,您将看到优化的代码。文章讲的是调试模式。
  • @Guffa 我将文章的相关部分复制到此答案中,但更具体地反驳了您的上述主张:Ideally you would have wanted it to be the case that simply changing the configuration in the Solution Configuration window to ‘Release’ would be sufficient.
  • @EugeneBeresovsky:对不起,你搞混了。文章在您引用的段落中所说的不是生成优化代码,而是生成调试信息。无需调试信息即可查看优化代码。
  • @Guffa 发布模式 本身 不会让您在调试时看到优化的代码,正如我在上面评论中的引用中所说的那样。只需阅读settings that you will need to change in order for you to see the Optimized code generated by the JIT compiler. 更改为发布模式之后的 3 个必要步骤,这只是第一步,其他步骤是 Set Generate debug info to pdb-only,最后一个是关于取消选中 Suppress JIT optimization on module load启用仅我的代码.
  • @Guffa 拼写也很明确:Whenever you launch a managed program under Visual Studio using (Start-Debugging or F5), it will by default, force the JIT to create Debug code. This is true even when you have selected the 'Release' configuration. The reason for this is to improve the debugging experience, but it also makes it impossible to see the Optimized code that you will get whenever your program is not running under the Visual Studio debugger.
【解决方案3】:

您应该查找从NGen tool 输出的文件。 NGen 在全局程序集缓存中编译和存储程序集的预编译版本。

【讨论】:

    【解决方案4】:

    您甚至可以使用 Sharplab 查看生成的代码 => https://sharplab.io/ 。在此您可以快速查看基于您在调试和发布配置中编写的 C# 代码生成的代码。

    【讨论】:

      【解决方案5】:

      最新版本的 .NET 可以提供更多跨平台、跨架构、仅限本地和开源的方法。这还允许您构建/修改 JIT 本身并查看结果。完整的步骤描述在:

      https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/viewing-jit-dumps.md

      缺点是它不那么“简单”或开箱即用。

      归结为:

      • 构建您的应用程序并发布它。像这样的:

        dotnet publish -c Release -r linux-x64

        但将linux-x64 替换为适当的操作系统/架构组合,如win-x64osx-arm64 视情况而定。

      • 构建 clrjit 的调试版本:

        git clone https://github.com/dotnet/runtime cd runtime ./build clr -c Debug

      • 将应用程序的 clrjit 替换为您构建的那个

        cp /path/to/dotnet/runtime/artifacts/bin/coreclr/Linux.x64.Debug/* bin/Release/net6.0/linux-x64/publish/

        适当调整Linux.x64net6.0linux-x64

      • 设置COMPlus_JitDump=<Method> 环境变量并运行应用程序以将JIT 输出转储到标准输出。

        COMPlus_JitDump=Main ./bin/Release/net6.0/linux-x64/publish/Application

      【讨论】:

        猜你喜欢
        • 2010-12-02
        • 1970-01-01
        • 2011-03-18
        • 2015-08-28
        • 2015-03-23
        • 2010-11-18
        • 1970-01-01
        相关资源
        最近更新 更多