【发布时间】:2011-01-25 11:52:11
【问题描述】:
如何跟踪 JIT 编译器生成的本机代码?
谢谢
【问题讨论】:
-
您只是想查看 IL 还是真的想在 IL 级别进行调试?
-
不,我想看原生代码:源代码 => C# 编译器 => IL => JIT => 原生代码
如何跟踪 JIT 编译器生成的本机代码?
谢谢
【问题讨论】:
在 Visual Studio 中,在代码中放置一个断点并开始调试。当它中断时,打开反汇编窗口(调试 > 窗口 > 反汇编或 Alt+Ctrl+D)。
【讨论】:
如果您只是在标准 Debug 或 Release exe 上使用 Debug->Windows->Disassembly,而不修改 Visual Studio 调试选项,您将只会看到 未优化 .NET 代码的版本。
看看这篇文章“How to see the Assembly code generated by the JIT using Visual Studio”。它解释了如何检查生成的 JIT 优化代码。
文章中的一个相关引述:
- 在 Visual Studio 中配置调试选项以允许 JIT 生成优化的代码并允许您调试优化的代码 代码。
转到工具 => 选项 => 调试 => 常规 · 确保 标有“在模块加载时抑制 JIT 优化”的框是 未选中。
· 确保标有“仅启用我的代码”的框是 未选中。
【讨论】:
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.
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 和 启用仅我的代码.
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.
您应该查找从NGen tool 输出的文件。 NGen 在全局程序集缓存中编译和存储程序集的预编译版本。
【讨论】:
您甚至可以使用 Sharplab 查看生成的代码 => https://sharplab.io/ 。在此您可以快速查看基于您在调试和发布配置中编写的 C# 代码生成的代码。
【讨论】:
最新版本的 .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-x64 或osx-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.x64、net6.0和linux-x64。
设置COMPlus_JitDump=<Method> 环境变量并运行应用程序以将JIT 输出转储到标准输出。
COMPlus_JitDump=Main ./bin/Release/net6.0/linux-x64/publish/Application
【讨论】: