stacktrace.c 4.6 KB

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