stacktrace.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Stack trace utility
  3. *
  4. * Copyright 2008 Christoph Hellwig, IBM Corp.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/export.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/stacktrace.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/processor.h>
  18. /*
  19. * Save stack-backtrace addresses into a stack_trace buffer.
  20. */
  21. static void save_context_stack(struct stack_trace *trace, unsigned long sp,
  22. struct task_struct *tsk, int savesched)
  23. {
  24. for (;;) {
  25. unsigned long *stack = (unsigned long *) sp;
  26. unsigned long newsp, ip;
  27. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  28. return;
  29. newsp = stack[0];
  30. ip = stack[STACK_FRAME_LR_SAVE];
  31. if (savesched || !in_sched_functions(ip)) {
  32. if (!trace->skip)
  33. trace->entries[trace->nr_entries++] = ip;
  34. else
  35. trace->skip--;
  36. }
  37. if (trace->nr_entries >= trace->max_entries)
  38. return;
  39. sp = newsp;
  40. }
  41. }
  42. void save_stack_trace(struct stack_trace *trace)
  43. {
  44. unsigned long sp;
  45. sp = current_stack_pointer();
  46. save_context_stack(trace, sp, current, 1);
  47. }
  48. EXPORT_SYMBOL_GPL(save_stack_trace);
  49. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  50. {
  51. unsigned long sp;
  52. if (tsk == current)
  53. sp = current_stack_pointer();
  54. else
  55. sp = tsk->thread.ksp;
  56. save_context_stack(trace, sp, tsk, 0);
  57. }
  58. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  59. void
  60. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  61. {
  62. save_context_stack(trace, regs->gpr[1], current, 0);
  63. }
  64. EXPORT_SYMBOL_GPL(save_stack_trace_regs);