1 /* $NetBSD: linux_ptrace.c,v 1.13 2008/04/28 20:23:42 martin Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.13 2008/04/28 20:23:42 martin Exp $");
36 #include <sys/param.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
40 #include <sys/ptrace.h>
41 #include <sys/systm.h>
42 #include <sys/syscallargs.h>
43 #include <uvm/uvm_extern.h>
45 #include <machine/reg.h>
47 #include <compat/linux/common/linux_types.h>
48 #include <compat/linux/common/linux_ptrace.h>
49 #include <compat/linux/common/linux_signal.h>
51 #include <compat/linux/common/linux_util.h>
52 #include <compat/linux/common/linux_machdep.h>
53 #include <compat/linux/common/linux_emuldata.h>
54 #include <compat/linux/common/linux_exec.h> /* for emul_linux */
56 #include <compat/linux/linux_syscallargs.h>
58 #include <lib/libkern/libkern.h> /* for offsetof() */
61 * On ARMv2, uregs contains R0--R15, orig_R0.
62 * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
63 * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
64 * just produce the ARMv3 version.
71 #define LINUX_REG_R0 0
72 #define LINUX_REG_R1 1
73 #define LINUX_REG_R2 2
74 #define LINUX_REG_R3 3
75 #define LINUX_REG_R4 4
76 #define LINUX_REG_R5 5
77 #define LINUX_REG_R6 6
78 #define LINUX_REG_R7 7
79 #define LINUX_REG_R8 8
80 #define LINUX_REG_R9 9
81 #define LINUX_REG_R10 10
82 #define LINUX_REG_FP 11
83 #define LINUX_REG_IP 12
84 #define LINUX_REG_SP 13
85 #define LINUX_REG_LR 14
86 #define LINUX_REG_PC 15
87 #define LINUX_REG_CPSR 16
88 #define LINUX_REG_ORIG_R0 17
90 int linux_ptrace_disabled
= 1; /* bitrotted */
93 linux_sys_ptrace_arch(struct lwp
*l
, const struct linux_sys_ptrace_args
*uap
, register_t
*retval
)
96 syscallarg(int) request;
101 struct proc
*p
= l
->l_proc
;
103 struct proc
*t
; /* target process */
105 struct reg
*regs
= NULL
;
106 struct fpreg
*fpregs
= NULL
;
107 struct linux_reg
*linux_regs
= NULL
;
108 struct linux_fpreg
*linux_fpregs
= NULL
;
110 if (linux_ptrace_disabled
)
113 request
= SCARG(uap
, request
);
115 if ((request
!= LINUX_PTRACE_GETREGS
) &&
116 (request
!= LINUX_PTRACE_SETREGS
))
119 /* Find the process we're supposed to be operating on. */
120 if ((t
= pfind(SCARG(uap
, pid
))) == NULL
)
124 * You can't do what you want to the process if:
125 * (1) It's not being traced at all,
127 if (!ISSET(t
->p_slflag
, PSL_TRACED
))
131 * (2) it's being traced by procfs (which has
132 * different signal delivery semantics),
134 if (ISSET(t
->p_slflag
, PSL_FSTRACE
))
138 * (3) it's not being traced by _you_, or
144 * (4) it's not currently stopped.
146 if (t
->p_stat
!= SSTOP
|| !t
->p_waited
)
150 * The entire ptrace interface needs work to be useful to
151 * a process with multiple LWPs. For the moment, we'll
152 * just kluge this and fail on others.
158 lt
= LIST_FIRST(&t
->p_lwps
);
163 case LINUX_PTRACE_GETREGS
:
164 regs
= malloc(sizeof(struct reg
), M_TEMP
, M_WAITOK
);
165 linux_regs
= malloc(sizeof(struct linux_reg
),
168 error
= process_read_regs(lt
, regs
);
172 memcpy(linux_regs
->uregs
, regs
->r
, 13 * sizeof(register_t
));
173 linux_regs
->uregs
[LINUX_REG_SP
] = regs
->r_sp
;
174 linux_regs
->uregs
[LINUX_REG_LR
] = regs
->r_lr
;
175 linux_regs
->uregs
[LINUX_REG_PC
] = regs
->r_pc
;
176 linux_regs
->uregs
[LINUX_REG_CPSR
] = regs
->r_cpsr
;
177 linux_regs
->uregs
[LINUX_REG_ORIG_R0
] = regs
->r
[0];
179 error
= copyout(linux_regs
, (void *)SCARG(uap
, data
),
180 sizeof(struct linux_reg
));
183 case LINUX_PTRACE_SETREGS
:
184 regs
= malloc(sizeof(struct reg
), M_TEMP
, M_WAITOK
);
185 linux_regs
= malloc(sizeof(struct linux_reg
),
188 error
= copyin((void *)SCARG(uap
, data
), linux_regs
,
189 sizeof(struct linux_reg
));
193 memcpy(regs
->r
, linux_regs
->uregs
, 13 * sizeof(register_t
));
194 regs
->r_sp
= linux_regs
->uregs
[LINUX_REG_SP
];
195 regs
->r_lr
= linux_regs
->uregs
[LINUX_REG_LR
];
196 regs
->r_pc
= linux_regs
->uregs
[LINUX_REG_PC
];
197 regs
->r_cpsr
= linux_regs
->uregs
[LINUX_REG_CPSR
];
199 error
= process_write_regs(lt
, regs
);
213 free(fpregs
, M_TEMP
);
215 free(linux_regs
, M_TEMP
);
217 free(linux_fpregs
, M_TEMP
);