在 Linux 上为独立的 .NET Core 应用程序运行 dotnet-sos、dotnet-dump、dotnet-gcdump

问题描述

我有一个 Ubuntu 18.04 x64 Linux 服务器,运行着一堆 .NET Core 应用程序。所有这些都作为独立的应用程序发布,.NET Core 版本在 2.1、3.1 和 5.0 之间变化。我没有在机器上安装 .NET Core 运行时(更不用说 SDK)并且不想安装它。

我想为 .NET Core 3.1 自包含应用程序运行 dotnet-sosdotnet-dumpdotnet-gcdump 等工具。链接页面为它们提供直接下载链接,例如https://aka.ms/dotnet-sos/linux-x64。但是,当我运行下载的 dotnet-sos 工具时,它失败了

A Fatal error occurred. The required library libhostfxr.so Could not be found.
If this is a self-contained application,that library should exist in [/var/tmp/.net/MY_USERNAME/dotnet-sos/mrdxw5vu.czm/].
If this is a framework-dependent application,install the runtime in the global location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [/etc/dotnet/install_location].

The .NET Core runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=ubuntu.18.04-x64

即使当前工作目录是我的 .NET 应用程序目录,它也会发生这种情况,其中包含 libhostfxr.so。该错误意味着应该可以为自包含应用程序运行该工具,但该文件确实不存在于 /var/tmp/.net/MY_USERNAME/dotnet-sos/mrdxw5vu.czm/ 中。如果我手动将该文件复制到上面的目录,它就会失败

It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App',version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.0&arch=x64&rid=ubuntu.18.04-x64

如何在不安装 .NET 运行时机器范围内运行此类工具?我可以轻松地从 https://dotnet.microsoft.com/download/dotnet/thank-you/runtime-3.1.14-linux-x64-binaries 下载二进制文件,但我应该把它们放在哪里以便工具找到它们?

解决方法

事实证明 .NET SDK 可以解压缩到任何目录,然后用于安装和运行 dotnet-dumpdotnet-gcdump,如下所示:

curl -fsSL https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash -s -- --no-path --install-dir ~/dotnet-sdk --channel 5.0
~/dotnet-sdk/dotnet tool install --tool-path ~/dotnet-sdk/tools dotnet-dump
DOTNET_ROOT=~/dotnet-sdk ~/dotnet-sdk/tools/dotnet-dump collect -p PID_TO_DUMP

(不过,我没有设法让 SOS 在 LLDB 中工作。“安装”它的工作原理与上述相同,但无法在 LLDB 中加载。)