Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pyocd/debug/breakpoints/manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pyOCD debugger
# Copyright (c) 2015-2019,2025 Arm Limited
# Copyright (c) 2021 Chris Reed
# Copyright (c) 2026 j4rvisstant
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +21,7 @@
from typing import (Dict, List, TYPE_CHECKING, Iterable, MutableSequence, Optional, Sequence, Tuple)

from .provider import Breakpoint
from ...core import exceptions
from ...core.target import Target

if TYPE_CHECKING:
Expand All @@ -46,6 +48,9 @@ class BreakpointManager:
add/remove request is recorded for later. Then, before the target is stepped or resumed, the
manager flushes breakpoint changes to the target. It is at this point when it decides which
provider to use for each new breakpoint.

A caller that must guarantee a removal has reached the target before it returns control, for
instance because the session may end without a further step or resume, calls flush_removals().
"""

## Number of hardware breakpoints to try to keep available.
Expand Down Expand Up @@ -275,6 +280,50 @@ def flush(self, is_step: bool = False) -> None:
finally:
self._ignore_notifications = False

def flush_removals(self) -> None:
"""@brief Physically remove breakpoints whose removal is pending.

Only removals are committed; pending additions stay deferred until flush(). Nothing is
done unless the core is halted, since restoring memory or clearing a comparator under a
running core is not safe; such a removal is committed by the next flush() instead.

After this returns, the owning provider has executed the removal and the probe's queued
transfers have been drained, so the change cannot still be sitting in the host queue.

@exception Error A removal could not be committed. If a provider fails to remove a
breakpoint, that breakpoint stays pending and the next flush() asks the provider
again. If draining the queued transfers fails, the removals already handed to the
providers are not retried.
"""
_, removed = self._get_updated_breakpoints()
if not removed:
return
if not self._core.is_halted():
LOG.debug("core not halted; deferring removal of %s", removed)
return

try:
# Ignore any notifications while we modify breakpoints.
self._ignore_notifications = True

LOG.debug("flushing removals=%s", removed)
for bp in removed:
assert bp.provider is not None
bp.provider.remove_breakpoint(bp)

# A provider may report failure by keeping the breakpoint rather than raising.
if bp.provider.find_breakpoint(bp.addr) is not None:
raise exceptions.DebugError("failed to remove %s breakpoint at 0x%08x"
% (bp.type.name, bp.addr))

del self._breakpoints[bp.addr]

# Flush all providers, then make sure the removals are really on the wire.
self._flush_all()
self._core.flush()
finally:
self._ignore_notifications = False

def get_breakpoint_type(self, addr: int) -> Optional[Target.BreakpointType]:
bp = self.find_breakpoint(addr)
return bp.type if (bp is not None) else None
Expand Down
26 changes: 26 additions & 0 deletions pyocd/gdbserver/gdbserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2006-2020,2025-2026 Arm Limited
# Copyright (c) 2021-2022 Chris Reed
# Copyright (c) 2022 Clay McClure
# Copyright (c) 2026 j4rvisstant
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -963,6 +964,8 @@ def breakpoint(self, client, data):
else:
LOG.debug("Command: Clear software breakpoint (addr=0x%08x)", addr)
self.target.remove_breakpoint(addr)
if not self._commit_breakpoint_removal(addr):
return self.create_rsp_packet(b'E01')
return self.create_rsp_packet(b"OK")

# handle hardware breakpoint Z1/z1
Expand All @@ -975,6 +978,8 @@ def breakpoint(self, client, data):
else:
LOG.debug("Command: Clear hardware breakpoint (addr=0x%08x)", addr)
self.target.remove_breakpoint(addr)
if not self._commit_breakpoint_removal(addr):
return self.create_rsp_packet(b'E01')
return self.create_rsp_packet(b"OK")

# handle hardware watchpoint Z2/z2/Z3/z3/Z4/z4
Expand Down Expand Up @@ -1008,6 +1013,27 @@ def breakpoint(self, client, data):
self.target.remove_watchpoint(addr, size, watchpoint_type)
return self.create_rsp_packet(b"OK")

def _commit_breakpoint_removal(self, addr: int) -> bool:
"""@brief Commit pending breakpoint removals to the target before acknowledging gdb.

A removal that is acknowledged with OK while the core is halted must already have reached
the target, so a session that ends without a further resume or step cannot leave a deleted
breakpoint armed. Pending additions stay deferred until the next resume or step. A removal
that arrives while the core is running (non-stop mode) stays pending, as before.

@return True if OK may be sent, False if the removal failed and E01 must be sent.
"""
bp_manager = getattr(self.target, 'bp_manager', None)
if bp_manager is None:
return True
try:
bp_manager.flush_removals()
return True
except exceptions.Error as e:
LOG.error("Command: Clear breakpoint (addr=0x%08x): failed to commit removal to target: %s",
addr, e, exc_info=self.session.log_tracebacks)
return False

def set_thread(self, client, data):
op = data[0:1]
thread_id = int(data[1:-3], 16)
Expand Down
Loading