1 #include <linux/sched.h>
2 #include <asm/ptrace.h>
3 #include <asm/bitops.h>
4 #include <asm/stacktrace.h>
5 #include <asm/unwind.h>
7 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
9 unsigned long unwind_get_return_address(struct unwind_state *state)
12 unsigned long *addr_p = unwind_get_return_address_ptr(state);
14 if (unwind_done(state))
17 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
20 return __kernel_text_address(addr) ? addr : 0;
22 EXPORT_SYMBOL_GPL(unwind_get_return_address);
24 static bool update_stack_state(struct unwind_state *state, void *addr,
27 struct stack_info *info = &state->stack_info;
30 * If addr isn't on the current stack, switch to the next one.
32 * We may have to traverse multiple stacks to deal with the possibility
33 * that 'info->next_sp' could point to an empty stack and 'addr' could
34 * be on a subsequent stack.
36 while (!on_stack(info, addr, len))
37 if (get_stack_info(info->next_sp, state->task, info,
44 bool unwind_next_frame(struct unwind_state *state)
46 unsigned long *next_bp;
48 if (unwind_done(state))
51 next_bp = (unsigned long *)*state->bp;
53 /* make sure the next frame's data is accessible */
54 if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE))
57 /* move to the next frame */
61 EXPORT_SYMBOL_GPL(unwind_next_frame);
63 void __unwind_start(struct unwind_state *state, struct task_struct *task,
64 struct pt_regs *regs, unsigned long *first_frame)
66 memset(state, 0, sizeof(*state));
69 /* don't even attempt to start from user mode regs */
70 if (regs && user_mode(regs)) {
71 state->stack_info.type = STACK_TYPE_UNKNOWN;
75 /* set up the starting stack frame */
76 state->bp = get_frame_pointer(task, regs);
78 /* initialize stack info and make sure the frame data is accessible */
79 get_stack_info(state->bp, state->task, &state->stack_info,
81 update_stack_state(state, state->bp, FRAME_HEADER_SIZE);
84 * The caller can provide the address of the first frame directly
85 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
86 * to start unwinding at. Skip ahead until we reach it.
88 while (!unwind_done(state) &&
89 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
90 state->bp < first_frame))
91 unwind_next_frame(state);
93 EXPORT_SYMBOL_GPL(__unwind_start);