summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/modules/genunix/zone.c
blob: 57545d94a31fc1ded239d6fccdae2f0e3053833a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2018, Joyent, Inc.  All rights reserved.
 */

#include <mdb/mdb_param.h>
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_ks.h>

#include "zone.h"

#include <stddef.h>
#include <sys/zone.h>

#define	ZONE_NAMELEN	20
#ifdef _LP64
#define	ZONE_PATHLEN	25
#else
#define	ZONE_PATHLEN	33
#endif

/*
 * Names corresponding to zone_status_t values in sys/zone.h
 */
char *zone_status_names[] = {
	"uninitialized",	/* ZONE_IS_UNINITIALIZED */
	"initialized",		/* ZONE_IS_INITIALIZED */
	"ready",		/* ZONE_IS_READY */
	"booting",		/* ZONE_IS_BOOTING */
	"running",		/* ZONE_IS_RUNNING */
	"shutting_down",	/* ZONE_IS_SHUTTING_DOWN */
	"empty",		/* ZONE_IS_EMPTY */
	"down",			/* ZONE_IS_DOWN */
	"dying",		/* ZONE_IS_DYING */
	"dead",			/* ZONE_IS_DEAD */
	"free"			/* ZONE_IS_FREE */
};

static int
zid_lookup_cb(uintptr_t addr, const zone_t *zone, void *arg)
{
	zoneid_t zid = *(uintptr_t *)arg;
	if (zone->zone_id == zid)
		mdb_printf("%p\n", addr);

	return (WALK_NEXT);
}

/*ARGSUSED*/
int
zid2zone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
		return (DCMD_USAGE);

	if (mdb_walk("zone", (mdb_walk_cb_t)zid_lookup_cb, &addr) == -1) {
		mdb_warn("failed to walk zone");
		return (DCMD_ERR);
	}

	return (DCMD_OK);
}

static int
zdid_lookup_cb(uintptr_t addr, const zone_t *zone, void *arg)
{
	zoneid_t zdid = *(uintptr_t *)arg;
	if (zone->zone_did == zdid)
		mdb_printf("%p\n", addr);

	return (WALK_NEXT);
}

/*ARGSUSED*/
int
zdid2zone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
		return (DCMD_USAGE);

	if (mdb_walk("zone", (mdb_walk_cb_t)zdid_lookup_cb, &addr) == -1) {
		mdb_warn("failed to walk zone");
		return (DCMD_ERR);
	}

	return (DCMD_OK);
}

