stacktrace.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Stack trace management functions
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/stacktrace.h>
  8. #include <linux/export.h>
  9. #include <asm/stacktrace.h>
  10. /*
  11. * Save stack-backtrace addresses into a stack_trace buffer:
  12. */
  13. static void save_raw_context_stack(struct stack_trace *trace,
  14. unsigned long reg29)
  15. {
  16. unsigned long *sp = (unsigned long *)reg29;
  17. unsigned long addr;
  18. while (!kstack_end(sp)) {
  19. addr = *sp++;
  20. if (__kernel_text_address(addr)) {
  21. if (trace->skip > 0)
  22. trace->skip--;
  23. else
  24. trace->entries[trace->nr_entries++] = addr;
  25. if (trace->nr_entries >= trace->max_entries)
  26. break;
  27. }
  28. }
  29. }
  30. static void save_context_stack(struct stack_trace *trace,
  31. struct task_struct *tsk, struct pt_regs *regs)
  32. {
  33. unsigned long sp = regs->regs[29];
  34. #ifdef CONFIG_KALLSYMS
  35. unsigned long ra = regs->regs[31];
  36. unsigned long pc = regs->cp0_epc;
  37. if (raw_show_trace || !__kernel_text_address(pc)) {
  38. unsigned long stack_page =
  39. (unsigned long)task_stack_page(tsk);
  40. if (stack_page && sp >= stack_page &&
  41. sp <= stack_page + THREAD_SIZE - 32)
  42. save_raw_context_stack(trace, sp);
  43. return;
  44. }
  45. do {
  46. if (trace->skip > 0)
  47. trace->skip--;
  48. else
  49. trace->entries[trace->nr_entries++] = pc;
  50. if (trace->nr_entries >= trace->max_entries)
  51. break;
  52. pc = unwind_stack(tsk, &sp, pc, &ra);
  53. } while (pc);
  54. #else
  55. save_raw_context_stack(trace, sp);
  56. #endif
  57. }
  58. /*
  59. * Save stack-backtrace addresses into a stack_trace buffer.
  60. */
  61. void save_stack_trace(struct stack_trace *trace)
  62. {
  63. save_stack_trace_tsk(current, trace);
  64. }
  65. EXPORT_SYMBOL_GPL(save_stack_trace);
  66. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  67. {
  68. struct pt_regs dummyregs;
  69. struct pt_regs *regs = &dummyregs;
  70. WARN_ON(trace->nr_entries || !trace->max_entries);
  71. if (tsk != current) {
  72. regs->regs[29] = tsk->thread.reg29;
  73. regs->regs[31] = 0;
  74. regs->cp0_epc = tsk->thread.reg31;
  75. } else
  76. prepare_frametrace(regs);
  77. save_context_stack(trace, tsk, regs);
  78. }
  79. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);