stacktrace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /*
  47. * -4 here because we care about the PC at time of bl,
  48. * not where the return will go.
  49. */
  50. frame->pc = *(unsigned long *)(fp + 8) - 4;
  51. return 0;
  52. }
  53. void notrace walk_stackframe(struct stackframe *frame,
  54. int (*fn)(struct stackframe *, void *), void *data)
  55. {
  56. while (1) {
  57. int ret;
  58. if (fn(frame, data))
  59. break;
  60. ret = unwind_frame(frame);
  61. if (ret < 0)
  62. break;
  63. }
  64. }
  65. EXPORT_SYMBOL(walk_stackframe);
  66. #ifdef CONFIG_STACKTRACE
  67. struct stack_trace_data {
  68. struct stack_trace *trace;
  69. unsigned int no_sched_functions;
  70. unsigned int skip;
  71. };
  72. static int save_trace(struct stackframe *frame, void *d)
  73. {
  74. struct stack_trace_data *data = d;
  75. struct stack_trace *trace = data->trace;
  76. unsigned long addr = frame->pc;
  77. if (data->no_sched_functions && in_sched_functions(addr))
  78. return 0;
  79. if (data->skip) {
  80. data->skip--;
  81. return 0;
  82. }
  83. trace->entries[trace->nr_entries++] = addr;
  84. return trace->nr_entries >= trace->max_entries;
  85. }
  86. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  87. {
  88. struct stack_trace_data data;
  89. struct stackframe frame;
  90. data.trace = trace;
  91. data.skip = trace->skip;
  92. if (tsk != current) {
  93. data.no_sched_functions = 1;
  94. frame.fp = thread_saved_fp(tsk);
  95. frame.sp = thread_saved_sp(tsk);
  96. frame.pc = thread_saved_pc(tsk);
  97. } else {
  98. data.no_sched_functions = 0;
  99. frame.fp = (unsigned long)__builtin_frame_address(0);
  100. frame.sp = current_stack_pointer;
  101. frame.pc = (unsigned long)save_stack_trace_tsk;
  102. }
  103. walk_stackframe(&frame, save_trace, &data);
  104. if (trace->nr_entries < trace->max_entries)
  105. trace->entries[trace->nr_entries++] = ULONG_MAX;
  106. }
  107. void save_stack_trace(struct stack_trace *trace)
  108. {
  109. save_stack_trace_tsk(current, trace);
  110. }
  111. EXPORT_SYMBOL_GPL(save_stack_trace);
  112. #endif