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 2000-2002 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 <mdb/mdb_modapi.h>
30*0Sstevel@tonic-gate #include <sys/machelf.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <libproc.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "proc_kludges.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate typedef struct prockuldge_mappings {
38*0Sstevel@tonic-gate 	struct ps_prochandle *pkm_Pr;
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 	uint_t pkm_idx;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 	uint_t pkm_count;
43*0Sstevel@tonic-gate 	uint_t pkm_max;
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 	prmap_t *pkm_mappings;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	uint_t pkm_old_max;
48*0Sstevel@tonic-gate 	prmap_t *pkm_old_mappings;
49*0Sstevel@tonic-gate } prockludge_mappings_t;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /* ARGSUSED */
52*0Sstevel@tonic-gate static int
prockludge_mappings_iter(prockludge_mappings_t * pkm,const prmap_t * pmp,const char * object_name)53*0Sstevel@tonic-gate prockludge_mappings_iter(prockludge_mappings_t *pkm, const prmap_t *pmp,
54*0Sstevel@tonic-gate     const char *object_name)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	if (pkm->pkm_count >= pkm->pkm_max) {
57*0Sstevel@tonic-gate 		int s = pkm->pkm_max ? pkm->pkm_max * 2 : 16;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 		pkm->pkm_old_max = pkm->pkm_max;
60*0Sstevel@tonic-gate 		pkm->pkm_old_mappings = pkm->pkm_mappings;
61*0Sstevel@tonic-gate 		pkm->pkm_max = s;
62*0Sstevel@tonic-gate 		pkm->pkm_mappings = mdb_alloc(sizeof (prmap_t) * s, UM_SLEEP);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 		bcopy(pkm->pkm_old_mappings, pkm->pkm_mappings,
65*0Sstevel@tonic-gate 		    sizeof (prmap_t) * pkm->pkm_old_max);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 		mdb_free(pkm->pkm_old_mappings,
68*0Sstevel@tonic-gate 		    sizeof (prmap_t) * pkm->pkm_old_max);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 		pkm->pkm_old_mappings = NULL;
71*0Sstevel@tonic-gate 		pkm->pkm_old_max = 0;
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 	bcopy(pmp, &pkm->pkm_mappings[pkm->pkm_count++], sizeof (prmap_t));
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	return (0);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate int
prockludge_mappings_walk_init(mdb_walk_state_t * mws)79*0Sstevel@tonic-gate prockludge_mappings_walk_init(mdb_walk_state_t *mws)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	struct ps_prochandle *Pr;
82*0Sstevel@tonic-gate 	int rc;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	prockludge_mappings_t *pkm;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (mdb_get_xdata("pshandle", &Pr, sizeof (Pr)) == -1) {
87*0Sstevel@tonic-gate 		mdb_warn("couldn't read pshandle xdata");
88*0Sstevel@tonic-gate 		return (WALK_ERR);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	pkm = mdb_zalloc(sizeof (prockludge_mappings_t), UM_SLEEP);
92*0Sstevel@tonic-gate 	pkm->pkm_Pr = Pr;
93*0Sstevel@tonic-gate 	mws->walk_data = pkm;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	rc = Pmapping_iter(Pr, (proc_map_f *)prockludge_mappings_iter, pkm);
96*0Sstevel@tonic-gate 	if (rc != 0) {
97*0Sstevel@tonic-gate 		mdb_warn("Pmapping_iter failed");
98*0Sstevel@tonic-gate 		/* clean up */
99*0Sstevel@tonic-gate 		prockludge_mappings_walk_fini(mws);
100*0Sstevel@tonic-gate 		return (WALK_ERR);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 	return (WALK_NEXT);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate int
prockludge_mappings_walk_step(mdb_walk_state_t * wsp)106*0Sstevel@tonic-gate prockludge_mappings_walk_step(mdb_walk_state_t *wsp)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	prockludge_mappings_t *pkm = wsp->walk_data;
109*0Sstevel@tonic-gate 	int status;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (pkm->pkm_idx >= pkm->pkm_count)
112*0Sstevel@tonic-gate 		return (WALK_DONE);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	status = wsp->walk_callback(0, &pkm->pkm_mappings[pkm->pkm_idx++],
115*0Sstevel@tonic-gate 	    wsp->walk_cbdata);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (status);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate void
prockludge_mappings_walk_fini(mdb_walk_state_t * wsp)121*0Sstevel@tonic-gate prockludge_mappings_walk_fini(mdb_walk_state_t *wsp)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	prockludge_mappings_t *pkm = wsp->walk_data;
124*0Sstevel@tonic-gate 	if (pkm != NULL) {
125*0Sstevel@tonic-gate 		if (pkm->pkm_old_mappings != NULL) {
126*0Sstevel@tonic-gate 			mdb_free(pkm->pkm_old_mappings,
127*0Sstevel@tonic-gate 			    sizeof (prmap_t) * pkm->pkm_old_max);
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 		if (pkm->pkm_mappings &&
130*0Sstevel@tonic-gate 		    pkm->pkm_mappings != pkm->pkm_old_mappings) {
131*0Sstevel@tonic-gate 			mdb_free(pkm->pkm_mappings,
132*0Sstevel@tonic-gate 			    sizeof (prmap_t) * pkm->pkm_max);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		mdb_free(pkm, sizeof (prockludge_mappings_t));
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static int add_count = 0;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate void
prockludge_add_walkers(void)141*0Sstevel@tonic-gate prockludge_add_walkers(void)
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	mdb_walker_t w;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (add_count++ == 0) {
146*0Sstevel@tonic-gate 		w.walk_name = KLUDGE_MAPWALK_NAME;
147*0Sstevel@tonic-gate 		w.walk_descr = "kludge: walk the process' prmap_ts";
148*0Sstevel@tonic-gate 		w.walk_init = prockludge_mappings_walk_init;
149*0Sstevel@tonic-gate 		w.walk_step = prockludge_mappings_walk_step;
150*0Sstevel@tonic-gate 		w.walk_fini = prockludge_mappings_walk_fini;
151*0Sstevel@tonic-gate 		w.walk_init_arg = NULL;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		if (mdb_add_walker(&w) == -1) {
154*0Sstevel@tonic-gate 			mdb_warn("unable to add walker "KLUDGE_MAPWALK_NAME);
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate void
prockludge_remove_walkers(void)160*0Sstevel@tonic-gate prockludge_remove_walkers(void)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	if (--add_count == 0) {
163*0Sstevel@tonic-gate 		mdb_remove_walker(KLUDGE_MAPWALK_NAME);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate }
166