Rizin
unix-like reverse engineering framework and cli tools
windows-all.c File Reference
#include <rz_windows.h>
#include <DbgHelp.h>
#include <w32dbg_wrap.h>

Go to the source code of this file.

Macros

#define DEF_PROC(proc)   static proc##_t *w32_##proc
 
#define GET_PROC(proc)
 

Typedefs

typedef BOOL __stdcall SymInitialize_t(_In_ HANDLE hProcess, _In_opt_ PCSTR UserSearchPath, _In_ BOOL fInvadeProcess)
 
typedef BOOL __stdcall SymCleanup_t(_In_ HANDLE hProcess)
 
typedef PVOID __stdcall SymFunctionTableAccess64_t(_In_ HANDLE hProcess, _In_ DWORD64 AddrBase)
 
typedef DWORD64 __stdcall SymGetModuleBase64_t(_In_ HANDLE hProcess, _In_ DWORD64 qwAddr)
 
typedef BOOL __stdcall StackWalk64_t(_In_ DWORD MachineType, _In_ HANDLE hProcess, _In_ HANDLE hThread, _Inout_ LPSTACKFRAME64 StackFrame, _Inout_ PVOID ContextRecord, _In_opt_ PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine, _In_opt_ PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine, _In_opt_ PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine, _In_opt_ PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress)
 

Functions

 DEF_PROC (SymInitialize)
 
 DEF_PROC (SymCleanup)
 
 DEF_PROC (SymFunctionTableAccess64)
 
 DEF_PROC (SymGetModuleBase64)
 
 DEF_PROC (StackWalk64)
 
static bool initialize_sym_api (void)
 
static RzListbacktrace_windows (RzDebug *dbg, ut64 at)
 

Macro Definition Documentation

◆ DEF_PROC

#define DEF_PROC (   proc)    static proc##_t *w32_##proc

Definition at line 8 of file windows-all.c.

◆ GET_PROC

#define GET_PROC (   proc)
Value:
w32_##proc = (proc##_t *)GetProcAddress(dbghelp, #proc); \
if (!w32_##proc) { \
return false; \
}
struct Proc * proc

Definition at line 9 of file windows-all.c.

Typedef Documentation

◆ StackWalk64_t

typedef BOOL __stdcall StackWalk64_t(_In_ DWORD MachineType, _In_ HANDLE hProcess, _In_ HANDLE hThread, _Inout_ LPSTACKFRAME64 StackFrame, _Inout_ PVOID ContextRecord, _In_opt_ PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine, _In_opt_ PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine, _In_opt_ PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine, _In_opt_ PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress)

Definition at line 31 of file windows-all.c.

◆ SymCleanup_t

typedef BOOL __stdcall SymCleanup_t(_In_ HANDLE hProcess)

Definition at line 20 of file windows-all.c.

◆ SymFunctionTableAccess64_t

typedef PVOID __stdcall SymFunctionTableAccess64_t(_In_ HANDLE hProcess, _In_ DWORD64 AddrBase)

Definition at line 23 of file windows-all.c.

◆ SymGetModuleBase64_t

typedef DWORD64 __stdcall SymGetModuleBase64_t(_In_ HANDLE hProcess, _In_ DWORD64 qwAddr)

Definition at line 27 of file windows-all.c.

◆ SymInitialize_t

typedef BOOL __stdcall SymInitialize_t(_In_ HANDLE hProcess, _In_opt_ PCSTR UserSearchPath, _In_ BOOL fInvadeProcess)

Definition at line 15 of file windows-all.c.

Function Documentation

◆ backtrace_windows()

static RzList* backtrace_windows ( RzDebug dbg,
ut64  at 
)
static

Definition at line 66 of file windows-all.c.

