stacktrace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/sched/debug.h>
  4. #include <linux/stacktrace.h>
  5. #include <asm/stacktrace.h>
  6. #include <asm/traps.h>
  7. #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
  8. /*
  9. * Unwind the current stack frame and store the new register values in the
  10. * structure passed as argument. Unwinding is equivalent to a function return,
  11. * hence the new PC value rather than LR should be used for backtrace.
  12. *
  13. * With framepointer enabled, a simple function prologue looks like this:
  14. * mov ip, sp
  15. * stmdb sp!, {fp, ip, lr, pc}
  16. * sub fp, ip, #4
  17. *
  18. * A simple function epilogue looks like this:
  19. * ldm sp, {fp, sp, pc}
  20. *
  21. * Note that with framepointer enabled, even the leaf functions have the same
  22. * prologue and epilogue, therefore we can ignore the LR value in this case.
  23. */
  24. int notrace unwind_frame(struct stackframe *frame)
  25. {
  26. unsigned long high, low;
  27. unsigned long fp = frame->fp;
  28. /* only go to a higher address on the stack */
  29. low = frame->sp;
  30. high = ALIGN(low, THREAD_SIZE);
  31. /* check current frame pointer is within bounds */
  32. if (fp < low + 12 || fp > high - 4)
  33. return -EINVAL;
  34. /* restore the registers from the stack frame */
  35. frame->fp = *(unsigned long *)(fp - 12);
  36. frame->sp = *(unsigned long *)(fp - 8);
  37. frame->pc = *(unsigned long *)(fp - 4);
  38. return 0;
  39. }
  40. #endif
  41. void notrace walk_stackframe(struct stackframe *frame,
  42. int (*fn)(struct stackframe *, void *), void *data)
  43. {
  44. while (1) {
  45. int ret;
  46. if (fn(frame, data))
  47. break;
  48. ret = unwind_frame(frame);
  49. if (ret < 0)
  50. break;
  51. }
  52. }
  53. EXPORT_SYMBOL(walk_stackframe);
  54. #ifdef CONFIG_STACKTRACE
  55. struct stack_trace_data {
  56. struct stack_trace *trace;
  57. unsigned long last_pc;
  58. unsigned int no_sched_functions;
  59. unsigned int skip;
  60. };
  61. static int save_trace(struct stackframe *frame, void *d)
  62. {
  63. struct stack_trace_data *data = d;
  64. struct stack_trace *trace = data->trace;
  65. struct pt_regs *regs;
  66. unsigned long addr = frame->pc;
  67. if (data->no_sched_functions && in_sched_functions(addr))
  68. return 0;
  69. if (data->skip) {
  70. data->skip--;
  71. return 0;
  72. }
  73. trace->entries[trace->nr_entries++] = addr;
  74. if (trace->nr_entries >= trace->max_entries)
  75. return 1;
  76. /*
  77. * in_exception_text() is designed to test if the PC is one of
  78. * the functions which has an exception stack above it, but
  79. * unfortunately what is in frame->pc is the return LR value,
  80. * not the saved PC value. So, we need to track the previous
  81. * frame PC value when doing this.
  82. */
  83. addr = data->last_pc;
  84. data->last_pc = frame->pc;
  85. if (!in_exception_text(addr))
  86. return 0;
  87. regs = (struct pt_regs *)frame->sp;
  88. trace->entries[trace->nr_entries++] = regs->ARM_pc;
  89. return trace->nr_entries >= trace->max_entries;
  90. }
  91. /* This must be noinline to so that our skip calculation works correctly */
  92. static noinline void __save_stack_trace(struct task_struct *tsk,
  93. struct stack_trace *trace, unsigned int nosched)
  94. {
  95. struct stack_trace_data data;
  96. struct stackframe frame;
  97. data.trace = trace;
  98. data.last_pc = ULONG_MAX;
  99. data.skip = trace->skip;
  100. data.no_sched_functions = nosched;
  101. if (tsk != current) {
  102. #ifdef CONFIG_SMP
  103. /*
  104. * What guarantees do we have here that 'tsk' is not
  105. * running on another CPU? For now, ignore it as we
  106. * can't guarantee we won't explode.
  107. */
  108. if (trace->nr_entries < trace->max_entries)
  109. trace->entries[trace->nr_entries++] = ULONG_MAX;
  110. return;
  111. #else
  112. frame.fp = thread_saved_fp(tsk);
  113. frame.sp = thread_saved_sp(tsk);
  114. frame.lr = 0; /* recovered from the stack */
  115. frame.pc = thread_saved_pc(tsk);
  116. #endif
  117. } else {
  118. /* We don't want this function nor the caller */
  119. data.skip += 2;
  120. frame.fp = (unsigned long)__builtin_frame_address(0);
  121. frame.sp = current_stack_pointer;
  122. frame.lr = (unsigned long)__builtin_return_address(0);
  123. frame.pc = (unsigned long)__save_stack_trace;
  124. }
  125. walk_stackframe(&frame, save_trace, &data);
  126. if (trace->nr_entries < trace->max_entries)
  127. trace->entries[trace->nr_entries++] = ULONG_MAX;
  128. }
  129. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  130. {
  131. struct stack_trace_data data;
  132. struct stackframe frame;
  133. data.trace = trace;
  134. data.skip = trace->skip;
  135. data.no_sched_functions = 0;
  136. frame.fp = regs->ARM_fp;
  137. frame.sp = regs->ARM_sp;
  138. frame.lr = regs->ARM_lr;
  139. frame.pc = regs->ARM_pc;
  140. walk_stackframe(&frame, save_trace, &data);
  141. if (trace->nr_entries < trace->max_entries)
  142. trace->entries[trace->nr_entries++] = ULONG_MAX;
  143. }
  144. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  145. {
  146. __save_stack_trace(tsk, trace, 1);
  147. }
  148. void save_stack_trace(struct stack_trace *trace)
  149. {
  150. __save_stack_trace(current, trace, 0);
  151. }
  152. EXPORT_SYMBOL_GPL(save_stack_trace);
  153. #endif