libc_fatal.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Catastrophic failure reports. Linux version.
  2. Copyright (C) 1993-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <execinfo.h>
  17. #include <fcntl.h>
  18. #include <not-cancel.h>
  19. #include <string.h>
  20. #include <sys/mman.h>
  21. #include <sys/uio.h>
  22. static bool
  23. writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total)
  24. {
  25. INTERNAL_SYSCALL_DECL (err);
  26. ssize_t cnt;
  27. do
  28. cnt = INTERNAL_SYSCALL (writev, err, 3, fd, iov, niov);
  29. while (INTERNAL_SYSCALL_ERROR_P (cnt, err)
  30. && INTERNAL_SYSCALL_ERRNO (cnt, err) == EINTR);
  31. return cnt == total;
  32. }
  33. #define WRITEV_FOR_FATAL writev_for_fatal
  34. static void
  35. backtrace_and_maps (int do_abort, bool written, int fd)
  36. {
  37. if (do_abort > 1 && written)
  38. {
  39. void *addrs[64];
  40. #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
  41. int n = __backtrace (addrs, naddrs);
  42. if (n > 2)
  43. {
  44. #define strnsize(str) str, strlen (str)
  45. #define writestr(str) __write_nocancel (fd, str)
  46. writestr (strnsize ("======= Backtrace: =========\n"));
  47. __backtrace_symbols_fd (addrs + 1, n - 1, fd);
  48. writestr (strnsize ("======= Memory map: ========\n"));
  49. int fd2 = __open_nocancel ("/proc/self/maps", O_RDONLY);
  50. char buf[1024];
  51. ssize_t n2;
  52. while ((n2 = __read_nocancel (fd2, buf, sizeof (buf))) > 0)
  53. if (__write_nocancel (fd, buf, n2) != n2)
  54. break;
  55. __close_nocancel_nostatus (fd2);
  56. }
  57. }
  58. }
  59. #define BEFORE_ABORT backtrace_and_maps
  60. #include <sysdeps/posix/libc_fatal.c>