Skip to content

Commit cbd5454

Browse files
committed
Slightly refactor stackwalk64 capture frames implementation
1 parent bd4bbf3 commit cbd5454

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

src/unwind/unwind.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <vector>
88

99
#ifdef CPPTRACE_UNWIND_WITH_DBGHELP
10-
#define UNWIND_MAY_NEED_CONTEXT_RECORD
1110
#define WIN32_LEAN_AND_MEAN
1211
#include <windows.h>
1312
#endif

src/unwind/unwind_with_dbghelp.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ namespace detail {
2323
#pragma warning(push)
2424
#pragma warning(disable: 4740) // warning C4740: flow in or out of inline asm code suppresses global optimization
2525
#endif
26+
void capture_context(CONTEXT& context) {
27+
#if defined(_M_IX86) || defined(__i386__)
28+
context.ContextFlags = CONTEXT_CONTROL;
29+
#if IS_MSVC
30+
__asm {
31+
label:
32+
mov [context.Ebp], ebp;
33+
mov [context.Esp], esp;
34+
mov eax, [label];
35+
mov [context.Eip], eax;
36+
}
37+
#else
38+
asm(
39+
"label:\n\t"
40+
"mov{l %%ebp, %[cEbp] | %[cEbp], ebp};\n\t"
41+
"mov{l %%esp, %[cEsp] | %[cEsp], esp};\n\t"
42+
"mov{l $label, %%eax | eax, OFFSET label};\n\t"
43+
"mov{l %%eax, %[cEip] | %[cEip], eax};\n\t"
44+
: [cEbp] "=r" (context.Ebp),
45+
[cEsp] "=r" (context.Esp),
46+
[cEip] "=r" (context.Eip)
47+
);
48+
#endif
49+
#else
50+
RtlCaptureContext(&context);
51+
#endif
52+
}
53+
2654
CPPTRACE_FORCE_NO_INLINE
2755
std::vector<frame_ptr> capture_frames(
2856
std::size_t skip,
@@ -40,31 +68,7 @@ namespace detail {
4068
if(exception_pointers) {
4169
context = *exception_pointers->ContextRecord;
4270
} else {
43-
#if defined(_M_IX86) || defined(__i386__)
44-
context.ContextFlags = CONTEXT_CONTROL;
45-
#if IS_MSVC
46-
__asm {
47-
label:
48-
mov [context.Ebp], ebp;
49-
mov [context.Esp], esp;
50-
mov eax, [label];
51-
mov [context.Eip], eax;
52-
}
53-
#else
54-
asm(
55-
"label:\n\t"
56-
"mov{l %%ebp, %[cEbp] | %[cEbp], ebp};\n\t"
57-
"mov{l %%esp, %[cEsp] | %[cEsp], esp};\n\t"
58-
"mov{l $label, %%eax | eax, OFFSET label};\n\t"
59-
"mov{l %%eax, %[cEip] | %[cEip], eax};\n\t"
60-
: [cEbp] "=r" (context.Ebp),
61-
[cEsp] "=r" (context.Esp),
62-
[cEip] "=r" (context.Eip)
63-
);
64-
#endif
65-
#else
66-
RtlCaptureContext(&context);
67-
#endif
71+
capture_context(context);
6872
}
6973
// Setup current frame
7074
STACKFRAME64 frame;
@@ -104,11 +108,6 @@ namespace detail {
104108

105109
// Dbghelp is is single-threaded, so acquire a lock.
106110
auto lock = get_dbghelp_lock();
107-
// For some reason SymInitialize must be called before StackWalk64
108-
// Note that the code assumes that
109-
// SymInitialize( GetCurrentProcess(), NULL, TRUE ) has
110-
// already been called.
111-
//
112111
auto syminit_info = ensure_syminit();
113112
HANDLE thread = GetCurrentThread();
114113
while(trace.size() < max_depth) {

0 commit comments

Comments
 (0)