#include <machine/reg.h>
#include <machine/psl.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/ptrace.h>
#include <sys/uio.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <mach/machine/thread_status.h>
#define SET(t, f) (t) |= (f)
#define CLR(t, f) (t) &= ~(f)
#define ISSET(t, f) ((t) & (f))
void psignal_lock __P((struct proc *, int, int));
int isinferior __P((struct proc *, struct proc *));
struct ptrace_args {
int req;
pid_t pid;
caddr_t addr;
int data;
};
int
ptrace(p, uap, retval)
struct proc *p;
struct ptrace_args *uap;
register_t *retval;
{
struct proc *t = current_proc();
vm_offset_t start_addr, end_addr,
kern_addr, offset;
vm_size_t size;
task_t task;
thread_t thread;
thread_act_t th_act;
struct uthread *ut;
int *locr0;
int error = 0;
#if defined(ppc)
struct ppc_thread_state64 statep;
#elif defined(i386)
struct i386_saved_state statep;
#else
#error architecture not supported
#endif
unsigned long state_count;
int tr_sigexc = 0;
if (uap->req == PT_DENY_ATTACH) {
if (ISSET(p->p_flag, P_TRACED)) {
exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
}
SET(p->p_flag, P_NOATTACH);
return(0);
}
if (uap->req == PT_FORCEQUOTA) {
if (is_suser()) {
SET(t->p_flag, P_FORCEQUOTA);
return (0);
} else
return (EPERM);
}
if (uap->req == PT_TRACE_ME) {
SET(p->p_flag, P_TRACED);
t->p_oppid = t->p_pptr->p_pid;
return(0);
}
if (uap->req == PT_SIGEXC) {
if (ISSET(p->p_flag, P_TRACED)) {
SET(p->p_flag, P_SIGEXC);
return(0);
} else
return(EINVAL);
}
if ((t = pfind(uap->pid)) == NULL)
return (ESRCH);
if (uap->pid <=2 )
return (EPERM);
task = t->task;
if (uap->req == PT_ATTACHEXC) {
uap->req = PT_ATTACH;
tr_sigexc = 1;
}
if (uap->req == PT_ATTACH) {
if (t->p_pid == p->p_pid)
return (EINVAL);
if (ISSET(t->p_flag, P_TRACED))
return (EBUSY);
if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
ISSET(t->p_flag, P_SUGID)) &&
(error = suser(p->p_ucred, &p->p_acflag)) != 0)
return (error);
if ((p->p_flag & P_TRACED) && isinferior(p, t))
return(EPERM);
if (ISSET(t->p_flag, P_NOATTACH)) {
psignal(p, SIGSEGV);
return (EBUSY);
}
SET(t->p_flag, P_TRACED);
if (tr_sigexc)
SET(t->p_flag, P_SIGEXC);
t->p_oppid = t->p_pptr->p_pid;
if (t->p_pptr != p)
proc_reparent(t, p);
if (get_task_userstop(task) == 0 ) {
t->p_xstat = 0;
psignal(t, SIGSTOP);
} else {
t->p_xstat = SIGSTOP;
task_resume(task);
}
return(0);
}
if (!ISSET(t->p_flag, P_TRACED))
return (EPERM);
if (t->p_pptr != p)
return (EBUSY);
if (t->p_stat != SSTOP)
return (EBUSY);
switch (uap->req) {
case PT_DETACH:
if (t->p_oppid != t->p_pptr->p_pid) {
struct proc *pp;
pp = pfind(t->p_oppid);
proc_reparent(t, pp ? pp : initproc);
}
t->p_oppid = 0;
CLR(t->p_flag, P_TRACED);
CLR(t->p_flag, P_SIGEXC);
goto resume;
case PT_KILL:
psignal_lock(t, SIGKILL, 0);
goto resume;
case PT_STEP:
case PT_CONTINUE:
th_act = (thread_act_t)get_firstthread(task);
if (th_act == THR_ACT_NULL)
goto errorLabel;
ut = (uthread_t)get_bsdthread_info(th_act);
locr0 = ut->uu_ar0;
#if defined(i386)
state_count = i386_NEW_THREAD_STATE_COUNT;
if (thread_getstatus(th_act, i386_NEW_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#elif defined(ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_getstatus(th_act, PPC_THREAD_STATE64, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#else
#error architecture not supported
#endif
if ((int)uap->addr != 1) {
#if defined(i386)
locr0[PC] = (int)uap->addr;
#elif defined(ppc)
#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
if (!ALIGNED((int)uap->addr, sizeof(int)))
return (ERESTART);
statep.srr0 = (uint64_t)((uint32_t)uap->addr);
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_setstatus(th_act, PPC_THREAD_STATE64, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#undef ALIGNED
#else
#error architecture not implemented!
#endif
}
if ((unsigned)uap->data < 0 || (unsigned)uap->data >= NSIG)
goto errorLabel;
if (uap->data != 0) {
psignal_lock(t, uap->data, 0);
}
#if defined(ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_getstatus(th_act, PPC_THREAD_STATE64, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#endif
#define MSR_SE_BIT 21
if (uap->req == PT_STEP) {
#if defined(i386)
locr0[PS] |= PSL_T;
#elif defined(ppc)
statep.srr1 |= MASK(MSR_SE);
#else
#error architecture not implemented!
#endif
}
else {
#if defined(i386)
locr0[PS] &= ~PSL_T;
#elif defined(ppc)
statep.srr1 &= ~MASK(MSR_SE);
#endif
}
#if defined (ppc)
state_count = PPC_THREAD_STATE64_COUNT;
if (thread_setstatus(th_act, PPC_THREAD_STATE64, &statep, &state_count) != KERN_SUCCESS) {
goto errorLabel;
}
#endif
resume:
t->p_xstat = uap->data;
t->p_stat = SRUN;
if (t->sigwait) {
wakeup((caddr_t)&(t->sigwait));
if ((t->p_flag & P_SIGEXC) == 0)
task_release(task);
}
break;
case PT_THUPDATE: {
thread_act_t target_act;
if ((unsigned)uap->data >= NSIG)
goto errorLabel;
th_act = (thread_act_t)port_name_to_act((void *)uap->addr);
if (th_act == THR_ACT_NULL)
return (ESRCH);
ut = (uthread_t)get_bsdthread_info(th_act);
if (uap->data)
ut->uu_siglist |= sigmask(uap->data);
t->p_xstat = uap->data;
t->p_stat = SRUN;
act_deallocate(th_act);
return(0);
}
break;
errorLabel:
default:
return(EINVAL);
}
return(0);
}