1
0

heap__get_stacktrace.c 2.4 KB

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