| 1 | // Copyright Antony Polukhin, 2016-2024. |
| 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 | |
| 7 | #ifndef BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP |
| 8 | #define BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP |
| 9 | |
| 10 | #include <boost/config.hpp> |
| 11 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 12 | # pragma once |
| 13 | #endif |
| 14 | |
| 15 | #include <boost/stacktrace/safe_dump_to.hpp> |
| 16 | |
| 17 | #include <unistd.h> // ::write |
| 18 | #include <fcntl.h> // ::open |
| 19 | #include <sys/stat.h> // S_IWUSR and friends |
| 20 | |
| 21 | |
| 22 | namespace boost { namespace stacktrace { namespace detail { |
| 23 | |
| 24 | std::size_t dump(int fd, const native_frame_ptr_t* frames, std::size_t frames_count) noexcept { |
| 25 | // We do not retry, because this function must be typically called from signal handler so it's: |
| 26 | // * to scary to continue in case of EINTR |
| 27 | // * EAGAIN or EWOULDBLOCK may occur only in case of O_NONBLOCK is set for fd, |
| 28 | // so it seems that user does not want to block |
| 29 | if (::write(fd: fd, buf: frames, n: sizeof(native_frame_ptr_t) * frames_count) == -1) { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | return frames_count; |
| 34 | } |
| 35 | |
| 36 | std::size_t dump(const char* file, const native_frame_ptr_t* frames, std::size_t frames_count) noexcept { |
| 37 | const int fd = ::open( |
| 38 | file: file, |
| 39 | O_CREAT | O_WRONLY | O_TRUNC, |
| 40 | #if defined(S_IWUSR) && defined(S_IRUSR) // Workarounds for some Android OSes |
| 41 | S_IWUSR | S_IRUSR |
| 42 | #elif defined(S_IWRITE) && defined(S_IREAD) |
| 43 | S_IWRITE | S_IREAD |
| 44 | #else |
| 45 | 0 |
| 46 | #endif |
| 47 | ); |
| 48 | if (fd == -1) { |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | const std::size_t size = boost::stacktrace::detail::dump(fd, frames, frames_count); |
| 53 | ::close(fd: fd); |
| 54 | return size; |
| 55 | } |
| 56 | |
| 57 | }}} // namespace boost::stacktrace::detail |
| 58 | |
| 59 | #endif // BOOST_STACKTRACE_DETAIL_SAFE_DUMP_POSIX_IPP |
| 60 | |