{"id":2265,"date":"2017-01-04T14:59:33","date_gmt":"2017-01-04T22:59:33","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2265"},"modified":"2017-01-04T14:59:33","modified_gmt":"2017-01-04T22:59:33","slug":"using-embedded-memory-explorer-to-track-code-dependencies","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/dependencies\/","title":{"rendered":"Using Embedded Memory Explorer to Track Code Dependencies"},"content":{"rendered":"<p>In this tutorial we will show how to use the Embedded Memory Explorer to track dependencies between various parts of your program. We will create a basic multi-threaded application based on FreeRTOS and will attempt using the STL types in it and show what problem it triggers and how to solve it.<\/p>\n<p>Before you begin, install VisualGDB 5.2 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2266\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname6.png\" alt=\"01-prjname\" width=\"786\" height=\"562\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname6.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname6-300x215.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; Embedded Binary&#8221; and click &#8220;next&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-binary1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2267\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-binary1.png\" alt=\"02-binary\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-binary1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-binary1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the next page select your toolchain and the device. We will use the STM32F4Discovery board with STM32F407VG, but the steps described below will work with any other board that can run FreeRTOS:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-device4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2268\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-device4.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-device4.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-device4-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the Sample Selection page choose the FreeRTOS sample:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-rtos1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2269\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-rtos1.png\" alt=\"04-rtos\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-rtos1.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-rtos1-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Finally select your debug method on the Debug Method page:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2273\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/debug.png\" alt=\"debug\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/debug.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/debug-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Then change the main file source extension from .c to .cpp and add extern &#8220;C&#8221; to the declaration of SysTick_Handler:\n<pre class=\"\">extern \"C\" void SysTick_Handler(void)\r\n{\r\n\u00a0\u00a0 \u00a0osSystickHandler();\r\n}<\/pre>\n<p>Then build the project and ensure it succeeds.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-build1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2270\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-build1.png\" alt=\"06-build\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-build1.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-build1-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-build1-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Now we will modify the code to use the <strong>vector<\/strong> and <strong>string<\/strong> classes from STL:\n<pre class=\"\">#include &lt;vector&gt;\r\n#include &lt;string&gt;\r\n\r\nstatic void LED_Thread1(void const *argument)\r\n{\r\n\u00a0\u00a0 \u00a0for (;;)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0std::vector&lt;std::string&gt; vec;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0vec.push_back(\"test1\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0vec.push_back(\"test2\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0vec.push_back(\"test3\");\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nstatic void LED_Thread2(void const *argument)\r\n{\r\n\u00a0\u00a0\u00a0 for (;;)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 std::vector&lt;std::string&gt; vec;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 vec.push_back(\"test1\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 vec.push_back(\"test2\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 vec.push_back(\"test3\");\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<\/li>\n<li>Press F5 to build and run the project. It will quickly trigger an exception in the operator new():<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2271\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-error.png\" alt=\"07-error\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-error.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-error-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-error-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Most memory-related problems under FreeRTOS are caused by accidental use of the standard <strong>malloc()<\/strong> and <strong>free()<\/strong> functions that are not aware of the FreeRTOS scheduler. So when a thread switch happens while <strong>malloc()<\/strong> or <strong>free()<\/strong> is running, this ultimately leads to memory corruption. Open the Embedded Memory Explorer via the View menu and locate the <strong>malloc()<\/strong> or <strong>_malloc_r()<\/strong> function:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-memexp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2272\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-memexp.png\" alt=\"08-memexp\" width=\"1019\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-memexp.png 1019w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-memexp-300x206.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/08-memexp-130x90.png 130w\" sizes=\"(max-width: 1019px) 100vw, 1019px\" \/><\/a><\/li>\n<li>Click the &#8216;show dependencies&#8217; button in the toolbar. VisualGDB will suggest enabling relocation record generation for the project. Click &#8220;yes&#8221; and build the project again:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-reloc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2274\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-reloc.png\" alt=\"09-reloc\" width=\"1222\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-reloc.png 1222w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-reloc-300x172.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-reloc-1024x586.png 1024w\" sizes=\"(max-width: 1222px) 100vw, 1222px\" \/><\/a><\/li>\n<li>Now you can enable the dependency view in memory explorer. Expand the dependencies for _malloc_r() to see the list of functions using it:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-depchain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2275\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-depchain.png\" alt=\"10-depchain\" width=\"977\" height=\"699\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-depchain.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-depchain-300x215.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a>The dependency view quickly shows that malloc() is used by the C++ operator new() that is in turn used by the vector and string allocators.<\/li>\n<li>The easiest way to fix this is to provide custom new() and delete() operators that will use the thread-aware FreeRTOS heap functions:\n<pre class=\"\">void * operator new(size_t size)\r\n{\r\n\u00a0\u00a0\u00a0 return pvPortMalloc(size);\r\n}\r\n\r\nvoid operator delete(void *p)\r\n{\r\n\u00a0\u00a0\u00a0 vPortFree(p);\r\n}<\/pre>\n<\/li>\n<li>Build the project. The memory explorer will show that the _malloc_r() function is only used by __sfmoreglue() that is in turn only used by the libc initialization code:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-sfp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2276\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-sfp.png\" alt=\"11-sfp\" width=\"977\" height=\"640\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-sfp.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-sfp-300x197.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>As there are no more references to malloc() and free() from the code that runs in FreeRTOS threads, it should no longer cause a crash. Press F5 to launch the project and confirm that the crash never happens. If you stop the project now, you should see that both threads are running normally:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-threads.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2277\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-threads.png\" alt=\"12-threads\" width=\"977\" height=\"790\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-threads.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-threads-300x243.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we will show how to use the Embedded Memory Explorer to track dependencies between various parts of<\/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,67],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2265"}],"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=2265"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2265\/revisions"}],"predecessor-version":[{"id":2278,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2265\/revisions\/2278"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}