149589Sbostic /*- 249589Sbostic * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. 349589Sbostic * All rights reserved. 423385Smckusick * 549589Sbostic * %sccs.include.proprietary.c% 649589Sbostic * 7*56517Sbostic * @(#)sys_process.c 7.33 (Berkeley) 10/11/92 823385Smckusick */ 97426Sroot 1030376Skarels #define IPCREG 11*56517Sbostic #include <sys/param.h> 12*56517Sbostic #include <sys/proc.h> 13*56517Sbostic #include <sys/vnode.h> 14*56517Sbostic #include <sys/buf.h> 15*56517Sbostic #include <sys/ptrace.h> 167426Sroot 17*56517Sbostic #include <machine/reg.h> 18*56517Sbostic #include <machine/psl.h> 19*56517Sbostic #include <vm/vm.h> 20*56517Sbostic #include <vm/vm_page.h> 2137520Smckusick 22*56517Sbostic #include <sys/user.h> 2348438Skarels 247501Sroot /* 257501Sroot * Priority for tracing 267501Sroot */ 277501Sroot #define IPCPRI PZERO 287501Sroot 297501Sroot /* 307501Sroot * Tracing variables. 317501Sroot * Used to pass trace command from 327501Sroot * parent to child being traced. 337501Sroot * This data base cannot be 347501Sroot * shared and is locked 357501Sroot * per user. 367501Sroot */ 377501Sroot struct { 387501Sroot int ip_lock; 397501Sroot int ip_req; 407501Sroot int *ip_addr; 417501Sroot int ip_data; 427501Sroot } ipc; 437501Sroot 447501Sroot /* 4549674Smckusick * Process debugging system call. 467501Sroot */ 4754931Storek struct ptrace_args { 4854931Storek int req; 4954931Storek int pid; 5054931Storek int *addr; 5154931Storek int data; 5254931Storek }; 5343378Smckusick ptrace(curp, uap, retval) 5443378Smckusick struct proc *curp; 5554931Storek register struct ptrace_args *uap; 5643378Smckusick int *retval; 5743378Smckusick { 5843378Smckusick register struct proc *p; 5954340Smckusick int error; 607501Sroot 6156312Shibler if (uap->req <= 0) { 6256312Shibler curp->p_flag |= STRC; 6356312Shibler return (0); 6456312Shibler } 6554340Smckusick p = pfind(uap->pid); 6656312Shibler if (p == 0) 6756312Shibler return (ESRCH); 6854340Smckusick if (uap->req == PT_ATTACH) { 6954340Smckusick /* 7054340Smckusick * Must be root if the process has used set user or 7154340Smckusick * group privileges or does not belong to the real 7254340Smckusick * user. Must not be already traced. 7354340Smckusick */ 7454340Smckusick if ((p->p_flag & SUGID || 7554340Smckusick p->p_cred->p_ruid != curp->p_cred->p_ruid) && 7654340Smckusick (error = suser(p->p_ucred, &p->p_acflag)) != 0) 7754340Smckusick return (error); 7854340Smckusick if (p->p_flag & STRC) 7954340Smckusick return (EALREADY); /* ??? */ 8054340Smckusick /* 8154340Smckusick * It would be nice if the tracing relationship was separate 8254340Smckusick * from the parent relationship but that would require 8354340Smckusick * another set of links in the proc struct or for "wait" 8454340Smckusick * to scan the entire proc table. To make life easier, 8554340Smckusick * we just re-parent the process we're trying to trace. 8654340Smckusick * The old parent is remembered so we can put things back 8754340Smckusick * on a "detach". 8854340Smckusick */ 8954340Smckusick p->p_flag |= STRC; 9054340Smckusick p->p_oppid = p->p_pptr->p_pid; 9154340Smckusick proc_reparent(p, curp); 9254340Smckusick psignal(p, SIGSTOP); 9354340Smckusick return (0); 9454340Smckusick } 9556312Shibler if (p->p_stat != SSTOP || p->p_pptr != curp || !(p->p_flag & STRC)) 9644405Skarels return (ESRCH); 977501Sroot while (ipc.ip_lock) 987501Sroot sleep((caddr_t)&ipc, IPCPRI); 997501Sroot ipc.ip_lock = p->p_pid; 1007501Sroot ipc.ip_data = uap->data; 1017501Sroot ipc.ip_addr = uap->addr; 1027501Sroot ipc.ip_req = uap->req; 1037501Sroot p->p_flag &= ~SWTED; 1047501Sroot while (ipc.ip_req > 0) { 1057501Sroot if (p->p_stat==SSTOP) 1067501Sroot setrun(p); 1077501Sroot sleep((caddr_t)&ipc, IPCPRI); 1087501Sroot } 10943378Smckusick *retval = ipc.ip_data; 1107501Sroot ipc.ip_lock = 0; 1117501Sroot wakeup((caddr_t)&ipc); 11243378Smckusick if (ipc.ip_req < 0) 11344405Skarels return (EIO); 11444405Skarels return (0); 1157501Sroot } 1167501Sroot 11749246Skarels #define PHYSOFF(p, o) ((caddr_t)(p) + (o)) 11849246Skarels 11945882Swilliam #if defined(i386) 12045882Swilliam #undef PC 12145882Swilliam #undef SP 12245882Swilliam #undef PS 12345882Swilliam #undef R0 12445882Swilliam #undef R1 1258952Sroot 12645882Swilliam #define PC tEIP 12745882Swilliam #define SP tESP 12845882Swilliam #define PS tEFLAGS 12945882Swilliam #define R0 tEDX 13045882Swilliam #define R1 tECX 13145882Swilliam #endif 13245882Swilliam 1337501Sroot /* 13449674Smckusick * Transmit a tracing request from the parent to the child process 13549674Smckusick * being debugged. This code runs in the context of the child process 13649674Smckusick * to fulfill the command requested by the parent. 1377501Sroot */ 13842928Smckusick procxmt(p) 13942928Smckusick register struct proc *p; 1407501Sroot { 14142928Smckusick register int i, *poff; 14249246Skarels extern char kstack[]; 1437501Sroot 14442928Smckusick if (ipc.ip_lock != p->p_pid) 1457501Sroot return (0); 14642928Smckusick p->p_slptime = 0; 14752374Smckusick p->p_addr->u_kproc.kp_proc.p_md.md_regs = p->p_md.md_regs; /* u.u_ar0 */ 1487501Sroot i = ipc.ip_req; 1497501Sroot ipc.ip_req = 0; 1507501Sroot switch (i) { 1517501Sroot 15226278Skarels case PT_READ_I: /* read the child's text space */ 1537501Sroot if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ)) 1547501Sroot goto error; 1557501Sroot ipc.ip_data = fuiword((caddr_t)ipc.ip_addr); 1567501Sroot break; 1577501Sroot 15826278Skarels case PT_READ_D: /* read the child's data space */ 1597501Sroot if (!useracc((caddr_t)ipc.ip_addr, 4, B_READ)) 1607501Sroot goto error; 1617501Sroot ipc.ip_data = fuword((caddr_t)ipc.ip_addr); 1627501Sroot break; 1637501Sroot 16426278Skarels case PT_READ_U: /* read the child's u. */ 16541991Smckusick #ifdef HPUXCOMPAT 16649101Skarels if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXTRACE) 16741991Smckusick i = hpuxtobsduoff(ipc.ip_addr); 16841991Smckusick else 16941991Smckusick #endif 1707501Sroot i = (int)ipc.ip_addr; 17149246Skarels if ((u_int) i > ctob(UPAGES)-sizeof(int) || (i & 1) != 0) 1727501Sroot goto error; 17349101Skarels ipc.ip_data = *(int *)PHYSOFF(p->p_addr, i); 1747501Sroot break; 1757501Sroot 17626278Skarels case PT_WRITE_I: /* write the child's text space */ 1778952Sroot if ((i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data)) < 0) { 17845735Smckusick vm_offset_t sa, ea; 17945735Smckusick int rv; 18045735Smckusick 18145735Smckusick sa = trunc_page((vm_offset_t)ipc.ip_addr); 18255270Smckusick ea = round_page((vm_offset_t)ipc.ip_addr+sizeof(int)); 18347543Skarels rv = vm_map_protect(&p->p_vmspace->vm_map, sa, ea, 18445735Smckusick VM_PROT_DEFAULT, FALSE); 18545735Smckusick if (rv == KERN_SUCCESS) { 1868952Sroot i = suiword((caddr_t)ipc.ip_addr, ipc.ip_data); 18747543Skarels (void) vm_map_protect(&p->p_vmspace->vm_map, 18847543Skarels sa, ea, VM_PROT_READ|VM_PROT_EXECUTE, 18947543Skarels FALSE); 19045735Smckusick } 1918952Sroot } 1927501Sroot if (i < 0) 1937501Sroot goto error; 1947501Sroot break; 1957501Sroot 19626278Skarels case PT_WRITE_D: /* write the child's data space */ 1977501Sroot if (suword((caddr_t)ipc.ip_addr, 0) < 0) 1987501Sroot goto error; 1997501Sroot (void) suword((caddr_t)ipc.ip_addr, ipc.ip_data); 2007501Sroot break; 2017501Sroot 20226278Skarels case PT_WRITE_U: /* write the child's u. */ 20341991Smckusick #ifdef HPUXCOMPAT 20449101Skarels if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXTRACE) 20541991Smckusick i = hpuxtobsduoff(ipc.ip_addr); 20641991Smckusick else 20741991Smckusick #endif 2087501Sroot i = (int)ipc.ip_addr; 20952178Smarc #ifdef mips 21052178Smarc poff = (int *)PHYSOFF(curproc->p_addr, i); 21152178Smarc #else 21249246Skarels poff = (int *)PHYSOFF(kstack, i); 21352178Smarc #endif 2148952Sroot for (i=0; i<NIPCREG; i++) 21552374Smckusick if (poff == &p->p_md.md_regs[ipcreg[i]]) 2167501Sroot goto ok; 21752374Smckusick if (poff == &p->p_md.md_regs[PS]) { 2188952Sroot ipc.ip_data |= PSL_USERSET; 21941991Smckusick ipc.ip_data &= ~PSL_USERCLR; 22032863Skarels #ifdef PSL_CM_CLR 22132863Skarels if (ipc.ip_data & PSL_CM) 22232863Skarels ipc.ip_data &= ~PSL_CM_CLR; 22332863Skarels #endif 2247501Sroot goto ok; 2257501Sroot } 22653873Smckusick #if defined(hp300) || defined(luna68k) 22741991Smckusick #ifdef FPCOPROC 22849246Skarels if (poff >= (int *)&((struct user *)kstack)->u_pcb.pcb_fpregs.fpf_regs && 22949246Skarels poff <= (int *)&((struct user *)kstack)->u_pcb.pcb_fpregs.fpf_fpiar) 23041991Smckusick goto ok; 23141991Smckusick #endif 23241991Smckusick #endif 2337501Sroot goto error; 2347501Sroot 2357501Sroot ok: 23642928Smckusick *poff = ipc.ip_data; 2377501Sroot break; 2387501Sroot 23926278Skarels case PT_STEP: /* single step the child */ 24026278Skarels case PT_CONTINUE: /* continue the child */ 24154340Smckusick if ((unsigned)ipc.ip_data >= NSIG) 24254340Smckusick goto error; 2437501Sroot if ((int)ipc.ip_addr != 1) 24452374Smckusick p->p_md.md_regs[PC] = (int)ipc.ip_addr; 24543895Skarels p->p_xstat = ipc.ip_data; /* see issig */ 24652178Smarc #ifdef PSL_T 24752178Smarc /* need something more machine independent here... */ 24826278Skarels if (i == PT_STEP) 24952374Smckusick p->p_md.md_regs[PS] |= PSL_T; 25052178Smarc #endif 2517501Sroot wakeup((caddr_t)&ipc); 2527501Sroot return (1); 2537501Sroot 25426278Skarels case PT_KILL: /* kill the child process */ 2557501Sroot wakeup((caddr_t)&ipc); 25645111Smckusick exit(p, (int)p->p_xstat); 2577501Sroot 25854340Smckusick case PT_DETACH: /* stop tracing the child */ 25954340Smckusick if ((unsigned)ipc.ip_data >= NSIG) 26054340Smckusick goto error; 26154340Smckusick if ((int)ipc.ip_addr != 1) 26254340Smckusick p->p_md.md_regs[PC] = (int)ipc.ip_addr; 26354340Smckusick p->p_xstat = ipc.ip_data; /* see issig */ 26454340Smckusick p->p_flag &= ~STRC; 26554340Smckusick if (p->p_oppid != p->p_pptr->p_pid) { 26654340Smckusick register struct proc *pp = pfind(p->p_oppid); 26754340Smckusick 26854340Smckusick if (pp) 26954340Smckusick proc_reparent(p, pp); 27054340Smckusick } 27154340Smckusick p->p_oppid = 0; 27254340Smckusick wakeup((caddr_t)&ipc); 27354340Smckusick return (1); 27454340Smckusick 2757501Sroot default: 2767501Sroot error: 2777501Sroot ipc.ip_req = -1; 2787501Sroot } 2797501Sroot wakeup((caddr_t)&ipc); 2807501Sroot return (0); 2817501Sroot } 282