blob: 50e2df30b0aa316d056953710852c2b4d8caed59 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_STACKTRACE_H
3#define __LINUX_STACKTRACE_H
4
5#include <linux/types.h>
David Brazdil0f672f62019-12-10 10:32:29 +00006#include <asm/errno.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007
8struct task_struct;
9struct pt_regs;
10
11#ifdef CONFIG_STACKTRACE
David Brazdil0f672f62019-12-10 10:32:29 +000012void stack_trace_print(const unsigned long *trace, unsigned int nr_entries,
13 int spaces);
14int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
15 unsigned int nr_entries, int spaces);
16unsigned int stack_trace_save(unsigned long *store, unsigned int size,
17 unsigned int skipnr);
18unsigned int stack_trace_save_tsk(struct task_struct *task,
19 unsigned long *store, unsigned int size,
20 unsigned int skipnr);
21unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
22 unsigned int size, unsigned int skipnr);
23unsigned int stack_trace_save_user(unsigned long *store, unsigned int size);
24
25/* Internal interfaces. Do not use in generic code */
26#ifdef CONFIG_ARCH_STACKWALK
27
28/**
29 * stack_trace_consume_fn - Callback for arch_stack_walk()
30 * @cookie: Caller supplied pointer handed back by arch_stack_walk()
31 * @addr: The stack entry address to consume
David Brazdil0f672f62019-12-10 10:32:29 +000032 *
33 * Return: True, if the entry was consumed or skipped
34 * False, if there is no space left to store
35 */
Olivier Deprez157378f2022-04-04 15:47:50 +020036typedef bool (*stack_trace_consume_fn)(void *cookie, unsigned long addr);
David Brazdil0f672f62019-12-10 10:32:29 +000037/**
38 * arch_stack_walk - Architecture specific function to walk the stack
39 * @consume_entry: Callback which is invoked by the architecture code for
40 * each entry.
41 * @cookie: Caller supplied pointer which is handed back to
42 * @consume_entry
43 * @task: Pointer to a task struct, can be NULL
44 * @regs: Pointer to registers, can be NULL
45 *
46 * ============ ======= ============================================
47 * task regs
48 * ============ ======= ============================================
49 * task NULL Stack trace from task (can be current)
50 * current regs Stack trace starting on regs->stackpointer
51 * ============ ======= ============================================
52 */
53void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
54 struct task_struct *task, struct pt_regs *regs);
55int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie,
56 struct task_struct *task);
57void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
58 const struct pt_regs *regs);
59
60#else /* CONFIG_ARCH_STACKWALK */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061struct stack_trace {
62 unsigned int nr_entries, max_entries;
63 unsigned long *entries;
Olivier Deprez157378f2022-04-04 15:47:50 +020064 unsigned int skip; /* input argument: How many entries to skip */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065};
66
67extern void save_stack_trace(struct stack_trace *trace);
68extern void save_stack_trace_regs(struct pt_regs *regs,
69 struct stack_trace *trace);
70extern void save_stack_trace_tsk(struct task_struct *tsk,
71 struct stack_trace *trace);
72extern int save_stack_trace_tsk_reliable(struct task_struct *tsk,
73 struct stack_trace *trace);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074extern void save_stack_trace_user(struct stack_trace *trace);
David Brazdil0f672f62019-12-10 10:32:29 +000075#endif /* !CONFIG_ARCH_STACKWALK */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076#endif /* CONFIG_STACKTRACE */
77
David Brazdil0f672f62019-12-10 10:32:29 +000078#if defined(CONFIG_STACKTRACE) && defined(CONFIG_HAVE_RELIABLE_STACKTRACE)
79int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
80 unsigned int size);
81#else
82static inline int stack_trace_save_tsk_reliable(struct task_struct *tsk,
83 unsigned long *store,
84 unsigned int size)
85{
86 return -ENOSYS;
87}
88#endif
89
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090#endif /* __LINUX_STACKTRACE_H */