54 lines
1.7 KiB
Python
Executable File
54 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
|
|
|
|
class Crashlog():
|
|
def __init__(self, crashlog: Path, executable: Path):
|
|
self.executable = executable
|
|
self.addresses = self.read_crashlog(crashlog)
|
|
self.translated_addresses = self.translate_addresses(self.addresses, executable)
|
|
|
|
def read_crashlog(self, crashlog: Path) -> list[str]:
|
|
_addresses = []
|
|
try:
|
|
with open(crashlog, "r") as f:
|
|
for line in f:
|
|
if len(line.strip()) <= 0:
|
|
continue
|
|
tokens = line.split()
|
|
_addresses.append(tokens[1])
|
|
except FileNotFoundError:
|
|
print("O arquivo não foi encontrado.")
|
|
except Exception as e:
|
|
print(f"Ocorreu um error: {e}")
|
|
finally:
|
|
return _addresses
|
|
|
|
def translate_addresses(self, addresses: list[str], executable: Path) -> dict[str, str]:
|
|
_translated_addr = {}
|
|
|
|
if shutil.which("addr2line") is None:
|
|
raise RuntimeError("addr2line binary not found")
|
|
|
|
for addr in addresses:
|
|
_path = executable.absolute()
|
|
_cmd = f"addr2line --functions --pretty-print --demangle --basenames --exe {_path} {addr}"
|
|
_process = subprocess.run(_cmd, shell=True, check=True, capture_output=True)
|
|
_translated_addr[addr] = _process.stdout
|
|
|
|
return _translated_addr
|
|
|
|
def __str__(self):
|
|
_stdout = ""
|
|
for key, value in self.translated_addresses.items():
|
|
_stdout += value.decode()
|
|
return _stdout
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(Crashlog(Path("crashlog.txt"), Path("mqttd")))
|
|
pass
|