Rizin
unix-like reverse engineering framework and cli tools
ptrace_wrap.c File Reference
#include <ptrace_wrap.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <rz_util/rz_sys.h>

Go to the source code of this file.

Functions

static void * th_run (ptrace_wrap_instance *inst)
 
int ptrace_wrap_instance_start (ptrace_wrap_instance *inst)
 
void ptrace_wrap_instance_stop (ptrace_wrap_instance *inst)
 
static void wrap_ptrace (ptrace_wrap_instance *inst)
 
static void wrap_fork (ptrace_wrap_instance *inst)
 
static void wrap_func (ptrace_wrap_instance *inst)
 
long ptrace_wrap (ptrace_wrap_instance *inst, ptrace_wrap_ptrace_request request, pid_t pid, void *addr, void *data)
 
pid_t ptrace_wrap_fork (ptrace_wrap_instance *inst, void(*child_callback)(void *), void *child_callback_user)
 
void * ptrace_wrap_func (ptrace_wrap_instance *inst, ptrace_wrap_func_func func, void *user)
 

Function Documentation

◆ ptrace_wrap()

long ptrace_wrap ( ptrace_wrap_instance inst,
ptrace_wrap_ptrace_request  request,
pid_t  pid,
void *  addr,
void *  data 
)

Definition at line 90 of file ptrace_wrap.c.

90  {
91  if (inst->th == pthread_self()) {
92  return ptrace(request, pid, addr, data);
93  }
94 
95  int _errno = 0;
97  inst->request.ptrace.request = request;
98  inst->request.ptrace.pid = pid;
99  inst->request.ptrace.addr = addr;
100  inst->request.ptrace.data = data;
101  inst->request.ptrace._errno = &_errno;
102  sem_post(&inst->request_sem);
103  sem_wait(&inst->result_sem);
104  errno = _errno;
105  return inst->ptrace_result;
106 }
static static sync static getppid static getegid const char static filename request
Definition: sflib.h:62
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid ptrace
Definition: sflib.h:57
int sem_wait(UV_PLATFORM_SEM_T *semid)
int sem_post(UV_PLATFORM_SEM_T *semid)
@ PTRACE_WRAP_REQUEST_TYPE_PTRACE
Definition: ptrace_wrap.h:19
ptrace_wrap_request request
Definition: ptrace_wrap.h:51
struct ptrace_wrap_request_t::@423::@425 ptrace
ptrace_wrap_request_type type
Definition: ptrace_wrap.h:27
static int addr
Definition: z80asm.c:58

References addr, pid, ptrace, ptrace_wrap_request_t::ptrace, ptrace_wrap_instance_t::ptrace_result, PTRACE_WRAP_REQUEST_TYPE_PTRACE, request, ptrace_wrap_instance_t::request, ptrace_wrap_instance_t::request_sem, ptrace_wrap_instance_t::result_sem, sem_post(), sem_wait(), ptrace_wrap_instance_t::th, and ptrace_wrap_request_t::type.

◆ ptrace_wrap_fork()

pid_t ptrace_wrap_fork ( ptrace_wrap_instance inst,
void(*)(void *)  child_callback,
void *  child_callback_user 
)

Definition at line 108 of file ptrace_wrap.c.

108  {
109  if (inst->th == pthread_self()) {
110  pid_t r = fork();
111  if (r == 0) {
112  child_callback(child_callback_user);
113  return 0;
114  } else {
115  return r;
116  }
117  }
118 
119  int _errno = 0;
121  inst->request.fork.child_callback = child_callback;
122  inst->request.fork.child_callback_user = child_callback_user;
123  inst->request.fork._errno = &_errno;
124  sem_post(&inst->request_sem);
125  sem_wait(&inst->result_sem);
126  errno = _errno;
127  return inst->fork_result;
128 }
#define r
Definition: crypto_rc6.c:12
@ PTRACE_WRAP_REQUEST_TYPE_FORK
Definition: ptrace_wrap.h:20
int pid_t
Definition: sftypes.h:38
struct ptrace_wrap_request_t::@423::@426 fork

References ptrace_wrap_request_t::fork, ptrace_wrap_instance_t::fork_result, PTRACE_WRAP_REQUEST_TYPE_FORK, r, ptrace_wrap_instance_t::request, ptrace_wrap_instance_t::request_sem, ptrace_wrap_instance_t::result_sem, sem_post(), sem_wait(), ptrace_wrap_instance_t::th, and ptrace_wrap_request_t::type.

◆ ptrace_wrap_func()

void* ptrace_wrap_func ( ptrace_wrap_instance inst,
ptrace_wrap_func_func  func,
void *  user 
)

Definition at line 130 of file ptrace_wrap.c.

