223766bff3eb9c7413366196d741f2195311b989
[lunaix-os.git] / lunaix-os / scripts / gdb / lunadbg / region_dump.py
1 import gdb
2 from .utils import pid_argument, llist_foreach
3
4 class MemoryRegionDump(gdb.Command):
5     """Dump virtual memory regions associated with a process"""
6     def __init__(self) -> None:
7         super().__init__("vmrs", gdb.COMMAND_USER)
8
9     def region_callback(self, idx, region):
10         print(f"VMR #{idx}:")
11         print( "  0x%x...0x%x [0x%x]"%(
12             region['start'], region['end'], 
13             region['end'] - region['start']))
14         
15         attr = region["attr"]
16         attr_str = []
17         if (attr & (1 << 2)):
18             attr_str.append("R")
19         if (attr & (1 << 3)):
20             attr_str.append("W")
21         if (attr & (1 << 4)):
22             attr_str.append("X")
23         print( "  attr: 0x%x (%s)"%(attr, "".join(attr_str)))
24         
25         file = region["mfile"]
26         if file == 0:
27             print( "  anonymous region")
28         else:
29             print( "  file mapped:")
30             print( "     dnode: %s @0x%x"%(file["dnode"]["name"]["value"].string(), file))
31             print( "     frange: 0x%x+0x%x"%(region["foff"], region["flen"]))
32
33     def invoke(self, argument: str, from_tty: bool) -> None:
34         argument = pid_argument(argument)
35         
36         pid = gdb.parse_and_eval(f"{argument}->pid")
37
38         argument = f"&{argument}->mm.regions"
39         val = gdb.parse_and_eval(argument)
40         head = val
41
42         region_t = gdb.lookup_type("struct mm_region").pointer()
43         
44         print("VMRS (pid: %d)"%(pid))
45
46         llist_foreach(val, region_t, lambda a,b: self.region_callback(a,b))