int
zoneprt(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	zone_t zn;
	char name[ZONE_NAMELEN];
	char path[ZONE_PATHLEN];
	int len;
	uint_t vopt_given;
	uint_t ropt_given;

	if (argc > 2)
		return (DCMD_USAGE);

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("zone", "zone", argc, argv) == -1) {
			mdb_warn("can't walk zones");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	/*
	 * Get the optional -r (reference counts) and -v (verbose output)
	 * arguments.
	 */
	vopt_given = FALSE;
	ropt_given = FALSE;
	if (argc > 0 && mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE,
	    &vopt_given, 'r', MDB_OPT_SETBITS, TRUE, &ropt_given, NULL) != argc)
		return (DCMD_USAGE);

	/*
	 * -v can only be specified with -r.
	 */
	if (vopt_given == TRUE && ropt_given == FALSE)
		return (DCMD_USAGE);

	/*
	 * Print a table header, if necessary.
	 */
	if (DCMD_HDRSPEC(flags)) {
		if (ropt_given == FALSE)
			mdb_printf("%<u>%?s %4s %-13s %-19s %-s%</u>\n",
			    "ADDR", "ID", "STATUS", "NAME", "PATH");
		else
			mdb_printf("%<u>%?s %6s %10s %10s %-19s%</u>\n",
			    "ADDR", "ID", "REFS", "CREFS", "NAME");
	}

	/*
	 * Read the zone_t structure at the given address and read its name.
	 */
	if (mdb_vread(&zn, sizeof (zone_t), addr) == -1) {
		mdb_warn("can't read zone_t structure at %p", addr);
		return (DCMD_ERR);
	}
	len = mdb_readstr(name, ZONE_NAMELEN, (uintptr_t)zn.zone_name);
	if (len > 0) {
		if (len == ZONE_NAMELEN)
			(void) strcpy(&name[len - 4], "...");
	} else {
		(void) strcpy(name, "??");
	}

	if (ropt_given == FALSE) {
		char *statusp;

		/*
		 * Default display
		 * Fetch the zone's path and print the results.
		 */
		len = mdb_readstr(path, ZONE_PATHLEN,
		    (uintptr_t)zn.zone_rootpath);
		if (len > 0) {
			if (len == ZONE_PATHLEN)
				(void) strcpy(&path[len - 4], "...");
		} else {
			(void) strcpy(path, "??");
		}
		if (zn.zone_status >= ZONE_IS_UNINITIALIZED && zn.zone_status <=
		    ZONE_IS_DEAD)
			statusp = zone_status_names[zn.zone_status];
		else
			statusp = "???";
		mdb_printf("%0?p %4d %-13s %-19s %s\n", addr, zn.zone_id,
		    statusp, name, path);
	} else {
		/*
		 * Display the zone's reference counts.
		 * Display the zone's subsystem-specific reference counts if
		 * the user specified the '-v' option.
		 */
		mdb_printf("%0?p %6d %10u %10u %-19s\n", addr, zn.zone_id,
		    zn.zone_ref, zn.zone_cred_ref, name);
		if (vopt_given == TRUE) {
			GElf_Sym subsys_names_sym;
			uintptr_t **zone_ref_subsys_names;
			uint_t num_subsys;
			uint_t n;

			/*
			 * Read zone_ref_subsys_names from the kernel image.
			 */
			if (mdb_lookup_by_name("zone_ref_subsys_names",
			    &subsys_names_sym) != 0) {
				mdb_warn("can't find zone_ref_subsys_names");
				return (DCMD_ERR);
			}
			if (subsys_names_sym.st_size != ZONE_REF_NUM_SUBSYS *
			    sizeof (char *)) {
				mdb_warn("number of subsystems in target "
				    "differs from what mdb expects (mismatched"
				    " kernel versions?)");
				if (subsys_names_sym.st_size <
				    ZONE_REF_NUM_SUBSYS * sizeof (char *))
					num_subsys = subsys_names_sym.st_size /
					    sizeof (char *);
				else
					num_subsys = ZONE_REF_NUM_SUBSYS;
			} else {
				num_subsys = ZONE_REF_NUM_SUBSYS;
			}
			if ((zone_ref_subsys_names = mdb_alloc(
			    subsys_names_sym.st_size, UM_GC)) == NULL) {
				mdb_warn("out of memory");
				return (DCMD_ERR);
			}
			if (mdb_readvar(zone_ref_subsys_names,
			    "zone_ref_subsys_names") == -1) {
				mdb_warn("can't find zone_ref_subsys_names");
				return (DCMD_ERR);
			}

			/*
			 * Display each subsystem's reference count if it's
			 * nonzero.
			 */
			mdb_inc_indent(7);
			for (n = 0; n < num_subsys; ++n) {
				char subsys_name[16];

				/*
				 * Skip subsystems lacking outstanding
				 * references.
				 */
				if (zn.zone_subsys_ref[n] == 0)
					continue;

				/*
				 * Each subsystem's name must be read from
				 * the target's image.
				 */
				if (mdb_readstr(subsys_name,
				    sizeof (subsys_name),
				    (uintptr_t)zone_ref_subsys_names[n]) ==
				    -1) {
					mdb_warn("unable to read subsystem name"
					    " from zone_ref_subsys_names[%u]",
					    n);
					return (DCMD_ERR);
				}
				mdb_printf("%15s: %10u\n", subsys_name,
				    zn.zone_subsys_ref[n]);
			}
			mdb_dec_indent(7);
		}
	}
	return (DCMD_OK);
}

int
zone_walk_init(mdb_walk_state_t *wsp)
{
	GElf_Sym sym;

	if (wsp->walk_addr == 0) {
		if (mdb_lookup_by_name("zone_active", &sym) == -1) {
			mdb_warn("failed to find 'zone_active'");
			return (WALK_ERR);
		}
		wsp->walk_addr = (uintptr_t)sym.st_value;
	}
	if (mdb_layered_walk("list", wsp) == -1) {
		mdb_warn("couldn't walk 'list'");
		return (WALK_ERR);
	}
	return (WALK_NEXT);
}

int
zone_walk_step(mdb_walk_state_t *wsp)
{
	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
	    wsp->walk_cbdata));
}

int
zsd_walk_init(mdb_walk_state_t *wsp)
{
	if (wsp->walk_addr == 0) {
		mdb_warn("global walk not supported\n");
		return (WALK_ERR);
	}
	wsp->walk_addr += offsetof(struct zone, zone_zsd);
	if (mdb_layered_walk("list", wsp) == -1) {
		mdb_warn("couldn't walk 'list'");
		return (WALK_ERR);
	}
	return (WALK_NEXT);
}

int
zsd_walk_step(mdb_walk_state_t *wsp)
{
	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
	    wsp->walk_cbdata));
}

/*
 * Helper structure used when walking ZSD entries via zsd().
 */
