1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/sched.h>
14#include <linux/sched/debug.h>
15#include <linux/stacktrace.h>
16
17#include <asm/stacktrace.h>
18
19#if defined(CONFIG_FRAME_POINTER)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36int notrace unwind_frame(struct stackframe *frame)
37{
38 unsigned long high, low;
39 unsigned long fp = frame->fp;
40
41
42 low = frame->sp;
43 high = ALIGN(low, THREAD_SIZE);
44
45
46 if (fp < (low + 12) || fp + 4 >= high)
47 return -EINVAL;
48
49
50 frame->fp = *(unsigned long *)(fp - 12);
51 frame->sp = *(unsigned long *)(fp - 8);
52 frame->pc = *(unsigned long *)(fp - 4);
53
54 return 0;
55}
56#endif
57
58void notrace walk_stackframe(struct stackframe *frame,
59 int (*fn)(struct stackframe *, void *), void *data)
60{
61 while (1) {
62 int ret;
63
64 if (fn(frame, data))
65 break;
66 ret = unwind_frame(frame);
67 if (ret < 0)
68 break;
69 }
70}
71EXPORT_SYMBOL(walk_stackframe);
72
73#ifdef CONFIG_STACKTRACE
74struct stack_trace_data {
75 struct stack_trace *trace;
76 unsigned int no_sched_functions;
77 unsigned int skip;
78};
79
80static int save_trace(struct stackframe *frame, void *d)
81{
82 struct stack_trace_data *data = d;
83 struct stack_trace *trace = data->trace;
84 unsigned long addr = frame->pc;
85
86 if (data->no_sched_functions && in_sched_functions(addr))
87 return 0;
88 if (data->skip) {
89 data->skip--;
90 return 0;
91 }
92
93 trace->entries[trace->nr_entries++] = addr;
94
95 return trace->nr_entries >= trace->max_entries;
96}
97
98void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
99{
100 struct stack_trace_data data;
101 struct stackframe frame;
102
103 data.trace = trace;
104 data.skip = trace->skip;
105
106 if (tsk != current) {
107 data.no_sched_functions = 1;
108 frame.fp = thread_saved_fp(tsk);
109 frame.sp = thread_saved_sp(tsk);
110 frame.lr = 0;
111 frame.pc = thread_saved_pc(tsk);
112 } else {
113 register unsigned long current_sp asm("sp");
114
115 data.no_sched_functions = 0;
116 frame.fp = (unsigned long)__builtin_frame_address(0);
117 frame.sp = current_sp;
118 frame.lr = (unsigned long)__builtin_return_address(0);
119 frame.pc = (unsigned long)save_stack_trace_tsk;
120 }
121
122 walk_stackframe(&frame, save_trace, &data);
123 if (trace->nr_entries < trace->max_entries)
124 trace->entries[trace->nr_entries++] = ULONG_MAX;
125}
126
127void save_stack_trace(struct stack_trace *trace)
128{
129 save_stack_trace_tsk(current, trace);
130}
131EXPORT_SYMBOL_GPL(save_stack_trace);
132#endif
133