stacktrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/sched/debug.h>
  4. #include <linux/stacktrace.h>
  5. #include <asm/sections.h>
  6. #include <asm/stacktrace.h>
  7. #include <asm/traps.h>
  8. #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
  9. /*
  10. * Unwind the current stack frame and store the new register values in the
  11. * structure passed as argument. Unwinding is equivalent to a function return,
  12. * hence the new PC value rather than LR should be used for backtrace.
  13. *
  14. * With framepointer enabled, a simple function prologue looks like this:
  15. * mov ip, sp
  16. * stmdb sp!, {fp, ip, lr, pc}
  17. * sub fp, ip, #4
  18. *
  19. * A simple function epilogue looks like this:
  20. * ldm sp, {fp, sp, pc}
  21. *
  22. * Note that with framepointer enabled, even the leaf functions have the same
  23. * prologue and epilogue, therefore we can ignore the LR value in this case.
  24. */
  25. int notrace unwind_frame(struct stackframe *frame)
  26. {
  27. unsigned long high, low;
  28. unsigned long fp = frame->fp;
  29. /* only go to a higher address on the stack */
  30. low = frame->sp;
  31. high = ALIGN(low, THREAD_SIZE);
  32. /* check current frame pointer is within bounds */
  33. if (fp < low + 12 || fp > high - 4)
  34. return -EINVAL;
  35. /* restore the registers from the stack frame */
  36. frame->fp = *(unsigned long *)(fp - 12);
  37. frame->sp = *(unsigned long *)(fp - 8);
  38. frame->pc = *(unsigned long *)(fp - 4);
  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 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. if (!in_entry_text(frame->pc))
  77. return 0;
  78. regs = (struct pt_regs *)frame->sp;
  79. trace->entries[trace->nr_entries++] = regs->ARM_pc;
  80. return trace->nr_entries >= trace->max_entries;
  81. }
  82. /* This must be noinline to so that our skip calculation works correctly */
  83. static noinline void __save_stack_trace(struct task_struct *tsk,
  84. struct stack_trace *trace, unsigned int nosched)
  85. {
  86. struct stack_trace_data data;
  87. struct stackframe frame;
  88. data.trace = trace;
  89. data.skip = trace->skip;
  90. data.no_sched_functions = nosched;
  91. if (tsk != current) {
  92. #ifdef CONFIG_SMP
  93. /*
  94. * What guarantees do we have here that 'tsk' is not
  95. * running on another CPU? For now, ignore it as we
  96. * can't guarantee we won't explode.
  97. */
  98. if (trace->nr_entries < trace->max_entries)
  99. trace->entries[trace->nr_entries++] = ULONG_MAX;
  100. return;
  101. #else
  102. frame.fp = thread_saved_fp(tsk);
  103. frame.sp = thread_saved_sp(tsk);
  104. frame.lr = 0; /* recovered from the stack */
  105. frame.pc = thread_saved_pc(tsk);
  106. #endif
  107. } else {
  108. /* We don't want this function nor the caller */
  109. data.skip += 2;
  110. frame.fp = (unsigned long)__builtin_frame_address(0);
  111. frame.sp = current_stack_pointer;
  112. frame.lr = (unsigned long)__builtin_return_address(0);
  113. frame.pc = (unsigned long)__save_stack_trace;
  114. }
  115. walk_stackframe(&frame, save_trace, &data);
  116. if (trace->nr_entries < trace->max_entries)
  117. trace->entries[trace->nr_entries++] = ULONG_MAX;
  118. }
  119. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  120. {
  121. struct stack_trace_data data;
  122. struct stackframe frame;
  123. data.trace = trace;
  124. data.skip = trace->skip;
  125. data.no_sched_functions = 0;
  126. frame.fp = regs->ARM_fp;
  127. frame.sp = regs->ARM_sp;
  128. frame.lr = regs->ARM_lr;
  129. frame.pc = regs->ARM_pc;
  130. walk_stackframe(&frame, save_trace, &data);
  131. if (trace->nr_entries < trace->max_entries)
  132. trace->entries[trace->nr_entries++] = ULONG_MAX;
  133. }
  134. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  135. {
  136. __save_stack_trace(tsk, trace, 1);
  137. }
  138. EXPORT_SYMBOL(save_stack_trace_tsk);
  139. void save_stack_trace(struct stack_trace *trace)
  140. {
  141. __save_stack_trace(current, trace, 0);
  142. }
  143. EXPORT_SYMBOL_GPL(save_stack_trace);
  144. #endif