feat: lunadbg GDB plugin to ease kernel debugging process.
[lunaix-os.git] / lunaix-os / scripts / gdb / lunadbg / region_dump.py
1 import gdb
2 from .utils import pid_argument
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 invoke(self, argument: str, from_tty: bool) -> None:
10         argument = pid_argument(argument)
11         
12         pid = gdb.parse_and_eval(f"{argument}->pid")
13
14         argument = f"&{argument}->mm.regions"
15         val = gdb.parse_and_eval(argument)
16         head = val
17
18         region_t = gdb.lookup_type("struct mm_region").pointer()
19         
20         print("VMRS (pid: %d)"%(pid))
21
22         i = 0
23         while(val["next"] != head):
24             region = val["next"].cast(region_t)
25             print(f"VMR #{i}:")
26             print( "  0x%x...0x%x [0x%x]"%(
27                 region['start'], region['end'], 
28                 region['end'] - region['start']))
29             
30             attr = region["attr"]
31             attr_str = []
32             if (attr & (1 << 2)):
33                 attr_str.append("R")
34             if (attr & (1 << 3)):
35                 attr_str.append("W")
36             if (attr & (1 << 4)):
37                 attr_str.append("X")
38             print( "  attr: 0x%x (%s)"%(attr, "".join(attr_str)))
39             
40             file = region["mfile"]
41             if file == 0:
42                 print( "  anonymous region")
43             else:
44                 print( "  file mapped:")
45                 print( "     dnode: %s @0x%x"%(file["dnode"]["name"]["value"].string(), file))
46                 print( "     frange: 0x%x+0x%x"%(region["foff"], region["flen"]))
47
48             val = val["next"]
49             i+=1