From 6ca507b07d5e8ef5d71c9e5748d4ce03cec4384f Mon Sep 17 00:00:00 2001 From: Jan Matyas Date: Tue, 7 Jan 2025 12:06:49 +0100 Subject: [PATCH] RISC-V Semihosting 3 of 3: Warn if encountered but disabled If semihosting is disabled but there is a semihosting request encountered in the program, provide a clear hint to the user what happened and what can be done about it. Change-Id: I8fa7b821ca9a853cbc884f38d138fa5c8946c84c Signed-off-by: Jan Matyas --- src/target/riscv/riscv_semihosting.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/target/riscv/riscv_semihosting.c b/src/target/riscv/riscv_semihosting.c index af8ec29af..4fc43f17b 100644 --- a/src/target/riscv/riscv_semihosting.c +++ b/src/target/riscv/riscv_semihosting.c @@ -110,11 +110,6 @@ enum semihosting_result riscv_semihosting(struct target *target, int *retval) struct semihosting *semihosting = target->semihosting; assert(semihosting); - if (!semihosting->is_active) { - LOG_TARGET_DEBUG(target, "Semihosting outcome: NONE (semihosting not enabled)"); - return SEMIHOSTING_NONE; - } - riscv_reg_t pc; int result = riscv_reg_get(target, &pc, GDB_REGNO_PC); if (result != ERROR_OK) { @@ -122,20 +117,37 @@ enum semihosting_result riscv_semihosting(struct target *target, int *retval) return SEMIHOSTING_ERROR; } - bool sequence_found; + bool sequence_found = false; *retval = riscv_semihosting_detect_magic_sequence(target, pc, &sequence_found); if (*retval != ERROR_OK) { LOG_TARGET_DEBUG(target, "Semihosting outcome: ERROR (during magic seq. detection)"); return SEMIHOSTING_ERROR; } + if (!semihosting->is_active) { + if (sequence_found) { + // If semihositing is encountered but disabled, provide an additional hint to the user. + LOG_TARGET_WARNING(target, "RISC-V semihosting call encountered in the program " + "but semihosting is disabled!"); + LOG_TARGET_WARNING(target, "The target will remain halted (PC = 0x%" TARGET_PRIxADDR ").", pc); + LOG_TARGET_WARNING(target, "Hint: Restart your debug session and enable semihosting " + "by command 'arm semihosting enable'."); + // TODO: This can be improved: If riscv_semihosting() is called also + // from "arm semihosting enable", the user will not be required to restart + // the debug session. + } + + LOG_TARGET_DEBUG(target, "Semihosting outcome: NONE (semihosting not enabled)"); + return SEMIHOSTING_NONE; + } + if (!sequence_found) { LOG_TARGET_DEBUG(target, "Semihosting outcome: NONE (no magic sequence)"); return SEMIHOSTING_NONE; } /* Otherwise we have a semihosting call (and semihosting is enabled). - * Proceed with the semihosting. */ + * Proceed with the handling of semihosting. */ /* * Perform semihosting call if we are not waiting on a fileio