LLDB mainline
WatchpointAlgorithms.cpp
Go to the documentation of this file.
1//===-- WatchpointAlgorithms.cpp ------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
11#include "lldb/Target/Process.h"
14#include "lldb/Utility/Log.h"
15
16#include <algorithm>
17#include <utility>
18#include <vector>
19
20using namespace lldb;
21using namespace lldb_private;
22
23std::vector<WatchpointResourceSP>
25 addr_t addr, size_t size, bool read, bool write,
26 WatchpointHardwareFeature supported_features, ArchSpec &arch) {
27
28 std::vector<Region> entries;
29
30 if (supported_features & eWatchpointHardwareArmMASK) {
31 entries =
32 PowerOf2Watchpoints(addr, size,
33 /*min_byte_size*/ 1,
34 /*max_byte_size*/ INT32_MAX,
35 /*address_byte_size*/ arch.GetAddressByteSize());
36 } else {
37 // As a fallback, assume we can watch any power-of-2
38 // number of bytes up through the size of an address in the target.
39 entries =
40 PowerOf2Watchpoints(addr, size,
41 /*min_byte_size*/ 1,
42 /*max_byte_size*/ arch.GetAddressByteSize(),
43 /*address_byte_size*/ arch.GetAddressByteSize());
44 }
45
48 "AtomizeWatchpointRequest user request addr {0:x} size {1}",
49 addr, size);
50 std::vector<WatchpointResourceSP> resources;
51 for (Region &ent : entries) {
53 log, "AtomizeWatchpointRequest creating resource {0:x} size {1}",
54 ent.addr, ent.size);
55 WatchpointResourceSP wp_res_sp =
56 std::make_shared<WatchpointResource>(ent.addr, ent.size, read, write);
57 resources.push_back(wp_res_sp);
58 }
59
60 return resources;
61}
62
63/// Convert a user's watchpoint request (\a user_addr and \a user_size)
64/// into hardware watchpoints, for a target that can watch a power-of-2
65/// region of memory (1, 2, 4, 8, etc), aligned to that same power-of-2
66/// memory address.
67///
68/// If a user asks to watch 4 bytes at address 0x1002 (0x1002-0x1005
69/// inclusive) we can implement this with two 2-byte watchpoints
70/// (0x1002 and 0x1004) or with an 8-byte watchpoint at 0x1000.
71/// A 4-byte watchpoint at 0x1002 would not be properly 4 byte aligned.
72///
73/// If a user asks to watch 16 bytes at 0x1000, and this target supports
74/// 8-byte watchpoints, we can implement this with two 8-byte watchpoints
75/// at 0x1000 and 0x1008.
76std::vector<WatchpointAlgorithms::Region>
78 size_t min_byte_size,
79 size_t max_byte_size,
80 uint32_t address_byte_size) {
81
84 log,
85 "AtomizeWatchpointRequest user request addr {0:x} size {1} "
86 "min_byte_size {2}, max_byte_size {3}, address_byte_size {4}",
87 user_addr, user_size, min_byte_size, max_byte_size, address_byte_size);
88
89 // Can't watch zero bytes.
90 if (user_size == 0)
91 return {};
92
93 size_t aligned_size = std::max(user_size, min_byte_size);
94 /// Round up \a user_size to the next power-of-2 size
95 /// user_size == 8 -> aligned_size == 8
96 /// user_size == 9 -> aligned_size == 16
97 aligned_size = llvm::bit_ceil(aligned_size);
98
99 addr_t aligned_start = user_addr & ~(aligned_size - 1);
100
101 // Does this power-of-2 memory range, aligned to power-of-2 that the
102 // hardware can watch, completely cover the requested region.
103 if (aligned_size <= max_byte_size &&
104 aligned_start + aligned_size >= user_addr + user_size)
105 return {{aligned_start, aligned_size}};
106
107 // If the maximum region we can watch is larger than the aligned
108 // size, try increasing the region size by one power of 2 and see
109 // if aligning to that amount can cover the requested region.
110 //
111 // Increasing the aligned_size repeatedly instead of splitting the
112 // watchpoint can result in us watching large regions of memory
113 // unintentionally when we could use small two watchpoints. e.g.
114 // user_addr 0x3ff8 user_size 32
115 // can be watched with four 8-byte watchpoints or if it's done with one
116 // MASK watchpoint, it would need to be a 32KB watchpoint (a 16KB
117 // watchpoint at 0x0 only covers 0x0000-0x4000). A user request
118 // at the end of a power-of-2 region can lead to these undesirably
119 // large watchpoints and many false positive hits to ignore.
120 if (max_byte_size >= (aligned_size << 1)) {
121 aligned_size <<= 1;
122 aligned_start = user_addr & ~(aligned_size - 1);
123 if (aligned_size <= max_byte_size &&
124 aligned_start + aligned_size >= user_addr + user_size)
125 return {{aligned_start, aligned_size}};
126
127 // Go back to our original aligned size, to try the multiple
128 // watchpoint approach.
129 aligned_size >>= 1;
130 }
131
132 // We need to split the user's watchpoint into two or more watchpoints
133 // that can be monitored by hardware, because of alignment and/or size
134 // reasons.
135 aligned_size = std::min(aligned_size, max_byte_size);
136 aligned_start = user_addr & ~(aligned_size - 1);
137
138 std::vector<Region> result;
139 addr_t current_address = aligned_start;
140 const addr_t user_end_address = user_addr + user_size;
141 while (current_address + aligned_size < user_end_address) {
142 result.push_back({current_address, aligned_size});
143 current_address += aligned_size;
144 }
145
146 if (current_address < user_end_address)
147 result.push_back({current_address, aligned_size});
148
149 return result;
150}
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:376
An architecture specification class.
Definition ArchSpec.h:32
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:681
static std::vector< Region > PowerOf2Watchpoints(lldb::addr_t user_addr, size_t user_size, size_t min_byte_size, size_t max_byte_size, uint32_t address_byte_size)
Convert a user's watchpoint request into an array of Regions, each of which can be watched by a singl...
static std::vector< lldb::WatchpointResourceSP > AtomizeWatchpointRequest(lldb::addr_t addr, size_t size, bool read, bool write, WatchpointHardwareFeature supported_features, ArchSpec &arch)
Convert a user's watchpoint request into an array of memory regions, each region watched by one hardw...
#define INT32_MAX
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::WatchpointResource > WatchpointResourceSP
uint64_t addr_t
Definition lldb-types.h:80