130  {
131  if (inst->th == pthread_self()) {
132  return func(user);
133  }
134 
136  inst->request.func.func = func;
137  inst->request.func.user = user;
138  sem_post(&inst->request_sem);
139  sem_wait(&inst->result_sem);
140  return inst->func_result;
141 }
@ PTRACE_WRAP_REQUEST_TYPE_FUNC
Definition: ptrace_wrap.h:21
ptrace_wrap_func_func func
Definition: ptrace_wrap.h:42

References ptrace_wrap_request_t::func, ptrace_wrap_instance_t::func_result, PTRACE_WRAP_REQUEST_TYPE_FUNC, ptrace_wrap_instance_t::request, ptrace_wrap_instance_t::request_sem, ptrace_wrap_instance_t::result_sem, sem_post(), sem_wait(), ptrace_wrap_instance_t::th, and ptrace_wrap_request_t::type.

◆ ptrace_wrap_instance_start()

int ptrace_wrap_instance_start ( ptrace_wrap_instance inst)

Definition at line 14 of file ptrace_wrap.c.

14  {
15  int r = sem_init(&inst->request_sem, 0, 0);
16  if (r != 0) {
17  return r;
18  }
19 
20  r = sem_init(&inst->result_sem, 0, 0);
21  if (r != 0) {
22  sem_destroy(&inst->request_sem);
23  return r;
24  }
25 
26  r = pthread_create(&inst->th, NULL, (void *(*)(void *))th_run, inst);
27  if (r != 0) {
28  sem_destroy(&inst->request_sem);
29  sem_destroy(&inst->result_sem);
30  return r;
31  }
32 
33  return 0;
34 }
#define NULL
Definition: cris-opc.c:27
int sem_destroy(UV_PLATFORM_SEM_T *semid)
int sem_init(UV_PLATFORM_SEM_T *semid, int pshared, unsigned int value)
static void * th_run(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:68

References NULL, r, ptrace_wrap_instance_t::request_sem, ptrace_wrap_instance_t::result_sem, sem_destroy(), sem_init(), ptrace_wrap_instance_t::th, and th_run().

◆ ptrace_wrap_instance_stop()

void ptrace_wrap_instance_stop ( ptrace_wrap_instance inst)

◆ th_run()

static void * th_run ( ptrace_wrap_instance inst)
static

Definition at line 68 of file ptrace_wrap.c.

68  {
69  while (1) {
70  sem_wait(&inst->request_sem);
71  switch (inst->request.type) {
73  goto stop;
75  wrap_ptrace(inst);
76  break;
78  wrap_fork(inst);
79  break;
81  wrap_func(inst);
82  break;
83  }
84  sem_post(&inst->result_sem);
85  }
86 stop:
87  return NULL;
88 }
static void wrap_ptrace(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:44
static void wrap_func(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:64
static void wrap_fork(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:55

References NULL, PTRACE_WRAP_REQUEST_TYPE_FORK, PTRACE_WRAP_REQUEST_TYPE_FUNC, PTRACE_WRAP_REQUEST_TYPE_PTRACE, PTRACE_WRAP_REQUEST_TYPE_STOP, ptrace_wrap_instance_t::request, ptrace_wrap_instance_t::request_sem, ptrace_wrap_instance_t::result_sem, sem_post(), sem_wait(), ptrace_wrap_request_t::type, wrap_fork(), wrap_func(), and wrap_ptrace().

Referenced by ptrace_wrap_instance_start().

◆ wrap_fork()

static void wrap_fork ( ptrace_wrap_instance inst)
static

Definition at line 55 of file ptrace_wrap.c.

55  {
56  pid_t r = rz_sys_fork();
57  if (r == 0) {
58  inst->request.fork.child_callback(inst->request.fork.child_callback_user);
59  } else {
60  inst->fork_result = r;
61  }
62 }
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679

References ptrace_wrap_request_t::fork, ptrace_wrap_instance_t::fork_result, r, ptrace_wrap_instance_t::request, and rz_sys_fork().

Referenced by th_run().

◆ wrap_func()

static void wrap_func ( ptrace_wrap_instance inst)
static

Definition at line 64 of file ptrace_wrap.c.

64  {
65  inst->func_result = inst->request.func.func(inst->request.func.user);
66 }

References ptrace_wrap_request_t::func, ptrace_wrap_instance_t::func_result, and ptrace_wrap_instance_t::request.

Referenced by th_run().

◆ wrap_ptrace()

static void wrap_ptrace ( ptrace_wrap_instance inst)
static

Definition at line 44 of file ptrace_wrap.c.

44  {
45  inst->ptrace_result = ptrace(
46  inst->request.ptrace.request,
47  inst->request.ptrace.pid,
48  inst->request.ptrace.addr,
49  inst->request.ptrace.data);
50  if (inst->request.ptrace._errno) {
51  *inst->request.ptrace._errno = errno;
52  }
53 }

References ptrace, ptrace_wrap_request_t::ptrace, ptrace_wrap_instance_t::ptrace_result, and ptrace_wrap_instance_t::request.

Referenced by th_run().