stacktrace.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/stacktrace.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/processor.h>
  17. /*
  18. * Save stack-backtrace addresses into a stack_trace buffer.
  19. */
  20. static void save_context_stack(struct stack_trace *trace, unsigned long sp,
  21. struct task_struct *tsk, int savesched)
  22. {
  23. for (;;) {
  24. unsigned long *stack = (unsigned long *) sp;
  25. unsigned long newsp, ip;
  26. if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
  27. return;
  28. newsp = stack[0];
  29. ip = stack[STACK_FRAME_LR_SAVE];
  30. if (savesched || !in_sched_functions(ip)) {
  31. if (!trace->skip)
  32. trace->entries[trace->nr_entries++] = ip;
  33. else
  34. trace->skip--;
  35. }
  36. if (trace->nr_entries >= trace->max_entries)
  37. return;
  38. sp = newsp;
  39. }
  40. }
  41. void save_stack_trace(struct stack_trace *trace)
  42. {
  43. unsigned long sp;
  44. sp = current_stack_pointer();
  45. save_context_stack(trace, sp, current, 1);
  46. }
  47. EXPORT_SYMBOL_GPL(save_stack_trace);
  48. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  49. {
  50. save_context_stack(trace, tsk->thread.ksp, tsk, 0);
  51. }
  52. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  53. void
  54. save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  55. {
  56. save_context_stack(trace, regs->gpr[1], current, 0);
  57. }
  58. EXPORT_SYMBOL_GPL(save_stack_trace_regs);