66  {
68  static RzThreadLock *lock = NULL;
69  if (!lock) {
70  lock = rz_th_lock_new(false);
71  if (!lock) {
72  return NULL;
73  }
74  }
75  W32DbgWInst *wrap = dbg->plugin_data;
76 #if __arm64__
77  DWORD machine_type = IMAGE_FILE_MACHINE_ARM64;
78 #elif __arm__
79  DWORD machine_type = IMAGE_FILE_MACHINE_ARMNT;
80 #elif __x86_64__
81  DWORD machine_type = IMAGE_FILE_MACHINE_AMD64;
82 #else
83  DWORD machine_type = IMAGE_FILE_MACHINE_I386;
84 #endif
85  STACKFRAME64 stack = { 0 };
86  stack.AddrFrame.Mode = AddrModeFlat;
87  stack.AddrFrame.Offset = rz_reg_getv(dbg->reg, rz_reg_get_name(dbg->reg, RZ_REG_NAME_BP));
88  stack.AddrStack.Mode = AddrModeFlat;
89  stack.AddrStack.Offset = rz_reg_getv(dbg->reg, rz_reg_get_name(dbg->reg, RZ_REG_NAME_SP));
90  stack.AddrPC.Mode = AddrModeFlat;
92 
94  if (!list) {
95  return NULL;
96  }
97  CONTEXT *ctx = (CONTEXT *)rz_reg_arena_peek(dbg->reg);
99  w32_SymInitialize(wrap->pi.hProcess, NULL, TRUE);
100  while (w32_StackWalk64(machine_type, wrap->pi.hProcess, wrap->pi.hThread, &stack, ctx, NULL, w32_SymFunctionTableAccess64, w32_SymGetModuleBase64, NULL)) {
102  if (!frame) {
103  break;
104  }
105  frame->addr = stack.AddrPC.Offset;
106  frame->bp = stack.AddrFrame.Offset;
107  frame->sp = stack.AddrStack.Offset;
108  frame->size = frame->bp - frame->sp;
109  if (!rz_list_append(list, frame)) {
110  free(frame);
111  break;
112  }
113  }
114  w32_SymCleanup(wrap->pi.hProcess);
116  free(ctx);
117  return list;
118 }
RZ_API ut8 * rz_reg_arena_peek(RzReg *reg)
Definition: arena.c:280
#define NULL
Definition: cris-opc.c:27
#define IMAGE_FILE_MACHINE_ARM64
Definition: debug_windbg.c:20
RzDebug * dbg
Definition: desil.c:30
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define TRUE
Definition: mybfd.h:103
RZ_API ut64 rz_reg_getv(RzReg *reg, const char *name)
Definition: reg.c:332
RZ_API const char * rz_reg_get_name(RzReg *reg, int role)
Definition: reg.c:147
@ RZ_REG_NAME_SP
Definition: rz_reg.h:44
@ RZ_REG_NAME_BP
Definition: rz_reg.h:46
@ RZ_REG_NAME_PC
Definition: rz_reg.h:43
#define RZ_NEW0(x)
Definition: rz_types.h:284
PROCESS_INFORMATION pi
Definition: w32dbg_wrap.h:40
RzReg * reg
Definition: rz_debug.h:286
void * plugin_data
Definition: rz_debug.h:296
Definition: z80asm.h:140
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl)
Releases a RzThreadLock structure.
Definition: thread_lock.c:75
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl)
Acquires a RzThreadLock structure.
Definition: thread_lock.c:45
static void lock(volatile int *lk)
Definition: malloc.c:61
static bool initialize_sym_api(void)
Definition: windows-all.c:48
DWORD

References rz_debug_frame_t::addr, rz_debug_frame_t::bp, dbg, DWORD, free(), IMAGE_FILE_MACHINE_ARM64, initialize_sym_api(), list(), lock(), NULL, W32DbgWInst::pi, rz_debug_t::plugin_data, rz_debug_t::reg, rz_list_append(), rz_list_newf(), RZ_NEW0, rz_reg_arena_peek(), rz_reg_get_name(), rz_reg_getv(), RZ_REG_NAME_BP, RZ_REG_NAME_PC, RZ_REG_NAME_SP, rz_th_lock_enter(), rz_th_lock_leave(), rz_th_lock_new(), rz_debug_frame_t::size, rz_debug_frame_t::sp, and TRUE.

Referenced by rz_debug_native_frames().

◆ DEF_PROC() [1/5]

DEF_PROC ( StackWalk64  )

◆ DEF_PROC() [2/5]

DEF_PROC ( SymCleanup  )

◆ DEF_PROC() [3/5]

DEF_PROC ( SymFunctionTableAccess64  )

◆ DEF_PROC() [4/5]

DEF_PROC ( SymGetModuleBase64  )

◆ DEF_PROC() [5/5]

DEF_PROC ( SymInitialize  )

◆ initialize_sym_api()

static bool initialize_sym_api ( void  )
inlinestatic

Definition at line 48 of file windows-all.c.

48  {
49  static bool initialized = false;
50  if (initialized) {
51  return true;
52  }
53  HMODULE dbghelp = LoadLibrary(TEXT("DbgHelp"));
54  if (!dbghelp) {
55  return false;
56  }
57  GET_PROC(SymInitialize);
58  GET_PROC(SymCleanup);
59  GET_PROC(SymFunctionTableAccess64);
60  GET_PROC(SymGetModuleBase64);
61  GET_PROC(StackWalk64);
62  initialized = true;
63  return true;
64 }
static int initialized
Definition: tricore-dis.c:96
#define GET_PROC(proc)
Definition: windows-all.c:9

References GET_PROC, and initialized.

Referenced by backtrace_windows().