[Lldb-commits] [lldb] 3053e14 - [lldb] [Platform] Support synthesizing siginfo_t

Michał Górny via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 27 04:33:58 PST 2022


Author: Michał Górny
Date: 2022-01-27T13:33:47+01:00
New Revision: 3053e143bef273a2e65519cea15be8f7ce6cd854

URL: https://github.com/llvm/llvm-project/commit/3053e143bef273a2e65519cea15be8f7ce6cd854
DIFF: https://github.com/llvm/llvm-project/commit/3053e143bef273a2e65519cea15be8f7ce6cd854.diff

LOG: [lldb] [Platform] Support synthesizing siginfo_t

Support synthesizing the siginfo_t type from the Platform plugin.
This type is going to be used by LLDB client to process the raw siginfo
data received from lldb-server without the necessity of relying
on target's debug info being present.

Differential Revision: https://reviews.llvm.org/D117707

Added: 
    lldb/unittests/Platform/PlatformSiginfoTest.cpp
    lldb/unittests/Platform/tools/generate_siginfo.c

Modified: 
    lldb/include/lldb/Target/Platform.h
    lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
    lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h
    lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
    lldb/source/Plugins/Platform/Linux/PlatformLinux.h
    lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
    lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
    lldb/source/Target/Platform.cpp
    lldb/unittests/Platform/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h
index d50badb1e687..4ef6c9d82b1a 100644
--- a/lldb/include/lldb/Target/Platform.h
+++ b/lldb/include/lldb/Target/Platform.h
@@ -863,6 +863,8 @@ class Platform : public PluginInterface {
     return nullptr;
   }
 
+  virtual CompilerType GetSiginfoType(const llvm::Triple &triple);
+
 protected:
   /// Create a list of ArchSpecs with the given OS and a architectures. The
   /// vendor field is left as an "unspecified unknown".

diff  --git a/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp b/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
index 754d06de7cb9..2bf0a44e4a3e 100644
--- a/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
+++ b/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
@@ -183,3 +183,97 @@ MmapArgList PlatformFreeBSD::GetMmapArgumentList(const ArchSpec &arch,
     args.push_back(0);
   return args;
 }
