https://github.com/mchoo7 created 
https://github.com/llvm/llvm-project/pull/183969

Add offset check for pcb structs. This is only enabled when the host machine 
architecture matches the architecture of register context its building. Note 
that `<machine/foo.h>` is dependent on host machine not the target, so `#ifdef 
__arch__` cannot be used here. List of HOST_ARCH is available 
[here](https://man.freebsd.org/cgi/man.cgi?query=arch&apropos=0&sektion=0&manpath=FreeBSD+16.0-CURRENT&format=html).

Due to identifier conflict, I had to lower PCB_FP and PCB_LR.

>From d7ddb79757989c7d3c5b9cb098a1a5c9a66ede3f Mon Sep 17 00:00:00 2001
From: Minsoo Choo <[email protected]>
Date: Sat, 28 Feb 2026 18:39:07 -0500
Subject: [PATCH] [lldb][Process/FreeBSDKernelCore] Add assertion for pcb

Signed-off-by: Minsoo Choo <[email protected]>
---
 .../FreeBSD-Kernel-Core/CMakeLists.txt        |  8 +++++
 .../RegisterContextFreeBSDKernelCore_arm.cpp  | 32 +++++++++++++++++++
 ...RegisterContextFreeBSDKernelCore_arm64.cpp | 25 ++++++++++++---
 .../RegisterContextFreeBSDKernelCore_i386.cpp | 14 ++++++++
 ...gisterContextFreeBSDKernelCore_ppc64le.cpp | 14 ++++++++
 ...gisterContextFreeBSDKernelCore_riscv64.cpp | 13 ++++++++
 ...egisterContextFreeBSDKernelCore_x86_64.cpp | 16 ++++++++++
 7 files changed, 118 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
index 1937143171369..b5ea4772a8b92 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/CMakeLists.txt
@@ -1,3 +1,11 @@
+# Used for pcb assertion
+# List of architectures:
+# 
https://man.freebsd.org/cgi/man.cgi?query=arch&apropos=0&sektion=0&manpath=FreeBSD+16.0-CURRENT&format=html
+if(CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
+    set(HOST_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
+    add_definitions(-DFreeBSD_${HOST_ARCH})
+endif()
+
 lldb_tablegen(ProcessFreeBSDKernelCoreProperties.inc -gen-lldb-property-defs
   SOURCE ProcessFreeBSDKernelCoreProperties.td
   TARGET LLDBPluginProcessFreeBSDKernelCorePropertiesGen)
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm.cpp
index 0d01a0ec48d3e..9738fc42b0b5e 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm.cpp
@@ -14,6 +14,11 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_armv7
+#include <cstddef>
+#include <machine/frame.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -61,6 +66,33 @@ bool RegisterContextFreeBSDKernelCore_arm::ReadRegister(
     llvm::support::ulittle32_t pc;
   } pcb;
 
+#ifdef FreeBSD_armv7
+  static_assert(offsetof(struct switchframe, sf_r4) ==
+                offsetof(decltype(pcb), r4));
+  static_assert(offsetof(struct switchframe, sf_r5) ==
+                offsetof(decltype(pcb), r5));
+  static_assert(offsetof(struct switchframe, sf_r6) ==
+                offsetof(decltype(pcb), r6));
+  static_assert(offsetof(struct switchframe, sf_r7) ==
+                offsetof(decltype(pcb), r7));
+  static_assert(offsetof(struct switchframe, sf_r8) ==
+                offsetof(decltype(pcb), r8));
+  static_assert(offsetof(struct switchframe, sf_r9) ==
+                offsetof(decltype(pcb), r9));
+  static_assert(offsetof(struct switchframe, sf_r10) ==
+                offsetof(decltype(pcb), r10));
+  static_assert(offsetof(struct switchframe, sf_r11) ==
+                offsetof(decltype(pcb), r11));
+  static_assert(offsetof(struct switchframe, sf_r12) ==
+                offsetof(decltype(pcb), r12));
+  static_assert(offsetof(struct switchframe, sf_sp) ==
+                offsetof(decltype(pcb), sp));
+  static_assert(offsetof(struct switchframe, sf_lr) ==
+                offsetof(decltype(pcb), lr));
+  static_assert(offsetof(struct switchframe, sf_pc) ==
+                offsetof(decltype(pcb), pc));
+#endif
+
   Status error;
   size_t rd =
       m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), error);
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm64.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm64.cpp
index afd60a64a6365..1931d7bfdbb93 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm64.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_arm64.cpp
@@ -18,6 +18,12 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_aarch64
+#include <cstddef>
+#include <machine/pcb.h>
+#include <sys/param.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -52,6 +58,11 @@ bool RegisterContextFreeBSDKernelCore_arm64::ReadRegister(
     llvm::support::ulittle64_t sp;
   } pcb;
 
