xref: /titanic_44/usr/src/lib/libnvpair/libnvpair.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
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 2004 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 <unistd.h>
30 #include <strings.h>
31 #include "libnvpair.h"
32 
33 /*
34  * libnvpair - A tools library for manipulating <name, value> pairs.
35  *
36  *	This library provides routines packing an unpacking nv pairs
37  *	for transporting data across process boundaries, transporting
38  *	between kernel and userland, and possibly saving onto disk files.
39  */
40 
41 static void
42 indent(FILE *fp, int depth)
43 {
44 	while (depth-- > 0)
45 		(void) fprintf(fp, "\t");
46 }
47 
48 /*
49  * nvlist_print - Prints elements in an event buffer
50  */
51 static
52 void
53 nvlist_print_with_indent(FILE *fp, nvlist_t *nvl, int depth)
54 {
55 	int i;
56 	char *name;
57 	uint_t nelem;
58 	nvpair_t *nvp;
59 
60 	if (nvl == NULL)
61 		return;
62 
63 	indent(fp, depth);
64 	(void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
65 
66 	nvp = nvlist_next_nvpair(nvl, NULL);
67 
68 	while (nvp) {
69 		data_type_t type = nvpair_type(nvp);
70 
71 		indent(fp, depth);
72 		name = nvpair_name(nvp);
73 		(void) fprintf(fp, "\t%s =", name);
74 		nelem = 0;
75 		switch (type) {
76 		case DATA_TYPE_BOOLEAN: {
77 			(void) fprintf(fp, " 1");
78 			break;
79 		}
80 		case DATA_TYPE_BOOLEAN_VALUE: {
81 			boolean_t val;
82 			(void) nvpair_value_boolean_value(nvp, &val);
83 			(void) fprintf(fp, " %d", val);
84 			break;
85 		}
86 		case DATA_TYPE_BYTE: {
87 			uchar_t val;
88 			(void) nvpair_value_byte(nvp, &val);
89 			(void) fprintf(fp, " 0x%2.2x", val);
90 			break;
91 		}
92 		case DATA_TYPE_INT8: {
93 			int8_t val;
94 			(void) nvpair_value_int8(nvp, &val);
95 			(void) fprintf(fp, " %d", val);
96 			break;
97 		}
98 		case DATA_TYPE_UINT8: {
99 			uint8_t val;
100 			(void) nvpair_value_uint8(nvp, &val);
101 			(void) fprintf(fp, " 0x%x", val);
102 			break;
103 		}
104 		case DATA_TYPE_INT16: {
105 			int16_t val;
106 			(void) nvpair_value_int16(nvp, &val);
107 			(void) fprintf(fp, " %d", val);
108 			break;
109 		}
110 		case DATA_TYPE_UINT16: {
111 			uint16_t val;
112 			(void) nvpair_value_uint16(nvp, &val);
113 			(void) fprintf(fp, " 0x%x", val);
114 			break;
115 		}
116 		case DATA_TYPE_INT32: {
117 			int32_t val;
118 			(void) nvpair_value_int32(nvp, &val);
119 			(void) fprintf(fp, " %d", val);
120 			break;
121 		}
122 		case DATA_TYPE_UINT32: {
123 			uint32_t val;
124 			(void) nvpair_value_uint32(nvp, &val);
125 			(void) fprintf(fp, " 0x%x", val);
126 			break;
127 		}
128 		case DATA_TYPE_INT64: {
129 			int64_t val;
130 			(void) nvpair_value_int64(nvp, &val);
131 			(void) fprintf(fp, " %lld", (longlong_t)val);
132 			break;
133 		}
134 		case DATA_TYPE_UINT64: {
135 			uint64_t val;
136 			(void) nvpair_value_uint64(nvp, &val);
137 			(void) fprintf(fp, " 0x%llx", (u_longlong_t)val);
138 			break;
139 		}
140 		case DATA_TYPE_STRING: {
141 			char *val;
142 			(void) nvpair_value_string(nvp, &val);
143 			(void) fprintf(fp, " %s", val);
144 			break;
145 		}
146 		case DATA_TYPE_BOOLEAN_ARRAY: {
147 			boolean_t *val;
148 			(void) nvpair_value_boolean_array(nvp, &val, &nelem);
149 			for (i = 0; i < nelem; i++)
150 				(void) fprintf(fp, " %d", val[i]);
151 			break;
152 		}
153 		case DATA_TYPE_BYTE_ARRAY: {
154 			uchar_t *val;
155 			(void) nvpair_value_byte_array(nvp, &val, &nelem);
156 			for (i = 0; i < nelem; i++)
157 				(void) fprintf(fp, " 0x%2.2x", val[i]);
158 			break;
159 		}
160 		case DATA_TYPE_INT8_ARRAY: {
161 			int8_t *val;
162 			(void) nvpair_value_int8_array(nvp, &val, &nelem);
163 			for (i = 0; i < nelem; i++)
164 				(void) fprintf(fp, " %d", val[i]);
165 			break;
166 		}
167 		case DATA_TYPE_UINT8_ARRAY: {
168 			uint8_t *val;
169 			(void) nvpair_value_uint8_array(nvp, &val, &nelem);
170 			for (i = 0; i < nelem; i++)
171 				(void) fprintf(fp, " 0x%x", val[i]);
172 			break;
173 		}
174 		case DATA_TYPE_INT16_ARRAY: {
175 			int16_t *val;
176 			(void) nvpair_value_int16_array(nvp, &val, &nelem);
177 			for (i = 0; i < nelem; i++)
178 				(void) fprintf(fp, " %d", val[i]);
179 			break;
180 		}
181 		case DATA_TYPE_UINT16_ARRAY: {
182 			uint16_t *val;
183 			(void) nvpair_value_uint16_array(nvp, &val, &nelem);
184 			for (i = 0; i < nelem; i++)
185 				(void) fprintf(fp, " 0x%x", val[i]);
186 			break;
187 		}
188 		case DATA_TYPE_INT32_ARRAY: {
189 			int32_t *val;
190 			(void) nvpair_value_int32_array(nvp, &val, &nelem);
191 			for (i = 0; i < nelem; i++)
192 				(void) fprintf(fp, " %d", val[i]);
193 			break;
194 		}
195 		case DATA_TYPE_UINT32_ARRAY: {
196 			uint32_t *val;
197 			(void) nvpair_value_uint32_array(nvp, &val, &nelem);
198 			for (i = 0; i < nelem; i++)
199 				(void) fprintf(fp, " 0x%x", val[i]);
200 			break;
201 		}
202 		case DATA_TYPE_INT64_ARRAY: {
203 			int64_t *val;
204 			(void) nvpair_value_int64_array(nvp, &val, &nelem);
205 			for (i = 0; i < nelem; i++)
206 				(void) fprintf(fp, " %lld", (longlong_t)val[i]);
207 			break;
208 		}
209 		case DATA_TYPE_UINT64_ARRAY: {
210 			uint64_t *val;
211 			(void) nvpair_value_uint64_array(nvp, &val, &nelem);
212 			for (i = 0; i < nelem; i++)
213 				(void) fprintf(fp, " 0x%llx",
214 				    (u_longlong_t)val[i]);
215 			break;
216 		}
217 		case DATA_TYPE_STRING_ARRAY: {
218 			char **val;
219 			(void) nvpair_value_string_array(nvp, &val, &nelem);
220 			for (i = 0; i < nelem; i++)
221 				(void) fprintf(fp, " %s", val[i]);
222 			break;
223 		}
224 		case DATA_TYPE_HRTIME: {
225 			hrtime_t val;
226 			(void) nvpair_value_hrtime(nvp, &val);
227 			(void) fprintf(fp, " 0x%llx", val);
228 			break;
229 		}
230 		case DATA_TYPE_NVLIST: {
231 			nvlist_t *val;
232 			(void) nvpair_value_nvlist(nvp, &val);
233 			(void) fprintf(fp, " (embedded nvlist)\n");
234 			nvlist_print_with_indent(fp, val, depth + 1);
235 			indent(fp, depth + 1);
236 			(void) fprintf(fp, "(end %s)\n", name);
237 			break;
238 		}
239 		case DATA_TYPE_NVLIST_ARRAY: {
240 			nvlist_t **val;
241 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
242 			(void) fprintf(fp, " (array of embedded nvlists)\n");
243 			for (i = 0; i < nelem; i++) {
244 				indent(fp, depth + 1);
245 				(void) fprintf(fp,
246 				    "(start %s[%d])\n", name, i);
247 				nvlist_print_with_indent(fp, val[i], depth + 1);
248 				indent(fp, depth + 1);
249 				(void) fprintf(fp, "(end %s[%d])\n", name, i);
250 			}
251 			break;
252 		}
253 		default:
254 			(void) fprintf(fp, " unknown data type (%d)", type);
255 			break;
256 		}
257 		(void) fprintf(fp, "\n");
258 		nvp = nvlist_next_nvpair(nvl, nvp);
259 	}
260 }
261 
262 void
263 nvlist_print(FILE *fp, nvlist_t *nvl)
264 {
265 	nvlist_print_with_indent(fp, nvl, 0);
266 }
267