backtrace.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Arm specific backtracing code for oprofile
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * Based on i386 oprofile backtrace code by John Levon, David Smith
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/oprofile.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/ptrace.h>
  20. #include "../kernel/stacktrace.h"
  21. static int report_trace(struct stackframe *frame, void *d)
  22. {
  23. unsigned int *depth = d;
  24. if (*depth) {
  25. oprofile_add_trace(frame->lr);
  26. (*depth)--;
  27. }
  28. return *depth == 0;
  29. }
  30. /*
  31. * The registers we're interested in are at the end of the variable
  32. * length saved register structure. The fp points at the end of this
  33. * structure so the address of this struct is:
  34. * (struct frame_tail *)(xxx->fp)-1
  35. */
  36. struct frame_tail {
  37. struct frame_tail *fp;
  38. unsigned long sp;
  39. unsigned long lr;
  40. } __attribute__((packed));
  41. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  42. {
  43. struct frame_tail buftail[2];
  44. /* Also check accessibility of one struct frame_tail beyond */
  45. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  46. return NULL;
  47. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  48. return NULL;
  49. oprofile_add_trace(buftail[0].lr);
  50. /* frame pointers should strictly progress back up the stack
  51. * (towards higher addresses) */
  52. if (tail >= buftail[0].fp)
  53. return NULL;
  54. return buftail[0].fp-1;
  55. }
  56. void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  57. {
  58. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  59. if (!user_mode(regs)) {
  60. unsigned long base = ((unsigned long)regs) & ~(THREAD_SIZE - 1);
  61. walk_stackframe(regs->ARM_fp, base, base + THREAD_SIZE,
  62. report_trace, &depth);
  63. return;
  64. }
  65. while (depth-- && tail && !((unsigned long) tail & 3))
  66. tail = user_backtrace(tail);
  67. }