1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <unistd.h> 30*7c478bd9Sstevel@tonic-gate #include <strings.h> 31*7c478bd9Sstevel@tonic-gate #include "libnvpair.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * libnvpair - A tools library for manipulating <name, value> pairs. 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * This library provides routines packing an unpacking nv pairs 37*7c478bd9Sstevel@tonic-gate * for transporting data across process boundaries, transporting 38*7c478bd9Sstevel@tonic-gate * between kernel and userland, and possibly saving onto disk files. 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate static void 42*7c478bd9Sstevel@tonic-gate indent(FILE *fp, int depth) 43*7c478bd9Sstevel@tonic-gate { 44*7c478bd9Sstevel@tonic-gate while (depth-- > 0) 45*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\t"); 46*7c478bd9Sstevel@tonic-gate } 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * nvlist_print - Prints elements in an event buffer 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate static 52*7c478bd9Sstevel@tonic-gate void 53*7c478bd9Sstevel@tonic-gate nvlist_print_with_indent(FILE *fp, nvlist_t *nvl, int depth) 54*7c478bd9Sstevel@tonic-gate { 55*7c478bd9Sstevel@tonic-gate int i; 56*7c478bd9Sstevel@tonic-gate char *name; 57*7c478bd9Sstevel@tonic-gate uint_t nelem; 58*7c478bd9Sstevel@tonic-gate nvpair_t *nvp; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate if (nvl == NULL) 61*7c478bd9Sstevel@tonic-gate return; 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate indent(fp, depth); 64*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl)); 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, NULL); 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate while (nvp) { 69*7c478bd9Sstevel@tonic-gate data_type_t type = nvpair_type(nvp); 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate indent(fp, depth); 72*7c478bd9Sstevel@tonic-gate name = nvpair_name(nvp); 73*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\t%s =", name); 74*7c478bd9Sstevel@tonic-gate nelem = 0; 75*7c478bd9Sstevel@tonic-gate switch (type) { 76*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN: { 77*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 1"); 78*7c478bd9Sstevel@tonic-gate break; 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE: { 81*7c478bd9Sstevel@tonic-gate boolean_t val; 82*7c478bd9Sstevel@tonic-gate (void) nvpair_value_boolean_value(nvp, &val); 83*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val); 84*7c478bd9Sstevel@tonic-gate break; 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE: { 87*7c478bd9Sstevel@tonic-gate uchar_t val; 88*7c478bd9Sstevel@tonic-gate (void) nvpair_value_byte(nvp, &val); 89*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%2.2x", val); 90*7c478bd9Sstevel@tonic-gate break; 91*7c478bd9Sstevel@tonic-gate } 92*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8: { 93*7c478bd9Sstevel@tonic-gate int8_t val; 94*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int8(nvp, &val); 95*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val); 96*7c478bd9Sstevel@tonic-gate break; 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8: { 99*7c478bd9Sstevel@tonic-gate uint8_t val; 100*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint8(nvp, &val); 101*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val); 102*7c478bd9Sstevel@tonic-gate break; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16: { 105*7c478bd9Sstevel@tonic-gate int16_t val; 106*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int16(nvp, &val); 107*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val); 108*7c478bd9Sstevel@tonic-gate break; 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16: { 111*7c478bd9Sstevel@tonic-gate uint16_t val; 112*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint16(nvp, &val); 113*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val); 114*7c478bd9Sstevel@tonic-gate break; 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32: { 117*7c478bd9Sstevel@tonic-gate int32_t val; 118*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int32(nvp, &val); 119*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val); 120*7c478bd9Sstevel@tonic-gate break; 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32: { 123*7c478bd9Sstevel@tonic-gate uint32_t val; 124*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint32(nvp, &val); 125*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val); 126*7c478bd9Sstevel@tonic-gate break; 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64: { 129*7c478bd9Sstevel@tonic-gate int64_t val; 130*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int64(nvp, &val); 131*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %lld", (longlong_t)val); 132*7c478bd9Sstevel@tonic-gate break; 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64: { 135*7c478bd9Sstevel@tonic-gate uint64_t val; 136*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &val); 137*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%llx", (u_longlong_t)val); 138*7c478bd9Sstevel@tonic-gate break; 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING: { 141*7c478bd9Sstevel@tonic-gate char *val; 142*7c478bd9Sstevel@tonic-gate (void) nvpair_value_string(nvp, &val); 143*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %s", val); 144*7c478bd9Sstevel@tonic-gate break; 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY: { 147*7c478bd9Sstevel@tonic-gate boolean_t *val; 148*7c478bd9Sstevel@tonic-gate (void) nvpair_value_boolean_array(nvp, &val, &nelem); 149*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 150*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val[i]); 151*7c478bd9Sstevel@tonic-gate break; 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY: { 154*7c478bd9Sstevel@tonic-gate uchar_t *val; 155*7c478bd9Sstevel@tonic-gate (void) nvpair_value_byte_array(nvp, &val, &nelem); 156*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 157*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%2.2x", val[i]); 158*7c478bd9Sstevel@tonic-gate break; 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY: { 161*7c478bd9Sstevel@tonic-gate int8_t *val; 162*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int8_array(nvp, &val, &nelem); 163*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 164*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val[i]); 165*7c478bd9Sstevel@tonic-gate break; 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY: { 168*7c478bd9Sstevel@tonic-gate uint8_t *val; 169*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint8_array(nvp, &val, &nelem); 170*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 171*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val[i]); 172*7c478bd9Sstevel@tonic-gate break; 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY: { 175*7c478bd9Sstevel@tonic-gate int16_t *val; 176*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int16_array(nvp, &val, &nelem); 177*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 178*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val[i]); 179*7c478bd9Sstevel@tonic-gate break; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY: { 182*7c478bd9Sstevel@tonic-gate uint16_t *val; 183*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint16_array(nvp, &val, &nelem); 184*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 185*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val[i]); 186*7c478bd9Sstevel@tonic-gate break; 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY: { 189*7c478bd9Sstevel@tonic-gate int32_t *val; 190*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int32_array(nvp, &val, &nelem); 191*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 192*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %d", val[i]); 193*7c478bd9Sstevel@tonic-gate break; 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY: { 196*7c478bd9Sstevel@tonic-gate uint32_t *val; 197*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint32_array(nvp, &val, &nelem); 198*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 199*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%x", val[i]); 200*7c478bd9Sstevel@tonic-gate break; 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY: { 203*7c478bd9Sstevel@tonic-gate int64_t *val; 204*7c478bd9Sstevel@tonic-gate (void) nvpair_value_int64_array(nvp, &val, &nelem); 205*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 206*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %lld", (longlong_t)val[i]); 207*7c478bd9Sstevel@tonic-gate break; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY: { 210*7c478bd9Sstevel@tonic-gate uint64_t *val; 211*7c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64_array(nvp, &val, &nelem); 212*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 213*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%llx", 214*7c478bd9Sstevel@tonic-gate (u_longlong_t)val[i]); 215*7c478bd9Sstevel@tonic-gate break; 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY: { 218*7c478bd9Sstevel@tonic-gate char **val; 219*7c478bd9Sstevel@tonic-gate (void) nvpair_value_string_array(nvp, &val, &nelem); 220*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 221*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " %s", val[i]); 222*7c478bd9Sstevel@tonic-gate break; 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME: { 225*7c478bd9Sstevel@tonic-gate hrtime_t val; 226*7c478bd9Sstevel@tonic-gate (void) nvpair_value_hrtime(nvp, &val); 227*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0x%llx", val); 228*7c478bd9Sstevel@tonic-gate break; 229*7c478bd9Sstevel@tonic-gate } 230*7c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST: { 231*7c478bd9Sstevel@tonic-gate nvlist_t *val; 232*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist(nvp, &val); 233*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " (embedded nvlist)\n"); 234*7c478bd9Sstevel@tonic-gate nvlist_print_with_indent(fp, val, depth + 1); 235*7c478bd9Sstevel@tonic-gate indent(fp, depth + 1); 236*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "(end %s)\n", name); 237*7c478bd9Sstevel@tonic-gate break; 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: { 240*7c478bd9Sstevel@tonic-gate nvlist_t **val; 241*7c478bd9Sstevel@tonic-gate (void) nvpair_value_nvlist_array(nvp, &val, &nelem); 242*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " (array of embedded nvlists)\n"); 243*7c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) { 244*7c478bd9Sstevel@tonic-gate indent(fp, depth + 1); 245*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, 246*7c478bd9Sstevel@tonic-gate "(start %s[%d])\n", name, i); 247*7c478bd9Sstevel@tonic-gate nvlist_print_with_indent(fp, val[i], depth + 1); 248*7c478bd9Sstevel@tonic-gate indent(fp, depth + 1); 249*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "(end %s[%d])\n", name, i); 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate break; 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate default: 254*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " unknown data type (%d)", type); 255*7c478bd9Sstevel@tonic-gate break; 256*7c478bd9Sstevel@tonic-gate } 257*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n"); 258*7c478bd9Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp); 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate } 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate void 263*7c478bd9Sstevel@tonic-gate nvlist_print(FILE *fp, nvlist_t *nvl) 264*7c478bd9Sstevel@tonic-gate { 265*7c478bd9Sstevel@tonic-gate nvlist_print_with_indent(fp, nvl, 0); 266*7c478bd9Sstevel@tonic-gate } 267