2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
63 #include <machine/reg.h>
64 #include <machine/psl.h>
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/errno.h>
70 #include <sys/ptrace.h>
73 #include <sys/sysctl.h>
76 #include <sys/mount.h>
78 #include <kern/task.h>
79 #include <kern/thread.h>
80 #include <mach/machine/thread_status.h>
82 /* Macros to clear/set/test flags. */
83 #define SET(t, f) (t) |= (f)
84 #define CLR(t, f) (t) &= ~(f)
85 #define ISSET(t, f) ((t) & (f))
87 void psignal_lock
__P((struct proc
*, int, int));
88 int isinferior
__P((struct proc
*, struct proc
*));
91 * sys-trace system call.
101 ptrace(p
, uap
, retval
)
103 struct ptrace_args
*uap
;
106 struct proc
*t
= current_proc(); /* target process */
107 vm_offset_t start_addr
, end_addr
,
117 struct ppc_thread_state statep
;
119 struct i386_saved_state statep
;
121 #error architecture not supported
123 unsigned long state_count
;
127 if (uap
->req
== PT_DENY_ATTACH
) {
128 if (ISSET(p
->p_flag
, P_TRACED
)) {
129 exit1(p
, W_EXITCODE(ENOTSUP
, 0), retval
);
130 /* drop funnel before we return */
131 thread_funnel_set(kernel_flock
, FALSE
);
132 thread_exception_return();
135 SET(p
->p_flag
, P_NOATTACH
);
140 if (uap
->req
== PT_FORCEQUOTA
) {
142 SET(t
->p_flag
, P_FORCEQUOTA
);
149 * Intercept and deal with "please trace me" request.
151 if (uap
->req
== PT_TRACE_ME
) {
152 SET(p
->p_flag
, P_TRACED
);
153 /* Non-attached case, our tracer is our parent. */
154 t
->p_oppid
= t
->p_pptr
->p_pid
;
157 if (uap
->req
== PT_SIGEXC
) {
158 if (ISSET(p
->p_flag
, P_TRACED
)) {
159 SET(p
->p_flag
, P_SIGEXC
);
166 * Locate victim, and make sure it is traceable.
168 if ((t
= pfind(uap
->pid
)) == NULL
)
172 /* We do not want ptrace to do anything with kernel, init
179 if (uap
->req
== PT_ATTACHEXC
) {
180 uap
->req
= PT_ATTACH
;
183 if (uap
->req
== PT_ATTACH
) {
186 * You can't attach to a process if:
187 * (1) it's the process that's doing the attaching,
189 if (t
->p_pid
== p
->p_pid
)
193 * (2) it's already being traced, or
195 if (ISSET(t
->p_flag
, P_TRACED
))
199 * (3) it's not owned by you, or is set-id on exec
200 * (unless you're root).
202 if ((t
->p_cred
->p_ruid
!= p
->p_cred
->p_ruid
||
203 ISSET(t
->p_flag
, P_SUGID
)) &&
204 (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
207 if ((p
->p_flag
& P_TRACED
) && isinferior(p
, t
))
210 if (ISSET(t
->p_flag
, P_NOATTACH
)) {
214 SET(t
->p_flag
, P_TRACED
);
216 SET(t
->p_flag
, P_SIGEXC
);
218 t
->p_oppid
= t
->p_pptr
->p_pid
;
222 if (get_task_userstop(task
) == 0 ) {
226 t
->p_xstat
= SIGSTOP
;
233 * You can't do what you want to the process if:
234 * (1) It's not being traced at all,
236 if (!ISSET(t
->p_flag
, P_TRACED
))
240 * (2) it's not being traced by _you_, or
246 * (3) it's not currently stopped.
248 if (t
->p_stat
!= SSTOP
)
252 * Mach version of ptrace executes request directly here,
253 * thus simplifying the interaction of ptrace and signals.
258 if (t
->p_oppid
!= t
->p_pptr
->p_pid
) {
261 pp
= pfind(t
->p_oppid
);
262 proc_reparent(t
, pp
? pp
: initproc
);
266 CLR(t
->p_flag
, P_TRACED
);
267 CLR(t
->p_flag
, P_SIGEXC
);
272 * Tell child process to kill itself after it
273 * is resumed by adding NSIG to p_cursig. [see issig]
275 psignal_lock(t
, SIGKILL
, 0);
278 case PT_STEP
: /* single step the child */
279 case PT_CONTINUE
: /* continue the child */
280 th_act
= (thread_act_t
)get_firstthread(task
);
281 if (th_act
== THR_ACT_NULL
)
283 ut
= (uthread_t
)get_bsdthread_info(th_act
);
286 state_count
= i386_NEW_THREAD_STATE_COUNT
;
287 if (thread_getstatus(th_act
, i386_NEW_THREAD_STATE
, &statep
, &state_count
) != KERN_SUCCESS
) {
291 state_count
= PPC_THREAD_STATE_COUNT
;
292 if (thread_getstatus(th_act
, PPC_THREAD_STATE
, &statep
, &state_count
) != KERN_SUCCESS
) {
296 #error architecture not supported
298 if ((int)uap
->addr
!= 1) {
300 locr0
[PC
] = (int)uap
->addr
;
302 #define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
303 if (!ALIGNED((int)uap
->addr
, sizeof(int)))
306 statep
.srr0
= (int)uap
->addr
;
307 state_count
= PPC_THREAD_STATE_COUNT
;
308 if (thread_setstatus(th_act
, PPC_THREAD_STATE
, &statep
, &state_count
) != KERN_SUCCESS
) {
313 #error architecture not implemented!
315 } /* (int)uap->addr != 1 */
317 if ((unsigned)uap
->data
< 0 || (unsigned)uap
->data
>= NSIG
)
320 if (uap
->data
!= 0) {
321 psignal_lock(t
, uap
->data
, 0);
324 state_count
= PPC_THREAD_STATE_COUNT
;
325 if (thread_getstatus(th_act
, PPC_THREAD_STATE
, &statep
, &state_count
) != KERN_SUCCESS
) {
330 #define MSR_SE_BIT 21
332 if (uap
->req
== PT_STEP
) {
336 statep
.srr1
|= MASK(MSR_SE
);
338 #error architecture not implemented!
340 } /* uap->req == PT_STEP */
341 else { /* PT_CONTINUE - clear trace bit if set */
345 statep
.srr1
&= ~MASK(MSR_SE
);
349 state_count
= PPC_THREAD_STATE_COUNT
;
350 if (thread_setstatus(th_act
, PPC_THREAD_STATE
, &statep
, &state_count
) != KERN_SUCCESS
) {
355 t
->p_xstat
= uap
->data
;
358 wakeup((caddr_t
)&(t
->sigwait
));
364 thread_act_t target_act
;
366 if ((unsigned)uap
->data
>= NSIG
)
368 th_act
= (thread_act_t
)port_name_to_act((void *)uap
->addr
);
369 if (th_act
== THR_ACT_NULL
)
371 ut
= (uthread_t
)get_bsdthread_info(th_act
);
373 ut
->uu_siglist
|= sigmask(uap
->data
);
374 t
->p_xstat
= uap
->data
;
376 act_deallocate(th_act
);