frame_decl.hpp 4.8 KB

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