{"id":75,"date":"2014-02-04T17:39:31","date_gmt":"2014-02-04T17:39:31","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=75"},"modified":"2014-02-04T17:39:31","modified_gmt":"2014-02-04T17:39:31","slug":"gnudebug","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/gnudebug\/","title":{"rendered":"Using Visual Studio to debug GNU tools"},"content":{"rendered":"\n<p>This tutorial demonstrates how to debug GNU applications built with GCC using Visual Studio. <\/p>\n<p>We will show several debugging techniques by applying a patch created for gdb 7.2a to gdb 7.4 (and thus obtaining a crashing executable due to no backward compatibility), debugging the crash with VisualGDB, locating the problem and resolving it.<\/p>\n<p>Although the specific case demonstrated in this example (applying an old patch to GDB) is rare, the tutorial shows various powerful debugging techniques made possible using GDB with Visual Studio.<\/p>\n<p>To reproduce the steps described in this tutorial one-by-one, follow these steps:<\/p>\n<p>1. Download and install <a href=\"http:\/\/mingw.org\/\">MinGW<\/a>, and <a href=\"\/trial\">VIsualGDB<\/a>.<\/p>\n<p>2. Download <a href=\"http:\/\/ftp.gnu.org\/gnu\/gdb\/gdb-7.4.tar.bz2\">gdb 7.4<\/a> to <strong>c:\\mspgcc-build<\/strong> and start the MinGW shell.<\/p>\n<p>3. Run the following commands to unpack the GDB sources:<\/p>\n<pre>cd \/c\/mspgcc-build\r\ntar xjf gdb-7.4.tar.bz2<\/pre>\n<p>4. Download the <a href=\"http:\/\/sourceforge.net\/projects\/mspgcc\/files\/Patches\/gdb-7.2a\/msp430-gdb-7.2a-20111205.patch\/download\"> MSP430 patch for gdb 7.2a<\/a> to <strong> c:\\mspgcc-build<\/strong>.<\/p>\n<p>5. Apply the patch. The patch adds support to the MSP430 processor family.<\/p>\n<pre>cd gdb-7.4\r\npatch -p1 &lt; ..\/msp430-gdb-7.2a-20111205.patch<\/pre>\n<p>6. Create a build directory and configure GDB for MSP430 target:<\/p>\n<pre>cd ..\r\nmkdir gdb-build\r\ncd gdb-build\r\n..\/gdb-7.4\/configure --target msp430 --prefix \/c\/mspgcc<\/pre>\n<p>7. Build the GDB:<\/p>\n<pre>make -j16<\/pre>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/01-build.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/01-build.png\" alt=\"01-build\" width=\"677\" height=\"342\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>8. Reproduce the GDB crash. Load a simple ELF file into the simulator, set a breakpoint and run it. You can download a sample MSP430 ELF file <a href=\"http:\/\/visualgdb.com\/tutorials\/gnudebug\/ledblink.elf\">here<\/a>.<\/p>\n<pre>gdb\/gdb LEDBlink.elf<\/pre>\n<p>Once GDB starts, execute the following commands:<\/p>\n<pre>target sim\r\nload\r\nb main\r\nrun<\/pre>\n<p>Once the breakpoint in main() is hit, GDB will crash:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/02-crash.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/02-crash.png\" alt=\"02-crash\" width=\"532\" height=\"286\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>9. To analyze the problem with VisualGDB we&#8217;ll be creating a Visual Studio project to build and debug it. Start Visual Studio, select File-&gt;New-&gt;Project:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/03-newproj.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/03-newproj.png\" alt=\"03-newproj\" width=\"700\" height=\"484\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>10. In the project type selection screen, please select &#8220;A Windows application&#8221;.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/04-mingw.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/04-mingw.png\" alt=\"04-mingw\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>11. On the next screen select &#8220;Import existing project&#8221; and specify the location of the gdb-build folder. <strong>The folder you specify should contain the Makefile. Do not specify the folder with GDB sources.<\/strong><\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/05-import.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/05-import.png\" alt=\"05-import\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>12. Ensure that the &#8220;GNU make&#8221; option was automatically activated. If not, you have specified a wrong directory (containing no Makefile).<\/p>\n<p>13. The next screen allows selecting the toolchain. Normally, you should leave the default settings.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/06-toolchain.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/06-toolchain.png\" alt=\"06-toolchain\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>14. At this point you can import all GDB source files to Visual Studio project. This will make them available from the Solution Explorer and will also import them to IntelliSense. However, importing over a 1000 GDB source files might take more than 10 minutes. Thus, it&#8217;s recommended to skip import at this point:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/08-no-import.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/08-no-import.png\" alt=\"08-no-import\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>15. As you are not using remote building, leave the default values on the file mapping page:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/09-mapping.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/09-mapping.png\" alt=\"09-mapping\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>16. When we were reproducing the bug manually, we started &#8220;<strong>gdb\/gdb ledblink.elf<\/strong>&#8221; from the <strong> c:\\mspgcc-build\\gdb-build<\/strong> directory. Now we need to tell VisualGDB to start this command automatically:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/10-gdb.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/10-gdb.png\" alt=\"10-gdb\" width=\"700\" height=\"543\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>17. When the wizard is done, an empty project will be created. Start debugging by selecting <strong>Debug-&gt;Start debugging with GDB<\/strong>.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/11-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/11-project.png\" alt=\"11-project\" width=\"700\" height=\"578\" \/><\/a><\/p>\n<p>18. VisualGDB will use the GDB from MinGW to debug our freshly built GDB for MSP430 platform.<\/p>\n<p>19. To reproduce the bug enter the commands that caused it before:<\/p>\n<pre>target sim\r\nload\r\nb main\r\nrun<\/pre>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/12-gdb.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/12-gdb.png\" alt=\"12-gdb\" width=\"677\" height=\"342\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>20. Visual Studio will report a caught SIGSEGV:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/13-segfault.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/13-segfault.png\" alt=\"13-segfault\" width=\"517\" height=\"227\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>21. Press &#8220;break&#8221;. Visual Studio will show the location of the error in the source code:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/14-callstack.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/14-callstack.png\" alt=\"14-callstack\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>22. By hovering the mouse over <strong>sniffer<\/strong> we can see that its value is zero. To see where does it come from, hover the mouse over <strong>unwinder<\/strong>. Let&#8217;s retrieve more information about the address of <strong>unwinder<\/strong> from GDB. Double-click at the value popup, select the address and press Ctrl+C to copy it to clipboard.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/15-addr.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/15-addr.png\" alt=\"15-addr\" width=\"700\" height=\"575\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>23. Open the GDB Console window type &#8220;<strong>info symbol &lt;space&gt;<\/strong>&#8221; and press Ctrl+V to paste the address. Press Enter to execute the command.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/16-infosym.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/16-infosym.png\" alt=\"16-infosym\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>24. The address actually corresponds to <strong> msp430_epilogue_frame_unwind<\/strong> symbol. Copy it and paste into the watch window by selecting it and pressing Ctrl+V.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/17-watch.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-16\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/17-watch.png\" alt=\"17-watch\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>25. Looks like the field values after NORMAL_FRAME do not correspond to the field names. E.g. the <strong> msp430_epilogue_sframe_this_id<\/strong> is assigned to <strong> stop_reason<\/strong> instead of <strong>this_id<\/strong>, and so on.<\/p>\n<p>26. Search the source files in the GCC directory for <strong> msp430_epilogue_frame_unwind<\/strong>. Open the <strong> msp430-tdep.c<\/strong> file containing the definition.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/18-struct.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/18-struct.png\" alt=\"18-struct\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>27. It seems that at some point a new member was added to the <strong>frame_unwind<\/strong> structure after the first one, breaking the compatibility with previous patches. To check this, open the <strong>avr-tdep.c<\/strong> file and search for similar definitions:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/19-avr.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-18\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/19-avr.png\" alt=\"19-avr\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>28. Looks like the <strong>stop_reason<\/strong> member was added and thus has to be initialized. Copy the member initialization from the AVR code to both structures. When done, build the project by pressing Ctrl+Shift+B.<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/20-builddone.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-19\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/20-builddone.png\" alt=\"20-builddone\" width=\"700\" height=\"578\" \/><br \/>\n    <\/a>\n  <\/p>\n<p>29. If you made any errors, GCC will report them and Visual Studio will display them in the error list. You can then press F8 to automatically jump to the first error and so on.<\/p>\n<p>30. Select <strong>Debug-&gt;Start debugging with GDB <\/strong>and try to reproduce the problem:<\/p>\n<p>\n    <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/21-fixed.png\"><br \/>\n      <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/gnudebug\/21-fixed.png\" alt=\"21-fixed\" width=\"677\" height=\"342\" \/><br \/>\n    <\/a>\n  <\/p>\n<p class=\"auto-style2\">31. As you can see, updating the <strong> frame_unwind<\/strong> initialization solved the problem.<\/p>\n<p><\/root><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial demonstrates how to debug GNU applications built with GCC using Visual Studio. We will show several debugging techniques<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[41,40,36],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/75"}],"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=75"}],"version-history":[{"count":0,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}