+#if defined(FreeBSD_aarch64) && __FreeBSD_version >= 1400084
+  static_assert(offsetof(struct pcb, pcb_x) == offsetof(decltype(pcb), x));
+  static_assert(offsetof(struct pcb, pcb_sp) == offsetof(decltype(pcb), sp));
+#endif
+
   // https://cgit.freebsd.org/src/tree/sys/arm64/include/pcb.h?h=stable%2F13
   struct {
     llvm::support::ulittle64_t x[30];
@@ -60,6 +71,12 @@ bool RegisterContextFreeBSDKernelCore_arm64::ReadRegister(
     llvm::support::ulittle64_t sp;
   } pcb13;
 
+#if defined(FreeBSD_aarch64) && __FreeBSD_version < 1400084
+  static_assert(offsetof(struct pcb, pcb_x) == offsetof(decltype(pcb13), x));
+  static_assert(offsetof(struct pcb, pcb_lr) == offsetof(decltype(pcb13), lr));
+  static_assert(offsetof(struct pcb, pcb_sp) == offsetof(decltype(pcb13), sp));
+#endif
+
   Status error;
   constexpr int FBSD14 = 1400084;
   int osreldate = FBSD14;
@@ -72,8 +89,8 @@ bool RegisterContextFreeBSDKernelCore_arm64::ReadRegister(
 
   // TODO: LLVM 24: Remove FreeBSD 13 support
   if (osreldate >= FBSD14) {
-    constexpr uint32_t PCB_FP = 10;
-    constexpr uint32_t PCB_LR = 11;
+    constexpr uint32_t pcb_fp = 10;
+    constexpr uint32_t pcb_lr = 11;
     size_t rd =
         m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), 
error);
     if (rd != sizeof(pcb))
@@ -92,14 +109,14 @@ bool RegisterContextFreeBSDKernelCore_arm64::ReadRegister(
     case gpr_x27_arm64:
     case gpr_x28_arm64:
     case gpr_fp_arm64:
-      static_assert(gpr_fp_arm64 - gpr_x19_arm64 == PCB_FP,
+      static_assert(gpr_fp_arm64 - gpr_x19_arm64 == pcb_fp,
                     "nonconsecutive arm64 register numbers");
       value = pcb.x[reg - gpr_x19_arm64];
       break;
     case gpr_lr_arm64:
     case gpr_pc_arm64:
       // The pc of crashing thread is stored in lr.
-      static_assert(gpr_lr_arm64 - gpr_x19_arm64 == PCB_LR,
+      static_assert(gpr_lr_arm64 - gpr_x19_arm64 == pcb_lr,
                     "nonconsecutive arm64 register numbers");
       value = pcb.x[reg - gpr_x19_arm64];
       break;
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_i386.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_i386.cpp
index 83ab12c73b918..b2b65f2380ec2 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_i386.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_i386.cpp
@@ -13,6 +13,11 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_i386
+#include <cstddef>
+#include <machine/pcb.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -50,6 +55,15 @@ bool RegisterContextFreeBSDKernelCore_i386::ReadRegister(
     llvm::support::ulittle32_t eip;
   } pcb;
 
+#ifdef FreeBSD_i386
+  static_assert(offsetof(struct pcb, pcb_edi) == offsetof(decltype(pcb), edi));
+  static_assert(offsetof(struct pcb, pcb_esi) == offsetof(decltype(pcb), esi));
+  static_assert(offsetof(struct pcb, pcb_ebp) == offsetof(decltype(pcb), ebp));
+  static_assert(offsetof(struct pcb, pcb_esp) == offsetof(decltype(pcb), esp));
+  static_assert(offsetof(struct pcb, pcb_ebx) == offsetof(decltype(pcb), ebx));
+  static_assert(offsetof(struct pcb, pcb_eip) == offsetof(decltype(pcb), eip));
+#endif
+
   Status error;
   size_t rd =
       m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), error);
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_ppc64le.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_ppc64le.cpp
index 69932eaeb4e0b..5cc4d6cf4994e 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_ppc64le.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_ppc64le.cpp
@@ -13,6 +13,11 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_powerpc64le
+#include <cstddef>
+#include <machine/pcb.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -37,6 +42,15 @@ bool RegisterContextFreeBSDKernelCore_ppc64le::ReadRegister(
     llvm::support::ulittle64_t lr;
   } pcb;
 
