#!/usr/bin/env python3 | |
from colorama import init, Fore, Style | |
from collections import OrderedDict | |
import sys | |
#get content | |
if len(sys.argv) > 1: | |
name1 = sys.argv[1] | |
else: | |
name1 = "log1" | |
if len(sys.argv) > 2: | |
name2 = sys.argv[2] | |
else: | |
name2 = "log2" | |
log1 = open(name1, "r") | |
log2 = open(name2, "r") | |
content1 = log1.readlines() | |
content1 = [x.strip() for x in content1] | |
content2 = log2.readlines() | |
content2 = [x.strip() for x in content2] | |
del content2[-1] | |
content1 = [x for x in content1 if x.startswith(";") == False] | |
content1 = [' '.join(x.split()) for x in content1] | |
i = 0 | |
while i < len(content1): | |
while content1[i].startswith("0x") == False: | |
content1[i] = content1[i][1:] | |
i += 1 | |
content1 = [x.split(";")[0] for x in content1] | |
#sort content2 and remove leading numbers | |
content2.sort(key=lambda x : int(x.split('.')[0])) | |
content2 = [x[len(x.split('.')[0])+1:len(x)] for x in content2] | |
#group operations within one instruction | |
instructions = list() | |
i = 0 | |
while i < len(content2): | |
if content2[i].startswith("addr="): | |
lastAddr = content2[i][5:] | |
a = list() | |
a.append(content2[i]) | |
i += 1 | |
while i < len(content2) and (content2[i].startswith("addr=") == False or content2[i][5:] == lastAddr): | |
if content2[i].startswith("addr=") == False and content2[i].startswith("mem.read=") == False and content2[i].startswith("mem.write=") == False and content2[i].startswith("reg.read=") == False and content2[i].startswith("reg.write=") == False: | |
a.append(content2[i]) | |
i += 1 | |
a = list(OrderedDict.fromkeys(a)) | |
instructions.append(a) | |
#print result | |
i = 0 | |
k = 0 | |
while i < len(instructions) and k < len(content1): | |
currentAddr = content1[k].split()[0] | |
if instructions[i][0][5:] != currentAddr: | |
print(Fore.LIGHTRED_EX + content1[k].split()[0] + " " + Fore.BLUE + content1[k].split()[1] + Fore.GREEN + content1[k][len(content1[k].split()[0]) + len(content1[k].split()[1]) + 1:]) | |
k += 1 | |
continue | |
print(Fore.LIGHTRED_EX + content1[k].split()[0] + " " + Fore.BLUE + content1[k].split()[1] + Fore.GREEN + content1[k][len(content1[k].split()[0]) + len(content1[k].split()[1]) + 1:]) | |
j = 0 | |
while j < len(instructions[i]): | |
s = instructions[i][j] | |
if s.startswith("reg"): | |
if s[4:].startswith("read"): | |
print(Fore.WHITE + s[0:4] + Fore.LIGHTBLUE_EX + s[4:9] + Fore.YELLOW + s[9:]) | |
else: | |
print(Fore.WHITE + s[0:4] + Fore.LIGHTCYAN_EX + s[4:10] + Fore.YELLOW + s[10:]) | |
if s.startswith("mem"): | |
if s[4:].startswith("read"): | |
print(Fore.LIGHTYELLOW_EX + s[0:4] + Fore.BLUE + s[4:9] + Fore.YELLOW + s[9:]) | |
else: | |
print(Fore.LIGHTYELLOW_EX + s[0:4] + Fore.CYAN + s[4:10] + Fore.YELLOW + s[10:]) | |
j += 1 | |
print("") | |
i += 1 | |
k += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
bart1e commentedJun 13, 2019
Usage:
./prettyTraceLog.py file1 file2
, wherefile1
contains output fromdtd
andfile2
fromdte
.Default values are
log1
andlog2
.