Rizin
unix-like reverse engineering framework and cli tools
ptrace_wrap.h File Reference
#include <pthread.h>
#include <semaphore.h>
#include <sys/ptrace.h>

Go to the source code of this file.

Classes

struct  ptrace_wrap_request_t
 
struct  ptrace_wrap_instance_t
 

Typedefs

typedef int ptrace_wrap_ptrace_request
 
typedef void *(* ptrace_wrap_func_func) (void *)
 
typedef struct ptrace_wrap_request_t ptrace_wrap_request
 
typedef struct ptrace_wrap_instance_t ptrace_wrap_instance
 

Enumerations

enum  ptrace_wrap_request_type { PTRACE_WRAP_REQUEST_TYPE_STOP , PTRACE_WRAP_REQUEST_TYPE_PTRACE , PTRACE_WRAP_REQUEST_TYPE_FORK , PTRACE_WRAP_REQUEST_TYPE_FUNC }
 

Functions

int ptrace_wrap_instance_start (ptrace_wrap_instance *inst)
 
void ptrace_wrap_instance_stop (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)
 

Typedef Documentation

◆ ptrace_wrap_func_func

typedef void*(* ptrace_wrap_func_func) (void *)

Definition at line 24 of file ptrace_wrap.h.

◆ ptrace_wrap_instance

◆ ptrace_wrap_ptrace_request

Definition at line 14 of file ptrace_wrap.h.

◆ ptrace_wrap_request

Enumeration Type Documentation

◆ ptrace_wrap_request_type

Enumerator
PTRACE_WRAP_REQUEST_TYPE_STOP 
PTRACE_WRAP_REQUEST_TYPE_PTRACE 
PTRACE_WRAP_REQUEST_TYPE_FORK 
PTRACE_WRAP_REQUEST_TYPE_FUNC 

Definition at line 17 of file ptrace_wrap.h.

17  {
ptrace_wrap_request_type
Definition: ptrace_wrap.h:17
@ PTRACE_WRAP_REQUEST_TYPE_STOP
Definition: ptrace_wrap.h:18
@ PTRACE_WRAP_REQUEST_TYPE_PTRACE
Definition: ptrace_wrap.h:19
@ PTRACE_WRAP_REQUEST_TYPE_FORK
Definition: ptrace_wrap.h:20
@ PTRACE_WRAP_REQUEST_TYPE_FUNC
Definition: ptrace_wrap.h:21

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 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
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_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()