11 class CrashLogPatcher
:
12 SYMBOL_REGEX
= re
.compile(r
"^([0-9a-fA-F]+) T _(.*)$")
13 UUID_REGEX
= re
.compile(r
"UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*")
15 def __init__(self
, data
, binary
, offsets
, json
):
18 self
.offsets
= offsets
21 def patch_executable(self
):
22 self
.data
= self
.data
.replace("@EXEC@", self
.binary
)
23 self
.data
= self
.data
.replace("@NAME@", os
.path
.basename(self
.binary
))
26 output
= subprocess
.check_output(["dwarfdump", "--uuid", self
.binary
]).decode(
29 m
= self
.UUID_REGEX
.match(output
)
31 self
.data
= self
.data
.replace("@UUID@", m
.group(1))
33 def patch_addresses(self
):
36 output
= subprocess
.check_output(["nm", self
.binary
]).decode("utf-8")
37 for line
in output
.splitlines():
38 m
= self
.SYMBOL_REGEX
.match(line
)
42 if symbol
in self
.offsets
:
43 patch_addr
= int(m
.group(1), 16) + int(self
.offsets
[symbol
])
45 patch_addr
= patch_addr
- 0x100000000
49 self
.data
= self
.data
.replace(
50 "@{}@".format(symbol
), str(representation(patch_addr
))
53 def remove_metadata(self
):
54 self
.data
= self
.data
[self
.data
.index("\n") + 1 :]
57 if __name__
== "__main__":
58 parser
= argparse
.ArgumentParser(description
="Crashlog Patcher")
59 parser
.add_argument("--binary", required
=True)
60 parser
.add_argument("--crashlog", required
=True)
61 parser
.add_argument("--offsets", required
=True)
62 parser
.add_argument("--json", default
=False, action
="store_true")
63 parser
.add_argument("--no-metadata", default
=False, action
="store_true")
64 args
= parser
.parse_args()
66 offsets
= json
.loads(args
.offsets
)
68 with
open(args
.crashlog
, "r") as file:
71 p
= CrashLogPatcher(data
, args
.binary
, offsets
, args
.json
)
79 with
open(args
.crashlog
, "w") as file: