unwind_guess.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <linux/sched.h>
  2. #include <linux/ftrace.h>
  3. #include <asm/ptrace.h>
  4. #include <asm/bitops.h>
  5. #include <asm/stacktrace.h>
  6. #include <asm/unwind.h>
  7. unsigned long unwind_get_return_address(struct unwind_state *state)
  8. {
  9. unsigned long addr;
  10. if (unwind_done(state))
  11. return 0;
  12. addr = READ_ONCE_NOCHECK(*state->sp);
  13. return ftrace_graph_ret_addr(state->task, &state->graph_idx,
  14. addr, state->sp);
  15. }
  16. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  17. bool unwind_next_frame(struct unwind_state *state)
  18. {
  19. struct stack_info *info = &state->stack_info;
  20. if (unwind_done(state))
  21. return false;
  22. do {
  23. for (state->sp++; state->sp < info->end; state->sp++) {
  24. unsigned long addr = READ_ONCE_NOCHECK(*state->sp);
  25. if (__kernel_text_address(addr))
  26. return true;
  27. }
  28. state->sp = info->next_sp;
  29. } while (!get_stack_info(state->sp, state->task, info,
  30. &state->stack_mask));
  31. return false;
  32. }
  33. EXPORT_SYMBOL_GPL(unwind_next_frame);
  34. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  35. struct pt_regs *regs, unsigned long *first_frame)
  36. {
  37. memset(state, 0, sizeof(*state));
  38. state->task = task;
  39. state->sp = first_frame;
  40. get_stack_info(first_frame, state->task, &state->stack_info,
  41. &state->stack_mask);
  42. /*
  43. * The caller can provide the address of the first frame directly
  44. * (first_frame) or indirectly (regs->sp) to indicate which stack frame
  45. * to start unwinding at. Skip ahead until we reach it.
  46. */
  47. if (!unwind_done(state) &&
  48. (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
  49. !__kernel_text_address(*first_frame)))
  50. unwind_next_frame(state);
  51. }
  52. EXPORT_SYMBOL_GPL(__unwind_start);