stacktrace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifdef CONFIG_FRAME_POINTER
  21. struct stackframe {
  22. unsigned long fp;
  23. unsigned long ra;
  24. };
  25. static void notrace walk_stackframe(struct task_struct *task,
  26. struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
  27. {
  28. unsigned long fp, sp, pc;
  29. if (regs) {
  30. fp = GET_FP(regs);
  31. sp = GET_USP(regs);
  32. pc = GET_IP(regs);
  33. } else if (task == NULL || task == current) {
  34. const register unsigned long current_sp __asm__ ("sp");
  35. fp = (unsigned long)__builtin_frame_address(0);
  36. sp = current_sp;
  37. pc = (unsigned long)walk_stackframe;
  38. } else {
  39. /* task blocked in __switch_to */
  40. fp = task->thread.s[0];
  41. sp = task->thread.sp;
  42. pc = task->thread.ra;
  43. }
  44. for (;;) {
  45. unsigned long low, high;
  46. struct stackframe *frame;
  47. if (unlikely(!__kernel_text_address(pc) || fn(pc, arg)))
  48. break;
  49. /* Validate frame pointer */
  50. low = sp + sizeof(struct stackframe);
  51. high = ALIGN(sp, THREAD_SIZE);
  52. if (unlikely(fp < low || fp > high || fp & 0x7))
  53. break;
  54. /* Unwind stack frame */
  55. frame = (struct stackframe *)fp - 1;
  56. sp = fp;
  57. fp = frame->fp;
  58. pc = frame->ra - 0x4;
  59. }
  60. }
  61. #else /* !CONFIG_FRAME_POINTER */
  62. static void notrace walk_stackframe(struct task_struct *task,
  63. struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
  64. {
  65. unsigned long sp, pc;
  66. unsigned long *ksp;
  67. if (regs) {
  68. sp = GET_USP(regs);
  69. pc = GET_IP(regs);
  70. } else if (task == NULL || task == current) {
  71. const register unsigned long current_sp __asm__ ("sp");
  72. sp = current_sp;
  73. pc = (unsigned long)walk_stackframe;
  74. } else {
  75. /* task blocked in __switch_to */
  76. sp = task->thread.sp;
  77. pc = task->thread.ra;
  78. }
  79. if (unlikely(sp & 0x7))
  80. return;
  81. ksp = (unsigned long *)sp;
  82. while (!kstack_end(ksp)) {
  83. if (__kernel_text_address(pc) && unlikely(fn(pc, arg)))
  84. break;
  85. pc = (*ksp++) - 0x4;
  86. }
  87. }
  88. #endif /* CONFIG_FRAME_POINTER */
  89. static bool print_trace_address(unsigned long pc, void *arg)
  90. {
  91. print_ip_sym(pc);
  92. return false;
  93. }
  94. void show_stack(struct task_struct *task, unsigned long *sp)
  95. {
  96. pr_cont("Call Trace:\n");
  97. walk_stackframe(task, NULL, print_trace_address, NULL);
  98. }
  99. static bool save_wchan(unsigned long pc, void *arg)
  100. {
  101. if (!in_sched_functions(pc)) {
  102. unsigned long *p = arg;
  103. *p = pc;
  104. return true;
  105. }
  106. return false;
  107. }
  108. unsigned long get_wchan(struct task_struct *task)
  109. {
  110. unsigned long pc = 0;
  111. if (likely(task && task != current && task->state != TASK_RUNNING))
  112. walk_stackframe(task, NULL, save_wchan, &pc);
  113. return pc;
  114. }
  115. #ifdef CONFIG_STACKTRACE
  116. static bool __save_trace(unsigned long pc, void *arg, bool nosched)
  117. {
  118. struct stack_trace *trace = arg;
  119. if (unlikely(nosched && in_sched_functions(pc)))
  120. return false;
  121. if (unlikely(trace->skip > 0)) {
  122. trace->skip--;
  123. return false;
  124. }
  125. trace->entries[trace->nr_entries++] = pc;
  126. return (trace->nr_entries >= trace->max_entries);
  127. }
  128. static bool save_trace(unsigned long pc, void *arg)
  129. {
  130. return __save_trace(pc, arg, false);
  131. }
  132. /*
  133. * Save stack-backtrace addresses into a stack_trace buffer.
  134. */
  135. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  136. {
  137. walk_stackframe(tsk, NULL, save_trace, trace);
  138. if (trace->nr_entries < trace->max_entries)
  139. trace->entries[trace->nr_entries++] = ULONG_MAX;
  140. }
  141. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  142. void save_stack_trace(struct stack_trace *trace)
  143. {
  144. save_stack_trace_tsk(NULL, trace);
  145. }
  146. EXPORT_SYMBOL_GPL(save_stack_trace);
  147. #endif /* CONFIG_STACKTRACE */