2 * linux/arch/ppc/kernel/ptrace.c
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * Derived from "arch/m68k/kernel/ptrace.c"
8 * Copyright (C) 1994 by Hamish Macdonald
9 * Taken from linux/kernel/ptrace.c and modified for M680x0.
10 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
12 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13 * and Paul Mackerras (paulus@linuxcare.com.au).
15 * This file is subject to the terms and conditions of the GNU General
16 * Public License. See the file README.legal in the main directory of
17 * this archive for more details.
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
29 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
35 * Set of msr bits that gdb can change on behalf of a process.
37 #define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
45 * Get contents of register REGNO in task TASK.
47 static inline unsigned long get_reg(struct task_struct
*task
, int regno
)
49 if (regno
< sizeof(struct pt_regs
) / sizeof(unsigned long))
50 return ((unsigned long *)task
->thread
.regs
)[regno
];
55 * Write contents of register REGNO in task TASK.
57 static inline int put_reg(struct task_struct
*task
, int regno
,
62 data
= (data
& MSR_DEBUGCHANGE
)
63 | (task
->thread
.regs
->msr
& ~MSR_DEBUGCHANGE
);
64 ((unsigned long *)task
->thread
.regs
)[regno
] = data
;
71 set_single_step(struct task_struct
*task
)
73 struct pt_regs
*regs
= task
->thread
.regs
;
78 clear_single_step(struct task_struct
*task
)
80 struct pt_regs
*regs
= task
->thread
.regs
;
84 int sys_ptrace(long request
, long pid
, long addr
, long data
)
86 struct task_struct
*child
;
90 if (request
== PTRACE_TRACEME
) {
91 /* are we already being traced? */
92 if (current
->ptrace
& PT_PTRACED
)
94 /* set the ptrace bit in the process flags. */
95 current
->ptrace
|= PT_PTRACED
;
100 read_lock(&tasklist_lock
);
101 child
= find_task_by_pid(pid
);
103 get_task_struct(child
);
104 read_unlock(&tasklist_lock
);
109 if (pid
== 1) /* you may not mess with init */
112 if (request
== PTRACE_ATTACH
) {
113 if (child
== current
)
115 if ((!child
->dumpable
||
116 (current
->uid
!= child
->euid
) ||
117 (current
->uid
!= child
->suid
) ||
118 (current
->uid
!= child
->uid
) ||
119 (current
->gid
!= child
->egid
) ||
120 (current
->gid
!= child
->sgid
) ||
121 (!cap_issubset(child
->cap_permitted
, current
->cap_permitted
)) ||
122 (current
->gid
!= child
->gid
))
123 && !capable(CAP_SYS_PTRACE
))
125 /* the same process cannot be attached many times */
126 if (child
->ptrace
& PT_PTRACED
)
128 child
->ptrace
|= PT_PTRACED
;
130 write_lock_irq(&tasklist_lock
);
131 if (child
->p_pptr
!= current
) {
133 child
->p_pptr
= current
;
136 write_unlock_irq(&tasklist_lock
);
138 send_sig(SIGSTOP
, child
, 1);
143 if (!(child
->ptrace
& PT_PTRACED
))
145 if (child
->state
!= TASK_STOPPED
) {
146 if (request
!= PTRACE_KILL
)
149 if (child
->p_pptr
!= current
)
153 /* when I and D space are separate, these will need to be fixed. */
154 case PTRACE_PEEKTEXT
: /* read word at location addr. */
155 case PTRACE_PEEKDATA
: {
159 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
161 if (copied
!= sizeof(tmp
))
163 ret
= put_user(tmp
,(unsigned long *) data
);
167 /* read the word at location addr in the USER area. */
168 /* XXX this will need fixing for 64-bit */
169 case PTRACE_PEEKUSR
: {
170 unsigned long index
, tmp
;
173 /* convert to index and check */
174 index
= (unsigned long) addr
>> 2;
175 if ((addr
& 3) || index
> PT_FPSCR
)
178 if (index
< PT_FPR0
) {
179 tmp
= get_reg(child
, (int) index
);
181 if (child
->thread
.regs
->msr
& MSR_FP
)
183 tmp
= ((unsigned long *)child
->thread
.fpr
)[index
- PT_FPR0
];
185 ret
= put_user(tmp
,(unsigned long *) data
);
189 /* If I and D space are separate, this will have to be fixed. */
190 case PTRACE_POKETEXT
: /* write the word at location addr. */
191 case PTRACE_POKEDATA
:
193 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
198 /* write the word at location addr in the USER area */
199 /* XXX this will need fixing for 64-bit */
200 case PTRACE_POKEUSR
: {
204 /* convert to index and check */
205 index
= (unsigned long) addr
>> 2;
206 if ((addr
& 3) || index
> PT_FPSCR
)
209 if (index
== PT_ORIG_R3
)
211 if (index
< PT_FPR0
) {
212 ret
= put_reg(child
, index
, data
);
214 if (child
->thread
.regs
->msr
& MSR_FP
)
216 ((unsigned long *)child
->thread
.fpr
)[index
- PT_FPR0
] = data
;
222 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
223 case PTRACE_CONT
: { /* restart after signal. */
225 if ((unsigned long) data
> _NSIG
)
227 if (request
== PTRACE_SYSCALL
)
228 child
->ptrace
|= PT_TRACESYS
;
230 child
->ptrace
&= ~PT_TRACESYS
;
231 child
->exit_code
= data
;
232 /* make sure the single step bit is not set. */
233 clear_single_step(child
);
234 wake_up_process(child
);
240 * make the child exit. Best I can do is send it a sigkill.
241 * perhaps it should be put in the status that it wants to
246 if (child
->state
== TASK_ZOMBIE
) /* already dead */
248 child
->exit_code
= SIGKILL
;
249 /* make sure the single step bit is not set. */
250 clear_single_step(child
);
251 wake_up_process(child
);
255 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
257 if ((unsigned long) data
> _NSIG
)
259 child
->ptrace
&= ~PT_TRACESYS
;
260 set_single_step(child
);
261 child
->exit_code
= data
;
262 /* give it a chance to run. */
263 wake_up_process(child
);
268 case PTRACE_DETACH
: { /* detach a process that was attached. */
270 if ((unsigned long) data
> _NSIG
)
272 child
->ptrace
&= ~(PT_PTRACED
|PT_TRACESYS
);
273 child
->exit_code
= data
;
274 write_lock_irq(&tasklist_lock
);
276 child
->p_pptr
= child
->p_opptr
;
278 write_unlock_irq(&tasklist_lock
);
279 /* make sure the single step bit is not set. */
280 clear_single_step(child
);
281 wake_up_process(child
);
291 free_task_struct(child
);
297 void syscall_trace(void)
299 if ((current
->ptrace
& (PT_PTRACED
|PT_TRACESYS
))
300 != (PT_PTRACED
|PT_TRACESYS
))
302 current
->exit_code
= SIGTRAP
;
303 current
->state
= TASK_STOPPED
;
304 notify_parent(current
, SIGCHLD
);
307 * this isn't the same as continuing with a signal, but it will do
308 * for normal use. strace only continues with a signal if the
309 * stopping signal is not SIGTRAP. -brl
311 if (current
->exit_code
) {
312 send_sig(current
->exit_code
, current
, 1);
313 current
->exit_code
= 0;