safe_dump_win.ipp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef BOOST_STACKTRACE_DETAIL_SAFE_DUMP_WIN_IPP
  7. #define BOOST_STACKTRACE_DETAIL_SAFE_DUMP_WIN_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 <boost/core/noncopyable.hpp>
  14. #include <boost/winapi/get_current_process.hpp>
  15. #include <boost/winapi/file_management.hpp>
  16. #include <boost/winapi/handles.hpp>
  17. #include <boost/winapi/access_rights.hpp>
  18. namespace boost { namespace stacktrace { namespace detail {
  19. std::size_t dump(void* /*fd*/, const native_frame_ptr_t* /*frames*/, std::size_t /*frames_count*/) noexcept {
  20. #if 0 // This code potentially could cause deadlocks (according to the MSDN). Disabled
  21. boost::winapi::DWORD_ written;
  22. const boost::winapi::DWORD_ bytes_to_write = static_cast<boost::winapi::DWORD_>(
  23. sizeof(native_frame_ptr_t) * frames_count
  24. );
  25. if (!boost::winapi::WriteFile(fd, frames, bytes_to_write, &written, 0)) {
  26. return 0;
  27. }
  28. return frames_count;
  29. #endif
  30. return 0;
  31. }
  32. std::size_t dump(const char* /*file*/, const native_frame_ptr_t* /*frames*/, std::size_t /*frames_count*/) noexcept {
  33. #if 0 // This code causing deadlocks on some platforms. Disabled
  34. void* const fd = boost::winapi::CreateFileA(
  35. file,
  36. boost::winapi::GENERIC_WRITE_,
  37. 0,
  38. 0,
  39. boost::winapi::CREATE_ALWAYS_,
  40. boost::winapi::FILE_ATTRIBUTE_NORMAL_,
  41. 0
  42. );
  43. if (fd == boost::winapi::invalid_handle_value) {
  44. return 0;
  45. }
  46. const std::size_t size = boost::stacktrace::detail::dump(fd, frames, frames_count);
  47. boost::winapi::CloseHandle(fd);
  48. return size;
  49. #endif
  50. return 0;
  51. }
  52. }}} // namespace boost::stacktrace::detail
  53. #endif // BOOST_STACKTRACE_DETAIL_SAFE_DUMP_WIN_IPP