Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2018 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | # This script will parse the serial console log file and create the required |
| 8 | # gcda files. |
| 9 | # Usage python3 ${ZEPHYR_BASE}/scripts/gen_gcov_files.py -i console_output.log |
| 10 | # Add -v for verbose |
| 11 | |
| 12 | import argparse |
| 13 | import os |
| 14 | import re |
| 15 | |
| 16 | def retrieve_data(input_file): |
| 17 | extracted_coverage_info = {} |
| 18 | capture_data = False |
Anas Nashif | a34f371 | 2019-01-25 09:26:39 -0500 | [diff] [blame] | 19 | reached_end = False |
Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 20 | with open(input_file, 'r') as fp: |
| 21 | for line in fp.readlines(): |
| 22 | if re.search("GCOV_COVERAGE_DUMP_START", line): |
| 23 | capture_data = True |
| 24 | continue |
| 25 | if re.search("GCOV_COVERAGE_DUMP_END", line): |
Anas Nashif | a34f371 | 2019-01-25 09:26:39 -0500 | [diff] [blame] | 26 | reached_end = True |
Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 27 | break |
| 28 | # Loop until the coverage data is found. |
| 29 | if not capture_data: |
| 30 | continue |
| 31 | |
| 32 | # Remove the leading delimiter "*" |
| 33 | file_name = line.split("<")[0][1:] |
| 34 | # Remove the trailing new line char |
| 35 | hex_dump = line.split("<")[1][:-1] |
| 36 | extracted_coverage_info.update({file_name:hex_dump}) |
Anas Nashif | a34f371 | 2019-01-25 09:26:39 -0500 | [diff] [blame] | 37 | |
| 38 | if not reached_end: |
| 39 | print("incomplete data captured from %s" %input_file) |
Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 40 | return extracted_coverage_info |
| 41 | |
| 42 | def create_gcda_files(extracted_coverage_info): |
| 43 | if args.verbose: |
| 44 | print("Generating gcda files") |
| 45 | for filename, hexdump_val in extracted_coverage_info.items(): |
| 46 | if args.verbose: |
| 47 | print(filename) |
| 48 | # if kobject_hash is given for coverage gcovr fails |
| 49 | # hence skipping it problem only in gcovr v4.1 |
| 50 | if "kobject_hash" in filename: |
Ulf Magnusson | 16b548f | 2019-09-05 19:16:10 +0200 | [diff] [blame] | 51 | filename = filename[:-4] + "gcno" |
Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 52 | try: |
| 53 | os.remove(filename) |
Ulf Magnusson | 16b548f | 2019-09-05 19:16:10 +0200 | [diff] [blame] | 54 | except Exception: |
Adithya Baglody | 131edad | 2018-09-05 14:36:37 +0530 | [diff] [blame] | 55 | pass |
| 56 | continue |
| 57 | |
| 58 | with open(filename, 'wb') as fp: |
| 59 | fp.write(bytes.fromhex(hexdump_val)) |
| 60 | |
| 61 | |
| 62 | def parse_args(): |
| 63 | global args |
| 64 | parser = argparse.ArgumentParser( |
| 65 | description=__doc__, |
| 66 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 67 | parser.add_argument("-i", "--input", required=True, |
| 68 | help="Input dump data") |
| 69 | parser.add_argument("-v", "--verbose", action="count", default=0, |
| 70 | help="Verbose Output") |
| 71 | args = parser.parse_args() |
| 72 | |
| 73 | |
| 74 | |
| 75 | def main(): |
| 76 | parse_args() |
| 77 | input_file = args.input |
| 78 | |
| 79 | extracted_coverage_info = retrieve_data(input_file) |
| 80 | create_gcda_files(extracted_coverage_info) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | main() |