sys_ptrace_common.c revision 1.1.2.4 1 1.1.2.4 pgoyette /* $NetBSD: sys_ptrace_common.c,v 1.1.2.4 2017/03/20 06:57:47 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 pgoyette * by Andrew Doran.
9 1.1.2.2 pgoyette *
10 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.1.2.2 pgoyette * are met:
13 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.1.2.2 pgoyette *
19 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 pgoyette */
31 1.1.2.2 pgoyette
32 1.1.2.2 pgoyette /*-
33 1.1.2.2 pgoyette * Copyright (c) 1982, 1986, 1989, 1993
34 1.1.2.2 pgoyette * The Regents of the University of California. All rights reserved.
35 1.1.2.2 pgoyette * (c) UNIX System Laboratories, Inc.
36 1.1.2.2 pgoyette * All or some portions of this file are derived from material licensed
37 1.1.2.2 pgoyette * to the University of California by American Telephone and Telegraph
38 1.1.2.2 pgoyette * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.1.2.2 pgoyette * the permission of UNIX System Laboratories, Inc.
40 1.1.2.2 pgoyette *
41 1.1.2.2 pgoyette * This code is derived from software contributed to Berkeley by
42 1.1.2.2 pgoyette * Jan-Simon Pendry.
43 1.1.2.2 pgoyette *
44 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
45 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
46 1.1.2.2 pgoyette * are met:
47 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
48 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
49 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
50 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
51 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
52 1.1.2.2 pgoyette * 3. Neither the name of the University nor the names of its contributors
53 1.1.2.2 pgoyette * may be used to endorse or promote products derived from this software
54 1.1.2.2 pgoyette * without specific prior written permission.
55 1.1.2.2 pgoyette *
56 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.1.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.1.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.1.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.1.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.1.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.1.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.1.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.1.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.1.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.1.2.2 pgoyette * SUCH DAMAGE.
67 1.1.2.2 pgoyette *
68 1.1.2.2 pgoyette * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 1.1.2.2 pgoyette */
70 1.1.2.2 pgoyette
71 1.1.2.2 pgoyette /*-
72 1.1.2.2 pgoyette * Copyright (c) 1993 Jan-Simon Pendry.
73 1.1.2.2 pgoyette * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 1.1.2.2 pgoyette *
75 1.1.2.2 pgoyette * This code is derived from software contributed to Berkeley by
76 1.1.2.2 pgoyette * Jan-Simon Pendry.
77 1.1.2.2 pgoyette *
78 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
79 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
80 1.1.2.2 pgoyette * are met:
81 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
82 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
83 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
84 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
85 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
86 1.1.2.2 pgoyette * 3. All advertising materials mentioning features or use of this software
87 1.1.2.2 pgoyette * must display the following acknowledgement:
88 1.1.2.2 pgoyette * This product includes software developed by the University of
89 1.1.2.2 pgoyette * California, Berkeley and its contributors.
90 1.1.2.2 pgoyette * 4. Neither the name of the University nor the names of its contributors
91 1.1.2.2 pgoyette * may be used to endorse or promote products derived from this software
92 1.1.2.2 pgoyette * without specific prior written permission.
93 1.1.2.2 pgoyette *
94 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 1.1.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 1.1.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 1.1.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 1.1.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 1.1.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 1.1.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 1.1.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 1.1.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 1.1.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 1.1.2.2 pgoyette * SUCH DAMAGE.
105 1.1.2.2 pgoyette *
106 1.1.2.2 pgoyette * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 1.1.2.2 pgoyette */
108 1.1.2.2 pgoyette
109 1.1.2.2 pgoyette /*
110 1.1.2.2 pgoyette * References:
111 1.1.2.2 pgoyette * (1) Bach's "The Design of the UNIX Operating System",
112 1.1.2.2 pgoyette * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 1.1.2.2 pgoyette * (3) the "4.4BSD Programmer's Reference Manual" published
114 1.1.2.2 pgoyette * by USENIX and O'Reilly & Associates.
115 1.1.2.2 pgoyette * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 1.1.2.2 pgoyette * ptrace() requests should actually do, and its text is quoted several times
117 1.1.2.2 pgoyette * in this file.
118 1.1.2.2 pgoyette */
119 1.1.2.2 pgoyette
120 1.1.2.2 pgoyette #include <sys/cdefs.h>
121 1.1.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.1.2.4 2017/03/20 06:57:47 pgoyette Exp $");
122 1.1.2.2 pgoyette
123 1.1.2.2 pgoyette #ifdef _KERNEL_OPT
124 1.1.2.2 pgoyette #include "opt_ptrace.h"
125 1.1.2.2 pgoyette #include "opt_ktrace.h"
126 1.1.2.2 pgoyette #include "opt_pax.h"
127 1.1.2.2 pgoyette #endif
128 1.1.2.2 pgoyette
129 1.1.2.2 pgoyette #include <sys/param.h>
130 1.1.2.2 pgoyette #include <sys/systm.h>
131 1.1.2.2 pgoyette #include <sys/proc.h>
132 1.1.2.2 pgoyette #include <sys/errno.h>
133 1.1.2.2 pgoyette #include <sys/exec.h>
134 1.1.2.2 pgoyette #include <sys/pax.h>
135 1.1.2.2 pgoyette #include <sys/ptrace.h>
136 1.1.2.2 pgoyette #include <sys/uio.h>
137 1.1.2.2 pgoyette #include <sys/ras.h>
138 1.1.2.2 pgoyette #include <sys/kmem.h>
139 1.1.2.2 pgoyette #include <sys/kauth.h>
140 1.1.2.2 pgoyette #include <sys/mount.h>
141 1.1.2.2 pgoyette #include <sys/syscallargs.h>
142 1.1.2.2 pgoyette #include <sys/module.h>
143 1.1.2.2 pgoyette #include <sys/condvar.h>
144 1.1.2.2 pgoyette #include <sys/mutex.h>
145 1.1.2.2 pgoyette
146 1.1.2.2 pgoyette #include <uvm/uvm_extern.h>
147 1.1.2.2 pgoyette
148 1.1.2.2 pgoyette #include <machine/reg.h>
149 1.1.2.2 pgoyette
150 1.1.2.2 pgoyette #ifdef PTRACE
151 1.1.2.2 pgoyette
152 1.1.2.2 pgoyette # ifdef DEBUG
153 1.1.2.2 pgoyette # define DPRINTF(a) uprintf a
154 1.1.2.2 pgoyette # else
155 1.1.2.2 pgoyette # define DPRINTF(a)
156 1.1.2.2 pgoyette # endif
157 1.1.2.2 pgoyette
158 1.1.2.2 pgoyette static kauth_listener_t ptrace_listener;
159 1.1.2.2 pgoyette static int process_auxv_offset(struct proc *, struct uio *);
160 1.1.2.2 pgoyette
161 1.1.2.2 pgoyette #if 0
162 1.1.2.2 pgoyette static int ptrace_cbref;
163 1.1.2.2 pgoyette static kmutex_t ptrace_mtx;
164 1.1.2.2 pgoyette static kcondvar_t ptrace_cv;
165 1.1.2.2 pgoyette #endif
166 1.1.2.2 pgoyette
167 1.1.2.2 pgoyette static int
168 1.1.2.2 pgoyette ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
169 1.1.2.2 pgoyette void *arg0, void *arg1, void *arg2, void *arg3)
170 1.1.2.2 pgoyette {
171 1.1.2.2 pgoyette struct proc *p;
172 1.1.2.2 pgoyette int result;
173 1.1.2.2 pgoyette
174 1.1.2.2 pgoyette result = KAUTH_RESULT_DEFER;
175 1.1.2.2 pgoyette p = arg0;
176 1.1.2.2 pgoyette
177 1.1.2.2 pgoyette #if 0
178 1.1.2.2 pgoyette mutex_enter(&ptrace_mtx);
179 1.1.2.2 pgoyette ptrace_cbref++;
180 1.1.2.2 pgoyette mutex_exit(&ptrace_mtx);
181 1.1.2.2 pgoyette #endif
182 1.1.2.2 pgoyette if (action != KAUTH_PROCESS_PTRACE)
183 1.1.2.2 pgoyette goto out;
184 1.1.2.2 pgoyette
185 1.1.2.2 pgoyette switch ((u_long)arg1) {
186 1.1.2.2 pgoyette case PT_TRACE_ME:
187 1.1.2.2 pgoyette case PT_ATTACH:
188 1.1.2.2 pgoyette case PT_WRITE_I:
189 1.1.2.2 pgoyette case PT_WRITE_D:
190 1.1.2.2 pgoyette case PT_READ_I:
191 1.1.2.2 pgoyette case PT_READ_D:
192 1.1.2.2 pgoyette case PT_IO:
193 1.1.2.2 pgoyette #ifdef PT_GETREGS
194 1.1.2.2 pgoyette case PT_GETREGS:
195 1.1.2.2 pgoyette #endif
196 1.1.2.2 pgoyette #ifdef PT_SETREGS
197 1.1.2.2 pgoyette case PT_SETREGS:
198 1.1.2.2 pgoyette #endif
199 1.1.2.2 pgoyette #ifdef PT_GETFPREGS
200 1.1.2.2 pgoyette case PT_GETFPREGS:
201 1.1.2.2 pgoyette #endif
202 1.1.2.2 pgoyette #ifdef PT_SETFPREGS
203 1.1.2.2 pgoyette case PT_SETFPREGS:
204 1.1.2.2 pgoyette #endif
205 1.1.2.4 pgoyette #ifdef PT_GETDBREGS
206 1.1.2.4 pgoyette case PT_GETDBREGS:
207 1.1.2.4 pgoyette #endif
208 1.1.2.4 pgoyette #ifdef PT_SETDBREGS
209 1.1.2.4 pgoyette case PT_SETDBREGS:
210 1.1.2.3 pgoyette #endif
211 1.1.2.2 pgoyette case PT_SET_EVENT_MASK:
212 1.1.2.2 pgoyette case PT_GET_EVENT_MASK:
213 1.1.2.2 pgoyette case PT_GET_PROCESS_STATE:
214 1.1.2.3 pgoyette case PT_SET_SIGINFO:
215 1.1.2.3 pgoyette case PT_GET_SIGINFO:
216 1.1.2.4 pgoyette case PT_SET_SIGMASK:
217 1.1.2.4 pgoyette case PT_GET_SIGMASK:
218 1.1.2.2 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
219 1.1.2.2 pgoyette PTRACE_MACHDEP_REQUEST_CASES
220 1.1.2.2 pgoyette #endif
221 1.1.2.2 pgoyette if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
222 1.1.2.2 pgoyette ISSET(p->p_flag, PK_SUGID)) {
223 1.1.2.2 pgoyette break;
224 1.1.2.2 pgoyette }
225 1.1.2.2 pgoyette
226 1.1.2.2 pgoyette result = KAUTH_RESULT_ALLOW;
227 1.1.2.2 pgoyette
228 1.1.2.2 pgoyette break;
229 1.1.2.2 pgoyette
230 1.1.2.2 pgoyette #ifdef PT_STEP
231 1.1.2.2 pgoyette case PT_STEP:
232 1.1.2.2 pgoyette #endif
233 1.1.2.2 pgoyette case PT_CONTINUE:
234 1.1.2.2 pgoyette case PT_KILL:
235 1.1.2.2 pgoyette case PT_DETACH:
236 1.1.2.2 pgoyette case PT_LWPINFO:
237 1.1.2.2 pgoyette case PT_SYSCALL:
238 1.1.2.2 pgoyette case PT_SYSCALLEMU:
239 1.1.2.2 pgoyette case PT_DUMPCORE:
240 1.1.2.4 pgoyette case PT_RESUME:
241 1.1.2.4 pgoyette case PT_SUSPEND:
242 1.1.2.2 pgoyette result = KAUTH_RESULT_ALLOW;
243 1.1.2.2 pgoyette break;
244 1.1.2.2 pgoyette
245 1.1.2.2 pgoyette default:
246 1.1.2.2 pgoyette break;
247 1.1.2.2 pgoyette }
248 1.1.2.2 pgoyette
249 1.1.2.2 pgoyette out:
250 1.1.2.2 pgoyette #if 0
251 1.1.2.2 pgoyette mutex_enter(&ptrace_mtx);
252 1.1.2.2 pgoyette if (--ptrace_cbref == 0)
253 1.1.2.2 pgoyette cv_broadcast(&ptrace_cv);
254 1.1.2.2 pgoyette mutex_exit(&ptrace_mtx);
255 1.1.2.2 pgoyette #endif
256 1.1.2.2 pgoyette
257 1.1.2.2 pgoyette return result;
258 1.1.2.2 pgoyette }
259 1.1.2.2 pgoyette
260 1.1.2.2 pgoyette int
261 1.1.2.2 pgoyette ptrace_init(void)
262 1.1.2.2 pgoyette {
263 1.1.2.2 pgoyette
264 1.1.2.2 pgoyette #if 0
265 1.1.2.2 pgoyette mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
266 1.1.2.2 pgoyette cv_init(&ptrace_cv, "ptracecb");
267 1.1.2.2 pgoyette ptrace_cbref = 0;
268 1.1.2.2 pgoyette #endif
269 1.1.2.2 pgoyette ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
270 1.1.2.2 pgoyette ptrace_listener_cb, NULL);
271 1.1.2.2 pgoyette return 0;
272 1.1.2.2 pgoyette }
273 1.1.2.2 pgoyette
274 1.1.2.2 pgoyette int
275 1.1.2.2 pgoyette ptrace_fini(void)
276 1.1.2.2 pgoyette {
277 1.1.2.2 pgoyette
278 1.1.2.2 pgoyette kauth_unlisten_scope(ptrace_listener);
279 1.1.2.2 pgoyette
280 1.1.2.2 pgoyette #if 0
281 1.1.2.2 pgoyette /* Make sure no-one is executing our kauth listener */
282 1.1.2.2 pgoyette
283 1.1.2.2 pgoyette mutex_enter(&ptrace_mtx);
284 1.1.2.2 pgoyette while (ptrace_cbref != 0)
285 1.1.2.2 pgoyette cv_wait(&ptrace_cv, &ptrace_mtx);
286 1.1.2.2 pgoyette mutex_exit(&ptrace_mtx);
287 1.1.2.2 pgoyette mutex_destroy(&ptrace_mtx);
288 1.1.2.2 pgoyette cv_destroy(&ptrace_cv);
289 1.1.2.2 pgoyette #endif
290 1.1.2.2 pgoyette
291 1.1.2.2 pgoyette return 0;
292 1.1.2.2 pgoyette }
293 1.1.2.2 pgoyette
294 1.1.2.2 pgoyette int
295 1.1.2.2 pgoyette do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
296 1.1.2.2 pgoyette void *addr, int data, register_t *retval)
297 1.1.2.2 pgoyette {
298 1.1.2.2 pgoyette struct proc *p = l->l_proc;
299 1.1.2.2 pgoyette struct lwp *lt;
300 1.1.2.2 pgoyette struct lwp *lt2;
301 1.1.2.2 pgoyette struct proc *t; /* target process */
302 1.1.2.2 pgoyette struct uio uio;
303 1.1.2.2 pgoyette struct iovec iov;
304 1.1.2.2 pgoyette struct ptrace_io_desc piod;
305 1.1.2.2 pgoyette struct ptrace_event pe;
306 1.1.2.2 pgoyette struct ptrace_state ps;
307 1.1.2.2 pgoyette struct ptrace_lwpinfo pl;
308 1.1.2.3 pgoyette struct ptrace_siginfo psi;
309 1.1.2.2 pgoyette struct vmspace *vm;
310 1.1.2.2 pgoyette int error, write, tmp, pheld;
311 1.1.2.2 pgoyette int signo = 0;
312 1.1.2.2 pgoyette int resume_all;
313 1.1.2.2 pgoyette ksiginfo_t ksi;
314 1.1.2.2 pgoyette char *path;
315 1.1.2.2 pgoyette int len = 0;
316 1.1.2.2 pgoyette error = 0;
317 1.1.2.2 pgoyette
318 1.1.2.2 pgoyette /*
319 1.1.2.2 pgoyette * If attaching or detaching, we need to get a write hold on the
320 1.1.2.2 pgoyette * proclist lock so that we can re-parent the target process.
321 1.1.2.2 pgoyette */
322 1.1.2.2 pgoyette mutex_enter(proc_lock);
323 1.1.2.2 pgoyette
324 1.1.2.2 pgoyette /* "A foolish consistency..." XXX */
325 1.1.2.2 pgoyette if (req == PT_TRACE_ME) {
326 1.1.2.2 pgoyette t = p;
327 1.1.2.2 pgoyette mutex_enter(t->p_lock);
328 1.1.2.2 pgoyette } else {
329 1.1.2.2 pgoyette /* Find the process we're supposed to be operating on. */
330 1.1.2.2 pgoyette t = proc_find(pid);
331 1.1.2.2 pgoyette if (t == NULL) {
332 1.1.2.2 pgoyette mutex_exit(proc_lock);
333 1.1.2.2 pgoyette return ESRCH;
334 1.1.2.2 pgoyette }
335 1.1.2.2 pgoyette
336 1.1.2.2 pgoyette /* XXX-elad */
337 1.1.2.2 pgoyette mutex_enter(t->p_lock);
338 1.1.2.2 pgoyette error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
339 1.1.2.2 pgoyette t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
340 1.1.2.2 pgoyette if (error) {
341 1.1.2.2 pgoyette mutex_exit(proc_lock);
342 1.1.2.2 pgoyette mutex_exit(t->p_lock);
343 1.1.2.2 pgoyette return ESRCH;
344 1.1.2.2 pgoyette }
345 1.1.2.2 pgoyette }
346 1.1.2.2 pgoyette
347 1.1.2.2 pgoyette /*
348 1.1.2.2 pgoyette * Grab a reference on the process to prevent it from execing or
349 1.1.2.2 pgoyette * exiting.
350 1.1.2.2 pgoyette */
351 1.1.2.2 pgoyette if (!rw_tryenter(&t->p_reflock, RW_READER)) {
352 1.1.2.2 pgoyette mutex_exit(proc_lock);
353 1.1.2.2 pgoyette mutex_exit(t->p_lock);
354 1.1.2.2 pgoyette return EBUSY;
355 1.1.2.2 pgoyette }
356 1.1.2.2 pgoyette
357 1.1.2.2 pgoyette /* Make sure we can operate on it. */
358 1.1.2.2 pgoyette switch (req) {
359 1.1.2.2 pgoyette case PT_TRACE_ME:
360 1.1.2.2 pgoyette /* Saying that you're being traced is always legal. */
361 1.1.2.2 pgoyette break;
362 1.1.2.2 pgoyette
363 1.1.2.2 pgoyette case PT_ATTACH:
364 1.1.2.2 pgoyette /*
365 1.1.2.2 pgoyette * You can't attach to a process if:
366 1.1.2.2 pgoyette * (1) it's the process that's doing the attaching,
367 1.1.2.2 pgoyette */
368 1.1.2.2 pgoyette if (t->p_pid == p->p_pid) {
369 1.1.2.2 pgoyette error = EINVAL;
370 1.1.2.2 pgoyette break;
371 1.1.2.2 pgoyette }
372 1.1.2.2 pgoyette
373 1.1.2.2 pgoyette /*
374 1.1.2.2 pgoyette * (2) it's a system process
375 1.1.2.2 pgoyette */
376 1.1.2.2 pgoyette if (t->p_flag & PK_SYSTEM) {
377 1.1.2.2 pgoyette error = EPERM;
378 1.1.2.2 pgoyette break;
379 1.1.2.2 pgoyette }
380 1.1.2.2 pgoyette
381 1.1.2.2 pgoyette /*
382 1.1.2.2 pgoyette * (3) it's already being traced, or
383 1.1.2.2 pgoyette */
384 1.1.2.2 pgoyette if (ISSET(t->p_slflag, PSL_TRACED)) {
385 1.1.2.2 pgoyette error = EBUSY;
386 1.1.2.2 pgoyette break;
387 1.1.2.2 pgoyette }
388 1.1.2.2 pgoyette
389 1.1.2.2 pgoyette /*
390 1.1.2.2 pgoyette * (4) the tracer is chrooted, and its root directory is
391 1.1.2.2 pgoyette * not at or above the root directory of the tracee
392 1.1.2.2 pgoyette */
393 1.1.2.2 pgoyette mutex_exit(t->p_lock); /* XXXSMP */
394 1.1.2.2 pgoyette tmp = proc_isunder(t, l);
395 1.1.2.2 pgoyette mutex_enter(t->p_lock); /* XXXSMP */
396 1.1.2.2 pgoyette if (!tmp) {
397 1.1.2.2 pgoyette error = EPERM;
398 1.1.2.2 pgoyette break;
399 1.1.2.2 pgoyette }
400 1.1.2.2 pgoyette break;
401 1.1.2.2 pgoyette
402 1.1.2.2 pgoyette case PT_READ_I:
403 1.1.2.2 pgoyette case PT_READ_D:
404 1.1.2.2 pgoyette case PT_WRITE_I:
405 1.1.2.2 pgoyette case PT_WRITE_D:
406 1.1.2.2 pgoyette case PT_IO:
407 1.1.2.4 pgoyette case PT_SET_SIGINFO:
408 1.1.2.4 pgoyette case PT_GET_SIGINFO:
409 1.1.2.4 pgoyette case PT_SET_SIGMASK:
410 1.1.2.4 pgoyette case PT_GET_SIGMASK:
411 1.1.2.2 pgoyette #ifdef PT_GETREGS
412 1.1.2.2 pgoyette case PT_GETREGS:
413 1.1.2.2 pgoyette #endif
414 1.1.2.2 pgoyette #ifdef PT_SETREGS
415 1.1.2.2 pgoyette case PT_SETREGS:
416 1.1.2.2 pgoyette #endif
417 1.1.2.2 pgoyette #ifdef PT_GETFPREGS
418 1.1.2.2 pgoyette case PT_GETFPREGS:
419 1.1.2.2 pgoyette #endif
420 1.1.2.2 pgoyette #ifdef PT_SETFPREGS
421 1.1.2.2 pgoyette case PT_SETFPREGS:
422 1.1.2.2 pgoyette #endif
423 1.1.2.4 pgoyette #ifdef PT_GETDBREGS
424 1.1.2.4 pgoyette case PT_GETDBREGS:
425 1.1.2.4 pgoyette #endif
426 1.1.2.4 pgoyette #ifdef PT_SETDBREGS
427 1.1.2.4 pgoyette case PT_SETDBREGS:
428 1.1.2.3 pgoyette #endif
429 1.1.2.2 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
430 1.1.2.2 pgoyette PTRACE_MACHDEP_REQUEST_CASES
431 1.1.2.2 pgoyette #endif
432 1.1.2.2 pgoyette /*
433 1.1.2.2 pgoyette * You can't read/write the memory or registers of a process
434 1.1.2.2 pgoyette * if the tracer is chrooted, and its root directory is not at
435 1.1.2.2 pgoyette * or above the root directory of the tracee.
436 1.1.2.2 pgoyette */
437 1.1.2.2 pgoyette mutex_exit(t->p_lock); /* XXXSMP */
438 1.1.2.2 pgoyette tmp = proc_isunder(t, l);
439 1.1.2.2 pgoyette mutex_enter(t->p_lock); /* XXXSMP */
440 1.1.2.2 pgoyette if (!tmp) {
441 1.1.2.2 pgoyette error = EPERM;
442 1.1.2.2 pgoyette break;
443 1.1.2.2 pgoyette }
444 1.1.2.2 pgoyette /*FALLTHROUGH*/
445 1.1.2.2 pgoyette
446 1.1.2.2 pgoyette case PT_CONTINUE:
447 1.1.2.2 pgoyette case PT_KILL:
448 1.1.2.2 pgoyette case PT_DETACH:
449 1.1.2.2 pgoyette case PT_LWPINFO:
450 1.1.2.2 pgoyette case PT_SYSCALL:
451 1.1.2.2 pgoyette case PT_SYSCALLEMU:
452 1.1.2.2 pgoyette case PT_DUMPCORE:
453 1.1.2.2 pgoyette #ifdef PT_STEP
454 1.1.2.2 pgoyette case PT_STEP:
455 1.1.2.2 pgoyette #endif
456 1.1.2.2 pgoyette case PT_SET_EVENT_MASK:
457 1.1.2.2 pgoyette case PT_GET_EVENT_MASK:
458 1.1.2.2 pgoyette case PT_GET_PROCESS_STATE:
459 1.1.2.4 pgoyette case PT_RESUME:
460 1.1.2.4 pgoyette case PT_SUSPEND:
461 1.1.2.2 pgoyette /*
462 1.1.2.2 pgoyette * You can't do what you want to the process if:
463 1.1.2.2 pgoyette * (1) It's not being traced at all,
464 1.1.2.2 pgoyette */
465 1.1.2.2 pgoyette if (!ISSET(t->p_slflag, PSL_TRACED)) {
466 1.1.2.2 pgoyette error = EPERM;
467 1.1.2.2 pgoyette break;
468 1.1.2.2 pgoyette }
469 1.1.2.2 pgoyette
470 1.1.2.2 pgoyette /*
471 1.1.2.2 pgoyette * (2) it's being traced by procfs (which has
472 1.1.2.2 pgoyette * different signal delivery semantics),
473 1.1.2.2 pgoyette */
474 1.1.2.2 pgoyette if (ISSET(t->p_slflag, PSL_FSTRACE)) {
475 1.1.2.2 pgoyette DPRINTF(("file system traced\n"));
476 1.1.2.2 pgoyette error = EBUSY;
477 1.1.2.2 pgoyette break;
478 1.1.2.2 pgoyette }
479 1.1.2.2 pgoyette
480 1.1.2.2 pgoyette /*
481 1.1.2.2 pgoyette * (3) it's not being traced by _you_, or
482 1.1.2.2 pgoyette */
483 1.1.2.2 pgoyette if (t->p_pptr != p) {
484 1.1.2.2 pgoyette DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
485 1.1.2.2 pgoyette p->p_pid));
486 1.1.2.2 pgoyette error = EBUSY;
487 1.1.2.2 pgoyette break;
488 1.1.2.2 pgoyette }
489 1.1.2.2 pgoyette
490 1.1.2.2 pgoyette /*
491 1.1.2.2 pgoyette * (4) it's not currently stopped.
492 1.1.2.2 pgoyette */
493 1.1.2.2 pgoyette if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
494 1.1.2.2 pgoyette DPRINTF(("stat %d flag %d\n", t->p_stat,
495 1.1.2.2 pgoyette !t->p_waited));
496 1.1.2.2 pgoyette error = EBUSY;
497 1.1.2.2 pgoyette break;
498 1.1.2.2 pgoyette }
499 1.1.2.2 pgoyette break;
500 1.1.2.2 pgoyette
501 1.1.2.2 pgoyette default: /* It was not a legal request. */
502 1.1.2.2 pgoyette error = EINVAL;
503 1.1.2.2 pgoyette break;
504 1.1.2.2 pgoyette }
505 1.1.2.2 pgoyette
506 1.1.2.2 pgoyette if (error == 0) {
507 1.1.2.2 pgoyette error = kauth_authorize_process(l->l_cred,
508 1.1.2.2 pgoyette KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
509 1.1.2.2 pgoyette NULL, NULL);
510 1.1.2.2 pgoyette }
511 1.1.2.2 pgoyette if (error == 0) {
512 1.1.2.2 pgoyette lt = lwp_find_first(t);
513 1.1.2.2 pgoyette if (lt == NULL)
514 1.1.2.2 pgoyette error = ESRCH;
515 1.1.2.2 pgoyette }
516 1.1.2.2 pgoyette
517 1.1.2.2 pgoyette if (error != 0) {
518 1.1.2.2 pgoyette mutex_exit(proc_lock);
519 1.1.2.2 pgoyette mutex_exit(t->p_lock);
520 1.1.2.2 pgoyette rw_exit(&t->p_reflock);
521 1.1.2.2 pgoyette return error;
522 1.1.2.2 pgoyette }
523 1.1.2.2 pgoyette
524 1.1.2.2 pgoyette /* Do single-step fixup if needed. */
525 1.1.2.2 pgoyette FIX_SSTEP(t);
526 1.1.2.2 pgoyette KASSERT(lt != NULL);
527 1.1.2.2 pgoyette lwp_addref(lt);
528 1.1.2.2 pgoyette
529 1.1.2.2 pgoyette /*
530 1.1.2.2 pgoyette * Which locks do we need held? XXX Ugly.
531 1.1.2.2 pgoyette */
532 1.1.2.2 pgoyette switch (req) {
533 1.1.2.2 pgoyette #ifdef PT_STEP
534 1.1.2.2 pgoyette case PT_STEP:
535 1.1.2.2 pgoyette #endif
536 1.1.2.2 pgoyette case PT_CONTINUE:
537 1.1.2.2 pgoyette case PT_DETACH:
538 1.1.2.2 pgoyette case PT_KILL:
539 1.1.2.2 pgoyette case PT_SYSCALL:
540 1.1.2.2 pgoyette case PT_SYSCALLEMU:
541 1.1.2.2 pgoyette case PT_ATTACH:
542 1.1.2.2 pgoyette case PT_TRACE_ME:
543 1.1.2.2 pgoyette pheld = 1;
544 1.1.2.2 pgoyette break;
545 1.1.2.2 pgoyette default:
546 1.1.2.2 pgoyette mutex_exit(proc_lock);
547 1.1.2.2 pgoyette mutex_exit(t->p_lock);
548 1.1.2.2 pgoyette pheld = 0;
549 1.1.2.2 pgoyette break;
550 1.1.2.2 pgoyette }
551 1.1.2.2 pgoyette
552 1.1.2.2 pgoyette /* Now do the operation. */
553 1.1.2.2 pgoyette write = 0;
554 1.1.2.2 pgoyette *retval = 0;
555 1.1.2.2 pgoyette tmp = 0;
556 1.1.2.2 pgoyette resume_all = 1;
557 1.1.2.2 pgoyette
558 1.1.2.2 pgoyette switch (req) {
559 1.1.2.2 pgoyette case PT_TRACE_ME:
560 1.1.2.2 pgoyette /* Just set the trace flag. */
561 1.1.2.2 pgoyette SET(t->p_slflag, PSL_TRACED);
562 1.1.2.2 pgoyette t->p_opptr = t->p_pptr;
563 1.1.2.2 pgoyette break;
564 1.1.2.2 pgoyette
565 1.1.2.2 pgoyette case PT_WRITE_I: /* XXX no separate I and D spaces */
566 1.1.2.2 pgoyette case PT_WRITE_D:
567 1.1.2.2 pgoyette #if defined(__HAVE_RAS)
568 1.1.2.2 pgoyette /*
569 1.1.2.2 pgoyette * Can't write to a RAS
570 1.1.2.2 pgoyette */
571 1.1.2.2 pgoyette if (ras_lookup(t, addr) != (void *)-1) {
572 1.1.2.2 pgoyette error = EACCES;
573 1.1.2.2 pgoyette break;
574 1.1.2.2 pgoyette }
575 1.1.2.2 pgoyette #endif
576 1.1.2.2 pgoyette write = 1;
577 1.1.2.2 pgoyette tmp = data;
578 1.1.2.2 pgoyette /* FALLTHROUGH */
579 1.1.2.2 pgoyette
580 1.1.2.2 pgoyette case PT_READ_I: /* XXX no separate I and D spaces */
581 1.1.2.2 pgoyette case PT_READ_D:
582 1.1.2.2 pgoyette /* write = 0 done above. */
583 1.1.2.2 pgoyette iov.iov_base = (void *)&tmp;
584 1.1.2.2 pgoyette iov.iov_len = sizeof(tmp);
585 1.1.2.2 pgoyette uio.uio_iov = &iov;
586 1.1.2.2 pgoyette uio.uio_iovcnt = 1;
587 1.1.2.2 pgoyette uio.uio_offset = (off_t)(unsigned long)addr;
588 1.1.2.2 pgoyette uio.uio_resid = sizeof(tmp);
589 1.1.2.2 pgoyette uio.uio_rw = write ? UIO_WRITE : UIO_READ;
590 1.1.2.2 pgoyette UIO_SETUP_SYSSPACE(&uio);
591 1.1.2.2 pgoyette
592 1.1.2.2 pgoyette error = process_domem(l, lt, &uio);
593 1.1.2.2 pgoyette if (!write)
594 1.1.2.2 pgoyette *retval = tmp;
595 1.1.2.2 pgoyette break;
596 1.1.2.2 pgoyette
597 1.1.2.2 pgoyette case PT_IO:
598 1.1.2.2 pgoyette error = ptm->ptm_copyinpiod(&piod, addr);
599 1.1.2.2 pgoyette if (error)
600 1.1.2.2 pgoyette break;
601 1.1.2.2 pgoyette
602 1.1.2.2 pgoyette iov.iov_base = piod.piod_addr;
603 1.1.2.2 pgoyette iov.iov_len = piod.piod_len;
604 1.1.2.2 pgoyette uio.uio_iov = &iov;
605 1.1.2.2 pgoyette uio.uio_iovcnt = 1;
606 1.1.2.2 pgoyette uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
607 1.1.2.2 pgoyette uio.uio_resid = piod.piod_len;
608 1.1.2.2 pgoyette
609 1.1.2.2 pgoyette switch (piod.piod_op) {
610 1.1.2.2 pgoyette case PIOD_READ_D:
611 1.1.2.2 pgoyette case PIOD_READ_I:
612 1.1.2.2 pgoyette uio.uio_rw = UIO_READ;
613 1.1.2.2 pgoyette break;
614 1.1.2.2 pgoyette case PIOD_WRITE_D:
615 1.1.2.2 pgoyette case PIOD_WRITE_I:
616 1.1.2.2 pgoyette /*
617 1.1.2.2 pgoyette * Can't write to a RAS
618 1.1.2.2 pgoyette */
619 1.1.2.2 pgoyette if (ras_lookup(t, addr) != (void *)-1) {
620 1.1.2.2 pgoyette return EACCES;
621 1.1.2.2 pgoyette }
622 1.1.2.2 pgoyette uio.uio_rw = UIO_WRITE;
623 1.1.2.2 pgoyette break;
624 1.1.2.2 pgoyette case PIOD_READ_AUXV:
625 1.1.2.2 pgoyette req = PT_READ_D;
626 1.1.2.2 pgoyette uio.uio_rw = UIO_READ;
627 1.1.2.4 pgoyette tmp = t->p_execsw->es_arglen;
628 1.1.2.2 pgoyette if (uio.uio_offset > tmp)
629 1.1.2.2 pgoyette return EIO;
630 1.1.2.2 pgoyette if (uio.uio_resid > tmp - uio.uio_offset)
631 1.1.2.2 pgoyette uio.uio_resid = tmp - uio.uio_offset;
632 1.1.2.2 pgoyette piod.piod_len = iov.iov_len = uio.uio_resid;
633 1.1.2.2 pgoyette error = process_auxv_offset(t, &uio);
634 1.1.2.2 pgoyette break;
635 1.1.2.2 pgoyette default:
636 1.1.2.2 pgoyette error = EINVAL;
637 1.1.2.2 pgoyette break;
638 1.1.2.2 pgoyette }
639 1.1.2.2 pgoyette if (error)
640 1.1.2.2 pgoyette break;
641 1.1.2.2 pgoyette error = proc_vmspace_getref(l->l_proc, &vm);
642 1.1.2.2 pgoyette if (error)
643 1.1.2.2 pgoyette break;
644 1.1.2.2 pgoyette uio.uio_vmspace = vm;
645 1.1.2.2 pgoyette
646 1.1.2.2 pgoyette error = process_domem(l, lt, &uio);
647 1.1.2.2 pgoyette piod.piod_len -= uio.uio_resid;
648 1.1.2.2 pgoyette (void) ptm->ptm_copyoutpiod(&piod, addr);
649 1.1.2.2 pgoyette
650 1.1.2.2 pgoyette uvmspace_free(vm);
651 1.1.2.2 pgoyette break;
652 1.1.2.2 pgoyette
653 1.1.2.2 pgoyette case PT_DUMPCORE:
654 1.1.2.2 pgoyette if ((path = addr) != NULL) {
655 1.1.2.2 pgoyette char *dst;
656 1.1.2.2 pgoyette len = data;
657 1.1.2.2 pgoyette
658 1.1.2.2 pgoyette if (len < 0 || len >= MAXPATHLEN) {
659 1.1.2.2 pgoyette error = EINVAL;
660 1.1.2.2 pgoyette break;
661 1.1.2.2 pgoyette }
662 1.1.2.2 pgoyette dst = kmem_alloc(len + 1, KM_SLEEP);
663 1.1.2.2 pgoyette if ((error = copyin(path, dst, len)) != 0) {
664 1.1.2.2 pgoyette kmem_free(dst, len + 1);
665 1.1.2.2 pgoyette break;
666 1.1.2.2 pgoyette }
667 1.1.2.2 pgoyette path = dst;
668 1.1.2.2 pgoyette path[len] = '\0';
669 1.1.2.2 pgoyette }
670 1.1.2.2 pgoyette error = (*coredump_vec)(lt, path);
671 1.1.2.2 pgoyette if (path)
672 1.1.2.2 pgoyette kmem_free(path, len + 1);
673 1.1.2.2 pgoyette break;
674 1.1.2.2 pgoyette
675 1.1.2.2 pgoyette #ifdef PT_STEP
676 1.1.2.2 pgoyette case PT_STEP:
677 1.1.2.2 pgoyette /*
678 1.1.2.2 pgoyette * From the 4.4BSD PRM:
679 1.1.2.2 pgoyette * "Execution continues as in request PT_CONTINUE; however
680 1.1.2.2 pgoyette * as soon as possible after execution of at least one
681 1.1.2.2 pgoyette * instruction, execution stops again. [ ... ]"
682 1.1.2.2 pgoyette */
683 1.1.2.2 pgoyette #endif
684 1.1.2.2 pgoyette case PT_CONTINUE:
685 1.1.2.2 pgoyette case PT_SYSCALL:
686 1.1.2.2 pgoyette case PT_DETACH:
687 1.1.2.2 pgoyette if (req == PT_SYSCALL) {
688 1.1.2.2 pgoyette if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
689 1.1.2.2 pgoyette SET(t->p_slflag, PSL_SYSCALL);
690 1.1.2.2 pgoyette #ifdef __HAVE_SYSCALL_INTERN
691 1.1.2.2 pgoyette (*t->p_emul->e_syscall_intern)(t);
692 1.1.2.2 pgoyette #endif
693 1.1.2.2 pgoyette }
694 1.1.2.2 pgoyette } else {
695 1.1.2.2 pgoyette if (ISSET(t->p_slflag, PSL_SYSCALL)) {
696 1.1.2.2 pgoyette CLR(t->p_slflag, PSL_SYSCALL);
697 1.1.2.2 pgoyette #ifdef __HAVE_SYSCALL_INTERN
698 1.1.2.2 pgoyette (*t->p_emul->e_syscall_intern)(t);
699 1.1.2.2 pgoyette #endif
700 1.1.2.2 pgoyette }
701 1.1.2.2 pgoyette }
702 1.1.2.2 pgoyette t->p_trace_enabled = trace_is_enabled(t);
703 1.1.2.2 pgoyette
704 1.1.2.2 pgoyette /*
705 1.1.2.2 pgoyette * Pick up the LWPID, if supplied. There are two cases:
706 1.1.2.2 pgoyette * data < 0 : step or continue single thread, lwp = -data
707 1.1.2.2 pgoyette * data > 0 in PT_STEP : step this thread, continue others
708 1.1.2.2 pgoyette * For operations other than PT_STEP, data > 0 means
709 1.1.2.2 pgoyette * data is the signo to deliver to the process.
710 1.1.2.2 pgoyette */
711 1.1.2.2 pgoyette tmp = data;
712 1.1.2.2 pgoyette if (tmp >= 0) {
713 1.1.2.2 pgoyette #ifdef PT_STEP
714 1.1.2.2 pgoyette if (req == PT_STEP)
715 1.1.2.2 pgoyette signo = 0;
716 1.1.2.2 pgoyette else
717 1.1.2.2 pgoyette #endif
718 1.1.2.2 pgoyette {
719 1.1.2.2 pgoyette signo = tmp;
720 1.1.2.2 pgoyette tmp = 0; /* don't search for LWP */
721 1.1.2.2 pgoyette }
722 1.1.2.2 pgoyette } else
723 1.1.2.2 pgoyette tmp = -tmp;
724 1.1.2.2 pgoyette
725 1.1.2.2 pgoyette if (tmp > 0) {
726 1.1.2.2 pgoyette if (req == PT_DETACH) {
727 1.1.2.2 pgoyette error = EINVAL;
728 1.1.2.2 pgoyette break;
729 1.1.2.2 pgoyette }
730 1.1.2.2 pgoyette lwp_delref2 (lt);
731 1.1.2.2 pgoyette lt = lwp_find(t, tmp);
732 1.1.2.2 pgoyette if (lt == NULL) {
733 1.1.2.2 pgoyette error = ESRCH;
734 1.1.2.2 pgoyette break;
735 1.1.2.2 pgoyette }
736 1.1.2.2 pgoyette lwp_addref(lt);
737 1.1.2.2 pgoyette resume_all = 0;
738 1.1.2.2 pgoyette signo = 0;
739 1.1.2.2 pgoyette }
740 1.1.2.2 pgoyette
741 1.1.2.2 pgoyette /*
742 1.1.2.2 pgoyette * From the 4.4BSD PRM:
743 1.1.2.2 pgoyette * "The data argument is taken as a signal number and the
744 1.1.2.2 pgoyette * child's execution continues at location addr as if it
745 1.1.2.2 pgoyette * incurred that signal. Normally the signal number will
746 1.1.2.2 pgoyette * be either 0 to indicate that the signal that caused the
747 1.1.2.2 pgoyette * stop should be ignored, or that value fetched out of
748 1.1.2.2 pgoyette * the process's image indicating which signal caused
749 1.1.2.2 pgoyette * the stop. If addr is (int *)1 then execution continues
750 1.1.2.2 pgoyette * from where it stopped."
751 1.1.2.2 pgoyette */
752 1.1.2.2 pgoyette
753 1.1.2.2 pgoyette /* Check that the data is a valid signal number or zero. */
754 1.1.2.2 pgoyette if (signo < 0 || signo >= NSIG) {
755 1.1.2.2 pgoyette error = EINVAL;
756 1.1.2.2 pgoyette break;
757 1.1.2.2 pgoyette }
758 1.1.2.2 pgoyette
759 1.1.2.4 pgoyette /* Prevent process deadlock */
760 1.1.2.4 pgoyette if (resume_all) {
761 1.1.2.4 pgoyette #ifdef PT_STEP
762 1.1.2.4 pgoyette if (req == PT_STEP) {
763 1.1.2.4 pgoyette if (lt->l_flag & LW_WSUSPEND) {
764 1.1.2.4 pgoyette error = EDEADLK;
765 1.1.2.4 pgoyette break;
766 1.1.2.4 pgoyette }
767 1.1.2.4 pgoyette } else
768 1.1.2.4 pgoyette #endif
769 1.1.2.4 pgoyette {
770 1.1.2.4 pgoyette error = EDEADLK;
771 1.1.2.4 pgoyette LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
772 1.1.2.4 pgoyette if ((lt2->l_flag & LW_WSUSPEND) == 0) {
773 1.1.2.4 pgoyette error = 0;
774 1.1.2.4 pgoyette break;
775 1.1.2.4 pgoyette }
776 1.1.2.4 pgoyette }
777 1.1.2.4 pgoyette if (error != 0)
778 1.1.2.4 pgoyette break;
779 1.1.2.4 pgoyette }
780 1.1.2.4 pgoyette } else {
781 1.1.2.4 pgoyette if (lt->l_flag & LW_WSUSPEND) {
782 1.1.2.4 pgoyette error = EDEADLK;
783 1.1.2.4 pgoyette break;
784 1.1.2.4 pgoyette }
785 1.1.2.4 pgoyette }
786 1.1.2.4 pgoyette
787 1.1.2.2 pgoyette /* If the address parameter is not (int *)1, set the pc. */
788 1.1.2.2 pgoyette if ((int *)addr != (int *)1) {
789 1.1.2.2 pgoyette error = process_set_pc(lt, addr);
790 1.1.2.2 pgoyette if (error != 0)
791 1.1.2.2 pgoyette break;
792 1.1.2.2 pgoyette }
793 1.1.2.2 pgoyette #ifdef PT_STEP
794 1.1.2.2 pgoyette /*
795 1.1.2.2 pgoyette * Arrange for a single-step, if that's requested and possible.
796 1.1.2.2 pgoyette * More precisely, set the single step status as requested for
797 1.1.2.2 pgoyette * the requested thread, and clear it for other threads.
798 1.1.2.2 pgoyette */
799 1.1.2.2 pgoyette LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
800 1.1.2.2 pgoyette if (lt != lt2) {
801 1.1.2.2 pgoyette lwp_lock(lt2);
802 1.1.2.2 pgoyette process_sstep(lt2, 0);
803 1.1.2.2 pgoyette lwp_unlock(lt2);
804 1.1.2.2 pgoyette }
805 1.1.2.2 pgoyette }
806 1.1.2.2 pgoyette error = process_sstep(lt, req == PT_STEP);
807 1.1.2.2 pgoyette if (error)
808 1.1.2.2 pgoyette break;
809 1.1.2.2 pgoyette #endif
810 1.1.2.2 pgoyette if (req == PT_DETACH) {
811 1.1.2.2 pgoyette CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
812 1.1.2.2 pgoyette
813 1.1.2.2 pgoyette /* give process back to original parent or init */
814 1.1.2.2 pgoyette if (t->p_opptr != t->p_pptr) {
815 1.1.2.2 pgoyette struct proc *pp = t->p_opptr;
816 1.1.2.2 pgoyette proc_reparent(t, pp ? pp : initproc);
817 1.1.2.2 pgoyette }
818 1.1.2.2 pgoyette
819 1.1.2.2 pgoyette /* not being traced any more */
820 1.1.2.2 pgoyette t->p_opptr = NULL;
821 1.1.2.2 pgoyette }
822 1.1.2.2 pgoyette sendsig:
823 1.1.2.2 pgoyette t->p_fpid = 0;
824 1.1.2.4 pgoyette t->p_vfpid = 0;
825 1.1.2.4 pgoyette t->p_vfpid_done = 0;
826 1.1.2.4 pgoyette t->p_lwp_created = 0;
827 1.1.2.4 pgoyette t->p_lwp_exited = 0;
828 1.1.2.2 pgoyette /* Finally, deliver the requested signal (or none). */
829 1.1.2.2 pgoyette if (t->p_stat == SSTOP) {
830 1.1.2.2 pgoyette /*
831 1.1.2.2 pgoyette * Unstop the process. If it needs to take a
832 1.1.2.2 pgoyette * signal, make all efforts to ensure that at
833 1.1.2.2 pgoyette * an LWP runs to see it.
834 1.1.2.2 pgoyette */
835 1.1.2.2 pgoyette t->p_xsig = signo;
836 1.1.2.2 pgoyette if (resume_all)
837 1.1.2.2 pgoyette proc_unstop(t);
838 1.1.2.2 pgoyette else
839 1.1.2.2 pgoyette lwp_unstop(lt);
840 1.1.2.3 pgoyette } else if (t->p_sigctx.ps_faked) {
841 1.1.2.3 pgoyette if (signo != t->p_sigctx.ps_info._signo) {
842 1.1.2.3 pgoyette error = EINVAL;
843 1.1.2.3 pgoyette break;
844 1.1.2.3 pgoyette }
845 1.1.2.3 pgoyette t->p_sigctx.ps_faked = false;
846 1.1.2.3 pgoyette KSI_INIT_EMPTY(&ksi);
847 1.1.2.3 pgoyette ksi.ksi_info = t->p_sigctx.ps_info;
848 1.1.2.3 pgoyette ksi.ksi_lid = t->p_sigctx.ps_lwp;
849 1.1.2.3 pgoyette kpsignal2(t, &ksi);
850 1.1.2.2 pgoyette } else if (signo != 0) {
851 1.1.2.2 pgoyette KSI_INIT_EMPTY(&ksi);
852 1.1.2.2 pgoyette ksi.ksi_signo = signo;
853 1.1.2.2 pgoyette kpsignal2(t, &ksi);
854 1.1.2.2 pgoyette }
855 1.1.2.2 pgoyette break;
856 1.1.2.2 pgoyette
857 1.1.2.2 pgoyette case PT_SYSCALLEMU:
858 1.1.2.2 pgoyette if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
859 1.1.2.2 pgoyette error = EINVAL;
860 1.1.2.2 pgoyette break;
861 1.1.2.2 pgoyette }
862 1.1.2.2 pgoyette SET(t->p_slflag, PSL_SYSCALLEMU);
863 1.1.2.2 pgoyette break;
864 1.1.2.2 pgoyette
865 1.1.2.2 pgoyette case PT_KILL:
866 1.1.2.2 pgoyette /* just send the process a KILL signal. */
867 1.1.2.2 pgoyette signo = SIGKILL;
868 1.1.2.2 pgoyette goto sendsig; /* in PT_CONTINUE, above. */
869 1.1.2.2 pgoyette
870 1.1.2.2 pgoyette case PT_ATTACH:
871 1.1.2.2 pgoyette /*
872 1.1.2.2 pgoyette * Go ahead and set the trace flag.
873 1.1.2.2 pgoyette * Save the old parent (it's reset in
874 1.1.2.2 pgoyette * _DETACH, and also in kern_exit.c:wait4()
875 1.1.2.2 pgoyette * Reparent the process so that the tracing
876 1.1.2.2 pgoyette * proc gets to see all the action.
877 1.1.2.2 pgoyette * Stop the target.
878 1.1.2.2 pgoyette */
879 1.1.2.3 pgoyette proc_changeparent(t, p);
880 1.1.2.2 pgoyette signo = SIGSTOP;
881 1.1.2.2 pgoyette goto sendsig;
882 1.1.2.2 pgoyette
883 1.1.2.2 pgoyette case PT_GET_EVENT_MASK:
884 1.1.2.2 pgoyette if (data != sizeof(pe)) {
885 1.1.2.2 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req,
886 1.1.2.2 pgoyette data, sizeof(pe)));
887 1.1.2.2 pgoyette error = EINVAL;
888 1.1.2.2 pgoyette break;
889 1.1.2.2 pgoyette }
890 1.1.2.2 pgoyette memset(&pe, 0, sizeof(pe));
891 1.1.2.2 pgoyette pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
892 1.1.2.2 pgoyette PTRACE_FORK : 0;
893 1.1.2.4 pgoyette pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
894 1.1.2.4 pgoyette PTRACE_VFORK : 0;
895 1.1.2.4 pgoyette pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
896 1.1.2.4 pgoyette PTRACE_VFORK_DONE : 0;
897 1.1.2.4 pgoyette pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
898 1.1.2.4 pgoyette PTRACE_LWP_CREATE : 0;
899 1.1.2.4 pgoyette pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
900 1.1.2.4 pgoyette PTRACE_LWP_EXIT : 0;
901 1.1.2.2 pgoyette error = copyout(&pe, addr, sizeof(pe));
902 1.1.2.2 pgoyette break;
903 1.1.2.2 pgoyette
904 1.1.2.2 pgoyette case PT_SET_EVENT_MASK:
905 1.1.2.2 pgoyette if (data != sizeof(pe)) {
906 1.1.2.2 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
907 1.1.2.2 pgoyette sizeof(pe)));
908 1.1.2.2 pgoyette error = EINVAL;
909 1.1.2.2 pgoyette break;
910 1.1.2.2 pgoyette }
911 1.1.2.2 pgoyette if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
912 1.1.2.2 pgoyette return error;
913 1.1.2.2 pgoyette if (pe.pe_set_event & PTRACE_FORK)
914 1.1.2.2 pgoyette SET(t->p_slflag, PSL_TRACEFORK);
915 1.1.2.2 pgoyette else
916 1.1.2.2 pgoyette CLR(t->p_slflag, PSL_TRACEFORK);
917 1.1.2.4 pgoyette #if notyet
918 1.1.2.4 pgoyette if (pe.pe_set_event & PTRACE_VFORK)
919 1.1.2.4 pgoyette SET(t->p_slflag, PSL_TRACEVFORK);
920 1.1.2.4 pgoyette else
921 1.1.2.4 pgoyette CLR(t->p_slflag, PSL_TRACEVFORK);
922 1.1.2.4 pgoyette #else
923 1.1.2.4 pgoyette if (pe.pe_set_event & PTRACE_VFORK) {
924 1.1.2.4 pgoyette error = ENOTSUP;
925 1.1.2.4 pgoyette break;
926 1.1.2.4 pgoyette }
927 1.1.2.4 pgoyette #endif
928 1.1.2.4 pgoyette if (pe.pe_set_event & PTRACE_VFORK_DONE)
929 1.1.2.4 pgoyette SET(t->p_slflag, PSL_TRACEVFORK_DONE);
930 1.1.2.4 pgoyette else
931 1.1.2.4 pgoyette CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
932 1.1.2.4 pgoyette if (pe.pe_set_event & PTRACE_LWP_CREATE)
933 1.1.2.4 pgoyette SET(t->p_slflag, PSL_TRACELWP_CREATE);
934 1.1.2.4 pgoyette else
935 1.1.2.4 pgoyette CLR(t->p_slflag, PSL_TRACELWP_CREATE);
936 1.1.2.4 pgoyette if (pe.pe_set_event & PTRACE_LWP_EXIT)
937 1.1.2.4 pgoyette SET(t->p_slflag, PSL_TRACELWP_EXIT);
938 1.1.2.4 pgoyette else
939 1.1.2.4 pgoyette CLR(t->p_slflag, PSL_TRACELWP_EXIT);
940 1.1.2.2 pgoyette break;
941 1.1.2.2 pgoyette
942 1.1.2.2 pgoyette case PT_GET_PROCESS_STATE:
943 1.1.2.2 pgoyette if (data != sizeof(ps)) {
944 1.1.2.2 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
945 1.1.2.2 pgoyette sizeof(ps)));
946 1.1.2.2 pgoyette error = EINVAL;
947 1.1.2.2 pgoyette break;
948 1.1.2.2 pgoyette }
949 1.1.2.2 pgoyette memset(&ps, 0, sizeof(ps));
950 1.1.2.2 pgoyette if (t->p_fpid) {
951 1.1.2.2 pgoyette ps.pe_report_event = PTRACE_FORK;
952 1.1.2.2 pgoyette ps.pe_other_pid = t->p_fpid;
953 1.1.2.4 pgoyette } else if (t->p_vfpid) {
954 1.1.2.4 pgoyette ps.pe_report_event = PTRACE_VFORK;
955 1.1.2.4 pgoyette ps.pe_other_pid = t->p_vfpid;
956 1.1.2.4 pgoyette } else if (t->p_vfpid_done) {
957 1.1.2.4 pgoyette ps.pe_report_event = PTRACE_VFORK_DONE;
958 1.1.2.4 pgoyette ps.pe_other_pid = t->p_vfpid_done;
959 1.1.2.4 pgoyette } else if (t->p_lwp_created) {
960 1.1.2.4 pgoyette ps.pe_report_event = PTRACE_LWP_CREATE;
961 1.1.2.4 pgoyette ps.pe_lwp = t->p_lwp_created;
962 1.1.2.4 pgoyette } else if (t->p_lwp_exited) {
963 1.1.2.4 pgoyette ps.pe_report_event = PTRACE_LWP_EXIT;
964 1.1.2.4 pgoyette ps.pe_lwp = t->p_lwp_exited;
965 1.1.2.2 pgoyette }
966 1.1.2.2 pgoyette error = copyout(&ps, addr, sizeof(ps));
967 1.1.2.2 pgoyette break;
968 1.1.2.2 pgoyette
969 1.1.2.2 pgoyette case PT_LWPINFO:
970 1.1.2.2 pgoyette if (data != sizeof(pl)) {
971 1.1.2.2 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
972 1.1.2.2 pgoyette sizeof(pl)));
973 1.1.2.2 pgoyette error = EINVAL;
974 1.1.2.2 pgoyette break;
975 1.1.2.2 pgoyette }
976 1.1.2.2 pgoyette error = copyin(addr, &pl, sizeof(pl));
977 1.1.2.2 pgoyette if (error)
978 1.1.2.2 pgoyette break;
979 1.1.2.2 pgoyette tmp = pl.pl_lwpid;
980 1.1.2.2 pgoyette lwp_delref(lt);
981 1.1.2.2 pgoyette mutex_enter(t->p_lock);
982 1.1.2.2 pgoyette if (tmp == 0)
983 1.1.2.2 pgoyette lt = lwp_find_first(t);
984 1.1.2.2 pgoyette else {
985 1.1.2.2 pgoyette lt = lwp_find(t, tmp);
986 1.1.2.2 pgoyette if (lt == NULL) {
987 1.1.2.2 pgoyette mutex_exit(t->p_lock);
988 1.1.2.2 pgoyette error = ESRCH;
989 1.1.2.2 pgoyette break;
990 1.1.2.2 pgoyette }
991 1.1.2.2 pgoyette lt = LIST_NEXT(lt, l_sibling);
992 1.1.2.2 pgoyette }
993 1.1.2.2 pgoyette while (lt != NULL && !lwp_alive(lt))
994 1.1.2.2 pgoyette lt = LIST_NEXT(lt, l_sibling);
995 1.1.2.2 pgoyette pl.pl_lwpid = 0;
996 1.1.2.2 pgoyette pl.pl_event = 0;
997 1.1.2.2 pgoyette if (lt) {
998 1.1.2.2 pgoyette lwp_addref(lt);
999 1.1.2.2 pgoyette pl.pl_lwpid = lt->l_lid;
1000 1.1.2.4 pgoyette
1001 1.1.2.4 pgoyette if (lt->l_flag & LW_WSUSPEND)
1002 1.1.2.4 pgoyette pl.pl_event = PL_EVENT_SUSPENDED;
1003 1.1.2.3 pgoyette /*
1004 1.1.2.3 pgoyette * If we match the lwp, or it was sent to every lwp,
1005 1.1.2.3 pgoyette * we set PL_EVENT_SIGNAL.
1006 1.1.2.3 pgoyette * XXX: ps_lwp == 0 means everyone and noone, so
1007 1.1.2.3 pgoyette * check ps_signo too.
1008 1.1.2.3 pgoyette */
1009 1.1.2.4 pgoyette else if (lt->l_lid == t->p_sigctx.ps_lwp
1010 1.1.2.4 pgoyette || (t->p_sigctx.ps_lwp == 0 &&
1011 1.1.2.4 pgoyette t->p_sigctx.ps_info._signo))
1012 1.1.2.2 pgoyette pl.pl_event = PL_EVENT_SIGNAL;
1013 1.1.2.2 pgoyette }
1014 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1015 1.1.2.2 pgoyette
1016 1.1.2.2 pgoyette error = copyout(&pl, addr, sizeof(pl));
1017 1.1.2.2 pgoyette break;
1018 1.1.2.2 pgoyette
1019 1.1.2.3 pgoyette case PT_SET_SIGINFO:
1020 1.1.2.3 pgoyette if (data != sizeof(psi)) {
1021 1.1.2.3 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
1022 1.1.2.3 pgoyette sizeof(psi)));
1023 1.1.2.3 pgoyette error = EINVAL;
1024 1.1.2.3 pgoyette break;
1025 1.1.2.3 pgoyette }
1026 1.1.2.3 pgoyette
1027 1.1.2.3 pgoyette error = copyin(addr, &psi, sizeof(psi));
1028 1.1.2.3 pgoyette if (error)
1029 1.1.2.3 pgoyette break;
1030 1.1.2.3 pgoyette
1031 1.1.2.3 pgoyette /* Check that the data is a valid signal number or zero. */
1032 1.1.2.3 pgoyette if (psi.psi_siginfo.si_signo < 0 ||
1033 1.1.2.3 pgoyette psi.psi_siginfo.si_signo >= NSIG) {
1034 1.1.2.3 pgoyette error = EINVAL;
1035 1.1.2.3 pgoyette break;
1036 1.1.2.3 pgoyette }
1037 1.1.2.3 pgoyette
1038 1.1.2.3 pgoyette tmp = psi.psi_lwpid;
1039 1.1.2.3 pgoyette if (tmp != 0)
1040 1.1.2.3 pgoyette lwp_delref(lt);
1041 1.1.2.3 pgoyette
1042 1.1.2.3 pgoyette mutex_enter(t->p_lock);
1043 1.1.2.3 pgoyette
1044 1.1.2.3 pgoyette if (tmp != 0) {
1045 1.1.2.3 pgoyette lt = lwp_find(t, tmp);
1046 1.1.2.3 pgoyette if (lt == NULL) {
1047 1.1.2.3 pgoyette mutex_exit(t->p_lock);
1048 1.1.2.3 pgoyette error = ESRCH;
1049 1.1.2.3 pgoyette break;
1050 1.1.2.3 pgoyette }
1051 1.1.2.3 pgoyette lwp_addref(lt);
1052 1.1.2.3 pgoyette }
1053 1.1.2.3 pgoyette
1054 1.1.2.3 pgoyette t->p_sigctx.ps_faked = true;
1055 1.1.2.3 pgoyette t->p_sigctx.ps_info = psi.psi_siginfo._info;
1056 1.1.2.3 pgoyette t->p_sigctx.ps_lwp = psi.psi_lwpid;
1057 1.1.2.3 pgoyette mutex_exit(t->p_lock);
1058 1.1.2.3 pgoyette break;
1059 1.1.2.3 pgoyette
1060 1.1.2.3 pgoyette case PT_GET_SIGINFO:
1061 1.1.2.3 pgoyette if (data != sizeof(psi)) {
1062 1.1.2.3 pgoyette DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
1063 1.1.2.3 pgoyette sizeof(psi)));
1064 1.1.2.3 pgoyette error = EINVAL;
1065 1.1.2.3 pgoyette break;
1066 1.1.2.3 pgoyette }
1067 1.1.2.3 pgoyette mutex_enter(t->p_lock);
1068 1.1.2.3 pgoyette psi.psi_siginfo._info = t->p_sigctx.ps_info;
1069 1.1.2.3 pgoyette psi.psi_lwpid = t->p_sigctx.ps_lwp;
1070 1.1.2.3 pgoyette mutex_exit(t->p_lock);
1071 1.1.2.3 pgoyette
1072 1.1.2.3 pgoyette error = copyout(&psi, addr, sizeof(psi));
1073 1.1.2.3 pgoyette if (error)
1074 1.1.2.3 pgoyette break;
1075 1.1.2.3 pgoyette
1076 1.1.2.3 pgoyette break;
1077 1.1.2.3 pgoyette
1078 1.1.2.4 pgoyette case PT_SET_SIGMASK:
1079 1.1.2.4 pgoyette write = 1;
1080 1.1.2.4 pgoyette
1081 1.1.2.4 pgoyette case PT_GET_SIGMASK:
1082 1.1.2.4 pgoyette /* write = 0 done above. */
1083 1.1.2.4 pgoyette
1084 1.1.2.4 pgoyette tmp = data;
1085 1.1.2.4 pgoyette if (tmp != 0 && t->p_nlwps > 1) {
1086 1.1.2.4 pgoyette lwp_delref(lt);
1087 1.1.2.4 pgoyette mutex_enter(t->p_lock);
1088 1.1.2.4 pgoyette lt = lwp_find(t, tmp);
1089 1.1.2.4 pgoyette if (lt == NULL) {
1090 1.1.2.4 pgoyette mutex_exit(t->p_lock);
1091 1.1.2.4 pgoyette error = ESRCH;
1092 1.1.2.4 pgoyette break;
1093 1.1.2.4 pgoyette }
1094 1.1.2.4 pgoyette lwp_addref(lt);
1095 1.1.2.4 pgoyette mutex_exit(t->p_lock);
1096 1.1.2.4 pgoyette }
1097 1.1.2.4 pgoyette
1098 1.1.2.4 pgoyette if (lt->l_flag & LW_SYSTEM)
1099 1.1.2.4 pgoyette error = EINVAL;
1100 1.1.2.4 pgoyette else if (write == 1) {
1101 1.1.2.4 pgoyette error = copyin(addr, <->l_sigmask, sizeof(sigset_t));
1102 1.1.2.4 pgoyette sigminusset(&sigcantmask, <->l_sigmask);
1103 1.1.2.4 pgoyette } else
1104 1.1.2.4 pgoyette error = copyout(<->l_sigmask, addr, sizeof(sigset_t));
1105 1.1.2.4 pgoyette
1106 1.1.2.4 pgoyette break;
1107 1.1.2.4 pgoyette
1108 1.1.2.4 pgoyette case PT_RESUME:
1109 1.1.2.4 pgoyette write = 1;
1110 1.1.2.4 pgoyette
1111 1.1.2.4 pgoyette case PT_SUSPEND:
1112 1.1.2.4 pgoyette /* write = 0 done above. */
1113 1.1.2.4 pgoyette
1114 1.1.2.4 pgoyette tmp = data;
1115 1.1.2.4 pgoyette if (tmp != 0 && t->p_nlwps > 1) {
1116 1.1.2.4 pgoyette lwp_delref(lt);
1117 1.1.2.4 pgoyette mutex_enter(t->p_lock);
1118 1.1.2.4 pgoyette lt = lwp_find(t, tmp);
1119 1.1.2.4 pgoyette if (lt == NULL) {
1120 1.1.2.4 pgoyette mutex_exit(t->p_lock);
1121 1.1.2.4 pgoyette error = ESRCH;
1122 1.1.2.4 pgoyette break;
1123 1.1.2.4 pgoyette }
1124 1.1.2.4 pgoyette lwp_addref(lt);
1125 1.1.2.4 pgoyette mutex_exit(t->p_lock);
1126 1.1.2.4 pgoyette }
1127 1.1.2.4 pgoyette if (lt->l_flag & LW_SYSTEM) {
1128 1.1.2.4 pgoyette error = EINVAL;
1129 1.1.2.4 pgoyette } else {
1130 1.1.2.4 pgoyette lwp_lock(lt);
1131 1.1.2.4 pgoyette if (write == 0)
1132 1.1.2.4 pgoyette lt->l_flag |= LW_WSUSPEND;
1133 1.1.2.4 pgoyette else
1134 1.1.2.4 pgoyette lt->l_flag &= ~LW_WSUSPEND;
1135 1.1.2.4 pgoyette lwp_unlock(lt);
1136 1.1.2.4 pgoyette }
1137 1.1.2.4 pgoyette break;
1138 1.1.2.4 pgoyette
1139 1.1.2.2 pgoyette #ifdef PT_SETREGS
1140 1.1.2.2 pgoyette case PT_SETREGS:
1141 1.1.2.2 pgoyette write = 1;
1142 1.1.2.2 pgoyette #endif
1143 1.1.2.2 pgoyette #ifdef PT_GETREGS
1144 1.1.2.2 pgoyette case PT_GETREGS:
1145 1.1.2.2 pgoyette /* write = 0 done above. */
1146 1.1.2.2 pgoyette #endif
1147 1.1.2.2 pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
1148 1.1.2.2 pgoyette tmp = data;
1149 1.1.2.2 pgoyette if (tmp != 0 && t->p_nlwps > 1) {
1150 1.1.2.2 pgoyette lwp_delref(lt);
1151 1.1.2.2 pgoyette mutex_enter(t->p_lock);
1152 1.1.2.2 pgoyette lt = lwp_find(t, tmp);
1153 1.1.2.2 pgoyette if (lt == NULL) {
1154 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1155 1.1.2.2 pgoyette error = ESRCH;
1156 1.1.2.2 pgoyette break;
1157 1.1.2.2 pgoyette }
1158 1.1.2.2 pgoyette lwp_addref(lt);
1159 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1160 1.1.2.2 pgoyette }
1161 1.1.2.2 pgoyette if (!process_validregs(lt))
1162 1.1.2.2 pgoyette error = EINVAL;
1163 1.1.2.2 pgoyette else {
1164 1.1.2.2 pgoyette error = proc_vmspace_getref(p, &vm);
1165 1.1.2.2 pgoyette if (error)
1166 1.1.2.2 pgoyette break;
1167 1.1.2.2 pgoyette iov.iov_base = addr;
1168 1.1.2.2 pgoyette iov.iov_len = PROC_REGSZ(p);
1169 1.1.2.2 pgoyette uio.uio_iov = &iov;
1170 1.1.2.2 pgoyette uio.uio_iovcnt = 1;
1171 1.1.2.2 pgoyette uio.uio_offset = 0;
1172 1.1.2.2 pgoyette uio.uio_resid = iov.iov_len;
1173 1.1.2.2 pgoyette uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1174 1.1.2.2 pgoyette uio.uio_vmspace = vm;
1175 1.1.2.2 pgoyette
1176 1.1.2.2 pgoyette error = ptm->ptm_doregs(l, lt, &uio);
1177 1.1.2.2 pgoyette uvmspace_free(vm);
1178 1.1.2.2 pgoyette }
1179 1.1.2.2 pgoyette break;
1180 1.1.2.2 pgoyette #endif
1181 1.1.2.2 pgoyette
1182 1.1.2.2 pgoyette #ifdef PT_SETFPREGS
1183 1.1.2.2 pgoyette case PT_SETFPREGS:
1184 1.1.2.2 pgoyette write = 1;
1185 1.1.2.3 pgoyette /*FALLTHROUGH*/
1186 1.1.2.2 pgoyette #endif
1187 1.1.2.2 pgoyette #ifdef PT_GETFPREGS
1188 1.1.2.2 pgoyette case PT_GETFPREGS:
1189 1.1.2.2 pgoyette /* write = 0 done above. */
1190 1.1.2.2 pgoyette #endif
1191 1.1.2.2 pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1192 1.1.2.2 pgoyette tmp = data;
1193 1.1.2.2 pgoyette if (tmp != 0 && t->p_nlwps > 1) {
1194 1.1.2.2 pgoyette lwp_delref(lt);
1195 1.1.2.2 pgoyette mutex_enter(t->p_lock);
1196 1.1.2.2 pgoyette lt = lwp_find(t, tmp);
1197 1.1.2.2 pgoyette if (lt == NULL) {
1198 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1199 1.1.2.2 pgoyette error = ESRCH;
1200 1.1.2.2 pgoyette break;
1201 1.1.2.2 pgoyette }
1202 1.1.2.2 pgoyette lwp_addref(lt);
1203 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1204 1.1.2.2 pgoyette }
1205 1.1.2.2 pgoyette if (!process_validfpregs(lt))
1206 1.1.2.2 pgoyette error = EINVAL;
1207 1.1.2.2 pgoyette else {
1208 1.1.2.2 pgoyette error = proc_vmspace_getref(p, &vm);
1209 1.1.2.2 pgoyette if (error)
1210 1.1.2.2 pgoyette break;
1211 1.1.2.2 pgoyette iov.iov_base = addr;
1212 1.1.2.2 pgoyette iov.iov_len = PROC_FPREGSZ(p);
1213 1.1.2.2 pgoyette uio.uio_iov = &iov;
1214 1.1.2.2 pgoyette uio.uio_iovcnt = 1;
1215 1.1.2.2 pgoyette uio.uio_offset = 0;
1216 1.1.2.2 pgoyette uio.uio_resid = iov.iov_len;
1217 1.1.2.2 pgoyette uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1218 1.1.2.2 pgoyette uio.uio_vmspace = vm;
1219 1.1.2.2 pgoyette
1220 1.1.2.2 pgoyette error = ptm->ptm_dofpregs(l, lt, &uio);
1221 1.1.2.2 pgoyette uvmspace_free(vm);
1222 1.1.2.2 pgoyette }
1223 1.1.2.2 pgoyette break;
1224 1.1.2.2 pgoyette #endif
1225 1.1.2.2 pgoyette
1226 1.1.2.4 pgoyette #ifdef PT_SETDBREGS
1227 1.1.2.4 pgoyette case PT_SETDBREGS:
1228 1.1.2.3 pgoyette write = 1;
1229 1.1.2.4 pgoyette /*FALLTHROUGH*/
1230 1.1.2.4 pgoyette #endif
1231 1.1.2.4 pgoyette #ifdef PT_GETDBREGS
1232 1.1.2.4 pgoyette case PT_GETDBREGS:
1233 1.1.2.4 pgoyette /* write = 0 done above. */
1234 1.1.2.4 pgoyette #endif
1235 1.1.2.4 pgoyette #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
1236 1.1.2.4 pgoyette tmp = data;
1237 1.1.2.3 pgoyette if (tmp != 0 && t->p_nlwps > 1) {
1238 1.1.2.3 pgoyette lwp_delref(lt);
1239 1.1.2.3 pgoyette mutex_enter(t->p_lock);
1240 1.1.2.3 pgoyette lt = lwp_find(t, tmp);
1241 1.1.2.3 pgoyette if (lt == NULL) {
1242 1.1.2.3 pgoyette mutex_exit(t->p_lock);
1243 1.1.2.3 pgoyette error = ESRCH;
1244 1.1.2.3 pgoyette break;
1245 1.1.2.3 pgoyette }
1246 1.1.2.3 pgoyette lwp_addref(lt);
1247 1.1.2.3 pgoyette mutex_exit(t->p_lock);
1248 1.1.2.3 pgoyette }
1249 1.1.2.4 pgoyette if (!process_validdbregs(lt))
1250 1.1.2.3 pgoyette error = EINVAL;
1251 1.1.2.3 pgoyette else {
1252 1.1.2.4 pgoyette error = proc_vmspace_getref(p, &vm);
1253 1.1.2.4 pgoyette if (error)
1254 1.1.2.4 pgoyette break;
1255 1.1.2.4 pgoyette iov.iov_base = addr;
1256 1.1.2.4 pgoyette iov.iov_len = PROC_DBREGSZ(p);
1257 1.1.2.4 pgoyette uio.uio_iov = &iov;
1258 1.1.2.4 pgoyette uio.uio_iovcnt = 1;
1259 1.1.2.4 pgoyette uio.uio_offset = 0;
1260 1.1.2.4 pgoyette uio.uio_resid = iov.iov_len;
1261 1.1.2.4 pgoyette uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1262 1.1.2.4 pgoyette uio.uio_vmspace = vm;
1263 1.1.2.4 pgoyette
1264 1.1.2.4 pgoyette error = ptm->ptm_dodbregs(l, lt, &uio);
1265 1.1.2.4 pgoyette uvmspace_free(vm);
1266 1.1.2.3 pgoyette }
1267 1.1.2.3 pgoyette break;
1268 1.1.2.3 pgoyette #endif
1269 1.1.2.3 pgoyette
1270 1.1.2.2 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
1271 1.1.2.2 pgoyette PTRACE_MACHDEP_REQUEST_CASES
1272 1.1.2.2 pgoyette error = ptrace_machdep_dorequest(l, lt, req, addr, data);
1273 1.1.2.2 pgoyette break;
1274 1.1.2.2 pgoyette #endif
1275 1.1.2.2 pgoyette }
1276 1.1.2.2 pgoyette
1277 1.1.2.2 pgoyette if (pheld) {
1278 1.1.2.2 pgoyette mutex_exit(t->p_lock);
1279 1.1.2.2 pgoyette mutex_exit(proc_lock);
1280 1.1.2.2 pgoyette }
1281 1.1.2.2 pgoyette if (lt != NULL)
1282 1.1.2.2 pgoyette lwp_delref(lt);
1283 1.1.2.2 pgoyette rw_exit(&t->p_reflock);
1284 1.1.2.2 pgoyette
1285 1.1.2.2 pgoyette return error;
1286 1.1.2.2 pgoyette }
1287 1.1.2.2 pgoyette
1288 1.1.2.2 pgoyette int
1289 1.1.2.2 pgoyette process_doregs(struct lwp *curl /*tracer*/,
1290 1.1.2.2 pgoyette struct lwp *l /*traced*/,
1291 1.1.2.2 pgoyette struct uio *uio)
1292 1.1.2.2 pgoyette {
1293 1.1.2.2 pgoyette #if defined(PT_GETREGS) || defined(PT_SETREGS)
1294 1.1.2.2 pgoyette int error;
1295 1.1.2.2 pgoyette struct reg r;
1296 1.1.2.2 pgoyette char *kv;
1297 1.1.2.2 pgoyette int kl;
1298 1.1.2.2 pgoyette
1299 1.1.2.2 pgoyette if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1300 1.1.2.2 pgoyette return EINVAL;
1301 1.1.2.2 pgoyette
1302 1.1.2.2 pgoyette kl = sizeof(r);
1303 1.1.2.2 pgoyette kv = (char *)&r;
1304 1.1.2.2 pgoyette
1305 1.1.2.2 pgoyette kv += uio->uio_offset;
1306 1.1.2.2 pgoyette kl -= uio->uio_offset;
1307 1.1.2.2 pgoyette if ((size_t)kl > uio->uio_resid)
1308 1.1.2.2 pgoyette kl = uio->uio_resid;
1309 1.1.2.2 pgoyette
1310 1.1.2.2 pgoyette error = process_read_regs(l, &r);
1311 1.1.2.2 pgoyette if (error == 0)
1312 1.1.2.2 pgoyette error = uiomove(kv, kl, uio);
1313 1.1.2.2 pgoyette if (error == 0 && uio->uio_rw == UIO_WRITE) {
1314 1.1.2.2 pgoyette if (l->l_stat != LSSTOP)
1315 1.1.2.2 pgoyette error = EBUSY;
1316 1.1.2.2 pgoyette else
1317 1.1.2.2 pgoyette error = process_write_regs(l, &r);
1318 1.1.2.2 pgoyette }
1319 1.1.2.2 pgoyette
1320 1.1.2.2 pgoyette uio->uio_offset = 0;
1321 1.1.2.2 pgoyette return error;
1322 1.1.2.2 pgoyette #else
1323 1.1.2.2 pgoyette return EINVAL;
1324 1.1.2.2 pgoyette #endif
1325 1.1.2.2 pgoyette }
1326 1.1.2.2 pgoyette
1327 1.1.2.2 pgoyette int
1328 1.1.2.2 pgoyette process_validregs(struct lwp *l)
1329 1.1.2.2 pgoyette {
1330 1.1.2.2 pgoyette
1331 1.1.2.2 pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
1332 1.1.2.2 pgoyette return (l->l_flag & LW_SYSTEM) == 0;
1333 1.1.2.2 pgoyette #else
1334 1.1.2.2 pgoyette return 0;
1335 1.1.2.2 pgoyette #endif
1336 1.1.2.2 pgoyette }
1337 1.1.2.2 pgoyette
1338 1.1.2.2 pgoyette int
1339 1.1.2.2 pgoyette process_dofpregs(struct lwp *curl /*tracer*/,
1340 1.1.2.2 pgoyette struct lwp *l /*traced*/,
1341 1.1.2.2 pgoyette struct uio *uio)
1342 1.1.2.2 pgoyette {
1343 1.1.2.2 pgoyette #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
1344 1.1.2.2 pgoyette int error;
1345 1.1.2.2 pgoyette struct fpreg r;
1346 1.1.2.2 pgoyette char *kv;
1347 1.1.2.2 pgoyette size_t kl;
1348 1.1.2.2 pgoyette
1349 1.1.2.2 pgoyette if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1350 1.1.2.2 pgoyette return EINVAL;
1351 1.1.2.2 pgoyette
1352 1.1.2.2 pgoyette kl = sizeof(r);
1353 1.1.2.2 pgoyette kv = (char *)&r;
1354 1.1.2.2 pgoyette
1355 1.1.2.2 pgoyette kv += uio->uio_offset;
1356 1.1.2.2 pgoyette kl -= uio->uio_offset;
1357 1.1.2.2 pgoyette if (kl > uio->uio_resid)
1358 1.1.2.2 pgoyette kl = uio->uio_resid;
1359 1.1.2.2 pgoyette
1360 1.1.2.2 pgoyette error = process_read_fpregs(l, &r, &kl);
1361 1.1.2.2 pgoyette if (error == 0)
1362 1.1.2.2 pgoyette error = uiomove(kv, kl, uio);
1363 1.1.2.2 pgoyette if (error == 0 && uio->uio_rw == UIO_WRITE) {
1364 1.1.2.2 pgoyette if (l->l_stat != LSSTOP)
1365 1.1.2.2 pgoyette error = EBUSY;
1366 1.1.2.2 pgoyette else
1367 1.1.2.2 pgoyette error = process_write_fpregs(l, &r, kl);
1368 1.1.2.2 pgoyette }
1369 1.1.2.2 pgoyette uio->uio_offset = 0;
1370 1.1.2.2 pgoyette return error;
1371 1.1.2.2 pgoyette #else
1372 1.1.2.2 pgoyette return EINVAL;
1373 1.1.2.2 pgoyette #endif
1374 1.1.2.2 pgoyette }
1375 1.1.2.2 pgoyette
1376 1.1.2.2 pgoyette int
1377 1.1.2.2 pgoyette process_validfpregs(struct lwp *l)
1378 1.1.2.2 pgoyette {
1379 1.1.2.2 pgoyette
1380 1.1.2.2 pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1381 1.1.2.2 pgoyette return (l->l_flag & LW_SYSTEM) == 0;
1382 1.1.2.2 pgoyette #else
1383 1.1.2.2 pgoyette return 0;
1384 1.1.2.2 pgoyette #endif
1385 1.1.2.2 pgoyette }
1386 1.1.2.2 pgoyette
1387 1.1.2.4 pgoyette int
1388 1.1.2.4 pgoyette process_dodbregs(struct lwp *curl /*tracer*/,
1389 1.1.2.4 pgoyette struct lwp *l /*traced*/,
1390 1.1.2.4 pgoyette struct uio *uio)
1391 1.1.2.4 pgoyette {
1392 1.1.2.4 pgoyette #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
1393 1.1.2.4 pgoyette int error;
1394 1.1.2.4 pgoyette struct dbreg r;
1395 1.1.2.4 pgoyette char *kv;
1396 1.1.2.4 pgoyette size_t kl;
1397 1.1.2.4 pgoyette
1398 1.1.2.4 pgoyette if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1399 1.1.2.4 pgoyette return EINVAL;
1400 1.1.2.4 pgoyette
1401 1.1.2.4 pgoyette kl = sizeof(r);
1402 1.1.2.4 pgoyette kv = (char *)&r;
1403 1.1.2.4 pgoyette
1404 1.1.2.4 pgoyette kv += uio->uio_offset;
1405 1.1.2.4 pgoyette kl -= uio->uio_offset;
1406 1.1.2.4 pgoyette if (kl > uio->uio_resid)
1407 1.1.2.4 pgoyette kl = uio->uio_resid;
1408 1.1.2.4 pgoyette
1409 1.1.2.4 pgoyette error = process_read_dbregs(l, &r, &kl);
1410 1.1.2.4 pgoyette if (error == 0)
1411 1.1.2.4 pgoyette error = uiomove(kv, kl, uio);
1412 1.1.2.4 pgoyette if (error == 0 && uio->uio_rw == UIO_WRITE) {
1413 1.1.2.4 pgoyette if (l->l_stat != LSSTOP)
1414 1.1.2.4 pgoyette error = EBUSY;
1415 1.1.2.4 pgoyette else
1416 1.1.2.4 pgoyette error = process_write_dbregs(l, &r, kl);
1417 1.1.2.4 pgoyette }
1418 1.1.2.4 pgoyette uio->uio_offset = 0;
1419 1.1.2.4 pgoyette return error;
1420 1.1.2.4 pgoyette #else
1421 1.1.2.4 pgoyette return EINVAL;
1422 1.1.2.4 pgoyette #endif
1423 1.1.2.4 pgoyette }
1424 1.1.2.4 pgoyette
1425 1.1.2.4 pgoyette int
1426 1.1.2.4 pgoyette process_validdbregs(struct lwp *l)
1427 1.1.2.4 pgoyette {
1428 1.1.2.4 pgoyette
1429 1.1.2.4 pgoyette #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
1430 1.1.2.4 pgoyette return (l->l_flag & LW_SYSTEM) == 0;
1431 1.1.2.4 pgoyette #else
1432 1.1.2.4 pgoyette return 0;
1433 1.1.2.4 pgoyette #endif
1434 1.1.2.4 pgoyette }
1435 1.1.2.4 pgoyette
1436 1.1.2.2 pgoyette static int
1437 1.1.2.2 pgoyette process_auxv_offset(struct proc *p, struct uio *uio)
1438 1.1.2.2 pgoyette {
1439 1.1.2.2 pgoyette struct ps_strings pss;
1440 1.1.2.2 pgoyette int error;
1441 1.1.2.2 pgoyette off_t off = (off_t)p->p_psstrp;
1442 1.1.2.2 pgoyette
1443 1.1.2.2 pgoyette if ((error = copyin_psstrings(p, &pss)) != 0)
1444 1.1.2.2 pgoyette return error;
1445 1.1.2.2 pgoyette
1446 1.1.2.2 pgoyette if (pss.ps_envstr == NULL)
1447 1.1.2.2 pgoyette return EIO;
1448 1.1.2.2 pgoyette
1449 1.1.2.2 pgoyette uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
1450 1.1.2.2 pgoyette #ifdef __MACHINE_STACK_GROWS_UP
1451 1.1.2.2 pgoyette if (uio->uio_offset < off)
1452 1.1.2.2 pgoyette return EIO;
1453 1.1.2.2 pgoyette #else
1454 1.1.2.2 pgoyette if (uio->uio_offset > off)
1455 1.1.2.2 pgoyette return EIO;
1456 1.1.2.2 pgoyette if ((uio->uio_offset + uio->uio_resid) > off)
1457 1.1.2.2 pgoyette uio->uio_resid = off - uio->uio_offset;
1458 1.1.2.2 pgoyette #endif
1459 1.1.2.2 pgoyette return 0;
1460 1.1.2.2 pgoyette }
1461 1.1.2.2 pgoyette #endif /* PTRACE */
1462 1.1.2.2 pgoyette
1463 1.1.2.2 pgoyette MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
1464 1.1.2.2 pgoyette
1465 1.1.2.2 pgoyette static int
1466 1.1.2.2 pgoyette ptrace_common_modcmd(modcmd_t cmd, void *arg)
1467 1.1.2.2 pgoyette {
1468 1.1.2.2 pgoyette int error;
1469 1.1.2.2 pgoyette
1470 1.1.2.2 pgoyette switch (cmd) {
1471 1.1.2.2 pgoyette case MODULE_CMD_INIT:
1472 1.1.2.2 pgoyette error = ptrace_init();
1473 1.1.2.2 pgoyette break;
1474 1.1.2.2 pgoyette case MODULE_CMD_FINI:
1475 1.1.2.2 pgoyette error = ptrace_fini();
1476 1.1.2.2 pgoyette break;
1477 1.1.2.2 pgoyette default:
1478 1.1.2.2 pgoyette ptrace_hooks();
1479 1.1.2.2 pgoyette error = ENOTTY;
1480 1.1.2.2 pgoyette break;
1481 1.1.2.2 pgoyette }
1482 1.1.2.2 pgoyette return error;
1483 1.1.2.2 pgoyette }
1484