1
0

heap__get_stacktrace.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Generic stacktrace function
  3. */
  4. #ifdef __GNUC__
  5. #include <string.h>
  6. #include "private.h"
  7. #include "context.h"
  8. /*--------------------------------------------------------- */
  9. void lub_heap__get_stackframe(stackframe_t * stack, unsigned max)
  10. {
  11. #define GET_RETURN_ADDRESS(n) \
  12. { \
  13. if(n < max) \
  14. { \
  15. unsigned long fn = (unsigned long)__builtin_return_address(n+2); \
  16. if(!fn || (fn > (unsigned long)lub_heap_data_end)) \
  17. { \
  18. break; \
  19. } \
  20. stack->backtrace[n] = (function_t*)fn; \
  21. } \
  22. else \
  23. { \
  24. break; \
  25. } \
  26. } \
  27. memset(stack, 0, sizeof(*stack));
  28. do {
  29. GET_RETURN_ADDRESS(0);
  30. GET_RETURN_ADDRESS(1);
  31. GET_RETURN_ADDRESS(2);
  32. GET_RETURN_ADDRESS(3);
  33. GET_RETURN_ADDRESS(4);
  34. GET_RETURN_ADDRESS(5);
  35. GET_RETURN_ADDRESS(6);
  36. GET_RETURN_ADDRESS(7);
  37. GET_RETURN_ADDRESS(8);
  38. GET_RETURN_ADDRESS(9);
  39. GET_RETURN_ADDRESS(10);
  40. GET_RETURN_ADDRESS(11);
  41. GET_RETURN_ADDRESS(12);
  42. GET_RETURN_ADDRESS(13);
  43. GET_RETURN_ADDRESS(14);
  44. GET_RETURN_ADDRESS(15);
  45. } while (0);
  46. }
  47. /*--------------------------------------------------------- */
  48. #else /* not __GNUC__ */
  49. #warning "Generic stack backtrace not implemented for non-GCC compiler..."
  50. #endif /* not __GNUC__ */