stack.c 651 B

12345678910111213141516171819202122232425262728293031
  1. #include <libcflat.h>
  2. #include <stack.h>
  3. int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)
  4. {
  5. static int walking;
  6. int depth = 0;
  7. const unsigned long *bp = (unsigned long *) frame;
  8. if (walking) {
  9. printf("RECURSIVE STACK WALK!!!\n");
  10. return 0;
  11. }
  12. walking = 1;
  13. for (depth = 0; bp && depth < max_depth; depth++) {
  14. return_addrs[depth] = (void *) bp[1];
  15. if (return_addrs[depth] == 0)
  16. break;
  17. bp = (unsigned long *) bp[0];
  18. }
  19. walking = 0;
  20. return depth;
  21. }
  22. int backtrace(const void **return_addrs, int max_depth)
  23. {
  24. return backtrace_frame(__builtin_frame_address(0), return_addrs,
  25. max_depth);
  26. }