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 void arch_stack_walk(stack_trace_consume_fn consume_entry
, void *cookie
,
16 struct task_struct
*task
, struct pt_regs
*regs
)
18 struct unwind_state state
;
21 if (regs
&& !consume_entry(cookie
, regs
->ip
))
24 for (unwind_start(&state
, task
, regs
, NULL
); !unwind_done(&state
);
25 unwind_next_frame(&state
)) {
26 addr
= unwind_get_return_address(&state
);
27 if (!addr
|| !consume_entry(cookie
, addr
))
32 int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry
,
33 void *cookie
, struct task_struct
*task
)
35 struct unwind_state state
;
39 for (unwind_start(&state
, task
, NULL
, NULL
);
40 !unwind_done(&state
) && !unwind_error(&state
);
41 unwind_next_frame(&state
)) {
43 regs
= unwind_get_entry_regs(&state
, NULL
);
45 /* Success path for user tasks */
50 * Kernel mode registers on the stack indicate an
51 * in-kernel interrupt or exception (e.g., preemption
52 * or a page fault), which can make frame pointers
55 if (IS_ENABLED(CONFIG_FRAME_POINTER
))
59 addr
= unwind_get_return_address(&state
);
62 * A NULL or invalid return address probably means there's some
63 * generated code which __kernel_text_address() doesn't know
69 if (!consume_entry(cookie
, addr
))
73 /* Check for stack corruption */
74 if (unwind_error(&state
))
80 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
82 struct stack_frame_user
{
83 const void __user
*next_fp
;
84 unsigned long ret_addr
;
88 copy_stack_frame(const struct stack_frame_user __user
*fp
,
89 struct stack_frame_user
*frame
)
93 if (!__access_ok(fp
, sizeof(*frame
)))
98 if (__get_user(frame
->next_fp
, &fp
->next_fp
) ||
99 __get_user(frame
->ret_addr
, &fp
->ret_addr
))
106 void arch_stack_walk_user(stack_trace_consume_fn consume_entry
, void *cookie
,
107 const struct pt_regs
*regs
)
109 const void __user
*fp
= (const void __user
*)regs
->bp
;
111 if (!consume_entry(cookie
, regs
->ip
))
115 struct stack_frame_user frame
;
117 frame
.next_fp
= NULL
;
119 if (!copy_stack_frame(fp
, &frame
))
121 if ((unsigned long)fp
< regs
->sp
)
125 if (!consume_entry(cookie
, frame
.ret_addr
))