Update _Unwind to not rely on a hard max frames

This commit is contained in:
Jeremy 2023-11-15 12:48:01 -05:00
parent 15572b029d
commit f6468b7a01
No known key found for this signature in database
GPG Key ID: B4C8300FEC395042

View File

@ -16,7 +16,7 @@ namespace cpptrace {
namespace detail { namespace detail {
struct unwind_state { struct unwind_state {
std::size_t skip; std::size_t skip;
std::size_t count; std::size_t max_depth;
std::vector<frame_ptr>& vec; std::vector<frame_ptr>& vec;
}; };
@ -31,9 +31,9 @@ namespace detail {
} }
} }
VERIFY( ASSERT(
state.count < state.vec.size(), state.vec.size() < state.max_frames,
"Somehow cpptrace::detail::unwind_callback is overflowing a vector" "Somehow cpptrace::detail::unwind_callback is being called beyond the max_depth"
); );
int is_before_instruction = 0; int is_before_instruction = 0;
frame_ptr ip = _Unwind_GetIPInfo(context, &is_before_instruction); frame_ptr ip = _Unwind_GetIPInfo(context, &is_before_instruction);
@ -43,9 +43,8 @@ namespace detail {
if (ip == frame_ptr(0)) { if (ip == frame_ptr(0)) {
return _URC_END_OF_STACK; return _URC_END_OF_STACK;
} else { } else {
// TODO: push_back?... state.vec.push_back(ip);
state.vec[state.count++] = ip; if(state.vec.size() >= state.max_frames) {
if(state.count == state.vec.size()) {
return _URC_END_OF_STACK; return _URC_END_OF_STACK;
} else { } else {
return _URC_NO_REASON; return _URC_NO_REASON;
@ -55,8 +54,8 @@ namespace detail {
CPPTRACE_FORCE_NO_INLINE CPPTRACE_FORCE_NO_INLINE
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) { std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) {
std::vector<frame_ptr> frames(skip + std::min(hard_max_frames, max_depth), 0); std::vector<frame_ptr> frames;
unwind_state state{skip + 1, 0, frames}; unwind_state state{skip + 1, max_depth, frames};
_Unwind_Backtrace(unwind_callback, &state); // presumably thread-safe _Unwind_Backtrace(unwind_callback, &state); // presumably thread-safe
frames.resize(state.count); frames.resize(state.count);
frames.shrink_to_fit(); frames.shrink_to_fit();