safe_dump_posix.ipp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP
  7. #define BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <boost/stacktrace/safe_dump_to.hpp>
  13. #include <unistd.h> // ::write
  14. #include <fcntl.h> // ::open
  15. #include <sys/stat.h> // S_IWUSR and friends
  16. namespace boost { namespace stacktrace { namespace detail {
  17. std::size_t dump(int fd, const native_frame_ptr_t* frames, std::size_t frames_count) BOOST_NOEXCEPT {
  18. // We do not retry, because this function must be typically called from signal handler so it's:
  19. // * to scary to continue in case of EINTR
  20. // * EAGAIN or EWOULDBLOCK may occur only in case of O_NONBLOCK is set for fd,
  21. // so it seems that user does not want to block
  22. if (::write(fd, frames, sizeof(native_frame_ptr_t) * frames_count) == -1) {
  23. return 0;
  24. }
  25. return frames_count;
  26. }
  27. std::size_t dump(const char* file, const native_frame_ptr_t* frames, std::size_t frames_count) BOOST_NOEXCEPT {
  28. const int fd = ::open(
  29. file,
  30. O_CREAT | O_WRONLY | O_TRUNC,
  31. #if defined(S_IWUSR) && defined(S_IRUSR) // Workarounds for some Android OSes
  32. S_IWUSR | S_IRUSR
  33. #elif defined(S_IWRITE) && defined(S_IREAD)
  34. S_IWRITE | S_IREAD
  35. #else
  36. 0
  37. #endif
  38. );
  39. if (fd == -1) {
  40. return 0;
  41. }
  42. const std::size_t size = boost::stacktrace::detail::dump(fd, frames, frames_count);
  43. ::close(fd);
  44. return size;
  45. }
  46. }}} // namespace boost::stacktrace::detail
  47. #endif // BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP