return_address.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * arch/arm/kernel/return_address.c
  3. *
  4. * Copyright (C) 2009 Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
  5. * for Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/ftrace.h>
  13. #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
  14. #include <linux/sched.h>
  15. #include <asm/stacktrace.h>
  16. struct return_address_data {
  17. unsigned int level;
  18. void *addr;
  19. };
  20. static int save_return_addr(struct stackframe *frame, void *d)
  21. {
  22. struct return_address_data *data = d;
  23. if (!data->level) {
  24. data->addr = (void *)frame->pc;
  25. return 1;
  26. } else {
  27. --data->level;
  28. return 0;
  29. }
  30. }
  31. void *return_address(unsigned int level)
  32. {
  33. struct return_address_data data;
  34. struct stackframe frame;
  35. data.level = level + 2;
  36. data.addr = NULL;
  37. frame.fp = (unsigned long)__builtin_frame_address(0);
  38. frame.sp = current_stack_pointer;
  39. frame.lr = (unsigned long)__builtin_return_address(0);
  40. frame.pc = (unsigned long)return_address;
  41. walk_stackframe(&frame, save_return_addr, &data);
  42. if (!data.level)
  43. return data.addr;
  44. else
  45. return NULL;
  46. }
  47. #endif /* if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND) */
  48. EXPORT_SYMBOL_GPL(return_address);