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
5*9acbbeafSnn35248 * Common Development and Distribution License (the "License").
6*9acbbeafSnn35248 * 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 */
217c478bd9Sstevel@tonic-gate /*
22*9acbbeafSnn35248 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
35*9acbbeafSnn35248 #include <limits.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
Pscantext(struct ps_prochandle * P)487c478bd9Sstevel@tonic-gate Pscantext(struct ps_prochandle *P)
497c478bd9Sstevel@tonic-gate {
50*9acbbeafSnn35248 char mapfile[PATH_MAX];
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 */
83*9acbbeafSnn35248 (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
84*9acbbeafSnn35248 procfs_path, (int)P->pid);
857c478bd9Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0) {
867c478bd9Sstevel@tonic-gate dprintf("failed to open %s: %s\n", mapfile, strerror(errno));
877c478bd9Sstevel@tonic-gate return (-1);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /* allocate a plausible initial buffer size */
917c478bd9Sstevel@tonic-gate nmap = 50;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /* read all the map structures, allocating more space as needed */
947c478bd9Sstevel@tonic-gate for (;;) {
957c478bd9Sstevel@tonic-gate prbuf = malloc(nmap * sizeof (prmap_t));
967c478bd9Sstevel@tonic-gate if (prbuf == NULL) {
977c478bd9Sstevel@tonic-gate dprintf("Pscantext: failed to allocate buffer\n");
987c478bd9Sstevel@tonic-gate (void) close(mapfd);
997c478bd9Sstevel@tonic-gate return (-1);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate nmappings = pread(mapfd, prbuf, nmap * sizeof (prmap_t), 0L);
1027c478bd9Sstevel@tonic-gate if (nmappings < 0) {
1037c478bd9Sstevel@tonic-gate dprintf("Pscantext: failed to read map file: %s\n",
1047c478bd9Sstevel@tonic-gate strerror(errno));
1057c478bd9Sstevel@tonic-gate free(prbuf);
1067c478bd9Sstevel@tonic-gate (void) close(mapfd);
1077c478bd9Sstevel@tonic-gate return (-1);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate nmappings /= sizeof (prmap_t);
1107c478bd9Sstevel@tonic-gate if (nmappings < nmap) /* we read them all */
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate /* allocate a bigger buffer */
1137c478bd9Sstevel@tonic-gate free(prbuf);
1147c478bd9Sstevel@tonic-gate nmap *= 2;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate (void) close(mapfd);
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * Scan each executable mapping looking for a syscall instruction.
1207c478bd9Sstevel@tonic-gate * In dynamically linked executables, syscall instructions are
1217c478bd9Sstevel@tonic-gate * typically only found in shared libraries. Because shared libraries
1227c478bd9Sstevel@tonic-gate * are most often mapped at the top of the address space, we minimize
1237c478bd9Sstevel@tonic-gate * our expected search time by starting at the last mapping and working
1247c478bd9Sstevel@tonic-gate * our way down to the first mapping.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate for (pdp = &prbuf[nmappings - 1]; sysaddr == 0 && syspri != 1 &&
1277c478bd9Sstevel@tonic-gate pdp >= prbuf; pdp--) {
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate offset = (off_t)pdp->pr_vaddr; /* beginning of text */
1307c478bd9Sstevel@tonic-gate endoff = offset + pdp->pr_size;
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* avoid non-EXEC mappings; avoid the stack and heap */
1337c478bd9Sstevel@tonic-gate if ((pdp->pr_mflags&MA_EXEC) == 0 ||
1347c478bd9Sstevel@tonic-gate (endoff > P->status.pr_stkbase &&
1357c478bd9Sstevel@tonic-gate offset < P->status.pr_stkbase + P->status.pr_stksize) ||
1367c478bd9Sstevel@tonic-gate (endoff > P->status.pr_brkbase &&
1377c478bd9Sstevel@tonic-gate offset < P->status.pr_brkbase + P->status.pr_brksize))
1387c478bd9Sstevel@tonic-gate continue;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate (void) lseek(P->asfd, (off_t)offset, 0);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate if ((nbytes = read(P->asfd, buf, 2*BLKSIZE)) <= 0)
1437c478bd9Sstevel@tonic-gate continue;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (nbytes < BLKSIZE)
1467c478bd9Sstevel@tonic-gate n2bytes = 0;
1477c478bd9Sstevel@tonic-gate else {
1487c478bd9Sstevel@tonic-gate n2bytes = nbytes - BLKSIZE;
1497c478bd9Sstevel@tonic-gate nbytes = BLKSIZE;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate p = (uchar_t *)buf;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* search text for a SYSCALL instruction */
1557c478bd9Sstevel@tonic-gate while (sysaddr == 0 && syspri != 1 && offset < endoff) {
1567c478bd9Sstevel@tonic-gate if (nbytes <= 0) { /* shift buffers */
1577c478bd9Sstevel@tonic-gate if ((nbytes = n2bytes) <= 0)
1587c478bd9Sstevel@tonic-gate break;
1597c478bd9Sstevel@tonic-gate (void) memcpy(buf,
1607c478bd9Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])],
1617c478bd9Sstevel@tonic-gate nbytes);
1627c478bd9Sstevel@tonic-gate n2bytes = 0;
1637c478bd9Sstevel@tonic-gate p = (uchar_t *)buf;
1647c478bd9Sstevel@tonic-gate if (nbytes == BLKSIZE &&
1657c478bd9Sstevel@tonic-gate offset + BLKSIZE < endoff)
1667c478bd9Sstevel@tonic-gate n2bytes = read(P->asfd,
1677c478bd9Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])],
1687c478bd9Sstevel@tonic-gate BLKSIZE);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if (syspri = Pissyscall_text(P, p, nbytes))
1727c478bd9Sstevel@tonic-gate sysaddr = offset;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate p += sizeof (instr_t);
1757c478bd9Sstevel@tonic-gate offset += sizeof (instr_t);
1767c478bd9Sstevel@tonic-gate nbytes -= sizeof (instr_t);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1801a7c1b72Smws free(prbuf);
1811a7c1b72Smws
1827c478bd9Sstevel@tonic-gate if ((P->sysaddr = sysaddr) != 0)
1837c478bd9Sstevel@tonic-gate return (0);
1847c478bd9Sstevel@tonic-gate else
1857c478bd9Sstevel@tonic-gate return (-1);
1867c478bd9Sstevel@tonic-gate }
187