location_from_symbol.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright Antony Polukhin, 2016-2021.
  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_LOCATION_FROM_SYMBOL_HPP
  7. #define BOOST_STACKTRACE_DETAIL_LOCATION_FROM_SYMBOL_HPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  13. # include <dlfcn.h>
  14. #else
  15. # include <boost/winapi/dll.hpp>
  16. #endif
  17. namespace boost { namespace stacktrace { namespace detail {
  18. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  19. class location_from_symbol {
  20. ::Dl_info dli_;
  21. public:
  22. explicit location_from_symbol(const void* addr) BOOST_NOEXCEPT
  23. : dli_()
  24. {
  25. if (!::dladdr(const_cast<void*>(addr), &dli_)) { // `dladdr` on Solaris accepts nonconst addresses
  26. dli_.dli_fname = 0;
  27. }
  28. }
  29. bool empty() const BOOST_NOEXCEPT {
  30. return !dli_.dli_fname;
  31. }
  32. const char* name() const BOOST_NOEXCEPT {
  33. return dli_.dli_fname;
  34. }
  35. };
  36. class program_location {
  37. public:
  38. const char* name() const BOOST_NOEXCEPT {
  39. return 0;
  40. }
  41. };
  42. #else
  43. class location_from_symbol {
  44. BOOST_STATIC_CONSTEXPR boost::winapi::DWORD_ DEFAULT_PATH_SIZE_ = 260;
  45. char file_name_[DEFAULT_PATH_SIZE_];
  46. public:
  47. explicit location_from_symbol(const void* addr) BOOST_NOEXCEPT {
  48. file_name_[0] = '\0';
  49. boost::winapi::MEMORY_BASIC_INFORMATION_ mbi;
  50. if (!boost::winapi::VirtualQuery(addr, &mbi, sizeof(mbi))) {
  51. return;
  52. }
  53. boost::winapi::HMODULE_ handle = reinterpret_cast<boost::winapi::HMODULE_>(mbi.AllocationBase);
  54. if (!boost::winapi::GetModuleFileNameA(handle, file_name_, DEFAULT_PATH_SIZE_)) {
  55. file_name_[0] = '\0';
  56. return;
  57. }
  58. }
  59. bool empty() const BOOST_NOEXCEPT {
  60. return file_name_[0] == '\0';
  61. }
  62. const char* name() const BOOST_NOEXCEPT {
  63. return file_name_;
  64. }
  65. };
  66. class program_location {
  67. BOOST_STATIC_CONSTEXPR boost::winapi::DWORD_ DEFAULT_PATH_SIZE_ = 260;
  68. char file_name_[DEFAULT_PATH_SIZE_];
  69. public:
  70. program_location() BOOST_NOEXCEPT {
  71. file_name_[0] = '\0';
  72. const boost::winapi::HMODULE_ handle = 0;
  73. if (!boost::winapi::GetModuleFileNameA(handle, file_name_, DEFAULT_PATH_SIZE_)) {
  74. file_name_[0] = '\0';
  75. }
  76. }
  77. const char* name() const BOOST_NOEXCEPT {
  78. return file_name_[0] ? file_name_ : 0;
  79. }
  80. };
  81. #endif
  82. }}} // namespace boost::stacktrace::detail
  83. #endif // BOOST_STACKTRACE_DETAIL_LOCATION_FROM_SYMBOL_HPP