[Lldb-commits] [lldb] [lldb][ElfCore] Improve main executable detection in core files (PR #157170)

via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 5 13:28:35 PDT 2025


https://github.com/GeorgeHuyubo created https://github.com/llvm/llvm-project/pull/157170

This change improves how LLDB's ProcessElfCore plugin identifies the main executable when loading ELF core files. Previously, the code would simply use the first entry in the NT_FILE section, which is not guaranteed to be the main executable, also the first entry might not have a valid UUID.

1. **Storing executable name**: Extract the executable name from the ELF NT_PRPSINFO note and store it in `m_executable_name`

2. **Preferential matching**: When selecting the main executable from NT_FILE entries, prefer entries whose path ends with the stored executable name

3. **UUID-based lookup**: Call `FindModuleUUID()` helper function to properly match modules by path and retrieve a valid UUID



>From 28869cdcde362c654b518fac7d9945d1d1ee823f Mon Sep 17 00:00:00 2001
From: George Hu <hyubo at meta.com>
Date: Fri, 5 Sep 2025 13:23:48 -0700
Subject: [PATCH] [lldb][ElfCore] Improve main executable detection in core
 files

---
 .../Plugins/Process/elf-core/ProcessElfCore.cpp  | 16 +++++++++++++---
 .../Plugins/Process/elf-core/ProcessElfCore.h    |  3 +++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
index 88eeddf178788..e88daebebfa7e 100644
--- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
+++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
@@ -257,12 +257,21 @@ Status ProcessElfCore::DoLoadCore() {
   // the main executable using data we found in the core file notes.
   lldb::ModuleSP exe_module_sp = GetTarget().GetExecutableModule();
   if (!exe_module_sp) {
-    // The first entry in the NT_FILE might be our executable
     if (!m_nt_file_entries.empty()) {
+      // The first entry in the NT_FILE might be our executable
+      llvm::StringRef executable_path = m_nt_file_entries[0].path;
+      // Prefer the NT_FILE entry matching m_executable_name as main executable.
+      for (const NT_FILE_Entry &file_entry : m_nt_file_entries)
+        if (llvm::StringRef(file_entry.path)
+                .ends_with("/" + m_executable_name)) {
+          executable_path = file_entry.path;
+          break;
+        }
+
       ModuleSpec exe_module_spec;
       exe_module_spec.GetArchitecture() = arch;
-      exe_module_spec.GetUUID() = m_nt_file_entries[0].uuid;
-      exe_module_spec.GetFileSpec().SetFile(m_nt_file_entries[0].path,
+      exe_module_spec.GetUUID() = FindModuleUUID(executable_path);
+      exe_module_spec.GetFileSpec().SetFile(executable_path,
                                             FileSpec::Style::native);
       if (exe_module_spec.GetFileSpec()) {
         exe_module_sp =
@@ -935,6 +944,7 @@ llvm::Error ProcessElfCore::parseLinuxNotes(llvm::ArrayRef<CoreNote> notes) {
         return status.ToError();
       thread_data.name.assign (prpsinfo.pr_fname, strnlen (prpsinfo.pr_fname, sizeof (prpsinfo.pr_fname)));
       SetID(prpsinfo.pr_pid);
+      m_executable_name = prpsinfo.pr_fname;
       break;
     }
     case ELF::NT_SIGINFO: {
diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
index a91c04a277f60..601c8c4ed1b92 100644
--- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
+++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
@@ -152,6 +152,9 @@ class ProcessElfCore : public lldb_private::PostMortemProcess {
   // NT_FILE entries found from the NOTE segment
   std::vector<NT_FILE_Entry> m_nt_file_entries;
 
+  // Executable name found from the ELF PRPSINFO
+  std::string m_executable_name;
+
   // Parse thread(s) data structures(prstatus, prpsinfo) from given NOTE segment
   llvm::Error ParseThreadContextsFromNoteSegment(
       const elf::ELFProgramHeader &segment_header,



More information about the lldb-commits mailing list