1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_STACKTRACE_H
3 #define __LINUX_STACKTRACE_H
4 
5 #include <linux/types.h>
6 #include <asm/errno.h>
7 
8 struct task_struct;
9 struct pt_regs;
10 
11 #ifdef CONFIG_STACKTRACE
12 void stack_trace_print(const unsigned long *trace, unsigned int nr_entries,
13 		       int spaces);
14 int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
15 			unsigned int nr_entries, int spaces);
16 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
17 			      unsigned int skipnr);
18 unsigned int stack_trace_save_tsk(struct task_struct *task,
19 				  unsigned long *store, unsigned int size,
20 				  unsigned int skipnr);
21 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
22 				   unsigned int size, unsigned int skipnr);
23 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size);
24 unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries);
25 
26 /* Internal interfaces. Do not use in generic code */
27 #ifdef CONFIG_ARCH_STACKWALK
28 
29 /**
30  * stack_trace_consume_fn - Callback for arch_stack_walk()
31  * @cookie:	Caller supplied pointer handed back by arch_stack_walk()
32  * @addr:	The stack entry address to consume
33  *
34  * Return:	True, if the entry was consumed or skipped
35  *		False, if there is no space left to store
36  */
37 typedef bool (*stack_trace_consume_fn)(void *cookie, unsigned long addr);
38 /**
39  * arch_stack_walk - Architecture specific function to walk the stack
40  * @consume_entry:	Callback which is invoked by the architecture code for
41  *			each entry.
42  * @cookie:		Caller supplied pointer which is handed back to
43  *			@consume_entry
44  * @task:		Pointer to a task struct, can be NULL
45  * @regs:		Pointer to registers, can be NULL
46  *
47  * ============ ======= ============================================
48  * task	        regs
49  * ============ ======= ============================================
50  * task		NULL	Stack trace from task (can be current)
51  * current	regs	Stack trace starting on regs->stackpointer
52  * ============ ======= ============================================
53  */
54 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
55 		     struct task_struct *task, struct pt_regs *regs);
56 
57 /**
58  * arch_stack_walk_reliable - Architecture specific function to walk the
59  *			      stack reliably
60  *
61  * @consume_entry:	Callback which is invoked by the architecture code for
62  *			each entry.
63  * @cookie:		Caller supplied pointer which is handed back to
64  *			@consume_entry
65  * @task:		Pointer to a task struct, can be NULL
66  *
67  * This function returns an error if it detects any unreliable
68  * features of the stack. Otherwise it guarantees that the stack
69  * trace is reliable.
70  *
71  * If the task is not 'current', the caller *must* ensure the task is
72  * inactive and its stack is pinned.
73  */
74 int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie,
75 			     struct task_struct *task);
76 
77 void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
78 			  const struct pt_regs *regs);
79 
80 #else /* CONFIG_ARCH_STACKWALK */
81 struct stack_trace {
82 	unsigned int nr_entries, max_entries;
83 	unsigned long *entries;
84 	unsigned int skip;	/* input argument: How many entries to skip */
85 };
86 
87 extern void save_stack_trace(struct stack_trace *trace);
88 extern void save_stack_trace_regs(struct pt_regs *regs,
89 				  struct stack_trace *trace);
90 extern void save_stack_trace_tsk(struct task_struct *tsk,
91 				struct stack_trace *trace);
92 extern int save_stack_trace_tsk_reliable(struct task_struct *tsk,
93 					 struct stack_trace *trace);
94 extern void save_stack_trace_user(struct stack_trace *trace);
95 #endif /* !CONFIG_ARCH_STACKWALK */
96 #endif /* CONFIG_STACKTRACE */
97 
98 #if defined(CONFIG_STACKTRACE) && defined(CONFIG_HAVE_RELIABLE_STACKTRACE)
99 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
100 				  unsigned int size);
101 #else
stack_trace_save_tsk_reliable(struct task_struct * tsk,unsigned long * store,unsigned int size)102 static inline int stack_trace_save_tsk_reliable(struct task_struct *tsk,
103 						unsigned long *store,
104 						unsigned int size)
105 {
106 	return -ENOSYS;
107 }
108 #endif
109 
110 #endif /* __LINUX_STACKTRACE_H */
111