{"id":6510,"date":"2020-07-26T20:55:27","date_gmt":"2020-07-27T03:55:27","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6510"},"modified":"2020-07-26T20:55:27","modified_gmt":"2020-07-27T03:55:27","slug":"monitoring-live-freertos-state-with-live-watch","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/livewatch\/freertos\/","title":{"rendered":"Monitoring Live FreeRTOS State with Live Watch"},"content":{"rendered":"<p>This tutorial shows how to monitor the live state of the FreeRTOS kernel using the Live Watch feature of VisualGDB. Live Watch works non-intrusively by scanning the target memory as it is running, and reconstructing various information about FreeRTOS. As of VisualGDB 5.5, Live Watch can show the following FreeRTOS parameters:<\/p>\n<ul>\n<li>Active threads and their state<\/li>\n<li>Current\/maximum stack usage by each thread<\/li>\n<li>Internal state of queues, mutexes and semaphores<\/li>\n<li>Mutexes owned by each thread<\/li>\n<li>Heap statistics and the list of allocated blocks<\/li>\n<\/ul>\n<p>In this tutorial we will create a basic FreeRTOS project for the STM32F4-Discovery board and will demonstrate various Live Watch views.<\/p>\n<p>Before you begin, install VisualGDB 5.5 and make sure you are using the Custom edition or higher.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/01-wizard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6511\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/01-wizard.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Enter the name and location of the project you are going to create: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/02-rtosdemo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6512\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/02-rtosdemo.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Proceed with the default settings on the first page of the VisualGDB-specific part of the wizard: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/03-msbuild-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6513\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/03-msbuild-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page, select the ARM toolchain and your target device. In this tutorial we will target the STM32F4-Discovery board, so we select the STM32F407VG device: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/04-device-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6514\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/04-device-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page, select the LEDBlink (FreeRTOS) sample. You can adjust the LED pins to match your board schematics, or simply proceed with the default settings, as the functionality shown in this tutorial will work without LEDs as well: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/05-blink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6515\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/05-blink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally choose the debugging settings that work with your board and click Finish to create the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/06-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6516\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/06-stlink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Once the project is created, press Ctrl-Shift-B to build it: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/07-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6517\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/07-built.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>Start debugging by pressing F5. Without stopping the project, select <strong>Debug-&gt;Windows-&gt;Live Watch<\/strong> and then switch it to the FreeRTOS mode (requires Custom edition): <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/08-rtos.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6518\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/08-rtos.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>The FreeRTOS view contains 4 main groups of nodes: <strong>Kernel statistics<\/strong>, <strong>Threads<\/strong>, <strong>Synchronization Primitives<\/strong> and <strong>Heap<\/strong>. We will demonstrate each of these items below. Expand the <strong>Kernel<\/strong> node and start plotting the <strong>Tick Count<\/strong> variable. VisualGDB will show how the tick count is increasing over time:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/09-stats.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6519\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/09-stats.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>Now expand the <strong>Threads<\/strong> node and locate LED2 in the list. See how its state changes between <strong>Delayed<\/strong> and <strong>Suspended<\/strong> as the LED1 thread keeps suspending and resuming it. Note that as none of the threads is doing any heavy computation, the <strong>IDLE<\/strong> thread remains active each time VisualGDB samples the thread state:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/10-threads.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6520\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/10-threads.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>Now we will show how to monitor the stack usage by FreeRTOS threads. Add a <strong>RecursionTest()<\/strong> function and modify <strong>LED_Thread2()<\/strong> to call it:\n<pre class=\"\">void RecursionTest(int level)\r\n{\r\n    volatile int x[5];\r\n    x[0]++;\r\n    if (level &gt; 0)\r\n        RecursionTest(level - 1);\r\n    else\r\n\tosDelay(200);\r\n}\r\n\r\nstatic void LED_Thread2(void const *argument)\r\n{\r\n    uint32_t count;\r\n    (void) argument;\r\n  \r\n    for (;;)\r\n    {\r\n        RecursionTest(1);\r\n        RecursionTest(2);\r\n        RecursionTest(3);\r\n        RecursionTest(4);\r\n        RecursionTest(5);\r\n    }\r\n}<\/pre>\n<p>Then set a breakpoint at the first call to <strong>RecursionTest()<\/strong>, start debugging and let the breakpoint trigger:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/11-usage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6521\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/11-usage.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>Locate the LED2 thread in the Live Watch view and start plotting its <strong>stack usage<\/strong> and <strong>highest stack usage<\/strong>. See how the stack usage keeps increasing together with the recursion level, and the highest stack usage reaches the maximum and remains the same:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/12-usage2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6522\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/12-usage2.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a>The current stack usage is sampled based from the saved stack pointer in the thread control block, so it will not be accurate while the thread is currently running. The highest stack usage is computed by scanning the memory region allocated for the stack and checking which parts of it have overwritten the initial padding value.<\/li>\n<li>Now we will show how to view the state of various synchronization primitives. Add a new Plain C file to the project (e.g. <strong>SemaphoreTests.c<\/strong>) and add the following code to it:\n<pre class=\"\">#include &lt;cmsis_os.h&gt;\r\n#include &lt;stm32f4xx_hal.h&gt;\r\nstatic xSemaphoreHandle s_DemoSemaphore;\r\n\r\nstatic void SenderThreadBody()\r\n{\r\n    for (;;)\r\n    {\r\n        for (int i = 0; i &lt; 5; i++)\r\n            xSemaphoreGive(s_DemoSemaphore);\r\n        osDelay(10000);\r\n    }\r\n}\r\n\r\nstatic void ReceiverThreadBody()\r\n{\r\n    for (;;)\r\n    {\r\n        xSemaphoreTake(s_DemoSemaphore, portMAX_DELAY);\r\n        osDelay(1000);\r\n    }\r\n}\r\n\r\nvoid StartSemaphoreTestThreads()\r\n{\r\n    TaskHandle_t task;\r\n\r\n    s_DemoSemaphore = xSemaphoreCreateCounting(100, 0);\r\n    xTaskCreate(SenderThreadBody, \"Sender\", 128, 0, 1, &amp;task);\r\n    xTaskCreate(ReceiverThreadBody, \"Receiver\", 128, 0, 1, &amp;task);\r\n}<\/pre>\n<p>Then call <strong>StartSemaphoreTestThreads()<\/strong> from the main source file just before the call to <strong>osKernelStart()<\/strong>. If you start debugging now, VisualGDB will show <strong>s_DemoSemaphore<\/strong> in the <strong>Synchronization Primitives<\/strong> view:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/13-sync.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6523\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/13-sync.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>You can plot the current value of the semaphore. VisualGDB will show how it increases to 4 as the sender thread releases it 5 times (it&#8217;s immediately taken by the receiver, so the value of 5 never stays long enough to be sampled) and then slowly drops to 0:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/14-sema.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6524\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/14-sema.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a>Live Watch will also show the threads waiting for a semaphore or a queue, and the mutexes owning a mutex.<\/li>\n<li>Finally, we will show how to observe the state of FreeRTOS heap. Modify LED_Thread1() as shown below:\n<pre class=\"\">        char *test = (char *)pvPortMalloc(16);\r\n        memset(test, 0x55, 16);\r\n        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);\r\n\t\tosDelay(2000);\r\n        vPortFree(test);<\/pre>\n<p>Then start debugging and begin plotting the <strong>Heap -&gt; Allocated Blocks<\/strong> metric:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/15-graph.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6525\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/15-graph.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a>VisualGDB will show how it switches between 14 and 15, as the thread keeps allocating and releasing the memory.<\/li>\n<li>You can also view individual heap blocks. Expand the <strong>[Heap Blocks]<\/strong> node and go to the end of the list:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/16-blocks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6526\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/16-blocks.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<li>The block allocated from <strong>LED_Thread1()<\/strong> will appear at the end of the list, showing the correct contents: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/17-55.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6527\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/17-55.png\" alt=\"\" width=\"1055\" height=\"796\" \/><\/a><\/li>\n<\/ol>\n<p>The FreeRTOS-specific logic of Live Watch is implemented in a separate <a href=\"https:\/\/github.com\/sysprogs\/RTOSPlugins\/tree\/master\/PluginFreeRTOS\">open-source plugin<\/a>, so you can fork and extend it to support any project-specific functionality you would like to monitor.<\/p>\n<p>You can find the project shown in this tutorial in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/ARM\/LiveWatch\/LiveRTOSDemo\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to monitor the live state of the FreeRTOS kernel using the Live Watch feature of VisualGDB.<\/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":[67,204],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6510"}],"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=6510"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6510\/revisions"}],"predecessor-version":[{"id":6528,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6510\/revisions\/6528"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}