xref: /onnv-gate/usr/src/lib/libproc/common/Pscantext.c (revision 2712:f74a135872bc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2712Snn35248  * Common Development and Distribution License (the "License").
6*2712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2712Snn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <fcntl.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <errno.h>
35*2712Snn35248 #include <limits.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "libproc.h"
380Sstevel@tonic-gate #include "Pcontrol.h"
390Sstevel@tonic-gate #include "Pisadep.h"
400Sstevel@tonic-gate #include "Putil.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	BLKSIZE	(8 * 1024)
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Look for a SYSCALL instruction in the process's address space.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate int
Pscantext(struct ps_prochandle * P)480Sstevel@tonic-gate Pscantext(struct ps_prochandle *P)
490Sstevel@tonic-gate {
50*2712Snn35248 	char mapfile[PATH_MAX];
510Sstevel@tonic-gate 	int mapfd;
520Sstevel@tonic-gate 	off_t offset;		/* offset in text section */
530Sstevel@tonic-gate 	off_t endoff;		/* ending offset in text section */
540Sstevel@tonic-gate 	uintptr_t sysaddr;	/* address of SYSCALL instruction */
550Sstevel@tonic-gate 	int syspri;		/* priority of SYSCALL instruction */
560Sstevel@tonic-gate 	int nbytes;		/* number of bytes in buffer */
570Sstevel@tonic-gate 	int n2bytes;		/* number of bytes in second buffer */
580Sstevel@tonic-gate 	int nmappings;		/* current number of mappings */
590Sstevel@tonic-gate 	prmap_t *pdp;		/* pointer to map descriptor */
600Sstevel@tonic-gate 	prmap_t *prbuf;		/* buffer for map descriptors */
610Sstevel@tonic-gate 	unsigned nmap;		/* number of map descriptors */
620Sstevel@tonic-gate 	uint32_t buf[2 * BLKSIZE / sizeof (uint32_t)];	/* text buffer */
630Sstevel@tonic-gate 	uchar_t *p;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	/* try the most recently-seen syscall address */
660Sstevel@tonic-gate 	syspri = 0;
670Sstevel@tonic-gate 	sysaddr = 0;
680Sstevel@tonic-gate 	if (P->sysaddr != 0 &&
690Sstevel@tonic-gate 	    (syspri = Pissyscall(P, P->sysaddr)))
700Sstevel@tonic-gate 		sysaddr = P->sysaddr;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/* try the previous instruction */
730Sstevel@tonic-gate 	if (sysaddr == 0 || syspri != 1)
740Sstevel@tonic-gate 		syspri = Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
750Sstevel@tonic-gate 		    &sysaddr);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (sysaddr != 0 && syspri == 1) {
780Sstevel@tonic-gate 		P->sysaddr = sysaddr;
790Sstevel@tonic-gate 		return (0);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/* open the /proc/<pid>/map file */
83*2712Snn35248 	(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
84*2712Snn35248 	    procfs_path, (int)P->pid);
850Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0) {
860Sstevel@tonic-gate 		dprintf("failed to open %s: %s\n", mapfile, strerror(errno));
870Sstevel@tonic-gate 		return (-1);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/* allocate a plausible initial buffer size */
910Sstevel@tonic-gate 	nmap = 50;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* read all the map structures, allocating more space as needed */
940Sstevel@tonic-gate 	for (;;) {
950Sstevel@tonic-gate 		prbuf = malloc(nmap * sizeof (prmap_t));
960Sstevel@tonic-gate 		if (prbuf == NULL) {
970Sstevel@tonic-gate 			dprintf("Pscantext: failed to allocate buffer\n");
980Sstevel@tonic-gate 			(void) close(mapfd);
990Sstevel@tonic-gate 			return (-1);
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 		nmappings = pread(mapfd, prbuf, nmap * sizeof (prmap_t), 0L);
1020Sstevel@tonic-gate 		if (nmappings < 0) {
1030Sstevel@tonic-gate 			dprintf("Pscantext: failed to read map file: %s\n",
1040Sstevel@tonic-gate 			    strerror(errno));
1050Sstevel@tonic-gate 			free(prbuf);
1060Sstevel@tonic-gate 			(void) close(mapfd);
1070Sstevel@tonic-gate 			return (-1);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		nmappings /= sizeof (prmap_t);
1100Sstevel@tonic-gate 		if (nmappings < nmap)	/* we read them all */
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 		/* allocate a bigger buffer */
1130Sstevel@tonic-gate 		free(prbuf);
1140Sstevel@tonic-gate 		nmap *= 2;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	(void) close(mapfd);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * Scan each executable mapping looking for a syscall instruction.
1200Sstevel@tonic-gate 	 * In dynamically linked executables, syscall instructions are
1210Sstevel@tonic-gate 	 * typically only found in shared libraries.  Because shared libraries
1220Sstevel@tonic-gate 	 * are most often mapped at the top of the address space, we minimize
1230Sstevel@tonic-gate 	 * our expected search time by starting at the last mapping and working
1240Sstevel@tonic-gate 	 * our way down to the first mapping.
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	for (pdp = &prbuf[nmappings - 1]; sysaddr == 0 && syspri != 1 &&
1270Sstevel@tonic-gate 	    pdp >= prbuf; pdp--) {
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		offset = (off_t)pdp->pr_vaddr;	/* beginning of text */
1300Sstevel@tonic-gate 		endoff = offset + pdp->pr_size;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		/* avoid non-EXEC mappings; avoid the stack and heap */
1330Sstevel@tonic-gate 		if ((pdp->pr_mflags&MA_EXEC) == 0 ||
1340Sstevel@tonic-gate 		    (endoff > P->status.pr_stkbase &&
1350Sstevel@tonic-gate 		    offset < P->status.pr_stkbase + P->status.pr_stksize) ||
1360Sstevel@tonic-gate 		    (endoff > P->status.pr_brkbase &&
1370Sstevel@tonic-gate 		    offset < P->status.pr_brkbase + P->status.pr_brksize))
1380Sstevel@tonic-gate 			continue;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		(void) lseek(P->asfd, (off_t)offset, 0);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		if ((nbytes = read(P->asfd, buf, 2*BLKSIZE)) <= 0)
1430Sstevel@tonic-gate 			continue;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if (nbytes < BLKSIZE)
1460Sstevel@tonic-gate 			n2bytes = 0;
1470Sstevel@tonic-gate 		else {
1480Sstevel@tonic-gate 			n2bytes = nbytes - BLKSIZE;
1490Sstevel@tonic-gate 			nbytes  = BLKSIZE;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		p = (uchar_t *)buf;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 		/* search text for a SYSCALL instruction */
1550Sstevel@tonic-gate 		while (sysaddr == 0 && syspri != 1 && offset < endoff) {
1560Sstevel@tonic-gate 			if (nbytes <= 0) {	/* shift buffers */
1570Sstevel@tonic-gate 				if ((nbytes = n2bytes) <= 0)
1580Sstevel@tonic-gate 					break;
1590Sstevel@tonic-gate 				(void) memcpy(buf,
1600Sstevel@tonic-gate 					&buf[BLKSIZE / sizeof (buf[0])],
1610Sstevel@tonic-gate 					nbytes);
1620Sstevel@tonic-gate 				n2bytes = 0;
1630Sstevel@tonic-gate 				p = (uchar_t *)buf;
1640Sstevel@tonic-gate 				if (nbytes == BLKSIZE &&
1650Sstevel@tonic-gate 				    offset + BLKSIZE < endoff)
1660Sstevel@tonic-gate 					n2bytes = read(P->asfd,
1670Sstevel@tonic-gate 						&buf[BLKSIZE / sizeof (buf[0])],
1680Sstevel@tonic-gate 						BLKSIZE);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			if (syspri = Pissyscall_text(P, p, nbytes))
1720Sstevel@tonic-gate 				sysaddr = offset;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 			p += sizeof (instr_t);
1750Sstevel@tonic-gate 			offset += sizeof (instr_t);
1760Sstevel@tonic-gate 			nbytes -= sizeof (instr_t);
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
180265Smws 	free(prbuf);
181265Smws 
1820Sstevel@tonic-gate 	if ((P->sysaddr = sysaddr) != 0)
1830Sstevel@tonic-gate 		return (0);
1840Sstevel@tonic-gate 	else
1850Sstevel@tonic-gate 		return (-1);
1860Sstevel@tonic-gate }
187