aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/gdb/units.py
diff options
context:
space:
mode:
Diffstat (limited to 'libs/kstd/gdb/units.py')
-rw-r--r--libs/kstd/gdb/units.py32
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)
+
+