Skip to content

boot: zephyr: call LOG_PANIC before jumping to app to flush in flight logs#2639

Merged
nordicjm merged 1 commit intomcu-tools:mainfrom
Tolt-Technologies:fix/swo-log-drain
Mar 12, 2026
Merged

boot: zephyr: call LOG_PANIC before jumping to app to flush in flight logs#2639
nordicjm merged 1 commit intomcu-tools:mainfrom
Tolt-Technologies:fix/swo-log-drain

Conversation

@JayToltTech
Copy link
Contributor

@JayToltTech JayToltTech commented Feb 20, 2026

Problem

Pending log messages were lost when mcuboot jumped to the app.
ZEPHYR_BOOT_LOG_STOP() drains the software log buffers, but log
backends may still have data in flight that is discarded when the
app reinitializes the hardware.

Discovered because SWO logs were lost during the transition from
bootloader to app.

Solution

Call LOG_PANIC() after ZEPHYR_BOOT_LOG_STOP(). This gives each
log backend the opportunity to finish transmitting before the jump.

SWO log fixes depend on zephyrproject-rtos/zephyr#104432 which adds
hardware drain logic to the SWO backend's panic handler.

Test plan

  • Build with CONFIG_LOG_BACKEND_SWO=y — verify all MCUboot log
    output is captured before app starts
  • Tested on nRF5340 with J-Link SWO viewer

@JayToltTech JayToltTech marked this pull request as ready for review February 20, 2026 22:49
Copilot AI review requested due to automatic review settings February 20, 2026 22:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request addresses lost or truncated log output when using Serial Wire Output (SWO) for logging in MCUboot. The issue occurs because the application reinitializes the ITM hardware on startup, discarding any data still being transmitted by the TPIU. The PR adds logic to wait for the ITM and TPIU hardware to finish transmitting before jumping to the application.

Changes:

  • Modified ZEPHYR_BOOT_LOG_STOP macro to preserve logging for non-UART backends (SWO, RTT) that don't conflict with serial recovery
  • Changed logging thread to start immediately with K_NO_WAIT instead of delaying by BOOT_LOG_PROCESSING_INTERVAL
  • Added k_wakeup call to ensure logging thread drains final messages promptly
  • Added SWO-specific drain logic that waits for ITM busy flag and adds 1ms delay for TPIU transmission

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JayToltTech JayToltTech force-pushed the fix/swo-log-drain branch 6 times, most recently from 5b1ab63 to a3468ee Compare February 21, 2026 00:56
Copy link
Collaborator

@nordicjm nordicjm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be too driver specific in my mind, logging should be enabled in minimal footprint mode, which means things are output immediately without a separate thread, so that should also apply over SWO (there might be a driver Kconfig which controls buffering)

@JayToltTech
Copy link
Contributor Author

JayToltTech commented Feb 23, 2026

That makes sense @nordicjm . So after reviewing your feedback, I think the right approach is:

  • fix Zephyr's log_backend_swo_panic() so it performs this TPIU drain (does not today)
  • Add a call to LOG_PANIC() here instead, which will trigger the flushing logic and call log_backend_swo_panic()

This seems like a general fix, and it may fix other unfound issues if people combine MCUBoot with deferred logging and other backends that also need a call to flush.

If you agree, I'll move forward with modifying this PR to call LOG_PANIC and a separate PR in the Zephyr tree to fix log_backend_swo_panic(). I'll confirm that this pair of fixes solves the problem I was seeing.

Note: in the case of SWO, I think this problem of draining TPIU will occur whether using immediate or buffered logs, because according to Zephyr, the data was 'transmitted' via ITM_SendChar() but there's still a secondary FIFO buffer flush wait that should occur before processor restart. I didn't test this specifically, but I think this is likely the case.

@JayToltTech JayToltTech changed the title boot: zephyr: wait for SWO TPIU drain before jumping to app boot: zephyr: call LOG_PANIC before jumping to app Feb 23, 2026
@JayToltTech JayToltTech changed the title boot: zephyr: call LOG_PANIC before jumping to app boot: zephyr: call LOG_PANIC before jumping to app to flush in flight logs Feb 23, 2026
@JayToltTech JayToltTech force-pushed the fix/swo-log-drain branch 3 times, most recently from 3b204a3 to cfd8535 Compare February 23, 2026 23:38
@JayToltTech JayToltTech requested a review from Copilot February 23, 2026 23:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JayToltTech JayToltTech requested a review from nordicjm February 23, 2026 23:58
@de-nordic de-nordic added the area: zephyr Affects the Zephyr port label Feb 26, 2026
@nordicjm
Copy link
Collaborator

Seems build is failing on a device

@JayToltTech JayToltTech force-pushed the fix/swo-log-drain branch 3 times, most recently from 97b42ed to 9330a33 Compare March 9, 2026 17:30
@JayToltTech
Copy link
Contributor Author

@nordicjm Fixed the build error by moving the LOG_PANIC into ZEPHYR_BOOT_LOG_STOP which is frankly a more appropriate location for the call in any case. This properly only calls LOG_PANIC if logging is enabled now.

Also fixed an issue with log_ctrl.h not being included in some log configurations that I caught along the way.

Rebased onto main.

… logs

Call LOG_PANIC() after ZEPHYR_BOOT_LOG_STOP() to give every log
backend the opportunity to finish transmitting before jumping to
the app.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
@de-nordic de-nordic requested a review from nordicjm March 11, 2026 13:17
@nordicjm nordicjm merged commit f84b9d3 into mcu-tools:main Mar 12, 2026
76 of 77 checks passed
@JayToltTech JayToltTech deleted the fix/swo-log-drain branch March 12, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: zephyr Affects the Zephyr port

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants