stacktrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Stack trace management functions
  3. *
  4. * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <[email protected]>
  5. */
  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>
  14. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  15. struct task_struct *task, struct pt_regs *regs)
  16. {
  17. struct unwind_state state;
  18. unsigned long addr;
  19. if (regs && !consume_entry(cookie, regs->ip))
  20. return;
  21. for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
  22. unwind_next_frame(&state)) {
  23. addr = unwind_get_return_address(&state);
  24. if (!addr || !consume_entry(cookie, addr))
  25. break;
  26. }
  27. }
  28. int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
  29. void *cookie, struct task_struct *task)
  30. {
  31. struct unwind_state state;
  32. struct pt_regs *regs;
  33. unsigned long addr;
  34. for (unwind_start(&state, task, NULL, NULL);
  35. !unwind_done(&state) && !unwind_error(&state);
  36. unwind_next_frame(&state)) {
  37. regs = unwind_get_entry_regs(&state, NULL);
  38. if (regs) {
  39. /* Success path for user tasks */
  40. if (user_mode(regs))
  41. return 0;
  42. /*
  43. * Kernel mode registers on the stack indicate an
  44. * in-kernel interrupt or exception (e.g., preemption
  45. * or a page fault), which can make frame pointers
  46. * unreliable.
  47. */
  48. if (IS_ENABLED(CONFIG_FRAME_POINTER))
  49. return -EINVAL;
  50. }
  51. addr = unwind_get_return_address(&state);
  52. /*
  53. * A NULL or invalid return address probably means there's some
  54. * generated code which __kernel_text_address() doesn't know
  55. * about.
  56. */
  57. if (!addr)
  58. return -EINVAL;
  59. if (!consume_entry(cookie, addr))
  60. return -EINVAL;
  61. }
  62. /* Check for stack corruption */
  63. if (unwind_error(&state))
  64. return -EINVAL;
  65. return 0;
  66. }
  67. /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
  68. struct stack_frame_user {
  69. const void __user *next_fp;
  70. unsigned long ret_addr;
  71. };
  72. static int
  73. copy_stack_frame(const struct stack_frame_user __user *fp,
  74. struct stack_frame_user *frame)
  75. {
  76. int ret;
  77. if (!__access_ok(fp, sizeof(*frame)))
  78. return 0;
  79. ret = 1;
  80. pagefault_disable();
  81. if (__get_user(frame->next_fp, &fp->next_fp) ||
  82. __get_user(frame->ret_addr, &fp->ret_addr))
  83. ret = 0;
  84. pagefault_enable();
  85. return ret;
  86. }
  87. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  88. const struct pt_regs *regs)
  89. {
  90. const void __user *fp = (const void __user *)regs->bp;
  91. if (!consume_entry(cookie, regs->ip))
  92. return;
  93. while (1) {
  94. struct stack_frame_user frame;
  95. frame.next_fp = NULL;
  96. frame.ret_addr = 0;
  97. if (!copy_stack_frame(fp, &frame))
  98. break;
  99. if ((unsigned long)fp < regs->sp)
  100. break;
  101. if (!frame.ret_addr)
  102. break;
  103. if (!consume_entry(cookie, frame.ret_addr))
  104. break;
  105. fp = frame.next_fp;
  106. }
  107. }