xref: /titanic_50/usr/src/lib/libproc/common/Pidle.c (revision 62b628a68db596a2d75a316dc7ffef658079231f)
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 /*
22*62b628a6SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <libelf.h>
287c478bd9Sstevel@tonic-gate #include <libgen.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate 
34186f7fbfSEdward Pilatowicz #include "libproc.h"
357c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static ssize_t
387c478bd9Sstevel@tonic-gate Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	size_t resid = n;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	while (resid > 0) {
437c478bd9Sstevel@tonic-gate 		map_info_t *mp;
447c478bd9Sstevel@tonic-gate 		uintptr_t mapoff;
457c478bd9Sstevel@tonic-gate 		ssize_t len;
467c478bd9Sstevel@tonic-gate 		off64_t off;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 		if ((mp = Paddr2mptr(P, addr)) == NULL)
497c478bd9Sstevel@tonic-gate 			break;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 		mapoff = addr - mp->map_pmap.pr_vaddr;
527c478bd9Sstevel@tonic-gate 		len = MIN(resid, mp->map_pmap.pr_size - mapoff);
537c478bd9Sstevel@tonic-gate 		off = mp->map_offset + mapoff;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 		if ((len = pread64(P->asfd, buf, len, off)) <= 0)
567c478bd9Sstevel@tonic-gate 			break;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 		resid -= len;
597c478bd9Sstevel@tonic-gate 		addr += len;
607c478bd9Sstevel@tonic-gate 		buf = (char *)buf + len;
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (n - resid);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
677c478bd9Sstevel@tonic-gate static ssize_t
687c478bd9Sstevel@tonic-gate Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	errno = EIO;
717c478bd9Sstevel@tonic-gate 	return (-1);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static const ps_rwops_t P_idle_ops = {
757c478bd9Sstevel@tonic-gate 	Pread_idle,
767c478bd9Sstevel@tonic-gate 	Pwrite_idle
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int
807c478bd9Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	prmap_t pmap;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n",
857c478bd9Sstevel@tonic-gate 	    (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
867c478bd9Sstevel@tonic-gate 	    (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
897c478bd9Sstevel@tonic-gate 	pmap.pr_size = php->p_filesz;
907c478bd9Sstevel@tonic-gate 	(void) strncpy(pmap.pr_mapname, fp->file_pname,
917c478bd9Sstevel@tonic-gate 	    sizeof (pmap.pr_mapname));
927c478bd9Sstevel@tonic-gate 	pmap.pr_offset = php->p_offset;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	pmap.pr_mflags = 0;
957c478bd9Sstevel@tonic-gate 	if (php->p_flags & PF_R)
967c478bd9Sstevel@tonic-gate 		pmap.pr_mflags |= MA_READ;
977c478bd9Sstevel@tonic-gate 	if (php->p_flags & PF_W)
987c478bd9Sstevel@tonic-gate 		pmap.pr_mflags |= MA_WRITE;
997c478bd9Sstevel@tonic-gate 	if (php->p_flags & PF_X)
1007c478bd9Sstevel@tonic-gate 		pmap.pr_mflags |= MA_EXEC;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	pmap.pr_pagesize = 0;
1037c478bd9Sstevel@tonic-gate 	pmap.pr_shmid = -1;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	return (Padd_mapping(P, php->p_offset, fp, &pmap));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate struct ps_prochandle *
1097c478bd9Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = NULL;
112186f7fbfSEdward Pilatowicz 	char buf[PATH_MAX];
1137c478bd9Sstevel@tonic-gate 	GElf_Ehdr ehdr;
1147c478bd9Sstevel@tonic-gate 	Elf *elf = NULL;
11530da1432Sahl 	size_t phnum;
1167c478bd9Sstevel@tonic-gate 	file_info_t *fp = NULL;
1177c478bd9Sstevel@tonic-gate 	int fd;
1187c478bd9Sstevel@tonic-gate 	int i;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if ((fd = open64(fname, O_RDONLY)) < 0) {
1217c478bd9Sstevel@tonic-gate 		dprintf("couldn't open file");
1227c478bd9Sstevel@tonic-gate 		*perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE;
1237c478bd9Sstevel@tonic-gate 		return (NULL);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
1277c478bd9Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf");
1287c478bd9Sstevel@tonic-gate 		*perr = G_ELF;
1297c478bd9Sstevel@tonic-gate 		goto err;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) {
1337c478bd9Sstevel@tonic-gate 		*perr = G_STRANGE;
1347c478bd9Sstevel@tonic-gate 		goto err;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
1387c478bd9Sstevel@tonic-gate 	P->state = PS_IDLE;
1397c478bd9Sstevel@tonic-gate 	P->pid = (pid_t)-1;
1407c478bd9Sstevel@tonic-gate 	P->asfd = fd;
1417c478bd9Sstevel@tonic-gate 	P->ctlfd = -1;
1427c478bd9Sstevel@tonic-gate 	P->statfd = -1;
1437c478bd9Sstevel@tonic-gate 	P->agentctlfd = -1;
1447c478bd9Sstevel@tonic-gate 	P->agentstatfd = -1;
1457c478bd9Sstevel@tonic-gate 	P->info_valid = -1;
1467c478bd9Sstevel@tonic-gate 	P->ops = &P_idle_ops;
1477c478bd9Sstevel@tonic-gate 	Pinitsym(P);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1507c478bd9Sstevel@tonic-gate 		*perr = G_ELF;
1517c478bd9Sstevel@tonic-gate 		return (NULL);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Construct a file_info_t that corresponds to this file.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if ((fp = calloc(1, sizeof (file_info_t))) == NULL) {
1587c478bd9Sstevel@tonic-gate 		*perr = G_STRANGE;
1597c478bd9Sstevel@tonic-gate 		goto err;
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
1637c478bd9Sstevel@tonic-gate 		*perr = G_STRANGE;
1647c478bd9Sstevel@tonic-gate 		goto err;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (*fname == '/') {
1687c478bd9Sstevel@tonic-gate 		(void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname));
1697c478bd9Sstevel@tonic-gate 	} else {
1707c478bd9Sstevel@tonic-gate 		size_t sz;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) ==
1737c478bd9Sstevel@tonic-gate 		    NULL) {
1747c478bd9Sstevel@tonic-gate 			*perr = G_STRANGE;
1757c478bd9Sstevel@tonic-gate 			goto err;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		sz = strlen(fp->file_pname);
1797c478bd9Sstevel@tonic-gate 		(void) snprintf(&fp->file_pname[sz],
1807c478bd9Sstevel@tonic-gate 		    sizeof (fp->file_pname) - sz, "/%s", fname);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	fp->file_fd = fd;
1847c478bd9Sstevel@tonic-gate 	fp->file_lo->rl_lmident = LM_ID_BASE;
185186f7fbfSEdward Pilatowicz 	if ((fp->file_lname = strdup(fp->file_pname)) == NULL) {
186186f7fbfSEdward Pilatowicz 		*perr = G_STRANGE;
187186f7fbfSEdward Pilatowicz 		goto err;
188186f7fbfSEdward Pilatowicz 	}
1897c478bd9Sstevel@tonic-gate 	fp->file_lbase = basename(fp->file_lname);
1907c478bd9Sstevel@tonic-gate 
191186f7fbfSEdward Pilatowicz 	if ((P->execname = strdup(fp->file_pname)) == NULL) {
192186f7fbfSEdward Pilatowicz 		*perr = G_STRANGE;
193186f7fbfSEdward Pilatowicz 		goto err;
194186f7fbfSEdward Pilatowicz 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	P->num_files++;
1977c478bd9Sstevel@tonic-gate 	list_link(fp, &P->file_head);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
2007c478bd9Sstevel@tonic-gate 		*perr = G_STRANGE;
2017c478bd9Sstevel@tonic-gate 		goto err;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
204*62b628a6SAli Bahrami 	if (elf_getphdrnum(elf, &phnum) == -1) {
20530da1432Sahl 		*perr = G_STRANGE;
20630da1432Sahl 		goto err;
20730da1432Sahl 	}
20830da1432Sahl 
20930da1432Sahl 	dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Sift through the program headers making the relevant maps.
2137c478bd9Sstevel@tonic-gate 	 */
21430da1432Sahl 	for (i = 0; i < phnum; i++) {
2157c478bd9Sstevel@tonic-gate 		GElf_Phdr phdr, *php;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) {
2187c478bd9Sstevel@tonic-gate 			*perr = G_STRANGE;
2197c478bd9Sstevel@tonic-gate 			goto err;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (php->p_type != PT_LOAD)
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if (idle_add_mapping(P, php, fp) != 0) {
2267c478bd9Sstevel@tonic-gate 			*perr = G_STRANGE;
2277c478bd9Sstevel@tonic-gate 			goto err;
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	Psort_mappings(P);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	P->map_exec = fp->file_map;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	P->status.pr_flags = PR_STOPPED;
2377c478bd9Sstevel@tonic-gate 	P->status.pr_nlwp = 0;
2387c478bd9Sstevel@tonic-gate 	P->status.pr_pid = (pid_t)-1;
2397c478bd9Sstevel@tonic-gate 	P->status.pr_ppid = (pid_t)-1;
2407c478bd9Sstevel@tonic-gate 	P->status.pr_pgid = (pid_t)-1;
2417c478bd9Sstevel@tonic-gate 	P->status.pr_sid = (pid_t)-1;
2427c478bd9Sstevel@tonic-gate 	P->status.pr_taskid = (taskid_t)-1;
2437c478bd9Sstevel@tonic-gate 	P->status.pr_projid = (projid_t)-1;
244186f7fbfSEdward Pilatowicz 	P->status.pr_zoneid = (zoneid_t)-1;
2457c478bd9Sstevel@tonic-gate 	switch (ehdr.e_ident[EI_CLASS]) {
2467c478bd9Sstevel@tonic-gate 	case ELFCLASS32:
2477c478bd9Sstevel@tonic-gate 		P->status.pr_dmodel = PR_MODEL_ILP32;
2487c478bd9Sstevel@tonic-gate 		break;
2497c478bd9Sstevel@tonic-gate 	case ELFCLASS64:
2507c478bd9Sstevel@tonic-gate 		P->status.pr_dmodel = PR_MODEL_LP64;
2517c478bd9Sstevel@tonic-gate 		break;
2527c478bd9Sstevel@tonic-gate 	default:
2537c478bd9Sstevel@tonic-gate 		*perr = G_FORMAT;
2547c478bd9Sstevel@tonic-gate 		goto err;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
258186f7fbfSEdward Pilatowicz 	 * Pfindobj() checks what zone a process is associated with, so
259186f7fbfSEdward Pilatowicz 	 * we call it after initializing pr_zoneid to -1.  This ensures
260186f7fbfSEdward Pilatowicz 	 * we don't get associated with any zone on the system.
261186f7fbfSEdward Pilatowicz 	 */
262186f7fbfSEdward Pilatowicz 	if (Pfindobj(P, fp->file_lname, buf, sizeof (buf)) != NULL) {
263186f7fbfSEdward Pilatowicz 		free(P->execname);
264186f7fbfSEdward Pilatowicz 		P->execname = strdup(buf);
265186f7fbfSEdward Pilatowicz 		if ((fp->file_rname = strdup(buf)) != NULL)
266186f7fbfSEdward Pilatowicz 			fp->file_rbase = basename(fp->file_rname);
267186f7fbfSEdward Pilatowicz 	}
268186f7fbfSEdward Pilatowicz 
269186f7fbfSEdward Pilatowicz 	/*
2707c478bd9Sstevel@tonic-gate 	 * The file and map lists are complete, and will never need to be
2717c478bd9Sstevel@tonic-gate 	 * adjusted.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	P->info_valid = 1;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	return (P);
2767c478bd9Sstevel@tonic-gate err:
2777c478bd9Sstevel@tonic-gate 	(void) close(fd);
2787c478bd9Sstevel@tonic-gate 	if (P != NULL)
2797c478bd9Sstevel@tonic-gate 		Pfree(P);
2807c478bd9Sstevel@tonic-gate 	if (elf != NULL)
2817c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
2827c478bd9Sstevel@tonic-gate 	return (NULL);
2837c478bd9Sstevel@tonic-gate }
284