xref: /llvm-project/compiler-rt/test/sanitizer_common/ios_commands/get_pid_from_output.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1"""
2Parses the id of the process that ran with ASAN from the output logs.
3"""
4import sys, argparse, re
5
6
7def main():
8    parser = argparse.ArgumentParser()
9    parser.add_argument(
10        "--infile",
11        nargs="?",
12        type=argparse.FileType("r"),
13        default=sys.stdin,
14        help="The sanitizer output to get the pid from",
15    )
16    parser.add_argument(
17        "--outfile",
18        nargs="?",
19        type=argparse.FileType("r"),
20        default=sys.stdout,
21        help="Where to write the result",
22    )
23    args = parser.parse_args()
24
25    pid = process_file(args.infile)
26    args.outfile.write(pid)
27    args.infile.close()
28    args.outfile.close()
29
30
31def process_file(infile):
32    # check first line is just ==== divider
33    first_line_pattern = re.compile(r"=*")
34    assert first_line_pattern.match(infile.readline())
35
36    # parse out pid from 2nd line
37    # `==PID==ERROR: SanitizerName: error-type on address...`
38    pid_pattern = re.compile(r"==([0-9]*)==ERROR:")
39    pid = pid_pattern.search(infile.readline()).group(1)
40
41    # ignore the rest
42
43    assert pid and pid.isdigit()
44
45    return pid
46
47
48if __name__ == "__main__":
49    main()
50