stacktrace.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if (ALIGN(frame->fp, THREAD_SIZE) != ALIGN(fp, THREAD_SIZE))
  38. return -EINVAL;
  39. return 0;
  40. }
  41. #endif
  42. void notrace walk_stackframe(struct stackframe *frame,
  43. int (*fn)(struct stackframe *, void *), void *data)
  44. {
  45. while (1) {
  46. int ret;
  47. if (fn(frame, data))
  48. break;
  49. ret = unwind_frame(frame);
  50. if (ret < 0)
  51. break;
  52. }
  53. }
  54. EXPORT_SYMBOL(walk_stackframe);
  55. #ifdef CONFIG_STACKTRACE
  56. struct stack_trace_data {
  57. struct stack_trace *trace;
  58. unsigned long last_pc;
  59. unsigned int no_sched_functions;
  60. unsigned int skip;
  61. };
  62. static int save_trace(struct stackframe *frame, void *d)
  63. {
  64. struct stack_trace_data *data = d;
  65. struct stack_trace *trace = data->trace;
  66. struct pt_regs *regs;
  67. unsigned long addr = frame->pc;
  68. if (data->no_sched_functions && in_sched_functions(addr))
  69. return 0;
  70. if (data->skip) {
  71. data->skip--;
  72. return 0;
  73. }
  74. trace->entries[trace->nr_entries++] = addr;
  75. if (trace->nr_entries >= trace->max_entries)
  76. return 1;
  77. /*
  78. * in_exception_text() is designed to test if the PC is one of
  79. * the functions which has an exception stack above it, but
  80. * unfortunately what is in frame->pc is the return LR value,
  81. * not the saved PC value. So, we need to track the previous
  82. * frame PC value when doing this.
  83. */
  84. addr = data->last_pc;
  85. data->last_pc = frame->pc;
  86. if (!in_exception_text(addr))
  87. return 0;
  88. regs = (struct pt_regs *)frame->sp;
  89. trace->entries[trace->nr_entries++] = regs->ARM_pc;
  90. return trace->nr_entries >= trace->max_entries;
  91. }
  92. /* This must be noinline to so that our skip calculation works correctly */
  93. static noinline void __save_stack_trace(struct task_struct *tsk,
  94. struct stack_trace *trace, unsigned int nosched)
  95. {
  96. struct stack_trace_data data;
  97. struct stackframe frame;
  98. data.trace = trace;
  99. data.last_pc = ULONG_MAX;
  100. data.skip = trace->skip;
  101. data.no_sched_functions = nosched;
  102. if (tsk != current) {
  103. #if 0
  104. #ifdef CONFIG_SMP
  105. /*
  106. * What guarantees do we have here that 'tsk' is not
  107. * running on another CPU? For now, ignore it as we
  108. * can't guarantee we won't explode.
  109. */
  110. if (trace->nr_entries < trace->max_entries)
  111. trace->entries[trace->nr_entries++] = ULONG_MAX;
  112. return;
  113. #endif
  114. #else
  115. frame.fp = thread_saved_fp(tsk);
  116. frame.sp = thread_saved_sp(tsk);
  117. frame.lr = 0; /* recovered from the stack */
  118. frame.pc = thread_saved_pc(tsk);
  119. #endif
  120. } else {
  121. /* We don't want this function nor the caller */
  122. data.skip += 2;
  123. frame.fp = (unsigned long)__builtin_frame_address(0);
  124. frame.sp = current_stack_pointer;
  125. frame.lr = (unsigned long)__builtin_return_address(0);
  126. frame.pc = (unsigned long)__save_stack_trace;
  127. }
  128. walk_stackframe(&frame, save_trace, &data);
  129. if (trace->nr_entries < trace->max_entries)
  130. trace->entries[trace->nr_entries++] = ULONG_MAX;
  131. }
  132. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  133. {
  134. struct stack_trace_data data;
  135. struct stackframe frame;
  136. data.trace = trace;
  137. data.skip = trace->skip;
  138. data.no_sched_functions = 0;
  139. frame.fp = regs->ARM_fp;
  140. frame.sp = regs->ARM_sp;
  141. frame.lr = regs->ARM_lr;
  142. frame.pc = regs->ARM_pc;
  143. walk_stackframe(&frame, save_trace, &data);
  144. if (trace->nr_entries < trace->max_entries)
  145. trace->entries[trace->nr_entries++] = ULONG_MAX;
  146. }
  147. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  148. {
  149. __save_stack_trace(tsk, trace, 1);
  150. }
  151. void save_stack_trace(struct stack_trace *trace)
  152. {
  153. __save_stack_trace(current, trace, 0);
  154. }
  155. EXPORT_SYMBOL_GPL(save_stack_trace);
  156. #endif