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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*1a7c1b72Smws  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "libproc.h"
387c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
397c478bd9Sstevel@tonic-gate #include "Pisadep.h"
407c478bd9Sstevel@tonic-gate #include "Putil.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	BLKSIZE	(8 * 1024)
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Look for a SYSCALL instruction in the process's address space.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
487c478bd9Sstevel@tonic-gate Pscantext(struct ps_prochandle *P)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	char mapfile[100];
517c478bd9Sstevel@tonic-gate 	int mapfd;
527c478bd9Sstevel@tonic-gate 	off_t offset;		/* offset in text section */
537c478bd9Sstevel@tonic-gate 	off_t endoff;		/* ending offset in text section */
547c478bd9Sstevel@tonic-gate 	uintptr_t sysaddr;	/* address of SYSCALL instruction */
557c478bd9Sstevel@tonic-gate 	int syspri;		/* priority of SYSCALL instruction */
567c478bd9Sstevel@tonic-gate 	int nbytes;		/* number of bytes in buffer */
577c478bd9Sstevel@tonic-gate 	int n2bytes;		/* number of bytes in second buffer */
587c478bd9Sstevel@tonic-gate 	int nmappings;		/* current number of mappings */
597c478bd9Sstevel@tonic-gate 	prmap_t *pdp;		/* pointer to map descriptor */
607c478bd9Sstevel@tonic-gate 	prmap_t *prbuf;		/* buffer for map descriptors */
617c478bd9Sstevel@tonic-gate 	unsigned nmap;		/* number of map descriptors */
627c478bd9Sstevel@tonic-gate 	uint32_t buf[2 * BLKSIZE / sizeof (uint32_t)];	/* text buffer */
637c478bd9Sstevel@tonic-gate 	uchar_t *p;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	/* try the most recently-seen syscall address */
667c478bd9Sstevel@tonic-gate 	syspri = 0;
677c478bd9Sstevel@tonic-gate 	sysaddr = 0;
687c478bd9Sstevel@tonic-gate 	if (P->sysaddr != 0 &&
697c478bd9Sstevel@tonic-gate 	    (syspri = Pissyscall(P, P->sysaddr)))
707c478bd9Sstevel@tonic-gate 		sysaddr = P->sysaddr;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* try the previous instruction */
737c478bd9Sstevel@tonic-gate 	if (sysaddr == 0 || syspri != 1)
747c478bd9Sstevel@tonic-gate 		syspri = Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
757c478bd9Sstevel@tonic-gate 		    &sysaddr);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (sysaddr != 0 && syspri == 1) {
787c478bd9Sstevel@tonic-gate 		P->sysaddr = sysaddr;
797c478bd9Sstevel@tonic-gate 		return (0);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* open the /proc/<pid>/map file */
837c478bd9Sstevel@tonic-gate 	(void) sprintf(mapfile, "/proc/%d/map", (int)P->pid);
847c478bd9Sstevel@tonic-gate 	if ((mapfd = open(mapfile, O_RDONLY)) < 0) {
857c478bd9Sstevel@tonic-gate 		dprintf("failed to open %s: %s\n", mapfile, strerror(errno));
867c478bd9Sstevel@tonic-gate 		return (-1);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* allocate a plausible initial buffer size */
907c478bd9Sstevel@tonic-gate 	nmap = 50;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/* read all the map structures, allocating more space as needed */
937c478bd9Sstevel@tonic-gate 	for (;;) {
947c478bd9Sstevel@tonic-gate 		prbuf = malloc(nmap * sizeof (prmap_t));
957c478bd9Sstevel@tonic-gate 		if (prbuf == NULL) {
967c478bd9Sstevel@tonic-gate 			dprintf("Pscantext: failed to allocate buffer\n");
977c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
987c478bd9Sstevel@tonic-gate 			return (-1);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		nmappings = pread(mapfd, prbuf, nmap * sizeof (prmap_t), 0L);
1017c478bd9Sstevel@tonic-gate 		if (nmappings < 0) {
1027c478bd9Sstevel@tonic-gate 			dprintf("Pscantext: failed to read map file: %s\n",
1037c478bd9Sstevel@tonic-gate 			    strerror(errno));
1047c478bd9Sstevel@tonic-gate 			free(prbuf);
1057c478bd9Sstevel@tonic-gate 			(void) close(mapfd);
1067c478bd9Sstevel@tonic-gate 			return (-1);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		nmappings /= sizeof (prmap_t);
1097c478bd9Sstevel@tonic-gate 		if (nmappings < nmap)	/* we read them all */
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		/* allocate a bigger buffer */
1127c478bd9Sstevel@tonic-gate 		free(prbuf);
1137c478bd9Sstevel@tonic-gate 		nmap *= 2;
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	(void) close(mapfd);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Scan each executable mapping looking for a syscall instruction.
1197c478bd9Sstevel@tonic-gate 	 * In dynamically linked executables, syscall instructions are
1207c478bd9Sstevel@tonic-gate 	 * typically only found in shared libraries.  Because shared libraries
1217c478bd9Sstevel@tonic-gate 	 * are most often mapped at the top of the address space, we minimize
1227c478bd9Sstevel@tonic-gate 	 * our expected search time by starting at the last mapping and working
1237c478bd9Sstevel@tonic-gate 	 * our way down to the first mapping.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	for (pdp = &prbuf[nmappings - 1]; sysaddr == 0 && syspri != 1 &&
1267c478bd9Sstevel@tonic-gate 	    pdp >= prbuf; pdp--) {
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		offset = (off_t)pdp->pr_vaddr;	/* beginning of text */
1297c478bd9Sstevel@tonic-gate 		endoff = offset + pdp->pr_size;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		/* avoid non-EXEC mappings; avoid the stack and heap */
1327c478bd9Sstevel@tonic-gate 		if ((pdp->pr_mflags&MA_EXEC) == 0 ||
1337c478bd9Sstevel@tonic-gate 		    (endoff > P->status.pr_stkbase &&
1347c478bd9Sstevel@tonic-gate 		    offset < P->status.pr_stkbase + P->status.pr_stksize) ||
1357c478bd9Sstevel@tonic-gate 		    (endoff > P->status.pr_brkbase &&
1367c478bd9Sstevel@tonic-gate 		    offset < P->status.pr_brkbase + P->status.pr_brksize))
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		(void) lseek(P->asfd, (off_t)offset, 0);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		if ((nbytes = read(P->asfd, buf, 2*BLKSIZE)) <= 0)
1427c478bd9Sstevel@tonic-gate 			continue;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		if (nbytes < BLKSIZE)
1457c478bd9Sstevel@tonic-gate 			n2bytes = 0;
1467c478bd9Sstevel@tonic-gate 		else {
1477c478bd9Sstevel@tonic-gate 			n2bytes = nbytes - BLKSIZE;
1487c478bd9Sstevel@tonic-gate 			nbytes  = BLKSIZE;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		p = (uchar_t *)buf;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		/* search text for a SYSCALL instruction */
1547c478bd9Sstevel@tonic-gate 		while (sysaddr == 0 && syspri != 1 && offset < endoff) {
1557c478bd9Sstevel@tonic-gate 			if (nbytes <= 0) {	/* shift buffers */
1567c478bd9Sstevel@tonic-gate 				if ((nbytes = n2bytes) <= 0)
1577c478bd9Sstevel@tonic-gate 					break;
1587c478bd9Sstevel@tonic-gate 				(void) memcpy(buf,
1597c478bd9Sstevel@tonic-gate 					&buf[BLKSIZE / sizeof (buf[0])],
1607c478bd9Sstevel@tonic-gate 					nbytes);
1617c478bd9Sstevel@tonic-gate 				n2bytes = 0;
1627c478bd9Sstevel@tonic-gate 				p = (uchar_t *)buf;
1637c478bd9Sstevel@tonic-gate 				if (nbytes == BLKSIZE &&
1647c478bd9Sstevel@tonic-gate 				    offset + BLKSIZE < endoff)
1657c478bd9Sstevel@tonic-gate 					n2bytes = read(P->asfd,
1667c478bd9Sstevel@tonic-gate 						&buf[BLKSIZE / sizeof (buf[0])],
1677c478bd9Sstevel@tonic-gate 						BLKSIZE);
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			if (syspri = Pissyscall_text(P, p, nbytes))
1717c478bd9Sstevel@tonic-gate 				sysaddr = offset;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 			p += sizeof (instr_t);
1747c478bd9Sstevel@tonic-gate 			offset += sizeof (instr_t);
1757c478bd9Sstevel@tonic-gate 			nbytes -= sizeof (instr_t);
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
179*1a7c1b72Smws 	free(prbuf);
180*1a7c1b72Smws 
1817c478bd9Sstevel@tonic-gate 	if ((P->sysaddr = sysaddr) != 0)
1827c478bd9Sstevel@tonic-gate 		return (0);
1837c478bd9Sstevel@tonic-gate 	else
1847c478bd9Sstevel@tonic-gate 		return (-1);
1857c478bd9Sstevel@tonic-gate }
186