xref: /onnv-gate/usr/src/cmd/mdb/common/modules/genunix/group.c (revision 3434:5142e1d7d0bc)
1*3434Sesaxe /*
2*3434Sesaxe  * CDDL HEADER START
3*3434Sesaxe  *
4*3434Sesaxe  * The contents of this file are subject to the terms of the
5*3434Sesaxe  * Common Development and Distribution License (the "License").
6*3434Sesaxe  * You may not use this file except in compliance with the License.
7*3434Sesaxe  *
8*3434Sesaxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3434Sesaxe  * or http://www.opensolaris.org/os/licensing.
10*3434Sesaxe  * See the License for the specific language governing permissions
11*3434Sesaxe  * and limitations under the License.
12*3434Sesaxe  *
13*3434Sesaxe  * When distributing Covered Code, include this CDDL HEADER in each
14*3434Sesaxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3434Sesaxe  * If applicable, add the following below this CDDL HEADER, with the
16*3434Sesaxe  * fields enclosed by brackets "[]" replaced with your own identifying
17*3434Sesaxe  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3434Sesaxe  *
19*3434Sesaxe  * CDDL HEADER END
20*3434Sesaxe  */
21*3434Sesaxe 
22*3434Sesaxe /*
23*3434Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3434Sesaxe  * Use is subject to license terms.
25*3434Sesaxe  */
26*3434Sesaxe 
27*3434Sesaxe #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*3434Sesaxe 
29*3434Sesaxe /*
30*3434Sesaxe  * Display group information and walk all elements of a group
31*3434Sesaxe  */
32*3434Sesaxe 
33*3434Sesaxe #include "group.h"
34*3434Sesaxe 
35*3434Sesaxe #include <mdb/mdb_modapi.h>
36*3434Sesaxe #include <sys/group.h>
37*3434Sesaxe 
38*3434Sesaxe /*
39*3434Sesaxe  * Display group information
40*3434Sesaxe  */
41*3434Sesaxe 
42*3434Sesaxe /* ARGSUSED */
43*3434Sesaxe int
group(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)44*3434Sesaxe group(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
45*3434Sesaxe {
46*3434Sesaxe 	group_t	group;
47*3434Sesaxe 	int	opt_q = 0; /* display only address. */
48*3434Sesaxe 
49*3434Sesaxe 	/* Should provide an address */
50*3434Sesaxe 	if (!(flags & DCMD_ADDRSPEC))
51*3434Sesaxe 		return (DCMD_USAGE);
52*3434Sesaxe 
53*3434Sesaxe 	if (mdb_getopts(argc, argv,
54*3434Sesaxe 	    'q', MDB_OPT_SETBITS, TRUE, &opt_q,
55*3434Sesaxe 		NULL) != argc)
56*3434Sesaxe 		return (DCMD_USAGE);
57*3434Sesaxe 
58*3434Sesaxe 	if (flags & DCMD_PIPE_OUT)
59*3434Sesaxe 		opt_q = B_TRUE;
60*3434Sesaxe 
61*3434Sesaxe 	if (DCMD_HDRSPEC(flags) && !opt_q) {
62*3434Sesaxe 		mdb_printf("%?s %6s %9s %?s\n",
63*3434Sesaxe 		    "ADDR",
64*3434Sesaxe 		    "SIZE",
65*3434Sesaxe 		    "CAPACITY",
66*3434Sesaxe 		    "SET");
67*3434Sesaxe 	}
68*3434Sesaxe 
69*3434Sesaxe 	if (mdb_vread(&group, sizeof (struct group), addr) == -1) {
70*3434Sesaxe 		mdb_warn("unable to read 'group' at %p", addr);
71*3434Sesaxe 		return (DCMD_ERR);
72*3434Sesaxe 	}
73*3434Sesaxe 
74*3434Sesaxe 	if (opt_q) {
75*3434Sesaxe 		mdb_printf("%0?p\n", addr);
76*3434Sesaxe 		return (DCMD_OK);
77*3434Sesaxe 	}
78*3434Sesaxe 
79*3434Sesaxe 	mdb_printf("%?p %6d %9d %?p\n",
80*3434Sesaxe 	    addr, group.grp_size, group.grp_capacity, group.grp_set);
81*3434Sesaxe 
82*3434Sesaxe 	return (DCMD_OK);
83*3434Sesaxe }
84*3434Sesaxe 
85*3434Sesaxe /*
86*3434Sesaxe  * Walk all elements in the group set.
87*3434Sesaxe  */
88*3434Sesaxe 
89*3434Sesaxe typedef struct group_walk {
90*3434Sesaxe 	uintptr_t *gw_set;
91*3434Sesaxe 	int gw_size;
92*3434Sesaxe 	int gw_pos;
93*3434Sesaxe 	int gw_initialized;
94*3434Sesaxe } group_walk_t;
95*3434Sesaxe 
96*3434Sesaxe 
97*3434Sesaxe /*
98*3434Sesaxe  * Initialize the walk structure with the copy of a group set, its size and the
99*3434Sesaxe  * initial pointer position.
100*3434Sesaxe  */
101*3434Sesaxe int
group_walk_init(mdb_walk_state_t * wsp)102*3434Sesaxe group_walk_init(mdb_walk_state_t *wsp)
103*3434Sesaxe {
104*3434Sesaxe 	group_walk_t	*gw;
105*3434Sesaxe 	group_t		group;
106*3434Sesaxe 
107*3434Sesaxe 	gw = mdb_alloc(sizeof (group_walk_t), UM_SLEEP | UM_GC);
108*3434Sesaxe 
109*3434Sesaxe 	if (mdb_vread(&group, sizeof (struct group), wsp->walk_addr) == -1) {
110*3434Sesaxe 		mdb_warn("couldn't read 'group' at %p", wsp->walk_addr);
111*3434Sesaxe 		return (WALK_ERR);
112*3434Sesaxe 	}
113*3434Sesaxe 
114*3434Sesaxe 	gw->gw_size = group.grp_size;
115*3434Sesaxe 	gw->gw_initialized = 0;
116*3434Sesaxe 	gw->gw_pos = 0;
117*3434Sesaxe 
118*3434Sesaxe 	if (gw->gw_size < 0) {
119*3434Sesaxe 		mdb_warn("invalid group at %p", wsp->walk_addr);
120*3434Sesaxe 		return (WALK_ERR);
121*3434Sesaxe 	}
122*3434Sesaxe 
123*3434Sesaxe 	if (gw->gw_size == 0)
124*3434Sesaxe 		return (WALK_DONE);
125*3434Sesaxe 
126*3434Sesaxe 	/*
127*3434Sesaxe 	 * Allocate space for the set and copy all set entries.
128*3434Sesaxe 	 */
129*3434Sesaxe 	gw->gw_set = mdb_alloc(group.grp_size * sizeof (uintptr_t),
130*3434Sesaxe 	    UM_SLEEP | UM_GC);
131*3434Sesaxe 
132*3434Sesaxe 	if (mdb_vread(gw->gw_set, group.grp_size * sizeof (uintptr_t),
133*3434Sesaxe 	    (uintptr_t)group.grp_set) == -1) {
134*3434Sesaxe 		mdb_warn("couldn't read 'group set' at %p", group.grp_set);
135*3434Sesaxe 		return (WALK_ERR);
136*3434Sesaxe 	}
137*3434Sesaxe 
138*3434Sesaxe 	wsp->walk_data = gw;
139*3434Sesaxe 	wsp->walk_addr = gw->gw_set[0];
140*3434Sesaxe 	gw->gw_pos = 0;
141*3434Sesaxe 
142*3434Sesaxe 	return (WALK_NEXT);
143*3434Sesaxe }
144*3434Sesaxe 
145*3434Sesaxe /*
146*3434Sesaxe  * Print element of the set and advance the pointer.
147*3434Sesaxe  */
148*3434Sesaxe int
group_walk_step(mdb_walk_state_t * wsp)149*3434Sesaxe group_walk_step(mdb_walk_state_t *wsp)
150*3434Sesaxe {
151*3434Sesaxe 	group_walk_t *gw = (group_walk_t *)wsp->walk_data;
152*3434Sesaxe 	int status;
153*3434Sesaxe 
154*3434Sesaxe 	/*
155*3434Sesaxe 	 * Already visited all valid elements, nothing else to do.
156*3434Sesaxe 	 */
157*3434Sesaxe 	if (gw->gw_size < 0)
158*3434Sesaxe 		return (WALK_DONE);
159*3434Sesaxe 
160*3434Sesaxe 	/*
161*3434Sesaxe 	 * Print non-NULL elements
162*3434Sesaxe 	 */
163*3434Sesaxe 	status = wsp->walk_addr == NULL ?
164*3434Sesaxe 	    WALK_NEXT :
165*3434Sesaxe 	    wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
166*3434Sesaxe 		wsp->walk_cbdata);
167*3434Sesaxe 
168*3434Sesaxe 	/*
169*3434Sesaxe 	 * Adjust walk_addr to point to the next element
170*3434Sesaxe 	 */
171*3434Sesaxe 	gw->gw_size--;
172*3434Sesaxe 
173*3434Sesaxe 	if (gw->gw_size > 0)
174*3434Sesaxe 		wsp->walk_addr = gw->gw_set[++gw->gw_pos];
175*3434Sesaxe 	else
176*3434Sesaxe 		status = WALK_DONE;
177*3434Sesaxe 
178*3434Sesaxe 	return (status);
179*3434Sesaxe }
180