From af9430a8cf423e42e1cebf8c688c5cf5cc982134 Mon Sep 17 00:00:00 2001 From: adityaanikam Date: Mon, 17 Aug 2026 14:22:47 +0530 Subject: [PATCH 1/2] probe: fix J-Link swo_read crash when swo_num_bytes() returns a negative value Signed-off-by: adityaanikam --- pyocd/probe/jlink_probe.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyocd/probe/jlink_probe.py b/pyocd/probe/jlink_probe.py index f58258c86..69724ef7a 100644 --- a/pyocd/probe/jlink_probe.py +++ b/pyocd/probe/jlink_probe.py @@ -3,6 +3,7 @@ # Copyright (c) 2021-2022 Chris Reed # Copyright (c) 2023 Marian Muller Rebeyrol # Copyright (c) 2026 Christophe Dufaza +# Copyright (c) 2026 Aditya Nikam # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -498,7 +499,10 @@ def swo_stop(self): def swo_read(self): try: - return self._link.swo_read(0, self._link.swo_num_bytes(), True) + count = self._link.swo_num_bytes() + if count <= 0: + return bytearray() + return self._link.swo_read(0, count, True) except JLinkException as exc: raise self._convert_exception(exc) from exc From 75aaac0b6c5f1640e1d3244509a3e4ff34d36aa9 Mon Sep 17 00:00:00 2001 From: Aditya Nikam <118399608+adityaanikam@users.noreply.github.com> Date: Fri, 4 Sep 2026 00:50:42 +0530 Subject: [PATCH 2/2] probe: fix J-Link swo_read crash when swo_num_bytes() returns zero JLinkProbe.swo_read() passed self._link.swo_num_bytes() straight into swo_read() as the count. pylink allocates a ctypes buffer sized to that count; passing 0 (normal when there is no SWO data available) crashes with IndexError, which isn't a JLinkException and so isn't caught by the existing except clause -- it kills the SWV reader thread outright. swo_num_bytes() itself never returns a negative value (pylink validates and raises internally), so the guard only needs to cover zero. Return an empty bytearray() in that case, matching DebugProbe.swo_read's documented return type. Fixes #2019. --- pyocd/probe/jlink_probe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyocd/probe/jlink_probe.py b/pyocd/probe/jlink_probe.py index 69724ef7a..357324687 100644 --- a/pyocd/probe/jlink_probe.py +++ b/pyocd/probe/jlink_probe.py @@ -500,7 +500,7 @@ def swo_stop(self): def swo_read(self): try: count = self._link.swo_num_bytes() - if count <= 0: + if count == 0: return bytearray() return self._link.swo_read(0, count, True) except JLinkException as exc: