{"id":4618,"date":"2019-04-25T22:31:21","date_gmt":"2019-04-26T05:31:21","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4618"},"modified":"2019-04-25T22:31:21","modified_gmt":"2019-04-26T05:31:21","slug":"diagnosing-problems-in-imported-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/import\/diagnose\/","title":{"rendered":"Diagnosing Problems in Imported Projects"},"content":{"rendered":"<p>This tutorial shows how to diagnose problems in projects imported into VisualGDB from other IDEs by comparing the command lines used to build the projects with VisualGDB and in the other IDE.<\/p>\n<p>Normally, the steps described in this tutorial are not needed because VisualGDB can automatically import commonly used settings from most popular project formats. However, sometimes, overridden values of rarely used settings can trigger behavior difference between the projects built with VisualGDB and other IDEs and this tutorial provides detailed steps on diagnosing such differences.<\/p>\n<p>In this tutorial we will do the following:<\/p>\n<ul>\n<li>Create a project with the Keil uVision IDE that will trigger a stack overflow unless full optimization is used.<\/li>\n<li>Override the optimization in Keil project properties.<\/li>\n<li>Import the project into VisualGDB using the basic Keil importing plugin. This will handle the source files and common settings (e.g. include search path and preprocessor macros), but will preserve the default optimization settings, triggering the stack overflow.<\/li>\n<li>We will then compare the 2 versions of the project, showing how to quickly locate the differences between them and to adjust the settings accordingly.<\/li>\n<\/ul>\n<p>Before you begin, install VisualGDB 5.4 or later.<\/p>\n<ol>\n<li>Start the Keil uVision IDE and create an RTX-based project with the following code in one of the thread functions:\n<pre class=\"\">void Thread1Body(void *argument)\r\n{\r\n    int bigvar[4096];\r\n    for (;;)\r\n    {\r\n        bigvar[4095] = 0;\r\n        LED_On(0);\r\n        osDelay(100);\r\n        LED_Off(0);\r\n        osDelay(100);\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4619\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/01-prj.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a>This will trigger a stack overflow unless the bigvar variable is optimized out.<\/li>\n<li>Build the debug configuration of the project and ensure it crashes. Then change the optimization level to O3 and ensure that the project starts working as expected:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/optimize.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4637\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/optimize.png\" alt=\"\" width=\"626\" height=\"468\" \/><\/a><\/li>\n<li>Now we will import the project into VisualGDB and will build it using the Keil compiler. Start the VisualGDB Embedded Project Wizard and enter the name and location of the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/03-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4621\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/03-newprj.png\" alt=\"\" width=\"941\" height=\"653\" \/><\/a><\/li>\n<li>Then choose &#8220;Automatically import a project in a different format&#8221; -&gt; &#8220;Import an existing Keil Project&#8221; -&gt; &#8220;Keil ARM Compiler&#8221; and check the &#8220;Move the Visual Studio project to the imported project directory&#8221; checkbox:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/04-import.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4622\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/04-import.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page of the wizard select the device to use:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/05-armclang.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4623\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/05-armclang.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally select the debug settings that will work with your setup and click &#8220;Finish&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/06-stlink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4624\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/06-stlink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>As of version 5.4, VisualGDB will import the basic project structure, including the project-specific source files, preprocessor macros and header search directories, however it will not automatically import advanced compiler settings, or the Keil packs:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/07-imported.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4625\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/07-imported.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>To fix the Keil Packs, open VisualGDB Project Properties -&gt; Keil Components (Custom edition and higher) and select the packs to reference:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/08-components.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4626\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/08-components.png\" alt=\"\" width=\"1034\" height=\"753\" \/><\/a><\/li>\n<li>Build the project and ensure that it triggers the stack overflow (i.e. the on-board LEDs will not blink as expected):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/09-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4627\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/09-built.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>Now we will begin troubleshooting the project. First of all, open the Embedded Memory Explorer and try comparing the ELF file built by VisualGDB against the ELF file built by Keil (typically the .axf extension):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/10-compare.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4628\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/10-compare.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>Then, switch to the detailed view and sort the symbols by the size difference:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/11-delta.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4629\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/11-delta.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a>In many cases this will provide clues about the root cause of the error. E.g. if the VisualGDB-built file is missing some sections, the difference could be caused by the linker script\/scatter file. Multiple symbols being considerably larger would suggest an optimization level difference.<\/li>\n<li>The next step would be to verify that the difference is caused by the build-time differences, not debug settings. Replace the ELF file produced by VisualGDB (typically <strong>VisualGDB\\Debug\\&lt;Project Name&gt;<\/strong>) with the ELF file produced by Keil (typically <strong>Objects\\&lt;Project Name&gt;.axf<\/strong>) and disable automatic project rebuild via Visual Studio options:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/12-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4630\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/12-build.png\" alt=\"\" width=\"744\" height=\"434\" \/><\/a>If the ELF file built with Keil uVision runs successfully under the VisualGDB debugger, the problem is caused by build-time settings and the rest of the tutorial will show how to quickly narrow it down.<\/li>\n<li>Enable batch file generation in the Keil IDE and build the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/13-batch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4631\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/13-batch.png\" alt=\"\" width=\"626\" height=\"468\" \/><\/a>This will dump all command line arguments used by Keil to text files with the <strong>.__i<\/strong>\u00a0or <strong>.lnp<\/strong> extension and will generate a .bat file invoking the Keil compiler and linker.<\/li>\n<li>Locate and open the .bat file generated by the Keil IDE. See how it contains compiler\/assembler command lines (lines 9-26) followed by a linker command line (line 27):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/14-cmdlines-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4636\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/14-cmdlines-1.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a>E.g. The command &#8220;C:\\Keil_v5\\ARM\\ARMCLANG\\Bin\\ArmLink&#8221; &#8211;Via &#8220;.\\Objects\\ImportTroubleshootingDemo.lnp&#8221; means &#8220;run the ARM linker with command-line options taken from the\u00a0ImportTroubleshootingDemo.lnp file.<\/li>\n<li>VisualGDB uses a similar mechanism for handling the command lines &#8211; it stores the command line arguments to <strong>.rsp<\/strong> files (instead of <strong>.__i<\/strong> or <strong>.lnp<\/strong>) and runs the compiler, assembler or linker the same way. Locate the .link.rsp file in the VisualGDB\\Debug directory and open in in a text editor:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/15-rspfile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4633\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/15-rspfile.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>Since it is not practical to compare each and every command line, we will now do a series of tests to quickly identify the one responsible for the problem, starting with the linekr. Open the command prompt window in the project directory and run the following command:\n<pre class=\"\">\"C:\\Keil_v5\\ARM\\ARMCLANG\\Bin\\ArmLink\" --Via VisualGDB\\Debug\\ImportTroubleshootingDemo.link.rsp<\/pre>\n<p>This will re-link the ELF file produced by VisualGDB (not rebuilding the object files):<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/16-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4634\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/16-built.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a><\/li>\n<li>Now we will begin our tests. First of all, replace the .o files (inputs of the linker) in the <strong>VisualGDB\\Debug<\/strong> directory with the .o files produced by Keil (from the <strong>Objects<\/strong> directory) and re-run the linker command line:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/17-cmdline2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4635\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/17-cmdline2.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a>Try debugging the VisualGDB project again (do not rebuild it, as it would replace the file we manually linked). If the project runs successfully, the problem is caused by one of the source files. If the project still doesn&#8217;t work, the problem is caused by the linker command line.<\/li>\n<li>If the problem is caused by the command line, try regrouping both the Keil&#8217;s <strong>.lnp<\/strong> file and the VisualGDB&#8217;s <strong>.rsp<\/strong> file to have the common options at the beginning and the differences at the end. Then, merge the differences, half at a time and check whether the result works. This will help quickly identify the missing setting. In this tutorial the problem is caused by the optimization settings of the main source file, so replacing the command line is not necessary (i.e. the project will run successfully with VisualGDB&#8217;s command line when using the .o files from uVision).<\/li>\n<li>Now we will identify a specific .o file responsible for the problem. The easiest way to do it is to restore (or rebuild) the .o files created by VisualGDB and then replace just half of them with the Keil equivalents. If the program&#8217;s behavior changes, it was caused by one of the recently replaced files. Roll back the replacement and repeat it with just the half of the files replaced last time. Due to the logarithmic complexity of this approach, it will only take a few iterations to narrow down the .o file responsible for the problem, even for huge projects.<\/li>\n<li>Locate the <strong>.__i<\/strong> and <strong>.rsp<\/strong> files for the <strong>.o<\/strong> file that is causing the problem:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/18-rspfiles.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4638\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/18-rspfiles.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>Regroup the .rsp file settings into 5 groups:\n<ul>\n<li>Include directories<\/li>\n<li>Preprocessor macros<\/li>\n<li>Flags that are common between 2 files<\/li>\n<li>Flags that have different values (e.g. optimization settings)<\/li>\n<li>Flags that are missing in one of the files<\/li>\n<\/ul>\n<p>Remove the warning-related flags, as they do not affect the compilation result. Also separate the part that specifies the names of the source file, object file and the dependency file:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/19-regrouped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4639\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/19-regrouped.png\" alt=\"\" width=\"1197\" height=\"782\" \/><\/a><\/li>\n<li>Verify that you can still build the object file using the regrouped .rsp file from VisualGDB (note that Clang-based ARMv6 compiler uses the <strong>@&lt;RSP file&gt;<\/strong> syntax instead of <strong>&#8211;Via &lt;RSP file&gt;<\/strong>) by running the compiler and linker manually:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/20-build-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4641\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/20-build-1.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a>Then ensure that the problem still persists.<\/li>\n<li>Replace all of the flags (except the input\/output file name specifications) with the version captured the uVision IDE. Build the source file, link the project and ensure that the problem got resolved.<\/li>\n<li>Now that we have confirmed that the problem is triggered by the compiler flags, we can narrow it down to a specific flag. Revert to the regrouped version of the VisualGDB&#8217;s RSP file and replace the first 2 groups with the values from the Keil&#8217;s file and check whether the behavior changes.<\/li>\n<li>Repeat the step above until you locate the flag group responsible for the behavior difference, then use the same approach to narrow down a specific flag. Once you have located it, change the corresponding setting in Visual Studio Project Properties and verify that the command line looks as expected:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/21-props.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4642\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/04\/21-props.png\" alt=\"\" width=\"838\" height=\"558\" \/><\/a>Now your VisualGDB-based projects should produce exactly the same results as the uVision-based one.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to diagnose problems in projects imported into VisualGDB from other IDEs by comparing the command lines<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[75,147],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4618"}],"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=4618"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4618\/revisions"}],"predecessor-version":[{"id":4644,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4618\/revisions\/4644"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}