xref: /csrg-svn/sys/kern/sys_process.c (revision 54931)
149589Sbostic /*-
249589Sbostic  * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
349589Sbostic  * All rights reserved.
423385Smckusick  *
549589Sbostic  * %sccs.include.proprietary.c%
649589Sbostic  *
7*54931Storek  *	@(#)sys_process.c	7.30 (Berkeley) 07/10/92
823385Smckusick  */
97426Sroot 
1030376Skarels #define IPCREG
1117095Sbloom #include "param.h"
1217095Sbloom #include "proc.h"
1337728Smckusick #include "vnode.h"
1417095Sbloom #include "buf.h"
1526278Skarels #include "ptrace.h"
167426Sroot 
1737520Smckusick #include "machine/reg.h"
1837520Smckusick #include "machine/psl.h"
1948438Skarels #include "vm/vm.h"
2047543Skarels #include "vm/vm_page.h"
2137520Smckusick 
2248438Skarels #include "user.h"
2348438Skarels 
247501Sroot /*
257501Sroot  * Priority for tracing
267501Sroot  */
277501Sroot #define	IPCPRI	PZERO
287501Sroot 
297501Sroot /*
307501Sroot  * Tracing variables.
317501Sroot  * Used to pass trace command from
327501Sroot  * parent to child being traced.
337501Sroot  * This data base cannot be
347501Sroot  * shared and is locked
357501Sroot  * per user.
367501Sroot  */
377501Sroot struct {
387501Sroot 	int	ip_lock;
397501Sroot 	int	ip_req;
407501Sroot 	int	*ip_addr;
417501Sroot 	int	ip_data;
427501Sroot } ipc;
437501Sroot 
447501Sroot /*
4549674Smckusick  * Process debugging system call.
467501Sroot  */
47*54931Storek struct ptrace_args {
48*54931Storek 	int	req;
49*54931Storek 	int	pid;
50*54931Storek 	int	*addr;
51*54931Storek 	int	data;
52*54931Storek };
5343378Smckusick ptrace(curp, uap, retval)
5443378Smckusick 	struct proc *curp;
55*54931Storek 	register struct ptrace_args *uap;
5643378Smckusick 	int *retval;
5743378Smckusick {
5843378Smckusick 	register struct proc *p;
5954340Smckusick 	int error;
607501Sroot 
6154340Smckusick 	p = pfind(uap->pid);
6254340Smckusick 	if (uap->req == PT_ATTACH) {
6354340Smckusick 		/*
6454340Smckusick 		 * Must be root if the process has used set user or
6554340Smckusick 		 * group privileges or does not belong to the real
6654340Smckusick 		 * user. Must not be already traced.
6754340Smckusick 		 */
6854340Smckusick 		if ((p->p_flag & SUGID ||
6954340Smckusick 		    p->p_cred->p_ruid != curp->p_cred->p_ruid) &&
7054340Smckusick 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
7154340Smckusick 			return (error);
7254340Smckusick 		if (p->p_flag & STRC)
7354340Smckusick 			return (EALREADY);	/* ??? */
7454340Smckusick 		/*
7554340Smckusick 		 * It would be nice if the tracing relationship was separate
7654340Smckusick 		 * from the parent relationship but that would require
7754340Smckusick 		 * another set of links in the proc struct or for "wait"
7854340Smckusick 		 * to scan the entire proc table.  To make life easier,
7954340Smckusick 		 * we just re-parent the process we're trying to trace.
8054340Smckusick 		 * The old parent is remembered so we can put things back
8154340Smckusick 		 * on a "detach".
8254340Smckusick 		 */
8354340Smckusick 		p->p_flag |= STRC;
8454340Smckusick 		p->p_oppid = p->p_pptr->p_pid;
8554340Smckusick 		proc_reparent(p, curp);
8654340Smckusick 		psignal(p, SIGSTOP);
8754340Smckusick 		return (0);
8854340Smckusick 	}
897501Sroot 	if (uap->req <= 0) {
9043378Smckusick 		curp->p_flag |= STRC;
9144405Skarels 		return (0);
927501Sroot 	}
9347543Skarels 	if (p == 0 || p->p_stat != SSTOP || p->p_pptr != curp ||
9443378Smckusick 	    !(p->p_flag & STRC))
9544405Skarels 		return (ESRCH);
967501Sroot 	while (ipc.ip_lock)
977501Sroot 		sleep((caddr_t)&ipc, IPCPRI);
987501Sroot 	ipc.ip_lock = p->p_pid;
997501Sroot 	ipc.ip_data = uap->data;
1007501Sroot 	ipc.ip_addr = uap->addr;
1017501Sroot 	ipc.ip_req = uap->req;
1027501Sroot 	p->p_flag &= ~SWTED;
1037501Sroot 	while (ipc.ip_req > 0) {
1047501Sroot 		if (p->p_stat==SSTOP)
1057501Sroot 			setrun(p);
1067501Sroot 		sleep((caddr_t)&ipc, IPCPRI);
1077501Sroot 	}
10843378Smckusick 	*retval = ipc.ip_data;
1097501Sroot 	ipc.ip_lock = 0;
1107501Sroot 	wakeup((caddr_t)&ipc);
11143378Smckusick 	if (ipc.ip_req < 0)
11244405Skarels 		return (EIO);
11344405Skarels 	return (0);
1147501Sroot }
1157501Sroot 
11649246Skarels #define	PHYSOFF(p, o) ((caddr_t)(p) + (o))
11749246Skarels 
11845882Swilliam #if defined(i386)
11945882Swilliam #undef        PC
12045882Swilliam #undef        SP
12145882Swilliam #undef        PS
12245882Swilliam #undef        R0
12345882Swilliam #undef        R1
1248952Sroot 
12545882Swilliam #define       PC      tEIP
12645882Swilliam #define       SP      tESP
12745882Swilliam #define       PS      tEFLAGS
12845882Swilliam #define       R0      tEDX
12945882Swilliam #define       R1      tECX
13045882Swilliam #endif
13145882Swilliam 
1327501Sroot /*
13349674Smckusick  * Transmit a tracing request from the parent to the child process
13449674Smckusick  * being debugged. This code runs in the context of the child process
13549674Smckusick  * to fulfill the command requested by the parent.
1367501Sroot  */
13742928Smckusick procxmt(p)
13842928Smckusick 	register struct proc *p;
1397501Sroot {
14042928Smckusick 	register int i, *poff;
14149246Skarels 	extern char kstack[];
1427501Sroot 
14342928Smckusick 	if (ipc.ip_lock != p->p_pid)
1447501Sroot 		return (0);
14542928Smckusick 	p->p_slptime = 0;
14652374Smckusick 	p->p_addr->u_kproc.kp_proc.p_md.md_regs = p->p_md.md_regs; /* u.u_ar0 */
1477501Sroot 	i = ipc.ip_req;
1487501Sroot 	ipc.ip_req = 0;
1497501Sroot 	switch (i) {
1507501Sroot 
15126278Skarels 	case PT_READ_I:			/* read the child's text space */
1527501Sroot 		if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
1537501Sroot 			goto error;
1547501Sroot 		ipc.ip_data = fuiword((caddr_t)ipc.ip_addr);
1557501Sroot 		break;
1567501Sroot 
15726278Skarels 	case PT_READ_D:			/* read the child's data space */
1587501Sroot 		if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ))
1597501Sroot 			goto error;
1607501Sroot 		ipc.ip_data = fuword((caddr_t)ipc.ip_addr);
1617501Sroot 		break;
1627501Sroot 
16326278Skarels 	case PT_READ_U:			/* read the child's u. */
16441991Smckusick #ifdef HPUXCOMPAT
16549101Skarels 		if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXTRACE)
16641991Smckusick 			i = hpuxtobsduoff(ipc.ip_addr);
16741991Smckusick 		else
16841991Smckusick #endif
1697501Sroot 		i = (int)ipc.ip_addr;
17049246Skarels 		if ((u_int) i > ctob(UPAGES)-sizeof(int) || (i & 1) != 0)
1717501Sroot 			goto error;
17249101Skarels 		ipc.ip_data = *(int *)PHYSOFF(p->p_addr, i);
1737501Sroot 		break;
1747501Sroot 
17526278Skarels 	case PT_WRITE_I:		/* write the child's text space */
1768952Sroot 		if ((i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data)) < 0) {
17745735Smckusick 			vm_offset_t sa, ea;
17845735Smckusick 			int rv;
17945735Smckusick 
18045735Smckusick 			sa = trunc_page((vm_offset_t)ipc.ip_addr);
18145735Smckusick 			ea = round_page((vm_offset_t)ipc.ip_addr+sizeof(int)-1);
18247543Skarels 			rv = vm_map_protect(&p->p_vmspace->vm_map, sa, ea,
18345735Smckusick 					VM_PROT_DEFAULT, FALSE);
18445735Smckusick 			if (rv == KERN_SUCCESS) {
1858952Sroot 				i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data);
18647543Skarels 				(void) vm_map_protect(&p->p_vmspace->vm_map,
18747543Skarels 					sa, ea, VM_PROT_READ|VM_PROT_EXECUTE,
18847543Skarels 					FALSE);
18945735Smckusick 			}
1908952Sroot 		}
1917501Sroot 		if (i < 0)
1927501Sroot 			goto error;
1937501Sroot 		break;
1947501Sroot 
19526278Skarels 	case PT_WRITE_D:		/* write the child's data space */
1967501Sroot 		if (suword((caddr_t)ipc.ip_addr, 0) < 0)
1977501Sroot 			goto error;
1987501Sroot 		(void) suword((caddr_t)ipc.ip_addr, ipc.ip_data);
1997501Sroot 		break;
2007501Sroot 
20126278Skarels 	case PT_WRITE_U:		/* write the child's u. */
20241991Smckusick #ifdef HPUXCOMPAT
20349101Skarels 		if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXTRACE)
20441991Smckusick 			i = hpuxtobsduoff(ipc.ip_addr);
20541991Smckusick 		else
20641991Smckusick #endif
2077501Sroot 		i = (int)ipc.ip_addr;
20852178Smarc #ifdef mips
20952178Smarc 		poff = (int *)PHYSOFF(curproc->p_addr, i);
21052178Smarc #else
21149246Skarels 		poff = (int *)PHYSOFF(kstack, i);
21252178Smarc #endif
2138952Sroot 		for (i=0; i<NIPCREG; i++)
21452374Smckusick 			if (poff == &p->p_md.md_regs[ipcreg[i]])
2157501Sroot 				goto ok;
21652374Smckusick 		if (poff == &p->p_md.md_regs[PS]) {
2178952Sroot 			ipc.ip_data |= PSL_USERSET;
21841991Smckusick 			ipc.ip_data &= ~PSL_USERCLR;
21932863Skarels #ifdef PSL_CM_CLR
22032863Skarels 			if (ipc.ip_data & PSL_CM)
22132863Skarels 				ipc.ip_data &= ~PSL_CM_CLR;
22232863Skarels #endif
2237501Sroot 			goto ok;
2247501Sroot 		}
22553873Smckusick #if defined(hp300) || defined(luna68k)
22641991Smckusick #ifdef FPCOPROC
22749246Skarels 		if (poff >= (int *)&((struct user *)kstack)->u_pcb.pcb_fpregs.fpf_regs &&
22849246Skarels 		    poff <= (int *)&((struct user *)kstack)->u_pcb.pcb_fpregs.fpf_fpiar)
22941991Smckusick 			goto ok;
23041991Smckusick #endif
23141991Smckusick #endif
2327501Sroot 		goto error;
2337501Sroot 
2347501Sroot 	ok:
23542928Smckusick 		*poff = ipc.ip_data;
2367501Sroot 		break;
2377501Sroot 
23826278Skarels 	case PT_STEP:			/* single step the child */
23926278Skarels 	case PT_CONTINUE:		/* continue the child */
24054340Smckusick 		if ((unsigned)ipc.ip_data >= NSIG)
24154340Smckusick 			goto error;
2427501Sroot 		if ((int)ipc.ip_addr != 1)
24352374Smckusick 			p->p_md.md_regs[PC] = (int)ipc.ip_addr;
24443895Skarels 		p->p_xstat = ipc.ip_data;	/* see issig */
24552178Smarc #ifdef PSL_T
24652178Smarc 		/* need something more machine independent here... */
24726278Skarels 		if (i == PT_STEP)
24852374Smckusick 			p->p_md.md_regs[PS] |= PSL_T;
24952178Smarc #endif
2507501Sroot 		wakeup((caddr_t)&ipc);
2517501Sroot 		return (1);
2527501Sroot 
25326278Skarels 	case PT_KILL:			/* kill the child process */
2547501Sroot 		wakeup((caddr_t)&ipc);
25545111Smckusick 		exit(p, (int)p->p_xstat);
2567501Sroot 
25754340Smckusick 	case PT_DETACH:			/* stop tracing the child */
25854340Smckusick 		if ((unsigned)ipc.ip_data >= NSIG)
25954340Smckusick 			goto error;
26054340Smckusick 		if ((int)ipc.ip_addr != 1)
26154340Smckusick 			p->p_md.md_regs[PC] = (int)ipc.ip_addr;
26254340Smckusick 		p->p_xstat = ipc.ip_data;	/* see issig */
26354340Smckusick 		p->p_flag &= ~STRC;
26454340Smckusick 		if (p->p_oppid != p->p_pptr->p_pid) {
26554340Smckusick                         register struct proc *pp = pfind(p->p_oppid);
26654340Smckusick 
26754340Smckusick                         if (pp)
26854340Smckusick                                 proc_reparent(p, pp);
26954340Smckusick 		}
27054340Smckusick 		p->p_oppid = 0;
27154340Smckusick 		wakeup((caddr_t)&ipc);
27254340Smckusick 		return (1);
27354340Smckusick 
2747501Sroot 	default:
2757501Sroot 	error:
2767501Sroot 		ipc.ip_req = -1;
2777501Sroot 	}
2787501Sroot 	wakeup((caddr_t)&ipc);
2797501Sroot 	return (0);
2807501Sroot }
281