stacktrace.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 ARM Limited
  4. * Copyright (C) 2014 Regents of the University of California
  5. */
  6. #include <linux/export.h>
  7. #include <linux/kallsyms.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/stacktrace.h>
  12. #include <linux/ftrace.h>
  13. #include <asm/stacktrace.h>
  14. #ifdef CONFIG_FRAME_POINTER
  15. void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
  16. bool (*fn)(void *, unsigned long), void *arg)
  17. {
  18. unsigned long fp, sp, pc;
  19. int level = 0;
  20. if (regs) {
  21. fp = frame_pointer(regs);
  22. sp = user_stack_pointer(regs);
  23. pc = instruction_pointer(regs);
  24. } else if (task == NULL || task == current) {
  25. fp = (unsigned long)__builtin_frame_address(0);
  26. sp = current_stack_pointer;
  27. pc = (unsigned long)walk_stackframe;
  28. level = -1;
  29. } else {
  30. /* task blocked in __switch_to */
  31. fp = task->thread.s[0];
  32. sp = task->thread.sp;
  33. pc = task->thread.ra;
  34. }
  35. for (;;) {
  36. unsigned long low, high;
  37. struct stackframe *frame;
  38. if (unlikely(!__kernel_text_address(pc) || (level++ >= 0 && !fn(arg, pc))))
  39. break;
  40. /* Validate frame pointer */
  41. low = sp + sizeof(struct stackframe);
  42. high = ALIGN(sp, THREAD_SIZE);
  43. if (unlikely(fp < low || fp > high || fp & 0x7))
  44. break;
  45. /* Unwind stack frame */
  46. frame = (struct stackframe *)fp - 1;
  47. sp = fp;
  48. if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
  49. fp = frame->ra;
  50. pc = regs->ra;
  51. } else {
  52. fp = frame->fp;
  53. pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
  54. &frame->ra);
  55. }
  56. }
  57. }
  58. #else /* !CONFIG_FRAME_POINTER */
  59. void notrace walk_stackframe(struct task_struct *task,
  60. struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
  61. {
  62. unsigned long sp, pc;
  63. unsigned long *ksp;
  64. if (regs) {
  65. sp = user_stack_pointer(regs);
  66. pc = instruction_pointer(regs);
  67. } else if (task == NULL || task == current) {
  68. sp = current_stack_pointer;
  69. pc = (unsigned long)walk_stackframe;
  70. } else {
  71. /* task blocked in __switch_to */
  72. sp = task->thread.sp;
  73. pc = task->thread.ra;
  74. }
  75. if (unlikely(sp & 0x7))
  76. return;
  77. ksp = (unsigned long *)sp;
  78. while (!kstack_end(ksp)) {
  79. if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
  80. break;
  81. pc = READ_ONCE_NOCHECK(*ksp++) - 0x4;
  82. }
  83. }
  84. #endif /* CONFIG_FRAME_POINTER */
  85. static bool print_trace_address(void *arg, unsigned long pc)
  86. {
  87. const char *loglvl = arg;
  88. print_ip_sym(loglvl, pc);
  89. return true;
  90. }
  91. noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
  92. const char *loglvl)
  93. {
  94. walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
  95. }
  96. void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
  97. {
  98. pr_cont("%sCall Trace:\n", loglvl);
  99. dump_backtrace(NULL, task, loglvl);
  100. }
  101. static bool save_wchan(void *arg, unsigned long pc)
  102. {
  103. if (!in_sched_functions(pc)) {
  104. unsigned long *p = arg;
  105. *p = pc;
  106. return false;
  107. }
  108. return true;
  109. }
  110. unsigned long __get_wchan(struct task_struct *task)
  111. {
  112. unsigned long pc = 0;
  113. if (!try_get_task_stack(task))
  114. return 0;
  115. walk_stackframe(task, NULL, save_wchan, &pc);
  116. put_task_stack(task);
  117. return pc;
  118. }
  119. noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  120. struct task_struct *task, struct pt_regs *regs)
  121. {
  122. walk_stackframe(task, regs, consume_entry, cookie);
  123. }