stacktrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Stack tracing support
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/export.h>
  20. #include <linux/sched.h>
  21. #include <linux/stacktrace.h>
  22. #include <asm/stacktrace.h>
  23. /*
  24. * AArch64 PCS assigns the frame pointer to x29.
  25. *
  26. * A simple function prologue looks like this:
  27. * sub sp, sp, #0x10
  28. * stp x29, x30, [sp]
  29. * mov x29, sp
  30. *
  31. * A simple function epilogue looks like this:
  32. * mov sp, x29
  33. * ldp x29, x30, [sp]
  34. * add sp, sp, #0x10
  35. */
  36. int notrace unwind_frame(struct stackframe *frame)
  37. {
  38. unsigned long high, low;
  39. unsigned long fp = frame->fp;
  40. low = frame->sp;
  41. high = ALIGN(low, THREAD_SIZE);
  42. if (fp < low || fp > high - 0x18 || fp & 0xf)
  43. return -EINVAL;
  44. frame->sp = fp + 0x10;
  45. frame->fp = *(unsigned long *)(fp);
  46. frame->pc = *(unsigned long *)(fp + 8);
  47. return 0;
  48. }
  49. void notrace walk_stackframe(struct stackframe *frame,
  50. int (*fn)(struct stackframe *, void *), void *data)
  51. {
  52. while (1) {
  53. int ret;
  54. if (fn(frame, data))
  55. break;
  56. ret = unwind_frame(frame);
  57. if (ret < 0)
  58. break;
  59. }
  60. }
  61. EXPORT_SYMBOL(walk_stackframe);
  62. #ifdef CONFIG_STACKTRACE
  63. struct stack_trace_data {
  64. struct stack_trace *trace;
  65. unsigned int no_sched_functions;
  66. unsigned int skip;
  67. };
  68. static int save_trace(struct stackframe *frame, void *d)
  69. {
  70. struct stack_trace_data *data = d;
  71. struct stack_trace *trace = data->trace;
  72. unsigned long addr = frame->pc;
  73. if (data->no_sched_functions && in_sched_functions(addr))
  74. return 0;
  75. if (data->skip) {
  76. data->skip--;
  77. return 0;
  78. }
  79. trace->entries[trace->nr_entries++] = addr;
  80. return trace->nr_entries >= trace->max_entries;
  81. }
  82. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  83. {
  84. struct stack_trace_data data;
  85. struct stackframe frame;
  86. data.trace = trace;
  87. data.skip = trace->skip;
  88. if (tsk != current) {
  89. data.no_sched_functions = 1;
  90. frame.fp = thread_saved_fp(tsk);
  91. frame.sp = thread_saved_sp(tsk);
  92. frame.pc = thread_saved_pc(tsk);
  93. } else {
  94. data.no_sched_functions = 0;
  95. frame.fp = (unsigned long)__builtin_frame_address(0);
  96. frame.sp = current_stack_pointer;
  97. frame.pc = (unsigned long)save_stack_trace_tsk;
  98. }
  99. walk_stackframe(&frame, save_trace, &data);
  100. if (trace->nr_entries < trace->max_entries)
  101. trace->entries[trace->nr_entries++] = ULONG_MAX;
  102. }
  103. void save_stack_trace(struct stack_trace *trace)
  104. {
  105. save_stack_trace_tsk(current, trace);
  106. }
  107. EXPORT_SYMBOL_GPL(save_stack_trace);
  108. #endif