|
| 1 | +# |
| 2 | +# Copyright 2020 Datto, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +# pylint: disable=missing-docstring |
| 18 | + |
| 19 | +import os |
| 20 | +import drgn |
| 21 | +import sdb |
| 22 | + |
| 23 | + |
| 24 | +class Nvpair: |
| 25 | + # Member data and methods to print the value of the |
| 26 | + # various nvpair_t types |
| 27 | + def __init__(self, nvp: drgn.Object, nvl: drgn.Object) -> None: |
| 28 | + self.addr = nvp.address_ |
| 29 | + self.nvp = nvp |
| 30 | + self.nvl = nvl |
| 31 | + self.nvp_size_ = sdb.type_canonicalize_size("nvpair_t") |
| 32 | + self.nvl_size = sdb.type_canonicalize_size("nvlist_t") |
| 33 | + self.name_size = int(nvp.nvp_name_sz) |
| 34 | + self.data_ptr = self.addr + ((self.nvp_size_ + self.name_size + 7) & ~7) |
| 35 | + |
| 36 | + def get_type(self, strip: int = 0) -> str: |
| 37 | + # N.B. data_type_t enum is not zero indexed it starts |
| 38 | + # with -1 so we add one to get the correct index |
| 39 | + fields = sdb.get_type("data_type_t").type.enumerators |
| 40 | + enum_string: str = fields[self.nvp.nvp_type + 1].name |
| 41 | + if strip: |
| 42 | + prefix = os.path.commonprefix([f[0] for f in fields]) |
| 43 | + return enum_string[prefix.rfind("_") + 1:] |
| 44 | + return enum_string |
| 45 | + |
| 46 | + def get_name(self) -> drgn.Object: |
| 47 | + # name is located after the nvpair_t struct |
| 48 | + ptr = self.addr + self.nvp_size_ |
| 49 | + return sdb.create_object("char *", ptr).string_().decode("utf-8") |
| 50 | + |
| 51 | + # works for all signed and unsigned int, long, long long |
| 52 | + @staticmethod |
| 53 | + def get_value_int(type_: str, loc: int) -> str: |
| 54 | + sz = sdb.create_object('size_t', sdb.type_canonicalize_size(type_)) |
| 55 | + # value is located after the name |
| 56 | + contents = sdb.get_prog().read(loc, sz) |
| 57 | + value = drgn.Object.from_bytes_(sdb.get_prog(), type_, contents) |
| 58 | + return str(value.value_()) |
| 59 | + |
| 60 | + # iterate the array, obtain and print nvpair values |
| 61 | + def get_array_values(self, type_: str) -> None: |
| 62 | + indent = " " |
| 63 | + count = self.nvp.nvp_value_elem |
| 64 | + type_ = type_.replace("_array", "") |
| 65 | + if "boolean" in type_: |
| 66 | + type_ = "uint32_t" |
| 67 | + val = "" |
| 68 | + # skip n 64 bit pointers for string and nvlist array type |
| 69 | + offset = count * 8 |
| 70 | + for x in range(count): |
| 71 | + if x == 0: |
| 72 | + print("values=") |
| 73 | + if "nvlist" in type_: |
| 74 | + self.nvl.set_indent(self.nvl.indent + 4) |
| 75 | + self.nvl.print_one( |
| 76 | + sdb.create_object("nvlist_t *", self.data_ptr + offset)) |
| 77 | + self.nvl.set_indent(self.nvl.indent - 4) |
| 78 | + offset += self.nvl_size |
| 79 | + elif "int" in type_: |
| 80 | + sz = sdb.create_object('size_t', |
| 81 | + sdb.type_canonicalize_size(type_)) |
| 82 | + val = self.get_value_int(type_, self.data_ptr + (sz * x)) |
| 83 | + print(f"{indent}{val}") |
| 84 | + elif "byte" in type_: |
| 85 | + val = self.get_value_int("unsigned char", self.data_ptr + x) |
| 86 | + print(f"{indent}{hex(int(val))}") |
| 87 | + elif "string" in type_: |
| 88 | + offset += len(val) |
| 89 | + val = sdb.create_object("char *", self.data_ptr + |
| 90 | + offset).string_().decode("utf-8") |
| 91 | + print(f"{indent}{val}") |
| 92 | + offset += 1 # null terminator |
| 93 | + |
| 94 | + # obtain and print the nvpair value |
| 95 | + def get_value(self) -> None: |
| 96 | + type_ = (self.get_type(1) + "_t").lower() |
| 97 | + if "array" in type_: |
| 98 | + self.get_array_values(type_) |
| 99 | + return |
| 100 | + if "nvlist" in type_: |
| 101 | + print("values=") |
| 102 | + self.nvl.set_indent(self.nvl.indent + 4) |
| 103 | + self.nvl.print_one(sdb.create_object("nvlist_t *", self.data_ptr)) |
| 104 | + self.nvl.set_indent(self.nvl.indent - 4) |
| 105 | + return |
| 106 | + print("value=", end='') |
| 107 | + if "boolean_value" in type_: |
| 108 | + type_ = "uint32_t" |
| 109 | + elif "boolean" in type_: # DATA_TYPE_BOOLEAN has name and no value |
| 110 | + print("") |
| 111 | + elif "hrtime" in type_: |
| 112 | + type_ = "int64_t" |
| 113 | + if "int" in type_: |
| 114 | + print(self.get_value_int(type_, self.data_ptr)) |
| 115 | + if "string" in type_: |
| 116 | + print( |
| 117 | + sdb.create_object("char *", |
| 118 | + self.data_ptr).string_().decode("utf-8")) |
| 119 | + if "byte" in type_: |
| 120 | + print(hex(int(self.get_value_int("unsigned char", self.data_ptr)))) |
| 121 | + return |
| 122 | + |
| 123 | + |
| 124 | +class Nvlist(sdb.SingleInputCommand): |
| 125 | + """ |
| 126 | + Print nvlist_t |
| 127 | +
|
| 128 | + DESCRIPTION |
| 129 | + Print all the nvpair_t in the passed nvlist_t. Handle basic types, |
| 130 | + array types, and nvlist_t types. Type double is omitted as it is |
| 131 | + not used in zfs. |
| 132 | +
|
| 133 | + EXAMPLE |
| 134 | + Print nvlist_t of snapshot holds from the nvlist_t * pointer address: |
| 135 | +
|
| 136 | + sdb> echo 0xffff970d0ff681e0 | nvlist |
| 137 | + name=monday-1 type=DATA_TYPE_UINT64 value=1633989858 |
| 138 | + name=monday-2 type=DATA_TYPE_UINT64 value=1633989863 |
| 139 | + """ |
| 140 | + |
| 141 | + names = ["nvlist"] |
| 142 | + input_type = "nvlist_t *" |
| 143 | + output_type = "nvlist_t *" |
| 144 | + indent = 0 |
| 145 | + |
| 146 | + def set_indent(self, indent: int) -> None: |
| 147 | + self.indent = indent |
| 148 | + |
| 149 | + # nvlist iteration methods |
| 150 | + @staticmethod |
| 151 | + def nvlist_contains_nvp(nvl: drgn.Object, nvp: drgn.Object) -> int: |
| 152 | + priv = drgn.cast("nvpriv_t *", nvl.nvl_priv) |
| 153 | + |
| 154 | + if nvp.address_ == 0: |
| 155 | + return 0 |
| 156 | + |
| 157 | + curr = priv.nvp_list |
| 158 | + while not sdb.is_null(curr): |
| 159 | + if curr.nvi_nvp.address_ == nvp.address_: |
| 160 | + return 1 |
| 161 | + # pylint: disable=protected-access |
| 162 | + curr = curr._nvi_un._nvi._nvi_next |
| 163 | + |
| 164 | + return 0 |
| 165 | + |
| 166 | + @staticmethod |
| 167 | + def nvlist_first_nvpair(nvl: drgn.Object) -> drgn.Object: |
| 168 | + if sdb.is_null(nvl) or sdb.is_null(nvl.nvl_priv): |
| 169 | + return None |
| 170 | + priv = drgn.cast("nvpriv_t *", nvl.nvl_priv) |
| 171 | + return priv.nvp_list.nvi_nvp |
| 172 | + |
| 173 | + @staticmethod |
| 174 | + def nvlist_next_nvpair(nvl: drgn.Object, nvp: drgn.Object) -> drgn.Object: |
| 175 | + if sdb.is_null(nvl) or sdb.is_null(nvl.nvl_priv): |
| 176 | + return None |
| 177 | + |
| 178 | + priv = drgn.cast("nvpriv_t *", nvl.nvl_priv) |
| 179 | + |
| 180 | + curr_addr = nvp.address_ - drgn.offsetof(sdb.get_type("i_nvp_t"), |
| 181 | + "nvi_nvp") |
| 182 | + curr = sdb.create_object("i_nvp_t *", curr_addr) |
| 183 | + |
| 184 | + if priv.nvp_curr == curr or Nvlist.nvlist_contains_nvp(nvl, nvp): |
| 185 | + # pylint: disable=protected-access |
| 186 | + curr = curr._nvi_un._nvi._nvi_next |
| 187 | + else: |
| 188 | + curr = drgn.NULL(sdb.get_prog(), "i_nvp_t *") |
| 189 | + |
| 190 | + if not sdb.is_null(curr): |
| 191 | + return curr.nvi_nvp |
| 192 | + |
| 193 | + return None |
| 194 | + |
| 195 | + # print one nvlist_t |
| 196 | + def print_one(self, nvl: drgn.Object) -> None: |
| 197 | + pair = self.nvlist_first_nvpair(nvl) |
| 198 | + while pair is not None: |
| 199 | + nvobj = Nvpair(pair, self) |
| 200 | + print(f"{' '*self.indent}", end='') |
| 201 | + print(f"name={nvobj.get_name()} ", end='') |
| 202 | + print(f"type={nvobj.get_type()} ", end='') |
| 203 | + # value will be printed in get_value function |
| 204 | + nvobj.get_value() |
| 205 | + pair = self.nvlist_next_nvpair(nvl, pair) |
| 206 | + |
| 207 | + def _call_one(self, obj: drgn.Object) -> None: |
| 208 | + self.print_one(obj) |
| 209 | + print("----") |
0 commit comments