STACK(9)
π STACK(9) β kernel thread stack tracing routines π
okay bestie, so this is STACK(9), a β¨Kernel Developer's Manualβ¨ page and honestly it's giving "let me peek at what the kernel is doing" in the most iconic way. It's the manual for the stack KPI β the little toolkit that lets you query kernel stack trace info and auto-generate stack trace strings for debugging and tracing purposes. π
πΈ NAME πΈ
stack β kernel thread stack tracing routines π
β¨ SYNOPSIS β¨
first, the includes, no cap:
#include <sys/param.h>
#include <sys/stack.h>
then in the kernel configuration file:
options DDB
options STACK
and here's the whole squad of functions, character for character:
struct stack *
stack_create(int flags);
void
stack_destroy(struct stack *st);
int
stack_put(struct stack *st, vm_offset_t pc);
void
stack_copy(const struct stack *src, struct stack *dst);
void
stack_zero(struct stack *st);
void
stack_print(const struct stack *st);
void
stack_print_ddb(const struct stack *st);
void
stack_print_short(const struct stack *st);
void
stack_print_short_ddb(const struct stack *st);
void
stack_sbuf_print(struct sbuf *sb, const struct stack *st);
void
stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st);
void
stack_save(struct stack *st);
int
stack_save_td(struct stack *st, struct thread *td);
π¦ DESCRIPTION π¦
so the stack KPI is literally the bestie that lets you query kernel stack trace information, plus it does the automated generation of kernel stack trace strings for debugging and tracing. main character energy fr fr. π₯
heads up: to use this KPI, at least one ofoptions DDBandoptions STACKhas to be compiled into the kernel. no options, no vibes. π
every stack trace is described by a struct stack. you can declare it the usual ways β including right on the stack β and optionally initialize it with stack_zero(), though ngl that's not necessary before saving a trace. you can also dynamically allocate it with stack_create(), and the flags argument gets passed to malloc(9). π«Ά
important: that dynamic allocation must be freed with stack_destroy(). periodt.
you can capture a trace of the current thread's kernel call stack using stack_save(). and stack_save_td() is there when you want to capture the kernel stack of a caller-specified thread. π
rules forstack_save_td()besties: callers must own the thread lock of the specified thread, and the thread's stack must not be swapped out.stack_save_td()can capture the kernel stack of a running thread, though note that this is not implemented on all platforms. if the thread is running, the caller must also hold the process lock for the target thread. π
okay, now the printing functions. stack_print() and stack_print_short() may be used to print a stack trace using the kernel printf(9) β and they may sleep as a result of acquiring sx(9) locks in the kernel linker while looking up symbol names. π€
in locking-sensitive environments, the unsynchronized stack_print_ddb() and stack_print_short_ddb() variants may be invoked. this function bypasses kernel linker locking, which makes it usable in ddb(4) β but not in a live system where linker data structures may change. π
stack_sbuf_print() may be used to construct a human-readable string, including conversion (where possible) from a simple kernel instruction pointer to a named symbol and offset. the argument sb must be an initialized struct sbuf as described in sbuf(9). this function may sleep if an auto-extending struct sbuf is used, or due to kernel linker locking. π
in locking-sensitive environments, such as ddb(4), the unsynchronized stack_sbuf_print_ddb() variant may be invoked to avoid kernel linker locking β it should be used with a fixed-length sbuf. π₯
and finally, the utility functions stack_zero, stack_copy, and stack_put may be used to manipulate stack data structures directly. lowkey obsessed with how tidy that is. π
β RETURN VALUES β
stack_put() returns 0 on success. otherwise the struct stack does not contain space to record additional frames, and a non-zero value is returned. π
stack_save_td() returns 0 when the stack capture was successful and a non-zero error number otherwise. in particular:
EBUSYis returned if the thread was running in user mode at the time that the capture was attempted πEOPNOTSUPPis returned if the operation is not implemented π«Ά
π SEE ALSO π
π§ AUTHORS π§
the stack function suite was created by Antoine Brodin. stack was extended by Robert Watson for general-purpose use outside of ddb(4). iconic behavior honestly. π
and that's the whole page bestie β this one's straight out of the Debian build, dated March 6, 2022, version STACK(9). go forth and trace those stacks, you absolute icon. πΈβ¨ππ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2007-2009 Robert N. M. Watson
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms