frame_msvc.ipp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_FRAME_MSVC_IPP
  7. #define BOOST_STACKTRACE_DETAIL_FRAME_MSVC_IPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <boost/stacktrace/frame.hpp>
  13. #include <boost/core/demangle.hpp>
  14. #include <boost/core/noncopyable.hpp>
  15. #include <boost/stacktrace/detail/to_dec_array.hpp>
  16. #include <boost/stacktrace/detail/to_hex_array.hpp>
  17. #include <windows.h>
  18. #include "dbgeng.h"
  19. #ifdef BOOST_MSVC
  20. # pragma comment(lib, "ole32.lib")
  21. # pragma comment(lib, "Dbgeng.lib")
  22. #endif
  23. #ifdef __CRT_UUID_DECL // for __MINGW32__
  24. __CRT_UUID_DECL(IDebugClient,0x27fe5639,0x8407,0x4f47,0x83,0x64,0xee,0x11,0x8f,0xb0,0x8a,0xc8)
  25. __CRT_UUID_DECL(IDebugControl,0x5182e668,0x105e,0x416e,0xad,0x92,0x24,0xef,0x80,0x04,0x24,0xba)
  26. __CRT_UUID_DECL(IDebugSymbols,0x8c31e98c,0x983a,0x48a5,0x90,0x16,0x6f,0xe5,0xd6,0x67,0xa9,0x50)
  27. #elif defined(DEFINE_GUID) && !defined(BOOST_MSVC)
  28. DEFINE_GUID(IID_IDebugClient,0x27fe5639,0x8407,0x4f47,0x83,0x64,0xee,0x11,0x8f,0xb0,0x8a,0xc8);
  29. DEFINE_GUID(IID_IDebugControl,0x5182e668,0x105e,0x416e,0xad,0x92,0x24,0xef,0x80,0x04,0x24,0xba);
  30. DEFINE_GUID(IID_IDebugSymbols,0x8c31e98c,0x983a,0x48a5,0x90,0x16,0x6f,0xe5,0xd6,0x67,0xa9,0x50);
  31. #endif
  32. // Testing. Remove later
  33. //# define __uuidof(x) ::IID_ ## x
  34. namespace boost { namespace stacktrace { namespace detail {
  35. class com_global_initer: boost::noncopyable {
  36. bool ok_;
  37. public:
  38. com_global_initer() BOOST_NOEXCEPT
  39. : ok_(false)
  40. {
  41. // COINIT_MULTITHREADED means that we must serialize access to the objects manually.
  42. // This is the fastest way to work. If user calls CoInitializeEx before us - we
  43. // can end up with other mode (which is OK for us).
  44. //
  45. // If we call CoInitializeEx befire user - user may end up with different mode, which is a problem.
  46. // So we need to call that initialization function as late as possible.
  47. const DWORD res = ::CoInitializeEx(0, COINIT_MULTITHREADED);
  48. ok_ = (res == S_OK || res == S_FALSE);
  49. }
  50. ~com_global_initer() BOOST_NOEXCEPT {
  51. if (ok_) {
  52. ::CoUninitialize();
  53. }
  54. }
  55. };
  56. template <class T>
  57. class com_holder: boost::noncopyable {
  58. T* holder_;
  59. public:
  60. com_holder(const com_global_initer&) BOOST_NOEXCEPT
  61. : holder_(0)
  62. {}
  63. T* operator->() const BOOST_NOEXCEPT {
  64. return holder_;
  65. }
  66. void** to_void_ptr_ptr() BOOST_NOEXCEPT {
  67. return reinterpret_cast<void**>(&holder_);
  68. }
  69. bool is_inited() const BOOST_NOEXCEPT {
  70. return !!holder_;
  71. }
  72. ~com_holder() BOOST_NOEXCEPT {
  73. if (holder_) {
  74. holder_->Release();
  75. }
  76. }
  77. };
  78. inline std::string mingw_demangling_workaround(const std::string& s) {
  79. #ifdef BOOST_GCC
  80. if (s.empty()) {
  81. return s;
  82. }
  83. if (s[0] != '_') {
  84. return boost::core::demangle(('_' + s).c_str());
  85. }
  86. return boost::core::demangle(s.c_str());
  87. #else
  88. return s;
  89. #endif
  90. }
  91. inline void trim_right_zeroes(std::string& s) {
  92. while (s.back() == '\0') {
  93. s.pop_back();
  94. }
  95. }
  96. class debugging_symbols: boost::noncopyable {
  97. static void try_init_com(com_holder< ::IDebugSymbols>& idebug, const com_global_initer& com) BOOST_NOEXCEPT {
  98. com_holder< ::IDebugClient> iclient(com);
  99. if (S_OK != ::DebugCreate(__uuidof(IDebugClient), iclient.to_void_ptr_ptr())) {
  100. return;
  101. }
  102. com_holder< ::IDebugControl> icontrol(com);
  103. const bool res0 = (S_OK == iclient->QueryInterface(
  104. __uuidof(IDebugControl),
  105. icontrol.to_void_ptr_ptr()
  106. ));
  107. if (!res0) {
  108. return;
  109. }
  110. const bool res1 = (S_OK == iclient->AttachProcess(
  111. 0,
  112. ::GetCurrentProcessId(),
  113. DEBUG_ATTACH_NONINVASIVE | DEBUG_ATTACH_NONINVASIVE_NO_SUSPEND
  114. ));
  115. if (!res1) {
  116. return;
  117. }
  118. if (S_OK != icontrol->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE)) {
  119. return;
  120. }
  121. // No cheking: QueryInterface sets the output parameter to NULL in case of error.
  122. iclient->QueryInterface(__uuidof(IDebugSymbols), idebug.to_void_ptr_ptr());
  123. }
  124. #ifndef BOOST_STACKTRACE_USE_WINDBG_CACHED
  125. boost::stacktrace::detail::com_global_initer com_;
  126. com_holder< ::IDebugSymbols> idebug_;
  127. public:
  128. debugging_symbols() BOOST_NOEXCEPT
  129. : com_()
  130. , idebug_(com_)
  131. {
  132. try_init_com(idebug_, com_);
  133. }
  134. #else
  135. #ifdef BOOST_NO_CXX11_THREAD_LOCAL
  136. # error Your compiler does not support C++11 thread_local storage. It`s impossible to build with BOOST_STACKTRACE_USE_WINDBG_CACHED.
  137. #endif
  138. static com_holder< ::IDebugSymbols>& get_thread_local_debug_inst() BOOST_NOEXCEPT {
  139. // [class.mfct]: A static local variable or local type in a member function always refers to the same entity, whether
  140. // or not the member function is inline.
  141. static thread_local boost::stacktrace::detail::com_global_initer com;
  142. static thread_local com_holder< ::IDebugSymbols> idebug(com);
  143. if (!idebug.is_inited()) {
  144. try_init_com(idebug, com);
  145. }
  146. return idebug;
  147. }
  148. com_holder< ::IDebugSymbols>& idebug_;
  149. public:
  150. debugging_symbols() BOOST_NOEXCEPT
  151. : idebug_( get_thread_local_debug_inst() )
  152. {}
  153. #endif // #ifndef BOOST_STACKTRACE_USE_WINDBG_CACHED
  154. bool is_inited() const BOOST_NOEXCEPT {
  155. return idebug_.is_inited();
  156. }
  157. std::string get_name_impl(const void* addr, std::string* module_name = 0) const {
  158. std::string result;
  159. if (!is_inited()) {
  160. return result;
  161. }
  162. const ULONG64 offset = reinterpret_cast<ULONG64>(addr);
  163. char name[256];
  164. name[0] = '\0';
  165. ULONG size = 0;
  166. bool res = (S_OK == idebug_->GetNameByOffset(
  167. offset,
  168. name,
  169. sizeof(name),
  170. &size,
  171. 0
  172. ));
  173. if (!res && size != 0) {
  174. result.resize(size);
  175. res = (S_OK == idebug_->GetNameByOffset(
  176. offset,
  177. &result[0],
  178. static_cast<ULONG>(result.size()),
  179. &size,
  180. 0
  181. ));
  182. trim_right_zeroes(result);
  183. } else if (res) {
  184. result = name;
  185. }
  186. if (!res) {
  187. result.clear();
  188. return result;
  189. }
  190. const std::size_t delimiter = result.find_first_of('!');
  191. if (module_name) {
  192. *module_name = result.substr(0, delimiter);
  193. }
  194. if (delimiter == std::string::npos) {
  195. // If 'delimiter' is equal to 'std::string::npos' then we have only module name.
  196. result.clear();
  197. return result;
  198. }
  199. result = mingw_demangling_workaround(
  200. result.substr(delimiter + 1)
  201. );
  202. return result;
  203. }
  204. std::size_t get_line_impl(const void* addr) const BOOST_NOEXCEPT {
  205. ULONG result = 0;
  206. if (!is_inited()) {
  207. return result;
  208. }
  209. const bool is_ok = (S_OK == idebug_->GetLineByOffset(
  210. reinterpret_cast<ULONG64>(addr),
  211. &result,
  212. 0,
  213. 0,
  214. 0,
  215. 0
  216. ));
  217. return (is_ok ? result : 0);
  218. }
  219. std::pair<std::string, std::size_t> get_source_file_line_impl(const void* addr) const {
  220. std::pair<std::string, std::size_t> result;
  221. if (!is_inited()) {
  222. return result;
  223. }
  224. const ULONG64 offset = reinterpret_cast<ULONG64>(addr);
  225. char name[256];
  226. name[0] = 0;
  227. ULONG size = 0;
  228. ULONG line_num = 0;
  229. bool res = (S_OK == idebug_->GetLineByOffset(
  230. offset,
  231. &line_num,
  232. name,
  233. sizeof(name),
  234. &size,
  235. 0
  236. ));
  237. if (res) {
  238. result.first = name;
  239. result.second = line_num;
  240. return result;
  241. }
  242. if (!res && size == 0) {
  243. return result;
  244. }
  245. result.first.resize(size);
  246. res = (S_OK == idebug_->GetLineByOffset(
  247. offset,
  248. &line_num,
  249. &result.first[0],
  250. static_cast<ULONG>(result.first.size()),
  251. &size,
  252. 0
  253. ));
  254. trim_right_zeroes(result.first);
  255. result.second = line_num;
  256. if (!res) {
  257. result.first.clear();
  258. result.second = 0;
  259. }
  260. return result;
  261. }
  262. void to_string_impl(const void* addr, std::string& res) const {
  263. if (!is_inited()) {
  264. return;
  265. }
  266. std::string module_name;
  267. std::string name = this->get_name_impl(addr, &module_name);
  268. if (!name.empty()) {
  269. res += name;
  270. } else {
  271. res += to_hex_array(addr).data();
  272. }
  273. std::pair<std::string, std::size_t> source_line = this->get_source_file_line_impl(addr);
  274. if (!source_line.first.empty() && source_line.second) {
  275. res += " at ";
  276. res += source_line.first;
  277. res += ':';
  278. res += boost::stacktrace::detail::to_dec_array(source_line.second).data();
  279. } else if (!module_name.empty()) {
  280. res += " in ";
  281. res += module_name;
  282. }
  283. }
  284. };
  285. std::string to_string(const frame* frames, std::size_t size) {
  286. boost::stacktrace::detail::debugging_symbols idebug;
  287. if (!idebug.is_inited()) {
  288. return std::string();
  289. }
  290. std::string res;
  291. res.reserve(64 * size);
  292. for (std::size_t i = 0; i < size; ++i) {
  293. if (i < 10) {
  294. res += ' ';
  295. }
  296. res += boost::stacktrace::detail::to_dec_array(i).data();
  297. res += '#';
  298. res += ' ';
  299. idebug.to_string_impl(frames[i].address(), res);
  300. res += '\n';
  301. }
  302. return res;
  303. }
  304. } // namespace detail
  305. std::string frame::name() const {
  306. boost::stacktrace::detail::debugging_symbols idebug;
  307. return idebug.get_name_impl(addr_);
  308. }
  309. std::string frame::source_file() const {
  310. boost::stacktrace::detail::debugging_symbols idebug;
  311. return idebug.get_source_file_line_impl(addr_).first;
  312. }
  313. std::size_t frame::source_line() const {
  314. boost::stacktrace::detail::debugging_symbols idebug;
  315. return idebug.get_line_impl(addr_);
  316. }
  317. std::string to_string(const frame& f) {
  318. std::string res;
  319. boost::stacktrace::detail::debugging_symbols idebug;
  320. idebug.to_string_impl(f.address(), res);
  321. return res;
  322. }
  323. }} // namespace boost::stacktrace
  324. #endif // BOOST_STACKTRACE_DETAIL_FRAME_MSVC_IPP