50 lines · python
1"""2Parses the id of the process that ran with ASAN from the output logs.3"""4import sys, argparse, re5 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 ==== divider33 first_line_pattern = re.compile(r"=*")34 assert first_line_pattern.match(infile.readline())35 36 # parse out pid from 2nd line37 # `==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 rest42 43 assert pid and pid.isdigit()44 45 return pid46 47 48if __name__ == "__main__":49 main()50