Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <execinfo.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6.  
  7. static void full_write(int fd, const char *buf, size_t len)
  8. {
  9. while (len > 0) {
  10. ssize_t ret = write(fd, buf, len);
  11.  
  12. if ((ret == -1) && (errno != EINTR))
  13. break;
  14.  
  15. buf += (size_t) ret;
  16. len -= (size_t) ret;
  17. }
  18. }
  19.  
  20. void print_backtrace(void)
  21. {
  22. static const char start[] = "BACKTRACE ------------\n";
  23. static const char end[] = "----------------------\n";
  24.  
  25. void *bt[1024];
  26. int bt_size;
  27. char **bt_syms;
  28. int i;
  29.  
  30. bt_size = backtrace(bt, 1024);
  31. bt_syms = backtrace_symbols(bt, bt_size);
  32. full_write(STDERR_FILENO, start, strlen(start));
  33. for (i = 1; i < bt_size; i++) {
  34. size_t len = strlen(bt_syms[i]);
  35. full_write(STDERR_FILENO, bt_syms[i], len);
  36. full_write(STDERR_FILENO, "\n", 1);
  37. }
  38. full_write(STDERR_FILENO, end, strlen(end));
  39. free(bt_syms);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement