stacktrace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2008 ARM Limited
  3. * Copyright (C) 2014 Regents of the University of California
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/export.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/debug.h>
  18. #include <linux/sched/task_stack.h>
  19. #include <linux/stacktrace.h>
  20. #include <linux/ftrace.h>
  21. #ifdef CONFIG_FRAME_POINTER
  22. struct stackframe {
  23. unsigned long fp;
  24. unsigned long ra;
  25. };
  26. static void notrace walk_stackframe(struct task_struct *task,
  27. struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
  28. {
  29. unsigned long fp, sp, pc;
  30. if (regs) {
  31. fp = GET_FP(regs);
  32. sp = GET_USP(regs);
  33. pc = GET_IP(regs);
  34. } else if (task == NULL || task == current) {
  35. const register unsigned long current_sp __asm__ ("sp");
  36. fp = (unsigned long)__builtin_frame_address(0);
  37. sp = current_sp;
  38. pc = (unsigned long)walk_stackframe;
  39. } else {
  40. /* task blocked in __switch_to */
  41. fp = task->thread.s[0];
  42. sp = task->thread.sp;
  43. pc = task->thread.ra;
  44. }
  45. for (;;) {
  46. unsigned long low, high;
  47. struct stackframe *frame;
  48. if (unlikely(!__kernel_text_address(pc) || fn(pc, arg)))
  49. break;
  50. /* Validate frame pointer */
  51. low = sp + sizeof(struct stackframe);
  52. high = ALIGN(sp, THREAD_SIZE);
  53. if (unlikely(fp < low || fp > high || fp & 0x7))
  54. break;
  55. /* Unwind stack frame */
  56. frame = (struct stackframe *)fp - 1;
  57. sp = fp;
  58. fp = frame->fp;
  59. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  60. pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
  61. (unsigned long *)(fp - 8));
  62. #else
  63. pc = frame->ra - 0x4;
  64. #endif
  65. }
  66. }
  67. #else /* !CONFIG_FRAME_POINTER */
  68. static void notrace walk_stackframe(struct task_struct *task,
  69. struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
  70. {
  71. unsigned long sp, pc;
  72. unsigned long *ksp;
  73. if (regs) {
  74. sp = GET_USP(regs);
  75. pc = GET_IP(regs);
  76. } else if (task == NULL || task == current) {
  77. const register unsigned long current_sp __asm__ ("sp");
  78. sp = current_sp;
  79. pc = (unsigned long)walk_stackframe;
  80. } else {
  81. /* task blocked in __switch_to */
  82. sp = task->thread.sp;
  83. pc = task->thread.ra;
  84. }
  85. if (unlikely(sp & 0x7))
  86. return;
  87. ksp = (unsigned long *)sp;
  88. while (!kstack_end(ksp)) {
  89. if (__kernel_text_address(pc) && unlikely(fn(pc, arg)))
  90. break;
  91. pc = (*ksp++) - 0x4;
  92. }
  93. }
  94. #endif /* CONFIG_FRAME_POINTER */
  95. static bool print_trace_address(unsigned long pc, void *arg)
  96. {
  97. print_ip_sym(pc);
  98. return false;
  99. }
  100. void show_stack(struct task_struct *task, unsigned long *sp)
  101. {
  102. pr_cont("Call Trace:\n");
  103. walk_stackframe(task, NULL, print_trace_address, NULL);
  104. }
  105. static bool save_wchan(unsigned long pc, void *arg)
  106. {
  107. if (!in_sched_functions(pc)) {
  108. unsigned long *p = arg;
  109. *p = pc;
  110. return true;
  111. }
  112. return false;
  113. }
  114. unsigned long get_wchan(struct task_struct *task)
  115. {
  116. unsigned long pc = 0;
  117. if (likely(task && task != current && task->state != TASK_RUNNING))
  118. walk_stackframe(task, NULL, save_wchan, &pc);
  119. return pc;
  120. }
  121. #ifdef CONFIG_STACKTRACE
  122. static bool __save_trace(unsigned long pc, void *arg, bool nosched)
  123. {
  124. struct stack_trace *trace = arg;
  125. if (unlikely(nosched && in_sched_functions(pc)))
  126. return false;
  127. if (unlikely(trace->skip > 0)) {
  128. trace->skip--;
  129. return false;
  130. }
  131. trace->entries[trace->nr_entries++] = pc;
  132. return (trace->nr_entries >= trace->max_entries);
  133. }
  134. static bool save_trace(unsigned long pc, void *arg)
  135. {
  136. return __save_trace(pc, arg, false);
  137. }
  138. /*
  139. * Save stack-backtrace addresses into a stack_trace buffer.
  140. */
  141. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  142. {
  143. walk_stackframe(tsk, NULL, save_trace, trace);
  144. if (trace->nr_entries < trace->max_entries)
  145. trace->entries[trace->nr_entries++] = ULONG_MAX;
  146. }
  147. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  148. void save_stack_trace(struct stack_trace *trace)
  149. {
  150. save_stack_trace_tsk(NULL, trace);
  151. }
  152. EXPORT_SYMBOL_GPL(save_stack_trace);
  153. #endif /* CONFIG_STACKTRACE */