stacktrace.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/stacktrace.h>
  4. #include <asm/stacktrace.h>
  5. #if defined(CONFIG_FRAME_POINTER)
  6. #ifdef CONFIG_KALLSYMS
  7. #include <linux/kallsyms.h>
  8. #include <linux/module.h>
  9. static unsigned long tbi_boing_addr;
  10. static unsigned long tbi_boing_size;
  11. static void tbi_boing_init(void)
  12. {
  13. /* We need to know where TBIBoingVec is and it's size */
  14. unsigned long size;
  15. unsigned long offset;
  16. char modname[MODULE_NAME_LEN];
  17. char name[KSYM_NAME_LEN];
  18. tbi_boing_addr = kallsyms_lookup_name("___TBIBoingVec");
  19. if (!tbi_boing_addr)
  20. tbi_boing_addr = 1;
  21. else if (!lookup_symbol_attrs(tbi_boing_addr, &size,
  22. &offset, modname, name))
  23. tbi_boing_size = size;
  24. }
  25. #endif
  26. #define ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
  27. /*
  28. * Unwind the current stack frame and store the new register values in the
  29. * structure passed as argument. Unwinding is equivalent to a function return,
  30. * hence the new PC value rather than LR should be used for backtrace.
  31. */
  32. int notrace unwind_frame(struct stackframe *frame)
  33. {
  34. struct metag_frame *fp = (struct metag_frame *)frame->fp;
  35. unsigned long lr;
  36. unsigned long fpnew;
  37. if (frame->fp & 0x7)
  38. return -EINVAL;
  39. fpnew = fp->fp;
  40. lr = fp->lr - 4;
  41. #ifdef CONFIG_KALLSYMS
  42. /* If we've reached TBIBoingVec then we're at an interrupt
  43. * entry point or a syscall entry point. The frame pointer
  44. * points to a pt_regs which can be used to continue tracing on
  45. * the other side of the boing.
  46. */
  47. if (!tbi_boing_addr)
  48. tbi_boing_init();
  49. if (tbi_boing_size && lr >= tbi_boing_addr &&
  50. lr < tbi_boing_addr + tbi_boing_size) {
  51. struct pt_regs *regs = (struct pt_regs *)fpnew;
  52. if (user_mode(regs))
  53. return -EINVAL;
  54. fpnew = regs->ctx.AX[1].U0;
  55. lr = regs->ctx.DX[4].U1;
  56. }
  57. #endif
  58. /* stack grows up, so frame pointers must decrease */
  59. if (fpnew < (ALIGN_DOWN((unsigned long)fp, THREAD_SIZE) +
  60. sizeof(struct thread_info)) || fpnew >= (unsigned long)fp)
  61. return -EINVAL;
  62. /* restore the registers from the stack frame */
  63. frame->fp = fpnew;
  64. frame->pc = lr;
  65. return 0;
  66. }
  67. #else
  68. int notrace unwind_frame(struct stackframe *frame)
  69. {
  70. struct metag_frame *sp = (struct metag_frame *)frame->sp;
  71. if (frame->sp & 0x7)
  72. return -EINVAL;
  73. while (!kstack_end(sp)) {
  74. unsigned long addr = sp->lr - 4;
  75. sp--;
  76. if (__kernel_text_address(addr)) {
  77. frame->sp = (unsigned long)sp;
  78. frame->pc = addr;
  79. return 0;
  80. }
  81. }
  82. return -EINVAL;
  83. }
  84. #endif
  85. void notrace walk_stackframe(struct stackframe *frame,
  86. int (*fn)(struct stackframe *, void *), void *data)
  87. {
  88. while (1) {
  89. int ret;
  90. if (fn(frame, data))
  91. break;
  92. ret = unwind_frame(frame);
  93. if (ret < 0)
  94. break;
  95. }
  96. }
  97. EXPORT_SYMBOL(walk_stackframe);
  98. #ifdef CONFIG_STACKTRACE
  99. struct stack_trace_data {
  100. struct stack_trace *trace;
  101. unsigned int no_sched_functions;
  102. unsigned int skip;
  103. };
  104. static int save_trace(struct stackframe *frame, void *d)
  105. {
  106. struct stack_trace_data *data = d;
  107. struct stack_trace *trace = data->trace;
  108. unsigned long addr = frame->pc;
  109. if (data->no_sched_functions && in_sched_functions(addr))
  110. return 0;
  111. if (data->skip) {
  112. data->skip--;
  113. return 0;
  114. }
  115. trace->entries[trace->nr_entries++] = addr;
  116. return trace->nr_entries >= trace->max_entries;
  117. }
  118. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  119. {
  120. struct stack_trace_data data;
  121. struct stackframe frame;
  122. data.trace = trace;
  123. data.skip = trace->skip;
  124. if (tsk != current) {
  125. #ifdef CONFIG_SMP
  126. /*
  127. * What guarantees do we have here that 'tsk' is not
  128. * running on another CPU? For now, ignore it as we
  129. * can't guarantee we won't explode.
  130. */
  131. if (trace->nr_entries < trace->max_entries)
  132. trace->entries[trace->nr_entries++] = ULONG_MAX;
  133. return;
  134. #else
  135. data.no_sched_functions = 1;
  136. frame.fp = thread_saved_fp(tsk);
  137. frame.sp = thread_saved_sp(tsk);
  138. frame.lr = 0; /* recovered from the stack */
  139. frame.pc = thread_saved_pc(tsk);
  140. #endif
  141. } else {
  142. register unsigned long current_sp asm ("A0StP");
  143. data.no_sched_functions = 0;
  144. frame.fp = (unsigned long)__builtin_frame_address(0);
  145. frame.sp = current_sp;
  146. frame.lr = (unsigned long)__builtin_return_address(0);
  147. frame.pc = (unsigned long)save_stack_trace_tsk;
  148. }
  149. walk_stackframe(&frame, save_trace, &data);
  150. if (trace->nr_entries < trace->max_entries)
  151. trace->entries[trace->nr_entries++] = ULONG_MAX;
  152. }
  153. void save_stack_trace(struct stack_trace *trace)
  154. {
  155. save_stack_trace_tsk(current, trace);
  156. }
  157. EXPORT_SYMBOL_GPL(save_stack_trace);
  158. #endif