{"id":6481,"date":"2020-09-22T22:41:41","date_gmt":"2020-09-23T05:41:41","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6481"},"modified":"2020-09-22T22:41:41","modified_gmt":"2020-09-23T05:41:41","slug":"monitoring-dynamic-stack-and-heap-usage-of-iar-based-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/iar\/stackheap\/","title":{"rendered":"Monitoring Dynamic Stack and Heap Usage of IAR-based Projects"},"content":{"rendered":"<p>This tutorial shows how to use the VisualGDB Live Watch to monitor the live stack and heap usage of projects built with the IAR compiler.<\/p>\n<p>We will create a basic IAR project for the STM32F4Discovery board, and will show how to check its stack and heap usage. Before you begin, install VisualGDB 5.5 or later and make sure you are using the Custom edition or higher.<\/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\/07\/01-newprj-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6482\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/01-newprj-2.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project you are creating, then click &#8220;Create&#8221; to proceed with the VisualGDB-specific part of the wizard: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/02-name-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6483\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/02-name-1.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>On the first page of the wizard, select &#8220;Create a new project -&gt; Embedded binary -&gt; MSBuild&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/03-msbuild.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6484\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/03-msbuild.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Then, choose the IAR compiler from the toolchain list and select your device:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/04-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6485\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/04-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>The IAR compiler does not include device-specific project samples, so simply proceed with creating an empty project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/05-nosample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6486\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/05-nosample.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the <strong>Debug Method<\/strong> page of the wizard, select the debugging settings that work with your setup:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/06-debug-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6487\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/06-debug-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>When done, click &#8220;Finish&#8221; to create the project.<\/li>\n<li>As the created project does not contain any sources yet, right-click on the <strong>Source Files<\/strong> folder and select <strong>Add-&gt;New Item<\/strong>. Then proceed with adding a C++ source:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/07-newsrc-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6496\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/07-newsrc-2.png\" alt=\"\" width=\"1222\" height=\"888\" \/><\/a><\/li>\n<li>Copy the following code into into the source file you just created:\n<pre class=\"\">#include &lt;iar_dlmalloc.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct mallinfo IARHeapInfo;\r\n\r\nvoid RecursionTest(int level)\r\n{\r\n    volatile int test = 123;\r\n    if (level &gt; 0)\r\n        RecursionTest(level - 1);\r\n}\r\n\r\nint main()\r\n{\r\n    RecursionTest(1);\r\n    RecursionTest(2);\r\n    RecursionTest(3);\r\n   \r\n    IARHeapInfo = __iar_dlmallinfo();\r\n    void *p = malloc(123);\r\n    IARHeapInfo = __iar_dlmallinfo();\r\n    free(p);\r\n    IARHeapInfo = __iar_dlmallinfo();\r\n}<\/pre>\n<p>The sample code calls the <strong>RecursionTest()<\/strong> function to demonstrate increasing stack usage, then it calls <strong>malloc()<\/strong> and <strong>free()<\/strong> to demonstrate the use of heap. Note that IAR does not automatically update the heap statistics unless you explicitly call the <strong>__iar_dmallinfo()<\/strong> function as shown in this tutorial. In order to monitor the heap usage in real time, you can create a wrapper around <strong>malloc()\/free()<\/strong> that will call <strong>__iar_dmallinfo()<\/strong>, or simply call it periodically from an interrupt handler.<\/li>\n<li>Note that if you try building the project now, it will fail because the default heap mode is not compatible with the <strong>__iar_dmallinfo()<\/strong> function:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/08-malloc-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6497\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/08-malloc-1.png\" alt=\"\" width=\"1222\" height=\"711\" \/><\/a><\/li>\n<li>In order to resolve it, change the heap implementation to &#8220;Advanced Heap&#8221; via VS Project Properties: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/09-heap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6490\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/09-heap.png\" alt=\"\" width=\"786\" height=\"544\" \/><\/a><\/li>\n<li>Before we can monitor the stack usage, we need to configure VisualGDB to fill the stack area (between<strong> CSTACK$$Base<\/strong> and the value of <strong>$sp<\/strong> in <strong>main()<\/strong>) with a fixed pattern. This can be done via the Embedded Debug Tweaking page of VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/10-fill.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6491\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/10-fill.png\" alt=\"\" width=\"841\" height=\"518\" \/><\/a><\/li>\n<li>\u00a0 Set a breakpoint on the second line of <strong>main()<\/strong> (due to the way breakpoints are handled internally, setting it at the beginning of main() may interfere with the logic that fills stack with the fixed pattern), then start debugging. Once the breakpoint hits, open <strong>Debug-&gt;Windows-&gt;Live Watch<\/strong> and switch to the <strong>Stack\/Heap<\/strong> view. You will now see the worst-case stack use and the current heap state:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/11-bkpt-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6498\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/11-bkpt-1.png\" alt=\"\" width=\"1222\" height=\"710\" \/><\/a><\/li>\n<li>The stack use is computed by repeatedly scanning the stack area and searching for the end of the initial stack pattern. It works in the background (even when the program is running) and produces accurate results based on the actual stack use. Try stepping over <strong>RecursionTest()<\/strong> and observe how the highest stack usage increases: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/12-stack-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6499\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/12-stack-1.png\" alt=\"\" width=\"1222\" height=\"887\" \/><\/a><\/li>\n<li>The heap statistics is computed by analyzing the global <strong>IARHeapInfo<\/strong> variable that must be explicitly updated from the code. It also works while the program is running, but relies on the program to call <strong>__iar_dlmallinfo()<\/strong> periodically. Try stepping over different calls to <strong>__iar_dmallinfo()<\/strong> and observe how the heap statistics in the Live Watch window changes:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/13-heap-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6500\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/07\/13-heap-1.png\" alt=\"\" width=\"1222\" height=\"887\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the VisualGDB Live Watch to monitor the live stack and heap usage of projects<\/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,157,219,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6481"}],"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=6481"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6481\/revisions"}],"predecessor-version":[{"id":6501,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6481\/revisions\/6501"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}