Skip to content

Instantly share code, notes, and snippets.

@zommiommy
Last active July 16, 2024 14:42
Show Gist options
  • Save zommiommy/e40c72e4c7fbb533a0a77aa9918795a5 to your computer and use it in GitHub Desktop.
Save zommiommy/e40c72e4c7fbb533a0a77aa9918795a5 to your computer and use it in GitHub Desktop.
make lldb usable. use `command script import /path/to/vmmap.py` to import it. It's important that the file is named vmmap
def parse_maps(pid):
with open("/proc/{pid}/maps".format(pid=pid)) as f:
for line in f.readlines():
vals = (
val.strip()
for val in line.split(" ")
if val.strip()
)
addrs = next(vals)
start, end = addrs.split("-")
start = int(start, 16)
end = int(end, 16)
yield {
"start":start,
"end": end,
"line": line.strip()
}
def regs(debugger, command, exe_ctx, result, internal_dict):
"""Print main registers and their values, optionally print all regs"""
if command.strip() == "all":
for regs in exe_ctx.frame.registers:
for reg in regs.children:
if reg.name is not None and reg.value is not None:
print("{:>8} : {:>10}".format(reg.name, reg.value))
else:
for reg in exe_ctx.frame.registers[0].children[:18]:
print("{:>8} : {:>10}".format(reg.name, reg.value))
def vmmap(debugger, command, exe_ctx, result, internal_dict):
"""Print the memory maps of the current process"""
for stuff in parse_maps(exe_ctx.process.id):
print(stuff["line"])
def find_map(debugger, command, exe_ctx, result, internal_dict):
"""Find in which map an address belongs to, also print the k rows above and
below"""
k = 4
regs = {
reg.name: reg.value
for regs in exe_ctx.frame.registers
for reg in regs.children
}
addr = command.split(" ")[0].lstrip("0x").strip()
addr = regs.get(addr, addr)
addr = int(addr, 16)
print("Looking up: 0x{:08X}".format(addr))
maps = list(parse_maps(exe_ctx.process.id))
found = None
for i, value in enumerate(maps):
if addr >= value["start"]:
found = i
break
else:
print("Addr {} could not be find in any mappings".format(addr))
return
for j in range(max(0, found - k), min(found + k, len(maps))):
if j == found:
pad = "-> "
else:
pad = " "
print(pad + maps[j]["line"])
def debug(debugger, command, exe_ctx, result, internal_dict):
__import__("IPython").embed()
# And the initialization code to add your commands
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('command script add -f vmmap.vmmap vmmap')
debugger.HandleCommand('command script add -f vmmap.find_map find_map')
debugger.HandleCommand('command script add -f vmmap.regs regs')
debugger.HandleCommand('command script add -f vmmap.debug debug')
print('vmmap initted')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment