2 /* By Ross Biro 1/23/92 */
3 /* edited by Linus Torvalds */
4 /* mangled further by Bob Manson (manson@santafe.edu) */
5 /* more mutilation by David Mosberger (davidm@azstarnet.com) */
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
10 #include <linux/smp.h>
11 #include <linux/smp_lock.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/user.h>
15 #include <linux/malloc.h>
17 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
32 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
37 #define BREAKINST 0x00000080 /* call_pal bpt */
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
45 * Processes always block with the following stack-layout:
47 * +================================+ <---- task + 2*PAGE_SIZE
48 * | PALcode saved frame (ps, pc, | ^
49 * | gp, a0, a1, a2) | |
50 * +================================+ | struct pt_regs
52 * | frame generated by SAVE_ALL | |
54 * +================================+
56 * | frame saved by do_switch_stack | | struct switch_stack
58 * +================================+
60 #define PT_REG(reg) (PAGE_SIZE*2 - sizeof(struct pt_regs) \
61 + (long)&((struct pt_regs *)0)->reg)
63 #define SW_REG(reg) (PAGE_SIZE*2 - sizeof(struct pt_regs) \
64 - sizeof(struct switch_stack) \
65 + (long)&((struct switch_stack *)0)->reg)
68 * The following table maps a register index into the stack offset at
69 * which the register is saved. Register indices are 0-31 for integer
70 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
71 * zero have no stack-slot and need to be treated specially (see
72 * get_reg/put_reg below).
75 REG_R0
= 0, REG_F0
= 32, REG_FPCR
= 63, REG_PC
= 64
78 static int regoff
[] = {
79 PT_REG( r0
), PT_REG( r1
), PT_REG( r2
), PT_REG( r3
),
80 PT_REG( r4
), PT_REG( r5
), PT_REG( r6
), PT_REG( r7
),
81 PT_REG( r8
), SW_REG( r9
), SW_REG( r10
), SW_REG( r11
),
82 SW_REG( r12
), SW_REG( r13
), SW_REG( r14
), SW_REG( r15
),
83 PT_REG( r16
), PT_REG( r17
), PT_REG( r18
), PT_REG( r19
),
84 PT_REG( r20
), PT_REG( r21
), PT_REG( r22
), PT_REG( r23
),
85 PT_REG( r24
), PT_REG( r25
), PT_REG( r26
), PT_REG( r27
),
86 PT_REG( r28
), PT_REG( gp
), -1, -1,
87 SW_REG(fp
[ 0]), SW_REG(fp
[ 1]), SW_REG(fp
[ 2]), SW_REG(fp
[ 3]),
88 SW_REG(fp
[ 4]), SW_REG(fp
[ 5]), SW_REG(fp
[ 6]), SW_REG(fp
[ 7]),
89 SW_REG(fp
[ 8]), SW_REG(fp
[ 9]), SW_REG(fp
[10]), SW_REG(fp
[11]),
90 SW_REG(fp
[12]), SW_REG(fp
[13]), SW_REG(fp
[14]), SW_REG(fp
[15]),
91 SW_REG(fp
[16]), SW_REG(fp
[17]), SW_REG(fp
[18]), SW_REG(fp
[19]),
92 SW_REG(fp
[20]), SW_REG(fp
[21]), SW_REG(fp
[22]), SW_REG(fp
[23]),
93 SW_REG(fp
[24]), SW_REG(fp
[25]), SW_REG(fp
[26]), SW_REG(fp
[27]),
94 SW_REG(fp
[28]), SW_REG(fp
[29]), SW_REG(fp
[30]), SW_REG(fp
[31]),
101 * Get address of register REGNO in task TASK.
104 get_reg_addr(struct task_struct
* task
, unsigned long regno
)
109 addr
= &task
->thread
.usp
;
110 } else if (regno
== 31 || regno
> 64) {
114 addr
= (long *)((long)task
+ regoff
[regno
]);
120 * Get contents of register REGNO in task TASK.
123 get_reg(struct task_struct
* task
, unsigned long regno
)
125 return *get_reg_addr(task
, regno
);
129 * Write contents of register REGNO in task TASK.
132 put_reg(struct task_struct
*task
, unsigned long regno
, long data
)
134 *get_reg_addr(task
, regno
) = data
;
139 read_int(struct task_struct
*task
, unsigned long addr
, int * data
)
141 int copied
= access_process_vm(task
, addr
, data
, sizeof(int), 0);
142 return (copied
== sizeof(int)) ? 0 : -EIO
;
146 write_int(struct task_struct
*task
, unsigned long addr
, int data
)
148 int copied
= access_process_vm(task
, addr
, &data
, sizeof(int), 1);
149 return (copied
== sizeof(int)) ? 0 : -EIO
;
156 ptrace_set_bpt(struct task_struct
* child
)
158 int displ
, i
, res
, reg_b
, nsaved
= 0;
162 pc
= get_reg(child
, REG_PC
);
163 res
= read_int(child
, pc
, &insn
);
167 op_code
= insn
>> 26;
168 if (op_code
>= 0x30) {
170 * It's a branch: instead of trying to figure out
171 * whether the branch will be taken or not, we'll put
172 * a breakpoint at either location. This is simpler,
173 * more reliable, and probably not a whole lot slower
174 * than the alternative approach of emulating the
175 * branch (emulation can be tricky for fp branches).
177 displ
= ((s32
)(insn
<< 11)) >> 9;
178 child
->thread
.bpt_addr
[nsaved
++] = pc
+ 4;
179 if (displ
) /* guard against unoptimized code */
180 child
->thread
.bpt_addr
[nsaved
++] = pc
+ 4 + displ
;
181 DBG(DBG_BPT
, ("execing branch\n"));
182 } else if (op_code
== 0x1a) {
183 reg_b
= (insn
>> 16) & 0x1f;
184 child
->thread
.bpt_addr
[nsaved
++] = get_reg(child
, reg_b
);
185 DBG(DBG_BPT
, ("execing jump\n"));
187 child
->thread
.bpt_addr
[nsaved
++] = pc
+ 4;
188 DBG(DBG_BPT
, ("execing normal insn\n"));
191 /* install breakpoints: */
192 for (i
= 0; i
< nsaved
; ++i
) {
193 res
= read_int(child
, child
->thread
.bpt_addr
[i
], &insn
);
196 child
->thread
.bpt_insn
[i
] = insn
;
197 DBG(DBG_BPT
, (" -> next_pc=%lx\n", child
->thread
.bpt_addr
[i
]));
198 res
= write_int(child
, child
->thread
.bpt_addr
[i
], BREAKINST
);
202 child
->thread
.bpt_nsaved
= nsaved
;
207 * Ensure no single-step breakpoint is pending. Returns non-zero
208 * value if child was being single-stepped.
211 ptrace_cancel_bpt(struct task_struct
* child
)
213 int i
, nsaved
= child
->thread
.bpt_nsaved
;
215 child
->thread
.bpt_nsaved
= 0;
218 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved
);
222 for (i
= 0; i
< nsaved
; ++i
) {
223 write_int(child
, child
->thread
.bpt_addr
[i
],
224 child
->thread
.bpt_insn
[i
]);
226 return (nsaved
!= 0);
230 sys_ptrace(long request
, long pid
, long addr
, long data
,
231 int a4
, int a5
, struct pt_regs regs
)
233 struct task_struct
*child
;
237 DBG(DBG_MEM
, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
238 request
, pid
, addr
, data
));
240 if (request
== PTRACE_TRACEME
) {
241 /* are we already being traced? */
242 if (current
->flags
& PF_PTRACED
)
244 /* set the ptrace bit in the process flags. */
245 current
->flags
|= PF_PTRACED
;
249 if (pid
== 1) /* you may not mess with init */
252 if (!(child
= find_task_by_pid(pid
)))
254 if (request
== PTRACE_ATTACH
) {
256 if (child
== current
)
258 if ((!child
->dumpable
||
259 (current
->uid
!= child
->euid
) ||
260 (current
->uid
!= child
->suid
) ||
261 (current
->uid
!= child
->uid
) ||
262 (current
->gid
!= child
->egid
) ||
263 (current
->gid
!= child
->sgid
) ||
264 (current
->gid
!= child
->gid
) ||
265 (!cap_issubset(child
->cap_permitted
, current
->cap_permitted
)))
266 && !capable(CAP_SYS_PTRACE
))
268 /* the same process cannot be attached many times */
269 if (child
->flags
& PF_PTRACED
)
271 child
->flags
|= PF_PTRACED
;
272 if (child
->p_pptr
!= current
) {
274 child
->p_pptr
= current
;
277 send_sig(SIGSTOP
, child
, 1);
282 if (!(child
->flags
& PF_PTRACED
)) {
283 DBG(DBG_MEM
, ("child not traced\n"));
286 if (child
->state
!= TASK_STOPPED
) {
287 DBG(DBG_MEM
, ("child process not stopped\n"));
288 if (request
!= PTRACE_KILL
)
291 if (child
->p_pptr
!= current
) {
292 DBG(DBG_MEM
, ("child not parent of this process\n"));
297 /* When I and D space are separate, these will need to be fixed. */
298 case PTRACE_PEEKTEXT
: /* read word at location addr. */
299 case PTRACE_PEEKDATA
: {
301 int copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
303 if (copied
!= sizeof(tmp
))
306 regs
.r0
= 0; /* special return: no errors */
311 /* Read register number ADDR. */
313 regs
.r0
= 0; /* special return: no errors */
314 ret
= get_reg(child
, addr
);
315 DBG(DBG_MEM
, ("peek $%ld->%#lx\n", addr
, ret
));
318 /* When I and D space are separate, this will have to be fixed. */
319 case PTRACE_POKETEXT
: /* write the word at location addr. */
320 case PTRACE_POKEDATA
: {
321 unsigned long tmp
= data
;
322 int copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 1);
323 ret
= (copied
== sizeof(tmp
)) ? 0 : -EIO
;
327 case PTRACE_POKEUSR
: /* write the specified register */
328 DBG(DBG_MEM
, ("poke $%ld<-%#lx\n", addr
, data
));
329 ret
= put_reg(child
, addr
, data
);
332 case PTRACE_SYSCALL
: /* continue and stop at next
333 (return from) syscall */
334 case PTRACE_CONT
: /* restart after signal. */
336 if ((unsigned long) data
> _NSIG
)
338 if (request
== PTRACE_SYSCALL
)
339 child
->flags
|= PF_TRACESYS
;
341 child
->flags
&= ~PF_TRACESYS
;
342 child
->exit_code
= data
;
343 wake_up_process(child
);
344 /* make sure single-step breakpoint is gone. */
345 ptrace_cancel_bpt(child
);
350 * Make the child exit. Best I can do is send it a sigkill.
351 * perhaps it should be put in the status that it wants to
355 if (child
->state
!= TASK_ZOMBIE
) {
356 wake_up_process(child
);
357 child
->exit_code
= SIGKILL
;
359 /* make sure single-step breakpoint is gone. */
360 ptrace_cancel_bpt(child
);
364 case PTRACE_SINGLESTEP
: /* execute single instruction. */
366 if ((unsigned long) data
> _NSIG
)
368 child
->thread
.bpt_nsaved
= -1; /* mark single-stepping */
369 child
->flags
&= ~PF_TRACESYS
;
370 wake_up_process(child
);
371 child
->exit_code
= data
;
372 /* give it a chance to run. */
376 case PTRACE_DETACH
: /* detach a process that was attached. */
378 if ((unsigned long) data
> _NSIG
)
380 child
->flags
&= ~(PF_PTRACED
|PF_TRACESYS
);
381 wake_up_process(child
);
382 child
->exit_code
= data
;
384 child
->p_pptr
= child
->p_opptr
;
386 /* make sure single-step breakpoint is gone. */
387 ptrace_cancel_bpt(child
);
403 if ((current
->flags
& (PF_PTRACED
|PF_TRACESYS
))
404 != (PF_PTRACED
|PF_TRACESYS
))
406 current
->exit_code
= SIGTRAP
;
407 current
->state
= TASK_STOPPED
;
408 notify_parent(current
, SIGCHLD
);
411 * This isn't the same as continuing with a signal, but it will do
412 * for normal use. strace only continues with a signal if the
413 * stopping signal is not SIGTRAP. -brl
415 if (current
->exit_code
) {
416 send_sig(current
->exit_code
, current
, 1);
417 current
->exit_code
= 0;