kernel: avoid divide-by-zero in thread_get_debug_info() for zero-size stacks

thread_get_debug_info() computes the current stack usage as
stack_used_current * 100 / thread->stack_size with no guard against a zero
stack size. On hosted targets the main thread has stack_size == 0, so
opening the "View OS stacks" debug screen divides by zero and panics with a
floating point exception.

Guard the division and report 0% when the stack size is unknown, which
matches the stack_usage() helper.

Likely affects all hosted non-SDL targets, but tested/confirmed on my
HiBy R1.

Change-Id: I8ecc56f53f1e97e8a59ca71c0e08a66093fecbae
This commit is contained in:
Michael McAllister 2026-07-21 22:08:24 +01:00 • committed by Solomon Peachy
parent 91774611cd
commit 8acd1c14d6

View file

@ -309,7 +309,10 @@ int thread_get_debug_info(unsigned int thread_id,
size_t stack_used_current =
thread->stack_size - (thread->context.sp - (uintptr_t)thread->stack);
infop->stack_usage_cur = stack_used_current * 100 / thread->stack_size;
/* Hosted implementations typically have the main stack managed
by the OS, so we create a fake entry with 0 length */
infop->stack_usage_cur = thread->stack_size ?
stack_used_current * 100 / thread->stack_size : 0;
#endif
#if NUM_CORES > 1
infop->core = thread->core;