{"id":6880,"date":"2020-10-19T08:13:47","date_gmt":"2020-10-19T15:13:47","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6880"},"modified":"2020-10-21T09:48:29","modified_gmt":"2020-10-21T16:48:29","slug":"using-live-coverage-to-watch-embedded-code-in-real-time","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/profiler\/embedded\/coverage\/live\/","title":{"rendered":"Using Live Coverage to Watch Embedded Code in Real Time"},"content":{"rendered":"<p>This tutorial shows how to use Live Coverage to debug embedded projects in a non-intrusive way. Live Coverage allows observing the execution of functions and specific branches in your program in real-time, without having to stop the program. Live Coverage is much faster than the <a href=\"https:\/\/visualgdb.com\/tutorials\/profiler\/embedded\/instrumentation\/\">instrumentation-based profiling<\/a>, as it doesn&#8217;t need to communicate every function call to the debugger. Instead it works by storing counters for functions and individual branches in RAM, and using VisualGDB&#8217;s live memory engine to read them while the program is running.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6881\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj-2.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project you are creating: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-prjname-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6882\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-prjname-1.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>In this tutorial we will create a new project for a basic USB device from scratch, and will then show how to enable Live Coverage. Select &#8220;<strong>Create a new project -&gt; Embedded Application<\/strong>&#8221; on the &#8220;Project Type&#8221; page of the wizard. Although Live Coverage does not require a specific build subsystem, we recommend using it with <strong>Advanced CMake<\/strong>, as this project type provides convenient commands for managing coverage settings via Solution Explorer: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6883\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-4.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select your toolchain and device, then click &#8220;Next&#8221;. In this tutorial we will use the STM32F407Discovery board, however Live Coverage will work with any ARM device that supports background memory reads: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6884\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>In this tutorial we will use the &#8220;<strong>USB Communications Device<\/strong>&#8221; sample to demonstrate how coverage works for frequently executed functions. However, you can also choose any other sample, including FreeRTOS-based ones:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-usbdev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6885\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-usbdev.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally, choose the debug settings that work with your board and click &#8220;Finish&#8221; to create the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6886\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-stlink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Before we enable code coverage for the project, build it via <strong>Build-&gt;Build Solution <\/strong>and open <strong>Embedded Memory Explorer<\/strong>. This will make sure VisualGDB remembers the initial memory footprint of the project as a baseline, allowing to measure the memory overhead of enabling the code coverage: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6887\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-build.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties and enable <strong>Live Coverage<\/strong> on the <strong>Code Coverage<\/strong> page:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-coverage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6888\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-coverage.png\" alt=\"\" width=\"1096\" height=\"690\" \/><\/a><\/li>\n<li>VisualGDB will automatically insert the <strong>bsp_configure_code_coverage()<\/strong> statement into CMakeLists.txt, enabling code coverage for all targets using the primary BSP. For MSBuild-based projects, VisualGDB will instead enable the MSBuild-level &#8220;Instrument for Code Coverage&#8221; setting (see our <a href=\"https:\/\/visualgdb.com\/tutorials\/profiler\/embedded\/coverage\/\">Embedded Code Coverage tutorial<\/a>). Build the project and observe the memory overhead in Embedded Memory Explorer:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-built2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6889\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-built2.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>The overhead is proportional to the amount of branches in the instrumented code. We will show how to considerably reduce it by de-instrumenting specific files and libraries later in the tutorial.<\/li>\n<li>Start the project without debugging. If you are using the <strong>USB Device<\/strong> sample, connect the secondary USB port of your development board to the computer. If the default Windows 10 driver for the USB COM port does not recognize the board, try explicitly installing the <strong>ST Virtual COM Port<\/strong> driver shipped with the ST-Link tool: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-devmgr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6890\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-devmgr.png\" alt=\"\" width=\"781\" height=\"572\" \/><\/a><\/li>\n<li>Enable the Raw Terminal on the virtual COM port created by the board via <strong>VisualGDB Project Properties -&gt; Raw Terminal<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-serterm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6891\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-serterm.png\" alt=\"\" width=\"1087\" height=\"718\" \/><\/a><\/li>\n<li>Now we are ready to debug the code. Press F5 to launch the debugging session and observe the Live Coverage window. It will show all functions instrumented for code coverage, with detailed line-by-line information. Enable the coverage highlighting (requires using Clang IntelliSense) and go to the <strong>VCP_read()<\/strong> function: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-cover.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6892\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-cover.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>VisualGDB will show that the <strong>VCP_read()<\/strong> function has been called many times, but never executed past the initial check for <strong>ReadDone<\/strong>.<\/li>\n<li>Type &#8216;1&#8217; in the COM port window in Visual Studio. The coverage for <strong>VCP_read()<\/strong> will change: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-typed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6893\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-typed.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>Note that because <strong>VCP_read()<\/strong> is continuously called by the loop in <strong>main()<\/strong>, VisualGDB will often capture the branch counters just as the function is executing the &#8220;<strong>ReadDone<\/strong>&#8221; check. This may sometimes confuse the <strong>gcov<\/strong> tool, marking the next branch (starting with &#8220;int remaining = &#8230;&#8221; line) as not covered. This will only affect the branch that was executing just at the time of memory read and will not have a cumulative effect. Hence unless the code spends most of the time in a single location, this will not cause any observable effects.<\/li>\n<li>Go to the <strong>VCP_write()<\/strong> function. The coverage will accurately show that <strong>USBD_CDC_TransmitPacket()<\/strong> never failed and that a buffer exceeding <strong>kMaxOutPacketSize<\/strong> was never passed to <strong>VCP_write()<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-write.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6894\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-write.png\" alt=\"\" width=\"1193\" height=\"895\" \/><\/a><\/li>\n<li>Try switching the coverage highlighting to &#8220;Recently executed code&#8221; mode. It will highlight recently taken branches with a brighter color, making it easier to track what the program is doing in real time: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-recent.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6895\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-recent.png\" alt=\"\" width=\"1193\" height=\"742\" \/><\/a><\/li>\n<li>You can also use Live Coverage to see what code executed as you stepped over a function, or between breakpoints. Set a breakpoint on a call to <strong>VCP_write()<\/strong> and wait for it to trigger: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-write.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6896\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/17-write.png\" alt=\"\" width=\"1193\" height=\"742\" \/><\/a><\/li>\n<li>Press F10 to step over the call to <strong>VCP_write()<\/strong> and switch <strong>Live Coverage<\/strong> to <strong>Recent Lines<\/strong> view:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-lines.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6897\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/18-lines.png\" alt=\"\" width=\"1193\" height=\"742\" \/><\/a><\/li>\n<li>VisualGDB will highlight all lines that executed during that step. Try double-clicking on the <strong>HAL_PCD_DataInStageCallback()<\/strong> line in the Live Coverage window, then select <strong>USBD_LL_DataInStage()<\/strong> and click &#8220;Go to Definition&#8221;. You will easily see which parts of <strong>USBD_LL_DataInStage()<\/strong> were executed this time without the need to step through it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-stage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6898\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/19-stage.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a>This could be useful for quickly understanding what part of a function caused an error.<\/li>\n<li>Now we will show how to measure and reduce the memory overhead caused by Live Coverage. Open <strong>Embedded Memory Explorer<\/strong> and switch to only viewing the differences from the last build. It will show <strong>__gcov0.&lt;&#8230;&gt;<\/strong> symbols added by GCC during code instrumentation:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-delta.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6899\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/20-delta.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>You can considerably reduce the overhead by disabling instrumentation for specific files or libraries. Right-click on the <strong>STM32F4 HAL Library<\/strong> and select &#8220;<strong>Disable Code Coverage<\/strong>&#8220;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-disable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6900\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/21-disable.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>VisualGDB will update the <strong>bsp_configure_code_coverage()<\/strong> statement, excluding the HAL library from instrumentation: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/22-conf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6901\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/22-conf.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Similarly, you can disable code coverage instrumentation for specific files anywhere in the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/23-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6902\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/23-file.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>Build the project and observe how the memory overhead considerably reduced:\u00a0<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/24-use-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6906\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/24-use-1.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a>Note that for Advanced CMake projects VisualGDB will automatically use a different Solution Explorer icon for files instrumented for code coverage.<\/li>\n<li>Press F5 to start another debugging session. Code coverage will work as expected for the files that were still instrumented:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/25-stats.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6904\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/25-stats.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a><\/li>\n<li>You can view a list of all files\/targets excluded from code coverage via the VS properties window for the BSP node in Solution Explorer: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/26-props.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6905\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/26-props.png\" alt=\"\" width=\"1193\" height=\"873\" \/><\/a>Alternatively, check the <strong>bsp_configure_code_coverage()<\/strong> statement in the project&#8217;s <strong>CMakeLists.txt<\/strong> file.<\/li>\n<\/ol>\n<p>If you would like to observe values of various variables without stopping the program, check out our <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/livevars\/\">Live Watch tutorial<\/a> for a detailed step-by-step example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use Live Coverage to debug embedded projects in a non-intrusive way. Live Coverage allows observing<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[53,156,204],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6880"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=6880"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6880\/revisions"}],"predecessor-version":[{"id":6909,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6880\/revisions\/6909"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}