xref: /titanic_50/usr/src/lib/libc/sparc/sys/ptrace.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ptrace(2) interface built on top of proc(4).
24  */
25 
26 /*
27  * Copyright 1990-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #pragma weak ptrace = _ptrace
34 
35 #include "synonyms.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <memory.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 #include <signal.h>
46 #include <sys/siginfo.h>
47 #include <sys/fault.h>
48 #include <sys/syscall.h>
49 #include <procfs.h>
50 #include <sys/psw.h>
51 #include <sys/user.h>
52 /*
53  * mtlib.h must precede thread.h
54  */
55 #include <mtlib.h>
56 #include <thread.h>
57 #include <synch.h>
58 #include <unistd.h>
59 
60 static mutex_t pt_lock = DEFAULTMUTEX;
61 
62 #define	TRUE	1
63 #define	FALSE	0
64 
65 /*
66  * All my children...
67  */
68 typedef struct cstatus {
69 	struct cstatus	*next;		/* linked list			*/
70 	pid_t		pid;		/* process-id			*/
71 	int		asfd;		/* /proc/<pid>/as		*/
72 	int		ctlfd;		/* /proc/<pid>/ctl		*/
73 	int		statusfd;	/* /proc/<pid>/status		*/
74 	int		flags;		/* see below			*/
75 	pstatus_t	pstatus;	/* from /proc/<pid>/status	*/
76 	user_t		user;		/* manufactured u-block		*/
77 } cstatus_t;
78 
79 /* flags */
80 #define	CS_SETREGS	0x01		/* set registers on run		*/
81 #define	CS_PSARGS	0x02		/* u_psargs[] has been fetched	*/
82 #define	CS_SIGNAL	0x04		/* u_signal[] has been fetched	*/
83 
84 #define	NULLCP	((cstatus_t *)0)
85 
86 static cstatus_t *childp = NULLCP;
87 
88 /* fake u-block offsets */
89 #define	UP		((user_t *)NULL)
90 #define	U_REG		((int)(&UP->u_reg[0]))
91 #define	U_AR0		((int)(&UP->u_ar0))
92 #define	U_PSARGS	((int)(&UP->u_psargs[0]))
93 #define	U_SIGNAL	((int)(&UP->u_signal[0]))
94 #define	U_CODE		((int)(&UP->u_code))
95 #define	U_ADDR		((int)(&UP->u_addr))
96 #define	U_END		((int)sizeof (user_t))
97 #define	REGADDR		0xffff0000	/* arbitrary kernel address for u_ar0 */
98 
99 /* external routines defined in this module */
100 extern	int	ptrace(int, pid_t, int, int);
101 /* static routines defined in this module */
102 static	cstatus_t *FindProc(pid_t);
103 static	void	CheckAllProcs(void);
104 static	int	Dupfd(int, int);
105 static	void	MakeProcName(char *, pid_t);
106 static	int	OpenProc(cstatus_t *);
107 static	void	CloseProc(cstatus_t *);
108 static	cstatus_t *GrabProc(pid_t);
109 static	void	ReleaseProc(cstatus_t *);
110 static	int	ProcUpdate(cstatus_t *);
111 static	void	MakeUser(cstatus_t *);
112 static	void	GetPsargs(cstatus_t *);
113 static	void	GetSignal(cstatus_t *);
114 
115 #if PTRACE_DEBUG
116 /* for debugging */
117 static char *
118 map(int request)
119 {
120 	static char name[20];
121 
122 	switch (request) {
123 	case 0:	return ("PTRACE_TRACEME");
124 	case 1:	return ("PTRACE_PEEKTEXT");
125 	case 2:	return ("PTRACE_PEEKDATA");
126 	case 3:	return ("PTRACE_PEEKUSER");
127 	case 4:	return ("PTRACE_POKETEXT");
128 	case 5:	return ("PTRACE_POKEDATA");
129 	case 6:	return ("PTRACE_POKEUSER");
130 	case 7:	return ("PTRACE_CONT");
131 	case 8:	return ("PTRACE_KILL");
132 	case 9:	return ("PTRACE_SINGLESTEP");
133 	}
134 	(void) sprintf(name, "%d", request);
135 	return (name);
136 }
137 #endif
138 
139 int
140 ptrace(int request, pid_t pid, int addr, int data)
141 {
142 	pstatus_t *ps;
143 	cstatus_t *cp;
144 	unsigned xaddr;
145 	struct {
146 		long cmd;
147 		union {
148 			long flags;
149 			sigset_t signals;
150 			fltset_t faults;
151 			sysset_t syscalls;
152 			siginfo_t siginfo;
153 		} arg;
154 	} ctl;
155 
156 #if PTRACE_DEBUG
157 	fprintf(stderr, " ptrace(%s, 0x%X, 0x%X, 0x%X)\n",
158 		map(request), pid, addr, data);
159 #endif
160 
161 	(void) _private_mutex_lock(&pt_lock);
162 
163 	if (request == 0) {	/* PTRACE_TRACEME, executed by traced process */
164 		/*
165 		 * Set stop-on-all-signals and nothing else.
166 		 * Turn off inherit-on-fork flag (grandchildren run away).
167 		 * Set ptrace-compatible flag.
168 		 */
169 		char procname[64];	/* /proc/<pid>/ctl */
170 		int fd;
171 
172 		MakeProcName(procname, getpid());
173 		(void) strcat(procname, "/ctl");
174 		if ((fd = open(procname, O_WRONLY, 0)) < 0)
175 			exit(255);
176 		ctl.cmd = PCSTRACE;
177 		prfillset(&ctl.arg.signals);
178 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sigset_t))
179 		    != sizeof (long)+sizeof (sigset_t))
180 			exit(255);
181 		ctl.cmd = PCSFAULT;
182 		premptyset(&ctl.arg.faults);
183 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (fltset_t))
184 		    != sizeof (long)+sizeof (fltset_t))
185 			exit(255);
186 		ctl.cmd = PCSENTRY;
187 		premptyset(&ctl.arg.syscalls);
188 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
189 		    != sizeof (long)+sizeof (sysset_t))
190 			exit(255);
191 		ctl.cmd = PCSEXIT;
192 		premptyset(&ctl.arg.syscalls);
193 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
194 		    != sizeof (long)+sizeof (sysset_t))
195 			exit(255);
196 		ctl.cmd = PCUNSET;
197 		ctl.arg.flags = PR_FORK;
198 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
199 		    != sizeof (long)+sizeof (long))
200 			exit(255);
201 		ctl.cmd = PCSET;
202 		ctl.arg.flags = PR_PTRACE;
203 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
204 		    != sizeof (long)+sizeof (long))
205 			exit(255);
206 		if (close(fd) != 0)
207 			exit(255);
208 
209 		(void) _private_mutex_unlock(&pt_lock);
210 		return (0);
211 	}
212 
213 again:
214 	errno = 0;
215 
216 	/* find the cstatus structure corresponding to pid */
217 	if ((cp = GrabProc(pid)) == NULLCP)
218 		goto esrch;
219 
220 	ps = &cp->pstatus;
221 	if (!(ps->pr_flags & PR_ISTOP)) {
222 		if (ProcUpdate(cp) != 0) {
223 			ReleaseProc(cp);
224 			goto esrch;
225 		}
226 		if (!(ps->pr_flags & PR_ISTOP))
227 			goto esrch;
228 	}
229 
230 	/*
231 	 * Process the request.
232 	 */
233 	errno = 0;
234 	switch (request) {
235 	case 1:		/* PTRACE_PEEKTEXT */
236 	case 2:		/* PTRACE_PEEKDATA */
237 		if (addr & 03)
238 			goto eio;
239 		if (pread(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
240 		    == sizeof (data)) {
241 			(void) _private_mutex_unlock(&pt_lock);
242 			return (data);
243 		}
244 		goto eio;
245 
246 	case 3:		/* PTRACE_PEEKUSER */
247 		if (addr & 03)
248 			goto eio;
249 		xaddr = addr;
250 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
251 			xaddr -= REGADDR-U_REG;
252 		if (xaddr >= U_PSARGS && xaddr < U_PSARGS+sizeof (UP->u_psargs))
253 			GetPsargs(cp);
254 		if (xaddr >= U_SIGNAL && xaddr < U_SIGNAL+sizeof (UP->u_signal))
255 			GetSignal(cp);
256 		if ((int)xaddr >= 0 && xaddr < U_END) {
257 			/* LINTED pointer alignment */
258 			data = *((int *)((caddr_t)(&cp->user) + xaddr));
259 			(void) _private_mutex_unlock(&pt_lock);
260 			return (data);
261 		}
262 		goto eio;
263 
264 	case 4:		/* PTRACE_POKETEXT */
265 	case 5:		/* PTRACE_POKEDATA */
266 		if (addr & 03)
267 			goto eio;
268 		xaddr = addr;
269 		if (xaddr >= (unsigned)cp->user.u_reg[REG_SP] &&
270 		    xaddr < (unsigned)cp->user.u_reg[REG_SP]+16*sizeof (int))
271 			cp->flags |= CS_SETREGS;
272 		if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
273 		    == sizeof (data)) {
274 			(void) _private_mutex_unlock(&pt_lock);
275 			return (data);
276 		}
277 		goto eio;
278 
279 	case 6:		/* PTRACE_POKEUSER */
280 		if (addr & 03)
281 			goto eio;
282 		xaddr = addr;
283 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
284 			xaddr -= REGADDR-U_REG;
285 		if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) {
286 			int rx = (xaddr-U_REG)/sizeof (greg_t);
287 			if (rx == REG_PS)
288 				data = (cp->user.u_reg[REG_PS] &
289 				    ~PSL_USERMASK) | (data & PSL_USERMASK);
290 			else if (rx == REG_SP || rx == REG_PC || rx == REG_nPC)
291 				data &= ~03;
292 			cp->user.u_reg[rx] = data;
293 			cp->flags |= CS_SETREGS;
294 			(void) _private_mutex_unlock(&pt_lock);
295 			return (data);
296 		}
297 		goto eio;
298 
299 	case 7:		/* PTRACE_CONT */
300 	case 9:		/* PTRACE_SINGLESTEP */
301 	    {
302 		long runctl[3];
303 
304 		if (cp->flags & CS_SETREGS) {
305 			long cmd;
306 			iovec_t iov[2];
307 
308 			ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
309 			ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
310 			ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
311 			ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
312 			ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
313 			ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
314 			ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
315 			ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
316 			ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
317 			ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
318 			ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
319 			ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
320 			ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
321 			ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
322 			ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
323 			ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
324 			ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
325 			ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
326 			ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
327 			(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
328 				16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
329 			cmd = PCSREG;
330 			iov[0].iov_base = (caddr_t)&cmd;
331 			iov[0].iov_len = sizeof (long);
332 			iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
333 			iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
334 			if (writev(cp->ctlfd, iov, 2) < 0)
335 				goto tryagain;
336 		}
337 		if (addr != 1 &&	/* new virtual address */
338 		    (addr & ~03) != cp->user.u_reg[REG_PC]) {
339 			runctl[0] = PCSVADDR;
340 			runctl[1] = (addr & ~03);
341 			if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long))
342 			    != 2*sizeof (long))
343 				goto tryagain;
344 		}
345 		/* make data the current signal */
346 		if (data != 0 && data != ps->pr_lwp.pr_cursig) {
347 			(void) memset((char *)&ctl.arg.siginfo, 0,
348 			    sizeof (siginfo_t));
349 			ctl.arg.siginfo.si_signo = data;
350 			ctl.cmd = PCSSIG;
351 			if (write(cp->ctlfd, (char *)&ctl,
352 			    sizeof (long)+sizeof (siginfo_t))
353 			    != sizeof (long)+sizeof (siginfo_t))
354 				goto tryagain;
355 		}
356 		if (data == 0)
357 			runctl[0] = PCCSIG;
358 		else
359 			runctl[0] = PCNULL;
360 		runctl[1] = PCRUN;
361 		runctl[2] = (request == 9)? PRSTEP : 0;
362 		if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long))
363 		    != 3*sizeof (long)) {
364 			if (errno == ENOENT) {
365 				/* current signal must have killed it */
366 				ReleaseProc(cp);
367 				(void) _private_mutex_unlock(&pt_lock);
368 				return (data);
369 			}
370 			goto tryagain;
371 		}
372 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
373 		cp->flags = 0;
374 		(void) _private_mutex_unlock(&pt_lock);
375 		return (data);
376 	    }
377 
378 	case 8:		/* PTRACE_KILL */
379 		/* overkill? */
380 		(void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t));
381 		ctl.arg.siginfo.si_signo = SIGKILL;
382 		ctl.cmd = PCSSIG;
383 		(void) write(cp->ctlfd, (char *)&ctl,
384 		    sizeof (long)+sizeof (siginfo_t));
385 		(void) kill(pid, SIGKILL);
386 		ReleaseProc(cp);
387 		(void) _private_mutex_unlock(&pt_lock);
388 		return (0);
389 
390 	default:
391 		goto eio;
392 	}
393 
394 tryagain:
395 	if (errno == EAGAIN) {
396 		if (OpenProc(cp) == 0)
397 			goto again;
398 		ReleaseProc(cp);
399 	}
400 eio:
401 	errno = EIO;
402 	(void) _private_mutex_unlock(&pt_lock);
403 	return (-1);
404 esrch:
405 	errno = ESRCH;
406 	(void) _private_mutex_unlock(&pt_lock);
407 	return (-1);
408 }
409 
410 /*
411  * Find the cstatus structure corresponding to pid.
412  */
413 static cstatus_t *
414 FindProc(pid_t pid)
415 {
416 	cstatus_t *cp;
417 
418 	for (cp = childp; cp != NULLCP; cp = cp->next)
419 		if (cp->pid == pid)
420 			break;
421 
422 	return (cp);
423 }
424 
425 /*
426  * Check every proc for existence, release those that are gone.
427  * Be careful about the linked list; ReleaseProc() changes it.
428  */
429 static void
430 CheckAllProcs()
431 {
432 	cstatus_t *cp = childp;
433 
434 	while (cp != NULLCP) {
435 		cstatus_t *next = cp->next;
436 
437 		if (ProcUpdate(cp) != 0)
438 			ReleaseProc(cp);
439 		cp = next;
440 	}
441 }
442 
443 /*
444  * Utility for OpenProc().
445  */
446 static int
447 Dupfd(int fd, int dfd)
448 {
449 	/*
450 	 * Make sure fd not one of 0, 1, or 2 to avoid stdio interference.
451 	 * Also, if dfd is greater than 2, dup fd to be exactly dfd.
452 	 */
453 	if (dfd > 2 || (0 <= fd && fd <= 2)) {
454 		if (dfd > 2 && fd != dfd)
455 			(void) close(dfd);
456 		else
457 			dfd = 3;
458 		if (fd != dfd) {
459 			dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd);
460 			(void) close(fd);
461 			fd = dfd;
462 		}
463 	}
464 	/*
465 	 * Mark filedescriptor close-on-exec.
466 	 * Should also be close-on-return-from-fork-in-child.
467 	 */
468 	(void) fcntl(fd, F_SETFD, (intptr_t)1);
469 	return (fd);
470 }
471 
472 /*
473  * Construct the /proc directory name:  "/proc/<pid>"
474  * The name buffer passed by the caller must be large enough.
475  */
476 static void
477 MakeProcName(char *procname, pid_t pid)
478 {
479 	(void) sprintf(procname, "/proc/%d", pid);
480 }
481 
482 /*
483  * Open/reopen the /proc/<pid> files.
484  */
485 static int
486 OpenProc(cstatus_t *cp)
487 {
488 	char procname[64];		/* /proc/nnnnn/fname */
489 	char *fname;
490 	int fd;
491 	int omode;
492 
493 	MakeProcName(procname, cp->pid);
494 	fname = procname + strlen(procname);
495 
496 	/*
497 	 * Use exclusive-open only if this is the first open.
498 	 */
499 	omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL);
500 	(void) strcpy(fname, "/as");
501 	if ((fd = open(procname, omode, 0)) < 0 ||
502 	    (cp->asfd = Dupfd(fd, cp->asfd)) < 0)
503 		goto err;
504 
505 	(void) strcpy(fname, "/ctl");
506 	if ((fd = open(procname, O_WRONLY, 0)) < 0 ||
507 	    (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0)
508 		goto err;
509 
510 	(void) strcpy(fname, "/status");
511 	if ((fd = open(procname, O_RDONLY, 0)) < 0 ||
512 	    (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0)
513 		goto err;
514 
515 	return (0);
516 
517 err:
518 	CloseProc(cp);
519 	return (-1);
520 }
521 
522 /*
523  * Close the /proc/<pid> files.
524  */
525 static void
526 CloseProc(cstatus_t *cp)
527 {
528 	if (cp->asfd > 0)
529 		(void) close(cp->asfd);
530 	if (cp->ctlfd > 0)
531 		(void) close(cp->ctlfd);
532 	if (cp->statusfd > 0)
533 		(void) close(cp->statusfd);
534 	cp->asfd = 0;
535 	cp->ctlfd = 0;
536 	cp->statusfd = 0;
537 }
538 
539 /*
540  * Take control of a child process.
541  */
542 static cstatus_t *
543 GrabProc(pid_t pid)
544 {
545 	cstatus_t *cp;
546 	long ctl[2];
547 	pid_t ppid;
548 
549 	if (pid <= 0)
550 		return (NULLCP);
551 
552 	if ((cp = FindProc(pid)) != NULLCP)	/* already grabbed */
553 		return (cp);
554 
555 	CheckAllProcs();	/* clean up before grabbing new process */
556 
557 	cp = (cstatus_t *)malloc(sizeof (cstatus_t));
558 	if (cp == NULLCP)
559 		return (NULLCP);
560 	(void) memset((char *)cp, 0, sizeof (cstatus_t));
561 	cp->pid = pid;
562 
563 	ppid = getpid();
564 	while (OpenProc(cp) == 0) {
565 		ctl[0] = PCSET;
566 		ctl[1] = PR_RLC;
567 		errno = 0;
568 
569 		if (pread(cp->statusfd, (char *)&cp->pstatus,
570 		    sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) &&
571 		    cp->pstatus.pr_ppid == ppid &&
572 		    (cp->pstatus.pr_flags & PR_PTRACE) &&
573 		    write(cp->ctlfd, (char *)ctl, 2*sizeof (long))
574 		    == 2*sizeof (long)) {
575 			cp->next = childp;
576 			childp = cp;
577 			MakeUser(cp);
578 			return (cp);
579 		}
580 
581 		if (errno != EAGAIN)
582 			break;
583 	}
584 
585 	free((char *)cp);
586 	return (NULLCP);
587 }
588 
589 /*
590  * Close the /proc/<pid> file, if open.
591  * Deallocate the memory used by the cstatus_t structure.
592  */
593 static void
594 ReleaseProc(cstatus_t *cp)
595 {
596 	CloseProc(cp);
597 
598 	if (childp == cp)
599 		childp = cp->next;
600 	else {
601 		cstatus_t *pcp;
602 
603 		for (pcp = childp; pcp != NULLCP; pcp = pcp->next) {
604 			if (pcp->next == cp) {
605 				pcp->next = cp->next;
606 				break;
607 			}
608 		}
609 	}
610 
611 	free((char *)cp);
612 }
613 
614 /*
615  * Update process information from /proc.
616  * Return 0 on success, -1 on failure.
617  */
618 static int
619 ProcUpdate(cstatus_t *cp)
620 {
621 	pstatus_t *ps = &cp->pstatus;
622 
623 	if (cp->flags & CS_SETREGS) {
624 		long cmd;
625 		iovec_t iov[2];
626 
627 		ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
628 		ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
629 		ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
630 		ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
631 		ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
632 		ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
633 		ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
634 		ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
635 		ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
636 		ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
637 		ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
638 		ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
639 		ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
640 		ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
641 		ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
642 		ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
643 		ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
644 		ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
645 		ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
646 		(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
647 			16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
648 		cmd = PCSREG;
649 		iov[0].iov_base = (caddr_t)&cmd;
650 		iov[0].iov_len = sizeof (long);
651 		iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
652 		iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
653 		(void) writev(cp->ctlfd, iov, 2);
654 		cp->flags &= ~CS_SETREGS;
655 	}
656 
657 	while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) {
658 		/* attempt to regain control */
659 		if (errno != EINTR &&
660 		    !(errno == EAGAIN && OpenProc(cp) == 0))
661 			return (-1);
662 	}
663 
664 	if (ps->pr_flags & PR_ISTOP)
665 		MakeUser(cp);
666 	else
667 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
668 
669 	return (0);
670 }
671 
672 /*
673  * Manufacture the contents of the fake u-block.
674  */
675 static void
676 MakeUser(cstatus_t *cp)
677 {
678 	pstatus_t *ps = &cp->pstatus;
679 
680 	cp->user.u_reg[REG_PSR] = ps->pr_lwp.pr_reg[R_PSR];
681 	cp->user.u_reg[REG_PC]  = ps->pr_lwp.pr_reg[R_PC];
682 	cp->user.u_reg[REG_nPC] = ps->pr_lwp.pr_reg[R_nPC];
683 	cp->user.u_reg[REG_Y]   = ps->pr_lwp.pr_reg[R_Y];
684 	cp->user.u_reg[REG_G1]  = ps->pr_lwp.pr_reg[R_G1];
685 	cp->user.u_reg[REG_G2]  = ps->pr_lwp.pr_reg[R_G2];
686 	cp->user.u_reg[REG_G3]  = ps->pr_lwp.pr_reg[R_G3];
687 	cp->user.u_reg[REG_G4]  = ps->pr_lwp.pr_reg[R_G4];
688 	cp->user.u_reg[REG_G5]  = ps->pr_lwp.pr_reg[R_G5];
689 	cp->user.u_reg[REG_G6]  = ps->pr_lwp.pr_reg[R_G6];
690 	cp->user.u_reg[REG_G7]  = ps->pr_lwp.pr_reg[R_G7];
691 	cp->user.u_reg[REG_O0]  = ps->pr_lwp.pr_reg[R_O0];
692 	cp->user.u_reg[REG_O1]  = ps->pr_lwp.pr_reg[R_O1];
693 	cp->user.u_reg[REG_O2]  = ps->pr_lwp.pr_reg[R_O2];
694 	cp->user.u_reg[REG_O3]  = ps->pr_lwp.pr_reg[R_O3];
695 	cp->user.u_reg[REG_O4]  = ps->pr_lwp.pr_reg[R_O4];
696 	cp->user.u_reg[REG_O5]  = ps->pr_lwp.pr_reg[R_O5];
697 	cp->user.u_reg[REG_O6]  = ps->pr_lwp.pr_reg[R_O6];
698 	cp->user.u_reg[REG_O7]  = ps->pr_lwp.pr_reg[R_O7];
699 	cp->user.u_ar0 = (greg_t *)REGADDR;
700 	cp->user.u_code = ps->pr_lwp.pr_info.si_code;
701 	cp->user.u_addr = ps->pr_lwp.pr_info.si_addr;
702 	cp->flags &= ~(CS_PSARGS|CS_SIGNAL);
703 }
704 
705 /*
706  * Fetch the contents of u_psargs[].
707  */
708 static void
709 GetPsargs(cstatus_t *cp)
710 {
711 	char procname[64];	/* /proc/<pid>/psinfo */
712 	int fd;
713 
714 	MakeProcName(procname, cp->pid);
715 	(void) strcat(procname, "/psinfo");
716 	if ((fd = open(procname, O_RDONLY, 0)) < 0) {
717 		(void) memset(cp->user.u_psargs, 0, PSARGSZ);
718 		return;
719 	}
720 	(void) pread(fd, cp->user.u_psargs, PSARGSZ,
721 	    (off_t)((psinfo_t *)0)->pr_psargs);
722 	(void) close(fd);
723 
724 	cp->flags |= CS_PSARGS;
725 }
726 
727 /*
728  * Fetch the contents of u_signal[].
729  */
730 static void
731 GetSignal(cstatus_t *cp)
732 {
733 	char procname[64];	/* /proc/<pid>/sigact */
734 	int fd;
735 	struct sigaction action[MAXSIG];
736 	int i;
737 
738 	MakeProcName(procname, cp->pid);
739 	(void) strcat(procname, "/sigact");
740 	(void) memset((char *)action, 0, sizeof (action));
741 	if ((fd = open(procname, O_RDONLY, 0)) >= 0) {
742 		(void) read(fd, (char *)action, sizeof (action));
743 		(void) close(fd);
744 	}
745 	for (i = 0; i < MAXSIG; i++)
746 		cp->user.u_signal[i] = action[i].sa_handler;
747 	cp->flags |= CS_SIGNAL;
748 }
749