diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-08-30 16:41:46 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-08-30 16:41:52 +0200 |
| commit | 3e87bb1da9e4980286d0d44b29f22a5dc8bb33d5 (patch) | |
| tree | 11341608917d90c50664e4a58bf99a479226acf3 /libs | |
| parent | 1cbd2d4238a6408c73c2d03f53c99669ccb34d31 (diff) | |
| download | kernel-3e87bb1da9e4980286d0d44b29f22a5dc8bb33d5.tar.xz kernel-3e87bb1da9e4980286d0d44b29f22a5dc8bb33d5.zip | |
kstd/gdb: add pretty printers for units
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/gdb/__init__.py | 4 | ||||
| -rw-r--r-- | libs/kstd/gdb/units.py | 32 |
2 files changed, 36 insertions, 0 deletions
diff --git a/libs/kstd/gdb/__init__.py b/libs/kstd/gdb/__init__.py index 26a38e3b..0cdafffd 100644 --- a/libs/kstd/gdb/__init__.py +++ b/libs/kstd/gdb/__init__.py @@ -1,5 +1,7 @@ import gdb.printing + +from .units import KstdBytesPrinter from .vector import KstdVectorPrinter from .string import KstdStringPrinter from .smart_pointers import ( @@ -18,6 +20,8 @@ def build_pretty_printers(): pp.add_printer("shared_ptr", "^kstd::shared_ptr<.*>$", KstdSharedPtrPrinter) pp.add_printer("weak_ptr", "^kstd::weak_ptr<.*>$", KstdWeakPtrPrinter) pp.add_printer("observer_ptr", "^kstd::observer_ptr<.*>$", KstdObserverPtrPrinter) + pp.add_printer("bytes", "^kstd::basic_unit<.*, kstd::bytes_tag, .*>$", KstdBytesPrinter) + pp.add_printer("offset", "^kstd::basic_unit<.*, kstd::byte_offset_tag, .*>$", KstdBytesPrinter) return pp diff --git a/libs/kstd/gdb/units.py b/libs/kstd/gdb/units.py index e69de29b..2ba533bd 100644 --- a/libs/kstd/gdb/units.py +++ b/libs/kstd/gdb/units.py @@ -0,0 +1,32 @@ +import gdb + + +class KstdBytesPrinter(gdb.ValuePrinter): + + def __init__(self, val): + self.__val = val + self.__value = val["value"] + + def to_string(self): + units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'] + size_in_bytes = int(self.__value) + + if size_in_bytes == 0: + return "0 B" + + sign = "-" if size_in_bytes < 0 else "" + absolute_size = abs(size_in_bytes) + + import math + + index = int(math.floor(math.log(absolute_size, 1024))) + index = min(index, len(units) - 1) + divisor = math.pow(1024, index) + size = round(absolute_size / divisor, 2) + + return f"{sign}{size} {units[index]}" + + def children(self): + yield ("value", self.__value) + + |
