| 1 |
#ifdef CPPTRACE_UNWIND_WITH_EXECINFO |
| 2 |
|
| 3 |
#include "unwind/unwind.hpp" |
| 4 |
#include "utils/common.hpp" |
| 5 |
#include "utils/utils.hpp" |
| 6 |
|
| 7 |
#include <algorithm> |
| 8 |
#include <climits> |
| 9 |
#include <cstddef> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <execinfo.h> |
| 13 |
|
| 14 |
CPPTRACE_BEGIN_NAMESPACE |
| 15 |
namespace detail { |
| 16 |
CPPTRACE_FORCE_NO_INLINE |
| 17 |
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) { |
| 18 |
skip++; |
| 19 |
std::vector<void*> addrs(skip + std::min(hard_max_frames, max_depth), nullptr); |
| 20 |
// thread safe |
| 21 |
const int n_frames = backtrace(addrs.data(), static_cast<int>(addrs.size())); |
| 22 |
// I hate the copy here but it's the only way that isn't UB |
| 23 |
std::vector<frame_ptr> frames(n_frames - skip, 0); |
| 24 |
for(int i = skip; i < n_frames; i++) { |
| 25 |
// On x86/x64/arm, as far as I can tell, the frame return address is always one after the call |
| 26 |
// So we just decrement to get the pc back inside the `call` / `bl` |
| 27 |
// This is done with _Unwind too but conditionally based on info from _Unwind_GetIPInfo. |
| 28 |
frames[i - skip] = reinterpret_cast<frame_ptr>(addrs[i]) - 1; |
| 29 |
} |
| 30 |
return frames; |
| 31 |
} |
| 32 |
|
| 33 |
CPPTRACE_FORCE_NO_INLINE |
| 34 |
std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) { |
| 35 |
// Can't safe trace with execinfo |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
bool has_safe_unwind() { |
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
CPPTRACE_END_NAMESPACE |
| 44 |
|
| 45 |
#endif |