Skip to content

Commit 97ced2f

Browse files
authored
Fix RTT Control Block Search (#1669)
1 parent 769141d commit 97ced2f

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

pyocd/debug/rtt.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright (C) 2021 Simon D. Levy <simon.d.levy@gmail.com>
55
# Copyright (C) 2022 Johan Carlsson <johan.carlsson@teenage.engineering>
66
# Copyright (c) 2022 Samuel Dewan
7+
# Copyright (c) 2024 Ein Terakawa <applause@elfmimi.jp>
78
# Copyright (C) 2023 Tejaswini Dasika <tejaswinidasika@gmail.com>
89
# Copyright (c) 2026 Arm Limited
910
# SPDX-License-Identifier: Apache-2.0
@@ -389,8 +390,8 @@ class GenericRTTControlBlock(RTTControlBlock):
389390

390391
target: SoCTarget
391392
_cb_search_address: int
392-
_cb_search_size_bytes: int
393-
_control_block_id: Sequence[int]
393+
_cb_search_size: int
394+
_control_block_id: bytes
394395

395396
def __init__(self, target: SoCTarget, address: int = None,
396397
size: int = None, control_block_id: bytes = b'SEGGER RTT'):
@@ -420,45 +421,32 @@ def __init__(self, target: SoCTarget, address: int = None,
420421
if size is None:
421422
# Address was specified, but size was not. Assume that the control
422423
# block is located exactly at the provided address.
423-
self._cb_search_size_bytes = 0
424+
self._cb_search_size = 0
424425
else:
425-
self._cb_search_size_bytes = size
426+
self._cb_search_size = size
426427
self._control_block_id = control_block_id
427428

428429
def _find_control_block(self) -> Optional[int]:
429-
addr: int = self._cb_search_address & ~0x3
430-
search_addr: int = addr
431-
search_size: int = self._cb_search_size_bytes
432-
if search_size < len(self._control_block_id):
433-
search_size = len(self._control_block_id)
434-
435-
id_len = len(self._control_block_id)
436-
offset: int = 0
437-
430+
id_bytes: bytes = self._control_block_id
431+
id_size: int = len(id_bytes)
432+
addr: int = self._cb_search_address
433+
search_size: int = max(self._cb_search_size, id_size)
434+
carry_over_size: int = (1 - id_size) // 4 * 4 # it's negative!
435+
chunk_size: int = 1024
436+
437+
data: bytes = b''
438438
while search_size:
439-
read_size = min(search_size, 32)
440-
data = self.target.read_memory_block8(search_addr, read_size)
441-
442-
if not data:
443-
break
444-
445-
for byte in data:
446-
if byte == self._control_block_id[offset]:
447-
offset += 1
448-
if offset == id_len:
449-
break
450-
else:
451-
num_skip_words = (offset + 1)
452-
addr += (num_skip_words * 1)
453-
search_size -= num_skip_words
454-
offset = 0
455-
456-
if offset == id_len:
457-
break
439+
read_size: int = min(search_size, chunk_size)
440+
prev: bytes = data[carry_over_size:]
441+
data = bytes(self.target.read_memory_block8(addr, read_size))
442+
idx: int = (prev + data).find(id_bytes)
443+
if idx >= 0:
444+
return addr - len(prev) + idx
458445

459-
search_addr += read_size
446+
addr += read_size
447+
search_size -= read_size
460448

461-
return addr if offset == id_len else None
449+
return None
462450

463451
def start(self):
464452
"""@brief Find the RTT control block on the target.

0 commit comments

Comments
 (0)