stacktrace.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, int savesched)
  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. (savesched || !in_sched_functions(addr))) {
  22. if (trace->skip > 0)
  23. trace->skip--;
  24. else
  25. trace->entries[trace->nr_entries++] = addr;
  26. if (trace->nr_entries >= trace->max_entries)
  27. break;
  28. }
  29. }
  30. }
  31. static void save_context_stack(struct stack_trace *trace,
  32. struct task_struct *tsk, struct pt_regs *regs, int savesched)
  33. {
  34. unsigned long sp = regs->regs[29];
  35. #ifdef CONFIG_KALLSYMS
  36. unsigned long ra = regs->regs[31];
  37. unsigned long pc = regs->cp0_epc;
  38. if (raw_show_trace || !__kernel_text_address(pc)) {
  39. unsigned long stack_page =
  40. (unsigned long)task_stack_page(tsk);
  41. if (stack_page && sp >= stack_page &&
  42. sp <= stack_page + THREAD_SIZE - 32)
  43. save_raw_context_stack(trace, sp, savesched);
  44. return;
  45. }
  46. do {
  47. if (savesched || !in_sched_functions(pc)) {
  48. if (trace->skip > 0)
  49. trace->skip--;
  50. else
  51. trace->entries[trace->nr_entries++] = pc;
  52. if (trace->nr_entries >= trace->max_entries)
  53. break;
  54. }
  55. pc = unwind_stack(tsk, &sp, pc, &ra);
  56. } while (pc);
  57. #else
  58. save_raw_context_stack(trace, sp, savesched);
  59. #endif
  60. }
  61. /*
  62. * Save stack-backtrace addresses into a stack_trace buffer.
  63. */
  64. void save_stack_trace(struct stack_trace *trace)
  65. {
  66. save_stack_trace_tsk(current, trace);
  67. }
  68. EXPORT_SYMBOL_GPL(save_stack_trace);
  69. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  70. {
  71. struct pt_regs dummyregs;
  72. struct pt_regs *regs = &dummyregs;
  73. WARN_ON(trace->nr_entries || !trace->max_entries);
  74. if (tsk != current) {
  75. regs->regs[29] = tsk->thread.reg29;
  76. regs->regs[31] = 0;
  77. regs->cp0_epc = tsk->thread.reg31;
  78. } else
  79. prepare_frametrace(regs);
  80. save_context_stack(trace, tsk, regs, tsk == current);
  81. }
  82. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);