#ifndef BASE_PROFILER_WIN32_STACK_FRAME_UNWINDER_H_
#define BASE_PROFILER_WIN32_STACK_FRAME_UNWINDER_H_
#include <windows.h>
#include <memory>
#include "base/base_export.h"
#include "base/profiler/module_cache.h"
#include "build/build_config.h"
namespace base {
#if !defined(ARCH_CPU_64_BITS)
struct RUNTIME_FUNCTION {
DWORD BeginAddress;
DWORD EndAddress;
};
using PRUNTIME_FUNCTION = RUNTIME_FUNCTION*;
#endif
inline ULONG64 ContextPC(CONTEXT* context) {
#if defined(ARCH_CPU_X86_64)
return context->Rip;
#elif defined(ARCH_CPU_X86)
return context->Eip;
#elif defined(ARCH_CPU_ARM64)
return context->Pc;
#else
#error Unsupported Windows Arch
#endif
}
class BASE_EXPORT Win32StackFrameUnwinder {
public:
class BASE_EXPORT UnwindFunctions {
public:
UnwindFunctions(const UnwindFunctions&) = delete;
UnwindFunctions& operator=(const UnwindFunctions&) = delete;
virtual ~UnwindFunctions();
virtual PRUNTIME_FUNCTION LookupFunctionEntry(DWORD64 program_counter,
PDWORD64 image_base) = 0;
virtual void VirtualUnwind(DWORD64 image_base,
DWORD64 program_counter,
PRUNTIME_FUNCTION runtime_function,
CONTEXT* context) = 0;
protected:
UnwindFunctions();
};
explicit Win32StackFrameUnwinder();
Win32StackFrameUnwinder(const Win32StackFrameUnwinder&) = delete;
Win32StackFrameUnwinder& operator=(const Win32StackFrameUnwinder&) = delete;
~Win32StackFrameUnwinder();
bool TryUnwind(bool at_top_frame,
CONTEXT* context,
const ModuleCache::Module* module);
private:
Win32StackFrameUnwinder(std::unique_ptr<UnwindFunctions> unwind_functions);
friend class Win32StackFrameUnwinderTest;
std::unique_ptr<UnwindFunctions> unwind_functions_;
};
}
#endif