Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GDB script: load symbols from all executable objects mapped in memory, automatically.
import re
import subprocess
pid = gdb.selected_inferior().pid
maps_path = "/proc/%d/maps" % pid
maps_lines = open(maps_path).read().split("\n")
for line in maps_lines:
memrange, perms, offset, devnum, size, path = (None, None, None, None, None, None)
try:
memrange, perms, offset, devnum, size, path = re.split("\s+", line)
except:
continue
# exclude [vdso], [vsyscall], etc.
if re.match("^\[.*\]$", path):
continue
# exclude non-executable segments
if perms[2] != "x":
continue
if path == "":
continue
lines = subprocess.check_output(["readelf", "--sections", path]).decode("utf-8").split("\n")
addr = None
for line in lines:
if re.match(".*\[[0-9 ]+\].*\.text", line):
tokens = re.split("\s+", line.strip())
addr = int(tokens[len(tokens)-2], 16)
break
if addr != None:
memstart = int(memrange.split("-")[0], 16)
offset = int(offset, 16)
textaddress = memstart - offset + addr
cmd = "add-symbol-file %s 0x%x" % (path, textaddress)
gdb.execute(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.