Unifying the Lunaix's Physical Memory Model (#28)
[lunaix-os.git] / lunaix-os / scripts / gdb / lunadbg / structs / region.py
1 from gdb import Type, Value, lookup_type
2 from . import KernelStruct
3 from ..utils import get_dnode_path
4
5 class MemRegion(KernelStruct):
6     def __init__(self, gdb_inferior: Value) -> None:
7         super().__init__(gdb_inferior, MemRegion)
8         self.__attr = self._kstruct["attr"]
9
10     @staticmethod
11     def get_type() -> Type:
12         return lookup_type("struct mm_region").pointer()
13     
14     def print_abstract(self, pp, *args):
15         self.print_detailed(pp, *args)
16     
17     def print_simple(self, pp, *args):
18         self.print_detailed(pp, *args)
19     
20     def print_detailed(self, pp, *args):
21         pp.print( "0x%x...0x%x [0x%x]"%(
22             self._kstruct['start'], self._kstruct['end'], 
23             self._kstruct['end'] - self._kstruct['start']))
24         
25         pp.printf("attributes: %s (0x%x)", ", ".join([self.get_vmr_kind(), self.get_protection()]), self.__attr)
26         
27         file = self._kstruct["mfile"]
28         if file == 0:
29             pp.print("anonymous region")
30         else:
31             pp.print("file mapped:")
32             ppp = pp.next_level()
33             ppp.print("dnode: %s @0x%x"%(get_dnode_path(file["dnode"]), file))
34             ppp.print("range: 0x%x+0x%x"%(self._kstruct["foff"], self._kstruct["flen"]))
35     
36     def get_protection(self):
37         attr_str = []
38         if (self.__attr & (1 << 2)):
39             attr_str.append("R")
40         if (self.__attr & (1 << 3)):
41             attr_str.append("W")
42         if (self.__attr & (1 << 4)):
43             attr_str.append("X")
44         return ''.join(attr_str)
45     
46     def get_vmr_kind(self):
47         """
48             #define REGION_TYPE_CODE (1 << 16)
49             #define REGION_TYPE_GENERAL (2 << 16)
50             #define REGION_TYPE_HEAP (3 << 16)
51             #define REGION_TYPE_STACK (4 << 16)
52         """
53         types = ["exec", "data", "heap", "stack"]
54         attr = ((self.__attr >> 16) & 0xf) - 1
55         if attr >= len(types):
56             return "unknown kind %d"%attr
57         return types[attr]
58     
59
60