Code Coverage
Source code coverage provides data indicating the count and frequency of every program execution path taken during runtime. GCOV is a GCC tool that, when used with the compiler, generates log files showing the execution count of each line of source code.
Your ESP-IDF project should be configured to generate gcda/gcno
coverage files using gcov
. Please read GCOV Code Coverage to learn more about code coverage with GCOV in ESP-IDF projects.
You can use the ESP-IDF: Configure Project SDKConfig for Coverage
to set the required configuration in the SDK Configuration Editor.
Code Coverage Example
Let’s use the ESP-IDF GCOV Example for this tutorial.
Navigate to
View
>Command Palette
.Type
ESP-IDF: New Project
and choose ESP-IDF version to use.Note
If you don’t see the option, please review the current ESP-IDF setup in Installation.
A window will open with settings to configure the project. Choose from a list of ESP-IDF examples, go to the
system
section, and selectgcov
. You will see aCreate Project Using Example GCOV
button at the top and a project description below. ClickCreate Project Using Example GCOV
.Select a container directory where to copy the example project. For example, if you choose
/Users/myUser/someFolder
, the resulting folder will be/Users/myUser/someFolder/gcov
. This new project directory will be created and opened in Visual Studio Code.Select an Espressif target (esp32, esp32s2, etc.):
Navigate to
View
>Command Palette
.Type
ESP-IDF: Set Espressif Device Target
command. The default target isesp32
, which is used in this tutorial.
Configure your sdkconfig project with the
ESP-IDF: Configure Project SDKConfig for Coverage
command or manually using theESP-IDF: SDK Configuration Editor
command. After all changes are made, clickSave
and close the window.The example will enable the following options by default:
Enable the Application Tracing Module under
Component Config
>Application Level Tracing
>Data Destination
by choosingTrace Memory
.Enable GCOV to host interface under
Component Config
>Application Level Tracing
>GCOV to Host Enable
.Enable OpenOCD Debug Stubs under
Component Config
>ESP32-specific
>OpenOCD Debug Stubs
.
Build the project, flash your device, and start the ESP-IDF Monitor using the
ESP-IDF: Build your Project
,ESP-IDF: Flash your Project
, andESP-IDF: Monitor Device
commands.Note
There is also an
ESP-IDF: Build, Flash and Start a Monitor on your Device
command that combines all three commands.Launch OpenOCD and send some commands. To start OpenOCD from the extension, execute the
ESP-IDF: OpenOCD Manager
command or use theOpenOCD Server (Running | Stopped)
button in the Visual Studio Code status bar. OpenOCD server output is shown inView
>Output
>ESP-IDF
.Launch a new terminal with menu
Terminal
>New Terminal
and executetelnet <oocd_host> <oocd_port>
, which defaults totelnet localhost 4444
. Latest macOS users can usenc <oocd_host> <oocd_port>
iftelnet
is not available.Note
You can modify
openocd.tcl.host
andopenocd.tcl.port
configuration settings to change these values.Send the OpenOCD command
esp gcov dump
for a hard-coded dump, which will perform two hard-coded dumps based on this example. Then send theesp gcov
command for an instant run-time dump.After dumping data, open the desired file in your editor and execute the
ESP-IDF: Add Editor Coverage
command to highlight the editor with code coverage.You can customize the highlight color through the following configuration settings in the extension’s
settings.json
file:Covered lines use
idf.coveredLightTheme
for light themes andidf.coveredDarkTheme
for dark themes.Partially covered lines use
idf.partialLightTheme
for light themes andidf.partialDarkTheme
for dark themes.Non-covered lines use
idf.uncoveredLightTheme
for light themes andidf.uncoveredDarkTheme
for dark themes.
Visual Studio Code supports
red
,rgb(255,0,120)
orrgba(120,0,0,0.1)
.When finished, use the
ESP-IDF: Remove Editor Coverage
command to remove the code coverage.Navigate to
View
>Command Palette
.Type
ESP-IDF: Get HTML Coverage Report for Project
and select the command to generate an HTML report for code coverage.
Note
Check the Troubleshooting section if you encounter any issues.