Last change
on this file since 2573 was
2573,
checked in by ymipsl, 9 months ago
|
create new external source lib : cpptrace, for statck trace output
YM
|
File size:
1.7 KB
|
Line | |
---|
1 | #ifdef CPPTRACE_UNWIND_WITH_UNWIND |
---|
2 | |
---|
3 | #include "unwind.hpp" |
---|
4 | #include "common.hpp" |
---|
5 | |
---|
6 | #include <algorithm> |
---|
7 | #include <cassert> |
---|
8 | #include <cstddef> |
---|
9 | #include <vector> |
---|
10 | |
---|
11 | #include <unwind.h> |
---|
12 | |
---|
13 | namespace cpptrace { |
---|
14 | namespace detail { |
---|
15 | struct unwind_state { |
---|
16 | std::size_t skip; |
---|
17 | std::size_t count; |
---|
18 | std::vector<void*>& vec; |
---|
19 | }; |
---|
20 | |
---|
21 | _Unwind_Reason_Code unwind_callback(_Unwind_Context* context, void* arg) { |
---|
22 | unwind_state& state = *static_cast<unwind_state*>(arg); |
---|
23 | if(state.skip) { |
---|
24 | state.skip--; |
---|
25 | if(_Unwind_GetIP(context) == uintptr_t(0)) { |
---|
26 | return _URC_END_OF_STACK; |
---|
27 | } else { |
---|
28 | return _URC_NO_REASON; |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | assert(state.count < state.vec.size()); |
---|
33 | int is_before_instruction = 0; |
---|
34 | uintptr_t ip = _Unwind_GetIPInfo(context, &is_before_instruction); |
---|
35 | if(!is_before_instruction && ip != uintptr_t(0)) { |
---|
36 | ip--; |
---|
37 | } |
---|
38 | if (ip == uintptr_t(0) || state.count == state.vec.size()) { |
---|
39 | return _URC_END_OF_STACK; |
---|
40 | } else { |
---|
41 | // TODO: push_back?... |
---|
42 | state.vec[state.count++] = (void*)ip; |
---|
43 | return _URC_NO_REASON; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | CPPTRACE_FORCE_NO_INLINE |
---|
48 | std::vector<void*> capture_frames(size_t skip) { |
---|
49 | std::vector<void*> frames(hard_max_frames, nullptr); |
---|
50 | unwind_state state{skip + 1, 0, frames}; |
---|
51 | _Unwind_Backtrace(unwind_callback, &state); // presumably thread-safe |
---|
52 | frames.resize(state.count); |
---|
53 | frames.shrink_to_fit(); |
---|
54 | return frames; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.