summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2021-01-21 21:48:58 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2021-01-21 21:48:58 +0100
commit13bb78203cac7d3a582da55e004c84104ff214dd (patch)
tree9e8adf467003e9fe876713dca1cb2a1dbf76b641
parent172d694cf7ba582635ac38454bad0f0fdaa14773 (diff)
mmio: Add mmio_ptr template.
-rw-r--r--gdb_plugins/mmio.py67
-rw-r--r--mmio/mmio.h29
2 files changed, 96 insertions, 0 deletions
diff --git a/gdb_plugins/mmio.py b/gdb_plugins/mmio.py
new file mode 100644
index 0000000..25ebcc3
--- /dev/null
+++ b/gdb_plugins/mmio.py
@@ -0,0 +1,67 @@
+import gdb.xmethod
+
+class RefWorker(gdb.xmethod.XMethodWorker):
+ def __init__(self, result_type):
+ self.result_type = result_type
+
+ def get_arg_types(self):
+ return None
+
+ def get_result_type(self, obj):
+ return self.result_type.reference()
+
+ def __call__(self, obj):
+ return obj['p'].cast(self.result_type.pointer()).dereference()
+
+class PtrWorker(gdb.xmethod.XMethodWorker):
+ def __init__(self, result_type):
+ self.result_type = result_type
+
+ def get_arg_types(self):
+ return None
+
+ def get_result_type(self, obj):
+ return self.result_type.pointer()
+
+ def __call__(self, obj):
+ return obj['p'].cast(self.result_type.pointer())
+
+class SubscriptWorker(gdb.xmethod.XMethodWorker):
+ def __init__(self, result_type):
+ self.result_type = result_type
+
+ def get_arg_types(self):
+ return [gdb.lookup_type('std::size_t')]
+
+ def get_result_type(self, obj, idx):
+ return self.result_type.reference()
+
+ def __call__(self, obj, idx):
+ return obj['p'].cast(self.result_type.pointer())[idx]
+
+class Method(gdb.xmethod.XMethod):
+ def __init__(self, name, worker):
+ gdb.xmethod.XMethod.__init__(self, name)
+ self.worker = worker
+
+class Matcher(gdb.xmethod.XMethodMatcher):
+ def __init__(self):
+ gdb.xmethod.XMethodMatcher.__init__(self, 'mmio_ptr matcher')
+
+ self.methods = [
+ Method('operator*', RefWorker),
+ Method('operator->', PtrWorker),
+ Method('operator[]', SubscriptWorker),
+ ]
+
+ def match(self, class_type, method_name):
+ if not class_type.name.startswith('mmio_ptr<'):
+ return
+
+ ptr_type = class_type.template_argument(0)
+
+ for method in self.methods:
+ if method.enabled and method.name == method_name:
+ return method.worker(ptr_type)
+
+gdb.xmethod.register_xmethod_matcher(None, Matcher(), replace = True)
diff --git a/mmio/mmio.h b/mmio/mmio.h
new file mode 100644
index 0000000..a756c8f
--- /dev/null
+++ b/mmio/mmio.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <cstdint>
+
+template <typename T>
+class mmio_ptr {
+ private:
+ const uintptr_t p;
+
+ protected:
+ T* ptr() const {
+ return reinterpret_cast<T*>(p);
+ }
+
+ public:
+ constexpr mmio_ptr(uintptr_t p) : p(p) {}
+
+ T* operator->() const {
+ return ptr();
+ }
+
+ T& operator*() const {
+ return *ptr();
+ }
+
+ T& operator[](std::size_t idx) const {
+ return ptr()[idx];
+ }
+};