* Copyright (c) 1993 Jan-Simon Pendry
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
* @(#)procfs_ctl.c 8.3 (Berkeley) %G%
* $Id: procfs_ctl.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
#include <sys/resource.h>
#include <sys/resourcevar.h>
#include <miscfs/procfs/procfs.h>
* True iff process (p) is in trace wait state
* relative to process (curp)
#define TRACE_WAIT_P(curp, p) \
((p)->p_stat == SSTOP && \
(p)->p_pptr == (curp) && \
((p)->p_flag & P_TRACED))
#define PROCFS_CTL_ATTACH 1
#define PROCFS_CTL_DETACH 2
#define PROCFS_CTL_STEP 3
#define PROCFS_CTL_WAIT 5
static vfs_namemap_t ctlnames
[] = {
/* special /proc commands */
{ "attach", PROCFS_CTL_ATTACH
},
{ "detach", PROCFS_CTL_DETACH
},
{ "step", PROCFS_CTL_STEP
},
{ "run", PROCFS_CTL_RUN
},
{ "wait", PROCFS_CTL_WAIT
},
static vfs_namemap_t signames
[] = {
/* regular signal names */
{ "hup", SIGHUP
}, { "int", SIGINT
},
{ "quit", SIGQUIT
}, { "ill", SIGILL
},
{ "trap", SIGTRAP
}, { "abrt", SIGABRT
},
{ "iot", SIGIOT
}, { "emt", SIGEMT
},
{ "fpe", SIGFPE
}, { "kill", SIGKILL
},
{ "bus", SIGBUS
}, { "segv", SIGSEGV
},
{ "sys", SIGSYS
}, { "pipe", SIGPIPE
},
{ "alrm", SIGALRM
}, { "term", SIGTERM
},
{ "urg", SIGURG
}, { "stop", SIGSTOP
},
{ "tstp", SIGTSTP
}, { "cont", SIGCONT
},
{ "chld", SIGCHLD
}, { "ttin", SIGTTIN
},
{ "ttou", SIGTTOU
}, { "io", SIGIO
},
{ "xcpu", SIGXCPU
}, { "xfsz", SIGXFSZ
},
{ "vtalrm", SIGVTALRM
}, { "prof", SIGPROF
},
{ "winch", SIGWINCH
}, { "info", SIGINFO
},
{ "usr1", SIGUSR1
}, { "usr2", SIGUSR2
},
procfs_control(curp
, p
, op
)
* Attach - attaches the target process for debugging
* by the calling process.
if (op
== PROCFS_CTL_ATTACH
) {
/* check whether already being traced */
if (p
->p_flag
& P_TRACED
)
/* can't trace yourself! */
if (p
->p_pid
== curp
->p_pid
)
* Go ahead and set the trace flag.
* Save the old parent (it's reset in
* _DETACH, and also in kern_exit.c:wait4()
* Reparent the process so that the tracing
* proc gets to see all the action.
p
->p_xstat
= 0; /* XXX ? */
p
->p_oppid
= p
->p_pptr
->p_pid
;
* Target process must be stopped, owned by (curp) and
* be set up for tracing (P_TRACED flag set).
* Allow DETACH to take place at any time for sanity.
* Allow WAIT any time, of course.
if (!TRACE_WAIT_P(curp
, p
))
* do single-step fixup if needed
* Don't deliver any signal by default.
* To continue with a signal, just send
* the signal name to the ctl file
* Detach. Cleans up the target process, reparent it if possible
* and set it running once more.
/* if not being traced, then this is a painless no-op */
if ((p
->p_flag
& P_TRACED
) == 0)
/* not being traced any more */
/* give process back to original parent */
if (p
->p_oppid
!= p
->p_pptr
->p_pid
) {
p
->p_flag
&= ~P_WAITED
; /* XXX ? */
wakeup((caddr_t
) curp
); /* XXX for CTL_WAIT below ? */
* Step. Let the target process execute a single instruction.
* Run. Let the target process continue running until a breakpoint
* Wait for the target process to stop.
* If the target is not being traced then just wait
if (p
->p_flag
& P_TRACED
) {
(p
->p_flag
& P_TRACED
) &&
error
= tsleep((caddr_t
) p
,
PWAIT
|PCATCH
, "procfsx", 0);
if (error
== 0 && !TRACE_WAIT_P(curp
, p
))
while (error
== 0 && p
->p_stat
!= SSTOP
) {
error
= tsleep((caddr_t
) p
,
PWAIT
|PCATCH
, "procfs", 0);
procfs_doctl(curp
, p
, pfs
, uio
)
char msg
[PROCFS_CTLLEN
+1];
if (uio
->uio_rw
!= UIO_WRITE
)
error
= vfs_getuserstr(uio
, msg
, &xlen
);
* Map signal names into signal generation
* or debug control. Unknown commands and/or signals
* Sending a signal while the process is being debugged
* also has the side effect of letting the target continue
* to run. There is no way to single-step a signal delivery.
nm
= vfs_findname(ctlnames
, msg
, xlen
);
error
= procfs_control(curp
, p
, nm
->nm_val
);
nm
= vfs_findname(signames
, msg
, xlen
);
if (TRACE_WAIT_P(curp
, p
)) {