diff --git a/lldb/unittests/Process/Utility/CMakeLists.txt b/lldb/unittests/Process/Utility/CMakeLists.txt --- a/lldb/unittests/Process/Utility/CMakeLists.txt +++ b/lldb/unittests/Process/Utility/CMakeLists.txt @@ -15,9 +15,10 @@ ${NETBSD_SOURCES}) add_lldb_unittest(ProcessUtilityTests - RegisterContextTest.cpp + DynamicRegisterInfoTest.cpp LinuxProcMapsTest.cpp MemoryTagManagerAArch64MTETest.cpp + RegisterContextTest.cpp ${PLATFORM_SOURCES} LINK_LIBS diff --git a/lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp b/lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp new file mode 100644 --- /dev/null +++ b/lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp @@ -0,0 +1,102 @@ +//===-- DynamicRegisterInfoTest.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 "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "Plugins/Process/Utility/DynamicRegisterInfo.h" + +#include "lldb/Utility/ArchSpec.h" + +using namespace lldb_private; + +static std::vector regs_to_vector(uint32_t *regs) { + std::vector ret; + if (regs) { + while (*regs != LLDB_INVALID_REGNUM) + ret.push_back(*regs++); + } + return ret; +} + +class DynamicRegisterInfoTest : public ::testing::Test { +protected: + DynamicRegisterInfo info; + uint32_t next_regnum = 0; + ConstString group{"group"}; + + uint32_t AddTestRegister(const char *name, uint32_t byte_size, + std::vector value_regs = {}, + std::vector invalidate_regs = {}) { + struct RegisterInfo new_reg { + name, nullptr, byte_size, LLDB_INVALID_INDEX32, lldb::eEncodingUint, + lldb::eFormatUnsigned, + {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, + next_regnum, next_regnum}, + nullptr, nullptr, nullptr, 0 + }; + + if (!value_regs.empty()) { + value_regs.push_back(LLDB_INVALID_REGNUM); + new_reg.value_regs = value_regs.data(); + } + if (!invalidate_regs.empty()) { + invalidate_regs.push_back(LLDB_INVALID_REGNUM); + new_reg.invalidate_regs = invalidate_regs.data(); + } + + info.AddRegister(new_reg, group); + return next_regnum++; + } + + void AssertRegisterInfo(uint32_t reg_num, const char *reg_name, + uint32_t byte_offset, + std::vector value_regs = {}, + std::vector invalidate_regs = {}) { + const RegisterInfo *reg = info.GetRegisterInfoAtIndex(reg_num); + EXPECT_NE(reg, nullptr); + if (!reg) + return; + + EXPECT_STREQ(reg->name, reg_name); + EXPECT_EQ(reg->byte_offset, byte_offset); + EXPECT_THAT(regs_to_vector(reg->value_regs), value_regs); + EXPECT_THAT(regs_to_vector(reg->invalidate_regs), invalidate_regs); + } +}; + +#define ASSERT_REG(reg, ...) { \ + SCOPED_TRACE("at register " #reg); \ + AssertRegisterInfo(reg, #reg, __VA_ARGS__); \ + } + +TEST_F(DynamicRegisterInfoTest, finalize_regs) { + // Add regular registers + uint32_t b1 = AddTestRegister("b1", 8); + uint32_t b2 = AddTestRegister("b2", 8); + + // Add a few sub-registers + uint32_t s1 = AddTestRegister("s1", 4, {b1}); + uint32_t s2 = AddTestRegister("s2", 4, {b2}); + + // Add a register with invalidate_regs + uint32_t i1 = AddTestRegister("i1", 8, {}, {b1}); + + // Add a register with indirect invalidate regs to be expanded + // TODO: why is it done conditionally to value_regs? + uint32_t i2 = AddTestRegister("i2", 4, {b2}, {i1}); + + info.Finalize(lldb_private::ArchSpec()); + + ASSERT_REG(b1, 0); + ASSERT_REG(b2, 8); + ASSERT_REG(s1, 0, {b1}); + ASSERT_REG(s2, 8, {b2}); + ASSERT_REG(i1, 16, {}, {b1}); + ASSERT_REG(i2, 8, {b2}, {b1, i1}); +}