|
| 1 | +// Copyright Antony Polukhin, 2016. |
| 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_BACKTRACE_HOLDER_WINDOWS_HPP |
| 8 | +#define BOOST_STACKTRACE_DETAIL_BACKTRACE_HOLDER_WINDOWS_HPP |
| 9 | + |
| 10 | +#include <boost/config.hpp> |
| 11 | +#ifdef BOOST_HAS_PRAGMA_ONCE |
| 12 | +# pragma once |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <windows.h> |
| 16 | +#include "DbgHelp.h" |
| 17 | +#include <WinBase.h> |
| 18 | + |
| 19 | +#include <boost/detail/winapi/get_current_process.hpp> |
| 20 | +#include <boost/detail/winapi/sym_from_addr.hpp> |
| 21 | + |
| 22 | +#if !defined(BOOST_ALL_NO_LIB) |
| 23 | +# define BOOST_LIB_NAME Dbghelp |
| 24 | +# ifdef BOOST_STACKTRACE_DYN_LINK |
| 25 | +# define BOOST_DYN_LINK |
| 26 | +# endif |
| 27 | +# include <boost/config/auto_link.hpp> |
| 28 | +#endif |
| 29 | + |
| 30 | +namespace boost { namespace stacktrace { namespace detail { |
| 31 | + |
| 32 | +struct symbol_info_with_stack { |
| 33 | + BOOST_STATIC_CONSTEXPR std::size_t max_name_length = MAX_SYM_NAME * sizeof(char); |
| 34 | + boost::detail::winapi::SYMBOL_INFO_ symbol; |
| 35 | + char name_part[max_name_length]; |
| 36 | +}; |
| 37 | + |
| 38 | +struct symbol_initialization_structure { |
| 39 | + boost::detail::winapi::HANDLE_ process; |
| 40 | + |
| 41 | + inline symbol_initialization_structure() BOOST_NOEXCEPT |
| 42 | + : process(boost::detail::winapi::GetCurrentProcess()) |
| 43 | + { |
| 44 | + SymInitialize(process, 0, true); |
| 45 | + } |
| 46 | + |
| 47 | + inline ~symbol_initialization_structure() BOOST_NOEXCEPT { |
| 48 | + SymCleanup(process); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +struct backtrace_holder { |
| 53 | + BOOST_STATIC_CONSTEXPR std::size_t max_size = 100u; |
| 54 | + |
| 55 | + std::size_t frames_count; |
| 56 | + void* buffer[max_size]; |
| 57 | + |
| 58 | + inline std::size_t size() const BOOST_NOEXCEPT { |
| 59 | + return frames_count; |
| 60 | + } |
| 61 | + |
| 62 | + inline std::string get_frame(std::size_t frame) const { |
| 63 | + std::string res; |
| 64 | + |
| 65 | + static symbol_initialization_structure symproc; |
| 66 | + |
| 67 | + if (frame >= frames_count) { |
| 68 | + return res; |
| 69 | + } |
| 70 | + |
| 71 | + symbol_info_with_stack s; |
| 72 | + s.symbol.MaxNameLen = symbol_info_with_stack::max_name_length; |
| 73 | + s.symbol.SizeOfStruct = sizeof(boost::detail::winapi::SYMBOL_INFO_); |
| 74 | + const bool sym_res = !!boost::detail::winapi::SymFromAddr( |
| 75 | + symproc.process, reinterpret_cast<boost::detail::winapi::ULONGLONG_>(buffer[frame]), 0, &s.symbol |
| 76 | + ); |
| 77 | + if (sym_res) { |
| 78 | + res = s.symbol.Name; |
| 79 | + } |
| 80 | + return res; |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +}}} // namespace boost::stacktrace::detail |
| 85 | + |
| 86 | +#endif // BOOST_STACKTRACE_DETAIL_BACKTRACE_HOLDER_WINDOWS_HPP |
0 commit comments