minix/kernel/system/do_trace.c
David van Moolenbroek b423d7b477 Merge of David's ptrace branch. Summary:
o Support for ptrace T_ATTACH/T_DETACH and T_SYSCALL
o PM signal handling logic should now work properly, even with debuggers
  being present
o Asynchronous PM/VFS protocol, full IPC support for senda(), and
  AMF_NOREPLY senda() flag

DETAILS

Process stop and delay call handling of PM:
o Added sys_runctl() kernel call with sys_stop() and sys_resume()
  aliases, for PM to stop and resume a process
o Added exception for sending/syscall-traced processes to sys_runctl(),
  and matching SIGKREADY pseudo-signal to PM
o Fixed PM signal logic to deal with requests from a process after
  stopping it (so-called "delay calls"), using the SIGKREADY facility
o Fixed various PM panics due to race conditions with delay calls versus
  VFS calls
o Removed special PRIO_STOP priority value
o Added SYS_LOCK RTS kernel flag, to stop an individual process from
  running while modifying its process structure

Signal and debugger handling in PM:
o Fixed debugger signals being dropped if a second signal arrives when
  the debugger has not retrieved the first one
o Fixed debugger signals being sent to the debugger more than once
o Fixed debugger signals unpausing process in VFS; removed PM_UNPAUSE_TR
  protocol message
o Detached debugger signals from general signal logic and from being
  blocked on VFS calls, meaning that even VFS can now be traced
o Fixed debugger being unable to receive more than one pending signal in
  one process stop
o Fixed signal delivery being delayed needlessly when multiple signals
  are pending
o Fixed wait test for tracer, which was returning for children that were
  not waited for
o Removed second parallel pending call from PM to VFS for any process
o Fixed process becoming runnable between exec() and debugger trap
o Added support for notifying the debugger before the parent when a
  debugged child exits
o Fixed debugger death causing child to remain stopped forever
o Fixed consistently incorrect use of _NSIG

Extensions to ptrace():
o Added T_ATTACH and T_DETACH ptrace request, to attach and detach a
  debugger to and from a process
o Added T_SYSCALL ptrace request, to trace system calls
o Added T_SETOPT ptrace request, to set trace options
o Added TO_TRACEFORK trace option, to attach automatically to children
  of a traced process
o Added TO_ALTEXEC trace option, to send SIGSTOP instead of SIGTRAP upon
  a successful exec() of the tracee
o Extended T_GETUSER ptrace support to allow retrieving a process's priv
  structure
o Removed T_STOP ptrace request again, as it does not help implementing
  debuggers properly
o Added MINIX3-specific ptrace test (test42)
o Added proper manual page for ptrace(2)

Asynchronous PM/VFS interface:
o Fixed asynchronous messages not being checked when receive() is called
  with an endpoint other than ANY
o Added AMF_NOREPLY senda() flag, preventing such messages from
  satisfying the receive part of a sendrec()
o Added asynsend3() that takes optional flags; asynsend() is now a
  #define passing in 0 as third parameter
o Made PM/VFS protocol asynchronous; reintroduced tell_fs()
o Made PM_BASE request/reply number range unique
o Hacked in a horrible temporary workaround into RS to deal with newly
  revealed RS-PM-VFS race condition triangle until VFS is asynchronous

System signal handling:
o Fixed shutdown logic of device drivers; removed old SIGKSTOP signal
o Removed is-superuser check from PM's do_procstat() (aka getsigset())
o Added sigset macros to allow system processes to deal with the full
  signal set, rather than just the POSIX subset

Miscellaneous PM fixes:
o Split do_getset into do_get and do_set, merging common code and making
  structure clearer
o Fixed setpriority() being able to put to sleep processes using an
  invalid parameter, or revive zombie processes
o Made find_proc() global; removed obsolete proc_from_pid()
o Cleanup here and there

Also included:
o Fixed false-positive boot order kernel warning
o Removed last traces of old NOTIFY_FROM code

