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 57675SEdward.Pilatowicz@Sun.COM * Common Development and Distribution License (the "License"). 67675SEdward.Pilatowicz@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*9900SAli.Bahrami@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <stdlib.h> 270Sstevel@tonic-gate #include <libelf.h> 280Sstevel@tonic-gate #include <libgen.h> 290Sstevel@tonic-gate #include <string.h> 300Sstevel@tonic-gate #include <strings.h> 310Sstevel@tonic-gate #include <errno.h> 320Sstevel@tonic-gate #include <sys/sysmacros.h> 330Sstevel@tonic-gate 347675SEdward.Pilatowicz@Sun.COM #include "libproc.h" 350Sstevel@tonic-gate #include "Pcontrol.h" 360Sstevel@tonic-gate 370Sstevel@tonic-gate static ssize_t 380Sstevel@tonic-gate Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr) 390Sstevel@tonic-gate { 400Sstevel@tonic-gate size_t resid = n; 410Sstevel@tonic-gate 420Sstevel@tonic-gate while (resid > 0) { 430Sstevel@tonic-gate map_info_t *mp; 440Sstevel@tonic-gate uintptr_t mapoff; 450Sstevel@tonic-gate ssize_t len; 460Sstevel@tonic-gate off64_t off; 470Sstevel@tonic-gate 480Sstevel@tonic-gate if ((mp = Paddr2mptr(P, addr)) == NULL) 490Sstevel@tonic-gate break; 500Sstevel@tonic-gate 510Sstevel@tonic-gate mapoff = addr - mp->map_pmap.pr_vaddr; 520Sstevel@tonic-gate len = MIN(resid, mp->map_pmap.pr_size - mapoff); 530Sstevel@tonic-gate off = mp->map_offset + mapoff; 540Sstevel@tonic-gate 550Sstevel@tonic-gate if ((len = pread64(P->asfd, buf, len, off)) <= 0) 560Sstevel@tonic-gate break; 570Sstevel@tonic-gate 580Sstevel@tonic-gate resid -= len; 590Sstevel@tonic-gate addr += len; 600Sstevel@tonic-gate buf = (char *)buf + len; 610Sstevel@tonic-gate } 620Sstevel@tonic-gate 630Sstevel@tonic-gate return (n - resid); 640Sstevel@tonic-gate } 650Sstevel@tonic-gate 660Sstevel@tonic-gate /*ARGSUSED*/ 670Sstevel@tonic-gate static ssize_t 680Sstevel@tonic-gate Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate errno = EIO; 710Sstevel@tonic-gate return (-1); 720Sstevel@tonic-gate } 730Sstevel@tonic-gate 740Sstevel@tonic-gate static const ps_rwops_t P_idle_ops = { 750Sstevel@tonic-gate Pread_idle, 760Sstevel@tonic-gate Pwrite_idle 770Sstevel@tonic-gate }; 780Sstevel@tonic-gate 790Sstevel@tonic-gate static int 800Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp) 810Sstevel@tonic-gate { 820Sstevel@tonic-gate prmap_t pmap; 830Sstevel@tonic-gate 840Sstevel@tonic-gate dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n", 850Sstevel@tonic-gate (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz, 860Sstevel@tonic-gate (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset); 870Sstevel@tonic-gate 880Sstevel@tonic-gate pmap.pr_vaddr = (uintptr_t)php->p_vaddr; 890Sstevel@tonic-gate pmap.pr_size = php->p_filesz; 900Sstevel@tonic-gate (void) strncpy(pmap.pr_mapname, fp->file_pname, 910Sstevel@tonic-gate sizeof (pmap.pr_mapname)); 920Sstevel@tonic-gate pmap.pr_offset = php->p_offset; 930Sstevel@tonic-gate 940Sstevel@tonic-gate pmap.pr_mflags = 0; 950Sstevel@tonic-gate if (php->p_flags & PF_R) 960Sstevel@tonic-gate pmap.pr_mflags |= MA_READ; 970Sstevel@tonic-gate if (php->p_flags & PF_W) 980Sstevel@tonic-gate pmap.pr_mflags |= MA_WRITE; 990Sstevel@tonic-gate if (php->p_flags & PF_X) 1000Sstevel@tonic-gate pmap.pr_mflags |= MA_EXEC; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate pmap.pr_pagesize = 0; 1030Sstevel@tonic-gate pmap.pr_shmid = -1; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate return (Padd_mapping(P, php->p_offset, fp, &pmap)); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate struct ps_prochandle * 1090Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr) 1100Sstevel@tonic-gate { 1110Sstevel@tonic-gate struct ps_prochandle *P = NULL; 1127675SEdward.Pilatowicz@Sun.COM char buf[PATH_MAX]; 1130Sstevel@tonic-gate GElf_Ehdr ehdr; 1140Sstevel@tonic-gate Elf *elf = NULL; 115942Sahl size_t phnum; 1160Sstevel@tonic-gate file_info_t *fp = NULL; 1170Sstevel@tonic-gate int fd; 1180Sstevel@tonic-gate int i; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate if ((fd = open64(fname, O_RDONLY)) < 0) { 1210Sstevel@tonic-gate dprintf("couldn't open file"); 1220Sstevel@tonic-gate *perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE; 1230Sstevel@tonic-gate return (NULL); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1270Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf"); 1280Sstevel@tonic-gate *perr = G_ELF; 1290Sstevel@tonic-gate goto err; 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) { 1330Sstevel@tonic-gate *perr = G_STRANGE; 1340Sstevel@tonic-gate goto err; 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 1380Sstevel@tonic-gate P->state = PS_IDLE; 1390Sstevel@tonic-gate P->pid = (pid_t)-1; 1400Sstevel@tonic-gate P->asfd = fd; 1410Sstevel@tonic-gate P->ctlfd = -1; 1420Sstevel@tonic-gate P->statfd = -1; 1430Sstevel@tonic-gate P->agentctlfd = -1; 1440Sstevel@tonic-gate P->agentstatfd = -1; 1450Sstevel@tonic-gate P->info_valid = -1; 1460Sstevel@tonic-gate P->ops = &P_idle_ops; 1470Sstevel@tonic-gate Pinitsym(P); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1500Sstevel@tonic-gate *perr = G_ELF; 1510Sstevel@tonic-gate return (NULL); 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Construct a file_info_t that corresponds to this file. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate if ((fp = calloc(1, sizeof (file_info_t))) == NULL) { 1580Sstevel@tonic-gate *perr = G_STRANGE; 1590Sstevel@tonic-gate goto err; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) { 1630Sstevel@tonic-gate *perr = G_STRANGE; 1640Sstevel@tonic-gate goto err; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (*fname == '/') { 1680Sstevel@tonic-gate (void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname)); 1690Sstevel@tonic-gate } else { 1700Sstevel@tonic-gate size_t sz; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) == 1730Sstevel@tonic-gate NULL) { 1740Sstevel@tonic-gate *perr = G_STRANGE; 1750Sstevel@tonic-gate goto err; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate sz = strlen(fp->file_pname); 1790Sstevel@tonic-gate (void) snprintf(&fp->file_pname[sz], 1800Sstevel@tonic-gate sizeof (fp->file_pname) - sz, "/%s", fname); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate fp->file_fd = fd; 1840Sstevel@tonic-gate fp->file_lo->rl_lmident = LM_ID_BASE; 1857675SEdward.Pilatowicz@Sun.COM if ((fp->file_lname = strdup(fp->file_pname)) == NULL) { 1867675SEdward.Pilatowicz@Sun.COM *perr = G_STRANGE; 1877675SEdward.Pilatowicz@Sun.COM goto err; 1887675SEdward.Pilatowicz@Sun.COM } 1890Sstevel@tonic-gate fp->file_lbase = basename(fp->file_lname); 1900Sstevel@tonic-gate 1917675SEdward.Pilatowicz@Sun.COM if ((P->execname = strdup(fp->file_pname)) == NULL) { 1927675SEdward.Pilatowicz@Sun.COM *perr = G_STRANGE; 1937675SEdward.Pilatowicz@Sun.COM goto err; 1947675SEdward.Pilatowicz@Sun.COM } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate P->num_files++; 1970Sstevel@tonic-gate list_link(fp, &P->file_head); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 2000Sstevel@tonic-gate *perr = G_STRANGE; 2010Sstevel@tonic-gate goto err; 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate 204*9900SAli.Bahrami@Sun.COM if (elf_getphdrnum(elf, &phnum) == -1) { 205942Sahl *perr = G_STRANGE; 206942Sahl goto err; 207942Sahl } 208942Sahl 209942Sahl dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * Sift through the program headers making the relevant maps. 2130Sstevel@tonic-gate */ 214942Sahl for (i = 0; i < phnum; i++) { 2150Sstevel@tonic-gate GElf_Phdr phdr, *php; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) { 2180Sstevel@tonic-gate *perr = G_STRANGE; 2190Sstevel@tonic-gate goto err; 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (php->p_type != PT_LOAD) 2230Sstevel@tonic-gate continue; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if (idle_add_mapping(P, php, fp) != 0) { 2260Sstevel@tonic-gate *perr = G_STRANGE; 2270Sstevel@tonic-gate goto err; 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate Psort_mappings(P); 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate (void) elf_end(elf); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate P->map_exec = fp->file_map; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate P->status.pr_flags = PR_STOPPED; 2370Sstevel@tonic-gate P->status.pr_nlwp = 0; 2380Sstevel@tonic-gate P->status.pr_pid = (pid_t)-1; 2390Sstevel@tonic-gate P->status.pr_ppid = (pid_t)-1; 2400Sstevel@tonic-gate P->status.pr_pgid = (pid_t)-1; 2410Sstevel@tonic-gate P->status.pr_sid = (pid_t)-1; 2420Sstevel@tonic-gate P->status.pr_taskid = (taskid_t)-1; 2430Sstevel@tonic-gate P->status.pr_projid = (projid_t)-1; 2447675SEdward.Pilatowicz@Sun.COM P->status.pr_zoneid = (zoneid_t)-1; 2450Sstevel@tonic-gate switch (ehdr.e_ident[EI_CLASS]) { 2460Sstevel@tonic-gate case ELFCLASS32: 2470Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_ILP32; 2480Sstevel@tonic-gate break; 2490Sstevel@tonic-gate case ELFCLASS64: 2500Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_LP64; 2510Sstevel@tonic-gate break; 2520Sstevel@tonic-gate default: 2530Sstevel@tonic-gate *perr = G_FORMAT; 2540Sstevel@tonic-gate goto err; 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2587675SEdward.Pilatowicz@Sun.COM * Pfindobj() checks what zone a process is associated with, so 2597675SEdward.Pilatowicz@Sun.COM * we call it after initializing pr_zoneid to -1. This ensures 2607675SEdward.Pilatowicz@Sun.COM * we don't get associated with any zone on the system. 2617675SEdward.Pilatowicz@Sun.COM */ 2627675SEdward.Pilatowicz@Sun.COM if (Pfindobj(P, fp->file_lname, buf, sizeof (buf)) != NULL) { 2637675SEdward.Pilatowicz@Sun.COM free(P->execname); 2647675SEdward.Pilatowicz@Sun.COM P->execname = strdup(buf); 2657675SEdward.Pilatowicz@Sun.COM if ((fp->file_rname = strdup(buf)) != NULL) 2667675SEdward.Pilatowicz@Sun.COM fp->file_rbase = basename(fp->file_rname); 2677675SEdward.Pilatowicz@Sun.COM } 2687675SEdward.Pilatowicz@Sun.COM 2697675SEdward.Pilatowicz@Sun.COM /* 2700Sstevel@tonic-gate * The file and map lists are complete, and will never need to be 2710Sstevel@tonic-gate * adjusted. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate P->info_valid = 1; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate return (P); 2760Sstevel@tonic-gate err: 2770Sstevel@tonic-gate (void) close(fd); 2780Sstevel@tonic-gate if (P != NULL) 2790Sstevel@tonic-gate Pfree(P); 2800Sstevel@tonic-gate if (elf != NULL) 2810Sstevel@tonic-gate (void) elf_end(elf); 2820Sstevel@tonic-gate return (NULL); 2830Sstevel@tonic-gate } 284