xref: /titanic_41/usr/src/cmd/mdb/common/modules/md/walk_units.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "mdinclude.h"
30 
31 
32 typedef struct unit_data {
33 	int	nunits;
34 	int	do_all;
35 	int	setno;
36 } unit_data_t;
37 
38 /*
39  * walk the units
40  */
41 /* ARGSUSED */
42 int
units_walk_init(mdb_walk_state_t * wsp)43 units_walk_init(mdb_walk_state_t *wsp)
44 {
45 	uintptr_t	addr;
46 	int		i;
47 
48 	snarf_sets();
49 	wsp->walk_data = mdb_alloc(sizeof (unit_data_t), UM_SLEEP);
50 	/*
51 	 * walk_data contains the following information:
52 	 *	 nunits : the number of units of the set we've printed out.
53 	 *	 setno: set number we're printing out the information from.
54 	 *	 do_all: print all the sets on the system or not.
55 	 */
56 	((unit_data_t *)wsp->walk_data)->nunits = 0;
57 	if (wsp->walk_addr == NULL) {
58 		/* if no address is specified, walk all units of all sets */
59 		mdb_printf("Units for set number 0\n");
60 		addr = (uintptr_t)mdset[0].s_un;
61 		wsp->walk_addr = addr;
62 		((unit_data_t *)wsp->walk_data)->setno = 0;
63 		((unit_data_t *)wsp->walk_data)->do_all = 1;
64 	} else {
65 		/* walk the specified set */
66 		((unit_data_t *)wsp->walk_data)->do_all = 0;
67 		for (i = 0; i < md_nsets; i++) {
68 			if (mdset[i].s_db == (void **)wsp->walk_addr) {
69 				wsp->walk_addr = (uintptr_t)mdset[i].s_un;
70 				((unit_data_t *)wsp->walk_data)->setno = i;
71 				return (WALK_NEXT);
72 			}
73 		}
74 	}
75 	return (WALK_NEXT);
76 }
77 
78 int
units_walk_step(mdb_walk_state_t * wsp)79 units_walk_step(mdb_walk_state_t *wsp)
80 {
81 	int		status;
82 	unit_data_t	*un = (unit_data_t *)wsp->walk_data;
83 	void		**ptr;
84 
85 	if (un->nunits >= md_nunits) {
86 		un->setno += 1;
87 		if ((un->setno < md_nsets) && (un->do_all == 1)) {
88 			un->nunits = 0;
89 			wsp->walk_addr = (uintptr_t)mdset[un->setno].s_un;
90 			if (wsp->walk_addr != NULL)
91 				mdb_printf("Units for set number %d\n",
92 				    un->setno);
93 		} else {
94 			return (WALK_DONE);
95 		}
96 	}
97 
98 	if (wsp->walk_addr == NULL) {
99 		un->nunits = md_nunits;
100 		return (WALK_NEXT);
101 	}
102 
103 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
104 
105 	if (status != WALK_DONE) {
106 		ptr = (void **)wsp->walk_addr;
107 		ptr++;
108 		wsp->walk_addr = (uintptr_t)ptr;
109 		un->nunits += 1;
110 	}
111 	return (status);
112 }
113 
114 void
units_walk_fini(mdb_walk_state_t * wsp)115 units_walk_fini(mdb_walk_state_t *wsp)
116 {
117 	mdb_free(wsp->walk_data, sizeof (unit_data_t));
118 }
119