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