Introduce a new kernel command line argument to collect hvsocket event logs during boot (#13537)

* Introduce a new kernel command line argument to collect hvsocket event logs during boot

* Cleanup diff

* unset env

* Add test coverage

* Fix format

* Remove prefix
This commit is contained in:
Blue
2025-10-01 15:35:49 -07:00
committed by GitHub
parent 582e15e4e0
commit 8313d50f2e
3 changed files with 99 additions and 19 deletions

View File

@@ -3875,6 +3875,63 @@ Return Value:
int WslEntryPoint(int Argc, char* Argv[]);
void EnableDebugMode(const std::string& Mode)
{
if (Mode == "hvsocket")
{
// Mount the debugfs.
THROW_LAST_ERROR_IF(UtilMount("none", "/sys/kernel/debug", "debugfs", 0, nullptr) < 0);
// Enable hvsocket events.
std::vector<const char*> files{
"/sys/kernel/debug/tracing/events/hyperv/vmbus_on_msg_dpc/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_on_message/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_onoffer/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_onoffer_rescind/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_onopen_result/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_ongpadl_created/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_ongpadl_torndown/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_open/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_close_internal/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_establish_gpadl_header/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_establish_gpadl_body/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_teardown_gpadl/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_release_relid/enable",
"/sys/kernel/debug/tracing/events/hyperv/vmbus_send_tl_connect_request/enable"};
for (auto* e : files)
{
WriteToFile(e, "1");
}
// Relay logs to the host.
std::thread relayThread{[]() {
constexpr auto path = "/sys/kernel/debug/tracing/trace_pipe";
std::ifstream file(path);
if (!file)
{
LOG_ERROR("Failed to open {}, {}", path, errno);
return;
}
std::string line;
while (std::getline(file, line))
{
LOG_INFO("{}", line);
}
LOG_ERROR("{}: closed", path);
}};
relayThread.detach();
}
else
{
LOG_ERROR("Unknown debugging mode: '{}'", Mode);
}
}
int main(int Argc, char* Argv[])
{
std::vector<gsl::byte> Buffer;
@@ -3997,6 +4054,37 @@ int main(int Argc, char* Argv[])
}
}
//
// Create the etc directory and mount procfs and sysfs.
//
if (UtilMkdir(ETC_PATH, 0755) < 0)
{
return -1;
}
if (UtilMount(nullptr, PROCFS_PATH, "proc", 0, nullptr) < 0)
{
return -1;
}
if (UtilMount(nullptr, SYSFS_PATH, "sysfs", 0, nullptr) < 0)
{
return -1;
}
//
// Enable debug mode, if specified.
//
if (const auto* debugMode = getenv(WSL_DEBUG_ENV))
{
LOG_ERROR("Running in debug mode: '{}'", debugMode);
EnableDebugMode(debugMode);
unsetenv(WSL_DEBUG_ENV);
}
//
// Establish the message channel with the service via hvsocket.
//
@@ -4024,25 +4112,6 @@ int main(int Argc, char* Argv[])
goto ErrorExit;
}
//
// Create the etc directory and mount procfs and sysfs.
//
if (UtilMkdir(ETC_PATH, 0755) < 0)
{
return -1;
}
if (UtilMount(nullptr, PROCFS_PATH, "proc", 0, nullptr) < 0)
{
return -1;
}
if (UtilMount(nullptr, SYSFS_PATH, "sysfs", 0, nullptr) < 0)
{
return -1;
}
if (getenv(WSL_ENABLE_CRASH_DUMP_ENV))
{
Config.EnableCrashDumpCollection = true;

View File

@@ -191,6 +191,8 @@ Abstract:
#define WSL_ENABLE_CRASH_DUMP_ENV "WSL_ENABLE_CRASH_DUMP"
#define WSL_DEBUG_ENV "WSL_DEBUG"
#define WSL_DISTRIBUTION_CONF "/etc/wsl-distribution.conf"
//

View File

@@ -6147,5 +6147,14 @@ Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n",
VERIFY_ARE_EQUAL(err, L"");
}
TEST_METHOD(WslDebug)
{
WSL2_TEST_ONLY();
// Verify that hvsocket debug events are logged to dmesg.
WslConfigChange config(LxssGenerateTestConfig({.kernelCommandLine = L"WSL_DEBUG=hvsocket"}));
VERIFY_ARE_EQUAL(LxsstuLaunchWsl(L"dmesg | grep -iF 'vmbus_send_tl_connect_request'"), 0L);
}
}; // namespace UnitTests
} // namespace UnitTests