dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / modules / libumem / proc_kludges.c
blob7faef6fffd04a2cba8275aa1244b9161cd66ebfa
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2000-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <mdb/mdb_modapi.h>
30 #include <sys/machelf.h>
32 #include <libproc.h>
33 #include <stdio.h>
35 #include "proc_kludges.h"
37 typedef struct prockuldge_mappings {
38 struct ps_prochandle *pkm_Pr;
40 uint_t pkm_idx;
42 uint_t pkm_count;
43 uint_t pkm_max;
45 prmap_t *pkm_mappings;
47 uint_t pkm_old_max;
48 prmap_t *pkm_old_mappings;
49 } prockludge_mappings_t;
51 /* ARGSUSED */
52 static int
53 prockludge_mappings_iter(prockludge_mappings_t *pkm, const prmap_t *pmp,
54 const char *object_name)
56 if (pkm->pkm_count >= pkm->pkm_max) {
57 int s = pkm->pkm_max ? pkm->pkm_max * 2 : 16;
59 pkm->pkm_old_max = pkm->pkm_max;
60 pkm->pkm_old_mappings = pkm->pkm_mappings;
61 pkm->pkm_max = s;
62 pkm->pkm_mappings = mdb_alloc(sizeof (prmap_t) * s, UM_SLEEP);
64 bcopy(pkm->pkm_old_mappings, pkm->pkm_mappings,
65 sizeof (prmap_t) * pkm->pkm_old_max);
67 mdb_free(pkm->pkm_old_mappings,
68 sizeof (prmap_t) * pkm->pkm_old_max);
70 pkm->pkm_old_mappings = NULL;
71 pkm->pkm_old_max = 0;
73 bcopy(pmp, &pkm->pkm_mappings[pkm->pkm_count++], sizeof (prmap_t));
75 return (0);
78 int
79 prockludge_mappings_walk_init(mdb_walk_state_t *mws)
81 struct ps_prochandle *Pr;
82 int rc;
84 prockludge_mappings_t *pkm;
86 if (mdb_get_xdata("pshandle", &Pr, sizeof (Pr)) == -1) {
87 mdb_warn("couldn't read pshandle xdata");
88 return (WALK_ERR);
91 pkm = mdb_zalloc(sizeof (prockludge_mappings_t), UM_SLEEP);
92 pkm->pkm_Pr = Pr;
93 mws->walk_data = pkm;
95 rc = Pmapping_iter(Pr, (proc_map_f *)prockludge_mappings_iter, pkm);
96 if (rc != 0) {
97 mdb_warn("Pmapping_iter failed");
98 /* clean up */
99 prockludge_mappings_walk_fini(mws);
100 return (WALK_ERR);
102 return (WALK_NEXT);
106 prockludge_mappings_walk_step(mdb_walk_state_t *wsp)
108 prockludge_mappings_t *pkm = wsp->walk_data;
109 int status;
111 if (pkm->pkm_idx >= pkm->pkm_count)
112 return (WALK_DONE);
114 status = wsp->walk_callback(0, &pkm->pkm_mappings[pkm->pkm_idx++],
115 wsp->walk_cbdata);
117 return (status);
120 void
121 prockludge_mappings_walk_fini(mdb_walk_state_t *wsp)
123 prockludge_mappings_t *pkm = wsp->walk_data;
124 if (pkm != NULL) {
125 if (pkm->pkm_old_mappings != NULL) {
126 mdb_free(pkm->pkm_old_mappings,
127 sizeof (prmap_t) * pkm->pkm_old_max);
129 if (pkm->pkm_mappings &&
130 pkm->pkm_mappings != pkm->pkm_old_mappings) {
131 mdb_free(pkm->pkm_mappings,
132 sizeof (prmap_t) * pkm->pkm_max);
134 mdb_free(pkm, sizeof (prockludge_mappings_t));
138 static int add_count = 0;
140 void
141 prockludge_add_walkers(void)
143 mdb_walker_t w;
145 if (add_count++ == 0) {
146 w.walk_name = KLUDGE_MAPWALK_NAME;
147 w.walk_descr = "kludge: walk the process' prmap_ts";
148 w.walk_init = prockludge_mappings_walk_init;
149 w.walk_step = prockludge_mappings_walk_step;
150 w.walk_fini = prockludge_mappings_walk_fini;
151 w.walk_init_arg = NULL;
153 if (mdb_add_walker(&w) == -1) {
154 mdb_warn("unable to add walker "KLUDGE_MAPWALK_NAME);
159 void
160 prockludge_remove_walkers(void)
162 if (--add_count == 0) {
163 mdb_remove_walker(KLUDGE_MAPWALK_NAME);