17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 58cd45542Sraf * Common Development and Distribution License (the "License"). 68cd45542Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 218cd45542Sraf 227c478bd9Sstevel@tonic-gate /* 238cd45542Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 248cd45542Sraf * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 288cd45542Sraf * ptrace(2) interface built on top of proc(4). 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 33*7257d1b4Sraf #pragma weak _ptrace = ptrace 347c478bd9Sstevel@tonic-gate 35*7257d1b4Sraf #include "lint.h" 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <memory.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate #include <fcntl.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/uio.h> 457c478bd9Sstevel@tonic-gate #include <signal.h> 467c478bd9Sstevel@tonic-gate #include <sys/siginfo.h> 477c478bd9Sstevel@tonic-gate #include <sys/fault.h> 487c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 497c478bd9Sstevel@tonic-gate #include <procfs.h> 507c478bd9Sstevel@tonic-gate #include <sys/psw.h> 517c478bd9Sstevel@tonic-gate #include <sys/user.h> 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * mtlib.h must precede thread.h 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate #include <mtlib.h> 567c478bd9Sstevel@tonic-gate #include <thread.h> 577c478bd9Sstevel@tonic-gate #include <synch.h> 587c478bd9Sstevel@tonic-gate #include <unistd.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate static mutex_t pt_lock = DEFAULTMUTEX; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define TRUE 1 637c478bd9Sstevel@tonic-gate #define FALSE 0 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * All my children... 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate typedef struct cstatus { 697c478bd9Sstevel@tonic-gate struct cstatus *next; /* linked list */ 707c478bd9Sstevel@tonic-gate pid_t pid; /* process-id */ 717c478bd9Sstevel@tonic-gate int asfd; /* /proc/<pid>/as */ 727c478bd9Sstevel@tonic-gate int ctlfd; /* /proc/<pid>/ctl */ 737c478bd9Sstevel@tonic-gate int statusfd; /* /proc/<pid>/status */ 747c478bd9Sstevel@tonic-gate int flags; /* see below */ 757c478bd9Sstevel@tonic-gate pstatus_t pstatus; /* from /proc/<pid>/status */ 767c478bd9Sstevel@tonic-gate user_t user; /* manufactured u-block */ 777c478bd9Sstevel@tonic-gate } cstatus_t; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* flags */ 807c478bd9Sstevel@tonic-gate #define CS_SETREGS 0x01 /* set registers on run */ 817c478bd9Sstevel@tonic-gate #define CS_PSARGS 0x02 /* u_psargs[] has been fetched */ 827c478bd9Sstevel@tonic-gate #define CS_SIGNAL 0x04 /* u_signal[] has been fetched */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate #define NULLCP ((cstatus_t *)0) 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static cstatus_t *childp = NULLCP; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* fake u-block offsets */ 897c478bd9Sstevel@tonic-gate #define UP ((user_t *)NULL) 907c478bd9Sstevel@tonic-gate #define U_REG ((int)(&UP->u_reg[0])) 917c478bd9Sstevel@tonic-gate #define U_AR0 ((int)(&UP->u_ar0)) 927c478bd9Sstevel@tonic-gate #define U_PSARGS ((int)(&UP->u_psargs[0])) 937c478bd9Sstevel@tonic-gate #define U_SIGNAL ((int)(&UP->u_signal[0])) 947c478bd9Sstevel@tonic-gate #define U_CODE ((int)(&UP->u_code)) 957c478bd9Sstevel@tonic-gate #define U_ADDR ((int)(&UP->u_addr)) 967c478bd9Sstevel@tonic-gate #define U_END ((int)sizeof (user_t)) 977c478bd9Sstevel@tonic-gate #define REGADDR 0xffff0000 /* arbitrary kernel address for u_ar0 */ 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* external routines defined in this module */ 1007c478bd9Sstevel@tonic-gate extern int ptrace(int, pid_t, int, int); 1017c478bd9Sstevel@tonic-gate /* static routines defined in this module */ 1027c478bd9Sstevel@tonic-gate static cstatus_t *FindProc(pid_t); 1037c478bd9Sstevel@tonic-gate static void CheckAllProcs(void); 1047c478bd9Sstevel@tonic-gate static int Dupfd(int, int); 1057c478bd9Sstevel@tonic-gate static void MakeProcName(char *, pid_t); 1067c478bd9Sstevel@tonic-gate static int OpenProc(cstatus_t *); 1077c478bd9Sstevel@tonic-gate static void CloseProc(cstatus_t *); 1087c478bd9Sstevel@tonic-gate static cstatus_t *GrabProc(pid_t); 1097c478bd9Sstevel@tonic-gate static void ReleaseProc(cstatus_t *); 1107c478bd9Sstevel@tonic-gate static int ProcUpdate(cstatus_t *); 1117c478bd9Sstevel@tonic-gate static void MakeUser(cstatus_t *); 1127c478bd9Sstevel@tonic-gate static void GetPsargs(cstatus_t *); 1137c478bd9Sstevel@tonic-gate static void GetSignal(cstatus_t *); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate #if PTRACE_DEBUG 1167c478bd9Sstevel@tonic-gate /* for debugging */ 1177c478bd9Sstevel@tonic-gate static char * 1187c478bd9Sstevel@tonic-gate map(int request) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate static char name[20]; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate switch (request) { 1237c478bd9Sstevel@tonic-gate case 0: return ("PTRACE_TRACEME"); 1247c478bd9Sstevel@tonic-gate case 1: return ("PTRACE_PEEKTEXT"); 1257c478bd9Sstevel@tonic-gate case 2: return ("PTRACE_PEEKDATA"); 1267c478bd9Sstevel@tonic-gate case 3: return ("PTRACE_PEEKUSER"); 1277c478bd9Sstevel@tonic-gate case 4: return ("PTRACE_POKETEXT"); 1287c478bd9Sstevel@tonic-gate case 5: return ("PTRACE_POKEDATA"); 1297c478bd9Sstevel@tonic-gate case 6: return ("PTRACE_POKEUSER"); 1307c478bd9Sstevel@tonic-gate case 7: return ("PTRACE_CONT"); 1317c478bd9Sstevel@tonic-gate case 8: return ("PTRACE_KILL"); 1327c478bd9Sstevel@tonic-gate case 9: return ("PTRACE_SINGLESTEP"); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate (void) sprintf(name, "%d", request); 1357c478bd9Sstevel@tonic-gate return (name); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate #endif 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate int 1407c478bd9Sstevel@tonic-gate ptrace(int request, pid_t pid, int addr, int data) 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate pstatus_t *ps; 1437c478bd9Sstevel@tonic-gate cstatus_t *cp; 1447c478bd9Sstevel@tonic-gate unsigned xaddr; 1457c478bd9Sstevel@tonic-gate struct { 1467c478bd9Sstevel@tonic-gate long cmd; 1477c478bd9Sstevel@tonic-gate union { 1487c478bd9Sstevel@tonic-gate long flags; 1497c478bd9Sstevel@tonic-gate sigset_t signals; 1507c478bd9Sstevel@tonic-gate fltset_t faults; 1517c478bd9Sstevel@tonic-gate sysset_t syscalls; 1527c478bd9Sstevel@tonic-gate siginfo_t siginfo; 1537c478bd9Sstevel@tonic-gate } arg; 1547c478bd9Sstevel@tonic-gate } ctl; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate #if PTRACE_DEBUG 1577c478bd9Sstevel@tonic-gate fprintf(stderr, " ptrace(%s, 0x%X, 0x%X, 0x%X)\n", 1588cd45542Sraf map(request), pid, addr, data); 1597c478bd9Sstevel@tonic-gate #endif 1607c478bd9Sstevel@tonic-gate 1618cd45542Sraf (void) mutex_lock(&pt_lock); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (request == 0) { /* PTRACE_TRACEME, executed by traced process */ 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Set stop-on-all-signals and nothing else. 1667c478bd9Sstevel@tonic-gate * Turn off inherit-on-fork flag (grandchildren run away). 1677c478bd9Sstevel@tonic-gate * Set ptrace-compatible flag. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate char procname[64]; /* /proc/<pid>/ctl */ 1707c478bd9Sstevel@tonic-gate int fd; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate MakeProcName(procname, getpid()); 1737c478bd9Sstevel@tonic-gate (void) strcat(procname, "/ctl"); 1747c478bd9Sstevel@tonic-gate if ((fd = open(procname, O_WRONLY, 0)) < 0) 1757c478bd9Sstevel@tonic-gate exit(255); 1767c478bd9Sstevel@tonic-gate ctl.cmd = PCSTRACE; 1777c478bd9Sstevel@tonic-gate prfillset(&ctl.arg.signals); 1787c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sigset_t)) 1797c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (sigset_t)) 1807c478bd9Sstevel@tonic-gate exit(255); 1817c478bd9Sstevel@tonic-gate ctl.cmd = PCSFAULT; 1827c478bd9Sstevel@tonic-gate premptyset(&ctl.arg.faults); 1837c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (fltset_t)) 1847c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (fltset_t)) 1857c478bd9Sstevel@tonic-gate exit(255); 1867c478bd9Sstevel@tonic-gate ctl.cmd = PCSENTRY; 1877c478bd9Sstevel@tonic-gate premptyset(&ctl.arg.syscalls); 1887c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t)) 1897c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (sysset_t)) 1907c478bd9Sstevel@tonic-gate exit(255); 1917c478bd9Sstevel@tonic-gate ctl.cmd = PCSEXIT; 1927c478bd9Sstevel@tonic-gate premptyset(&ctl.arg.syscalls); 1937c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t)) 1947c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (sysset_t)) 1957c478bd9Sstevel@tonic-gate exit(255); 1967c478bd9Sstevel@tonic-gate ctl.cmd = PCUNSET; 1977c478bd9Sstevel@tonic-gate ctl.arg.flags = PR_FORK; 1987c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long)) 1997c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (long)) 2007c478bd9Sstevel@tonic-gate exit(255); 2017c478bd9Sstevel@tonic-gate ctl.cmd = PCSET; 2027c478bd9Sstevel@tonic-gate ctl.arg.flags = PR_PTRACE; 2037c478bd9Sstevel@tonic-gate if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long)) 2047c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (long)) 2057c478bd9Sstevel@tonic-gate exit(255); 2067c478bd9Sstevel@tonic-gate if (close(fd) != 0) 2077c478bd9Sstevel@tonic-gate exit(255); 2087c478bd9Sstevel@tonic-gate 2098cd45542Sraf (void) mutex_unlock(&pt_lock); 2107c478bd9Sstevel@tonic-gate return (0); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate again: 2147c478bd9Sstevel@tonic-gate errno = 0; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* find the cstatus structure corresponding to pid */ 2177c478bd9Sstevel@tonic-gate if ((cp = GrabProc(pid)) == NULLCP) 2187c478bd9Sstevel@tonic-gate goto esrch; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate ps = &cp->pstatus; 2217c478bd9Sstevel@tonic-gate if (!(ps->pr_flags & PR_ISTOP)) { 2227c478bd9Sstevel@tonic-gate if (ProcUpdate(cp) != 0) { 2237c478bd9Sstevel@tonic-gate ReleaseProc(cp); 2247c478bd9Sstevel@tonic-gate goto esrch; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate if (!(ps->pr_flags & PR_ISTOP)) 2277c478bd9Sstevel@tonic-gate goto esrch; 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * Process the request. 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate errno = 0; 2347c478bd9Sstevel@tonic-gate switch (request) { 2357c478bd9Sstevel@tonic-gate case 1: /* PTRACE_PEEKTEXT */ 2367c478bd9Sstevel@tonic-gate case 2: /* PTRACE_PEEKDATA */ 2377c478bd9Sstevel@tonic-gate if (addr & 03) 2387c478bd9Sstevel@tonic-gate goto eio; 2397c478bd9Sstevel@tonic-gate if (pread(cp->asfd, (char *)&data, sizeof (data), (off_t)addr) 2407c478bd9Sstevel@tonic-gate == sizeof (data)) { 2418cd45542Sraf (void) mutex_unlock(&pt_lock); 2427c478bd9Sstevel@tonic-gate return (data); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate goto eio; 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate case 3: /* PTRACE_PEEKUSER */ 2477c478bd9Sstevel@tonic-gate if (addr & 03) 2487c478bd9Sstevel@tonic-gate goto eio; 2497c478bd9Sstevel@tonic-gate xaddr = addr; 2507c478bd9Sstevel@tonic-gate if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t)) 2517c478bd9Sstevel@tonic-gate xaddr -= REGADDR-U_REG; 2527c478bd9Sstevel@tonic-gate if (xaddr >= U_PSARGS && xaddr < U_PSARGS+sizeof (UP->u_psargs)) 2537c478bd9Sstevel@tonic-gate GetPsargs(cp); 2547c478bd9Sstevel@tonic-gate if (xaddr >= U_SIGNAL && xaddr < U_SIGNAL+sizeof (UP->u_signal)) 2557c478bd9Sstevel@tonic-gate GetSignal(cp); 2567c478bd9Sstevel@tonic-gate if ((int)xaddr >= 0 && xaddr < U_END) { 2577c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */ 2587c478bd9Sstevel@tonic-gate data = *((int *)((caddr_t)(&cp->user) + xaddr)); 2598cd45542Sraf (void) mutex_unlock(&pt_lock); 2607c478bd9Sstevel@tonic-gate return (data); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate goto eio; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate case 4: /* PTRACE_POKETEXT */ 2657c478bd9Sstevel@tonic-gate case 5: /* PTRACE_POKEDATA */ 2667c478bd9Sstevel@tonic-gate if (addr & 03) 2677c478bd9Sstevel@tonic-gate goto eio; 2687c478bd9Sstevel@tonic-gate xaddr = addr; 2697c478bd9Sstevel@tonic-gate if (xaddr >= (unsigned)cp->user.u_reg[REG_SP] && 2707c478bd9Sstevel@tonic-gate xaddr < (unsigned)cp->user.u_reg[REG_SP]+16*sizeof (int)) 2717c478bd9Sstevel@tonic-gate cp->flags |= CS_SETREGS; 2727c478bd9Sstevel@tonic-gate if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr) 2737c478bd9Sstevel@tonic-gate == sizeof (data)) { 2748cd45542Sraf (void) mutex_unlock(&pt_lock); 2757c478bd9Sstevel@tonic-gate return (data); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate goto eio; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate case 6: /* PTRACE_POKEUSER */ 2807c478bd9Sstevel@tonic-gate if (addr & 03) 2817c478bd9Sstevel@tonic-gate goto eio; 2827c478bd9Sstevel@tonic-gate xaddr = addr; 2837c478bd9Sstevel@tonic-gate if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t)) 2847c478bd9Sstevel@tonic-gate xaddr -= REGADDR-U_REG; 2857c478bd9Sstevel@tonic-gate if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) { 2867c478bd9Sstevel@tonic-gate int rx = (xaddr-U_REG)/sizeof (greg_t); 2877c478bd9Sstevel@tonic-gate if (rx == REG_PS) 2887c478bd9Sstevel@tonic-gate data = (cp->user.u_reg[REG_PS] & 2897c478bd9Sstevel@tonic-gate ~PSL_USERMASK) | (data & PSL_USERMASK); 2907c478bd9Sstevel@tonic-gate else if (rx == REG_SP || rx == REG_PC || rx == REG_nPC) 2917c478bd9Sstevel@tonic-gate data &= ~03; 2927c478bd9Sstevel@tonic-gate cp->user.u_reg[rx] = data; 2937c478bd9Sstevel@tonic-gate cp->flags |= CS_SETREGS; 2948cd45542Sraf (void) mutex_unlock(&pt_lock); 2957c478bd9Sstevel@tonic-gate return (data); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate goto eio; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate case 7: /* PTRACE_CONT */ 3007c478bd9Sstevel@tonic-gate case 9: /* PTRACE_SINGLESTEP */ 3018cd45542Sraf { 3027c478bd9Sstevel@tonic-gate long runctl[3]; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if (cp->flags & CS_SETREGS) { 3057c478bd9Sstevel@tonic-gate long cmd; 3067c478bd9Sstevel@tonic-gate iovec_t iov[2]; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR]; 3097c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_PC] = cp->user.u_reg[REG_PC]; 3107c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC]; 3117c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_Y] = cp->user.u_reg[REG_Y]; 3127c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G1] = cp->user.u_reg[REG_G1]; 3137c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G2] = cp->user.u_reg[REG_G2]; 3147c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G3] = cp->user.u_reg[REG_G3]; 3157c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G4] = cp->user.u_reg[REG_G4]; 3167c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G5] = cp->user.u_reg[REG_G5]; 3177c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G6] = cp->user.u_reg[REG_G6]; 3187c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G7] = cp->user.u_reg[REG_G7]; 3197c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O0] = cp->user.u_reg[REG_O0]; 3207c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O1] = cp->user.u_reg[REG_O1]; 3217c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O2] = cp->user.u_reg[REG_O2]; 3227c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O3] = cp->user.u_reg[REG_O3]; 3237c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O4] = cp->user.u_reg[REG_O4]; 3247c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O5] = cp->user.u_reg[REG_O5]; 3257c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O6] = cp->user.u_reg[REG_O6]; 3267c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O7] = cp->user.u_reg[REG_O7]; 3277c478bd9Sstevel@tonic-gate (void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0], 3288cd45542Sraf 16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]); 3297c478bd9Sstevel@tonic-gate cmd = PCSREG; 3307c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)&cmd; 3317c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (long); 3327c478bd9Sstevel@tonic-gate iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0]; 3337c478bd9Sstevel@tonic-gate iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg); 3347c478bd9Sstevel@tonic-gate if (writev(cp->ctlfd, iov, 2) < 0) 3357c478bd9Sstevel@tonic-gate goto tryagain; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate if (addr != 1 && /* new virtual address */ 3387c478bd9Sstevel@tonic-gate (addr & ~03) != cp->user.u_reg[REG_PC]) { 3397c478bd9Sstevel@tonic-gate runctl[0] = PCSVADDR; 3407c478bd9Sstevel@tonic-gate runctl[1] = (addr & ~03); 3417c478bd9Sstevel@tonic-gate if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long)) 3427c478bd9Sstevel@tonic-gate != 2*sizeof (long)) 3437c478bd9Sstevel@tonic-gate goto tryagain; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate /* make data the current signal */ 3467c478bd9Sstevel@tonic-gate if (data != 0 && data != ps->pr_lwp.pr_cursig) { 3477c478bd9Sstevel@tonic-gate (void) memset((char *)&ctl.arg.siginfo, 0, 3487c478bd9Sstevel@tonic-gate sizeof (siginfo_t)); 3497c478bd9Sstevel@tonic-gate ctl.arg.siginfo.si_signo = data; 3507c478bd9Sstevel@tonic-gate ctl.cmd = PCSSIG; 3517c478bd9Sstevel@tonic-gate if (write(cp->ctlfd, (char *)&ctl, 3527c478bd9Sstevel@tonic-gate sizeof (long)+sizeof (siginfo_t)) 3537c478bd9Sstevel@tonic-gate != sizeof (long)+sizeof (siginfo_t)) 3547c478bd9Sstevel@tonic-gate goto tryagain; 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate if (data == 0) 3577c478bd9Sstevel@tonic-gate runctl[0] = PCCSIG; 3587c478bd9Sstevel@tonic-gate else 3597c478bd9Sstevel@tonic-gate runctl[0] = PCNULL; 3607c478bd9Sstevel@tonic-gate runctl[1] = PCRUN; 3617c478bd9Sstevel@tonic-gate runctl[2] = (request == 9)? PRSTEP : 0; 3627c478bd9Sstevel@tonic-gate if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long)) 3637c478bd9Sstevel@tonic-gate != 3*sizeof (long)) { 3647c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 3657c478bd9Sstevel@tonic-gate /* current signal must have killed it */ 3667c478bd9Sstevel@tonic-gate ReleaseProc(cp); 3678cd45542Sraf (void) mutex_unlock(&pt_lock); 3687c478bd9Sstevel@tonic-gate return (data); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate goto tryagain; 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate (void) memset((char *)ps, 0, sizeof (pstatus_t)); 3737c478bd9Sstevel@tonic-gate cp->flags = 0; 3748cd45542Sraf (void) mutex_unlock(&pt_lock); 3757c478bd9Sstevel@tonic-gate return (data); 3768cd45542Sraf } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate case 8: /* PTRACE_KILL */ 3797c478bd9Sstevel@tonic-gate /* overkill? */ 3807c478bd9Sstevel@tonic-gate (void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t)); 3817c478bd9Sstevel@tonic-gate ctl.arg.siginfo.si_signo = SIGKILL; 3827c478bd9Sstevel@tonic-gate ctl.cmd = PCSSIG; 3837c478bd9Sstevel@tonic-gate (void) write(cp->ctlfd, (char *)&ctl, 3847c478bd9Sstevel@tonic-gate sizeof (long)+sizeof (siginfo_t)); 3857c478bd9Sstevel@tonic-gate (void) kill(pid, SIGKILL); 3867c478bd9Sstevel@tonic-gate ReleaseProc(cp); 3878cd45542Sraf (void) mutex_unlock(&pt_lock); 3887c478bd9Sstevel@tonic-gate return (0); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate default: 3917c478bd9Sstevel@tonic-gate goto eio; 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate tryagain: 3957c478bd9Sstevel@tonic-gate if (errno == EAGAIN) { 3967c478bd9Sstevel@tonic-gate if (OpenProc(cp) == 0) 3977c478bd9Sstevel@tonic-gate goto again; 3987c478bd9Sstevel@tonic-gate ReleaseProc(cp); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate eio: 4017c478bd9Sstevel@tonic-gate errno = EIO; 4028cd45542Sraf (void) mutex_unlock(&pt_lock); 4037c478bd9Sstevel@tonic-gate return (-1); 4047c478bd9Sstevel@tonic-gate esrch: 4057c478bd9Sstevel@tonic-gate errno = ESRCH; 4068cd45542Sraf (void) mutex_unlock(&pt_lock); 4077c478bd9Sstevel@tonic-gate return (-1); 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate /* 4117c478bd9Sstevel@tonic-gate * Find the cstatus structure corresponding to pid. 4127c478bd9Sstevel@tonic-gate */ 4137c478bd9Sstevel@tonic-gate static cstatus_t * 4147c478bd9Sstevel@tonic-gate FindProc(pid_t pid) 4157c478bd9Sstevel@tonic-gate { 4167c478bd9Sstevel@tonic-gate cstatus_t *cp; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate for (cp = childp; cp != NULLCP; cp = cp->next) 4197c478bd9Sstevel@tonic-gate if (cp->pid == pid) 4207c478bd9Sstevel@tonic-gate break; 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate return (cp); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * Check every proc for existence, release those that are gone. 4277c478bd9Sstevel@tonic-gate * Be careful about the linked list; ReleaseProc() changes it. 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate static void 4307c478bd9Sstevel@tonic-gate CheckAllProcs() 4317c478bd9Sstevel@tonic-gate { 4327c478bd9Sstevel@tonic-gate cstatus_t *cp = childp; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate while (cp != NULLCP) { 4357c478bd9Sstevel@tonic-gate cstatus_t *next = cp->next; 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate if (ProcUpdate(cp) != 0) 4387c478bd9Sstevel@tonic-gate ReleaseProc(cp); 4397c478bd9Sstevel@tonic-gate cp = next; 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * Utility for OpenProc(). 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate static int 4477c478bd9Sstevel@tonic-gate Dupfd(int fd, int dfd) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * Make sure fd not one of 0, 1, or 2 to avoid stdio interference. 4517c478bd9Sstevel@tonic-gate * Also, if dfd is greater than 2, dup fd to be exactly dfd. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate if (dfd > 2 || (0 <= fd && fd <= 2)) { 4547c478bd9Sstevel@tonic-gate if (dfd > 2 && fd != dfd) 4557c478bd9Sstevel@tonic-gate (void) close(dfd); 4567c478bd9Sstevel@tonic-gate else 4577c478bd9Sstevel@tonic-gate dfd = 3; 4587c478bd9Sstevel@tonic-gate if (fd != dfd) { 4597c478bd9Sstevel@tonic-gate dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd); 4607c478bd9Sstevel@tonic-gate (void) close(fd); 4617c478bd9Sstevel@tonic-gate fd = dfd; 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate /* 4657c478bd9Sstevel@tonic-gate * Mark filedescriptor close-on-exec. 4667c478bd9Sstevel@tonic-gate * Should also be close-on-return-from-fork-in-child. 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate (void) fcntl(fd, F_SETFD, (intptr_t)1); 4697c478bd9Sstevel@tonic-gate return (fd); 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate /* 4737c478bd9Sstevel@tonic-gate * Construct the /proc directory name: "/proc/<pid>" 4747c478bd9Sstevel@tonic-gate * The name buffer passed by the caller must be large enough. 4757c478bd9Sstevel@tonic-gate */ 4767c478bd9Sstevel@tonic-gate static void 4777c478bd9Sstevel@tonic-gate MakeProcName(char *procname, pid_t pid) 4787c478bd9Sstevel@tonic-gate { 4797c478bd9Sstevel@tonic-gate (void) sprintf(procname, "/proc/%d", pid); 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate /* 4837c478bd9Sstevel@tonic-gate * Open/reopen the /proc/<pid> files. 4847c478bd9Sstevel@tonic-gate */ 4857c478bd9Sstevel@tonic-gate static int 4867c478bd9Sstevel@tonic-gate OpenProc(cstatus_t *cp) 4877c478bd9Sstevel@tonic-gate { 4887c478bd9Sstevel@tonic-gate char procname[64]; /* /proc/nnnnn/fname */ 4897c478bd9Sstevel@tonic-gate char *fname; 4907c478bd9Sstevel@tonic-gate int fd; 4917c478bd9Sstevel@tonic-gate int omode; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate MakeProcName(procname, cp->pid); 4947c478bd9Sstevel@tonic-gate fname = procname + strlen(procname); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * Use exclusive-open only if this is the first open. 4987c478bd9Sstevel@tonic-gate */ 4997c478bd9Sstevel@tonic-gate omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL); 5007c478bd9Sstevel@tonic-gate (void) strcpy(fname, "/as"); 5017c478bd9Sstevel@tonic-gate if ((fd = open(procname, omode, 0)) < 0 || 5027c478bd9Sstevel@tonic-gate (cp->asfd = Dupfd(fd, cp->asfd)) < 0) 5037c478bd9Sstevel@tonic-gate goto err; 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate (void) strcpy(fname, "/ctl"); 5067c478bd9Sstevel@tonic-gate if ((fd = open(procname, O_WRONLY, 0)) < 0 || 5077c478bd9Sstevel@tonic-gate (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0) 5087c478bd9Sstevel@tonic-gate goto err; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate (void) strcpy(fname, "/status"); 5117c478bd9Sstevel@tonic-gate if ((fd = open(procname, O_RDONLY, 0)) < 0 || 5127c478bd9Sstevel@tonic-gate (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0) 5137c478bd9Sstevel@tonic-gate goto err; 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate return (0); 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate err: 5187c478bd9Sstevel@tonic-gate CloseProc(cp); 5197c478bd9Sstevel@tonic-gate return (-1); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * Close the /proc/<pid> files. 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate static void 5267c478bd9Sstevel@tonic-gate CloseProc(cstatus_t *cp) 5277c478bd9Sstevel@tonic-gate { 5287c478bd9Sstevel@tonic-gate if (cp->asfd > 0) 5297c478bd9Sstevel@tonic-gate (void) close(cp->asfd); 5307c478bd9Sstevel@tonic-gate if (cp->ctlfd > 0) 5317c478bd9Sstevel@tonic-gate (void) close(cp->ctlfd); 5327c478bd9Sstevel@tonic-gate if (cp->statusfd > 0) 5337c478bd9Sstevel@tonic-gate (void) close(cp->statusfd); 5347c478bd9Sstevel@tonic-gate cp->asfd = 0; 5357c478bd9Sstevel@tonic-gate cp->ctlfd = 0; 5367c478bd9Sstevel@tonic-gate cp->statusfd = 0; 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate /* 5407c478bd9Sstevel@tonic-gate * Take control of a child process. 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate static cstatus_t * 5437c478bd9Sstevel@tonic-gate GrabProc(pid_t pid) 5447c478bd9Sstevel@tonic-gate { 5457c478bd9Sstevel@tonic-gate cstatus_t *cp; 5467c478bd9Sstevel@tonic-gate long ctl[2]; 5477c478bd9Sstevel@tonic-gate pid_t ppid; 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate if (pid <= 0) 5507c478bd9Sstevel@tonic-gate return (NULLCP); 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate if ((cp = FindProc(pid)) != NULLCP) /* already grabbed */ 5537c478bd9Sstevel@tonic-gate return (cp); 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate CheckAllProcs(); /* clean up before grabbing new process */ 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate cp = (cstatus_t *)malloc(sizeof (cstatus_t)); 5587c478bd9Sstevel@tonic-gate if (cp == NULLCP) 5597c478bd9Sstevel@tonic-gate return (NULLCP); 5607c478bd9Sstevel@tonic-gate (void) memset((char *)cp, 0, sizeof (cstatus_t)); 5617c478bd9Sstevel@tonic-gate cp->pid = pid; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate ppid = getpid(); 5647c478bd9Sstevel@tonic-gate while (OpenProc(cp) == 0) { 5657c478bd9Sstevel@tonic-gate ctl[0] = PCSET; 5667c478bd9Sstevel@tonic-gate ctl[1] = PR_RLC; 5677c478bd9Sstevel@tonic-gate errno = 0; 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate if (pread(cp->statusfd, (char *)&cp->pstatus, 5707c478bd9Sstevel@tonic-gate sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) && 5717c478bd9Sstevel@tonic-gate cp->pstatus.pr_ppid == ppid && 5727c478bd9Sstevel@tonic-gate (cp->pstatus.pr_flags & PR_PTRACE) && 5737c478bd9Sstevel@tonic-gate write(cp->ctlfd, (char *)ctl, 2*sizeof (long)) 5747c478bd9Sstevel@tonic-gate == 2*sizeof (long)) { 5757c478bd9Sstevel@tonic-gate cp->next = childp; 5767c478bd9Sstevel@tonic-gate childp = cp; 5777c478bd9Sstevel@tonic-gate MakeUser(cp); 5787c478bd9Sstevel@tonic-gate return (cp); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate if (errno != EAGAIN) 5827c478bd9Sstevel@tonic-gate break; 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate free((char *)cp); 5867c478bd9Sstevel@tonic-gate return (NULLCP); 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate /* 5907c478bd9Sstevel@tonic-gate * Close the /proc/<pid> file, if open. 5917c478bd9Sstevel@tonic-gate * Deallocate the memory used by the cstatus_t structure. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate static void 5947c478bd9Sstevel@tonic-gate ReleaseProc(cstatus_t *cp) 5957c478bd9Sstevel@tonic-gate { 5967c478bd9Sstevel@tonic-gate CloseProc(cp); 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if (childp == cp) 5997c478bd9Sstevel@tonic-gate childp = cp->next; 6007c478bd9Sstevel@tonic-gate else { 6017c478bd9Sstevel@tonic-gate cstatus_t *pcp; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate for (pcp = childp; pcp != NULLCP; pcp = pcp->next) { 6047c478bd9Sstevel@tonic-gate if (pcp->next == cp) { 6057c478bd9Sstevel@tonic-gate pcp->next = cp->next; 6067c478bd9Sstevel@tonic-gate break; 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate free((char *)cp); 6127c478bd9Sstevel@tonic-gate } 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * Update process information from /proc. 6167c478bd9Sstevel@tonic-gate * Return 0 on success, -1 on failure. 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate static int 6197c478bd9Sstevel@tonic-gate ProcUpdate(cstatus_t *cp) 6207c478bd9Sstevel@tonic-gate { 6217c478bd9Sstevel@tonic-gate pstatus_t *ps = &cp->pstatus; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (cp->flags & CS_SETREGS) { 6247c478bd9Sstevel@tonic-gate long cmd; 6257c478bd9Sstevel@tonic-gate iovec_t iov[2]; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR]; 6287c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_PC] = cp->user.u_reg[REG_PC]; 6297c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC]; 6307c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_Y] = cp->user.u_reg[REG_Y]; 6317c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G1] = cp->user.u_reg[REG_G1]; 6327c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G2] = cp->user.u_reg[REG_G2]; 6337c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G3] = cp->user.u_reg[REG_G3]; 6347c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G4] = cp->user.u_reg[REG_G4]; 6357c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G5] = cp->user.u_reg[REG_G5]; 6367c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G6] = cp->user.u_reg[REG_G6]; 6377c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_G7] = cp->user.u_reg[REG_G7]; 6387c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O0] = cp->user.u_reg[REG_O0]; 6397c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O1] = cp->user.u_reg[REG_O1]; 6407c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O2] = cp->user.u_reg[REG_O2]; 6417c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O3] = cp->user.u_reg[REG_O3]; 6427c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O4] = cp->user.u_reg[REG_O4]; 6437c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O5] = cp->user.u_reg[REG_O5]; 6447c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O6] = cp->user.u_reg[REG_O6]; 6457c478bd9Sstevel@tonic-gate ps->pr_lwp.pr_reg[R_O7] = cp->user.u_reg[REG_O7]; 6467c478bd9Sstevel@tonic-gate (void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0], 6478cd45542Sraf 16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]); 6487c478bd9Sstevel@tonic-gate cmd = PCSREG; 6497c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)&cmd; 6507c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (long); 6517c478bd9Sstevel@tonic-gate iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0]; 6527c478bd9Sstevel@tonic-gate iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg); 6537c478bd9Sstevel@tonic-gate (void) writev(cp->ctlfd, iov, 2); 6547c478bd9Sstevel@tonic-gate cp->flags &= ~CS_SETREGS; 6557c478bd9Sstevel@tonic-gate } 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) { 6587c478bd9Sstevel@tonic-gate /* attempt to regain control */ 6597c478bd9Sstevel@tonic-gate if (errno != EINTR && 6607c478bd9Sstevel@tonic-gate !(errno == EAGAIN && OpenProc(cp) == 0)) 6617c478bd9Sstevel@tonic-gate return (-1); 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate if (ps->pr_flags & PR_ISTOP) 6657c478bd9Sstevel@tonic-gate MakeUser(cp); 6667c478bd9Sstevel@tonic-gate else 6677c478bd9Sstevel@tonic-gate (void) memset((char *)ps, 0, sizeof (pstatus_t)); 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate return (0); 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate /* 6737c478bd9Sstevel@tonic-gate * Manufacture the contents of the fake u-block. 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate static void 6767c478bd9Sstevel@tonic-gate MakeUser(cstatus_t *cp) 6777c478bd9Sstevel@tonic-gate { 6787c478bd9Sstevel@tonic-gate pstatus_t *ps = &cp->pstatus; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_PSR] = ps->pr_lwp.pr_reg[R_PSR]; 6817c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_PC] = ps->pr_lwp.pr_reg[R_PC]; 6827c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_nPC] = ps->pr_lwp.pr_reg[R_nPC]; 6837c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_Y] = ps->pr_lwp.pr_reg[R_Y]; 6847c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G1] = ps->pr_lwp.pr_reg[R_G1]; 6857c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G2] = ps->pr_lwp.pr_reg[R_G2]; 6867c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G3] = ps->pr_lwp.pr_reg[R_G3]; 6877c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G4] = ps->pr_lwp.pr_reg[R_G4]; 6887c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G5] = ps->pr_lwp.pr_reg[R_G5]; 6897c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G6] = ps->pr_lwp.pr_reg[R_G6]; 6907c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_G7] = ps->pr_lwp.pr_reg[R_G7]; 6917c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O0] = ps->pr_lwp.pr_reg[R_O0]; 6927c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O1] = ps->pr_lwp.pr_reg[R_O1]; 6937c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O2] = ps->pr_lwp.pr_reg[R_O2]; 6947c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O3] = ps->pr_lwp.pr_reg[R_O3]; 6957c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O4] = ps->pr_lwp.pr_reg[R_O4]; 6967c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O5] = ps->pr_lwp.pr_reg[R_O5]; 6977c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O6] = ps->pr_lwp.pr_reg[R_O6]; 6987c478bd9Sstevel@tonic-gate cp->user.u_reg[REG_O7] = ps->pr_lwp.pr_reg[R_O7]; 6997c478bd9Sstevel@tonic-gate cp->user.u_ar0 = (greg_t *)REGADDR; 7007c478bd9Sstevel@tonic-gate cp->user.u_code = ps->pr_lwp.pr_info.si_code; 7017c478bd9Sstevel@tonic-gate cp->user.u_addr = ps->pr_lwp.pr_info.si_addr; 7027c478bd9Sstevel@tonic-gate cp->flags &= ~(CS_PSARGS|CS_SIGNAL); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * Fetch the contents of u_psargs[]. 7077c478bd9Sstevel@tonic-gate */ 7087c478bd9Sstevel@tonic-gate static void 7097c478bd9Sstevel@tonic-gate GetPsargs(cstatus_t *cp) 7107c478bd9Sstevel@tonic-gate { 7117c478bd9Sstevel@tonic-gate char procname[64]; /* /proc/<pid>/psinfo */ 7127c478bd9Sstevel@tonic-gate int fd; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate MakeProcName(procname, cp->pid); 7157c478bd9Sstevel@tonic-gate (void) strcat(procname, "/psinfo"); 7167c478bd9Sstevel@tonic-gate if ((fd = open(procname, O_RDONLY, 0)) < 0) { 7177c478bd9Sstevel@tonic-gate (void) memset(cp->user.u_psargs, 0, PSARGSZ); 7187c478bd9Sstevel@tonic-gate return; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate (void) pread(fd, cp->user.u_psargs, PSARGSZ, 7217c478bd9Sstevel@tonic-gate (off_t)((psinfo_t *)0)->pr_psargs); 7227c478bd9Sstevel@tonic-gate (void) close(fd); 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate cp->flags |= CS_PSARGS; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate /* 7287c478bd9Sstevel@tonic-gate * Fetch the contents of u_signal[]. 7297c478bd9Sstevel@tonic-gate */ 7307c478bd9Sstevel@tonic-gate static void 7317c478bd9Sstevel@tonic-gate GetSignal(cstatus_t *cp) 7327c478bd9Sstevel@tonic-gate { 7337c478bd9Sstevel@tonic-gate char procname[64]; /* /proc/<pid>/sigact */ 7347c478bd9Sstevel@tonic-gate int fd; 7357c478bd9Sstevel@tonic-gate struct sigaction action[MAXSIG]; 7367c478bd9Sstevel@tonic-gate int i; 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate MakeProcName(procname, cp->pid); 7397c478bd9Sstevel@tonic-gate (void) strcat(procname, "/sigact"); 7407c478bd9Sstevel@tonic-gate (void) memset((char *)action, 0, sizeof (action)); 7417c478bd9Sstevel@tonic-gate if ((fd = open(procname, O_RDONLY, 0)) >= 0) { 7427c478bd9Sstevel@tonic-gate (void) read(fd, (char *)action, sizeof (action)); 7437c478bd9Sstevel@tonic-gate (void) close(fd); 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSIG; i++) 7467c478bd9Sstevel@tonic-gate cp->user.u_signal[i] = action[i].sa_handler; 7477c478bd9Sstevel@tonic-gate cp->flags |= CS_SIGNAL; 7487c478bd9Sstevel@tonic-gate } 749