unwind_frame.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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>
  6. #define FRAME_HEADER_SIZE (sizeof(long) * 2)
  7. unsigned long unwind_get_return_address(struct unwind_state *state)
  8. {
  9. unsigned long addr;
  10. unsigned long *addr_p = unwind_get_return_address_ptr(state);
  11. if (unwind_done(state))
  12. return 0;
  13. addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
  14. addr_p);
  15. return __kernel_text_address(addr) ? addr : 0;
  16. }
  17. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  18. static bool update_stack_state(struct unwind_state *state, void *addr,
  19. size_t len)
  20. {
  21. struct stack_info *info = &state->stack_info;
  22. /*
  23. * If addr isn't on the current stack, switch to the next one.
  24. *
  25. * We may have to traverse multiple stacks to deal with the possibility
  26. * that 'info->next_sp' could point to an empty stack and 'addr' could
  27. * be on a subsequent stack.
  28. */
  29. while (!on_stack(info, addr, len))
  30. if (get_stack_info(info->next_sp, state->task, info,
  31. &state->stack_mask))
  32. return false;
  33. return true;
  34. }
  35. bool unwind_next_frame(struct unwind_state *state)
  36. {
  37. unsigned long *next_bp;
  38. if (unwind_done(state))
  39. return false;
  40. next_bp = (unsigned long *)*state->bp;
  41. /* make sure the next frame's data is accessible */
  42. if (!update_stack_state(state, next_bp, FRAME_HEADER_SIZE))
  43. return false;
  44. /* move to the next frame */
  45. state->bp = next_bp;
  46. return true;
  47. }
  48. EXPORT_SYMBOL_GPL(unwind_next_frame);
  49. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  50. struct pt_regs *regs, unsigned long *first_frame)
  51. {
  52. memset(state, 0, sizeof(*state));
  53. state->task = task;
  54. /* don't even attempt to start from user mode regs */
  55. if (regs && user_mode(regs)) {
  56. state->stack_info.type = STACK_TYPE_UNKNOWN;
  57. return;
  58. }
  59. /* set up the starting stack frame */
  60. state->bp = get_frame_pointer(task, regs);
  61. /* initialize stack info and make sure the frame data is accessible */
  62. get_stack_info(state->bp, state->task, &state->stack_info,
  63. &state->stack_mask);
  64. update_stack_state(state, state->bp, FRAME_HEADER_SIZE);
  65. /*
  66. * The caller can provide the address of the first frame directly
  67. * (first_frame) or indirectly (regs->sp) to indicate which stack frame
  68. * to start unwinding at. Skip ahead until we reach it.
  69. */
  70. while (!unwind_done(state) &&
  71. (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
  72. state->bp < first_frame))
  73. unwind_next_frame(state);
  74. }
  75. EXPORT_SYMBOL_GPL(__unwind_start);