Index: lldb/unittests/Process/Utility/CMakeLists.txt =================================================================== --- lldb/unittests/Process/Utility/CMakeLists.txt +++ 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 Index: lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp =================================================================== --- /dev/null +++ lldb/unittests/Process/Utility/DynamicRegisterInfoTest.cpp @@ -0,0 +1,124 @@ +//===-- 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; + +class TestDynamicRegisterInfo : public DynamicRegisterInfo { + uint32_t next_regnum = 0; + ConstString group{"group"}; + +public: + 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(); + } + + 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 = {}) { + std::string reg_msg = + llvm::formatv("at register {0} (num: {1})", reg_name, reg_num); + const RegisterInfo *reg = GetRegisterInfoAtIndex(reg_num); + EXPECT_NE(reg, nullptr) << reg_msg; + if (!reg) + return; + + EXPECT_EQ(reg->byte_offset, byte_offset) << reg_msg; + + if (value_regs.empty()) + EXPECT_EQ(reg->value_regs, nullptr) << reg_msg; + else { + EXPECT_NE(reg->value_regs, nullptr) << reg_msg; + + size_t i; + for (i = 0; i < value_regs.size(); i++) { + EXPECT_EQ(reg->value_regs[i], value_regs[i]) + << reg_msg << " at i = " << i; + if (reg->value_regs[i] == LLDB_INVALID_REGNUM) + break; + } + + EXPECT_EQ(reg->value_regs[i], LLDB_INVALID_REGNUM) + << reg_msg << " at i = " << i; + } + + if (invalidate_regs.empty()) + EXPECT_EQ(reg->invalidate_regs, nullptr) << reg_msg; + else { + EXPECT_NE(reg->invalidate_regs, nullptr) << reg_msg; + + size_t i; + for (i = 0; i < invalidate_regs.size(); i++) { + EXPECT_EQ(reg->invalidate_regs[i], invalidate_regs[i]) + << reg_msg << " at i = " << i; + if (reg->invalidate_regs[i] == LLDB_INVALID_REGNUM) + break; + } + + EXPECT_EQ(reg->invalidate_regs[i], LLDB_INVALID_REGNUM) + << reg_msg << " at i = " << i; + } + } +}; + +#define ASSERT_REG(reg, ...) info.AssertRegisterInfo(reg, #reg, __VA_ARGS__) + +TEST(DynamicRegisterInfoTest, finalize_regs) { + TestDynamicRegisterInfo info; + + // Add regular registers + uint32_t b1 = info.AddTestRegister("b1", 8); + uint32_t b2 = info.AddTestRegister("b2", 8); + + // Add a few sub-registers + uint32_t s1 = info.AddTestRegister("s1", 4, {b1}); + uint32_t s2 = info.AddTestRegister("s2", 4, {b2}); + + // Add a register with invalidate_regs + uint32_t i1 = info.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 = info.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}); +}