166 changes: 166 additions & 0 deletions lldb/source/Plugins/Platform/Linux/PlatformLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions lldb/source/Plugins/Platform/Linux/PlatformLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
141 changes: 141 additions & 0 deletions lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ bool GDBRemoteCommunicationClient::GetQXferMemoryMapReadSupported() {
return m_supports_qXfer_memory_map_read == eLazyBoolYes;
}

bool GDBRemoteCommunicationClient::GetQXferSigInfoReadSupported() {
if (m_supports_qXfer_siginfo_read == eLazyBoolCalculate) {
GetRemoteQSupported();
}
return m_supports_qXfer_siginfo_read == eLazyBoolYes;
}

uint64_t GDBRemoteCommunicationClient::GetRemoteMaxPacketSize() {
if (m_max_packet_size == 0) {
GetRemoteQSupported();
Expand Down Expand Up @@ -273,6 +280,7 @@ void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) {
m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
m_supports_qXfer_features_read = eLazyBoolCalculate;
m_supports_qXfer_memory_map_read = eLazyBoolCalculate;
m_supports_qXfer_siginfo_read = eLazyBoolCalculate;
m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
m_uses_native_signals = eLazyBoolCalculate;
m_supports_qProcessInfoPID = true;
Expand Down Expand Up @@ -320,6 +328,7 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
m_supports_qXfer_features_read = eLazyBoolNo;
m_supports_qXfer_memory_map_read = eLazyBoolNo;
m_supports_qXfer_siginfo_read = eLazyBoolNo;
m_supports_multiprocess = eLazyBoolNo;
m_supports_qEcho = eLazyBoolNo;
m_supports_QPassSignals = eLazyBoolNo;
Expand Down Expand Up @@ -362,6 +371,8 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
m_supports_qXfer_features_read = eLazyBoolYes;
else if (x == "qXfer:memory-map:read+")
m_supports_qXfer_memory_map_read = eLazyBoolYes;
else if (x == "qXfer:siginfo:read+")
m_supports_qXfer_siginfo_read = eLazyBoolYes;
else if (x == "qEcho")
m_supports_qEcho = eLazyBoolYes;
else if (x == "QPassSignals+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {

bool GetQXferMemoryMapReadSupported();

bool GetQXferSigInfoReadSupported();

LazyBool SupportsAllocDeallocMemory() // const
{
// Uncomment this to have lldb pretend the debug server doesn't respond to
Expand Down Expand Up @@ -551,6 +553,7 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {
LazyBool m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
LazyBool m_supports_qXfer_features_read = eLazyBoolCalculate;
LazyBool m_supports_qXfer_memory_map_read = eLazyBoolCalculate;
LazyBool m_supports_qXfer_siginfo_read = eLazyBoolCalculate;
LazyBool m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
LazyBool m_supports_jThreadExtendedInfo = eLazyBoolCalculate;
LazyBool m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolCalculate;
Expand Down
20 changes: 20 additions & 0 deletions lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,23 @@ bool ThreadGDBRemote::CalculateStopInfo() {
->CalculateThreadStopInfo(this);
return false;
}

llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
ThreadGDBRemote::GetSiginfo(size_t max_size) const {
ProcessSP process_sp(GetProcess());
if (!process_sp)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"no process");
ProcessGDBRemote *gdb_process =
static_cast<ProcessGDBRemote *>(process_sp.get());
if (!gdb_process->m_gdb_comm.GetQXferSigInfoReadSupported())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"qXfer:siginfo:read not supported");

llvm::Expected<std::string> response =
gdb_process->m_gdb_comm.ReadExtFeature("siginfo", "");
if (!response)
return response.takeError();

return llvm::MemoryBuffer::getMemBufferCopy(response.get());
}
3 changes: 3 additions & 0 deletions lldb/source/Plugins/Process/gdb-remote/ThreadGDBRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class ThreadGDBRemote : public Thread {

StructuredData::ObjectSP FetchThreadExtendedInfo() override;

llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
GetSiginfo(size_t max_size) const override;

protected:
friend class ProcessGDBRemote;

Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Target/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,3 +2003,7 @@ size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,

return 0;
}

CompilerType Platform::GetSiginfoType(const llvm::Triple& triple) {
return CompilerType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,99 @@ def cont(self):
lldb.eStopReasonSignal)
self.assertEqual(process.threads[0].GetStopDescription(100),
'signal SIGUSR1')

def do_siginfo_test(self, platform, target_yaml, raw_data, expected):
class MyResponder(MockGDBServerResponder):
def qSupported(self, client_supported):
return "PacketSize=3fff;QStartNoAckMode+;qXfer:siginfo:read+"

def qXferRead(self, obj, annex, offset, length):
if obj == "siginfo":
return raw_data, False
else:
return None, False

def haltReason(self):
return "T02"

def cont(self):
return self.haltReason()

self.server.responder = MyResponder()

self.runCmd("platform select " + platform)
target = self.createTarget(target_yaml)
process = self.connect(target)

error = lldb.SBError()
siginfo = process.threads[0].GetSiginfo(error)
self.assertTrue(siginfo, error)

for key, value in expected.items():
self.assertEqual(siginfo.GetValueForExpressionPath("." + key)
.GetValueAsUnsigned(),
value)


def test_siginfo_linux_amd64(self):
data = (
# si_signo si_errno si_code
"\x11\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00"
# __pad0 si_pid si_uid
"\x00\x00\x00\x00\xbf\xf7\x0b\x00\xe8\x03\x00\x00"
# si_status
"\x0c\x00\x00\x00" + "\x00" * 100)
expected = {
"si_signo": 17, # SIGCHLD
"si_errno": 0,
"si_code": 1, # CLD_EXITED
"_sifields._sigchld.si_pid": 784319,
"_sifields._sigchld.si_uid": 1000,
"_sifields._sigchld.si_status": 12,
"_sifields._sigchld.si_utime": 0,
"_sifields._sigchld.si_stime": 0,
}
self.do_siginfo_test("remote-linux", "basic_eh_frame.yaml",
data, expected)

def test_siginfo_linux_i386(self):
data = (
# si_signo si_errno si_code
"\x11\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00"
# si_pid si_uid si_status
"\x49\x43\x07\x00\xe8\x03\x00\x00\x0c\x00\x00\x00"
+ "\x00" * 104)
expected = {
"si_signo": 17, # SIGCHLD
"si_errno": 0,
"si_code": 1, # CLD_EXITED
"_sifields._sigchld.si_pid": 475977,
"_sifields._sigchld.si_uid": 1000,
"_sifields._sigchld.si_status": 12,
"_sifields._sigchld.si_utime": 0,
"_sifields._sigchld.si_stime": 0,
}
self.do_siginfo_test("remote-linux", "basic_eh_frame-i386.yaml",
data, expected)

def test_siginfo_freebsd_amd64(self):
data = (
# si_signo si_errno si_code
"\x0b\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00"
# si_pid si_uid si_status
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
# si_addr
"\x76\x98\xba\xdc\xfe\x00\x00\x00"
# si_status si_trapno
"\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00"
+ "\x00" * 36)

expected = {
"si_signo": 11, # SIGSEGV
"si_errno": 0,
"si_code": 1, # SEGV_MAPERR
"si_addr": 0xfedcba9876,
"_reason._fault._trapno": 12,
}
self.do_siginfo_test("remote-freebsd", "basic_eh_frame.yaml",
data, expected)
4 changes: 4 additions & 0 deletions lldb/unittests/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
add_lldb_unittest(LLDBPlatformTests
PlatformAppleSimulatorTest.cpp
PlatformDarwinTest.cpp
PlatformSiginfoTest.cpp

LINK_LIBS
lldbPluginPlatformFreeBSD
lldbPluginPlatformLinux
lldbPluginPlatformMacOSX
lldbPluginPlatformNetBSD
LINK_COMPONENTS
Support
)
Expand Down
311 changes: 311 additions & 0 deletions lldb/unittests/Platform/PlatformSiginfoTest.cpp
Original file line number Diff line number Diff line change
@@ -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},
});
}
}
112 changes: 112 additions & 0 deletions lldb/unittests/Platform/tools/generate_siginfo.c
Original file line number Diff line number Diff line change
@@ -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;
}