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 /* 230Sstevel@tonic-gate * 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 <stdlib.h> 300Sstevel@tonic-gate #include <libelf.h> 310Sstevel@tonic-gate #include <libgen.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <strings.h> 340Sstevel@tonic-gate #include <errno.h> 350Sstevel@tonic-gate #include <sys/sysmacros.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include "Pcontrol.h" 380Sstevel@tonic-gate 390Sstevel@tonic-gate static ssize_t 400Sstevel@tonic-gate Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr) 410Sstevel@tonic-gate { 420Sstevel@tonic-gate size_t resid = n; 430Sstevel@tonic-gate 440Sstevel@tonic-gate while (resid > 0) { 450Sstevel@tonic-gate map_info_t *mp; 460Sstevel@tonic-gate uintptr_t mapoff; 470Sstevel@tonic-gate ssize_t len; 480Sstevel@tonic-gate off64_t off; 490Sstevel@tonic-gate 500Sstevel@tonic-gate if ((mp = Paddr2mptr(P, addr)) == NULL) 510Sstevel@tonic-gate break; 520Sstevel@tonic-gate 530Sstevel@tonic-gate mapoff = addr - mp->map_pmap.pr_vaddr; 540Sstevel@tonic-gate len = MIN(resid, mp->map_pmap.pr_size - mapoff); 550Sstevel@tonic-gate off = mp->map_offset + mapoff; 560Sstevel@tonic-gate 570Sstevel@tonic-gate if ((len = pread64(P->asfd, buf, len, off)) <= 0) 580Sstevel@tonic-gate break; 590Sstevel@tonic-gate 600Sstevel@tonic-gate resid -= len; 610Sstevel@tonic-gate addr += len; 620Sstevel@tonic-gate buf = (char *)buf + len; 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate return (n - resid); 660Sstevel@tonic-gate } 670Sstevel@tonic-gate 680Sstevel@tonic-gate /*ARGSUSED*/ 690Sstevel@tonic-gate static ssize_t 700Sstevel@tonic-gate Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr) 710Sstevel@tonic-gate { 720Sstevel@tonic-gate errno = EIO; 730Sstevel@tonic-gate return (-1); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate static const ps_rwops_t P_idle_ops = { 770Sstevel@tonic-gate Pread_idle, 780Sstevel@tonic-gate Pwrite_idle 790Sstevel@tonic-gate }; 800Sstevel@tonic-gate 810Sstevel@tonic-gate static int 820Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate prmap_t pmap; 850Sstevel@tonic-gate 860Sstevel@tonic-gate dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n", 870Sstevel@tonic-gate (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz, 880Sstevel@tonic-gate (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset); 890Sstevel@tonic-gate 900Sstevel@tonic-gate pmap.pr_vaddr = (uintptr_t)php->p_vaddr; 910Sstevel@tonic-gate pmap.pr_size = php->p_filesz; 920Sstevel@tonic-gate (void) strncpy(pmap.pr_mapname, fp->file_pname, 930Sstevel@tonic-gate sizeof (pmap.pr_mapname)); 940Sstevel@tonic-gate pmap.pr_offset = php->p_offset; 950Sstevel@tonic-gate 960Sstevel@tonic-gate pmap.pr_mflags = 0; 970Sstevel@tonic-gate if (php->p_flags & PF_R) 980Sstevel@tonic-gate pmap.pr_mflags |= MA_READ; 990Sstevel@tonic-gate if (php->p_flags & PF_W) 1000Sstevel@tonic-gate pmap.pr_mflags |= MA_WRITE; 1010Sstevel@tonic-gate if (php->p_flags & PF_X) 1020Sstevel@tonic-gate pmap.pr_mflags |= MA_EXEC; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate pmap.pr_pagesize = 0; 1050Sstevel@tonic-gate pmap.pr_shmid = -1; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate return (Padd_mapping(P, php->p_offset, fp, &pmap)); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate struct ps_prochandle * 1110Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate struct ps_prochandle *P = NULL; 1140Sstevel@tonic-gate GElf_Ehdr ehdr; 1150Sstevel@tonic-gate Elf *elf = NULL; 116*942Sahl size_t phnum; 1170Sstevel@tonic-gate file_info_t *fp = NULL; 1180Sstevel@tonic-gate int fd; 1190Sstevel@tonic-gate int i; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate if ((fd = open64(fname, O_RDONLY)) < 0) { 1220Sstevel@tonic-gate dprintf("couldn't open file"); 1230Sstevel@tonic-gate *perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE; 1240Sstevel@tonic-gate return (NULL); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1280Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf"); 1290Sstevel@tonic-gate *perr = G_ELF; 1300Sstevel@tonic-gate goto err; 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) { 1340Sstevel@tonic-gate *perr = G_STRANGE; 1350Sstevel@tonic-gate goto err; 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 1390Sstevel@tonic-gate P->state = PS_IDLE; 1400Sstevel@tonic-gate P->pid = (pid_t)-1; 1410Sstevel@tonic-gate P->asfd = fd; 1420Sstevel@tonic-gate P->ctlfd = -1; 1430Sstevel@tonic-gate P->statfd = -1; 1440Sstevel@tonic-gate P->agentctlfd = -1; 1450Sstevel@tonic-gate P->agentstatfd = -1; 1460Sstevel@tonic-gate P->info_valid = -1; 1470Sstevel@tonic-gate P->ops = &P_idle_ops; 1480Sstevel@tonic-gate Pinitsym(P); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1510Sstevel@tonic-gate *perr = G_ELF; 1520Sstevel@tonic-gate return (NULL); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Construct a file_info_t that corresponds to this file. 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate if ((fp = calloc(1, sizeof (file_info_t))) == NULL) { 1590Sstevel@tonic-gate *perr = G_STRANGE; 1600Sstevel@tonic-gate goto err; 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) { 1640Sstevel@tonic-gate *perr = G_STRANGE; 1650Sstevel@tonic-gate goto err; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if (*fname == '/') { 1690Sstevel@tonic-gate (void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname)); 1700Sstevel@tonic-gate } else { 1710Sstevel@tonic-gate size_t sz; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) == 1740Sstevel@tonic-gate NULL) { 1750Sstevel@tonic-gate *perr = G_STRANGE; 1760Sstevel@tonic-gate goto err; 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate sz = strlen(fp->file_pname); 1800Sstevel@tonic-gate (void) snprintf(&fp->file_pname[sz], 1810Sstevel@tonic-gate sizeof (fp->file_pname) - sz, "/%s", fname); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate fp->file_fd = fd; 1850Sstevel@tonic-gate fp->file_lo->rl_lmident = LM_ID_BASE; 1860Sstevel@tonic-gate fp->file_lname = strdup(fp->file_pname); 1870Sstevel@tonic-gate fp->file_lbase = basename(fp->file_lname); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate P->execname = strdup(fp->file_pname); 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate P->num_files++; 1920Sstevel@tonic-gate list_link(fp, &P->file_head); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 1950Sstevel@tonic-gate *perr = G_STRANGE; 1960Sstevel@tonic-gate goto err; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 199*942Sahl if (elf_getphnum(elf, &phnum) == 0) { 200*942Sahl *perr = G_STRANGE; 201*942Sahl goto err; 202*942Sahl } 203*942Sahl 204*942Sahl dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * Sift through the program headers making the relevant maps. 2080Sstevel@tonic-gate */ 209*942Sahl for (i = 0; i < phnum; i++) { 2100Sstevel@tonic-gate GElf_Phdr phdr, *php; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) { 2130Sstevel@tonic-gate *perr = G_STRANGE; 2140Sstevel@tonic-gate goto err; 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (php->p_type != PT_LOAD) 2180Sstevel@tonic-gate continue; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate if (idle_add_mapping(P, php, fp) != 0) { 2210Sstevel@tonic-gate *perr = G_STRANGE; 2220Sstevel@tonic-gate goto err; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate Psort_mappings(P); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate (void) elf_end(elf); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate P->map_exec = fp->file_map; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate P->status.pr_flags = PR_STOPPED; 2320Sstevel@tonic-gate P->status.pr_nlwp = 0; 2330Sstevel@tonic-gate P->status.pr_pid = (pid_t)-1; 2340Sstevel@tonic-gate P->status.pr_ppid = (pid_t)-1; 2350Sstevel@tonic-gate P->status.pr_pgid = (pid_t)-1; 2360Sstevel@tonic-gate P->status.pr_sid = (pid_t)-1; 2370Sstevel@tonic-gate P->status.pr_taskid = (taskid_t)-1; 2380Sstevel@tonic-gate P->status.pr_projid = (projid_t)-1; 2390Sstevel@tonic-gate switch (ehdr.e_ident[EI_CLASS]) { 2400Sstevel@tonic-gate case ELFCLASS32: 2410Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_ILP32; 2420Sstevel@tonic-gate break; 2430Sstevel@tonic-gate case ELFCLASS64: 2440Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_LP64; 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate default: 2470Sstevel@tonic-gate *perr = G_FORMAT; 2480Sstevel@tonic-gate goto err; 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * The file and map lists are complete, and will never need to be 2530Sstevel@tonic-gate * adjusted. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate P->info_valid = 1; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate return (P); 2580Sstevel@tonic-gate err: 2590Sstevel@tonic-gate (void) close(fd); 2600Sstevel@tonic-gate if (P != NULL) 2610Sstevel@tonic-gate Pfree(P); 2620Sstevel@tonic-gate if (elf != NULL) 2630Sstevel@tonic-gate (void) elf_end(elf); 2640Sstevel@tonic-gate return (NULL); 2650Sstevel@tonic-gate } 266