+
+CompilerType PlatformFreeBSD::GetSiginfoType(const llvm::Triple &triple) {
+  if (!m_type_system_up)
+    m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
+  TypeSystemClang *ast = m_type_system_up.get();
+
+  // generic types
+  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
+  CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
+  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
+  CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
+
+  // platform-specific types
+  CompilerType &pid_type = int_type;
+  CompilerType &uid_type = uint_type;
+
+  CompilerType sigval_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(sigval_type);
+  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
+                            lldb::eAccessPublic, 0);
+  ast->CompleteTagDeclarationDefinition(sigval_type);
+
+  // siginfo_t
+  CompilerType siginfo_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
+      clang::TTK_Struct, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(siginfo_type);
+  ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_pid", pid_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_uid", uid_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_status", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_addr", voidp_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(siginfo_type, "si_value", sigval_type,
+                            lldb::eAccessPublic, 0);
+
+  // union used to hold the signal data
+  CompilerType union_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(union_type);
+
+  ast->AddFieldToRecordType(
+      union_type, "_fault",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_trapno", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_timer",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_timerid", int_type},
+                                         {"_overrun", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_mesgq",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_mqd", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_poll",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_band", long_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(union_type);
+  ast->AddFieldToRecordType(siginfo_type, "_reason", union_type,
+                            lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(siginfo_type);
+  return siginfo_type;
+}

diff  --git a/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h b/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h
index fd37b13de017..7f9dfd87a59a 100644
--- a/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h
+++ b/lldb/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.h
@@ -10,6 +10,7 @@
 #define LLDB_SOURCE_PLUGINS_PLATFORM_FREEBSD_PLATFORMFREEBSD_H
 
 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
 
 namespace lldb_private {
 namespace platform_freebsd {
@@ -53,7 +54,12 @@ class PlatformFreeBSD : public PlatformPOSIX {
                                   unsigned flags, lldb::addr_t fd,
                                   lldb::addr_t offset) override;
 
+  CompilerType GetSiginfoType(const llvm::Triple &triple) override;
+
   std::vector<ArchSpec> m_supported_architectures;
+
+private:
+  std::unique_ptr<TypeSystemClang> m_type_system_up;
 };
 
 } // namespace platform_freebsd

diff  --git a/lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp b/lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
index 6278d51289d7..92f44b7c6b50 100644
--- a/lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
+++ b/lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
@@ -309,3 +309,169 @@ MmapArgList PlatformLinux::GetMmapArgumentList(const ArchSpec &arch,
   return args;
 }
 
+CompilerType PlatformLinux::GetSiginfoType(const llvm::Triple &triple) {
+  if (!m_type_system_up)
+    m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
+  TypeSystemClang *ast = m_type_system_up.get();
+
+  bool si_errno_then_code = true;
+
+  switch (triple.getArch()) {
+  case llvm::Triple::mips:
+  case llvm::Triple::mipsel:
+  case llvm::Triple::mips64:
+  case llvm::Triple::mips64el:
+    // mips has si_code and si_errno swapped
+    si_errno_then_code = false;
+    break;
+  default:
+    break;
+  }
+
+  // generic types
+  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
+  CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
+  CompilerType short_type = ast->GetBasicType(eBasicTypeShort);
+  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
+  CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
+
+  // platform-specific types
+  CompilerType &pid_type = int_type;
+  CompilerType &uid_type = uint_type;
+  CompilerType &clock_type = long_type;
+  CompilerType &band_type = long_type;
+
+  CompilerType sigval_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(sigval_type);
+  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
+                            lldb::eAccessPublic, 0);
+  ast->CompleteTagDeclarationDefinition(sigval_type);
+
+  CompilerType sigfault_bounds_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(sigfault_bounds_type);
+  ast->AddFieldToRecordType(sigfault_bounds_type, "_addr_bnd",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_lower", voidp_type},
+                                         {"_upper", voidp_type},
+                                     }),
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(sigfault_bounds_type, "_pkey", uint_type,
+                            lldb::eAccessPublic, 0);
+  ast->CompleteTagDeclarationDefinition(sigfault_bounds_type);
+
+  // siginfo_t
+  CompilerType siginfo_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
+      clang::TTK_Struct, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(siginfo_type);
+  ast->AddFieldToRecordType(siginfo_type, "si_signo", int_type,
+                            lldb::eAccessPublic, 0);
+
+  if (si_errno_then_code) {
+    ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
+                              lldb::eAccessPublic, 0);
+    ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
+                              lldb::eAccessPublic, 0);
+  } else {
+    ast->AddFieldToRecordType(siginfo_type, "si_code", int_type,
+                              lldb::eAccessPublic, 0);
+    ast->AddFieldToRecordType(siginfo_type, "si_errno", int_type,
+                              lldb::eAccessPublic, 0);
+  }
+
+  // the structure is padded on 64-bit arches to fix alignment
+  if (triple.isArch64Bit())
+    ast->AddFieldToRecordType(siginfo_type, "__pad0", int_type,
+                              lldb::eAccessPublic, 0);
+
+  // union used to hold the signal data
+  CompilerType union_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(union_type);
+
+  ast->AddFieldToRecordType(
+      union_type, "_kill",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_pid", pid_type},
+                                         {"si_uid", uid_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_timer",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_tid", int_type},
+                                         {"si_overrun", int_type},
+                                         {"si_sigval", sigval_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_rt",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_pid", pid_type},
+                                         {"si_uid", uid_type},
+                                         {"si_sigval", sigval_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_sigchld",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_pid", pid_type},
+                                         {"si_uid", uid_type},
+                                         {"si_status", int_type},
+                                         {"si_utime", clock_type},
+                                         {"si_stime", clock_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_sigfault",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_addr", voidp_type},
+                                         {"si_addr_lsb", short_type},
+                                         {"_bounds", sigfault_bounds_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_sigpoll",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"si_band", band_type},
+                                         {"si_fd", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  // NB: SIGSYS is not present on ia64 but we don't seem to support that
+  ast->AddFieldToRecordType(
+      union_type, "_sigsys",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_call_addr", voidp_type},
+                                         {"_syscall", int_type},
+                                         {"_arch", uint_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(union_type);
+  ast->AddFieldToRecordType(siginfo_type, "_sifields", union_type,
+                            lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(siginfo_type);
+  return siginfo_type;
+}

diff  --git a/lldb/source/Plugins/Platform/Linux/PlatformLinux.h b/lldb/source/Plugins/Platform/Linux/PlatformLinux.h
index 13224901cd92..87d0926e3a8e 100644
--- a/lldb/source/Plugins/Platform/Linux/PlatformLinux.h
+++ b/lldb/source/Plugins/Platform/Linux/PlatformLinux.h
@@ -10,6 +10,7 @@
 #define LLDB_SOURCE_PLUGINS_PLATFORM_LINUX_PLATFORMLINUX_H
 
 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
 
 namespace lldb_private {
 namespace platform_linux {
@@ -58,7 +59,12 @@ class PlatformLinux : public PlatformPOSIX {
                                   unsigned flags, lldb::addr_t fd,
                                   lldb::addr_t offset) override;
 
+  CompilerType GetSiginfoType(const llvm::Triple &triple) override;
+
   std::vector<ArchSpec> m_supported_architectures;
+
+private:
+  std::unique_ptr<TypeSystemClang> m_type_system_up;
 };
 
 } // namespace platform_linux

diff  --git a/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp b/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
index 552b3890615c..ddba64bc5d11 100644
--- a/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
+++ b/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
@@ -202,3 +202,144 @@ MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch,
   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
   return args;
 }
+
+CompilerType PlatformNetBSD::GetSiginfoType(const llvm::Triple &triple) {
+  if (!m_type_system_up)
+    m_type_system_up.reset(new TypeSystemClang("siginfo", triple));
+  TypeSystemClang *ast = m_type_system_up.get();
+
+  // generic types
+  CompilerType int_type = ast->GetBasicType(eBasicTypeInt);
+  CompilerType uint_type = ast->GetBasicType(eBasicTypeUnsignedInt);
+  CompilerType long_type = ast->GetBasicType(eBasicTypeLong);
+  CompilerType long_long_type = ast->GetBasicType(eBasicTypeLongLong);
+  CompilerType voidp_type = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
+
+  // platform-specific types
+  CompilerType &pid_type = int_type;
+  CompilerType &uid_type = uint_type;
+  CompilerType &clock_type = uint_type;
+  CompilerType &lwpid_type = int_type;
+
+  CompilerType sigval_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_sigval_t",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(sigval_type);
+  ast->AddFieldToRecordType(sigval_type, "sival_int", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(sigval_type, "sival_ptr", voidp_type,
+                            lldb::eAccessPublic, 0);
+  ast->CompleteTagDeclarationDefinition(sigval_type);
+
+  CompilerType ptrace_option_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(ptrace_option_type);
+  ast->AddFieldToRecordType(ptrace_option_type, "_pe_other_pid", pid_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(ptrace_option_type, "_pe_lwp", lwpid_type,
+                            lldb::eAccessPublic, 0);
+  ast->CompleteTagDeclarationDefinition(ptrace_option_type);
+
+  // siginfo_t
+  CompilerType siginfo_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "__lldb_siginfo_t",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(siginfo_type);
+
+  // struct _ksiginfo
+  CompilerType ksiginfo_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Struct, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(ksiginfo_type);
+  ast->AddFieldToRecordType(ksiginfo_type, "_signo", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(ksiginfo_type, "_code", int_type,
+                            lldb::eAccessPublic, 0);
+  ast->AddFieldToRecordType(ksiginfo_type, "_errno", int_type,
+                            lldb::eAccessPublic, 0);
+
+  // the structure is padded on 64-bit arches to fix alignment
+  if (triple.isArch64Bit())
+    ast->AddFieldToRecordType(ksiginfo_type, "__pad0", int_type,
+                              lldb::eAccessPublic, 0);
+
+  // union used to hold the signal data
+  CompilerType union_type = ast->CreateRecordType(
+      nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "",
+      clang::TTK_Union, lldb::eLanguageTypeC);
+  ast->StartTagDeclarationDefinition(union_type);
+
+  ast->AddFieldToRecordType(
+      union_type, "_rt",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_pid", pid_type},
+                                         {"_uid", uid_type},
+                                         {"_value", sigval_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_child",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_pid", pid_type},
+                                         {"_uid", uid_type},
+                                         {"_status", int_type},
+                                         {"_utime", clock_type},
+                                         {"_stime", clock_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_fault",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_addr", voidp_type},
+                                         {"_trap", int_type},
+                                         {"_trap2", int_type},
+                                         {"_trap3", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_poll",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_band", long_type},
+                                         {"_fd", int_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(union_type, "_syscall",
+                            ast->CreateStructForIdentifier(
+                                ConstString(),
+                                {
+                                    {"_sysnum", int_type},
+                                    {"_retval", int_type.GetArrayType(2)},
+                                    {"_error", int_type},
+                                    {"_args", long_long_type.GetArrayType(8)},
+                                }),
+                            lldb::eAccessPublic, 0);
+
+  ast->AddFieldToRecordType(
+      union_type, "_ptrace_state",
+      ast->CreateStructForIdentifier(ConstString(),
+                                     {
+                                         {"_pe_report_event", int_type},
+                                         {"_option", ptrace_option_type},
+                                     }),
+      lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(union_type);
+  ast->AddFieldToRecordType(ksiginfo_type, "_reason", union_type,
+                            lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(ksiginfo_type);
+  ast->AddFieldToRecordType(siginfo_type, "_info", ksiginfo_type,
+                            lldb::eAccessPublic, 0);
+
+  ast->CompleteTagDeclarationDefinition(siginfo_type);
+  return siginfo_type;
+}

diff  --git a/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h b/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
index 7158fbd26efb..433cf6653126 100644
--- a/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
+++ b/lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
@@ -10,6 +10,7 @@
 #define LLDB_SOURCE_PLUGINS_PLATFORM_NETBSD_PLATFORMNETBSD_H
 
 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
 
 namespace lldb_private {
 namespace platform_netbsd {
@@ -55,7 +56,12 @@ class PlatformNetBSD : public PlatformPOSIX {
                                   unsigned flags, lldb::addr_t fd,
                                   lldb::addr_t offset) override;
 
+  CompilerType GetSiginfoType(const llvm::Triple &triple) override;
+
   std::vector<ArchSpec> m_supported_architectures;
+
+private:
+  std::unique_ptr<TypeSystemClang> m_type_system_up;
 };
 
 } // namespace platform_netbsd

diff  --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index 00e1c1603227..3c331c8760df 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -2003,3 +2003,7 @@ size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
 
   return 0;
 }
+
+CompilerType Platform::GetSiginfoType(const llvm::Triple& triple) {
+  return CompilerType();
+}

diff  --git a/lldb/unittests/Platform/CMakeLists.txt b/lldb/unittests/Platform/CMakeLists.txt
index ca5031b9b43e..f63ac3b3a280 100644
--- a/lldb/unittests/Platform/CMakeLists.txt
+++ b/lldb/unittests/Platform/CMakeLists.txt
@@ -1,9 +1,13 @@
 add_lldb_unittest(LLDBPlatformTests
   PlatformAppleSimulatorTest.cpp
   PlatformDarwinTest.cpp
+  PlatformSiginfoTest.cpp
 
   LINK_LIBS
+    lldbPluginPlatformFreeBSD
+    lldbPluginPlatformLinux
     lldbPluginPlatformMacOSX
+    lldbPluginPlatformNetBSD
   LINK_COMPONENTS
     Support
   )

diff  --git a/lldb/unittests/Platform/PlatformSiginfoTest.cpp b/lldb/unittests/Platform/PlatformSiginfoTest.cpp
new file mode 100644
index 000000000000..e8a5a88f34c4
--- /dev/null
+++ b/lldb/unittests/Platform/PlatformSiginfoTest.cpp
@@ -0,0 +1,311 @@
+//===-- PlatformSiginfoTest.cpp -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include <initializer_list>
+#include <tuple>
+
+#include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
+#include "Plugins/Platform/Linux/PlatformLinux.h"
+#include "Plugins/Platform/NetBSD/PlatformNetBSD.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
+
+#include "TestingSupport/SubsystemRAII.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Utility/ArchSpec.h"
+#include "lldb/Utility/Reproducer.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::repro;
+
+namespace {
+class PlatformSiginfoTest : public ::testing::Test {
+  SubsystemRAII<FileSystem, HostInfo, TypeSystemClang> subsystems;
+  PlatformSP platform_sp;
+  DebuggerSP debugger_sp;
+  TargetSP target_sp;
+
+public:
+  CompilerType siginfo_type;
+
+  void SetUp() override {
+    llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None));
+    platform_freebsd::PlatformFreeBSD::Initialize();
+    platform_linux::PlatformLinux::Initialize();
+    platform_netbsd::PlatformNetBSD::Initialize();
+  }
+
+  void TearDown() override {
+    platform_netbsd::PlatformNetBSD::Terminate();
+    platform_linux::PlatformLinux::Terminate();
+    platform_freebsd::PlatformFreeBSD::Terminate();
+    Reproducer::Terminate();
+  }
+
+  typedef std::tuple<const char *, uint64_t, uint64_t> field_tuple;
+
+  void ExpectField(const CompilerType &siginfo_type, field_tuple field) {
+    const char *path;
+    uint64_t offset, size;
+    std::tie(path, offset, size) = field;
+
+    SCOPED_TRACE(path);
+    CompilerType field_type = siginfo_type;
+    uint64_t total_offset = 0;
+    for (auto field_name : llvm::split(path, '.')) {
+      uint64_t bit_offset;
+      ASSERT_NE(field_type.GetIndexOfFieldWithName(field_name.str().c_str(),
+                                                   &field_type, &bit_offset),
+                UINT32_MAX);
+      total_offset += bit_offset;
+    }
+
+    EXPECT_EQ(total_offset, offset * 8);
+    EXPECT_EQ(field_type.GetByteSize(nullptr), llvm::Optional<uint64_t>(size));
+  }
+
+  void ExpectFields(const CompilerType &container,
+                    std::initializer_list<field_tuple> fields) {
+    for (auto x : fields)
+      ExpectField(container, x);
+  }
+
+  void InitializeSiginfo(const std::string &triple) {
+    ArchSpec arch(triple);
+
+    switch (arch.GetTriple().getOS()) {
+    case llvm::Triple::FreeBSD:
+      platform_sp =
+          platform_freebsd::PlatformFreeBSD::CreateInstance(true, &arch);
+      break;
+    case llvm::Triple::Linux:
+      platform_sp = platform_linux::PlatformLinux::CreateInstance(true, &arch);
+      break;
+    case llvm::Triple::NetBSD:
+      platform_sp =
+          platform_netbsd::PlatformNetBSD::CreateInstance(true, &arch);
+      break;
+    default:
+      llvm_unreachable("unknown ostype in triple");
+    }
+    Platform::SetHostPlatform(platform_sp);
+
+    debugger_sp = Debugger::CreateInstance();
+    ASSERT_TRUE(debugger_sp);
+
+    debugger_sp->GetTargetList().CreateTarget(
+        *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
+    ASSERT_TRUE(target_sp);
+
+    siginfo_type = platform_sp->GetSiginfoType(arch.GetTriple());
+  }
+};
+
+} // namespace
+
+TEST_F(PlatformSiginfoTest, TestLinux_64bit) {
+  for (std::string arch : {"x86_64", "aarch64", "powerpc64le"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo(arch + "-pc-linux-gnu");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(siginfo_type,
+                 {
+                     {"si_signo", 0, 4},
+                     {"si_errno", 4, 4},
+                     {"si_code", 8, 4},
+                     {"_sifields._kill.si_pid", 16, 4},
+                     {"_sifields._kill.si_uid", 20, 4},
+                     {"_sifields._timer.si_tid", 16, 4},
+                     {"_sifields._timer.si_overrun", 20, 4},
+                     {"_sifields._timer.si_sigval", 24, 8},
+                     {"_sifields._rt.si_pid", 16, 4},
+                     {"_sifields._rt.si_uid", 20, 4},
+                     {"_sifields._rt.si_sigval", 24, 8},
+                     {"_sifields._sigchld.si_pid", 16, 4},
+                     {"_sifields._sigchld.si_uid", 20, 4},
+                     {"_sifields._sigchld.si_status", 24, 4},
+                     {"_sifields._sigchld.si_utime", 32, 8},
+                     {"_sifields._sigchld.si_stime", 40, 8},
+                     {"_sifields._sigfault.si_addr", 16, 8},
+                     {"_sifields._sigfault.si_addr_lsb", 24, 2},
+                     {"_sifields._sigfault._bounds._addr_bnd._lower", 32, 8},
+                     {"_sifields._sigfault._bounds._addr_bnd._upper", 40, 8},
+                     {"_sifields._sigfault._bounds._pkey", 32, 4},
+                     {"_sifields._sigpoll.si_band", 16, 8},
+                     {"_sifields._sigpoll.si_fd", 24, 4},
+                     {"_sifields._sigsys._call_addr", 16, 8},
+                     {"_sifields._sigsys._syscall", 24, 4},
+                     {"_sifields._sigsys._arch", 28, 4},
+                 });
+  }
+}
+
+TEST_F(PlatformSiginfoTest, TestLinux_32bit) {
+  for (std::string arch : {"i386", "armv7"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo(arch + "-pc-linux");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(siginfo_type,
+                 {
+                     {"si_signo", 0, 4},
+                     {"si_errno", 4, 4},
+                     {"si_code", 8, 4},
+                     {"_sifields._kill.si_pid", 12, 4},
+                     {"_sifields._kill.si_uid", 16, 4},
+                     {"_sifields._timer.si_tid", 12, 4},
+                     {"_sifields._timer.si_overrun", 16, 4},
+                     {"_sifields._timer.si_sigval", 20, 4},
+                     {"_sifields._rt.si_pid", 12, 4},
+                     {"_sifields._rt.si_uid", 16, 4},
+                     {"_sifields._rt.si_sigval", 20, 4},
+                     {"_sifields._sigchld.si_pid", 12, 4},
+                     {"_sifields._sigchld.si_uid", 16, 4},
+                     {"_sifields._sigchld.si_status", 20, 4},
+                     {"_sifields._sigchld.si_utime", 24, 4},
+                     {"_sifields._sigchld.si_stime", 28, 4},
+                     {"_sifields._sigfault.si_addr", 12, 4},
+                     {"_sifields._sigfault.si_addr_lsb", 16, 2},
+                     {"_sifields._sigfault._bounds._addr_bnd._lower", 20, 4},
+                     {"_sifields._sigfault._bounds._addr_bnd._upper", 24, 4},
+                     {"_sifields._sigfault._bounds._pkey", 20, 4},
+                     {"_sifields._sigpoll.si_band", 12, 4},
+                     {"_sifields._sigpoll.si_fd", 16, 4},
+                     {"_sifields._sigsys._call_addr", 12, 4},
+                     {"_sifields._sigsys._syscall", 16, 4},
+                     {"_sifields._sigsys._arch", 20, 4},
+                 });
+  }
+}
+
+TEST_F(PlatformSiginfoTest, TestFreeBSD_64bit) {
+  for (std::string arch : {"x86_64", "aarch64"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo("x86_64-unknown-freebsd13.0");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(siginfo_type, {
+                                   {"si_signo", 0, 4},
+                                   {"si_errno", 4, 4},
+                                   {"si_code", 8, 4},
+                                   {"si_pid", 12, 4},
+                                   {"si_uid", 16, 4},
+                                   {"si_status", 20, 4},
+                                   {"si_addr", 24, 8},
+                                   {"si_value", 32, 8},
+                                   {"_reason._fault._trapno", 40, 4},
+                                   {"_reason._timer._timerid", 40, 4},
+                                   {"_reason._timer._overrun", 44, 4},
+                                   {"_reason._mesgq._mqd", 40, 4},
+                                   {"_reason._poll._band", 40, 8},
+                               });
+  }
+}
+
+TEST_F(PlatformSiginfoTest, TestFreeBSD_32bit) {
+  for (std::string arch : {"i386"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo(arch + "-unknown-freebsd13.0");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(siginfo_type, {
+                                   {"si_signo", 0, 4},
+                                   {"si_errno", 4, 4},
+                                   {"si_code", 8, 4},
+                                   {"si_pid", 12, 4},
+                                   {"si_uid", 16, 4},
+                                   {"si_status", 20, 4},
+                                   {"si_addr", 24, 4},
+                                   {"si_value", 28, 4},
+                                   {"_reason._fault._trapno", 32, 4},
+                                   {"_reason._timer._timerid", 32, 4},
+                                   {"_reason._timer._overrun", 36, 4},
+                                   {"_reason._mesgq._mqd", 32, 4},
+                                   {"_reason._poll._band", 32, 4},
+                               });
+  }
+}
+
+TEST_F(PlatformSiginfoTest, TestNetBSD_64bit) {
+  for (std::string arch : {"x86_64"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo(arch + "-unknown-netbsd9.0");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(
+        siginfo_type,
+        {
+            {"_info._signo", 0, 4},
+            {"_info._code", 4, 4},
+            {"_info._errno", 8, 4},
+            {"_info._reason._rt._pid", 16, 4},
+            {"_info._reason._rt._uid", 20, 4},
+            {"_info._reason._rt._value", 24, 8},
+            {"_info._reason._child._pid", 16, 4},
+            {"_info._reason._child._uid", 20, 4},
+            {"_info._reason._child._status", 24, 4},
+            {"_info._reason._child._utime", 28, 4},
+            {"_info._reason._child._stime", 32, 4},
+            {"_info._reason._fault._addr", 16, 8},
+            {"_info._reason._fault._trap", 24, 4},
+            {"_info._reason._fault._trap2", 28, 4},
+            {"_info._reason._fault._trap3", 32, 4},
+            {"_info._reason._poll._band", 16, 8},
+            {"_info._reason._poll._fd", 24, 4},
+            {"_info._reason._syscall._sysnum", 16, 4},
+            {"_info._reason._syscall._retval", 20, 8},
+            {"_info._reason._syscall._error", 28, 4},
+            {"_info._reason._syscall._args", 32, 64},
+            {"_info._reason._ptrace_state._pe_report_event", 16, 4},
+            {"_info._reason._ptrace_state._option._pe_other_pid", 20, 4},
+            {"_info._reason._ptrace_state._option._pe_lwp", 20, 4},
+        });
+  }
+}
+
+TEST_F(PlatformSiginfoTest, TestNetBSD_32bit) {
+  for (std::string arch : {"i386"}) {
+    SCOPED_TRACE(arch);
+    InitializeSiginfo(arch + "-unknown-netbsd9.0");
+    ASSERT_TRUE(siginfo_type);
+
+    ExpectFields(
+        siginfo_type,
+        {
+            {"_info._signo", 0, 4},
+            {"_info._code", 4, 4},
+            {"_info._errno", 8, 4},
+            {"_info._reason._rt._pid", 12, 4},
+            {"_info._reason._rt._uid", 16, 4},
+            {"_info._reason._rt._value", 20, 4},
+            {"_info._reason._child._pid", 12, 4},
+            {"_info._reason._child._uid", 16, 4},
+            {"_info._reason._child._status", 20, 4},
+            {"_info._reason._child._utime", 24, 4},
+            {"_info._reason._child._stime", 28, 4},
+            {"_info._reason._fault._addr", 12, 4},
+            {"_info._reason._fault._trap", 16, 4},
+            {"_info._reason._fault._trap2", 20, 4},
+            {"_info._reason._fault._trap3", 24, 4},
+            {"_info._reason._poll._band", 12, 4},
+            {"_info._reason._poll._fd", 16, 4},
+            {"_info._reason._syscall._sysnum", 12, 4},
+            {"_info._reason._syscall._retval", 16, 8},
+            {"_info._reason._syscall._error", 24, 4},
+            {"_info._reason._syscall._args", 28, 64},
+            {"_info._reason._ptrace_state._pe_report_event", 12, 4},
+            {"_info._reason._ptrace_state._option._pe_other_pid", 16, 4},
+            {"_info._reason._ptrace_state._option._pe_lwp", 16, 4},
+        });
+  }
+}

diff  --git a/lldb/unittests/Platform/tools/generate_siginfo.c b/lldb/unittests/Platform/tools/generate_siginfo.c
new file mode 100644
index 000000000000..5294a1b141dc
--- /dev/null
+++ b/lldb/unittests/Platform/tools/generate_siginfo.c
@@ -0,0 +1,112 @@
+//===-- generate_siginfo_linux.c ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <signal.h>
+#include <stddef.h>
+#include <stdio.h>
+
+siginfo_t siginfo;
+
+#define P(member)                                                              \
+  printf("                   {\"%s\", %zd, %zd},\n", #member,   \
+         offsetof(siginfo_t, member), sizeof(siginfo.member));
+
+// undef annoying "POSIX friendliness" macros
+#undef si_pid
+#undef si_uid
+#undef si_overrun
+#undef si_status
+#undef si_utime
+#undef si_stime
+#undef si_addr
+#undef si_addr_lsb
+#undef si_band
+#undef si_fd
+
+int main() {
+  printf("  ExpectFields(siginfo_type,\n");
+  printf("               {\n");
+
+#if !defined(__NetBSD__)
+  P(si_signo);
+  P(si_errno);
+  P(si_code);
+
+#if defined(__GLIBC__)
+  P(_sifields._kill.si_pid);
+  P(_sifields._kill.si_uid);
+  P(_sifields._timer.si_tid);
+  P(_sifields._timer.si_overrun);
+  P(_sifields._timer.si_sigval);
+  P(_sifields._rt.si_pid);
+  P(_sifields._rt.si_uid);
+  P(_sifields._rt.si_sigval);
+  P(_sifields._sigchld.si_pid);
+  P(_sifields._sigchld.si_uid);
+  P(_sifields._sigchld.si_status);
+  P(_sifields._sigchld.si_utime);
+  P(_sifields._sigchld.si_stime);
+  P(_sifields._sigfault.si_addr);
+  P(_sifields._sigfault.si_addr_lsb);
+  P(_sifields._sigfault._bounds._addr_bnd._lower);
+  P(_sifields._sigfault._bounds._addr_bnd._upper);
+  P(_sifields._sigfault._bounds._pkey);
+  P(_sifields._sigpoll.si_band);
+  P(_sifields._sigpoll.si_fd);
+  P(_sifields._sigsys._call_addr);
+  P(_sifields._sigsys._syscall);
+  P(_sifields._sigsys._arch);
+#endif // defined(__GLIBC__)
+
+#if defined(__FreeBSD__)
+  // these are top-level fields on FreeBSD
+  P(si_pid);
+  P(si_uid);
+  P(si_status);
+  P(si_addr);
+  P(si_value);
+  P(_reason._fault._trapno);
+  P(_reason._timer._timerid);
+  P(_reason._timer._overrun);
+  P(_reason._mesgq._mqd);
+  P(_reason._poll._band);
+#endif // defined(__FreeBSD__)
+
+#else // defined(__NetBSD__)
+
+  P(_info._signo);
+  P(_info._code);
+  P(_info._errno);
+  P(_info._reason._rt._pid);
+  P(_info._reason._rt._uid);
+  P(_info._reason._rt._value);
+  P(_info._reason._child._pid);
+  P(_info._reason._child._uid);
+  P(_info._reason._child._status);
+  P(_info._reason._child._utime);
+  P(_info._reason._child._stime);
+  P(_info._reason._fault._addr);
+  P(_info._reason._fault._trap);
+  P(_info._reason._fault._trap2);
+  P(_info._reason._fault._trap3);
+  P(_info._reason._poll._band);
+  P(_info._reason._poll._fd);
+  P(_info._reason._syscall._sysnum);
+  P(_info._reason._syscall._retval);
+  P(_info._reason._syscall._error);
+  P(_info._reason._syscall._args);
+  P(_info._reason._ptrace_state._pe_report_event);
+  P(_info._reason._ptrace_state._option._pe_other_pid);
+  P(_info._reason._ptrace_state._option._pe_lwp);
+
+#endif // defined(__NetBSD__)
+
+  printf("               });\n");
+
+  return 0;
+}


        


More information about the lldb-commits mailing list