2 * Stack trace management functions
4 * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/stacktrace.h>
10 #include <linux/export.h>
11 #include <linux/uaccess.h>
12 #include <asm/stacktrace.h>
13 #include <asm/unwind.h>
15 static int save_stack_address(struct stack_trace
*trace
, unsigned long addr
,
18 if (nosched
&& in_sched_functions(addr
))
21 if (trace
->skip
> 0) {
26 if (trace
->nr_entries
>= trace
->max_entries
)
29 trace
->entries
[trace
->nr_entries
++] = addr
;
33 static void noinline
__save_stack_trace(struct stack_trace
*trace
,
34 struct task_struct
*task
, struct pt_regs
*regs
,
37 struct unwind_state state
;
41 save_stack_address(trace
, regs
->ip
, nosched
);
43 for (unwind_start(&state
, task
, regs
, NULL
); !unwind_done(&state
);
44 unwind_next_frame(&state
)) {
45 addr
= unwind_get_return_address(&state
);
46 if (!addr
|| save_stack_address(trace
, addr
, nosched
))
50 if (trace
->nr_entries
< trace
->max_entries
)
51 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
55 * Save stack-backtrace addresses into a stack_trace buffer.
57 void save_stack_trace(struct stack_trace
*trace
)
60 __save_stack_trace(trace
, current
, NULL
, false);
62 EXPORT_SYMBOL_GPL(save_stack_trace
);
64 void save_stack_trace_regs(struct pt_regs
*regs
, struct stack_trace
*trace
)
66 __save_stack_trace(trace
, current
, regs
, false);
69 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
71 if (!try_get_task_stack(tsk
))
76 __save_stack_trace(trace
, tsk
, NULL
, true);
80 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
82 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
84 static int __always_inline
85 __save_stack_trace_reliable(struct stack_trace
*trace
,
86 struct task_struct
*task
)
88 struct unwind_state state
;
92 for (unwind_start(&state
, task
, NULL
, NULL
);
93 !unwind_done(&state
) && !unwind_error(&state
);
94 unwind_next_frame(&state
)) {
96 regs
= unwind_get_entry_regs(&state
, NULL
);
98 /* Success path for user tasks */
103 * Kernel mode registers on the stack indicate an
104 * in-kernel interrupt or exception (e.g., preemption
105 * or a page fault), which can make frame pointers
109 if (IS_ENABLED(CONFIG_FRAME_POINTER
))
113 addr
= unwind_get_return_address(&state
);
116 * A NULL or invalid return address probably means there's some
117 * generated code which __kernel_text_address() doesn't know
123 if (save_stack_address(trace
, addr
, false))
127 /* Check for stack corruption */
128 if (unwind_error(&state
))
131 /* Success path for non-user tasks, i.e. kthreads and idle tasks */
132 if (!(task
->flags
& (PF_KTHREAD
| PF_IDLE
)))
136 if (trace
->nr_entries
< trace
->max_entries
)
137 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
143 * This function returns an error if it detects any unreliable features of the
144 * stack. Otherwise it guarantees that the stack trace is reliable.
146 * If the task is not 'current', the caller *must* ensure the task is inactive.
148 int save_stack_trace_tsk_reliable(struct task_struct
*tsk
,
149 struct stack_trace
*trace
)
154 * If the task doesn't have a stack (e.g., a zombie), the stack is
157 if (!try_get_task_stack(tsk
))
160 ret
= __save_stack_trace_reliable(trace
, tsk
);
166 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
168 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
170 struct stack_frame_user
{
171 const void __user
*next_fp
;
172 unsigned long ret_addr
;
176 copy_stack_frame(const void __user
*fp
, struct stack_frame_user
*frame
)
180 if (!access_ok(fp
, sizeof(*frame
)))
185 if (__copy_from_user_inatomic(frame
, fp
, sizeof(*frame
)))
192 static inline void __save_stack_trace_user(struct stack_trace
*trace
)
194 const struct pt_regs
*regs
= task_pt_regs(current
);
195 const void __user
*fp
= (const void __user
*)regs
->bp
;
197 if (trace
->nr_entries
< trace
->max_entries
)
198 trace
->entries
[trace
->nr_entries
++] = regs
->ip
;
200 while (trace
->nr_entries
< trace
->max_entries
) {
201 struct stack_frame_user frame
;
203 frame
.next_fp
= NULL
;
205 if (!copy_stack_frame(fp
, &frame
))
207 if ((unsigned long)fp
< regs
->sp
)
209 if (frame
.ret_addr
) {
210 trace
->entries
[trace
->nr_entries
++] =
213 if (fp
== frame
.next_fp
)
219 void save_stack_trace_user(struct stack_trace
*trace
)
222 * Trace user stack if we are not a kernel thread
225 __save_stack_trace_user(trace
);
227 if (trace
->nr_entries
< trace
->max_entries
)
228 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;