Created
June 30, 2020 07:09
-
-
Save klemens-morgenstern/29171ae11345e190c23aece17fa7284f to your computer and use it in GitHub Desktop.
Interpret a log output based on a location database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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