File: has_stackwalk.cpp

package info (click to toggle)
cpptrace 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: cpp: 15,646; python: 962; ansic: 155; sh: 103; makefile: 86
file content (102 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dbghelp.h>

#define IS_CLANG 0
#define IS_GCC 0
#define IS_MSVC 0

#if defined(__clang__)
 #undef IS_CLANG
 #define IS_CLANG 1
#elif defined(__GNUC__) || defined(__GNUG__)
 #undef IS_GCC
 #define IS_GCC 1
#elif defined(_MSC_VER)
 #undef IS_MSVC
 #define IS_MSVC 1
#else
 #error "Unsupported compiler"
#endif

int main() {
    HANDLE proc = GetCurrentProcess();
    HANDLE thread = GetCurrentThread();
    // https://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/

    // Get current thread context
    // GetThreadContext cannot be used on the current thread.
    // RtlCaptureContext doesn't work on i386
    CONTEXT context;
    #if defined(_M_IX86) || defined(__i386__)
    ZeroMemory(&context, sizeof(CONTEXT));
    context.ContextFlags = CONTEXT_CONTROL;
    #if IS_MSVC
    __asm {
        label:
        mov [context.Ebp], ebp;
        mov [context.Esp], esp;
        mov eax, [label];
        mov [context.Eip], eax;
    }
    #else
    asm(
        "label:\n\t"
        "mov{l %%ebp, %[cEbp] | %[cEbp], ebp};\n\t"
        "mov{l %%esp, %[cEsp] | %[cEsp], esp};\n\t"
        "mov{l $label, %%eax | eax, OFFSET label};\n\t"
        "mov{l %%eax, %[cEip] | %[cEip], eax};\n\t"
        : [cEbp] "=r" (context.Ebp),
            [cEsp] "=r" (context.Esp),
            [cEip] "=r" (context.Eip)
    );
    #endif
    #else
    RtlCaptureContext(&context);
    #endif
    // Setup current frame
    STACKFRAME64 frame;
    ZeroMemory(&frame, sizeof(STACKFRAME64));
    DWORD machine_type;
    #if defined(_M_IX86) || defined(__i386__)
    machine_type           = IMAGE_FILE_MACHINE_I386;
    frame.AddrPC.Offset    = context.Eip;
    frame.AddrPC.Mode      = AddrModeFlat;
    frame.AddrFrame.Offset = context.Ebp;
    frame.AddrFrame.Mode   = AddrModeFlat;
    frame.AddrStack.Offset = context.Esp;
    frame.AddrStack.Mode   = AddrModeFlat;
    #elif defined(_M_X64) || defined(__x86_64__)
    machine_type           = IMAGE_FILE_MACHINE_AMD64;
    frame.AddrPC.Offset    = context.Rip;
    frame.AddrPC.Mode      = AddrModeFlat;
    frame.AddrFrame.Offset = context.Rsp;
    frame.AddrFrame.Mode   = AddrModeFlat;
    frame.AddrStack.Offset = context.Rsp;
    frame.AddrStack.Mode   = AddrModeFlat;
    #elif defined(_M_IA64) || defined(__aarch64__)
    machine_type           = IMAGE_FILE_MACHINE_IA64;
    frame.AddrPC.Offset    = context.StIIP;
    frame.AddrPC.Mode      = AddrModeFlat;
    frame.AddrFrame.Offset = context.IntSp;
    frame.AddrFrame.Mode   = AddrModeFlat;
    frame.AddrBStore.Offset= context.RsBSP;
    frame.AddrBStore.Mode  = AddrModeFlat;
    frame.AddrStack.Offset = context.IntSp;
    frame.AddrStack.Mode   = AddrModeFlat;
    #else
    #error "Cpptrace: StackWalk64 not supported for this platform yet"
    #endif
    ZeroMemory(&context, sizeof(CONTEXT));
    StackWalk64(
        machine_type,
        proc,
        thread,
        &frame,
        machine_type == IMAGE_FILE_MACHINE_I386 ? NULL : &context,
        NULL,
        SymFunctionTableAccess64,
        SymGetModuleBase64,
        NULL
    );
}