如何在 .NET VM (CLR) 中查看 JIT 编译的代码
如何跟踪 JIT 编译器生成的本机代码?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何跟踪 JIT 编译器生成的本机代码?
谢谢
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
在 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).
如果您仅在标准调试或发布 exe 上使用“调试”->“Windows”->“反汇编”,而不修改 Visual Studio 调试选项,您将只会看到非优化 .NET 代码的版本。
看看这篇文章“如何使用 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:
您甚至可以使用 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.
您应该查找 NGen 工具。 NGen 编译程序集的预编译版本并将其存储在全局程序集缓存中。
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.
最新版本的 .NET 可以提供更跨平台、跨体系结构、仅限本地和开源的方法。这还允许您构建/修改 JIT 本身并查看结果。完整步骤如下所述:
https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/viewing-jit-dumps.md
缺点是它不那么“简单”或易于使用开箱即用。
归结为:
构建您的应用程序并发布它。像这样的事情:
dotnetpublish-cRelease-rlinux-x64
但是将
linux-x64
替换为适当的操作系统/架构组合,例如win-x64
或osx-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.x64
、net6.0
和linux-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, likewin-x64
orosx-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
andlinux-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