1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <string.h> 32*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 33*7c478bd9Sstevel@tonic-gate #include <unistd.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <errno.h> 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include "libproc.h" 38*7c478bd9Sstevel@tonic-gate #include "Pcontrol.h" 39*7c478bd9Sstevel@tonic-gate #include "Pisadep.h" 40*7c478bd9Sstevel@tonic-gate #include "Putil.h" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #define BLKSIZE (8 * 1024) 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* 45*7c478bd9Sstevel@tonic-gate * Look for a SYSCALL instruction in the process's address space. 46*7c478bd9Sstevel@tonic-gate */ 47*7c478bd9Sstevel@tonic-gate int 48*7c478bd9Sstevel@tonic-gate Pscantext(struct ps_prochandle *P) 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate char mapfile[100]; 51*7c478bd9Sstevel@tonic-gate int mapfd; 52*7c478bd9Sstevel@tonic-gate off_t offset; /* offset in text section */ 53*7c478bd9Sstevel@tonic-gate off_t endoff; /* ending offset in text section */ 54*7c478bd9Sstevel@tonic-gate uintptr_t sysaddr; /* address of SYSCALL instruction */ 55*7c478bd9Sstevel@tonic-gate int syspri; /* priority of SYSCALL instruction */ 56*7c478bd9Sstevel@tonic-gate int nbytes; /* number of bytes in buffer */ 57*7c478bd9Sstevel@tonic-gate int n2bytes; /* number of bytes in second buffer */ 58*7c478bd9Sstevel@tonic-gate int nmappings; /* current number of mappings */ 59*7c478bd9Sstevel@tonic-gate prmap_t *pdp; /* pointer to map descriptor */ 60*7c478bd9Sstevel@tonic-gate prmap_t *prbuf; /* buffer for map descriptors */ 61*7c478bd9Sstevel@tonic-gate unsigned nmap; /* number of map descriptors */ 62*7c478bd9Sstevel@tonic-gate uint32_t buf[2 * BLKSIZE / sizeof (uint32_t)]; /* text buffer */ 63*7c478bd9Sstevel@tonic-gate uchar_t *p; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate /* try the most recently-seen syscall address */ 66*7c478bd9Sstevel@tonic-gate syspri = 0; 67*7c478bd9Sstevel@tonic-gate sysaddr = 0; 68*7c478bd9Sstevel@tonic-gate if (P->sysaddr != 0 && 69*7c478bd9Sstevel@tonic-gate (syspri = Pissyscall(P, P->sysaddr))) 70*7c478bd9Sstevel@tonic-gate sysaddr = P->sysaddr; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* try the previous instruction */ 73*7c478bd9Sstevel@tonic-gate if (sysaddr == 0 || syspri != 1) 74*7c478bd9Sstevel@tonic-gate syspri = Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], 75*7c478bd9Sstevel@tonic-gate &sysaddr); 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate if (sysaddr != 0 && syspri == 1) { 78*7c478bd9Sstevel@tonic-gate P->sysaddr = sysaddr; 79*7c478bd9Sstevel@tonic-gate return (0); 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* open the /proc/<pid>/map file */ 83*7c478bd9Sstevel@tonic-gate (void) sprintf(mapfile, "/proc/%d/map", (int)P->pid); 84*7c478bd9Sstevel@tonic-gate if ((mapfd = open(mapfile, O_RDONLY)) < 0) { 85*7c478bd9Sstevel@tonic-gate dprintf("failed to open %s: %s\n", mapfile, strerror(errno)); 86*7c478bd9Sstevel@tonic-gate return (-1); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate /* allocate a plausible initial buffer size */ 90*7c478bd9Sstevel@tonic-gate nmap = 50; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /* read all the map structures, allocating more space as needed */ 93*7c478bd9Sstevel@tonic-gate for (;;) { 94*7c478bd9Sstevel@tonic-gate prbuf = malloc(nmap * sizeof (prmap_t)); 95*7c478bd9Sstevel@tonic-gate if (prbuf == NULL) { 96*7c478bd9Sstevel@tonic-gate dprintf("Pscantext: failed to allocate buffer\n"); 97*7c478bd9Sstevel@tonic-gate (void) close(mapfd); 98*7c478bd9Sstevel@tonic-gate return (-1); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate nmappings = pread(mapfd, prbuf, nmap * sizeof (prmap_t), 0L); 101*7c478bd9Sstevel@tonic-gate if (nmappings < 0) { 102*7c478bd9Sstevel@tonic-gate dprintf("Pscantext: failed to read map file: %s\n", 103*7c478bd9Sstevel@tonic-gate strerror(errno)); 104*7c478bd9Sstevel@tonic-gate free(prbuf); 105*7c478bd9Sstevel@tonic-gate (void) close(mapfd); 106*7c478bd9Sstevel@tonic-gate return (-1); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate nmappings /= sizeof (prmap_t); 109*7c478bd9Sstevel@tonic-gate if (nmappings < nmap) /* we read them all */ 110*7c478bd9Sstevel@tonic-gate break; 111*7c478bd9Sstevel@tonic-gate /* allocate a bigger buffer */ 112*7c478bd9Sstevel@tonic-gate free(prbuf); 113*7c478bd9Sstevel@tonic-gate nmap *= 2; 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate (void) close(mapfd); 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /* 118*7c478bd9Sstevel@tonic-gate * Scan each executable mapping looking for a syscall instruction. 119*7c478bd9Sstevel@tonic-gate * In dynamically linked executables, syscall instructions are 120*7c478bd9Sstevel@tonic-gate * typically only found in shared libraries. Because shared libraries 121*7c478bd9Sstevel@tonic-gate * are most often mapped at the top of the address space, we minimize 122*7c478bd9Sstevel@tonic-gate * our expected search time by starting at the last mapping and working 123*7c478bd9Sstevel@tonic-gate * our way down to the first mapping. 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate for (pdp = &prbuf[nmappings - 1]; sysaddr == 0 && syspri != 1 && 126*7c478bd9Sstevel@tonic-gate pdp >= prbuf; pdp--) { 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate offset = (off_t)pdp->pr_vaddr; /* beginning of text */ 129*7c478bd9Sstevel@tonic-gate endoff = offset + pdp->pr_size; 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate /* avoid non-EXEC mappings; avoid the stack and heap */ 132*7c478bd9Sstevel@tonic-gate if ((pdp->pr_mflags&MA_EXEC) == 0 || 133*7c478bd9Sstevel@tonic-gate (endoff > P->status.pr_stkbase && 134*7c478bd9Sstevel@tonic-gate offset < P->status.pr_stkbase + P->status.pr_stksize) || 135*7c478bd9Sstevel@tonic-gate (endoff > P->status.pr_brkbase && 136*7c478bd9Sstevel@tonic-gate offset < P->status.pr_brkbase + P->status.pr_brksize)) 137*7c478bd9Sstevel@tonic-gate continue; 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate (void) lseek(P->asfd, (off_t)offset, 0); 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate if ((nbytes = read(P->asfd, buf, 2*BLKSIZE)) <= 0) 142*7c478bd9Sstevel@tonic-gate continue; 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate if (nbytes < BLKSIZE) 145*7c478bd9Sstevel@tonic-gate n2bytes = 0; 146*7c478bd9Sstevel@tonic-gate else { 147*7c478bd9Sstevel@tonic-gate n2bytes = nbytes - BLKSIZE; 148*7c478bd9Sstevel@tonic-gate nbytes = BLKSIZE; 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate p = (uchar_t *)buf; 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate /* search text for a SYSCALL instruction */ 154*7c478bd9Sstevel@tonic-gate while (sysaddr == 0 && syspri != 1 && offset < endoff) { 155*7c478bd9Sstevel@tonic-gate if (nbytes <= 0) { /* shift buffers */ 156*7c478bd9Sstevel@tonic-gate if ((nbytes = n2bytes) <= 0) 157*7c478bd9Sstevel@tonic-gate break; 158*7c478bd9Sstevel@tonic-gate (void) memcpy(buf, 159*7c478bd9Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])], 160*7c478bd9Sstevel@tonic-gate nbytes); 161*7c478bd9Sstevel@tonic-gate n2bytes = 0; 162*7c478bd9Sstevel@tonic-gate p = (uchar_t *)buf; 163*7c478bd9Sstevel@tonic-gate if (nbytes == BLKSIZE && 164*7c478bd9Sstevel@tonic-gate offset + BLKSIZE < endoff) 165*7c478bd9Sstevel@tonic-gate n2bytes = read(P->asfd, 166*7c478bd9Sstevel@tonic-gate &buf[BLKSIZE / sizeof (buf[0])], 167*7c478bd9Sstevel@tonic-gate BLKSIZE); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate if (syspri = Pissyscall_text(P, p, nbytes)) 171*7c478bd9Sstevel@tonic-gate sysaddr = offset; 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate p += sizeof (instr_t); 174*7c478bd9Sstevel@tonic-gate offset += sizeof (instr_t); 175*7c478bd9Sstevel@tonic-gate nbytes -= sizeof (instr_t); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate if ((P->sysaddr = sysaddr) != 0) 180*7c478bd9Sstevel@tonic-gate return (0); 181*7c478bd9Sstevel@tonic-gate else 182*7c478bd9Sstevel@tonic-gate return (-1); 183*7c478bd9Sstevel@tonic-gate } 184