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