struct zsd_cb_data {
	uint_t		keygiven;	/* Was a key specified (are we */
					/* searching for a specific ZSD */
					/* entry)? */
	zone_key_t	key;		/* Key of ZSD for which we're looking */
	uint_t		found;		/* Was the specific ZSD entry found? */
	uint_t		voptgiven;	/* Display verbose information? */
};

/*
 * Helper function for zsd() that displays information from a single ZSD struct.
 * 'datap' must point to a valid zsd_cb_data struct.
 */
/* ARGSUSED */
static int
zsd_print(uintptr_t addrp, const void * datap, void * privatep)
{
	struct zsd_entry entry;
	struct zsd_cb_data *cbdp;

	if (mdb_vread(&entry, sizeof (entry), addrp) == -1) {
		mdb_warn("couldn't read zsd_entry at %p", addrp);
		return (WALK_ERR);
	}
	cbdp = (struct zsd_cb_data *)privatep;

	/*
	 * Are we looking for a single entry specified by a key?  Then make sure
	 * that the current ZSD's key is what we're looking for.
	 */
	if (cbdp->keygiven == TRUE && cbdp->key != entry.zsd_key)
		return (WALK_NEXT);

	mdb_printf("%?x %0?p %8x\n", entry.zsd_key, entry.zsd_data,
	    entry.zsd_flags);
	if (cbdp->voptgiven == TRUE)
		mdb_printf("    Create CB:   %a\n    Shutdown CB: %a\n"
		    "    Destroy CB:  %a\n", entry.zsd_create,
		    entry.zsd_shutdown, entry.zsd_destroy);
	if (cbdp->keygiven == TRUE) {
		cbdp->found = TRUE;
		return (WALK_DONE);
	}
	return (WALK_NEXT);
}

int
zsd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	zone_t zone;
	const mdb_arg_t *argp;
	int argcindex;
	struct zsd_cb_data cbd;
	char name[ZONE_NAMELEN];
	int len;

	/*
	 * Walk all zones if necessary.
	 */
	if (argc > 2)
		return (DCMD_USAGE);
	if ((flags & DCMD_ADDRSPEC) == 0) {
		if (mdb_walk_dcmd("zone", "zsd", argc, argv) == -1) {
			mdb_warn("failed to walk zone\n");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	/*
	 * Make sure a zone_t can be read from the specified address.
	 */
	if (mdb_vread(&zone, sizeof (zone), addr) == -1) {
		mdb_warn("couldn't read zone_t at %p", (void *)addr);
		return (DCMD_ERR);
	}

	/*
	 * Get the optional arguments (key or -v or both).  Note that
	 * mdb_getopts() will not parse a key argument because it is not
	 * preceded by an option letter.  We'll get around this by requiring
	 * that all options precede the optional key argument.
	 */
	cbd.keygiven = FALSE;
	cbd.voptgiven = FALSE;
	if (argc > 0 && (argcindex = mdb_getopts(argc, argv, 'v',
	    MDB_OPT_SETBITS, TRUE, &cbd.voptgiven, NULL)) != argc) {
		/*
		 * No options may appear after the key.
		 */
		if (argcindex != argc - 1)
			return (DCMD_USAGE);

		/*
		 * The missed argument should be a key.
		 */
		argp = &argv[argcindex];
		if (argp->a_type == MDB_TYPE_IMMEDIATE)
			cbd.key = argp->a_un.a_val;
		else
			cbd.key = mdb_strtoull(argp->a_un.a_str);
		cbd.keygiven = TRUE;
		cbd.found = FALSE;
	}

	/*
	 * Prepare to output the specified zone's ZSD information.
	 */
	if (DCMD_HDRSPEC(flags))
		mdb_printf("%<u>%-19s %?s %?s %8s%</u>\n", "ZONE", "KEY",
		    "VALUE", "FLAGS");
	len = mdb_readstr(name, ZONE_NAMELEN, (uintptr_t)zone.zone_name);
	if (len > 0) {
		if (len == ZONE_NAMELEN)
			(void) strcpy(&name[len - 4], "...");
	} else {
		(void) strcpy(name, "??");
	}
	mdb_printf("%-19s ", name);

	/*
	 * Display the requested ZSD entries.
	 */
	mdb_inc_indent(21);
	if (mdb_pwalk("zsd", zsd_print, &cbd, addr) != 0) {
		mdb_warn("failed to walk zsd\n");
		mdb_dec_indent(21);
		return (DCMD_ERR);
	}
	if (cbd.keygiven == TRUE && cbd.found == FALSE) {
		mdb_printf("no corresponding ZSD entry found\n");
		mdb_dec_indent(21);
		return (DCMD_ERR);
	}
	mdb_dec_indent(21);
	return (DCMD_OK);
}