Skip to content

Instantly share code, notes, and snippets.

@klemens-morgenstern
Created June 30, 2020 07:09
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 klemens-morgenstern/29171ae11345e190c23aece17fa7284f to your computer and use it in GitHub Desktop.
Save klemens-morgenstern/29171ae11345e190c23aece17fa7284f to your computer and use it in GitHub Desktop.
Interpret a log output based on a location database
# usage python generate_loc_db.py <database>
import sys
import json
# We will read from stdin, for simplicity sake
# Load the previously generated data from the json
db = None
with open(sys.argv[1]) as f:
db = json.load(f)
# We need to functions: one to read integers as binary and one to read the strings.
def read_ptr():
# We read eight bytes since I am using this on an x64 machine. This needs to adjusted to the pointer size on the target
# This can be detected by sending a proper header before the pointer, but we skip that for the example.
bytes = sys.stdin.buffer.read(8)
# This means it's closed, so we stop execution.
if len(bytes) == 0:
return None
# We also assume little endian, you can adjust that.
return int.from_bytes(bytes, byteorder="little")
# Secondly read the string until the null terminator
def read_string():
chars = bytearray()
while True:
c = sys.stdin.buffer.read(1)
if c == b'\x00': # Null terminator, so return the previous bytes decoded as a string
return chars.decode()
chars.extend(c)
# Alright, so let's read the address of main.
main_addr = read_ptr()
offset = main_addr - db['main']['address']
while True:
addr_raw = read_ptr()
if addr_raw is None:
break
addr = addr_raw - offset
msg = read_string()
# Now find the address in the saved marks. If that throws StopIterator we have an error in the pre generated data
mark = next(mark for mark in db['marks'] if mark['address'] == addr)
# This is the formatting to so we get {filename}({line}): {message}
print('{}({}): {}'.format(mark['filename'], mark['line'], msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment