Index | Thread | Search

From:
Yuichiro NAITO <naito.yuichiro@gmail.com>
Subject:
lldb: fails to read a process core file (regression)
To:
tech@openbsd.org
Date:
Wed, 31 Jul 2024 16:38:25 +0900

Download raw body.

Thread
I received a mail personally that reports lldb fails to read a process core
file. It's a regression of my kernel core file support patch. My apologies.

The ProcessOpenBSDKernel class opens a core file via kvm_open(3).
The kvm_open(3) succeeds in opening a core file even if it is a process core.
So the OpenBSDKernel plugin is used for a process core file but can't analyze
it.

If a process core file is passed to the ProcessOpenBSDKernel class, its
instantiation should fail. A process core file is an ELF binary. I add
the check logic to see whether an ELF binary or not and make it fail
instantiation in case of an ELF binary.

OK?

diff --git a/gnu/llvm/lldb/source/Plugins/Process/OpenBSDKernel/ProcessOpenBSDKernel.cpp b/gnu/llvm/lldb/source/Plugins/Process/OpenBSDKernel/ProcessOpenBSDKernel.cpp
index 300a35d4051..8016399586f 100644
--- a/gnu/llvm/lldb/source/Plugins/Process/OpenBSDKernel/ProcessOpenBSDKernel.cpp
+++ b/gnu/llvm/lldb/source/Plugins/Process/OpenBSDKernel/ProcessOpenBSDKernel.cpp
@@ -19,6 +19,7 @@
 #define _KERNEL
 #include <machine/cpu.h>
 #include <sys/proc.h>
+#include <sys/exec_elf.h>
 #undef _KERNEL
 #endif
 
@@ -60,6 +61,14 @@ lldb::ProcessSP ProcessOpenBSDKernel::CreateInstance(lldb::TargetSP target_sp,
   ModuleSP executable = target_sp->GetExecutableModule();
   if (crash_file && !can_connect && executable) {
 #if defined(__OpenBSD__)
+    char buf[4];
+    FILE *fp = fopen(crash_file->GetPath().c_str(), "r");
+    if (fp == NULL)
+      return nullptr;
+    size_t r = fread(buf, 1, sizeof(buf), fp);
+    fclose(fp);
+    if (r != sizeof(buf) || memcmp(buf, ELFMAG, sizeof(buf)) == 0)
+      return nullptr;
     kvm_t *kvm =
         kvm_open(executable->GetFileSpec().GetPath().c_str(),
 		 crash_file->GetPath().c_str(), nullptr, O_RDONLY, nullptr);

-- 
Yuichiro NAITO (naito.yuichiro@gmail.com)