stacktrace.c 987 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/sched.h>
  13. #include <linux/stacktrace.h>
  14. #include <asm/ptrace.h>
  15. /*
  16. * Save stack-backtrace addresses into a stack_trace buffer.
  17. */
  18. void save_stack_trace(struct stack_trace *trace)
  19. {
  20. unsigned long sp;
  21. asm("mr %0,1" : "=r" (sp));
  22. for (;;) {
  23. unsigned long *stack = (unsigned long *) sp;
  24. unsigned long newsp, ip;
  25. if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
  26. return;
  27. newsp = stack[0];
  28. ip = stack[STACK_FRAME_LR_SAVE];
  29. if (!trace->skip)
  30. trace->entries[trace->nr_entries++] = ip;
  31. else
  32. trace->skip--;
  33. if (trace->nr_entries >= trace->max_entries)
  34. return;
  35. sp = newsp;
  36. }
  37. }