frame_decl.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_DECL_HPP
  7. #define BOOST_STACKTRACE_DETAIL_FRAME_DECL_HPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <iosfwd>
  13. #include <string>
  14. #include <boost/core/explicit_operator_bool.hpp>
  15. #include <boost/stacktrace/safe_dump_to.hpp> // boost::stacktrace::detail::native_frame_ptr_t
  16. #include <boost/stacktrace/detail/void_ptr_cast.hpp>
  17. #include <boost/stacktrace/detail/push_options.h>
  18. /// @file boost/stacktrace/detail/frame_decl.hpp
  19. /// Use <boost/stacktrace/frame.hpp> header instead of this one!
  20. namespace boost { namespace stacktrace {
  21. /// @class boost::stacktrace::frame boost/stacktrace/detail/frame_decl.hpp <boost/stacktrace/frame.hpp>
  22. /// @brief Class that stores frame/function address and can get information about it at runtime.
  23. class frame {
  24. public:
  25. typedef boost::stacktrace::detail::native_frame_ptr_t native_frame_ptr_t;
  26. private:
  27. /// @cond
  28. native_frame_ptr_t addr_;
  29. /// @endcond
  30. public:
  31. /// @brief Constructs frame that references NULL address.
  32. /// Calls to source_file() and source_line() will return empty string.
  33. /// Calls to source_line() will return 0.
  34. ///
  35. /// @b Complexity: O(1).
  36. ///
  37. /// @b Async-Handler-Safety: Safe.
  38. /// @throws Nothing.
  39. BOOST_CONSTEXPR frame() BOOST_NOEXCEPT
  40. : addr_(0)
  41. {}
  42. #ifdef BOOST_STACKTRACE_DOXYGEN_INVOKED
  43. /// @brief Copy constructs frame.
  44. ///
  45. /// @b Complexity: O(1).
  46. ///
  47. /// @b Async-Handler-Safety: Safe.
  48. /// @throws Nothing.
  49. constexpr frame(const frame&) = default;
  50. /// @brief Copy assigns frame.
  51. ///
  52. /// @b Complexity: O(1).
  53. ///
  54. /// @b Async-Handler-Safety: Safe.
  55. /// @throws Nothing.
  56. constexpr frame& operator=(const frame&) = default;
  57. #endif
  58. /// @brief Constructs frame that references addr and could later generate information about that address using platform specific features.
  59. ///
  60. /// @b Complexity: O(1).
  61. ///
  62. /// @b Async-Handler-Safety: Safe.
  63. /// @throws Nothing.
  64. BOOST_CONSTEXPR explicit frame(native_frame_ptr_t addr) BOOST_NOEXCEPT
  65. : addr_(addr)
  66. {}
  67. /// @brief Constructs frame that references function_addr and could later generate information about that function using platform specific features.
  68. ///
  69. /// @b Complexity: O(1).
  70. ///
  71. /// @b Async-Handler-Safety: Safe.
  72. /// @throws Nothing.
  73. template <class T>
  74. explicit frame(T* function_addr) BOOST_NOEXCEPT
  75. : addr_(boost::stacktrace::detail::void_ptr_cast<native_frame_ptr_t>(function_addr))
  76. {}
  77. /// @returns Name of the frame (function name in a human readable form).
  78. ///
  79. /// @b Complexity: unknown (lots of platform specific work).
  80. ///
  81. /// @b Async-Handler-Safety: Unsafe.
  82. /// @throws std::bad_alloc if not enough memory to construct resulting string.
  83. BOOST_STACKTRACE_FUNCTION std::string name() const;
  84. /// @returns Address of the frame function.
  85. ///
  86. /// @b Complexity: O(1).
  87. ///
  88. /// @b Async-Handler-Safety: Safe.
  89. /// @throws Nothing.
  90. BOOST_CONSTEXPR native_frame_ptr_t address() const BOOST_NOEXCEPT {
  91. return addr_;
  92. }
  93. /// @returns Path to the source file, were the function of the frame is defined. Returns empty string
  94. /// if this->source_line() == 0.
  95. /// @throws std::bad_alloc if not enough memory to construct resulting string.
  96. ///
  97. /// @b Complexity: unknown (lots of platform specific work).
  98. ///
  99. /// @b Async-Handler-Safety: Unsafe.
  100. BOOST_STACKTRACE_FUNCTION std::string source_file() const;
  101. /// @returns Code line in the source file, were the function of the frame is defined.
  102. /// @throws std::bad_alloc if not enough memory to construct string for internal needs.
  103. ///
  104. /// @b Complexity: unknown (lots of platform specific work).
  105. ///
  106. /// @b Async-Handler-Safety: Unsafe.
  107. BOOST_STACKTRACE_FUNCTION std::size_t source_line() const;
  108. /// @brief Checks that frame is not references NULL address.
  109. /// @returns `true` if `this->address() != 0`
  110. ///
  111. /// @b Complexity: O(1)
  112. ///
  113. /// @b Async-Handler-Safety: Safe.
  114. BOOST_EXPLICIT_OPERATOR_BOOL()
  115. /// @brief Checks that frame references NULL address.
  116. /// @returns `true` if `this->address() == 0`
  117. ///
  118. /// @b Complexity: O(1)
  119. ///
  120. /// @b Async-Handler-Safety: Safe.
  121. BOOST_CONSTEXPR bool empty() const BOOST_NOEXCEPT { return !address(); }
  122. /// @cond
  123. BOOST_CONSTEXPR bool operator!() const BOOST_NOEXCEPT { return !address(); }
  124. /// @endcond
  125. };
  126. namespace detail {
  127. BOOST_STACKTRACE_FUNCTION std::string to_string(const frame* frames, std::size_t size);
  128. } // namespace detail
  129. }} // namespace boost::stacktrace
  130. #include <boost/stacktrace/detail/pop_options.h>
  131. #endif // BOOST_STACKTRACE_DETAIL_FRAME_DECL_HPP