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