size_t load_here()

in src/backward.h [948:1010]


    size_t load_here(size_t depth = 32)
    {

        CONTEXT localCtx; // used when no context is provided

        if (depth == 0) {
            return 0;
        }

        if (!ctx_) {
            ctx_ = &localCtx;
            RtlCaptureContext(ctx_);
        }

        if (!thd_) {
            thd_ = GetCurrentThread();
        }

        HANDLE process = GetCurrentProcess();

        STACKFRAME64 s;
        memset(&s, 0, sizeof(STACKFRAME64));

        // TODO: 32 bit context capture
        s.AddrStack.Mode = AddrModeFlat;
        s.AddrFrame.Mode = AddrModeFlat;
        s.AddrPC.Mode = AddrModeFlat;
#    ifdef _M_X64
        s.AddrPC.Offset = ctx_->Rip;
        s.AddrStack.Offset = ctx_->Rsp;
        s.AddrFrame.Offset = ctx_->Rbp;
#    else
        s.AddrPC.Offset = ctx_->Eip;
        s.AddrStack.Offset = ctx_->Esp;
        s.AddrFrame.Offset = ctx_->Ebp;
#    endif

        if (!machine_type_) {
#    ifdef _M_X64
            machine_type_ = IMAGE_FILE_MACHINE_AMD64;
#    else
            machine_type_ = IMAGE_FILE_MACHINE_I386;
#    endif
        }

        for (;;) {
            // NOTE: this only works if PDBs are already loaded!
            SetLastError(0);
            if (!StackWalk64(
                    machine_type_, process, thd_, &s, ctx_, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
                break;

            if (s.AddrReturn.Offset == 0)
                break;

            _stacktrace.push_back(reinterpret_cast<void*>(s.AddrPC.Offset));

            if (size() >= depth)
                break;
        }

        return size();
    }