Skip to content

Instantly share code, notes, and snippets.

@sevaa
Last active November 15, 2023 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevaa/af048829b87d093b2606daed6f7185c3 to your computer and use it in GitHub Desktop.
Save sevaa/af048829b87d093b2606daed6f7185c3 to your computer and use it in GitHub Desktop.
Test gist for debugging location list parsing
from sys import argv
from elftools.common.exceptions import ELFParseError
from elftools.dwarf.locationlists import LocationLists, LocationListsPair, LocationParser
from elftools.elf.elffile import ELFFile
from inspect import getframeinfo
from elftools.dwarf.dwarfinfo import DWARFInfo
def location_lists(self):
""" Get a LocationLists object representing the .debug_loc/debug_loclists section of
the DWARF data, or None if this section doesn't exist.
If both sections exist, it returns a LocationListsPair.
"""
if self.debug_loclists_sec and self.debug_loc_sec is None:
return LocationLists(self.debug_loclists_sec.stream, self.structs, 5, self)
elif self.debug_loc_sec and self.debug_loclists_sec is None:
return LocationLists(self.debug_loc_sec.stream, self.structs, 4, self)
elif self.debug_loc_sec and self.debug_loclists_sec:
return LocationListsPair(self.debug_loc_sec.stream, self.debug_loclists_sec.stream, self.structs, self)
else:
return None
DWARFInfo.location_lists = location_lists
die_offset = int(argv[2], 16)
di = ELFFile(open(argv[1], 'rb')).get_dwarf_info()
CU = next(cu for cu in di.iter_CUs() if cu.cu_offset <= die_offset < cu.cu_die_offset + cu.header.unit_length)
die = next(die for die in CU.iter_DIEs() if die.offset == die_offset)
locparser = LocationParser(di.location_lists())
attr = die.attributes['DW_AT_location']
try:
locparser.parse_from_attribute(attr, CU['version'], die = die)
print("Parsed")
except ELFParseError as exc:
tb = exc.__traceback__
tracebacks = []
while tb.tb_next:
tracebacks.insert(0, tb)
tb = tb.tb_next
crash_frame = tracebacks[1].tb_frame
fi = getframeinfo(crash_frame)
print("%s:%d\n" %(fi.filename, fi.lineno))
for (k, v) in crash_frame.f_locals.items():
print("%s: %s" % (k, str(v)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment