|
| 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 | +from typing import Iterable |
| 20 | +import drgn |
| 21 | +import sdb |
| 22 | +from sdb.commands.spl.spl_list import SPLList |
| 23 | + |
| 24 | + |
| 25 | +class Zfs_Refcount(sdb.PrettyPrinter): |
| 26 | + names = ["zfs_refcount"] |
| 27 | + input_type = "zfs_refcount_t *" |
| 28 | + output_type = "zfs_refcount_t *" |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def print_ref(obj: drgn.Object) -> None: |
| 32 | + ptr = int(obj.ref_holder) |
| 33 | + c = sdb.create_object("char *", ptr) |
| 34 | + try: |
| 35 | + s = c.string_().decode("utf-8") |
| 36 | + except UnicodeDecodeError: |
| 37 | + s = "" |
| 38 | + print(f"{hex(ptr)} {s} ") |
| 39 | + |
| 40 | + def pretty_print(self, objs: Iterable[drgn.Object]) -> None: |
| 41 | + for zr in objs: |
| 42 | + # handle the lack of rc_tracked in non-debug zfs build |
| 43 | + tracked = 0 |
| 44 | + try: |
| 45 | + tracked = zr.rc_tracked |
| 46 | + except AttributeError: |
| 47 | + pass |
| 48 | + print(f"zfs_recount_t at {hex(zr)} has {int(zr.rc_count)} " |
| 49 | + f"current holds tracked={int(tracked)}") |
| 50 | + if tracked: |
| 51 | + ref_list = zr.rc_list |
| 52 | + list_addr = ref_list.address_of_() |
| 53 | + refs = sdb.execute_pipeline( |
| 54 | + [list_addr], |
| 55 | + [SPLList(), sdb.Cast(["reference_t *"])], |
| 56 | + ) |
| 57 | + |
| 58 | + for ref in refs: |
| 59 | + Zfs_Refcount.print_ref(ref) |
0 commit comments