xref: /titanic_50/usr/src/lib/libtnfctl/prb_lmap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Interfaces that deal with loadobjects (shared objects) in target
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <link.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include "tnfctl.h"
41*7c478bd9Sstevel@tonic-gate #include "prb_proc_int.h"
42*7c478bd9Sstevel@tonic-gate #include "dbg.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * iterate over all loadobjects calling the callback function "obj_func"
46*7c478bd9Sstevel@tonic-gate  * for every loadobject with information about the loadobject.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate prb_status_t
prb_loadobj_iter(prb_proc_ctl_t * proc_p,prb_loadobj_f * obj_func,void * cd)49*7c478bd9Sstevel@tonic-gate prb_loadobj_iter(prb_proc_ctl_t *proc_p, prb_loadobj_f *obj_func, void *cd)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	prb_status_t	prbstat;
52*7c478bd9Sstevel@tonic-gate 	Elf3264_Dyn	dentry;
53*7c478bd9Sstevel@tonic-gate 	struct r_debug	r_dbg;
54*7c478bd9Sstevel@tonic-gate 	uintptr_t	lmapaddr;
55*7c478bd9Sstevel@tonic-gate 	struct link_map lmap;
56*7c478bd9Sstevel@tonic-gate 	prb_loadobj_t	loadobj;
57*7c478bd9Sstevel@tonic-gate 	int		retval = 0;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_loadobj_iter_start, "libtnfctl",
60*7c478bd9Sstevel@tonic-gate 			"start prb_loadobj_iter; sunw%verbosity 1");
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if (proc_p->dbgaddr == 0) {
63*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr,
64*7c478bd9Sstevel@tonic-gate 			"prb_loadobj_iter: dbgaddr not set\n"));
65*7c478bd9Sstevel@tonic-gate 		return (PRB_STATUS_BADARG);
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	prbstat = prb_proc_read(proc_p, proc_p->dbgaddr, &dentry,
69*7c478bd9Sstevel@tonic-gate 				sizeof (dentry));
70*7c478bd9Sstevel@tonic-gate 	if (prbstat || !dentry.d_un.d_ptr) {
71*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr,
72*7c478bd9Sstevel@tonic-gate 			"prb_lmap_update: error in d_un.d_ptr\n"));
73*7c478bd9Sstevel@tonic-gate 		return (prbstat);
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	/* read in the debug struct that it points to */
76*7c478bd9Sstevel@tonic-gate 	prbstat = prb_proc_read(proc_p, dentry.d_un.d_ptr,
77*7c478bd9Sstevel@tonic-gate 		&r_dbg, sizeof (r_dbg));
78*7c478bd9Sstevel@tonic-gate 	if (prbstat)
79*7c478bd9Sstevel@tonic-gate 		return (prbstat);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_1(prb_loadobj_iter_1, "libtnfctl", "sunw%verbosity 1",
82*7c478bd9Sstevel@tonic-gate 		tnf_string, link_map_state,
83*7c478bd9Sstevel@tonic-gate 		(r_dbg.r_state == RT_CONSISTENT) ? "RT_CONSISTENT" :
84*7c478bd9Sstevel@tonic-gate 			(r_dbg.r_state == RT_ADD) ? "RT_ADD" : "RT_DELETE");
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* if the link map is not consistent, bail now */
87*7c478bd9Sstevel@tonic-gate 	if (r_dbg.r_state != RT_CONSISTENT)
88*7c478bd9Sstevel@tonic-gate 		return (PRB_STATUS_BADLMAPSTATE);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	lmap.l_next = NULL;			/* makes lint happy */
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	for (lmapaddr = (uintptr_t) r_dbg.r_map; lmapaddr;
93*7c478bd9Sstevel@tonic-gate 		lmapaddr = (uintptr_t) lmap.l_next) {
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		prbstat = prb_proc_read(proc_p, lmapaddr, &lmap, sizeof (lmap));
96*7c478bd9Sstevel@tonic-gate 		if (prbstat)
97*7c478bd9Sstevel@tonic-gate 			return (prbstat);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		loadobj.text_base = lmap.l_addr;
100*7c478bd9Sstevel@tonic-gate 		loadobj.data_base = lmap.l_addr;
101*7c478bd9Sstevel@tonic-gate 		loadobj.objname = NULL;
102*7c478bd9Sstevel@tonic-gate 		/*
103*7c478bd9Sstevel@tonic-gate 		 * client of this interface should deal with -1 for objfd,
104*7c478bd9Sstevel@tonic-gate 		 * so no error checking is needed on this ioctl
105*7c478bd9Sstevel@tonic-gate 		 */
106*7c478bd9Sstevel@tonic-gate 		loadobj.objfd = ioctl(proc_p->procfd, PIOCOPENM, &lmap.l_addr);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		(void) prb_proc_readstr(proc_p, (uintptr_t) lmap.l_name,
109*7c478bd9Sstevel@tonic-gate 						&loadobj.objname);
110*7c478bd9Sstevel@tonic-gate 		retval = obj_func(proc_p, &loadobj, cd);
111*7c478bd9Sstevel@tonic-gate 		if (loadobj.objname)
112*7c478bd9Sstevel@tonic-gate 			free((char *)loadobj.objname);
113*7c478bd9Sstevel@tonic-gate 		if (loadobj.objfd != -1)
114*7c478bd9Sstevel@tonic-gate 			close(loadobj.objfd);
115*7c478bd9Sstevel@tonic-gate 		/* check for error */
116*7c478bd9Sstevel@tonic-gate 		if (retval == 1)
117*7c478bd9Sstevel@tonic-gate 			return (PRB_STATUS_BADARG);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_loadobj_iter_end, "libtnfctl",
121*7c478bd9Sstevel@tonic-gate 			"end prb_loadobj_iter; sunw%verbosity 1");
122*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * Return a fd for the main executable and also the address of where
127*7c478bd9Sstevel@tonic-gate  * it was mapped.
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate prb_status_t
prb_mainobj_get(prb_proc_ctl_t * proc_p,int * objfd,uintptr_t * baseaddr)130*7c478bd9Sstevel@tonic-gate prb_mainobj_get(prb_proc_ctl_t *proc_p, int *objfd, uintptr_t *baseaddr)
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 	int		procfd;
133*7c478bd9Sstevel@tonic-gate 	int		retfd;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	procfd = proc_p->procfd;
137*7c478bd9Sstevel@tonic-gate again:
138*7c478bd9Sstevel@tonic-gate 	retfd = ioctl(procfd, PIOCOPENM, 0);
139*7c478bd9Sstevel@tonic-gate 	if (retfd < 0) {
140*7c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
141*7c478bd9Sstevel@tonic-gate 			goto again;
142*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr,
143*7c478bd9Sstevel@tonic-gate 			"prb_mainobj_get: PIOCOPENM failed: %s\n",
144*7c478bd9Sstevel@tonic-gate 			strerror(errno)));
145*7c478bd9Sstevel@tonic-gate 		return (prb_status_map(errno));
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	*objfd = retfd;
148*7c478bd9Sstevel@tonic-gate 	*baseaddr = 0;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
151*7c478bd9Sstevel@tonic-gate }
152