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 5186f7fbfSEdward Pilatowicz * Common Development and Distribution License (the "License"). 6186f7fbfSEdward Pilatowicz * 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 /* 2262b628a6SAli Bahrami * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 25*2a12f85aSJeremy Jones /* 26*2a12f85aSJeremy Jones * Copyright (c) 2013 by Delphix. All rights reserved. 27*2a12f85aSJeremy Jones */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <libelf.h> 317c478bd9Sstevel@tonic-gate #include <libgen.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <strings.h> 347c478bd9Sstevel@tonic-gate #include <errno.h> 357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 367c478bd9Sstevel@tonic-gate 37186f7fbfSEdward Pilatowicz #include "libproc.h" 387c478bd9Sstevel@tonic-gate #include "Pcontrol.h" 397c478bd9Sstevel@tonic-gate 40*2a12f85aSJeremy Jones /*ARGSUSED*/ 417c478bd9Sstevel@tonic-gate static ssize_t 42*2a12f85aSJeremy Jones Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr, 43*2a12f85aSJeremy Jones void *data) 447c478bd9Sstevel@tonic-gate { 457c478bd9Sstevel@tonic-gate size_t resid = n; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate while (resid > 0) { 487c478bd9Sstevel@tonic-gate map_info_t *mp; 497c478bd9Sstevel@tonic-gate uintptr_t mapoff; 507c478bd9Sstevel@tonic-gate ssize_t len; 517c478bd9Sstevel@tonic-gate off64_t off; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate if ((mp = Paddr2mptr(P, addr)) == NULL) 547c478bd9Sstevel@tonic-gate break; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate mapoff = addr - mp->map_pmap.pr_vaddr; 577c478bd9Sstevel@tonic-gate len = MIN(resid, mp->map_pmap.pr_size - mapoff); 587c478bd9Sstevel@tonic-gate off = mp->map_offset + mapoff; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate if ((len = pread64(P->asfd, buf, len, off)) <= 0) 617c478bd9Sstevel@tonic-gate break; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate resid -= len; 647c478bd9Sstevel@tonic-gate addr += len; 657c478bd9Sstevel@tonic-gate buf = (char *)buf + len; 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate return (n - resid); 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 727c478bd9Sstevel@tonic-gate static ssize_t 73*2a12f85aSJeremy Jones Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr, 74*2a12f85aSJeremy Jones void *data) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate errno = EIO; 777c478bd9Sstevel@tonic-gate return (-1); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 80*2a12f85aSJeremy Jones /*ARGSUSED*/ 81*2a12f85aSJeremy Jones static int 82*2a12f85aSJeremy Jones Ppriv_idle(struct ps_prochandle *P, prpriv_t **pprv, void *data) 83*2a12f85aSJeremy Jones { 84*2a12f85aSJeremy Jones prpriv_t *pp; 85*2a12f85aSJeremy Jones 86*2a12f85aSJeremy Jones pp = proc_get_priv(P->pid); 87*2a12f85aSJeremy Jones if (pp == NULL) { 88*2a12f85aSJeremy Jones return (-1); 89*2a12f85aSJeremy Jones } 90*2a12f85aSJeremy Jones 91*2a12f85aSJeremy Jones *pprv = pp; 92*2a12f85aSJeremy Jones return (0); 93*2a12f85aSJeremy Jones } 94*2a12f85aSJeremy Jones 95*2a12f85aSJeremy Jones /* Default operations for the idl ops vector. */ 96*2a12f85aSJeremy Jones static void * 97*2a12f85aSJeremy Jones Pidle_voidp() 98*2a12f85aSJeremy Jones { 99*2a12f85aSJeremy Jones errno = ENODATA; 100*2a12f85aSJeremy Jones return (NULL); 101*2a12f85aSJeremy Jones } 102*2a12f85aSJeremy Jones 103*2a12f85aSJeremy Jones static int 104*2a12f85aSJeremy Jones Pidle_int() 105*2a12f85aSJeremy Jones { 106*2a12f85aSJeremy Jones errno = ENODATA; 107*2a12f85aSJeremy Jones return (-1); 108*2a12f85aSJeremy Jones } 109*2a12f85aSJeremy Jones 110*2a12f85aSJeremy Jones static const ps_ops_t P_idle_ops = { 111*2a12f85aSJeremy Jones .pop_pread = Pread_idle, 112*2a12f85aSJeremy Jones .pop_pwrite = Pwrite_idle, 113*2a12f85aSJeremy Jones .pop_cred = (pop_cred_t)Pidle_int, 114*2a12f85aSJeremy Jones .pop_priv = Ppriv_idle, 115*2a12f85aSJeremy Jones .pop_psinfo = (pop_psinfo_t)Pidle_voidp, 116*2a12f85aSJeremy Jones .pop_platform = (pop_platform_t)Pidle_voidp, 117*2a12f85aSJeremy Jones .pop_uname = (pop_uname_t)Pidle_int, 118*2a12f85aSJeremy Jones .pop_zonename = (pop_zonename_t)Pidle_voidp, 119*2a12f85aSJeremy Jones #if defined(__i386) || defined(__amd64) 120*2a12f85aSJeremy Jones .pop_ldt = (pop_ldt_t)Pidle_int 121*2a12f85aSJeremy Jones #endif 1227c478bd9Sstevel@tonic-gate }; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate static int 1257c478bd9Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate prmap_t pmap; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n", 1307c478bd9Sstevel@tonic-gate (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz, 1317c478bd9Sstevel@tonic-gate (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate pmap.pr_vaddr = (uintptr_t)php->p_vaddr; 1347c478bd9Sstevel@tonic-gate pmap.pr_size = php->p_filesz; 1357c478bd9Sstevel@tonic-gate (void) strncpy(pmap.pr_mapname, fp->file_pname, 1367c478bd9Sstevel@tonic-gate sizeof (pmap.pr_mapname)); 1377c478bd9Sstevel@tonic-gate pmap.pr_offset = php->p_offset; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate pmap.pr_mflags = 0; 1407c478bd9Sstevel@tonic-gate if (php->p_flags & PF_R) 1417c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_READ; 1427c478bd9Sstevel@tonic-gate if (php->p_flags & PF_W) 1437c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_WRITE; 1447c478bd9Sstevel@tonic-gate if (php->p_flags & PF_X) 1457c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_EXEC; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate pmap.pr_pagesize = 0; 1487c478bd9Sstevel@tonic-gate pmap.pr_shmid = -1; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate return (Padd_mapping(P, php->p_offset, fp, &pmap)); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate struct ps_prochandle * 1547c478bd9Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate struct ps_prochandle *P = NULL; 157186f7fbfSEdward Pilatowicz char buf[PATH_MAX]; 1587c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 1597c478bd9Sstevel@tonic-gate Elf *elf = NULL; 16030da1432Sahl size_t phnum; 1617c478bd9Sstevel@tonic-gate file_info_t *fp = NULL; 1627c478bd9Sstevel@tonic-gate int fd; 1637c478bd9Sstevel@tonic-gate int i; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if ((fd = open64(fname, O_RDONLY)) < 0) { 1667c478bd9Sstevel@tonic-gate dprintf("couldn't open file"); 1677c478bd9Sstevel@tonic-gate *perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE; 1687c478bd9Sstevel@tonic-gate return (NULL); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1727c478bd9Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf"); 1737c478bd9Sstevel@tonic-gate *perr = G_ELF; 1747c478bd9Sstevel@tonic-gate goto err; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) { 1787c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1797c478bd9Sstevel@tonic-gate goto err; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 1837c478bd9Sstevel@tonic-gate P->state = PS_IDLE; 1847c478bd9Sstevel@tonic-gate P->pid = (pid_t)-1; 1857c478bd9Sstevel@tonic-gate P->asfd = fd; 1867c478bd9Sstevel@tonic-gate P->ctlfd = -1; 1877c478bd9Sstevel@tonic-gate P->statfd = -1; 1887c478bd9Sstevel@tonic-gate P->agentctlfd = -1; 1897c478bd9Sstevel@tonic-gate P->agentstatfd = -1; 1907c478bd9Sstevel@tonic-gate P->info_valid = -1; 191*2a12f85aSJeremy Jones Pinit_ops(&P->ops, &P_idle_ops); 1927c478bd9Sstevel@tonic-gate Pinitsym(P); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1957c478bd9Sstevel@tonic-gate *perr = G_ELF; 1967c478bd9Sstevel@tonic-gate return (NULL); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * Construct a file_info_t that corresponds to this file. 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate if ((fp = calloc(1, sizeof (file_info_t))) == NULL) { 2037c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2047c478bd9Sstevel@tonic-gate goto err; 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) { 2087c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2097c478bd9Sstevel@tonic-gate goto err; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate if (*fname == '/') { 2137c478bd9Sstevel@tonic-gate (void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname)); 2147c478bd9Sstevel@tonic-gate } else { 2157c478bd9Sstevel@tonic-gate size_t sz; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) == 2187c478bd9Sstevel@tonic-gate NULL) { 2197c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2207c478bd9Sstevel@tonic-gate goto err; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate sz = strlen(fp->file_pname); 2247c478bd9Sstevel@tonic-gate (void) snprintf(&fp->file_pname[sz], 2257c478bd9Sstevel@tonic-gate sizeof (fp->file_pname) - sz, "/%s", fname); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate fp->file_fd = fd; 2297c478bd9Sstevel@tonic-gate fp->file_lo->rl_lmident = LM_ID_BASE; 230186f7fbfSEdward Pilatowicz if ((fp->file_lname = strdup(fp->file_pname)) == NULL) { 231186f7fbfSEdward Pilatowicz *perr = G_STRANGE; 232186f7fbfSEdward Pilatowicz goto err; 233186f7fbfSEdward Pilatowicz } 2347c478bd9Sstevel@tonic-gate fp->file_lbase = basename(fp->file_lname); 2357c478bd9Sstevel@tonic-gate 236186f7fbfSEdward Pilatowicz if ((P->execname = strdup(fp->file_pname)) == NULL) { 237186f7fbfSEdward Pilatowicz *perr = G_STRANGE; 238186f7fbfSEdward Pilatowicz goto err; 239186f7fbfSEdward Pilatowicz } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate P->num_files++; 2427c478bd9Sstevel@tonic-gate list_link(fp, &P->file_head); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 2457c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2467c478bd9Sstevel@tonic-gate goto err; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 24962b628a6SAli Bahrami if (elf_getphdrnum(elf, &phnum) == -1) { 25030da1432Sahl *perr = G_STRANGE; 25130da1432Sahl goto err; 25230da1432Sahl } 25330da1432Sahl 25430da1432Sahl dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * Sift through the program headers making the relevant maps. 2587c478bd9Sstevel@tonic-gate */ 25930da1432Sahl for (i = 0; i < phnum; i++) { 2607c478bd9Sstevel@tonic-gate GElf_Phdr phdr, *php; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) { 2637c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2647c478bd9Sstevel@tonic-gate goto err; 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate if (php->p_type != PT_LOAD) 2687c478bd9Sstevel@tonic-gate continue; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (idle_add_mapping(P, php, fp) != 0) { 2717c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2727c478bd9Sstevel@tonic-gate goto err; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate Psort_mappings(P); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate (void) elf_end(elf); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate P->map_exec = fp->file_map; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate P->status.pr_flags = PR_STOPPED; 2827c478bd9Sstevel@tonic-gate P->status.pr_nlwp = 0; 2837c478bd9Sstevel@tonic-gate P->status.pr_pid = (pid_t)-1; 2847c478bd9Sstevel@tonic-gate P->status.pr_ppid = (pid_t)-1; 2857c478bd9Sstevel@tonic-gate P->status.pr_pgid = (pid_t)-1; 2867c478bd9Sstevel@tonic-gate P->status.pr_sid = (pid_t)-1; 2877c478bd9Sstevel@tonic-gate P->status.pr_taskid = (taskid_t)-1; 2887c478bd9Sstevel@tonic-gate P->status.pr_projid = (projid_t)-1; 289186f7fbfSEdward Pilatowicz P->status.pr_zoneid = (zoneid_t)-1; 2907c478bd9Sstevel@tonic-gate switch (ehdr.e_ident[EI_CLASS]) { 2917c478bd9Sstevel@tonic-gate case ELFCLASS32: 2927c478bd9Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_ILP32; 2937c478bd9Sstevel@tonic-gate break; 2947c478bd9Sstevel@tonic-gate case ELFCLASS64: 2957c478bd9Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_LP64; 2967c478bd9Sstevel@tonic-gate break; 2977c478bd9Sstevel@tonic-gate default: 2987c478bd9Sstevel@tonic-gate *perr = G_FORMAT; 2997c478bd9Sstevel@tonic-gate goto err; 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate /* 303186f7fbfSEdward Pilatowicz * Pfindobj() checks what zone a process is associated with, so 304186f7fbfSEdward Pilatowicz * we call it after initializing pr_zoneid to -1. This ensures 305186f7fbfSEdward Pilatowicz * we don't get associated with any zone on the system. 306186f7fbfSEdward Pilatowicz */ 307186f7fbfSEdward Pilatowicz if (Pfindobj(P, fp->file_lname, buf, sizeof (buf)) != NULL) { 308186f7fbfSEdward Pilatowicz free(P->execname); 309186f7fbfSEdward Pilatowicz P->execname = strdup(buf); 310186f7fbfSEdward Pilatowicz if ((fp->file_rname = strdup(buf)) != NULL) 311186f7fbfSEdward Pilatowicz fp->file_rbase = basename(fp->file_rname); 312186f7fbfSEdward Pilatowicz } 313186f7fbfSEdward Pilatowicz 314186f7fbfSEdward Pilatowicz /* 3157c478bd9Sstevel@tonic-gate * The file and map lists are complete, and will never need to be 3167c478bd9Sstevel@tonic-gate * adjusted. 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate P->info_valid = 1; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate return (P); 3217c478bd9Sstevel@tonic-gate err: 3227c478bd9Sstevel@tonic-gate (void) close(fd); 3237c478bd9Sstevel@tonic-gate if (P != NULL) 3247c478bd9Sstevel@tonic-gate Pfree(P); 3257c478bd9Sstevel@tonic-gate if (elf != NULL) 3267c478bd9Sstevel@tonic-gate (void) elf_end(elf); 3277c478bd9Sstevel@tonic-gate return (NULL); 3287c478bd9Sstevel@tonic-gate } 329