xref: /llvm-project/lldb/test/API/functionalities/postmortem/FreeBSDKernel/tools/copy-sparse.py (revision 9b1d27b2fa727a3a6f53a803d75beed50a1be999)
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'),
11                      help='Input vmcore file')
12    argp.add_argument('outfile', type=argparse.FileType('wb'),
13                      help='Output vmcore file')
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