xref: /titanic_50/usr/src/cmd/mdb/common/modules/genunix/nvpair.c (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/sysinfo.h>
29 #include <sys/nvpair.h>
30 #include <sys/nvpair_impl.h>
31 
32 #include <ctype.h>
33 #include <mdb/mdb_modapi.h>
34 
35 #include "nvpair.h"
36 
37 #define	NVPAIR_VALUE_INDENT	4
38 #define	NELEM(a)		(sizeof (a) / sizeof ((a)[0]))
39 
40 /*
41  * nvpair walker
42  */
43 int
44 nvpair_walk_init(mdb_walk_state_t *wsp)
45 {
46 	nvlist_t nvlist;
47 	nvpriv_t nvpriv;
48 	i_nvp_t *tmp;
49 
50 	if (wsp->walk_addr == NULL) {
51 		mdb_warn("nvpair does not support global walks\n");
52 		return (WALK_ERR);
53 	}
54 
55 	if (mdb_vread(&nvlist, sizeof (nvlist), wsp->walk_addr) == -1) {
56 		mdb_warn("failed to read nvlist at %p", wsp->walk_addr);
57 		return (WALK_ERR);
58 	}
59 
60 	if (mdb_vread(&nvpriv, sizeof (nvpriv), nvlist.nvl_priv) == -1) {
61 		mdb_warn("failed to read nvpriv at %p", nvlist.nvl_priv);
62 		return (WALK_ERR);
63 	}
64 
65 	tmp = (i_nvp_t *)nvpriv.nvp_list;
66 	wsp->walk_addr = (uintptr_t)tmp;
67 	return (WALK_NEXT);
68 }
69 
70 int
71 nvpair_walk_step(mdb_walk_state_t *wsp)
72 {
73 	int	status;
74 	nvpair_t *nvpair;
75 	i_nvp_t i_nvp, *tmp;
76 
77 	if (wsp->walk_addr == NULL)
78 		return (WALK_DONE);
79 
80 	if (mdb_vread(&i_nvp, sizeof (i_nvp), wsp->walk_addr) == -1) {
81 		mdb_warn("failed to read i_nvp at %p", wsp->walk_addr);
82 		return (WALK_ERR);
83 	}
84 
85 	nvpair = &((i_nvp_t *)wsp->walk_addr)->nvi_nvp;
86 	status = wsp->walk_callback((uintptr_t)nvpair, NULL, wsp->walk_cbdata);
87 
88 	tmp = i_nvp.nvi_next;
89 	wsp->walk_addr = (uintptr_t)tmp;
90 	return (status);
91 }
92 
93 /*
94  * ::nvlist [-v]
95  *
96  * Print out an entire nvlist.  This is shorthand for '::walk nvpair |
97  * ::nvpair -rq'.  The '-v' option invokes '::nvpair' without the "-q" option.
98  */
99 /*ARGSUSED*/
100 int
101 print_nvlist(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
102 {
103 	int verbose = B_FALSE;
104 	mdb_arg_t v;
105 
106 	if (!(flags & DCMD_ADDRSPEC))
107 		return (DCMD_USAGE);
108 
109 	if (mdb_getopts(argc, argv,
110 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
111 	    NULL) != argc)
112 		return (DCMD_USAGE);
113 
114 	v.a_type = MDB_TYPE_STRING;
115 	if (verbose)
116 		v.a_un.a_str = "-r";
117 	else
118 		v.a_un.a_str = "-rq";
119 
120 	return (mdb_pwalk_dcmd("nvpair", "nvpair", 1, &v, addr));
121 }
122 
123 /*
124  * ::nvpair [-rq]
125  *
126  * 	-r	Recursively print any nvlist elements
127  * 	-q	Quiet mode; print members only as "name=value"
128  *
129  * Prints out a single nvpair.  By default, all information is printed.  When
130  * given the '-q' option, the type of elements is hidden, and elements are
131  * instead printed simply as 'name=value'.
132  */
133 typedef struct {
134 	data_type_t	type;
135 	int		elem_size;
136 	char		*type_name;
137 } nvpair_info_t;
138 
139 nvpair_info_t nvpair_info[] =  {
140 	{ DATA_TYPE_BOOLEAN,		1, "boolean" },
141 	{ DATA_TYPE_BOOLEAN_VALUE,	4, "boolean_value" },
142 	{ DATA_TYPE_BYTE,		1, "byte" },
143 	{ DATA_TYPE_INT8,		1, "int8" },
144 	{ DATA_TYPE_UINT8,		1, "uint8" },
145 	{ DATA_TYPE_INT16,		2, "int16" },
146 	{ DATA_TYPE_UINT16,		2, "uint16" },
147 	{ DATA_TYPE_INT32,		4, "int32" },
148 	{ DATA_TYPE_UINT32,		4, "uint32" },
149 	{ DATA_TYPE_INT64,		8, "int64" },
150 	{ DATA_TYPE_UINT64,		8, "uint64" },
151 	{ DATA_TYPE_STRING,		0, "string" },
152 	{ DATA_TYPE_NVLIST,		0, "nvpair_list" },
153 	{ DATA_TYPE_HRTIME,		8, "hrtime" },
154 	{ DATA_TYPE_BOOLEAN_ARRAY,	4, "boolean_array" },
155 	{ DATA_TYPE_BYTE_ARRAY,		1, "byte_array" },
156 	{ DATA_TYPE_INT8_ARRAY,		1, "int8_array" },
157 	{ DATA_TYPE_UINT8_ARRAY,	1, "uint8_array" },
158 	{ DATA_TYPE_INT16_ARRAY,	2, "int16_array" },
159 	{ DATA_TYPE_UINT16_ARRAY,	2, "uint16_array" },
160 	{ DATA_TYPE_INT32_ARRAY,	4, "int32_array" },
161 	{ DATA_TYPE_UINT32_ARRAY,	4, "uint32_array" },
162 	{ DATA_TYPE_INT64_ARRAY, 	8, "int64_array" },
163 	{ DATA_TYPE_UINT64_ARRAY,	8, "uint64_array" },
164 	{ DATA_TYPE_STRING_ARRAY,	0, "string_array" },
165 	{ DATA_TYPE_NVLIST_ARRAY,	0, "nvpair list_array" }
166 };
167 
168 static void
169 nvpair_print_value(char *data, int32_t elem_size, int32_t nelem,
170     data_type_t type)
171 {
172 	int32_t i;
173 
174 	if (elem_size == 0) {
175 		char *p = data;
176 
177 		/* print out all the strings */
178 		for (i = 0; i < nelem - 1; i++) {
179 			mdb_printf("'%s' + ", p);
180 			p += strlen(p) + 1;
181 		}
182 		mdb_printf("'%s'", p);
183 	} else if (type == DATA_TYPE_BOOLEAN_VALUE ||
184 	    type == DATA_TYPE_BOOLEAN_ARRAY) {
185 		/* LINTED - pointer alignment */
186 		boolean_t *p = (boolean_t *)data;
187 
188 		for (i = 0; i < nelem; i++) {
189 			if (i > 0)
190 				mdb_printf(".");
191 			mdb_printf("%d", p[i]);
192 		}
193 	} else {
194 		unsigned char	*p = (unsigned char *)data;
195 		int		size = elem_size * nelem;
196 
197 		/*
198 		 * if elem_size != 0 then we are printing out an array
199 		 * where each element is of elem_size
200 		 */
201 		mdb_nhconvert(p, p, elem_size);
202 		mdb_printf("%02x", *p);
203 		for (i = 1; i < size; i++) {
204 			if ((i % elem_size) == 0) {
205 				mdb_nhconvert(&p[i], &p[i], elem_size);
206 				mdb_printf(".");
207 			}
208 			mdb_printf("%02x", p[i]);
209 		}
210 	}
211 	mdb_printf("\n");
212 }
213 
214 /*ARGSUSED*/
215 int
216 nvpair_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
217 {
218 	nvpair_t	nvpair_tmp, *nvpair;
219 	int32_t		i, size, nelem, elem_size = 0;
220 	char		*data = NULL, *data_end = NULL;
221 	char		*type_name = NULL;
222 	data_type_t	type = DATA_TYPE_UNKNOWN;
223 	int		quiet = FALSE;
224 	int		recurse = FALSE;
225 
226 	if (!(flags & DCMD_ADDRSPEC))
227 		return (DCMD_USAGE);
228 
229 	if (mdb_getopts(argc, argv,
230 	    'r', MDB_OPT_SETBITS, TRUE, &recurse,
231 	    'q', MDB_OPT_SETBITS, TRUE, &quiet,
232 	    NULL) != argc)
233 		return (DCMD_USAGE);
234 
235 	/* read in the nvpair header so we can get the size */
236 	if (mdb_vread(&nvpair_tmp, sizeof (nvpair), addr) == -1) {
237 		mdb_warn("failed to read nvpair at %p", addr);
238 		return (DCMD_ERR);
239 	}
240 	size = NVP_SIZE(&nvpair_tmp);
241 	if (size == 0) {
242 		mdb_warn("nvpair of size zero at %p", addr);
243 		return (DCMD_OK);
244 	}
245 
246 	/* read in the entire nvpair */
247 	nvpair = mdb_alloc(size, UM_SLEEP | UM_GC);
248 	if (mdb_vread(nvpair, size, addr) == -1) {
249 		mdb_warn("failed to read nvpair and data at %p", addr);
250 		return (DCMD_ERR);
251 	}
252 
253 	/* lookup type decoding information for this nvpair */
254 	type = NVP_TYPE(nvpair);
255 	nelem = NVP_NELEM(nvpair);
256 	for (i = 0; i < NELEM(nvpair_info); i++) {
257 		if (nvpair_info[i].type == type) {
258 			elem_size = nvpair_info[i].elem_size;
259 			type_name = nvpair_info[i].type_name;
260 			break;
261 		}
262 	}
263 
264 	if (quiet) {
265 		mdb_printf("%s", NVP_NAME(nvpair));
266 	} else {
267 		/* print out the first line of nvpair info */
268 		mdb_printf("name='%s'", NVP_NAME(nvpair));
269 		if (type_name != NULL) {
270 			mdb_printf(" type=%s", type_name);
271 		} else {
272 			/*
273 			 * If the nvpair type is unknown we print the type
274 			 * number
275 			 */
276 			mdb_printf(" type=0x%x", type);
277 		}
278 		mdb_printf(" items=%d\n", nelem);
279 	}
280 
281 	/* if there is no data and the type is known then we're done */
282 	if ((nelem == 0) && (type_name != NULL)) {
283 		if (quiet)
284 			mdb_printf("(unknown)\n");
285 		return (DCMD_OK);
286 	}
287 
288 	/* get pointers to the data to print out */
289 	data = (char *)NVP_VALUE(nvpair);
290 	data_end = (char *)nvpair + NVP_SIZE(nvpair);
291 
292 	/*
293 	 * The value of the name-value pair for a single embedded
294 	 * list is the nvlist_t structure for the embedded list.
295 	 * So we print that address out (computed as an offset from
296 	 * the nvpair address we received as addr).
297 	 *
298 	 * The value of the name-value pair for an array of embedded
299 	 * lists is nelem pointers to nvlist_t structures followed
300 	 * by the structures themselves.  We display the list
301 	 * of pointers as the pair's value.
302 	 */
303 	if (type == DATA_TYPE_NVLIST) {
304 		char *p = (char *)addr + (data - (char *)nvpair);
305 		if (recurse) {
306 			if (quiet)
307 				mdb_printf("\n");
308 			mdb_inc_indent(NVPAIR_VALUE_INDENT);
309 			if (mdb_pwalk_dcmd("nvpair", "nvpair", argc, argv,
310 			    (uintptr_t)p) != DCMD_OK)
311 				return (DCMD_ERR);
312 			mdb_dec_indent(NVPAIR_VALUE_INDENT);
313 		} else {
314 			if (!quiet) {
315 				mdb_inc_indent(NVPAIR_VALUE_INDENT);
316 				mdb_printf("value", p);
317 			}
318 			mdb_printf("=%p\n", p);
319 			if (!quiet)
320 				mdb_dec_indent(NVPAIR_VALUE_INDENT);
321 		}
322 		return (DCMD_OK);
323 
324 	} else if (type == DATA_TYPE_NVLIST_ARRAY) {
325 		if (recurse) {
326 			for (i = 0; i < nelem; i++,
327 			    data += sizeof (nvlist_t *)) {
328 				nvlist_t **nl = (nvlist_t **)(void *)data;
329 				if (quiet && i != 0)
330 					mdb_printf("%s", NVP_NAME(nvpair));
331 				mdb_printf("[%d]\n", i);
332 				mdb_inc_indent(NVPAIR_VALUE_INDENT);
333 				if (mdb_pwalk_dcmd("nvpair", "nvpair", argc,
334 				    argv, (uintptr_t)*nl) != DCMD_OK)
335 					return (DCMD_ERR);
336 				mdb_dec_indent(NVPAIR_VALUE_INDENT);
337 			}
338 		} else {
339 			if (!quiet) {
340 				mdb_inc_indent(NVPAIR_VALUE_INDENT);
341 				mdb_printf("value");
342 			}
343 			mdb_printf("=");
344 			for (i = 0; i < nelem; i++,
345 			    data += sizeof (nvlist_t *)) {
346 				nvlist_t **nl = (nvlist_t **)(void *)data;
347 				mdb_printf("%c%p", " "[i == 0], *nl);
348 			}
349 			mdb_printf("\n");
350 			if (!quiet)
351 				mdb_dec_indent(NVPAIR_VALUE_INDENT);
352 		}
353 		return (DCMD_OK);
354 	}
355 
356 	/* if it's a string array, skip the index pointers */
357 	if (type == DATA_TYPE_STRING_ARRAY)
358 		data += (sizeof (int64_t) * nelem);
359 
360 	/* if the type is unknown, treat the data as a byte array */
361 	if (type_name == NULL) {
362 		elem_size = 1;
363 		nelem = data_end - data;
364 	}
365 
366 	/*
367 	 * if the type is of strings, make sure they are printable
368 	 * otherwise print them out as byte arrays
369 	 */
370 	if (elem_size == 0) {
371 		int32_t	count = 0;
372 
373 		i = 0;
374 		while ((&data[i] < data_end) && (count < nelem)) {
375 			if (data[i] == '\0')
376 				count++;
377 			else if (!isprint(data[i]))
378 				break;
379 			i++;
380 		}
381 		if (count != nelem) {
382 			/* there is unprintable data, output as byte array */
383 			elem_size = 1;
384 			nelem =  data_end - data;
385 		}
386 	}
387 
388 	if (!quiet) {
389 		mdb_inc_indent(NVPAIR_VALUE_INDENT);
390 		mdb_printf("value=");
391 	} else {
392 		mdb_printf("=");
393 	}
394 	nvpair_print_value(data, elem_size, nelem, type);
395 	if (!quiet)
396 		mdb_dec_indent(NVPAIR_VALUE_INDENT);
397 
398 	return (DCMD_OK);
399 }
400