(一)利用 mdb 调试获取 nvlist_t 中 nvpair_t(name/value) 对

时间:2022-11-22 04:13:53

服务器:192.168.2.122

root@2236:~# mdb -k
> ::spa
ADDR                 STATE NAME                                                
ffffff02dfaea000    ACTIVE p3way
ffffff02e38c0000    ACTIVE p98          //选取存储池 p98 作为调试对象
ffffff02e7fb3000    ACTIVE p99
ffffff02d55b0000    ACTIVE rpool
> ffffff02e38c0000::print -at spa_t
ffffff02e38c0000 spa_t {
    ffffff02e38c0000 char [256] spa_name = [ "p98" ]
    ffffff02e38c0100 char *spa_comment = 0
    ffffff02e38c0108 avl_node_t spa_avl = {
        ffffff02e38c0108 struct avl_node *[2] avl_child = [ 0xffffff02dfaea108,
0xffffff02d55b0108 ]
        ffffff02e38c0118 uintptr_t avl_pcb = 0x2
    }
    ffffff02e38c0120 nvlist_t *spa_config = 0xffffff02dfaae820
    ffffff02e38c0128 nvlist_t *spa_config_syncing = 0
    ffffff02e38c0130 nvlist_t *spa_config_splitting = 0
    ffffff02e38c0138 nvlist_t *spa_load_info = 0xffffff02dfaae7f0

//spa_t 结构体中元素 nvlist_t *spa_config 的所有信息

> 0xffffff02dfaae820::print -at nvlist_t
ffffff02dfaae820 nvlist_t {
    ffffff02dfaae820 int32_t nvl_version = 0
    ffffff02dfaae824 uint32_t nvl_nvflag = 0x1
    ffffff02dfaae828 uint64_t nvl_priv = 0xffffff02e1f78b18
    ffffff02dfaae830 uint32_t nvl_flag = 0
    ffffff02dfaae834 int32_t nvl_pad = 0
}

> 0xffffff02e1f78b18::print -at nvpriv_t
ffffff02e1f78b18 nvpriv_t {
    ffffff02e1f78b18 i_nvp_t *nvp_list = 0xffffff02dfa99398
    ffffff02e1f78b20 i_nvp_t *nvp_last = 0xffffff02dfa305a8
    ffffff02e1f78b28 i_nvp_t *nvp_curr = 0
    ffffff02e1f78b30 nv_alloc_t *nvp_nva = nv_alloc_sleep_def
    ffffff02e1f78b38 uint32_t nvp_stat = 0
}
> 0xffffff02dfa99398::print -at i_nvp_t
ffffff02dfa99398 i_nvp_t {
    ffffff02dfa99398 union  _nvi_un = {
        ffffff02dfa99398 uint64_t _nvi_align = 0xffffff02e12eea78
        ffffff02dfa99398 struct  _nvi = {
            ffffff02dfa99398 i_nvp_t *_nvi_next = 0xffffff02e12eea78
            ffffff02dfa993a0 i_nvp_t *_nvi_prev = 0
        }
    }
    ffffff02dfa993a8 nvpair_t nvi_nvp = {
        ffffff02dfa993a8 int32_t nvp_size = 0x20
        ffffff02dfa993ac int16_t nvp_name_sz = 0x8
        ffffff02dfa993ae int16_t nvp_reserve = 0
        ffffff02dfa993b0 int32_t nvp_value_elem = 0x1
        ffffff02dfa993b4 data_type_t nvp_type = 8 (DATA_TYPE_UINT64)
    }
}

//nvpair_t 类型的大小
> ::sizeof nvpair_t
sizeof (nvpair_t) = 0x10

//nvpair_t 地址为 ffffff02dfa993a8 的属性名称为 version
> ffffff02dfa993b8::print -at char
ffffff02dfa993b8 char 'v'
> ffffff02dfa993b9::print -at char
ffffff02dfa993b9 char 'e'
> ffffff02dfa993ba::print -at char
ffffff02dfa993ba char 'r'
> ffffff02dfa993bb::print -at char
ffffff02dfa993bb char 's'
> ffffff02dfa993bc::print -at char
ffffff02dfa993bc char 'i'
> ffffff02dfa993bd::print -at char
ffffff02dfa993bd char 'o'
> ffffff02dfa993be::print -at char
ffffff02dfa993be char 'n'
> ffffff02dfa993bf::print -at char
ffffff02dfa993bf char '\0'

//地址为 ffffff02dfa993a8 的 nvpair_t 指向的下一个 nvpair_t 地址
> 0xffffff02e12eea78::print -at i_nvp_t
ffffff02e12eea78 i_nvp_t {
    ffffff02e12eea78 union  _nvi_un = {
        ffffff02e12eea78 uint64_t _nvi_align = 0xffffff02e1454d80
        ffffff02e12eea78 struct  _nvi = {
            ffffff02e12eea78 i_nvp_t *_nvi_next = 0xffffff02e1454d80
            ffffff02e12eea80 i_nvp_t *_nvi_prev = 0xffffff02dfa99398
        }
    }
    ffffff02e12eea88 nvpair_t nvi_nvp = {
        ffffff02e12eea88 int32_t nvp_size = 0x20
        ffffff02e12eea8c int16_t nvp_name_sz = 0x5
        ffffff02e12eea8e int16_t nvp_reserve = 0
        ffffff02e12eea90 int32_t nvp_value_elem = 0x1
        ffffff02e12eea94 data_type_t nvp_type = 9 (DATA_TYPE_STRING)
    }
}

//nvpair_t 地址为 ffffff02e12eea88 的属性名称为 name
> ffffff02e12eea98::print -at char      //计算得来
ffffff02e12eea98 char 'n'
> ffffff02e12eea99::print -at char
ffffff02e12eea99 char 'a'
> ffffff02e12eea9a::print -at char
ffffff02e12eea9a char 'm'
> ffffff02e12eea9b::print -at char
ffffff02e12eea9b char 'e'
> ffffff02e12eea9c::print -at char
ffffff02e12eea9c char '\0'
//nvpair_t 地址为 ffffff02e12eea88 的属性名称对应的属性值为 p98
> ffffff02e12eeaa0::print -at char       //计算得来
ffffff02e12eeaa0 char 'p'
> ffffff02e12eeaa1::print -at char
ffffff02e12eeaa1 char '9'
> ffffff02e12eeaa2::print -at char
ffffff02e12eeaa2 char '8'
> ffffff02e12eeaa3::print -at char
ffffff02e12eeaa3 char '\0'

//地址为 0xffffff02dfa305a8 的 nvpair_t 结构(spa_config的最后一个nvpair_t结构==>i_nvp_t *nvp_last = 0xffffff02dfa305a8)
> 0xffffff02dfa305a8::print -at i_nvp_t
ffffff02dfa305a8 i_nvp_t {
    ffffff02dfa305a8 union  _nvi_un = {
        ffffff02dfa305a8 uint64_t _nvi_align = 0
        ffffff02dfa305a8 struct  _nvi = {
            ffffff02dfa305a8 i_nvp_t *_nvi_next = 0
            ffffff02dfa305b0 i_nvp_t *_nvi_prev = 0xffffff02e3e72780
        }
    }
    ffffff02dfa305b8 nvpair_t nvi_nvp = {
        ffffff02dfa305b8 int32_t nvp_size = 0x40
        ffffff02dfa305bc int16_t nvp_name_sz = 0x12
        ffffff02dfa305be int16_t nvp_reserve = 0
        ffffff02dfa305c0 int32_t nvp_value_elem = 0x1
        ffffff02dfa305c4 data_type_t nvp_type = 0t19 (DATA_TYPE_NVLIST)
    }
}
> ffffff02dfa305c8::print -at char
ffffff02dfa305c8 char 'f'
> ffffff02dfa305c9::print -at char
ffffff02dfa305c9 char 'e'
> ffffff02dfa305ca::print -at char
ffffff02dfa305ca char 'a'
> ffffff02dfa305cb::print -at char
ffffff02dfa305cb char 't'
> ffffff02dfa305cc::print -at char
ffffff02dfa305cc char 'u'
> ffffff02dfa305cd::print -at char
ffffff02dfa305cd char 'r'
> ffffff02dfa305ce::print -at char
ffffff02dfa305ce char 'e'
> ffffff02dfa305cf::print -at char
ffffff02dfa305cf char 's'
> ffffff02dfa305d0::print -at char
ffffff02dfa305d0 char '_'
> ffffff02dfa305d1::print -at char
ffffff02dfa305d1 char 'f'
> ffffff02dfa305d2::print -at char
ffffff02dfa305d2 char 'o'
> ffffff02dfa305d3::print -at char
ffffff02dfa305d3 char 'r'
> ffffff02dfa305d4::print -at char
ffffff02dfa305d4 char '_'
> ffffff02dfa305d5::print -at char
ffffff02dfa305d5 char 'r'
> ffffff02dfa305d6::print -at char
ffffff02dfa305d6 char 'e'
> ffffff02dfa305d7::print -at char
ffffff02dfa305d7 char 'a'
> ffffff02dfa305d8::print -at char
ffffff02dfa305d8 char 'd'
> ffffff02dfa305d9::print -at char
ffffff02dfa305d9 char '\0'
==> 属性名称:features_for_read

//计算出属性值存放的地址为: ffffff02dfa305e0 -- DATA_TYPE_NVLIST类型
> ffffff02dfa305e0::print -at nvlist_t
ffffff02dfa305e0 nvlist_t {
    ffffff02dfa305e0 int32_t nvl_version = 0
    ffffff02dfa305e4 uint32_t nvl_nvflag = 0x1
    ffffff02dfa305e8 uint64_t nvl_priv = 0xffffff02e1b0cb08
    ffffff02dfa305f0 uint32_t nvl_flag = 0
    ffffff02dfa305f4 int32_t nvl_pad = 0
}

> 0xffffff02e1b0cb08::print -at nvpriv_t
ffffff02e1b0cb08 nvpriv_t {
    ffffff02e1b0cb08 i_nvp_t *nvp_list = 0
    ffffff02e1b0cb10 i_nvp_t *nvp_last = 0
    ffffff02e1b0cb18 i_nvp_t *nvp_curr = 0
    ffffff02e1b0cb20 nv_alloc_t *nvp_nva = nv_alloc_sleep_def
    ffffff02e1b0cb28 uint32_t nvp_stat = 0x1
}

//源代码中如何从 nvlist_t 获取 nvpair_t (name/value) 对

usr/src/lib/smbsrv/libmlsvc/common/dfs.c
rc = nvlist_lookup_string(nvl, "comment", &cmnt);
rc |= nvlist_lookup_string(nvl, "guid", &guid); int
nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
{
return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
} static int
nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
uint_t *nelem, void *data)
{
nvpriv_t *priv;
nvpair_t *nvp;
i_nvp_t *curr; if (name == NULL || nvl == NULL ||
(priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
return (EINVAL); if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
return (ENOTSUP); for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
nvp = &curr->nvi_nvp; if (strcmp(name, NVP_NAME(nvp)) == && NVP_TYPE(nvp) == type)
//116 #define NVP_NAME(nvp) ((char *)(nvp) + sizeof (nvpair_t))
return (nvpair_value_common(nvp, type, nelem, data));
} return (ENOENT);
} static int
nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
{
if (nvp == NULL || nvpair_type(nvp) != type)
return (EINVAL); /*
1256 * For non-array types, we copy the data.
1257 * For array types (including string), we set a pointer.
1258 */
switch (type) {
case DATA_TYPE_BOOLEAN:
if (nelem != NULL)
*nelem = ;
break; case DATA_TYPE_BOOLEAN_VALUE:
case DATA_TYPE_BYTE:
case DATA_TYPE_INT8:
case DATA_TYPE_UINT8:
case DATA_TYPE_INT16:
case DATA_TYPE_UINT16:
case DATA_TYPE_INT32:
case DATA_TYPE_UINT32:
case DATA_TYPE_INT64:
case DATA_TYPE_UINT64:
case DATA_TYPE_HRTIME:
#if !defined(_KERNEL)
case DATA_TYPE_DOUBLE:
#endif
if (data == NULL)
return (EINVAL);
bcopy(NVP_VALUE(nvp), data,
(size_t)i_get_value_size(type, NULL, ));
/*
112 #define NV_ALIGN(x) (((ulong_t)(x) + 7ul) & ~7ul)
119 #define NVP_VALUE(nvp) ((char *)(nvp) + NV_ALIGN(sizeof (nvpair_t) \
120 + (nvp)->nvp_name_sz))
*/
if (nelem != NULL)
*nelem = ;
break; case DATA_TYPE_NVLIST:
case DATA_TYPE_STRING:
if (data == NULL)
return (EINVAL);
*(void **)data = (void *)NVP_VALUE(nvp);
if (nelem != NULL)
*nelem = ;
break; case DATA_TYPE_BOOLEAN_ARRAY:
case DATA_TYPE_BYTE_ARRAY:
case DATA_TYPE_INT8_ARRAY:
case DATA_TYPE_UINT8_ARRAY:
case DATA_TYPE_INT16_ARRAY:
case DATA_TYPE_UINT16_ARRAY:
case DATA_TYPE_INT32_ARRAY:
case DATA_TYPE_UINT32_ARRAY:
case DATA_TYPE_INT64_ARRAY:
case DATA_TYPE_UINT64_ARRAY:
case DATA_TYPE_STRING_ARRAY:
case DATA_TYPE_NVLIST_ARRAY:
if (nelem == NULL || data == NULL)
return (EINVAL);
if ((*nelem = NVP_NELEM(nvp)) != )
*(void **)data = (void *)NVP_VALUE(nvp);
else
*(void **)data = NULL;
break; default:
return (ENOTSUP);
} return ();
}
//相关数据结构如下
/* nvlist header */
typedef struct nvlist {
int32_t nvl_version;
uint32_t nvl_nvflag; /* persistent flags */
uint64_t nvl_priv; /* ptr to private data if not packed */
uint32_t nvl_flag;
int32_t nvl_pad; /* currently not used, for alignment */
} nvlist_t; typedef struct {
i_nvp_t *nvp_list; /* linked list of nvpairs */
i_nvp_t *nvp_last; /* last nvpair */
i_nvp_t *nvp_curr; /* current walker nvpair */
nv_alloc_t *nvp_nva; /* pluggable allocator */
uint32_t nvp_stat; /* internal state */
} nvpriv_t; /*
44 * implementation linked list for pre-packed data
45 */
typedef struct i_nvp i_nvp_t; struct i_nvp {
union {
uint64_t _nvi_align; /* ensure alignment */
struct {
i_nvp_t *_nvi_next; /* pointer to next nvpair */
i_nvp_t *_nvi_prev; /* pointer to prev nvpair */
} _nvi;
} _nvi_un;
nvpair_t nvi_nvp; /* nvpair */
}; typedef struct nvpair {
int32_t nvp_size; /* size of this nvpair */
int16_t nvp_name_sz; /* length of name string */
int16_t nvp_reserve; /* not used */
int32_t nvp_value_elem; /* number of elements for array types */
data_type_t nvp_type; /* type of value */
/* name string */
/* aligned ptr array for string arrays */
/* aligned array of data for value */
} nvpair_t;