[Lldb-commits] [lldb] [lldb][FreeBSDKernel] Find .debug files using pseudo sysroot (PR #224845)

Minsoo Choo via lldb-commits lldb-commits at lists.llvm.org
Sat Sep 19 11:37:41 PDT 2026


https://github.com/mchoo7 updated https://github.com/llvm/llvm-project/pull/224845

>From 58f4ced1d7db68584105b69ce697a5b3a89a6d63 Mon Sep 17 00:00:00 2001
From: Minsoo Choo <minsoo at minsoo.io>
Date: Sat, 19 Sep 2026 12:43:37 -0400
Subject: [PATCH] [lldb][FreeBSDKernel] Find separate debug files in extracted
 roots

Discover the base kernel debug file before libkvm and the dynamic loader need
its symbols. Infer an extracted filesystem root from the local kernel path
and honor its .gnu_debuglink name under usr/lib/debug.

Continue to derive module debug paths from linker_files, while allowing an
ET_DYN object that initially names itself as its symbol file.
---
 .../DynamicLoaderFreeBSDKernel.cpp            | 41 ++++++++++++
 .../FreeBSD-Kernel-Core/CMakeLists.txt        |  2 +
 .../ProcessFreeBSDKernelCore.cpp              | 66 +++++++++++++++++++
 3 files changed, 109 insertions(+)

diff --git a/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp b/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
index ffda99cfcc7362..62f1290418ffa2 100644
--- a/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
+++ b/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
@@ -13,6 +13,7 @@
 #include "lldb/Core/ModuleSpec.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/Section.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/StreamFile.h"
 #include "lldb/Interpreter/OptionValueProperties.h"
 #include "lldb/Symbol/ObjectFile.h"
@@ -84,6 +85,43 @@ static bool is_kmod(Module *module) {
   return true;
 }
 
+static bool set_debug_file(const ModuleSP &module,
+                           llvm::StringRef target_path) {
+  if (!module)
+    return false;
+
+  FileSpec local_file = module->GetFileSpec();
+
+  // ET_DYN kernels can initially use the object itself as their symbol file.
+  // That does not mean a separate debug file has already been found.  Preserve
+  // an explicitly selected symbol file, but continue searching when the
+  // symbol-file path is the same as the object-file path.
+  FileSpec symbol_file = module->GetSymbolFileFileSpec();
+  if (symbol_file && symbol_file != local_file)
+    return false;
+
+  FileSpec target_file(target_path);
+  if (!target_file.IsAbsolute())
+    return false;
+
+  std::string local_path = local_file.GetPath();
+  std::string normalized_target_path = target_file.GetPath();
+  llvm::StringRef local_sysroot(local_path);
+  if (!local_sysroot.consume_back(normalized_target_path))
+    return false;
+
+  std::string debug_path = local_sysroot.str();
+  debug_path += "/usr/lib/debug";
+  debug_path += normalized_target_path;
+  debug_path += ".debug";
+  FileSpec debug_file(debug_path);
+  if (!FileSystem::Instance().Exists(debug_file))
+    return false;
+
+  module->SetSymbolFileFileSpec(debug_file);
+  return true;
+}
+
 static bool is_reloc(Module *module) {
   if (!module)
     return false;
@@ -379,6 +417,9 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
       }
     }
 
+    if (m_module_sp && !IsKernel())
+      set_debug_file(m_module_sp, GetPath());
+
     if (m_module_sp) {
       // If the file is not kernel or kmod, the target should be loaded once and
       // don't reload again
diff --git a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
index 3d0509953b6c66..b9487abe105e0e 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
@@ -25,7 +25,9 @@ if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
       Support
     LINK_LIBS
       lldbCore
+      lldbHost
       lldbTarget
+      lldbPluginObjectFileELF
       kvm
     )
 
diff --git a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
index 93cd0e1998204e..3b5892c4059db5 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -6,8 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <optional>
+
 #include "lldb/Core/Module.h"
+#include "lldb/Core/ModuleList.h"
 #include "lldb/Core/PluginManager.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Interpreter/CommandInterpreter.h"
 #include "lldb/Interpreter/CommandObjectMultiword.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
@@ -20,6 +24,7 @@
 #include "llvm/Support/Error.h"
 
 #include "Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
 #include "ProcessFreeBSDKernelCore.h"
 #include "ThreadFreeBSDKernelCore.h"
 
@@ -64,6 +69,59 @@ static PluginProperties &GetGlobalPluginProperties() {
   return g_settings;
 }
 
+static bool SetKernelDebugFile(const ModuleSP &module) {
+  if (!module)
+    return false;
+
+  FileSpec object_file = module->GetFileSpec();
+  FileSystem::Instance().Resolve(object_file);
+
+  FileSpec symbol_file = module->GetSymbolFileFileSpec();
+  if (symbol_file)
+    FileSystem::Instance().Resolve(symbol_file);
+  if (symbol_file && symbol_file != object_file)
+    return false;
+
+  auto *elf = llvm::dyn_cast_or_null<ObjectFileELF>(module->GetObjectFile());
+  if (!elf)
+    return false;
+
+  // GetDebugLink() returns information collected while parsing section
+  // headers, which might not have happened when a PT_NOTE supplied the UUID.
+  (void)elf->GetSectionList();
+  std::optional<FileSpec> debug_link = elf->GetDebugLink();
+  if (!debug_link || debug_link->GetFilename().empty())
+    return false;
+
+  // The kernel's linker_files list may require symbols from kernel.debug to
+  // locate.  Infer an extracted filesystem's root from the local kernel path
+  // and load its FreeBSD-installed debug file before the dynamic loader runs.
+  std::string object_path = object_file.GetPath();
+  FileSpec sysroot = object_file;
+  while (sysroot.RemoveLastPathComponent()) {
+    std::string sysroot_path = sysroot.GetPath();
+    llvm::StringRef relative_path(object_path);
+    if (!relative_path.consume_front(sysroot_path))
+      continue;
+    relative_path = relative_path.ltrim("/\\");
+
+    FileSpec relative_file(relative_path);
+    FileSpec debug_file = sysroot;
+    debug_file.AppendPathComponent("usr/lib/debug");
+    if (!relative_file.GetDirectory().empty())
+      debug_file.AppendPathComponent(
+          relative_file.GetDirectory().GetStringRef());
+    debug_file.AppendPathComponent(debug_link->GetFilename().GetStringRef());
+    if (!FileSystem::Instance().Exists(debug_file))
+      continue;
+
+    module->SetSymbolFileFileSpec(debug_file);
+    return true;
+  }
+
+  return false;
+}
+
 class CommandObjectProcessFreeBSDKernelCoreRefreshThreads
     : public CommandObjectParsed {
 public:
@@ -195,6 +253,8 @@ Status ProcessFreeBSDKernelCore::DoLoadCore() {
     return Status::FromErrorString(
         "ProcessFreeBSDKernelCore: no executable module set on target");
 
+  bool symbols_changed = SetKernelDebugFile(executable);
+
   char errbuf[_POSIX2_LINE_MAX];
   m_kvm = kvm_open2(executable->GetFileSpec().GetPath().c_str(),
                     GetCoreFile().GetPath().c_str(), O_RDWR, errbuf, nullptr);
@@ -210,6 +270,12 @@ Status ProcessFreeBSDKernelCore::DoLoadCore() {
 
   SetKernelDisplacement();
 
+  if (symbols_changed) {
+    ModuleList modules;
+    modules.Append(executable);
+    GetTarget().SymbolsDidLoad(modules);
+  }
+
   return Status();
 }
 



More information about the lldb-commits mailing list