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 2005 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_param.h>
30*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
31*0Sstevel@tonic-gate #include <mdb/mdb_ks.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "zone.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stddef.h>
36*0Sstevel@tonic-gate #include <sys/zone.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #define	ZONE_NAMELEN	20
39*0Sstevel@tonic-gate #ifdef _LP64
40*0Sstevel@tonic-gate #define	ZONE_PATHLEN	32
41*0Sstevel@tonic-gate #else
42*0Sstevel@tonic-gate #define	ZONE_PATHLEN	40
43*0Sstevel@tonic-gate #endif
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate int
46*0Sstevel@tonic-gate zoneprt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	zone_t zn;
49*0Sstevel@tonic-gate 	char name[ZONE_NAMELEN];
50*0Sstevel@tonic-gate 	char path[ZONE_PATHLEN];
51*0Sstevel@tonic-gate 	int len;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	if (argc != 0)
54*0Sstevel@tonic-gate 		return (DCMD_USAGE);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
57*0Sstevel@tonic-gate 		if (mdb_walk_dcmd("zone", "zone", argc, argv) == -1) {
58*0Sstevel@tonic-gate 			mdb_warn("can't walk zones");
59*0Sstevel@tonic-gate 			return (DCMD_ERR);
60*0Sstevel@tonic-gate 		}
61*0Sstevel@tonic-gate 		return (DCMD_OK);
62*0Sstevel@tonic-gate 	}
63*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
64*0Sstevel@tonic-gate 		mdb_printf("%<u>%?s %6s %-20s %-s%</u>\n",
65*0Sstevel@tonic-gate 		    "ADDR", "ID", "NAME", "PATH");
66*0Sstevel@tonic-gate 	}
67*0Sstevel@tonic-gate 	if (mdb_vread(&zn, sizeof (zone_t), addr) == -1) {
68*0Sstevel@tonic-gate 		mdb_warn("can't read zone_t structure at %p", addr);
69*0Sstevel@tonic-gate 		return (DCMD_ERR);
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 	len = mdb_readstr(name, ZONE_NAMELEN, (uintptr_t)zn.zone_name);
72*0Sstevel@tonic-gate 	if (len > 0) {
73*0Sstevel@tonic-gate 		if (len == ZONE_NAMELEN)
74*0Sstevel@tonic-gate 			(void) strcpy(&name[len - 4], "...");
75*0Sstevel@tonic-gate 	} else {
76*0Sstevel@tonic-gate 		(void) strcpy(name, "??");
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 	len = mdb_readstr(path, ZONE_PATHLEN, (uintptr_t)zn.zone_rootpath);
79*0Sstevel@tonic-gate 	if (len > 0) {
80*0Sstevel@tonic-gate 		if (len == ZONE_PATHLEN)
81*0Sstevel@tonic-gate 			(void) strcpy(&path[len - 4], "...");
82*0Sstevel@tonic-gate 	} else {
83*0Sstevel@tonic-gate 		(void) strcpy(path, "??");
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	mdb_printf("%0?p %6d %-20s %s\n", addr, zn.zone_id, name, path);
86*0Sstevel@tonic-gate 	return (DCMD_OK);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate int
90*0Sstevel@tonic-gate zone_walk_init(mdb_walk_state_t *wsp)
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate 	GElf_Sym sym;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
95*0Sstevel@tonic-gate 		if (mdb_lookup_by_name("zone_active", &sym) == -1) {
96*0Sstevel@tonic-gate 			mdb_warn("failed to find 'zone_active'");
97*0Sstevel@tonic-gate 			return (WALK_ERR);
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 		wsp->walk_addr = (uintptr_t)sym.st_value;
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1) {
102*0Sstevel@tonic-gate 		mdb_warn("couldn't walk 'list'");
103*0Sstevel@tonic-gate 		return (WALK_ERR);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	return (WALK_NEXT);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate int
109*0Sstevel@tonic-gate zone_walk_step(mdb_walk_state_t *wsp)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
112*0Sstevel@tonic-gate 		    wsp->walk_cbdata));
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate int
116*0Sstevel@tonic-gate zsd_walk_init(mdb_walk_state_t *wsp)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
119*0Sstevel@tonic-gate 		mdb_warn("global walk not supported\n");
120*0Sstevel@tonic-gate 		return (WALK_ERR);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	wsp->walk_addr += offsetof(struct zone, zone_zsd);
123*0Sstevel@tonic-gate 	if (mdb_layered_walk("list", wsp) == -1) {
124*0Sstevel@tonic-gate 		mdb_warn("couldn't walk 'list'");
125*0Sstevel@tonic-gate 		return (WALK_ERR);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	return (WALK_NEXT);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate int
131*0Sstevel@tonic-gate zsd_walk_step(mdb_walk_state_t *wsp)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
134*0Sstevel@tonic-gate 		    wsp->walk_cbdata));
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate struct zsd_cb_data {
138*0Sstevel@tonic-gate 	zone_key_t	key;
139*0Sstevel@tonic-gate 	int		found;
140*0Sstevel@tonic-gate };
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /* ARGSUSED */
143*0Sstevel@tonic-gate static int
144*0Sstevel@tonic-gate zsd_match(uintptr_t addr, const void *data, void *private)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	struct zsd_entry ze;
147*0Sstevel@tonic-gate 	struct zsd_cb_data *cbdata = private;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if (mdb_vread(&ze, sizeof (struct zsd_entry), addr) == -1) {
150*0Sstevel@tonic-gate 		mdb_warn("couldn't read zsd_entry at %p", addr);
151*0Sstevel@tonic-gate 		return (WALK_ERR);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 	if (ze.zsd_key != cbdata->key)
154*0Sstevel@tonic-gate 		return (WALK_NEXT);
155*0Sstevel@tonic-gate 	cbdata->found = 1;
156*0Sstevel@tonic-gate 	mdb_printf("%p\n", ze.zsd_data);
157*0Sstevel@tonic-gate 	return (WALK_DONE);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate int
161*0Sstevel@tonic-gate zsd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	zone_t zone;
164*0Sstevel@tonic-gate 	const mdb_arg_t *argp = &argv[0];
165*0Sstevel@tonic-gate 	zone_key_t key;
166*0Sstevel@tonic-gate 	struct zsd_cb_data cbd;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) == 0) {
169*0Sstevel@tonic-gate 		mdb_warn("address of zone not specified\n");
170*0Sstevel@tonic-gate 		return (DCMD_ERR);
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	if (argc != 1)
173*0Sstevel@tonic-gate 		return (DCMD_USAGE);
174*0Sstevel@tonic-gate 	if (argp->a_type == MDB_TYPE_IMMEDIATE)
175*0Sstevel@tonic-gate 		key = argp->a_un.a_val;
176*0Sstevel@tonic-gate 	else
177*0Sstevel@tonic-gate 		key = mdb_strtoull(argp->a_un.a_str);
178*0Sstevel@tonic-gate 	if (mdb_vread(&zone, sizeof (struct zone), addr) == -1) {
179*0Sstevel@tonic-gate 		mdb_warn("couldn't read zone_t at %p", addr);
180*0Sstevel@tonic-gate 		return (DCMD_ERR);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	cbd.key = key;
183*0Sstevel@tonic-gate 	cbd.found = 0;
184*0Sstevel@tonic-gate 	if (mdb_pwalk("zsd", zsd_match, &cbd, addr) != 0) {
185*0Sstevel@tonic-gate 		mdb_warn("failed to walk zsd\n");
186*0Sstevel@tonic-gate 		return (DCMD_ERR);
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 	if (cbd.found == 0) {
189*0Sstevel@tonic-gate 		mdb_warn("no corresponding ZSD value found for key %d\n",
190*0Sstevel@tonic-gate 		    key);
191*0Sstevel@tonic-gate 		return (DCMD_ERR);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 	return (DCMD_OK);
194*0Sstevel@tonic-gate }
195