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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*265Smws * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate #include <fcntl.h> 330Sstevel@tonic-gate #include <unistd.h> 340Sstevel@tonic-gate #include <string.h> 350Sstevel@tonic-gate #include <errno.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 480Sstevel@tonic-gate Pscantext(struct ps_prochandle *P) 490Sstevel@tonic-gate { 500Sstevel@tonic-gate char mapfile[100]; 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 */ 830Sstevel@tonic-gate (void) sprintf(mapfile, "/proc/%d/map", (int)P->pid); 840Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0) { 850Sstevel@tonic-gate dprintf("failed to open %s: %s\n", mapfile, strerror(errno)); 860Sstevel@tonic-gate return (-1); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* allocate a plausible initial buffer size */ 900Sstevel@tonic-gate nmap = 50; 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* read all the map structures, allocating more space as needed */ 930Sstevel@tonic-gate for (;;) { 940Sstevel@tonic-gate prbuf = malloc(nmap * sizeof (prmap_t)); 950Sstevel@tonic-gate if (prbuf == NULL) { 960Sstevel@tonic-gate dprintf("Pscantext: failed to allocate buffer\n"); 970Sstevel@tonic-gate (void) close(mapfd); 980Sstevel@tonic-gate return (-1); 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate nmappings = pread(mapfd, prbuf, nmap * sizeof (prmap_t), 0L); 1010Sstevel@tonic-gate if (nmappings < 0) { 1020Sstevel@tonic-gate dprintf("Pscantext: failed to read map file: %s\n", 1030Sstevel@tonic-gate strerror(errno)); 1040Sstevel@tonic-gate free(prbuf); 1050Sstevel@tonic-gate (void) close(mapfd); 1060Sstevel@tonic-gate return (-1); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate nmappings /= sizeof (prmap_t); 1090Sstevel@tonic-gate if (nmappings < nmap) /* we read them all */ 1100Sstevel@tonic-gate break; 1110Sstevel@tonic-gate /* allocate a bigger buffer */ 1120Sstevel@tonic-gate free(prbuf); 1130Sstevel@tonic-gate nmap *= 2; 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate (void) close(mapfd); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Scan each executable mapping looking for a syscall instruction. 1190Sstevel@tonic-gate * In dynamically linked executables, syscall instructions are 1200Sstevel@tonic-gate * typically only found in shared libraries. Because shared libraries 1210Sstevel@tonic-gate * are most often mapped at the top of the address space, we minimize 1220Sstevel@tonic-gate * our expected search time by starting at the last mapping and working 1230Sstevel@tonic-gate * our way down to the first mapping. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate for (pdp = &prbuf[nmappings - 1]; sysaddr == 0 && syspri != 1 && 1260Sstevel@tonic-gate pdp >= prbuf; pdp--) { 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate offset = (off_t)pdp->pr_vaddr; /* beginning of text */ 1290Sstevel@tonic-gate endoff = offset + pdp->pr_size; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* avoid non-EXEC mappings; avoid the stack and heap */ 1320Sstevel@tonic-gate if ((pdp->pr_mflags&MA_EXEC) == 0 || 1330Sstevel@tonic-gate (endoff > P->status.pr_stkbase && 1340Sstevel@tonic-gate offset < P->status.pr_stkbase + P->status.pr_stksize) || 1350Sstevel@tonic-gate (endoff > P->status.pr_brkbase && 1360Sstevel@tonic-gate offset < P->status.pr_brkbase + P->status.pr_brksize)) 1370Sstevel@tonic-gate continue; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate (void) lseek(P->asfd, (off_t)offset, 0); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate if ((nbytes = read(P->asfd, buf, 2*BLKSIZE)) <= 0) 1420Sstevel@tonic-gate continue; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (nbytes < BLKSIZE) 1450Sstevel@tonic-gate n2bytes = 0; 1460Sstevel@tonic-gate else { 1470Sstevel@tonic-gate n2bytes = nbytes - BLKSIZE; 1480Sstevel@tonic-gate nbytes = BLKSIZE; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate p = (uchar_t *)buf; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* search text for a SYSCALL instruction */ 1540Sstevel@tonic-gate while (sysaddr == 0 && syspri != 1 && offset < endoff) { 1550Sstevel@tonic-gate if (nbytes <= 0) { /* shift buffers */ 1560Sstevel@tonic-gate if ((nbytes = n2bytes) <= 0) 1570Sstevel@tonic-gate break; 1580Sstevel@tonic-gate (void) memcpy(buf, 1590Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])], 1600Sstevel@tonic-gate nbytes); 1610Sstevel@tonic-gate n2bytes = 0; 1620Sstevel@tonic-gate p = (uchar_t *)buf; 1630Sstevel@tonic-gate if (nbytes == BLKSIZE && 1640Sstevel@tonic-gate offset + BLKSIZE < endoff) 1650Sstevel@tonic-gate n2bytes = read(P->asfd, 1660Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])], 1670Sstevel@tonic-gate BLKSIZE); 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate if (syspri = Pissyscall_text(P, p, nbytes)) 1710Sstevel@tonic-gate sysaddr = offset; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate p += sizeof (instr_t); 1740Sstevel@tonic-gate offset += sizeof (instr_t); 1750Sstevel@tonic-gate nbytes -= sizeof (instr_t); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 179*265Smws free(prbuf); 180*265Smws 1810Sstevel@tonic-gate if ((P->sysaddr = sysaddr) != 0) 1820Sstevel@tonic-gate return (0); 1830Sstevel@tonic-gate else 1840Sstevel@tonic-gate return (-1); 1850Sstevel@tonic-gate } 186