1 //===-- WatchpointAlgorithmsTests.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 "lldb/Breakpoint/WatchpointAlgorithms.h"
17 using namespace lldb_private
;
19 class WatchpointAlgorithmsTest
: public WatchpointAlgorithms
{
21 using WatchpointAlgorithms::PowerOf2Watchpoints
;
22 using WatchpointAlgorithms::Region
;
26 WatchpointAlgorithmsTest::Region user
; // What the user requested
27 std::vector
<WatchpointAlgorithmsTest::Region
>
28 hw
; // The hardware watchpoints we'll use
31 void check_testcase(testcase test
,
32 std::vector
<WatchpointAlgorithmsTest::Region
> result
,
33 size_t min_byte_size
, size_t max_byte_size
,
34 uint32_t address_byte_size
) {
36 EXPECT_EQ(result
.size(), test
.hw
.size());
37 for (size_t i
= 0; i
< result
.size(); i
++) {
38 EXPECT_EQ(result
[i
].addr
, test
.hw
[i
].addr
);
39 EXPECT_EQ(result
[i
].size
, test
.hw
[i
].size
);
43 TEST(WatchpointAlgorithmsTests
, PowerOf2Watchpoints
) {
46 std::vector
<testcase
> doubleword_max
= {
48 // These two tests don't work if lldb is built on
49 // a 32-bit system (likely with a 32-bit size_t).
50 // A 32-bit lldb debugging a 64-bit process isn't
51 // critical right now.
63 {{0x1010, 8}, {0x1018, 8}}
71 {{0x1004, 4}, {0x1008, 4}}
75 {{0x1000, 8}, {0x1008, 8}}
79 {{0x1000, 8}, {0x1008, 8}, {0x1010, 8}}
83 {{0x1010, 8}, {0x1018, 8}, {0x1020, 8}, {0x1028, 8}}
87 for (testcase test
: doubleword_max
) {
88 addr_t user_addr
= test
.user
.addr
;
89 size_t user_size
= test
.user
.size
;
90 size_t min_byte_size
= 1;
91 size_t max_byte_size
= 8;
92 size_t address_byte_size
= 8;
93 auto result
= WatchpointAlgorithmsTest::PowerOf2Watchpoints(
94 user_addr
, user_size
, min_byte_size
, max_byte_size
, address_byte_size
);
96 check_testcase(test
, result
, min_byte_size
, max_byte_size
,
101 std::vector
<testcase
> word_max
= {
108 {{0x1000, 4}, {0x1004, 4}}
112 for (testcase test
: word_max
) {
113 addr_t user_addr
= test
.user
.addr
;
114 size_t user_size
= test
.user
.size
;
115 size_t min_byte_size
= 1;
116 size_t max_byte_size
= 4;
117 size_t address_byte_size
= 4;
118 auto result
= WatchpointAlgorithmsTest::PowerOf2Watchpoints(
119 user_addr
, user_size
, min_byte_size
, max_byte_size
, address_byte_size
);
121 check_testcase(test
, result
, min_byte_size
, max_byte_size
,
126 std::vector
<testcase
> twogig_max
= {
136 // We increase 36 to the aligned 64 byte size, but
137 // 0x1000-0x1040 doesn't cover the requested region. Then
138 // we expand to 128 bytes starting at 0x1000 that does
139 // cover it. Is this a good tradeoff for a 36 byte region?
153 // In this case, our aligned size is 128, and increasing it to 256
154 // still can't watch the requested region. The algorithm
155 // falls back to using two 128 byte watchpoints.
156 // The alternative would be to use a 1024B watchpoint
157 // starting at 0x1000, to watch this 120 byte user request.
159 // This still isn't ideal. The user is asking to watch 0x12e0-1358
160 // and could be optimally handled by a
161 // 16-byte watchpoint at 0x12e0 and a 128-byte watchpoint at 0x1300
164 {{0x1280, 128}, {0x1300, 128}}
168 for (testcase test
: twogig_max
) {
169 addr_t user_addr
= test
.user
.addr
;
170 size_t user_size
= test
.user
.size
;
171 size_t min_byte_size
= 1;
172 size_t max_byte_size
= INT32_MAX
;
173 size_t address_byte_size
= 8;
174 auto result
= WatchpointAlgorithmsTest::PowerOf2Watchpoints(
175 user_addr
, user_size
, min_byte_size
, max_byte_size
, address_byte_size
);
177 check_testcase(test
, result
, min_byte_size
, max_byte_size
,