THINGS OF POSSIBLE INTEREST

o It should now be possible to run PM at any priority, even lower than
  user processes
o No assumptions are made about communication speed between PM and VFS,
  although communication must be FIFO
o A debugger will now receive incoming debuggee signals at kill time
  only; the process may not yet be fully stopped
o A first step has been made towards making the SYSTEM task preemptible
2009-09-30 09:57:22 +00:00

200 lines
5.8 KiB
C

/* The kernel call implemented in this file:
* m_type: SYS_TRACE
*
* The parameters for this kernel call are:
* m2_i1: CTL_ENDPT process that is traced
* m2_i2: CTL_REQUEST trace request
* m2_l1: CTL_ADDRESS address at traced process' space
* m2_l2: CTL_DATA data to be written or returned here
*/
#include "../system.h"
#include <sys/ptrace.h>
#if USE_TRACE
/*==========================================================================*
* do_trace *
*==========================================================================*/
#define TR_VLSIZE ((vir_bytes) sizeof(long))
PUBLIC int do_trace(m_ptr)
register message *m_ptr;
{
/* Handle the debugging commands supported by the ptrace system call
* The commands are:
* T_STOP stop the process
* T_OK enable tracing by parent for this process
* T_GETINS return value from instruction space
* T_GETDATA return value from data space
* T_GETUSER return value from user process table
* T_SETINS set value from instruction space
* T_SETDATA set value from data space
* T_SETUSER set value in user process table
* T_RESUME resume execution
* T_EXIT exit
* T_STEP set trace bit
* T_SYSCALL trace system call
*
* The T_OK and T_EXIT commands are handled completely by the process manager,
* all others come here.
*/
register struct proc *rp;
vir_bytes tr_addr = (vir_bytes) m_ptr->CTL_ADDRESS;
long tr_data = m_ptr->CTL_DATA;
int tr_request = m_ptr->CTL_REQUEST;
int tr_proc_nr_e = m_ptr->CTL_ENDPT, tr_proc_nr;
unsigned char ub;
int i;
#define COPYTOPROC(seg, addr, myaddr, length) { \
struct vir_addr fromaddr, toaddr; \
int r; \
fromaddr.proc_nr_e = SYSTEM; \
toaddr.proc_nr_e = tr_proc_nr_e; \
fromaddr.offset = (myaddr); \
toaddr.offset = (addr); \
fromaddr.segment = D; \
toaddr.segment = (seg); \
if((r=virtual_copy_vmcheck(&fromaddr, &toaddr, length)) != OK) { \
printf("Can't copy in sys_trace: %d\n", r);\
return r;\
} \
}
#define COPYFROMPROC(seg, addr, myaddr, length) { \
struct vir_addr fromaddr, toaddr; \
int r; \
fromaddr.proc_nr_e = tr_proc_nr_e; \
toaddr.proc_nr_e = SYSTEM; \
fromaddr.offset = (addr); \
toaddr.offset = (myaddr); \
fromaddr.segment = (seg); \
toaddr.segment = D; \
if((r=virtual_copy_vmcheck(&fromaddr, &toaddr, length)) != OK) { \
printf("Can't copy in sys_trace: %d\n", r);\
return r;\
} \
}
if(!isokendpt(tr_proc_nr_e, &tr_proc_nr)) return(EINVAL);
if (iskerneln(tr_proc_nr)) return(EPERM);
rp = proc_addr(tr_proc_nr);
if (isemptyp(rp)) return(EINVAL);
switch (tr_request) {
case T_STOP: /* stop process */
RTS_LOCK_SET(rp, P_STOP);
rp->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
rp->p_misc_flags &= ~MF_SC_TRACE; /* clear syscall trace flag */
return(OK);
case T_GETINS: /* return value from instruction space */
if (rp->p_memmap[T].mem_len != 0) {
COPYFROMPROC(T, tr_addr, (vir_bytes) &tr_data, sizeof(long));
m_ptr->CTL_DATA = tr_data;
break;
}
/* Text space is actually data space - fall through. */
case T_GETDATA: /* return value from data space */
COPYFROMPROC(D, tr_addr, (vir_bytes) &tr_data, sizeof(long));
m_ptr->CTL_DATA= tr_data;
break;
case T_GETUSER: /* return value from process table */
if ((tr_addr & (sizeof(long) - 1)) != 0) return(EIO);
if (tr_addr <= sizeof(struct proc) - sizeof(long)) {
m_ptr->CTL_DATA = *(long *) ((char *) rp + (int) tr_addr);
break;
}
/* The process's proc struct is followed by its priv struct.
* The alignment here should be unnecessary, but better safe..
*/
i = sizeof(long) - 1;
tr_addr -= (sizeof(struct proc) + i) & ~i;
if (tr_addr > sizeof(struct priv) - sizeof(long)) return(EIO);
m_ptr->CTL_DATA = *(long *) ((char *) rp->p_priv + (int) tr_addr);
break;
case T_SETINS: /* set value in instruction space */
if (rp->p_memmap[T].mem_len != 0) {
COPYTOPROC(T, tr_addr, (vir_bytes) &tr_data, sizeof(long));
m_ptr->CTL_DATA = 0;
break;
}
/* Text space is actually data space - fall through. */
case T_SETDATA: /* set value in data space */
COPYTOPROC(D, tr_addr, (vir_bytes) &tr_data, sizeof(long));
m_ptr->CTL_DATA = 0;
break;
case T_SETUSER: /* set value in process table */
if ((tr_addr & (sizeof(reg_t) - 1)) != 0 ||
tr_addr > sizeof(struct stackframe_s) - sizeof(reg_t))
return(EIO);
i = (int) tr_addr;
#if (_MINIX_CHIP == _CHIP_INTEL)
/* Altering segment registers might crash the kernel when it
* tries to load them prior to restarting a process, so do
* not allow it.
*/
if (i == (int) &((struct proc *) 0)->p_reg.cs ||
i == (int) &((struct proc *) 0)->p_reg.ds ||
i == (int) &((struct proc *) 0)->p_reg.es ||
#if _WORD_SIZE == 4
i == (int) &((struct proc *) 0)->p_reg.gs ||
i == (int) &((struct proc *) 0)->p_reg.fs ||
#endif
i == (int) &((struct proc *) 0)->p_reg.ss)
return(EIO);
#endif
if (i == (int) &((struct proc *) 0)->p_reg.psw)
/* only selected bits are changeable */
SETPSW(rp, tr_data);
else
*(reg_t *) ((char *) &rp->p_reg + i) = (reg_t) tr_data;
m_ptr->CTL_DATA = 0;
break;
case T_RESUME: /* resume execution */
RTS_LOCK_UNSET(rp, P_STOP);
m_ptr->CTL_DATA = 0;
break;
case T_STEP: /* set trace bit */
rp->p_reg.psw |= TRACEBIT;
RTS_LOCK_UNSET(rp, P_STOP);
m_ptr->CTL_DATA = 0;
break;
case T_SYSCALL: /* trace system call */
rp->p_misc_flags |= MF_SC_TRACE;
RTS_LOCK_UNSET(rp, P_STOP);
m_ptr->CTL_DATA = 0;
break;
case T_READB_INS: /* get value from instruction space */
COPYFROMPROC(rp->p_memmap[T].mem_len > 0 ? T : D, tr_addr, (vir_bytes) &ub, 1);
m_ptr->CTL_DATA = ub;
break;
case T_WRITEB_INS: /* set value in instruction space */
COPYTOPROC(rp->p_memmap[T].mem_len > 0 ? T : D,tr_addr, (vir_bytes) &tr_data, 1);
m_ptr->CTL_DATA = 0;
break;
default:
return(EINVAL);
}
return(OK);
}
#endif /* USE_TRACE */