Printing Stack Traces in the Linux Kernel

EN

When something goes wrong in the kernel, the first thing I usually want to know is how the code got there. A stack trace tells you that. This post is my notes on the few ways to print one, and what you need in the kernel config to get useful output.

The kernel stack, briefly

Every task has its own kernel stack, separate from its user-space stack. It is small and has a fixed size: 16KB on x86_64 and arm64, 8KB on 32-bit ARM. There is no room to grow, so deep recursion or big local arrays in kernel code can overflow it.

With CONFIG_VMAP_STACK=y the stack is allocated from vmalloc space with a guard page next to it. An overflow then hits the guard page and you get a clean oops, instead of silently corrupting whatever memory sits next to the stack.

dump_stack()

This is the one I use most. It prints the stack of the current task to the kernel log.

#include <linux/printk.h>

void dump_stack(void);

Put it where you want to see the call path:

if (unexpected_condition) {
    pr_err("unexpected condition\n");
    dump_stack();
}

The output starts with the CPU, PID, command name and kernel version, then the call trace. On x86_64 it looks roughly like this (trimmed):

CPU: 2 PID: 1234 Comm: insmod Tainted: G           O       6.1.0 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996)
Call Trace:
 <TASK>
 dump_stack_lvl+0x48/0x60
 trace_test_init+0x15/0x1000 [trace_test]
 do_one_initcall+0x5a/0x220
 do_init_module+0x4a/0x1f0
 ...
 </TASK>

Read it from top to bottom: the top line is where dump_stack() was called, each line below is the caller of the line above.

If you also want a warning in the log and a taint flag, WARN_ON(cond) prints a stack trace when cond is true. That is often the better choice for “this should never happen” checks.

show_stack()

show_stack() prints the stack of a given task, not only the current one.

void show_stack(struct task_struct *task, unsigned long *sp,
                const char *loglvl);

Pass NULL for the task and stack pointer to get the current task:

show_stack(NULL, NULL, KERN_INFO);

The loglvl argument was added in 5.9. Older kernels only take the first two arguments.

Printing the caller with %pS

Sometimes I don’t need the whole trace, only who called a function. printk can turn an address into a symbol name with %pS:

pr_info("called from %pS\n", __builtin_return_address(0));

This prints something like called from my_probe+0x3c/0x120 [my_driver]. Old code used print_symbol() for this, but that function has been removed from the kernel.

Saving a trace and printing it later

If you want to capture a trace now and print it later, for example to keep the last few traces in a buffer, use the stacktrace API:

#include <linux/stacktrace.h>

unsigned long entries[16];
unsigned int nr;

nr = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
/* ... later ... */
stack_trace_print(entries, nr, 0);

The third argument of stack_trace_save() is how many frames to skip from the top. The third argument of stack_trace_print() is how many spaces to indent each line.

Small test module

This is the module I use to check that traces work on a new kernel or board:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>

static int __init trace_test_init(void)
{
    pr_info("trace_test loaded, dumping stack\n");
    dump_stack();
    return 0;
}

static void __exit trace_test_exit(void)
{
    pr_info("trace_test unloaded\n");
}

module_init(trace_test_init);
module_exit(trace_test_exit);
MODULE_LICENSE("GPL");

Kernel config

These are the options that matter for readable traces:

CONFIG_KALLSYMS=y        # function names instead of raw addresses
CONFIG_KALLSYMS_ALL=y    # also include data symbols
CONFIG_STACKTRACE=y      # needed for the stack_trace_save() API
CONFIG_VMAP_STACK=y      # guard page, catches stack overflow

How the kernel walks the stack depends on the architecture. On x86_64 the default unwinder is ORC (CONFIG_UNWINDER_ORC), which uses tables generated at build time and does not need frame pointers. You can switch to CONFIG_UNWINDER_FRAME_POINTER if you want. On arm64 and RISC-V the unwinder follows frame pointers. If a trace looks wrong or cut short, check the unwinder first. The code lives in arch/<arch>/kernel/, usually in dumpstack.c, stacktrace.c or traps.c.

A note on production systems

Stack traces show kernel addresses, which helps an attacker get around KASLR. On production systems you usually restrict this with kernel.dmesg_restrict=1 (or CONFIG_SECURITY_DMESG_RESTRICT=y) so only root can read the log, and kernel.kptr_restrict to hide raw pointers printed with %pK.