Skip to content

Commit 6517506

Browse files
committed
AArch64 debugserver parse ESR register for watchpoints
Have debugserver parse the watchpoint flags out of the exception syndrome register when we get a watchpoint mach exception. Relay those fields up to lldb in the stop reply packet, if the watchpoint number was reported by the hardware, use the address from that as the watchpoint address. Change how watchpoints are reported to lldb from using the mach exception data, to using the `reason:watchpoint` and `description:asciihex` method that lldb-server uses, which can relay the actual trap address as well as the address of a watched memory region responsible for the trap, so lldb can step past it. Have debugserver look for the nearest watchpoint that it has set when it gets a watchpoint trap, so accesses that are reported as starting before the watched region are associated with the correct watchpoint to lldb. Add a test case for this specific issue. Differential Revision: https://reviews.llvm.org/D147820 rdar://83996471 (cherry picked from commit e76cfac)
1 parent 951f936 commit 6517506

File tree

10 files changed

+362
-2
lines changed

10 files changed

+362
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C_SOURCES := main.c
2+
3+
include Makefile.rules
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Watch one byte in the middle of a doubleword, mutate the
3+
entire doubleword including the watched byte. On AArch64
4+
the trap address is probably the start of the doubleword,
5+
instead of the address of our watched byte. Test that lldb
6+
correctly associates this watchpoint trap with our watchpoint.
7+
"""
8+
9+
10+
11+
import lldb
12+
from lldbsuite.test.decorators import *
13+
from lldbsuite.test.lldbtest import *
14+
from lldbsuite.test import lldbutil
15+
16+
17+
class UnalignedWatchpointTestCase(TestBase):
18+
NO_DEBUG_INFO_TESTCASE = True
19+
@skipIfOutOfTreeDebugserver
20+
def test_unaligned_watchpoint(self):
21+
"""Test an unaligned watchpoint triggered by a larger aligned write."""
22+
self.build()
23+
self.main_source_file = lldb.SBFileSpec("main.c")
24+
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
25+
"break here", self.main_source_file)
26+
27+
frame = thread.GetFrameAtIndex(0)
28+
29+
self.expect("watchpoint set variable a.buf[2]")
30+
31+
self.runCmd("process continue")
32+
33+
# We should be stopped again due to the watchpoint (write type), but
34+
# only once. The stop reason of the thread should be watchpoint.
35+
self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
36+
substrs=['stopped',
37+
'stop reason = watchpoint'])
38+
39+
# Use the '-v' option to do verbose listing of the watchpoint.
40+
# The hit count should now be 1.
41+
self.expect("watchpoint list -v",
42+
substrs=['hit_count = 1'])
43+
44+
self.runCmd("process continue")
45+
46+
# We should be stopped again due to the watchpoint (write type), but
47+
# only once. The stop reason of the thread should be watchpoint.
48+
self.expect("thread list", STOPPED_DUE_TO_WATCHPOINT,
49+
substrs=['stopped',
50+
'stop reason = watchpoint'])
51+
52+
# Use the '-v' option to do verbose listing of the watchpoint.
53+
# The hit count should now be 1.
54+
self.expect("watchpoint list -v",
55+
substrs=['hit_count = 2'])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
int main() {
4+
union {
5+
uint8_t buf[8];
6+
uint64_t val;
7+
} a;
8+
a.val = 0; // break here
9+
for (int i = 0; i < 5; i++) {
10+
a.val = i;
11+
printf("a.val is %lu\n", a.val);
12+
}
13+
}

lldb/tools/debugserver/source/DNBBreakpoint.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ const DNBBreakpoint *DNBBreakpointList::FindByAddress(nub_addr_t addr) const {
8282
return NULL;
8383
}
8484

85+
const DNBBreakpoint *
86+
DNBBreakpointList::FindByHardwareIndex(uint32_t idx) const {
87+
for (const auto &pos : m_breakpoints)
88+
if (pos.second.GetHardwareIndex() == idx)
89+
return &pos.second;
90+
91+
return nullptr;
92+
}
93+
94+
const DNBBreakpoint *
95+
DNBBreakpointList::FindNearestWatchpoint(nub_addr_t addr) const {
96+
// Exact match
97+
for (const auto &pos : m_breakpoints) {
98+
if (pos.second.IsEnabled()) {
99+
nub_addr_t start_addr = pos.second.Address();
100+
nub_addr_t end_addr = start_addr + pos.second.ByteSize();
101+
if (addr >= start_addr && addr <= end_addr)
102+
return &pos.second;
103+
}
104+
}
105+
106+
// Find watchpoint nearest to this address
107+
// before or after the watched region of memory
108+
const DNBBreakpoint *closest = nullptr;
109+
uint32_t best_match = UINT32_MAX;
110+
for (const auto &pos : m_breakpoints) {
111+
if (pos.second.IsEnabled()) {
112+
nub_addr_t start_addr = pos.second.Address();
113+
nub_addr_t end_addr = start_addr + pos.second.ByteSize();
114+
uint32_t delta = addr < start_addr ? start_addr - addr : addr - end_addr;
115+
if (delta < best_match) {
116+
closest = &pos.second;
117+
best_match = delta;
118+
}
119+
}
120+
}
121+
return closest;
122+
}
123+
85124
// Finds the next breakpoint at an address greater than or equal to "addr"
86125
size_t DNBBreakpointList::FindBreakpointsThatOverlapRange(
87126
nub_addr_t addr, nub_addr_t size, std::vector<DNBBreakpoint *> &bps) {

lldb/tools/debugserver/source/DNBBreakpoint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ class DNBBreakpointList {
122122
DNBBreakpoint *Add(nub_addr_t addr, nub_size_t length, bool hardware);
123123
bool Remove(nub_addr_t addr);
124124
DNBBreakpoint *FindByAddress(nub_addr_t addr);
125+
const DNBBreakpoint *FindNearestWatchpoint(nub_addr_t addr) const;
125126
const DNBBreakpoint *FindByAddress(nub_addr_t addr) const;
127+
const DNBBreakpoint *FindByHardwareIndex(uint32_t idx) const;
126128

127129
size_t FindBreakpointsThatOverlapRange(nub_addr_t addr, nub_addr_t size,
128130
std::vector<DNBBreakpoint *> &bps);

lldb/tools/debugserver/source/DNBDefs.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ enum DNBThreadStopType {
234234
eStopTypeInvalid = 0,
235235
eStopTypeSignal,
236236
eStopTypeException,
237-
eStopTypeExec
237+
eStopTypeExec,
238+
eStopTypeWatchpoint
238239
};
239240

240241
enum DNBMemoryPermissions {
@@ -264,6 +265,37 @@ struct DNBThreadStopInfo {
264265
nub_size_t data_count;
265266
nub_addr_t data[DNB_THREAD_STOP_INFO_MAX_EXC_DATA];
266267
} exception;
268+
269+
// eStopTypeWatchpoint
270+
struct {
271+
// The trigger address from the mach exception
272+
// (likely the contents of the FAR register)
273+
nub_addr_t mach_exception_addr;
274+
275+
// The trigger address, adjusted to be the start
276+
// address of one of the existing watchpoints for
277+
// lldb's benefit.
278+
nub_addr_t addr;
279+
280+
// The watchpoint hardware index.
281+
uint32_t hw_idx;
282+
283+
// If the esr_fields bitfields have been filled in.
284+
bool esr_fields_set;
285+
struct {
286+
uint32_t
287+
iss; // "ISS encoding for an exception from a Watchpoint exception"
288+
uint32_t wpt; // Watchpoint number
289+
bool wptv; // Watchpoint number Valid
290+
bool wpf; // Watchpoint might be false-positive
291+
bool fnp; // FAR not Precise
292+
bool vncr; // watchpoint from use of VNCR_EL2 reg by EL1
293+
bool fnv; // FAR not Valid
294+
bool cm; // Cache maintenance
295+
bool wnr; // Write not Read
296+
uint32_t dfsc; // Data Fault Status Code
297+
} esr_fields;
298+
} watchpoint;
267299
} details;
268300
};
269301

lldb/tools/debugserver/source/MacOSX/MachException.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ bool MachException::Data::GetStopInfo(
158158
return true;
159159
}
160160

161+
#if defined(__arm64__) || defined(__aarch64__)
162+
if (exc_type == EXC_BREAKPOINT && exc_data[0] == EXC_ARM_DA_DEBUG &&
163+
exc_data.size() > 1) {
164+
stop_info->reason = eStopTypeWatchpoint;
165+
stop_info->details.watchpoint.mach_exception_addr = exc_data[1];
166+
stop_info->details.watchpoint.addr = INVALID_NUB_ADDRESS;
167+
if (exc_data.size() > 2) {
168+
stop_info->details.watchpoint.hw_idx = exc_data[2];
169+
}
170+
return true;
171+
}
172+
#endif
173+
161174
// We always stop with a mach exceptions
162175
stop_info->reason = eStopTypeException;
163176
// Save the EXC_XXXX exception type

lldb/tools/debugserver/source/MacOSX/MachProcess.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ class MachProcess {
383383
void PrivateResume();
384384
void StopProfileThread();
385385

386+
void RefineWatchpointStopInfo(nub_thread_t tid,
387+
struct DNBThreadStopInfo *stop_info);
388+
386389
uint32_t Flags() const { return m_flags; }
387390
nub_state_t DoSIGSTOP(bool clear_bps_and_wps, bool allow_running,
388391
uint32_t *thread_idx_ptr);

lldb/tools/debugserver/source/MacOSX/MachProcess.mm

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,8 @@ static bool mach_header_validity_test(uint32_t magic, uint32_t cputype) {
12771277
if (m_thread_list.GetThreadStoppedReason(tid, stop_info)) {
12781278
if (m_did_exec)
12791279
stop_info->reason = eStopTypeExec;
1280+
if (stop_info->reason == eStopTypeWatchpoint)
1281+
RefineWatchpointStopInfo(tid, stop_info);
12801282
return true;
12811283
}
12821284
return false;
@@ -1420,6 +1422,135 @@ static bool mach_header_validity_test(uint32_t magic, uint32_t cputype) {
14201422
m_profile_events.ResetEvents(eMachProcessProfileCancel);
14211423
}
14221424

1425+
/// return 1 if bit position \a bit is set in \a value
1426+
static uint32_t bit(uint32_t value, uint32_t bit) {
1427+
return (value >> bit) & 1u;
1428+
}
1429+
1430+
// return the bitfield "value[msbit:lsbit]".
1431+
static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
1432+
assert(msbit >= lsbit);
1433+
uint64_t shift_left = sizeof(value) * 8 - 1 - msbit;
1434+
value <<=
1435+
shift_left; // shift anything above the msbit off of the unsigned edge
1436+
value >>= shift_left + lsbit; // shift it back again down to the lsbit
1437+
// (including undoing any shift from above)
1438+
return value; // return our result
1439+
}
1440+
1441+
void MachProcess::RefineWatchpointStopInfo(
1442+
nub_thread_t tid, struct DNBThreadStopInfo *stop_info) {
1443+
const DNBBreakpoint *wp = m_watchpoints.FindNearestWatchpoint(
1444+
stop_info->details.watchpoint.mach_exception_addr);
1445+
if (wp) {
1446+
stop_info->details.watchpoint.addr = wp->Address();
1447+
stop_info->details.watchpoint.hw_idx = wp->GetHardwareIndex();
1448+
DNBLogThreadedIf(LOG_WATCHPOINTS,
1449+
"MachProcess::RefineWatchpointStopInfo "
1450+
"mach exception addr 0x%llx moved in to nearest "
1451+
"watchpoint, 0x%llx-0x%llx",
1452+
stop_info->details.watchpoint.mach_exception_addr,
1453+
wp->Address(), wp->Address() + wp->ByteSize() - 1);
1454+
} else {
1455+
stop_info->details.watchpoint.addr =
1456+
stop_info->details.watchpoint.mach_exception_addr;
1457+
}
1458+
1459+
stop_info->details.watchpoint.esr_fields_set = false;
1460+
std::optional<uint64_t> esr, far;
1461+
nub_size_t num_reg_sets = 0;
1462+
const DNBRegisterSetInfo *reg_sets = GetRegisterSetInfo(tid, &num_reg_sets);
1463+
for (nub_size_t set = 0; set < num_reg_sets; set++) {
1464+
if (reg_sets[set].registers == NULL)
1465+
continue;
1466+
for (uint32_t reg = 0; reg < reg_sets[set].num_registers; ++reg) {
1467+
if (strcmp(reg_sets[set].registers[reg].name, "esr") == 0) {
1468+
DNBRegisterValue reg_value;
1469+
if (GetRegisterValue(tid, set, reg, &reg_value)) {
1470+
esr = reg_value.value.uint64;
1471+
}
1472+
}
1473+
if (strcmp(reg_sets[set].registers[reg].name, "far") == 0) {
1474+
DNBRegisterValue reg_value;
1475+
if (GetRegisterValue(tid, set, reg, &reg_value)) {
1476+
far = reg_value.value.uint64;
1477+
}
1478+
}
1479+
}
1480+
}
1481+
1482+
if (esr && far) {
1483+
if (*far != stop_info->details.watchpoint.mach_exception_addr) {
1484+
// AFAIK the kernel is going to put the FAR value in the mach
1485+
// exception, if they don't match, it's interesting enough to log it.
1486+
DNBLogThreadedIf(LOG_WATCHPOINTS,
1487+
"MachProcess::RefineWatchpointStopInfo mach exception "
1488+
"addr 0x%llx but FAR register has value 0x%llx",
1489+
stop_info->details.watchpoint.mach_exception_addr, *far);
1490+
}
1491+
uint32_t exception_class = bits(*esr, 31, 26);
1492+
1493+
// "Watchpoint exception from a lower Exception level"
1494+
if (exception_class == 0b110100) {
1495+
stop_info->details.watchpoint.esr_fields_set = true;
1496+
// Documented in the ARM ARM A-Profile Dec 2022 edition
1497+
// Section D17.2 ("General system control registers"),
1498+
// Section D17.2.37 "ESR_EL1, Exception Syndrome Register (EL1)",
1499+
// "Field Descriptions"
1500+
// "ISS encoding for an exception from a Watchpoint exception"
1501+
uint32_t iss = bits(*esr, 23, 0);
1502+
stop_info->details.watchpoint.esr_fields.iss = iss;
1503+
stop_info->details.watchpoint.esr_fields.wpt =
1504+
bits(iss, 23, 18); // Watchpoint number
1505+
stop_info->details.watchpoint.esr_fields.wptv =
1506+
bit(iss, 17); // Watchpoint number Valid
1507+
stop_info->details.watchpoint.esr_fields.wpf =
1508+
bit(iss, 16); // Watchpoint might be false-positive
1509+
stop_info->details.watchpoint.esr_fields.fnp =
1510+
bit(iss, 15); // FAR not Precise
1511+
stop_info->details.watchpoint.esr_fields.vncr =
1512+
bit(iss, 13); // watchpoint from use of VNCR_EL2 reg by EL1
1513+
stop_info->details.watchpoint.esr_fields.fnv =
1514+
bit(iss, 10); // FAR not Valid
1515+
stop_info->details.watchpoint.esr_fields.cm =
1516+
bit(iss, 6); // Cache maintenance
1517+
stop_info->details.watchpoint.esr_fields.wnr =
1518+
bit(iss, 6); // Write not Read
1519+
stop_info->details.watchpoint.esr_fields.dfsc =
1520+
bits(iss, 5, 0); // Data Fault Status Code
1521+
1522+
DNBLogThreadedIf(LOG_WATCHPOINTS,
1523+
"ESR watchpoint fields parsed: "
1524+
"iss = 0x%x, wpt = %u, wptv = %d, wpf = %d, fnp = %d, "
1525+
"vncr = %d, fnv = %d, cm = %d, wnr = %d, dfsc = 0x%x",
1526+
stop_info->details.watchpoint.esr_fields.iss,
1527+
stop_info->details.watchpoint.esr_fields.wpt,
1528+
stop_info->details.watchpoint.esr_fields.wptv,
1529+
stop_info->details.watchpoint.esr_fields.wpf,
1530+
stop_info->details.watchpoint.esr_fields.fnp,
1531+
stop_info->details.watchpoint.esr_fields.vncr,
1532+
stop_info->details.watchpoint.esr_fields.fnv,
1533+
stop_info->details.watchpoint.esr_fields.cm,
1534+
stop_info->details.watchpoint.esr_fields.wnr,
1535+
stop_info->details.watchpoint.esr_fields.dfsc);
1536+
1537+
if (stop_info->details.watchpoint.esr_fields.wptv) {
1538+
DNBLogThreadedIf(LOG_WATCHPOINTS,
1539+
"Watchpoint Valid field true, "
1540+
"finding startaddr of watchpoint %d",
1541+
stop_info->details.watchpoint.esr_fields.wpt);
1542+
stop_info->details.watchpoint.hw_idx =
1543+
stop_info->details.watchpoint.esr_fields.wpt;
1544+
const DNBBreakpoint *wp = m_watchpoints.FindByHardwareIndex(
1545+
stop_info->details.watchpoint.esr_fields.wpt);
1546+
if (wp) {
1547+
stop_info->details.watchpoint.addr = wp->Address();
1548+
}
1549+
}
1550+
}
1551+
}
1552+
}
1553+
14231554
bool MachProcess::StartProfileThread() {
14241555
DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( )", __FUNCTION__);
14251556
// Create the thread that profiles the inferior and reports back if enabled

0 commit comments

Comments
 (0)