xref: /llvm-project/lldb/test/API/functionalities/postmortem/FreeBSDKernel/tools/copy-sparse.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1#!/usr/bin/env python
2
3import argparse
4import re
5import sys
6
7
8def main():
9    argp = argparse.ArgumentParser()
10    argp.add_argument("infile", type=argparse.FileType("rb"), help="Input vmcore file")
11    argp.add_argument(
12        "outfile", type=argparse.FileType("wb"), help="Output vmcore file"
13    )
14    args = argp.parse_args()
15
16    inf = args.infile
17    outf = args.outfile
18    line_re = re.compile(r"^% RD: (\d+) (\d+)")
19
20    # copy the first chunk that usually includes ELF headers
21    # (not output by patched libfbsdvmcore since libelf reads this)
22    outf.write(inf.read(1024))
23
24    for l in sys.stdin:
25        m = line_re.match(l)
26        if m is None:
27            continue
28        offset, size = [int(x) for x in m.groups()]
29
30        inf.seek(offset)
31        outf.seek(offset)
32        outf.write(inf.read(size))
33
34
35if __name__ == "__main__":
36    main()
37