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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 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 377c478bd9Sstevel@tonic-gate #include "Pcontrol.h" 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate static ssize_t 407c478bd9Sstevel@tonic-gate Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr) 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate size_t resid = n; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate while (resid > 0) { 457c478bd9Sstevel@tonic-gate map_info_t *mp; 467c478bd9Sstevel@tonic-gate uintptr_t mapoff; 477c478bd9Sstevel@tonic-gate ssize_t len; 487c478bd9Sstevel@tonic-gate off64_t off; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if ((mp = Paddr2mptr(P, addr)) == NULL) 517c478bd9Sstevel@tonic-gate break; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate mapoff = addr - mp->map_pmap.pr_vaddr; 547c478bd9Sstevel@tonic-gate len = MIN(resid, mp->map_pmap.pr_size - mapoff); 557c478bd9Sstevel@tonic-gate off = mp->map_offset + mapoff; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate if ((len = pread64(P->asfd, buf, len, off)) <= 0) 587c478bd9Sstevel@tonic-gate break; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate resid -= len; 617c478bd9Sstevel@tonic-gate addr += len; 627c478bd9Sstevel@tonic-gate buf = (char *)buf + len; 637c478bd9Sstevel@tonic-gate } 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate return (n - resid); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 697c478bd9Sstevel@tonic-gate static ssize_t 707c478bd9Sstevel@tonic-gate Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate errno = EIO; 737c478bd9Sstevel@tonic-gate return (-1); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static const ps_rwops_t P_idle_ops = { 777c478bd9Sstevel@tonic-gate Pread_idle, 787c478bd9Sstevel@tonic-gate Pwrite_idle 797c478bd9Sstevel@tonic-gate }; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static int 827c478bd9Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp) 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate prmap_t pmap; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n", 877c478bd9Sstevel@tonic-gate (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz, 887c478bd9Sstevel@tonic-gate (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate pmap.pr_vaddr = (uintptr_t)php->p_vaddr; 917c478bd9Sstevel@tonic-gate pmap.pr_size = php->p_filesz; 927c478bd9Sstevel@tonic-gate (void) strncpy(pmap.pr_mapname, fp->file_pname, 937c478bd9Sstevel@tonic-gate sizeof (pmap.pr_mapname)); 947c478bd9Sstevel@tonic-gate pmap.pr_offset = php->p_offset; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate pmap.pr_mflags = 0; 977c478bd9Sstevel@tonic-gate if (php->p_flags & PF_R) 987c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_READ; 997c478bd9Sstevel@tonic-gate if (php->p_flags & PF_W) 1007c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_WRITE; 1017c478bd9Sstevel@tonic-gate if (php->p_flags & PF_X) 1027c478bd9Sstevel@tonic-gate pmap.pr_mflags |= MA_EXEC; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate pmap.pr_pagesize = 0; 1057c478bd9Sstevel@tonic-gate pmap.pr_shmid = -1; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate return (Padd_mapping(P, php->p_offset, fp, &pmap)); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate struct ps_prochandle * 1117c478bd9Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr) 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate struct ps_prochandle *P = NULL; 1147c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 1157c478bd9Sstevel@tonic-gate Elf *elf = NULL; 116*30da1432Sahl size_t phnum; 1177c478bd9Sstevel@tonic-gate file_info_t *fp = NULL; 1187c478bd9Sstevel@tonic-gate int fd; 1197c478bd9Sstevel@tonic-gate int i; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if ((fd = open64(fname, O_RDONLY)) < 0) { 1227c478bd9Sstevel@tonic-gate dprintf("couldn't open file"); 1237c478bd9Sstevel@tonic-gate *perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE; 1247c478bd9Sstevel@tonic-gate return (NULL); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1287c478bd9Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf"); 1297c478bd9Sstevel@tonic-gate *perr = G_ELF; 1307c478bd9Sstevel@tonic-gate goto err; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) { 1347c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1357c478bd9Sstevel@tonic-gate goto err; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 1397c478bd9Sstevel@tonic-gate P->state = PS_IDLE; 1407c478bd9Sstevel@tonic-gate P->pid = (pid_t)-1; 1417c478bd9Sstevel@tonic-gate P->asfd = fd; 1427c478bd9Sstevel@tonic-gate P->ctlfd = -1; 1437c478bd9Sstevel@tonic-gate P->statfd = -1; 1447c478bd9Sstevel@tonic-gate P->agentctlfd = -1; 1457c478bd9Sstevel@tonic-gate P->agentstatfd = -1; 1467c478bd9Sstevel@tonic-gate P->info_valid = -1; 1477c478bd9Sstevel@tonic-gate P->ops = &P_idle_ops; 1487c478bd9Sstevel@tonic-gate Pinitsym(P); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 1517c478bd9Sstevel@tonic-gate *perr = G_ELF; 1527c478bd9Sstevel@tonic-gate return (NULL); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * Construct a file_info_t that corresponds to this file. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate if ((fp = calloc(1, sizeof (file_info_t))) == NULL) { 1597c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1607c478bd9Sstevel@tonic-gate goto err; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) { 1647c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1657c478bd9Sstevel@tonic-gate goto err; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (*fname == '/') { 1697c478bd9Sstevel@tonic-gate (void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname)); 1707c478bd9Sstevel@tonic-gate } else { 1717c478bd9Sstevel@tonic-gate size_t sz; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) == 1747c478bd9Sstevel@tonic-gate NULL) { 1757c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1767c478bd9Sstevel@tonic-gate goto err; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate sz = strlen(fp->file_pname); 1807c478bd9Sstevel@tonic-gate (void) snprintf(&fp->file_pname[sz], 1817c478bd9Sstevel@tonic-gate sizeof (fp->file_pname) - sz, "/%s", fname); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate fp->file_fd = fd; 1857c478bd9Sstevel@tonic-gate fp->file_lo->rl_lmident = LM_ID_BASE; 1867c478bd9Sstevel@tonic-gate fp->file_lname = strdup(fp->file_pname); 1877c478bd9Sstevel@tonic-gate fp->file_lbase = basename(fp->file_lname); 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate P->execname = strdup(fp->file_pname); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate P->num_files++; 1927c478bd9Sstevel@tonic-gate list_link(fp, &P->file_head); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 1957c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 1967c478bd9Sstevel@tonic-gate goto err; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 199*30da1432Sahl if (elf_getphnum(elf, &phnum) == 0) { 200*30da1432Sahl *perr = G_STRANGE; 201*30da1432Sahl goto err; 202*30da1432Sahl } 203*30da1432Sahl 204*30da1432Sahl dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Sift through the program headers making the relevant maps. 2087c478bd9Sstevel@tonic-gate */ 209*30da1432Sahl for (i = 0; i < phnum; i++) { 2107c478bd9Sstevel@tonic-gate GElf_Phdr phdr, *php; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) { 2137c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2147c478bd9Sstevel@tonic-gate goto err; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (php->p_type != PT_LOAD) 2187c478bd9Sstevel@tonic-gate continue; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (idle_add_mapping(P, php, fp) != 0) { 2217c478bd9Sstevel@tonic-gate *perr = G_STRANGE; 2227c478bd9Sstevel@tonic-gate goto err; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate Psort_mappings(P); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate (void) elf_end(elf); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate P->map_exec = fp->file_map; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate P->status.pr_flags = PR_STOPPED; 2327c478bd9Sstevel@tonic-gate P->status.pr_nlwp = 0; 2337c478bd9Sstevel@tonic-gate P->status.pr_pid = (pid_t)-1; 2347c478bd9Sstevel@tonic-gate P->status.pr_ppid = (pid_t)-1; 2357c478bd9Sstevel@tonic-gate P->status.pr_pgid = (pid_t)-1; 2367c478bd9Sstevel@tonic-gate P->status.pr_sid = (pid_t)-1; 2377c478bd9Sstevel@tonic-gate P->status.pr_taskid = (taskid_t)-1; 2387c478bd9Sstevel@tonic-gate P->status.pr_projid = (projid_t)-1; 2397c478bd9Sstevel@tonic-gate switch (ehdr.e_ident[EI_CLASS]) { 2407c478bd9Sstevel@tonic-gate case ELFCLASS32: 2417c478bd9Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_ILP32; 2427c478bd9Sstevel@tonic-gate break; 2437c478bd9Sstevel@tonic-gate case ELFCLASS64: 2447c478bd9Sstevel@tonic-gate P->status.pr_dmodel = PR_MODEL_LP64; 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate default: 2477c478bd9Sstevel@tonic-gate *perr = G_FORMAT; 2487c478bd9Sstevel@tonic-gate goto err; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * The file and map lists are complete, and will never need to be 2537c478bd9Sstevel@tonic-gate * adjusted. 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate P->info_valid = 1; 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate return (P); 2587c478bd9Sstevel@tonic-gate err: 2597c478bd9Sstevel@tonic-gate (void) close(fd); 2607c478bd9Sstevel@tonic-gate if (P != NULL) 2617c478bd9Sstevel@tonic-gate Pfree(P); 2627c478bd9Sstevel@tonic-gate if (elf != NULL) 2637c478bd9Sstevel@tonic-gate (void) elf_end(elf); 2647c478bd9Sstevel@tonic-gate return (NULL); 2657c478bd9Sstevel@tonic-gate } 266