如何在 .NET VM (CLR) 中查看 JIT 编译的代码

发布于 2024-08-24 06:16:57 字数 38 浏览 9 评论 0 原文

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

谢谢

How can I have a trace of native code generated by the JIT-Compiler ?

Thanks

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

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

发布评论

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

评论(5

尝蛊 2024-08-31 06:16:57

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

In Visual Studio place a breakpoint in the code and start debugging. When it breaks, open the Disassembly window (Debug > Windows > Disassembly or Alt+Ctrl+D).

秋意浓 2024-08-31 06:16:57

如果您仅在标准调试或发布 exe 上使用“调试”->“Windows”->“反汇编”,而不修改 Visual Studio 调试选项,您将只会看到非优化 .NET 代码的版本。

看看这篇文章“如何使用 Visual Studio 查看 JIT 生成的汇编代码"。它解释了如何检查生成的 JIT 优化代码。

文章中的一段相关引用:

  • 在 Visual Studio 中配置调试选项以允许 JIT 生成优化代码并允许您调试优化后的代码
    代码。
  • 转到工具=>选项=>调试=>一般事项 · 确保
    标有“抑制模块加载时的 JIT 优化”的框是
    未选中。

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

    If you just use Debug->Windows->Disassembly on a standard Debug or Release exe, without modifying Visual Studio Debugging options, you will just see a version of non optimized .NET code.

    Have a look at this article "How to see the Assembly code generated by the JIT using Visual Studio". It explains how to inspect generated JIT optimized code.

    One relevant quote from the article:

    1. Configure the Debugging Options in Visual Studio to allow the JIT to generate optimized code and to allow you to debug the optimized
      code.

    Go to Tools => Options => Debugging => General · Make sure
    that box labeled ‘Suppress JIT optimization on module load’ is
    Unchecked.

    · Make sure that the box labeled ‘Enable Just My Code’ is
    Unchecked.

    江心雾 2024-08-31 06:16:57

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

    最近流行的编译器资源管理器也开始支持.NET 语言。这是示例 => https://godbolt.org/z/P49Y6Ejh6
    它不如 SharpLab 快,但仍然是一个可行的选择。

    You can even use Sharplab to see generated code => https://sharplab.io/ . In this, you can quickly see the generated code based on what C# code you write in both Debug and Release configuration.

    Recently popular compiler explorer also started supporting .NET languages. Here is the example => https://godbolt.org/z/P49Y6Ejh6
    It's not fast as SharpLab, but still it's a viable option to look for.

    病女 2024-08-31 06:16:57

    You should look for the files output from the NGen tool. NGen compiles and stores pre-jitted versions of assemblies in the Global Assembly Cache.

    娇纵 2024-08-31 06:16:57

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

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

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

    归结为:

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

      dotnetpublish-cRelease-rlinux-x64

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

    • 构建 clrjit 的调试版本:

      git 克隆 https://github.com/dotnet/runtime
      cd 运行时
      ./build clr -c 调试

    • 将应用程序的 clrjit 替换为您构建的 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= 环境变量并运行应用程序以将 JIT 输出转储到标准输出。

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

    There's a more cross-platform, cross-architecture, local-only and open source approach possible with recent versions of .NET. This also allows you to build/modify the JIT itself and see the results. The complete steps are described at:

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

    The downside is that it's not as "simple" or easy to use out of the box.

    It boils down to:

    • Build your application and publish it. Something like this:

      dotnet publish -c Release -r linux-x64

      But replace linux-x64 with the appropriate OS/architecture combo, like win-x64 or osx-arm64 as appropriate.

    • Build a Debug build of the clrjit:

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

    • Replace your application's clrjit with the one you built

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

      Adjust Linux.x64, net6.0 and linux-x64 as appropriate.

    • Set the COMPlus_JitDump=<Method> environment variable and run the application to dump the JIT output to the standard output.

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

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