stacktrace.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * linux/arch/unicore32/kernel/stacktrace.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/stacktrace.h>
  15. #include <asm/stacktrace.h>
  16. #if defined(CONFIG_FRAME_POINTER)
  17. /*
  18. * Unwind the current stack frame and store the new register values in the
  19. * structure passed as argument. Unwinding is equivalent to a function return,
  20. * hence the new PC value rather than LR should be used for backtrace.
  21. *
  22. * With framepointer enabled, a simple function prologue looks like this:
  23. * mov ip, sp
  24. * stmdb sp!, {fp, ip, lr, pc}
  25. * sub fp, ip, #4
  26. *
  27. * A simple function epilogue looks like this:
  28. * ldm sp, {fp, sp, pc}
  29. *
  30. * Note that with framepointer enabled, even the leaf functions have the same
  31. * prologue and epilogue, therefore we can ignore the LR value in this case.
  32. */
  33. int notrace unwind_frame(struct stackframe *frame)
  34. {
  35. unsigned long high, low;
  36. unsigned long fp = frame->fp;
  37. /* only go to a higher address on the stack */
  38. low = frame->sp;
  39. high = ALIGN(low, THREAD_SIZE);
  40. /* check current frame pointer is within bounds */
  41. if (fp < (low + 12) || fp + 4 >= high)
  42. return -EINVAL;
  43. /* restore the registers from the stack frame */
  44. frame->fp = *(unsigned long *)(fp - 12);
  45. frame->sp = *(unsigned long *)(fp - 8);
  46. frame->pc = *(unsigned long *)(fp - 4);
  47. return 0;
  48. }
  49. #endif
  50. void notrace walk_stackframe(struct stackframe *frame,
  51. int (*fn)(struct stackframe *, void *), void *data)
  52. {
  53. while (1) {
  54. int ret;
  55. if (fn(frame, data))
  56. break;
  57. ret = unwind_frame(frame);
  58. if (ret < 0)
  59. break;
  60. }
  61. }
  62. EXPORT_SYMBOL(walk_stackframe);
  63. #ifdef CONFIG_STACKTRACE
  64. struct stack_trace_data {
  65. struct stack_trace *trace;
  66. unsigned int no_sched_functions;
  67. unsigned int skip;
  68. };
  69. static int save_trace(struct stackframe *frame, void *d)
  70. {
  71. struct stack_trace_data *data = d;
  72. struct stack_trace *trace = data->trace;
  73. unsigned long addr = frame->pc;
  74. if (data->no_sched_functions && in_sched_functions(addr))
  75. return 0;
  76. if (data->skip) {
  77. data->skip--;
  78. return 0;
  79. }
  80. trace->entries[trace->nr_entries++] = addr;
  81. return trace->nr_entries >= trace->max_entries;
  82. }
  83. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  84. {
  85. struct stack_trace_data data;
  86. struct stackframe frame;
  87. data.trace = trace;
  88. data.skip = trace->skip;
  89. if (tsk != current) {
  90. data.no_sched_functions = 1;
  91. frame.fp = thread_saved_fp(tsk);
  92. frame.sp = thread_saved_sp(tsk);
  93. frame.lr = 0; /* recovered from the stack */
  94. frame.pc = thread_saved_pc(tsk);
  95. } else {
  96. register unsigned long current_sp asm("sp");
  97. data.no_sched_functions = 0;
  98. frame.fp = (unsigned long)__builtin_frame_address(0);
  99. frame.sp = current_sp;
  100. frame.lr = (unsigned long)__builtin_return_address(0);
  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