zephyr/include/zephyr/debug/stack.h
Michele Sardo 1ce6a26f70 debug: stack: include kernel.h for k_thread API usage
The debug/stack.h header uses k_thread types and APIs such as
struct k_thread, k_thread_stack_space_get(), and k_thread_name_get(),
but does not include <zephyr/kernel.h>.

This makes the header rely on transitive includes, which can lead to
build failures depending on include order.

Add an explicit include of <zephyr/kernel.h> to make the header
self-contained.

Signed-off-by: Michele Sardo <msmttchr@gmail.com>
2026-01-06 16:04:15 +00:00

46 lines
1.1 KiB
C

/**
* @file debug/stack.h
* Stack usage analysis helpers
*/
/*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DEBUG_STACK_H_
#define ZEPHYR_INCLUDE_DEBUG_STACK_H_
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
#include <zephyr/toolchain.h>
#include <stdbool.h>
static inline void log_stack_usage(const struct k_thread *thread)
{
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
size_t unused, size = thread->stack_info.size;
TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_SHADOW);
LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_SHADOW);
if (k_thread_stack_space_get(thread, &unused) == 0) {
unsigned int pcnt = ((size - unused) * 100U) / size;
const char *tname;
tname = k_thread_name_get((k_tid_t)thread);
if (tname == NULL) {
tname = "unknown";
}
LOG_INF("%p (%s):\tunused %zu\tusage %zu / %zu (%u %%)",
thread, tname, unused, size - unused, size,
pcnt);
}
#else
ARG_UNUSED(thread);
#endif
}
#endif /* ZEPHYR_INCLUDE_DEBUG_STACK_H_ */