2022-07-22 09:33:19 -04:00
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- |
2020-04-09 04:40:57 -04:00
2019-06-25 14:41:19 -04:00
# Blink Example With Coverage Info (Gcov)
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
(See the README.md file in the upper level 'examples' directory for more information about examples.)
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
The following example demonstrates how to compile an ESP-IDF project to generate code coverage data, and how generate a code coverage report using Gcov or Lcov. Refer to the [Gcov Guide ](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html#gcov-source-code-coverage ) for more details on the code coverage features supported in ESP-IDF.
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
This example implements a simple blink application but with code coverage enabled. The example will demonstrate the following features:
2019-10-01 13:42:32 -04:00
* How to compile a project with coverage info enabled.
2019-06-25 14:41:19 -04:00
* Various methods of dumping code coverage data (e.g. Instant Run-Time Dump and Hard-coded Dump).
* How to generate a code coverage report.
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
## How to use example
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
### Hardware Required
2018-02-15 12:09:03 -05:00
2021-06-03 14:13:57 -04:00
To run this example, you need a supported dev board connected to a JTAG adapter, which can come in the following forms:
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
* [ESP-WROVER-KIT ](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/modules-and-boards.html#esp-wrover-kit-v4-1 ) which integrates an on-board JTAG adapter. Ensure that the [required jumpers to enable JTAG are connected ](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/get-started-wrover-kit.html#setup-options ) on the WROVER-KIT.
2022-07-22 09:33:19 -04:00
* ESP core board (e.g. ESP32-DevKitC) can also work as long as you connect it to an external JTAG adapter (e.g. FT2232H, J-LINK).
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
This example will assume that that an ESP-WROVER-KIT is used.
2018-09-18 00:23:56 -04:00
2019-06-25 14:41:19 -04:00
1. Connect the JTAG interface to ESP32 board, and power up both the JTAG and ESP32. For details about how to set up JTAG interface, please see [JTAG Debugging ](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html ).
2018-09-18 00:23:56 -04:00
2019-06-25 14:41:19 -04:00
2. After connecting JTAG interface, you need to [Run OpenOCD ](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#run-openocd ).
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
3. Open a separate terminal window and run telnet by entering the command below. The telnet terminal window is used to feed commands to OpenOCD:
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
```bash
telnet localhost 4444
2018-02-15 12:09:03 -05:00
```
2019-06-25 14:41:19 -04:00
### Configure the project
2017-08-16 19:36:00 -04:00
2018-02-15 12:09:03 -05:00
```
2019-06-25 14:41:19 -04:00
idf.py menuconfig
2018-02-15 12:09:03 -05:00
```
2020-03-16 12:06:47 -04:00
The example will enable the following options by default:
2019-06-25 14:41:19 -04:00
2021-09-21 16:46:33 -04:00
* Enable the Application Tracing Module under `Component config -> Application Level Tracing -> Data Destination` by choosing `JTAG` .
2019-06-25 14:41:19 -04:00
* Enable GCOV to host interface under `Component config -> Application Level Tracing -> GCOV to Host Enable` .
2021-09-15 17:06:10 -04:00
* Enable OpenOCD Debug Stubs under `Component config -> ESP System Settings -> OpenOCD debug stubs`
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
### Build, Flash, and Run
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
Build the project and flash it to the board, then run monitor tool to view serial output:
2018-02-15 12:09:03 -05:00
2017-08-16 19:36:00 -04:00
```
2019-06-25 14:41:19 -04:00
idf.py -p PORT flash monitor
2017-08-16 19:36:00 -04:00
```
2019-06-25 14:41:19 -04:00
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
2018-02-15 12:09:03 -05:00
2019-06-25 14:41:19 -04:00
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
## Example Output
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
### 1. Hard-coded Dump
2017-08-16 19:36:00 -04:00
2020-03-16 12:06:47 -04:00
The example will initially execute two hard-coded dumps. Therefore, when the application outputs `Ready to dump GCOV data...` , users should execute the `esp gcov dump` OpenOCD command. The example should output the following:
2017-08-16 19:36:00 -04:00
```
2019-10-01 06:16:43 -04:00
blink_dummy_func: Counter = 0
some_dummy_func: Counter = 0
2019-06-25 14:41:19 -04:00
Ready to dump GCOV data...
GCOV data have been dumped.
2019-10-01 06:16:43 -04:00
blink_dummy_func: Counter = 1
some_dummy_func: Counter = 2
2019-06-25 14:41:19 -04:00
Ready to dump GCOV data...
GCOV data have been dumped.
2017-08-16 19:36:00 -04:00
```
2019-06-25 14:41:19 -04:00
### 2. Instant Run-Time Dump
2020-03-16 12:06:47 -04:00
After the two hard-coded dumps, the example will continue looping through it's main blink function. Users can call `esp gcov` OpenOCD command to trigger an instant run-time dump. The output should resemble the following:
2019-06-28 09:24:05 -04:00
```
2019-10-01 06:16:43 -04:00
blink_dummy_func: Counter = 2
some_dummy_func: Counter = 4
blink_dummy_func: Counter = 3
some_dummy_func: Counter = 6
blink_dummy_func: Counter = 4
some_dummy_func: Counter = 8
blink_dummy_func: Counter = 5
some_dummy_func: Counter = 10
blink_dummy_func: Counter = 6
some_dummy_func: Counter = 12
blink_dummy_func: Counter = 7
some_dummy_func: Counter = 14
blink_dummy_func: Counter = 8
some_dummy_func: Counter = 16
blink_dummy_func: Counter = 9
some_dummy_func: Counter = 18
blink_dummy_func: Counter = 10
some_dummy_func: Counter = 20
2019-06-25 14:41:19 -04:00
...
2019-06-28 09:24:05 -04:00
```
2020-04-17 05:51:00 -04:00
### Generating Gcovr Report
2019-06-25 14:41:19 -04:00
2020-04-17 05:51:00 -04:00
After dumping one or more times, a coverage report can be generated by calling `cmake --build build/ --target gcovr-report` . This should result in an HTML code coverage report being generated in the build directory.
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
To clean Gcov and report related data from the build directory, call `cmake --build build/ --target cov-data-clean`
2017-08-16 19:36:00 -04:00
2019-06-25 14:41:19 -04:00
The following log should be output when generating the coverage report:
2017-08-16 19:36:00 -04:00
```
2020-04-17 05:51:00 -04:00
[1/2] Generating coverage report in: /home/user/esp/esp-idf/examples/system/gcov/build/coverage_report
2019-10-01 06:16:43 -04:00
Using gcov: xtensa-esp32-elf-gcov
2020-04-17 05:51:00 -04:00
[2/2] cd /home/user/esp/esp-idf/examples/system/gcov/build & & gcovr -r /home/user/esp/esp-idf/examples/system/gcov...a-esp32-elf-gcov -s --html-details /home/user/esp/esp-idf/examples/system/gcov/build/coverage_report/html/index.htm
lines: 100.0% (27 out of 27)
branches: 100.0% (2 out of 2)
2018-06-28 09:38:17 -04:00
```
2019-06-25 14:41:19 -04:00
## Troubleshooting
### OpenOCD Out of Sync
If the following log is output when issuing an OpenOCD command via telnet, it could indicate that OpenOCD and the ESP32 are out of sync. This occurs when the ESP32 is externally reset whilst connected to OpenOCD (e.g., by pressing the EN button).
```
Open On-Chip Debugger
2020-03-16 12:06:47 -04:00
> esp gcov dump
2019-06-25 14:41:19 -04:00
Target halted. PRO_CPU: PC=0x4008AFF4 (active) APP_CPU: PC=0x400E396E
Total trace memory: 16384 bytes
Connect targets...
Target halted. PRO_CPU: PC=0x400D5D74 (active) APP_CPU: PC=0x400E396E
timed out while waiting for target halted / 1 - 2
Failed to wait halt on bp target (-4)!
Failed to halt targets (-4)!
Failed to connect to targets (-4)!
```
This issue can be resolved in the following ways:
* Reset the board by issuing the `reset` command via telnet
* Restart OpenOCD
2020-04-17 05:51:00 -04:00
### gcovr not found
gcovr can be installed from the package database of your operating system or directly as a Python package, e.g:
2019-06-25 14:41:19 -04:00
2020-04-17 05:51:00 -04:00
```
python -m pip install gcovr
```