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/kstd/gdb/units.py | |
| parent | 1cbd2d4238a6408c73c2d03f53c99669ccb34d31 (diff) | |
| download | kernel-3e87bb1da9e4980286d0d44b29f22a5dc8bb33d5.tar.xz kernel-3e87bb1da9e4980286d0d44b29f22a5dc8bb33d5.zip | |
kstd/gdb: add pretty printers for units
Diffstat (limited to 'libs/kstd/gdb/units.py')
| -rw-r--r-- | libs/kstd/gdb/units.py | 32 |
1 files changed, 32 insertions, 0 deletions
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) + + |
