xref: /netbsd-src/sys/kern/sys_process.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: sys_process.c,v 1.47 1995/02/09 05:19:18 mycroft Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
42  */
43 
44 /*
45  * References:
46  *	(1) Bach's "The Design of the UNIX Operating System",
47  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
48  *	(3) the "4.4BSD Programmer's Reference Manual" published
49  *		by USENIX and O'Reilly & Associates.
50  * The 4.4BSD PRM does a reasonably good job of documenting what the various
51  * ptrace() requests should actually do, and its text is quoted several times
52  * in this file.
53  */
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/errno.h>
59 #include <sys/ptrace.h>
60 #include <sys/uio.h>
61 #include <sys/user.h>
62 
63 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
65 
66 #include <machine/reg.h>
67 
68 /* Macros to clear/set/test flags. */
69 #define	SET(t, f)	(t) |= (f)
70 #define	CLR(t, f)	(t) &= ~(f)
71 #define	ISSET(t, f)	((t) & (f))
72 
73 /*
74  * Process debugging system call.
75  */
76 int
77 ptrace(p, uap, retval)
78 	struct proc *p;
79 	struct ptrace_args /* {
80 		syscallarg(int) req;
81 		syscallarg(pid_t) pid;
82 		syscallarg(caddr_t) addr;
83 		syscallarg(int) data;
84 	} */ *uap;
85 	register_t *retval;
86 {
87 	struct proc *t;				/* target process */
88 	struct uio uio;
89 	struct iovec iov;
90 	int error, write;
91 
92 	/* "A foolish consistency..." XXX */
93 	if (SCARG(uap, req) == PT_TRACE_ME)
94 		t = p;
95 	else {
96 		/* Find the process we're supposed to be operating on. */
97 		if ((t = pfind(SCARG(uap, pid))) == NULL)
98 			return (ESRCH);
99 	}
100 
101 	/* Make sure we can operate on it. */
102 	switch (SCARG(uap, req)) {
103 	case  PT_TRACE_ME:
104 		/* Saying that you're being traced is always legal. */
105 		break;
106 
107 	case  PT_ATTACH:
108 		/*
109 		 * You can't attach to a process if:
110 		 *	(1) it's the process that's doing the attaching,
111 		 */
112 		if (t->p_pid == p->p_pid)
113 			return (EINVAL);
114 
115 		/*
116 		 *	(2) it's already being traced, or
117 		 */
118 		if (ISSET(t->p_flag, P_TRACED))
119 			return (EBUSY);
120 
121 		/*
122 		 *	(3) it's not owned by you, or is set-id on exec
123 		 *	    (unless you're root).
124 		 */
125 		if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
126 			ISSET(t->p_flag, P_SUGID)) &&
127 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
128 			return (error);
129 		break;
130 
131 	case  PT_READ_I:
132 	case  PT_READ_D:
133 	case  PT_WRITE_I:
134 	case  PT_WRITE_D:
135 	case  PT_CONTINUE:
136 	case  PT_KILL:
137 	case  PT_DETACH:
138 #ifdef PT_STEP
139 	case  PT_STEP:
140 #endif
141 #ifdef PT_GETREGS
142 	case  PT_GETREGS:
143 #endif
144 #ifdef PT_SETREGS
145 	case  PT_SETREGS:
146 #endif
147 #ifdef PT_GETFPREGS
148 	case  PT_GETFPREGS:
149 #endif
150 #ifdef PT_SETFPREGS
151 	case  PT_SETFPREGS:
152 #endif
153 		/*
154 		 * You can't do what you want to the process if:
155 		 *	(1) It's not being traced at all,
156 		 */
157 		if (!ISSET(t->p_flag, P_TRACED))
158 			return (EPERM);
159 
160 		/*
161 		 *	(2) it's not being traced by _you_, or
162 		 */
163 		if (t->p_pptr != p)
164 			return (EBUSY);
165 
166 		/*
167 		 *	(3) it's not currently stopped.
168 		 */
169 		if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
170 			return (EBUSY);
171 		break;
172 
173 	default:			/* It was not a legal request. */
174 		return (EINVAL);
175 	}
176 
177 	/* Do single-step fixup if needed. */
178 	FIX_SSTEP(t);
179 
180 	/* Now do the operation. */
181 	write = 0;
182 	*retval = 0;
183 
184 	switch (SCARG(uap, req)) {
185 	case  PT_TRACE_ME:
186 		/* Just set the trace flag. */
187 		SET(t->p_flag, P_TRACED);
188 		t->p_oppid = t->p_pptr->p_pid;
189 		return (0);
190 
191 	case  PT_WRITE_I:		/* XXX no seperate I and D spaces */
192 	case  PT_WRITE_D:
193 		write = 1;
194 	case  PT_READ_I:		/* XXX no seperate I and D spaces */
195 	case  PT_READ_D:
196 		/* write = 0 done above. */
197 		iov.iov_base =
198 		    write ? (caddr_t)&SCARG(uap, data) : (caddr_t)retval;
199 		iov.iov_len = sizeof(int);
200 		uio.uio_iov = &iov;
201 		uio.uio_iovcnt = 1;
202 		uio.uio_offset = (off_t)(long)SCARG(uap, addr);
203 		uio.uio_resid = sizeof(int);
204 		uio.uio_segflg = UIO_SYSSPACE;
205 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
206 		uio.uio_procp = p;
207 		return (procfs_domem(p, t, NULL, &uio));
208 
209 #ifdef PT_STEP
210 	case  PT_STEP:
211 		/*
212 		 * From the 4.4BSD PRM:
213 		 * "Execution continues as in request PT_CONTINUE; however
214 		 * as soon as possible after execution of at least one
215 		 * instruction, execution stops again. [ ... ]"
216 		 */
217 #endif
218 	case  PT_CONTINUE:
219 	case  PT_DETACH:
220 		/*
221 		 * From the 4.4BSD PRM:
222 		 * "The data argument is taken as a signal number and the
223 		 * child's execution continues at location addr as if it
224 		 * incurred that signal.  Normally the signal number will
225 		 * be either 0 to indicate that the signal that caused the
226 		 * stop should be ignored, or that value fetched out of
227 		 * the process's image indicating which signal caused
228 		 * the stop.  If addr is (int *)1 then execution continues
229 		 * from where it stopped."
230 		 */
231 
232 		/* Check that the data is a valid signal number or zero. */
233 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
234 			return (EINVAL);
235 
236 #ifdef PT_STEP
237 		/*
238 		 * Arrange for a single-step, if that's requested and possible.
239 		 */
240 		if (error = process_sstep(t, SCARG(uap, req) == PT_STEP))
241 			return (error);
242 #endif
243 
244 		/* If the address paramter is not (int *)1, set the pc. */
245 		if ((int *)SCARG(uap, addr) != (int *)1)
246 			if (error = process_set_pc(t, SCARG(uap, addr)))
247 				return (error);
248 
249 		if (SCARG(uap, req) == PT_DETACH) {
250 			/* give process back to original parent or init */
251 			if (t->p_oppid != t->p_pptr->p_pid) {
252 				struct proc *pp;
253 
254 				pp = pfind(t->p_oppid);
255 				proc_reparent(t, pp ? pp : initproc);
256 			}
257 
258 			/* not being traced any more */
259 			t->p_oppid = 0;
260 			CLR(t->p_flag, P_TRACED|P_WAITED);
261 		}
262 
263 		/* Finally, deliver the requested signal (or none). */
264 sendsig:
265 		if (t->p_stat == SSTOP) {
266 			t->p_xstat = SCARG(uap, data);
267 			setrunnable(t);
268 		} else {
269 			if (SCARG(uap, data) != 0)
270 				psignal(t, SCARG(uap, data));
271 		}
272 		return (0);
273 
274 	case  PT_KILL:
275 		/* just send the process a KILL signal. */
276 		SCARG(uap, data) = SIGKILL;
277 		goto sendsig;	/* in PT_CONTINUE, above. */
278 
279 	case  PT_ATTACH:
280 		/*
281 		 * As done in procfs:
282 		 * Go ahead and set the trace flag.
283 		 * Save the old parent (it's reset in
284 		 *   _DETACH, and also in kern_exit.c:wait4()
285 		 * Reparent the process so that the tracing
286 		 *   proc gets to see all the action.
287 		 * Stop the target.
288 		 */
289 		SET(t->p_flag, P_TRACED);
290 		t->p_oppid = t->p_pptr->p_pid;
291 		if (t->p_pptr != p)
292 			proc_reparent(t, p);
293 		t->p_xstat = 0;         /* XXX ? */
294 		psignal(t, SIGSTOP);
295 		return (0);
296 
297 #ifdef PT_SETREGS
298 	case  PT_SETREGS:
299 		write = 1;
300 #endif
301 #ifdef PT_GETREGS
302 	case  PT_GETREGS:
303 		/* write = 0 done above. */
304 #endif
305 #if defined(PT_SETREGS) || defined(PT_GETREGS)
306 		if (!procfs_validregs(t))
307 			return (EINVAL);
308 		else {
309 			iov.iov_base = SCARG(uap, addr);
310 			iov.iov_len = sizeof(struct reg);
311 			uio.uio_iov = &iov;
312 			uio.uio_iovcnt = 1;
313 			uio.uio_offset = 0;
314 			uio.uio_resid = sizeof(struct reg);
315 			uio.uio_segflg = UIO_USERSPACE;
316 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
317 			uio.uio_procp = p;
318 			return (procfs_doregs(p, t, NULL, &uio));
319 		}
320 #endif
321 
322 #ifdef PT_SETFPREGS
323 	case  PT_SETFPREGS:
324 		write = 1;
325 #endif
326 #ifdef PT_GETFPREGS
327 	case  PT_GETFPREGS:
328 		/* write = 0 done above. */
329 #endif
330 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
331 		if (!procfs_validfpregs(t))
332 			return (EINVAL);
333 		else {
334 			iov.iov_base = SCARG(uap, addr);
335 			iov.iov_len = sizeof(struct fpreg);
336 			uio.uio_iov = &iov;
337 			uio.uio_iovcnt = 1;
338 			uio.uio_offset = 0;
339 			uio.uio_resid = sizeof(struct fpreg);
340 			uio.uio_segflg = UIO_USERSPACE;
341 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
342 			uio.uio_procp = p;
343 			return (procfs_dofpregs(p, t, NULL, &uio));
344 		}
345 #endif
346 	}
347 
348 #ifdef DIAGNOSTIC
349 	panic("ptrace: impossible");
350 #endif
351 }
352 
353 trace_req(a1)
354 	struct proc *a1;
355 {
356 
357 	/* just return 1 to keep other parts of the system happy */
358 	return (1);
359 }
360