1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <libelf.h>
31*0Sstevel@tonic-gate #include <libgen.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <strings.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "Pcontrol.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static ssize_t
40*0Sstevel@tonic-gate Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate 	size_t resid = n;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	while (resid > 0) {
45*0Sstevel@tonic-gate 		map_info_t *mp;
46*0Sstevel@tonic-gate 		uintptr_t mapoff;
47*0Sstevel@tonic-gate 		ssize_t len;
48*0Sstevel@tonic-gate 		off64_t off;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 		if ((mp = Paddr2mptr(P, addr)) == NULL)
51*0Sstevel@tonic-gate 			break;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 		mapoff = addr - mp->map_pmap.pr_vaddr;
54*0Sstevel@tonic-gate 		len = MIN(resid, mp->map_pmap.pr_size - mapoff);
55*0Sstevel@tonic-gate 		off = mp->map_offset + mapoff;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 		if ((len = pread64(P->asfd, buf, len, off)) <= 0)
58*0Sstevel@tonic-gate 			break;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 		resid -= len;
61*0Sstevel@tonic-gate 		addr += len;
62*0Sstevel@tonic-gate 		buf = (char *)buf + len;
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	return (n - resid);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*ARGSUSED*/
69*0Sstevel@tonic-gate static ssize_t
70*0Sstevel@tonic-gate Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	errno = EIO;
73*0Sstevel@tonic-gate 	return (-1);
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static const ps_rwops_t P_idle_ops = {
77*0Sstevel@tonic-gate 	Pread_idle,
78*0Sstevel@tonic-gate 	Pwrite_idle
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static int
82*0Sstevel@tonic-gate idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	prmap_t pmap;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n",
87*0Sstevel@tonic-gate 	    (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
88*0Sstevel@tonic-gate 	    (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
91*0Sstevel@tonic-gate 	pmap.pr_size = php->p_filesz;
92*0Sstevel@tonic-gate 	(void) strncpy(pmap.pr_mapname, fp->file_pname,
93*0Sstevel@tonic-gate 	    sizeof (pmap.pr_mapname));
94*0Sstevel@tonic-gate 	pmap.pr_offset = php->p_offset;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	pmap.pr_mflags = 0;
97*0Sstevel@tonic-gate 	if (php->p_flags & PF_R)
98*0Sstevel@tonic-gate 		pmap.pr_mflags |= MA_READ;
99*0Sstevel@tonic-gate 	if (php->p_flags & PF_W)
100*0Sstevel@tonic-gate 		pmap.pr_mflags |= MA_WRITE;
101*0Sstevel@tonic-gate 	if (php->p_flags & PF_X)
102*0Sstevel@tonic-gate 		pmap.pr_mflags |= MA_EXEC;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	pmap.pr_pagesize = 0;
105*0Sstevel@tonic-gate 	pmap.pr_shmid = -1;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	return (Padd_mapping(P, php->p_offset, fp, &pmap));
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate struct ps_prochandle *
111*0Sstevel@tonic-gate Pgrab_file(const char *fname, int *perr)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	struct ps_prochandle *P = NULL;
114*0Sstevel@tonic-gate 	GElf_Ehdr ehdr;
115*0Sstevel@tonic-gate 	Elf *elf = NULL;
116*0Sstevel@tonic-gate 	file_info_t *fp = NULL;
117*0Sstevel@tonic-gate 	int fd;
118*0Sstevel@tonic-gate 	int i;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if ((fd = open64(fname, O_RDONLY)) < 0) {
121*0Sstevel@tonic-gate 		dprintf("couldn't open file");
122*0Sstevel@tonic-gate 		*perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE;
123*0Sstevel@tonic-gate 		return (NULL);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
127*0Sstevel@tonic-gate 		dprintf("libproc ELF version is more recent than libelf");
128*0Sstevel@tonic-gate 		*perr = G_ELF;
129*0Sstevel@tonic-gate 		goto err;
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) {
133*0Sstevel@tonic-gate 		*perr = G_STRANGE;
134*0Sstevel@tonic-gate 		goto err;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
138*0Sstevel@tonic-gate 	P->state = PS_IDLE;
139*0Sstevel@tonic-gate 	P->pid = (pid_t)-1;
140*0Sstevel@tonic-gate 	P->asfd = fd;
141*0Sstevel@tonic-gate 	P->ctlfd = -1;
142*0Sstevel@tonic-gate 	P->statfd = -1;
143*0Sstevel@tonic-gate 	P->agentctlfd = -1;
144*0Sstevel@tonic-gate 	P->agentstatfd = -1;
145*0Sstevel@tonic-gate 	P->info_valid = -1;
146*0Sstevel@tonic-gate 	P->ops = &P_idle_ops;
147*0Sstevel@tonic-gate 	Pinitsym(P);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
150*0Sstevel@tonic-gate 		*perr = G_ELF;
151*0Sstevel@tonic-gate 		return (NULL);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * Construct a file_info_t that corresponds to this file.
156*0Sstevel@tonic-gate 	 */
157*0Sstevel@tonic-gate 	if ((fp = calloc(1, sizeof (file_info_t))) == NULL) {
158*0Sstevel@tonic-gate 		*perr = G_STRANGE;
159*0Sstevel@tonic-gate 		goto err;
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
163*0Sstevel@tonic-gate 		*perr = G_STRANGE;
164*0Sstevel@tonic-gate 		goto err;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (*fname == '/') {
168*0Sstevel@tonic-gate 		(void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname));
169*0Sstevel@tonic-gate 	} else {
170*0Sstevel@tonic-gate 		size_t sz;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) ==
173*0Sstevel@tonic-gate 		    NULL) {
174*0Sstevel@tonic-gate 			*perr = G_STRANGE;
175*0Sstevel@tonic-gate 			goto err;
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 		sz = strlen(fp->file_pname);
179*0Sstevel@tonic-gate 		(void) snprintf(&fp->file_pname[sz],
180*0Sstevel@tonic-gate 		    sizeof (fp->file_pname) - sz, "/%s", fname);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	fp->file_fd = fd;
184*0Sstevel@tonic-gate 	fp->file_lo->rl_lmident = LM_ID_BASE;
185*0Sstevel@tonic-gate 	fp->file_lname = strdup(fp->file_pname);
186*0Sstevel@tonic-gate 	fp->file_lbase = basename(fp->file_lname);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	P->execname = strdup(fp->file_pname);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	P->num_files++;
191*0Sstevel@tonic-gate 	list_link(fp, &P->file_head);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
194*0Sstevel@tonic-gate 		*perr = G_STRANGE;
195*0Sstevel@tonic-gate 		goto err;
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	dprintf("Pgrab_file: ehdr.e_phnum = %d\n", ehdr.e_phnum);
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	/*
201*0Sstevel@tonic-gate 	 * Sift through the program headers making the relevant maps.
202*0Sstevel@tonic-gate 	 */
203*0Sstevel@tonic-gate 	for (i = 0; i < ehdr.e_phnum; i++) {
204*0Sstevel@tonic-gate 		GElf_Phdr phdr, *php;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) {
207*0Sstevel@tonic-gate 			*perr = G_STRANGE;
208*0Sstevel@tonic-gate 			goto err;
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		if (php->p_type != PT_LOAD)
212*0Sstevel@tonic-gate 			continue;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 		if (idle_add_mapping(P, php, fp) != 0) {
215*0Sstevel@tonic-gate 			*perr = G_STRANGE;
216*0Sstevel@tonic-gate 			goto err;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 	Psort_mappings(P);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	(void) elf_end(elf);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	P->map_exec = fp->file_map;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	P->status.pr_flags = PR_STOPPED;
226*0Sstevel@tonic-gate 	P->status.pr_nlwp = 0;
227*0Sstevel@tonic-gate 	P->status.pr_pid = (pid_t)-1;
228*0Sstevel@tonic-gate 	P->status.pr_ppid = (pid_t)-1;
229*0Sstevel@tonic-gate 	P->status.pr_pgid = (pid_t)-1;
230*0Sstevel@tonic-gate 	P->status.pr_sid = (pid_t)-1;
231*0Sstevel@tonic-gate 	P->status.pr_taskid = (taskid_t)-1;
232*0Sstevel@tonic-gate 	P->status.pr_projid = (projid_t)-1;
233*0Sstevel@tonic-gate 	switch (ehdr.e_ident[EI_CLASS]) {
234*0Sstevel@tonic-gate 	case ELFCLASS32:
235*0Sstevel@tonic-gate 		P->status.pr_dmodel = PR_MODEL_ILP32;
236*0Sstevel@tonic-gate 		break;
237*0Sstevel@tonic-gate 	case ELFCLASS64:
238*0Sstevel@tonic-gate 		P->status.pr_dmodel = PR_MODEL_LP64;
239*0Sstevel@tonic-gate 		break;
240*0Sstevel@tonic-gate 	default:
241*0Sstevel@tonic-gate 		*perr = G_FORMAT;
242*0Sstevel@tonic-gate 		goto err;
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	/*
246*0Sstevel@tonic-gate 	 * The file and map lists are complete, and will never need to be
247*0Sstevel@tonic-gate 	 * adjusted.
248*0Sstevel@tonic-gate 	 */
249*0Sstevel@tonic-gate 	P->info_valid = 1;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	return (P);
252*0Sstevel@tonic-gate err:
253*0Sstevel@tonic-gate 	(void) close(fd);
254*0Sstevel@tonic-gate 	if (P != NULL)
255*0Sstevel@tonic-gate 		Pfree(P);
256*0Sstevel@tonic-gate 	if (elf != NULL)
257*0Sstevel@tonic-gate 		(void) elf_end(elf);
258*0Sstevel@tonic-gate 	return (NULL);
259*0Sstevel@tonic-gate }
260