1 //===-- PlatformSiginfoTest.cpp -------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include <initializer_list>
15 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
16 #include "Plugins/Platform/Linux/PlatformLinux.h"
17 #include "Plugins/Platform/NetBSD/PlatformNetBSD.h"
18 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
20 #include "TestingSupport/SubsystemRAII.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Host/FileSystem.h"
23 #include "lldb/Host/HostInfo.h"
24 #include "lldb/Utility/ArchSpec.h"
27 using namespace lldb_private
;
28 using namespace lldb_private::repro
;
31 class PlatformSiginfoTest
: public ::testing::Test
{
32 SubsystemRAII
<FileSystem
, HostInfo
, TypeSystemClang
> subsystems
;
33 PlatformSP platform_sp
;
34 DebuggerSP debugger_sp
;
38 CompilerType siginfo_type
;
40 void SetUp() override
{
41 platform_freebsd::PlatformFreeBSD::Initialize();
42 platform_linux::PlatformLinux::Initialize();
43 platform_netbsd::PlatformNetBSD::Initialize();
46 void TearDown() override
{
47 platform_netbsd::PlatformNetBSD::Terminate();
48 platform_linux::PlatformLinux::Terminate();
49 platform_freebsd::PlatformFreeBSD::Terminate();
52 typedef std::tuple
<const char *, uint64_t, uint64_t> field_tuple
;
54 void ExpectField(const CompilerType
&siginfo_type
, field_tuple field
) {
56 uint64_t offset
, size
;
57 std::tie(path
, offset
, size
) = field
;
60 CompilerType field_type
= siginfo_type
;
61 uint64_t total_offset
= 0;
62 for (auto field_name
: llvm::split(path
, '.')) {
64 ASSERT_NE(field_type
.GetIndexOfFieldWithName(field_name
.str().c_str(),
65 &field_type
, &bit_offset
),
67 total_offset
+= bit_offset
;
70 EXPECT_EQ(total_offset
, offset
* 8);
71 EXPECT_EQ(field_type
.GetByteSize(nullptr), std::optional
<uint64_t>(size
));
74 void ExpectFields(const CompilerType
&container
,
75 std::initializer_list
<field_tuple
> fields
) {
77 ExpectField(container
, x
);
80 void InitializeSiginfo(const std::string
&triple
) {
81 ArchSpec
arch(triple
);
83 switch (arch
.GetTriple().getOS()) {
84 case llvm::Triple::FreeBSD
:
86 platform_freebsd::PlatformFreeBSD::CreateInstance(true, &arch
);
88 case llvm::Triple::Linux
:
89 platform_sp
= platform_linux::PlatformLinux::CreateInstance(true, &arch
);
91 case llvm::Triple::NetBSD
:
93 platform_netbsd::PlatformNetBSD::CreateInstance(true, &arch
);
96 llvm_unreachable("unknown ostype in triple");
98 Platform::SetHostPlatform(platform_sp
);
100 debugger_sp
= Debugger::CreateInstance();
101 ASSERT_TRUE(debugger_sp
);
103 debugger_sp
->GetTargetList().CreateTarget(
104 *debugger_sp
, "", arch
, eLoadDependentsNo
, platform_sp
, target_sp
);
105 ASSERT_TRUE(target_sp
);
107 siginfo_type
= platform_sp
->GetSiginfoType(arch
.GetTriple());
113 TEST_F(PlatformSiginfoTest
, TestLinux_64bit
) {
114 for (std::string arch
: {"x86_64", "aarch64", "powerpc64le"}) {
116 InitializeSiginfo(arch
+ "-pc-linux-gnu");
117 ASSERT_TRUE(siginfo_type
);
119 ExpectFields(siginfo_type
,
124 {"_sifields._kill.si_pid", 16, 4},
125 {"_sifields._kill.si_uid", 20, 4},
126 {"_sifields._timer.si_tid", 16, 4},
127 {"_sifields._timer.si_overrun", 20, 4},
128 {"_sifields._timer.si_sigval", 24, 8},
129 {"_sifields._rt.si_pid", 16, 4},
130 {"_sifields._rt.si_uid", 20, 4},
131 {"_sifields._rt.si_sigval", 24, 8},
132 {"_sifields._sigchld.si_pid", 16, 4},
133 {"_sifields._sigchld.si_uid", 20, 4},
134 {"_sifields._sigchld.si_status", 24, 4},
135 {"_sifields._sigchld.si_utime", 32, 8},
136 {"_sifields._sigchld.si_stime", 40, 8},
137 {"_sifields._sigfault.si_addr", 16, 8},
138 {"_sifields._sigfault.si_addr_lsb", 24, 2},
139 {"_sifields._sigfault._bounds._addr_bnd._lower", 32, 8},
140 {"_sifields._sigfault._bounds._addr_bnd._upper", 40, 8},
141 {"_sifields._sigfault._bounds._pkey", 32, 4},
142 {"_sifields._sigpoll.si_band", 16, 8},
143 {"_sifields._sigpoll.si_fd", 24, 4},
144 {"_sifields._sigsys._call_addr", 16, 8},
145 {"_sifields._sigsys._syscall", 24, 4},
146 {"_sifields._sigsys._arch", 28, 4},
151 TEST_F(PlatformSiginfoTest
, TestLinux_32bit
) {
152 for (std::string arch
: {"i386", "armv7"}) {
154 InitializeSiginfo(arch
+ "-pc-linux");
155 ASSERT_TRUE(siginfo_type
);
157 ExpectFields(siginfo_type
,
162 {"_sifields._kill.si_pid", 12, 4},
163 {"_sifields._kill.si_uid", 16, 4},
164 {"_sifields._timer.si_tid", 12, 4},
165 {"_sifields._timer.si_overrun", 16, 4},
166 {"_sifields._timer.si_sigval", 20, 4},
167 {"_sifields._rt.si_pid", 12, 4},
168 {"_sifields._rt.si_uid", 16, 4},
169 {"_sifields._rt.si_sigval", 20, 4},
170 {"_sifields._sigchld.si_pid", 12, 4},
171 {"_sifields._sigchld.si_uid", 16, 4},
172 {"_sifields._sigchld.si_status", 20, 4},
173 {"_sifields._sigchld.si_utime", 24, 4},
174 {"_sifields._sigchld.si_stime", 28, 4},
175 {"_sifields._sigfault.si_addr", 12, 4},
176 {"_sifields._sigfault.si_addr_lsb", 16, 2},
177 {"_sifields._sigfault._bounds._addr_bnd._lower", 20, 4},
178 {"_sifields._sigfault._bounds._addr_bnd._upper", 24, 4},
179 {"_sifields._sigfault._bounds._pkey", 20, 4},
180 {"_sifields._sigpoll.si_band", 12, 4},
181 {"_sifields._sigpoll.si_fd", 16, 4},
182 {"_sifields._sigsys._call_addr", 12, 4},
183 {"_sifields._sigsys._syscall", 16, 4},
184 {"_sifields._sigsys._arch", 20, 4},
189 TEST_F(PlatformSiginfoTest
, TestFreeBSD_64bit
) {
190 for (std::string arch
: {"x86_64", "aarch64"}) {
192 InitializeSiginfo("x86_64-unknown-freebsd13.0");
193 ASSERT_TRUE(siginfo_type
);
195 ExpectFields(siginfo_type
, {
201 {"si_status", 20, 4},
204 {"_reason._fault._trapno", 40, 4},
205 {"_reason._timer._timerid", 40, 4},
206 {"_reason._timer._overrun", 44, 4},
207 {"_reason._mesgq._mqd", 40, 4},
208 {"_reason._poll._band", 40, 8},
213 TEST_F(PlatformSiginfoTest
, TestFreeBSD_32bit
) {
214 for (std::string arch
: {"i386"}) {
216 InitializeSiginfo(arch
+ "-unknown-freebsd13.0");
217 ASSERT_TRUE(siginfo_type
);
219 ExpectFields(siginfo_type
, {
225 {"si_status", 20, 4},
228 {"_reason._fault._trapno", 32, 4},
229 {"_reason._timer._timerid", 32, 4},
230 {"_reason._timer._overrun", 36, 4},
231 {"_reason._mesgq._mqd", 32, 4},
232 {"_reason._poll._band", 32, 4},
237 TEST_F(PlatformSiginfoTest
, TestNetBSD_64bit
) {
238 for (std::string arch
: {"x86_64"}) {
240 InitializeSiginfo(arch
+ "-unknown-netbsd9.0");
241 ASSERT_TRUE(siginfo_type
);
246 {"_info._signo", 0, 4},
247 {"_info._code", 4, 4},
248 {"_info._errno", 8, 4},
249 {"_info._reason._rt._pid", 16, 4},
250 {"_info._reason._rt._uid", 20, 4},
251 {"_info._reason._rt._value", 24, 8},
252 {"_info._reason._child._pid", 16, 4},
253 {"_info._reason._child._uid", 20, 4},
254 {"_info._reason._child._status", 24, 4},
255 {"_info._reason._child._utime", 28, 4},
256 {"_info._reason._child._stime", 32, 4},
257 {"_info._reason._fault._addr", 16, 8},
258 {"_info._reason._fault._trap", 24, 4},
259 {"_info._reason._fault._trap2", 28, 4},
260 {"_info._reason._fault._trap3", 32, 4},
261 {"_info._reason._poll._band", 16, 8},
262 {"_info._reason._poll._fd", 24, 4},
263 {"_info._reason._syscall._sysnum", 16, 4},
264 {"_info._reason._syscall._retval", 20, 8},
265 {"_info._reason._syscall._error", 28, 4},
266 {"_info._reason._syscall._args", 32, 64},
267 {"_info._reason._ptrace_state._pe_report_event", 16, 4},
268 {"_info._reason._ptrace_state._option._pe_other_pid", 20, 4},
269 {"_info._reason._ptrace_state._option._pe_lwp", 20, 4},
274 TEST_F(PlatformSiginfoTest
, TestNetBSD_32bit
) {
275 for (std::string arch
: {"i386"}) {
277 InitializeSiginfo(arch
+ "-unknown-netbsd9.0");
278 ASSERT_TRUE(siginfo_type
);
283 {"_info._signo", 0, 4},
284 {"_info._code", 4, 4},
285 {"_info._errno", 8, 4},
286 {"_info._reason._rt._pid", 12, 4},
287 {"_info._reason._rt._uid", 16, 4},
288 {"_info._reason._rt._value", 20, 4},
289 {"_info._reason._child._pid", 12, 4},
290 {"_info._reason._child._uid", 16, 4},
291 {"_info._reason._child._status", 20, 4},
292 {"_info._reason._child._utime", 24, 4},
293 {"_info._reason._child._stime", 28, 4},
294 {"_info._reason._fault._addr", 12, 4},
295 {"_info._reason._fault._trap", 16, 4},
296 {"_info._reason._fault._trap2", 20, 4},
297 {"_info._reason._fault._trap3", 24, 4},
298 {"_info._reason._poll._band", 12, 4},
299 {"_info._reason._poll._fd", 16, 4},
300 {"_info._reason._syscall._sysnum", 12, 4},
301 {"_info._reason._syscall._retval", 16, 8},
302 {"_info._reason._syscall._error", 24, 4},
303 {"_info._reason._syscall._args", 28, 64},
304 {"_info._reason._ptrace_state._pe_report_event", 12, 4},
305 {"_info._reason._ptrace_state._option._pe_other_pid", 16, 4},
306 {"_info._reason._ptrace_state._option._pe_lwp", 16, 4},