{"id":5329,"date":"2019-12-16T20:44:18","date_gmt":"2019-12-17T04:44:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=5329"},"modified":"2019-12-16T20:44:18","modified_gmt":"2019-12-17T04:44:18","slug":"configuring-symbol-loading-behavior-for-linux-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/symbols\/load\/","title":{"rendered":"Configuring Symbol Loading Behavior for Linux Projects"},"content":{"rendered":"<p>This tutorial shows how to speed up debugging of Linux projects by controlling which of the shared library symbols will get loaded automatically. We will also show how to automatically strip debug symbols from the deployed binaries in order to reduce the space used on the target. Before you begin, install VisualGDB 5.5 or later and ensure you are using the Custom edition or higher.<\/p>\n<p>In this tutorial we will target Raspberry Pi with a Windows-based cross-toolchain using a CMake-based project, however most of the steps shown below will also apply to other project types.<\/p>\n<ol>\n<li>Start Visual Studio and select the VisualGDB Linux Project Wizard under File-&gt;New-&gt;Project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/01-wizards.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5330\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/01-wizards.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>Enter the name and location where you would like to create the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5331\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/02-name.png\" alt=\"\" width=\"1024\" height=\"710\" \/><\/a><\/li>\n<li>On the first page of the VisualGDB Linux Project Wizard select &#8220;Create a new project -&gt; Application -&gt; CMake&#8221; and check the &#8220;Advanced CMake Project Subsystem&#8221; page:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/03-cmake.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5332\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/03-cmake.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>We will use the Advanced CMake Project Subsystem as it is the easiest way to create a single project containing multiple targets (main executable and several libraries), however the configuration steps shown below will also work for Make-based, MSBuild-based and Qt-based projects.<\/li>\n<li>In this tutorial we will use the Raspberry Pi cross-compiler to build the code on the Windows machine and deploy it to the target. However, the rest of the tutorial (except automatic symbol stripping) will also work for projects built directly on the target:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/04-cross.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5333\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/04-cross.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to complete the wizard. VisualGDB will generate a CMake-based project consisting of the main executable. Right-click on the project node in Solution Explorer and select Add-&gt;New Item:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/05-newitem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5334\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/05-newitem.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Select &#8220;Shared Library&#8221; in the &#8220;Add New Project Item&#8221; dialog, enter &#8220;SharedLibrary1&#8221; in the <strong>Name<\/strong> field and press Add to proceed with creating it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/06-lib1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5335\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/06-lib1.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Repeat the previous step to create another library called <strong>SharedLibrary2<\/strong>. Then open the source files for both <strong>SharedLibrary1<\/strong> and <strong>SharedLibrary2<\/strong> and rename the functions defined in them accordingly:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/07-renamed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5336\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/07-renamed.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Update the main source file to call the shared library functions:\n<pre class=\"\">void SharedLibraryFunction1();\r\nvoid SharedLibraryFunction2();\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    SharedLibraryFunction1();\r\n    SharedLibraryFunction2();\r\n\r\n    char sz[] = \"Hello, World!\";\t\/\/Hover mouse over \"sz\" while debugging to see its contents\r\n\tcout &lt;&lt; sz &lt;&lt; endl;\t\/\/&lt;================= Put a breakpoint here\r\n\treturn 0;\r\n}<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/08-call.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5337\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/08-call.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Finally, add references to the shared libraries to the main executable:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/09-reference.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5338\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/09-reference.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Press F5 to build the project and start debugging it. VisualGDB will automatically deploy the built files and load their symbols, allowing you to step through the code:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/11-step.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5339\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/11-step.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>The shared libraries created in this tutorial are very small, hence loading their symbols does not slow down the debug sessions. However, debugging projects with multiple large shared libraries could result in considerable startup delays. We will now show how to control which symbols get loaded automatically in order to reduce the delays. Go to the <strong>SharedLibraryFunction1()<\/strong> function and add the following lines to it:\n<pre class=\"\">\u00a0   int **p = 0;\r\n    p[0] = 0;<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/14-segv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5341\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/14-segv.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a>This will trigger a SIGSEGV signal when the function gets executed.<\/li>\n<li>Open VisualGDB Project Properties -&gt; Debug Symbols (available in Custom edition or higher). VisualGDB will show the libraries loaded during the previous debug session, noting down how much time it took to process each one of them. Locate <strong>libSharedLibrary1.so<\/strong> in the list and uncheck it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/15-noload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5342\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/15-noload.png\" alt=\"\" width=\"1067\" height=\"662\" \/><\/a><\/li>\n<li>Click <strong>OK<\/strong> to close the VisualGDB Project Properties window and press F5 to begin debugging again. The code we added to <strong>SharedLibraryFunction1()<\/strong> will now trigger a SIGSEGV error:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/17-segv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5343\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/17-segv.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a>As we have disabled the symbol loading for <strong>SharedLibrary1<\/strong>, stepping into the code defined in that library, or setting breakpoints there will not work. Visual Studio will also display a generic &#8220;library not loaded&#8221; message for the SIGSEGV exception raised on our code, instead of pointing to a specific source line.<\/li>\n<li>You can still debug the code in <strong>SharedLibrary1<\/strong> by loading the symbols manually. Simply select &#8220;Load Symbols&#8221; in either Call Stack window, or the Modules window and VisualGDB will automatically load the missing symbols, as long as they are available:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/18-loadsyms.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5344\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/18-loadsyms.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>If the source location is not shown immediately, double-click at the topmost entry in the Call Stack and Visual Studio will show the source location:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/19-updated.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5345\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/19-updated.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a>When debugging large real-world projects with multiple shared libraries, we recommend disabling automatic symbol loading for the libraries where you do not expect to trigger any breakpoints, and loading them on-demand via the Call Stack window as shown above. The <strong>Debug Symbols<\/strong> page of VisualGDB Project Properties will show the exact amount of time that could be saved by skipping the loading of each shared library.<\/li>\n<li>Now we will show how to reduce the size of the debugged binaries on the target by automatically stripping debug symbols from them. Open VS Project Properties for the project node and change the deployment directory to <strong>\/tmp\/SymbolManagementDemo<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/12-deploy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5340\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/12-deploy.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Start debugging. Once a breakpoint hits, open the SSH Console window and list the contents of the deployment directory. Note the sizes of the executables and libraries in that directory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/20-sizes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5346\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/20-sizes.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties on the <strong>Debug Settings<\/strong> page and check the &#8220;<strong>Strip debug symbols during deployment<\/strong>&#8221; checkbox:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/21-strip.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5347\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/21-strip.png\" alt=\"\" width=\"1067\" height=\"662\" \/><\/a><\/li>\n<li>Start debugging again and list the deployment directory contents once more:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/22-stripped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5348\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/10\/22-stripped.png\" alt=\"\" width=\"1197\" height=\"830\" \/><\/a>Note how the size of the main binary and the libraries has been reduced because VisualGDB has removed the debug symbols from them. This will only work for cross-compiled projects where gdb (that requires the debug symbols) is running on a different machine than the target itself.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to speed up debugging of Linux projects by controlling which of the shared library symbols will<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[77,79,33,189],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5329"}],"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=5329"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5329\/revisions"}],"predecessor-version":[{"id":5349,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/5329\/revisions\/5349"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=5329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=5329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=5329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}