Home | History | Annotate | Line # | Download | only in i386
linux_ptrace.c revision 1.3
      1  1.3  tron /*	$NetBSD: linux_ptrace.c,v 1.3 1999/12/16 15:11:19 tron Exp $	*/
      2  1.1  tron 
      3  1.1  tron /*-
      4  1.1  tron  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  tron  * All rights reserved.
      6  1.1  tron  *
      7  1.1  tron  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tron  * by Matthias Scheler.
      9  1.1  tron  *
     10  1.1  tron  * Redistribution and use in source and binary forms, with or without
     11  1.1  tron  * modification, are permitted provided that the following conditions
     12  1.1  tron  * are met:
     13  1.1  tron  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tron  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tron  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tron  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tron  *    documentation and/or other materials provided with the distribution.
     18  1.1  tron  * 3. All advertising materials mentioning features or use of this software
     19  1.1  tron  *    must display the following acknowledgement:
     20  1.1  tron  *	This product includes software developed by the NetBSD
     21  1.1  tron  *	Foundation, Inc. and its contributors.
     22  1.1  tron  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  tron  *    contributors may be used to endorse or promote products derived
     24  1.1  tron  *    from this software without specific prior written permission.
     25  1.1  tron  *
     26  1.1  tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  tron  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  tron  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  tron  */
     38  1.1  tron 
     39  1.1  tron #include <sys/types.h>
     40  1.1  tron #include <sys/param.h>
     41  1.1  tron #include <sys/mount.h>
     42  1.1  tron #include <sys/proc.h>
     43  1.1  tron #include <sys/ptrace.h>
     44  1.1  tron #include <sys/systm.h>
     45  1.1  tron #include <sys/syscallargs.h>
     46  1.1  tron 
     47  1.3  tron #include <machine/reg.h>
     48  1.3  tron 
     49  1.1  tron #include <compat/linux/common/linux_types.h>
     50  1.1  tron #include <compat/linux/common/linux_ptrace.h>
     51  1.1  tron #include <compat/linux/common/linux_signal.h>
     52  1.1  tron 
     53  1.1  tron #include <compat/linux/common/linux_util.h>
     54  1.1  tron #include <compat/linux/common/linux_machdep.h>
     55  1.1  tron 
     56  1.1  tron #include <compat/linux/linux_syscallargs.h>
     57  1.1  tron 
     58  1.2  tron #define LINUX_PTRACE_GETREGS		12
     59  1.2  tron #define LINUX_PTRACE_SETREGS		13
     60  1.2  tron #define LINUX_PTRACE_GETFPREGS		14
     61  1.2  tron #define LINUX_PTRACE_SETFPREGS		15
     62  1.2  tron 
     63  1.2  tron struct linux_reg {
     64  1.2  tron 	long ebx;
     65  1.2  tron 	long ecx;
     66  1.2  tron 	long edx;
     67  1.2  tron 	long esi;
     68  1.2  tron 	long edi;
     69  1.2  tron 	long ebp;
     70  1.2  tron 	long eax;
     71  1.2  tron 	int  xds;
     72  1.2  tron 	int  xes;
     73  1.2  tron 	long orig_eax;
     74  1.2  tron 	long eip;
     75  1.2  tron 	int  xcs;
     76  1.2  tron 	long eflags;
     77  1.2  tron 	long esp;
     78  1.2  tron 	int  xss;
     79  1.2  tron };
     80  1.2  tron 
     81  1.3  tron #define ISSET(t, f)	((t) & (f))
     82  1.3  tron 
     83  1.1  tron int
     84  1.3  tron linux_sys_ptrace_arch(p, v, retval)
     85  1.1  tron 	struct proc *p;
     86  1.1  tron 	void *v;
     87  1.1  tron 	register_t *retval;
     88  1.1  tron {
     89  1.1  tron 	struct linux_sys_ptrace_args /* {
     90  1.1  tron 		syscallarg(int) request;
     91  1.1  tron 		syscallarg(int) pid;
     92  1.1  tron 		syscallarg(int) addr;
     93  1.1  tron 		syscallarg(int) data;
     94  1.1  tron 	} */ *uap = v;
     95  1.3  tron 	int request, error;
     96  1.3  tron 	struct proc *t;				/* target process */
     97  1.3  tron 	struct reg regs;
     98  1.3  tron 	struct linux_reg linux_regs;
     99  1.3  tron 
    100  1.3  tron 	request = SCARG(uap, request);
    101  1.3  tron 
    102  1.3  tron 	if ((request != LINUX_PTRACE_GETREGS) &&
    103  1.3  tron 	    (request != LINUX_PTRACE_SETREGS) &&
    104  1.3  tron 	    (request != LINUX_PTRACE_GETFPREGS) &&
    105  1.3  tron 	    (request != LINUX_PTRACE_SETFPREGS))
    106  1.3  tron 		return EIO;
    107  1.1  tron 
    108  1.3  tron 	/* Find the process we're supposed to be operating on. */
    109  1.3  tron 	if ((t = pfind(SCARG(uap, pid))) == NULL)
    110  1.3  tron 		return ESRCH;
    111  1.3  tron 
    112  1.3  tron 	/*
    113  1.3  tron 	 * You can't do what you want to the process if:
    114  1.3  tron 	 *	(1) It's not being traced at all,
    115  1.3  tron 	 */
    116  1.3  tron 	if (!ISSET(t->p_flag, P_TRACED))
    117  1.3  tron 		return EPERM;
    118  1.3  tron 
    119  1.3  tron 	/*
    120  1.3  tron 	 *	(2) it's being traced by procfs (which has
    121  1.3  tron 	 *	    different signal delivery semantics),
    122  1.3  tron 	 */
    123  1.3  tron 	if (ISSET(t->p_flag, P_FSTRACE))
    124  1.3  tron 		return EBUSY;
    125  1.3  tron 
    126  1.3  tron 	/*
    127  1.3  tron 	 *	(3) it's not being traced by _you_, or
    128  1.3  tron 	 */
    129  1.3  tron 	if (t->p_pptr != p)
    130  1.3  tron 		return EBUSY;
    131  1.3  tron 
    132  1.3  tron 	/*
    133  1.3  tron 	 *	(4) it's not currently stopped.
    134  1.3  tron 	 */
    135  1.3  tron 	if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
    136  1.3  tron 		return EBUSY;
    137  1.3  tron 
    138  1.3  tron 	*retval = 0;
    139  1.3  tron 
    140  1.3  tron 	switch (request) {
    141  1.3  tron 	case  LINUX_PTRACE_GETREGS:
    142  1.3  tron 		error = process_read_regs(t, &regs);
    143  1.3  tron 		if (error != 0)
    144  1.3  tron 			return error;
    145  1.3  tron 
    146  1.3  tron 		linux_regs.ebx = regs.r_ebx;
    147  1.3  tron 		linux_regs.ecx = regs.r_ecx;
    148  1.3  tron 		linux_regs.edx = regs.r_edx;
    149  1.3  tron 		linux_regs.esi = regs.r_esi;
    150  1.3  tron 		linux_regs.edi = regs.r_edi;
    151  1.3  tron 		linux_regs.ebp = regs.r_ebp;
    152  1.3  tron 		linux_regs.eax = regs.r_eax;
    153  1.3  tron 		linux_regs.xds = regs.r_ds;
    154  1.3  tron 		linux_regs.xes = regs.r_es;
    155  1.3  tron 		linux_regs.orig_eax = regs.r_eax; /* XXX is this correct? */
    156  1.3  tron 		linux_regs.eip = regs.r_eip;
    157  1.3  tron 		linux_regs.xcs = regs.r_cs;
    158  1.3  tron 		linux_regs.eflags = regs.r_eflags;
    159  1.3  tron 		linux_regs.esp = regs.r_esp;
    160  1.3  tron 		linux_regs.xss = regs.r_ss;
    161  1.3  tron 
    162  1.3  tron 		return copyout(&linux_regs, (caddr_t)SCARG(uap, data),
    163  1.3  tron 		    sizeof(struct linux_reg));
    164  1.3  tron 	case  LINUX_PTRACE_SETREGS:
    165  1.3  tron 		error = copyin((caddr_t)SCARG(uap, data), &linux_regs,
    166  1.3  tron 		    sizeof(struct linux_reg));
    167  1.3  tron 		if (error != 0)
    168  1.3  tron 			return error;
    169  1.3  tron 
    170  1.3  tron 		regs.r_ebx = linux_regs.ebx;
    171  1.3  tron 		regs.r_ecx = linux_regs.ecx;
    172  1.3  tron 		regs.r_edx = linux_regs.edx;
    173  1.3  tron 		regs.r_esi = linux_regs.esi;
    174  1.3  tron 		regs.r_edi = linux_regs.edi;
    175  1.3  tron 		regs.r_ebp = linux_regs.ebp;
    176  1.3  tron 		regs.r_eax = linux_regs.eax;
    177  1.3  tron 		regs.r_ds = linux_regs.xds;
    178  1.3  tron 		regs.r_es = linux_regs.xes;
    179  1.3  tron 		regs.r_eip = linux_regs.eip;
    180  1.3  tron 		regs.r_cs = linux_regs.xcs;
    181  1.3  tron 		regs.r_eflags = linux_regs.eflags;
    182  1.3  tron 		regs.r_esp = linux_regs.esp;
    183  1.3  tron 		regs.r_ss = linux_regs.xss;
    184  1.1  tron 
    185  1.3  tron 		return process_write_regs(t, &regs);
    186  1.1  tron 	}
    187  1.1  tron 
    188  1.3  tron 	return EIO;
    189  1.1  tron }
    190