| 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 | |
| 9 | #include "lldb/Breakpoint/WatchpointAlgorithms.h" |
| 10 | #include "lldb/Breakpoint/WatchpointResource.h" |
| 11 | #include "lldb/Target/Process.h" |
| 12 | #include "lldb/Utility/ArchSpec.h" |
| 13 | #include "lldb/Utility/LLDBLog.h" |
| 14 | #include "lldb/Utility/Log.h" |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | std::vector<WatchpointResourceSP> |
| 24 | WatchpointAlgorithms::AtomizeWatchpointRequest( |
| 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(user_addr: addr, user_size: 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(user_addr: addr, user_size: size, |
| 41 | /*min_byte_size*/ 1, |
| 42 | /*max_byte_size*/ arch.GetAddressByteSize(), |
| 43 | /*address_byte_size*/ arch.GetAddressByteSize()); |
| 44 | } |
| 45 | |
| 46 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 47 | LLDB_LOGV(log, "AtomizeWatchpointRequest user request addr {0:x} size {1}" , |
| 48 | addr, size); |
| 49 | std::vector<WatchpointResourceSP> resources; |
| 50 | for (Region &ent : entries) { |
| 51 | LLDB_LOGV(log, "AtomizeWatchpointRequest creating resource {0:x} size {1}" , |
| 52 | ent.addr, ent.size); |
| 53 | WatchpointResourceSP wp_res_sp = |
| 54 | std::make_shared<WatchpointResource>(args&: ent.addr, args&: ent.size, args&: read, args&: write); |
| 55 | resources.push_back(x: wp_res_sp); |
| 56 | } |
| 57 | |
| 58 | return resources; |
| 59 | } |
| 60 | |
| 61 | /// Convert a user's watchpoint request (\a user_addr and \a user_size) |
| 62 | /// into hardware watchpoints, for a target that can watch a power-of-2 |
| 63 | /// region of memory (1, 2, 4, 8, etc), aligned to that same power-of-2 |
| 64 | /// memory address. |
| 65 | /// |
| 66 | /// If a user asks to watch 4 bytes at address 0x1002 (0x1002-0x1005 |
| 67 | /// inclusive) we can implement this with two 2-byte watchpoints |
| 68 | /// (0x1002 and 0x1004) or with an 8-byte watchpoint at 0x1000. |
| 69 | /// A 4-byte watchpoint at 0x1002 would not be properly 4 byte aligned. |
| 70 | /// |
| 71 | /// If a user asks to watch 16 bytes at 0x1000, and this target supports |
| 72 | /// 8-byte watchpoints, we can implement this with two 8-byte watchpoints |
| 73 | /// at 0x1000 and 0x1008. |
| 74 | std::vector<WatchpointAlgorithms::Region> |
| 75 | WatchpointAlgorithms::PowerOf2Watchpoints(addr_t user_addr, size_t user_size, |
| 76 | size_t min_byte_size, |
| 77 | size_t max_byte_size, |
| 78 | uint32_t address_byte_size) { |
| 79 | |
| 80 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 81 | LLDB_LOGV(log, |
| 82 | "AtomizeWatchpointRequest user request addr {0:x} size {1} " |
| 83 | "min_byte_size {2}, max_byte_size {3}, address_byte_size {4}" , |
| 84 | user_addr, user_size, min_byte_size, max_byte_size, |
| 85 | address_byte_size); |
| 86 | |
| 87 | // Can't watch zero bytes. |
| 88 | if (user_size == 0) |
| 89 | return {}; |
| 90 | |
| 91 | size_t aligned_size = std::max(a: user_size, b: min_byte_size); |
| 92 | /// Round up \a user_size to the next power-of-2 size |
| 93 | /// user_size == 8 -> aligned_size == 8 |
| 94 | /// user_size == 9 -> aligned_size == 16 |
| 95 | aligned_size = llvm::bit_ceil(Value: aligned_size); |
| 96 | |
| 97 | addr_t aligned_start = user_addr & ~(aligned_size - 1); |
| 98 | |
| 99 | // Does this power-of-2 memory range, aligned to power-of-2 that the |
| 100 | // hardware can watch, completely cover the requested region. |
| 101 | if (aligned_size <= max_byte_size && |
| 102 | aligned_start + aligned_size >= user_addr + user_size) |
| 103 | return {{.addr: aligned_start, .size: aligned_size}}; |
| 104 | |
| 105 | // If the maximum region we can watch is larger than the aligned |
| 106 | // size, try increasing the region size by one power of 2 and see |
| 107 | // if aligning to that amount can cover the requested region. |
| 108 | // |
| 109 | // Increasing the aligned_size repeatedly instead of splitting the |
| 110 | // watchpoint can result in us watching large regions of memory |
| 111 | // unintentionally when we could use small two watchpoints. e.g. |
| 112 | // user_addr 0x3ff8 user_size 32 |
| 113 | // can be watched with four 8-byte watchpoints or if it's done with one |
| 114 | // MASK watchpoint, it would need to be a 32KB watchpoint (a 16KB |
| 115 | // watchpoint at 0x0 only covers 0x0000-0x4000). A user request |
| 116 | // at the end of a power-of-2 region can lead to these undesirably |
| 117 | // large watchpoints and many false positive hits to ignore. |
| 118 | if (max_byte_size >= (aligned_size << 1)) { |
| 119 | aligned_size <<= 1; |
| 120 | aligned_start = user_addr & ~(aligned_size - 1); |
| 121 | if (aligned_size <= max_byte_size && |
| 122 | aligned_start + aligned_size >= user_addr + user_size) |
| 123 | return {{.addr: aligned_start, .size: aligned_size}}; |
| 124 | |
| 125 | // Go back to our original aligned size, to try the multiple |
| 126 | // watchpoint approach. |
| 127 | aligned_size >>= 1; |
| 128 | } |
| 129 | |
| 130 | // We need to split the user's watchpoint into two or more watchpoints |
| 131 | // that can be monitored by hardware, because of alignment and/or size |
| 132 | // reasons. |
| 133 | aligned_size = std::min(a: aligned_size, b: max_byte_size); |
| 134 | aligned_start = user_addr & ~(aligned_size - 1); |
| 135 | |
| 136 | std::vector<Region> result; |
| 137 | addr_t current_address = aligned_start; |
| 138 | const addr_t user_end_address = user_addr + user_size; |
| 139 | while (current_address + aligned_size < user_end_address) { |
| 140 | result.push_back(x: {.addr: current_address, .size: aligned_size}); |
| 141 | current_address += aligned_size; |
| 142 | } |
| 143 | |
| 144 | if (current_address < user_end_address) |
| 145 | result.push_back(x: {.addr: current_address, .size: aligned_size}); |
| 146 | |
| 147 | return result; |
| 148 | } |
| 149 | |