1 // Copyright Antony Polukhin, 2016-2017.
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)
7 #ifndef BOOST_STACKTRACE_DETAIL_FRAME_MSVC_IPP
8 #define BOOST_STACKTRACE_DETAIL_FRAME_MSVC_IPP
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #include <boost/stacktrace/frame.hpp>
17 #include <boost/core/demangle.hpp>
18 #include <boost/core/noncopyable.hpp>
19 #include <boost/lexical_cast.hpp>
20 #include <boost/stacktrace/detail/to_hex_array.hpp>
24 #include <boost/detail/winapi/get_current_process.hpp>
27 # pragma comment(lib, "ole32.lib")
28 # pragma comment(lib, "Dbgeng.lib")
32 #ifdef __CRT_UUID_DECL // for __MINGW32__
33 __CRT_UUID_DECL(IDebugClient,0x27fe5639,0x8407,0x4f47,0x83,0x64,0xee,0x11,0x8f,0xb0,0x8a,0xc8)
34 __CRT_UUID_DECL(IDebugControl,0x5182e668,0x105e,0x416e,0xad,0x92,0x24,0xef,0x80,0x04,0x24,0xba)
35 __CRT_UUID_DECL(IDebugSymbols,0x8c31e98c,0x983a,0x48a5,0x90,0x16,0x6f,0xe5,0xd6,0x67,0xa9,0x50)
36 #elif defined(DEFINE_GUID) && !defined(BOOST_MSVC)
37 DEFINE_GUID(IID_IDebugClient,0x27fe5639,0x8407,0x4f47,0x83,0x64,0xee,0x11,0x8f,0xb0,0x8a,0xc8);
38 DEFINE_GUID(IID_IDebugControl,0x5182e668,0x105e,0x416e,0xad,0x92,0x24,0xef,0x80,0x04,0x24,0xba);
39 DEFINE_GUID(IID_IDebugSymbols,0x8c31e98c,0x983a,0x48a5,0x90,0x16,0x6f,0xe5,0xd6,0x67,0xa9,0x50);
44 // Testing. Remove later
45 //# define __uuidof(x) ::IID_ ## x
47 namespace boost { namespace stacktrace { namespace detail {
49 class com_global_initer: boost::noncopyable {
53 com_global_initer() BOOST_NOEXCEPT
56 // COINIT_MULTITHREADED means that we must serialize access to the objects manually.
57 // This is the fastest way to work. If user calls CoInitializeEx before us - we
58 // can end up with other mode (which is OK for us).
60 // If we call CoInitializeEx befire user - user may end up with different mode, which is a problem.
61 // So we need to call that initialization function as late as possible.
62 const boost::detail::winapi::DWORD_ res = ::CoInitializeEx(0, COINIT_MULTITHREADED);
63 ok_ = (res == S_OK || res == S_FALSE);
66 ~com_global_initer() BOOST_NOEXCEPT {
75 class com_holder: boost::noncopyable {
79 com_holder(const com_global_initer&) BOOST_NOEXCEPT
83 T* operator->() const BOOST_NOEXCEPT {
87 void** to_void_ptr_ptr() BOOST_NOEXCEPT {
88 return reinterpret_cast<void**>(&holder_);
91 bool is_inited() const BOOST_NOEXCEPT {
95 ~com_holder() BOOST_NOEXCEPT {
103 static std::string minwg_demangling_workaround(const std::string& s) {
110 return boost::core::demangle(('_' + s).c_str());
113 return boost::core::demangle(s.c_str());
119 class debugging_symbols: boost::noncopyable {
120 static void try_init_com(com_holder< ::IDebugSymbols>& idebug, const com_global_initer& com) BOOST_NOEXCEPT {
121 com_holder< ::IDebugClient> iclient(com);
122 if (S_OK != ::DebugCreate(__uuidof(IDebugClient), iclient.to_void_ptr_ptr())) {
126 com_holder< ::IDebugControl> icontrol(com);
127 const bool res0 = (S_OK == iclient->QueryInterface(
128 __uuidof(IDebugControl),
129 icontrol.to_void_ptr_ptr()
135 const bool res1 = (S_OK == iclient->AttachProcess(
137 ::GetCurrentProcessId(),
138 DEBUG_ATTACH_NONINVASIVE | DEBUG_ATTACH_NONINVASIVE_NO_SUSPEND
144 if (S_OK != icontrol->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE)) {
148 // No cheking: QueryInterface sets the output parameter to NULL in case of error.
149 iclient->QueryInterface(__uuidof(IDebugSymbols), idebug.to_void_ptr_ptr());
152 #ifndef BOOST_STACKTRACE_USE_WINDBG_CACHED
154 boost::stacktrace::detail::com_global_initer com_;
155 com_holder< ::IDebugSymbols> idebug_;
157 debugging_symbols() BOOST_NOEXCEPT
161 try_init_com(idebug_, com_);
166 #ifdef BOOST_NO_CXX11_THREAD_LOCAL
167 # error Your compiler does not support C++11 thread_local storage. It`s impossible to build with BOOST_STACKTRACE_USE_WINDBG_CACHED.
170 static com_holder< ::IDebugSymbols>& get_thread_local_debug_inst() BOOST_NOEXCEPT {
171 // [class.mfct]: A static local variable or local type in a member function always refers to the same entity, whether
172 // or not the member function is inline.
173 static thread_local boost::stacktrace::detail::com_global_initer com;
174 static thread_local com_holder< ::IDebugSymbols> idebug(com);
176 if (!idebug.is_inited()) {
177 try_init_com(idebug, com);
183 com_holder< ::IDebugSymbols>& idebug_;
185 debugging_symbols() BOOST_NOEXCEPT
186 : idebug_( get_thread_local_debug_inst() )
189 #endif // #ifndef BOOST_STACKTRACE_USE_WINDBG_CACHED
191 bool is_inited() const BOOST_NOEXCEPT {
192 return idebug_.is_inited();
195 std::string get_name_impl(const void* addr, std::string* module_name = 0) const {
200 const ULONG64 offset = reinterpret_cast<ULONG64>(addr);
205 bool res = (S_OK == idebug_->GetNameByOffset(
213 if (!res && size != 0) {
215 res = (S_OK == idebug_->GetNameByOffset(
218 static_cast<ULONG>(result.size()),
231 const std::size_t delimiter = result.find_first_of('!');
233 *module_name = result.substr(0, delimiter);
236 if (delimiter == std::string::npos) {
237 // If 'delimiter' is equal to 'std::string::npos' then we have only module name.
242 result = minwg_demangling_workaround(
243 result.substr(delimiter + 1)
249 std::size_t get_line_impl(const void* addr) const BOOST_NOEXCEPT {
255 const bool is_ok = (S_OK == idebug_->GetLineByOffset(
256 reinterpret_cast<ULONG64>(addr),
264 return (is_ok ? result : 0);
267 std::pair<std::string, std::size_t> get_source_file_line_impl(const void* addr) const {
268 std::pair<std::string, std::size_t> result;
272 const ULONG64 offset = reinterpret_cast<ULONG64>(addr);
278 bool res = (S_OK == idebug_->GetLineByOffset(
289 result.second = line_num;
293 if (!res && size == 0) {
297 result.first.resize(size);
298 res = (S_OK == idebug_->GetLineByOffset(
302 static_cast<ULONG>(result.first.size()),
306 result.second = line_num;
309 result.first.clear();
316 void to_string_impl(const void* addr, std::string& res) const {
321 std::string module_name;
322 std::string name = this->get_name_impl(addr, &module_name);
326 res += to_hex_array(addr).data();
329 std::pair<std::string, std::size_t> source_line = this->get_source_file_line_impl(addr);
330 if (!source_line.first.empty() && source_line.second) {
332 res += source_line.first;
334 res += boost::lexical_cast<boost::array<char, 40> >(source_line.second).data();
335 } else if (!module_name.empty()) {
342 std::string to_string(const frame* frames, std::size_t size) {
343 boost::stacktrace::detail::debugging_symbols idebug;
344 if (!idebug.is_inited()) {
345 return std::string();
349 res.reserve(64 * size);
350 for (std::size_t i = 0; i < size; ++i) {
354 res += boost::lexical_cast<boost::array<char, 40> >(i).data();
357 idebug.to_string_impl(frames[i].address(), res);
364 } // namespace detail
366 std::string frame::name() const {
367 boost::stacktrace::detail::debugging_symbols idebug;
368 return idebug.get_name_impl(addr_);
372 std::string frame::source_file() const {
373 boost::stacktrace::detail::debugging_symbols idebug;
374 return idebug.get_source_file_line_impl(addr_).first;
377 std::size_t frame::source_line() const {
378 boost::stacktrace::detail::debugging_symbols idebug;
379 return idebug.get_line_impl(addr_);
382 std::string to_string(const frame& f) {
385 boost::stacktrace::detail::debugging_symbols idebug;
386 idebug.to_string_impl(f.address(), res);
390 }} // namespace boost::stacktrace
392 #endif // BOOST_STACKTRACE_DETAIL_FRAME_MSVC_IPP