{"id":156,"date":"2014-02-04T17:39:31","date_gmt":"2014-02-04T17:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=156"},"modified":"2014-02-04T17:39:31","modified_gmt":"2014-02-04T17:39:31","slug":"linux-quickdebug","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/quickdebug\/","title":{"rendered":"Using Quick Debug to debug Linux applications"},"content":{"rendered":"\n<p>This tutorial shows how to quickly debug a Linux program built with GCC without creating a separate Visual Studio project.<\/p>\n<p>In this tutorial we will build GNU binutils on a Linux machine and use Visual Studio to debug the <strong>cxxfilt<\/strong> program (GNU C++ name demangler) to find out which function inside it does the actual demangling. You can use the techniques described here to debug any other tool built with GCC.<\/p>\n<ol>\n<li>On your Linux machine download and unpack binutils source code. If you are using a Debian-based Linux distribution, you can do it by running the following command:\n<pre>apt-get source binutils<\/pre>\n<\/li>\n<li>Configure and build binutils by running the following commands on your Linux machine:\n<pre>cd ~\r\nmkdir binutils-build\r\ncd binutils-build\r\n..\/binutils-2.22\/configure\r\nmake<\/pre>\n<\/li>\n<li>At this point the <strong>~\/binutils-build\/binutils<\/strong> directory will contain the <strong>cxxfilt <\/strong>binary. If you run it and type in a C++-mangled name as seen in a binary file (e.g. <strong>_Z4testi<\/strong>) it will demangle it for you (e.g. <strong>test(int)<\/strong> for <strong>_Z4testi<\/strong>).<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/01-cxxfilt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/01-cxxfilt.png\" alt=\"01-cxxfilt\" width=\"449\" height=\"175\" \/><\/a><\/li>\n<li>The fastest way to debug the cxxfilt binary remotely from Visual Studio is using the Quick Debug feature available in VisualGDB 3.0 and later.<\/li>\n<li>Start Visual Studio and select <strong>Debug-&gt;Quick Debug with GDB<\/strong>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/02-quickdebug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/02-quickdebug.png\" alt=\"02-quickdebug\" width=\"693\" height=\"571\" \/><\/a><\/li>\n<li>Select &#8220;Debug a Linux app&#8221;, then select the remote machine. Setup a new SSH connection if needed.<\/li>\n<li>Specify the path to the program you want to debug (i.e. <strong>cxxfilt<\/strong>). Use the button to the right to browse the files on your Linux machine. Specify &#8220;gdb&#8221; as the GDB binary to use the default GDB available on your Linux machine.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/03-appsetup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/03-appsetup.png\" alt=\"03-appsetup\" width=\"645\" height=\"482\" \/><\/a>It is recommended to save the preset under some meaningful name to avoid doing manual setup next time you want to debug the same executable. You can always delete saved presets once you don&#8217;t need them any more.<\/li>\n<li>Press &#8220;Debug&#8221; to start debugging. A breakpoint in <strong>main()<\/strong> will be triggered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/04-dbgstart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/04-dbgstart.png\" alt=\"04-dbgstart\" width=\"693\" height=\"571\" \/><\/a><\/li>\n<li>Note that VisualGDB has automatically downloaded the <strong>cxxfilt.c<\/strong> source file from your Linux machine into <strong>%LOCALAPPDATA%\\VisualGDB\\AutoDownloadedSources<\/strong>. VisualGDB will continue downloading necessary files once you step into them or select them in Call Stack. However, the download process is one-way: you cannot modify those files from Windows. If you want to edit your Linux sources from Windows, <a href=\"\/tutorials\/linux\/\">create a new Linux project<\/a> with VisualGDB Project Wizard.<\/li>\n<li>Resume the application by pressing F5. Then break into it by clicking &#8220;Break All&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/05-breakall.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/05-breakall.png\" alt=\"05-breakall\" width=\"693\" height=\"571\" \/><\/a><\/li>\n<li>Use Call Stack to find the code that is waiting for user input. Scroll down and set a breakpoint on the <strong> demangle_it()<\/strong> call. Resume the program by pressing F5.<\/li>\n<li>Go to the Remote Console pane, type <strong>_Z4testi<\/strong> and press Enter. Your breakpoint will be hit:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/06-break.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/06-break.png\" alt=\"06-break\" width=\"693\" height=\"571\" \/><\/a><\/li>\n<li>Step into the <strong>demangle_it()<\/strong> function to see where is the actual demangling performed:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/07-demangle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/07-demangle.png\" alt=\"07-demangle\" width=\"693\" height=\"571\" \/><\/a><\/li>\n<li>As you can see, the demangling is done by calling <strong>cplus_demangle_v3()<\/strong> inside <strong> cplus-dem.c <\/strong>from <strong>libiberty<\/strong>.<\/li>\n<li>Note that as you have not created a VisualGDB project, the Solution Explorer won&#8217;t show any files. To browse through all source files listed in symbol files, press the Source File List button on the GDB Session window:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/sourcelist-btn.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/sourcelist-btn.png\" alt=\"sourcelist-btn\" width=\"500\" height=\"208\" \/><\/a><\/li>\n<li>The VisualGDB source browser will show you all files reported by GDB, the corresponding Windows locations (if any path mapping was established) and will allow opening the files in Visual Studio. Missing files will be automatically downloaded. Go to the Hierarchical View, select an arbitrary file and press Open. The file will be downloaded and opened by Visual Studio:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/08-filemgr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/linux\/quickdebug\/08-filemgr.png\" alt=\"08-filemgr\" width=\"700\" height=\"369\" \/><\/a><\/li>\n<li>Press Shift-F5 to end debugging.<\/li>\n<\/ol>\n<p><\/root><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to quickly debug a Linux program built with GCC without creating a separate Visual Studio project.<\/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":[33,82],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/156"}],"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=156"}],"version-history":[{"count":0,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}