stacktrace.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/sched.h>
  3. #include <linux/sched/debug.h>
  4. #include <linux/stacktrace.h>
  5. #include <linux/thread_info.h>
  6. #include <linux/ftrace.h>
  7. #include <linux/export.h>
  8. #include <asm/ptrace.h>
  9. #include <asm/stacktrace.h>
  10. #include "kstack.h"
  11. static void __save_stack_trace(struct thread_info *tp,
  12. struct stack_trace *trace,
  13. bool skip_sched)
  14. {
  15. unsigned long ksp, fp;
  16. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  17. struct task_struct *t;
  18. int graph = 0;
  19. #endif
  20. if (tp == current_thread_info()) {
  21. stack_trace_flush();
  22. __asm__ __volatile__("mov %%fp, %0" : "=r" (ksp));
  23. } else {
  24. ksp = tp->ksp;
  25. }
  26. fp = ksp + STACK_BIAS;
  27. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  28. t = tp->task;
  29. #endif
  30. do {
  31. struct sparc_stackf *sf;
  32. struct pt_regs *regs;
  33. unsigned long pc;
  34. if (!kstack_valid(tp, fp))
  35. break;
  36. sf = (struct sparc_stackf *) fp;
  37. regs = (struct pt_regs *) (sf + 1);
  38. if (kstack_is_trap_frame(tp, regs)) {
  39. if (!(regs->tstate & TSTATE_PRIV))
  40. break;
  41. pc = regs->tpc;
  42. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  43. } else {
  44. pc = sf->callers_pc;
  45. fp = (unsigned long)sf->fp + STACK_BIAS;
  46. }
  47. if (trace->skip > 0)
  48. trace->skip--;
  49. else if (!skip_sched || !in_sched_functions(pc)) {
  50. trace->entries[trace->nr_entries++] = pc;
  51. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  52. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  53. struct ftrace_ret_stack *ret_stack;
  54. ret_stack = ftrace_graph_get_ret_stack(t,
  55. graph);
  56. if (ret_stack) {
  57. pc = ret_stack->ret;
  58. if (trace->nr_entries <
  59. trace->max_entries)
  60. trace->entries[trace->nr_entries++] = pc;
  61. graph++;
  62. }
  63. }
  64. #endif
  65. }
  66. } while (trace->nr_entries < trace->max_entries);
  67. }
  68. void save_stack_trace(struct stack_trace *trace)
  69. {
  70. __save_stack_trace(current_thread_info(), trace, false);
  71. }
  72. EXPORT_SYMBOL_GPL(save_stack_trace);
  73. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  74. {
  75. struct thread_info *tp = task_thread_info(tsk);
  76. __save_stack_trace(tp, trace, true);
  77. }
  78. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);