Rizin
unix-like reverse engineering framework and cli tools
w32dbg_wrap.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2019-2020 GustavoLCR <gugulcr@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <windows.h>
5 #include <w32dbg_wrap.h>
6 
7 static DWORD WINAPI __w32dbg_thread(LPVOID param) {
8  W32DbgWInst *inst = param;
9  W32DbgWParams *params = &inst->params;
10  PROCESS_INFORMATION *pi = &inst->pi;
11  while (1) {
12  WaitForSingleObject(inst->request_sem, INFINITE);
13  switch (params->type) {
14  case W32_CONTINUE:
15  params->ret = ContinueDebugEvent(pi->dwProcessId, pi->dwThreadId, params->continue_status);
16  break;
17  case W32_WAIT:
18  params->ret = WaitForDebugEvent(params->wait.de, params->wait.wait_time);
19  if (params->ret) {
20  pi->dwProcessId = params->wait.de->dwProcessId;
21  pi->dwThreadId = params->wait.de->dwThreadId;
22  }
23  break;
24  case W32_CALL_FUNC:
25  params->ret = params->func.func(params->func.user);
26  break;
27  case W32_ATTACH:
28  params->ret = DebugActiveProcess(pi->dwProcessId);
29  break;
30  case W32_DETACH:
31  case W32_STOP:
32  params->ret = DebugActiveProcessStop(pi->dwProcessId);
33  break;
34  default:
35  break;
36  }
37  if (!params->ret) {
38  params->err = GetLastError();
39  }
40  ReleaseSemaphore(inst->result_sem, 1, NULL);
41  if (params->type == W32_STOP) {
42  break;
43  }
44  }
45  return 0;
46 }
47 
49  W32DbgWInst *inst = calloc(1, sizeof(W32DbgWInst));
50  if (inst) {
51  inst->request_sem = CreateSemaphore(NULL, 0, 1, NULL);
52  inst->result_sem = CreateSemaphore(NULL, 0, 1, NULL);
53  inst->debugThread = CreateThread(NULL, 0, __w32dbg_thread, inst, 0, NULL);
54  }
55  return inst;
56 }
57 
59  if (!inst) {
60  return;
61  }
62  inst->params.type = W32_STOP;
64  WaitForSingleObject(inst->debugThread, INFINITE);
65  CloseHandle(inst->request_sem);
66  CloseHandle(inst->result_sem);
67  CloseHandle(inst->debugThread);
68  free(inst);
69 }
70 
72  ReleaseSemaphore(inst->request_sem, 1, NULL);
73  WaitForSingleObject(inst->result_sem, INFINITE);
74  return w32dbgw_ret(inst);
75 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
HANDLE result_sem
Definition: w32dbg_wrap.h:38
W32DbgWParams params
Definition: w32dbg_wrap.h:36
PROCESS_INFORMATION pi
Definition: w32dbg_wrap.h:40
HANDLE debugThread
Definition: w32dbg_wrap.h:35
HANDLE request_sem
Definition: w32dbg_wrap.h:37
int(* func)(void *)
Definition: w32dbg_wrap.h:26
DWORD continue_status
Definition: w32dbg_wrap.h:20
w32dbg_wrap_req type
Definition: w32dbg_wrap.h:18
struct W32DbgWParams::@606::@608 wait
W32DbgWInst * w32dbg_wrap_new(void)
Definition: w32dbg_wrap.c:48
int w32dbg_wrap_wait_ret(W32DbgWInst *inst)
Definition: w32dbg_wrap.c:71
static DWORD WINAPI __w32dbg_thread(LPVOID param)
Definition: w32dbg_wrap.c:7
void w32dbg_wrap_free(W32DbgWInst *inst)
Definition: w32dbg_wrap.c:58
@ W32_ATTACH
Definition: w32dbg_wrap.h:10
@ W32_STOP
Definition: w32dbg_wrap.h:13
@ W32_CONTINUE
Definition: w32dbg_wrap.h:9
@ W32_DETACH
Definition: w32dbg_wrap.h:11
@ W32_CALL_FUNC
Definition: w32dbg_wrap.h:14
@ W32_WAIT
Definition: w32dbg_wrap.h:12
#define w32dbgw_ret(inst)
Definition: w32dbg_wrap.h:45
DWORD