+#ifdef FreeBSD_powerpc64le
+  static_assert(offsetof(struct pcb, pcb_context) ==
+                offsetof(decltype(pcb), context));
+  static_assert(offsetof(struct pcb, pcb_cr) == offsetof(decltype(pcb), cr));
+  static_assert(offsetof(struct pcb, pcb_sp) == offsetof(decltype(pcb), sp));
+  static_assert(offsetof(struct pcb, pcb_toc) == offsetof(decltype(pcb), toc));
+  static_assert(offsetof(struct pcb, pcb_lr) == offsetof(decltype(pcb), lr));
+#endif
+
   Status error;
   size_t rd =
       m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), error);
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_riscv64.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_riscv64.cpp
index b705c6d6d43bd..00957241b9996 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_riscv64.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_riscv64.cpp
@@ -13,6 +13,11 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_riscv64
+#include <cstddef>
+#include <machine/pcb.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -52,6 +57,14 @@ bool RegisterContextFreeBSDKernelCore_riscv64::ReadRegister(
     llvm::support::ulittle64_t s[12];
   } pcb;
 
+#ifdef FreeBSD_riscv64
+  static_assert(offsetof(struct pcb, pcb_ra) == offsetof(decltype(pcb), ra));
+  static_assert(offsetof(struct pcb, pcb_sp) == offsetof(decltype(pcb), sp));
+  static_assert(offsetof(struct pcb, pcb_gp) == offsetof(decltype(pcb), gp));
+  static_assert(offsetof(struct pcb, pcb_tp) == offsetof(decltype(pcb), tp));
+  static_assert(offsetof(struct pcb, pcb_s) == offsetof(decltype(pcb), s));
+#endif
+
   Status error;
   size_t rd =
       m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), error);
diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_x86_64.cpp
 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_x86_64.cpp
index a4fe219a2e72d..b72f5f3b08e40 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_x86_64.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/RegisterContextFreeBSDKernelCore_x86_64.cpp
@@ -13,6 +13,11 @@
 #include "lldb/Utility/RegisterValue.h"
 #include "llvm/Support/Endian.h"
 
+#ifdef FreeBSD_amd64
+#include <cstddef>
+#include <machine/pcb.h>
+#endif
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -54,6 +59,17 @@ bool RegisterContextFreeBSDKernelCore_x86_64::ReadRegister(
     llvm::support::ulittle64_t rip;
   } pcb;
 
+#ifdef FreeBSD_amd64
+  static_assert(offsetof(struct pcb, pcb_r15) == offsetof(decltype(pcb), r15));
+  static_assert(offsetof(struct pcb, pcb_r14) == offsetof(decltype(pcb), r14));
+  static_assert(offsetof(struct pcb, pcb_r13) == offsetof(decltype(pcb), r13));
+  static_assert(offsetof(struct pcb, pcb_r12) == offsetof(decltype(pcb), r12));
+  static_assert(offsetof(struct pcb, pcb_rbp) == offsetof(decltype(pcb), rbp));
+  static_assert(offsetof(struct pcb, pcb_rsp) == offsetof(decltype(pcb), rsp));
+  static_assert(offsetof(struct pcb, pcb_rbx) == offsetof(decltype(pcb), rbx));
+  static_assert(offsetof(struct pcb, pcb_rip) == offsetof(decltype(pcb), rip));
+#endif
+
   Status error;
   size_t rd =
       m_thread.GetProcess()->ReadMemory(m_pcb_addr, &pcb, sizeof(pcb), error);

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to