stacktrace.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Stack trace management functions
  3. *
  4. * Copyright IBM Corp. 2006
  5. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/stacktrace.h>
  9. #include <linux/kallsyms.h>
  10. #include <linux/module.h>
  11. static unsigned long save_context_stack(struct stack_trace *trace,
  12. unsigned long sp,
  13. unsigned long low,
  14. unsigned long high,
  15. int savesched)
  16. {
  17. struct stack_frame *sf;
  18. struct pt_regs *regs;
  19. unsigned long addr;
  20. while(1) {
  21. sp &= PSW_ADDR_INSN;
  22. if (sp < low || sp > high)
  23. return sp;
  24. sf = (struct stack_frame *)sp;
  25. while(1) {
  26. addr = sf->gprs[8] & PSW_ADDR_INSN;
  27. if (!trace->skip)
  28. trace->entries[trace->nr_entries++] = addr;
  29. else
  30. trace->skip--;
  31. if (trace->nr_entries >= trace->max_entries)
  32. return sp;
  33. low = sp;
  34. sp = sf->back_chain & PSW_ADDR_INSN;
  35. if (!sp)
  36. break;
  37. if (sp <= low || sp > high - sizeof(*sf))
  38. return sp;
  39. sf = (struct stack_frame *)sp;
  40. }
  41. /* Zero backchain detected, check for interrupt frame. */
  42. sp = (unsigned long)(sf + 1);
  43. if (sp <= low || sp > high - sizeof(*regs))
  44. return sp;
  45. regs = (struct pt_regs *)sp;
  46. addr = regs->psw.addr & PSW_ADDR_INSN;
  47. if (savesched || !in_sched_functions(addr)) {
  48. if (!trace->skip)
  49. trace->entries[trace->nr_entries++] = addr;
  50. else
  51. trace->skip--;
  52. }
  53. if (trace->nr_entries >= trace->max_entries)
  54. return sp;
  55. low = sp;
  56. sp = regs->gprs[15];
  57. }
  58. }
  59. void save_stack_trace(struct stack_trace *trace)
  60. {
  61. register unsigned long sp asm ("15");
  62. unsigned long orig_sp, new_sp;
  63. orig_sp = sp & PSW_ADDR_INSN;
  64. new_sp = save_context_stack(trace, orig_sp,
  65. S390_lowcore.panic_stack - PAGE_SIZE,
  66. S390_lowcore.panic_stack, 1);
  67. if (new_sp != orig_sp)
  68. return;
  69. new_sp = save_context_stack(trace, new_sp,
  70. S390_lowcore.async_stack - ASYNC_SIZE,
  71. S390_lowcore.async_stack, 1);
  72. if (new_sp != orig_sp)
  73. return;
  74. save_context_stack(trace, new_sp,
  75. S390_lowcore.thread_info,
  76. S390_lowcore.thread_info + THREAD_SIZE, 1);
  77. }
  78. EXPORT_SYMBOL_GPL(save_stack_trace);
  79. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  80. {
  81. unsigned long sp, low, high;
  82. sp = tsk->thread.ksp & PSW_ADDR_INSN;
  83. low = (unsigned long) task_stack_page(tsk);
  84. high = (unsigned long) task_pt_regs(tsk);
  85. save_context_stack(trace, sp, low, high, 0);
  86. if (trace->nr_entries < trace->max_entries)
  87. trace->entries[trace->nr_entries++] = ULONG_MAX;
  88. }
  89. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);