When running code coverage in Keil μVision, the code coverage data can be exported to a file for further analysis. This file uses a specific format to store the coverage information generated during testing. Understanding this file format is important for processing and visualizing the coverage data outside of the μVision environment.
Overview of Code Coverage in Keil μVision
Keil μVision supports code coverage testing to determine which parts of the code have been executed during a test run. When enabled, the debugger instruments the code to record which instructions and branches are hit. After completing test runs, μVision can then generate statistics and annotate the code to highlight covered vs uncovered sections.
To use code coverage in μVision, the following steps are typically followed:
- Enable code coverage in the debug configuration settings
- Run debugging session(s) exercising the code with tests
- Halt debugging and collect coverage data
- Generate coverage report and statistics in μVision
- (Optional) Export raw coverage data to file
The exported file contains the raw coverage trace data gathered during testing. This allows offline processing and analysis of coverage using external tools.
Code Coverage Export File Format
When code coverage is enabled in μVision, the trace data is stored in a proprietary binary format in the session folder. This cannot be easily parsed outside of μVision. Using the export option, the current code coverage data can be saved to a CSV text file.
The code coverage export CSV file contains rows indicating each code region that was tracked during testing. The key fields are:
- Module – Name of module/file containing the code
- Address – Start address of the code region
- Size – Number of bytes in the region
- Hits – Number of times the region was executed
Additional fields provide further details on the module name, function name, source file, and line number. The most important data for coverage analysis are the module, address, size, and hits columns. Each row represents a tracked code region, with hits indicating how many times it was executed.
Sample Export File
“Module”,”Address”,”Size”,”Hits”,”Function”,”Source File”,”Line”,”Name” “main.c”,0x00000004,0x4,1,,,,”main” “main.c”,0x00000090,0x1c,1,”func_1″,”main.c”,6, “main.c”,0x000000B0,0x8,0,,,,”func_2″ “lib.c”,0x00000120,0x8,5,”process_data”,”lib.c”,12, “lib.c”,0x00000130,0x10,0,”helpers”,”lib.c”,20,
This shows tracked regions from two modules – main.c and lib.c. The first row is the main function, which starts at address 0x4, is 0x4 bytes long, and was executed once. The second region is func_1, also executed once. func_2 was never executed. process_data in lib.c was executed 5 times.
Processing the Exported Code Coverage Data
The CSV export file provides raw data that can be post-processed to analyze code coverage in different ways. Here are some things that can be done with the data:
- Aggregate statistics by module – calculate percentage of each module covered
- Aggregate statistics by function – see which functions were fully or partially tested
- Generate detailed reports showing uncovered code
- Create custom visualizations of coverage data
- Integrate coverage data into other tools and workflows
The address and size fields allow mapping back to the exact instructions in the code binary. By looking for regions with zero hits, the precise uncovered code segments can be determined.
The hits data can also be used to understand coverage from an execution frequency standpoint. Code that was only exercised a few times during testing could be highlighted.
Custom scripts can parse the CSV file and generate any desired statistics or reports. The data can be imported into spreadsheets or visualization tools. Many options are available for offline analysis.
Using Code Coverage Data
The code coverage export file provides valuable data to help assess your test suite quality. The key ways this data can help include:
- Find untested code – Quickly identify modules, functions, and code regions that were missed completely.
- Improve test coverage – Use coverage data to guide development of new tests targeting untouched code areas.
- Risk analysis – Estimate risk of untested code based on complexity, dependencies, inputs, etc.
- Monitor coverage regression – Track coverage over time across builds and test runs.
Visualizing the coverage data is extremely helpful for understanding gaps and guiding test improvements. Metrics and reports generated from the exported file can be integrated into the development workflow and process.
Code coverage data is most useful when checked regularly during the development cycle. The incremental testing model allows improving coverage over time before release. Analyzing code coverage frequently ensures problems are caught early.
Conclusion
Keil μVision’s code coverage export file format provides detailed execution trace data for offline analysis. The CSV file contains records for each tracked code region along with hit counts to identify uncovered code. This data can be post-processed into reports and metrics that quantify test suite quality and identify improvement opportunities. Regularly exporting and reviewing code coverage is a best practice for achieving high quality embedded software.