{"id":3761,"date":"2018-03-18T16:43:18","date_gmt":"2018-03-18T23:43:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=3761"},"modified":"2019-10-24T14:09:48","modified_gmt":"2019-10-24T21:09:48","slug":"diagnosing-internal-openocd-problems-with-visualgdb","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/openocd\/build\/","title":{"rendered":"Building OpenOCD for Windows and Exploring its Internal Logic"},"content":{"rendered":"<p>This tutorial shows how to diagnose problems internal to OpenOCD using VisualGDB\u00a0and the <a href=\"https:\/\/github.com\/sysprogs\/openocd\">Sysprogs OpenOCD fork<\/a>\u00a0that includes convenient CMake build rules. Before you begin, install VisualGDB 5.4 or later.<\/p>\n<p>We will show how to build OpenOCD from\u00a0our Github repo, configure it to run with meaningful arguments and analyze its internal logic responsible for erasing FLASH memory.<\/p>\n<ol>\n<li>Clone\u00a0our OpenOCD repo (including submodules) into a folder on your\u00a0computer:\n<pre class=\"\">git clone --recurse-submodules https:\/\/github.com\/sysprogs\/openocd.git<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-git.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3763\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-git.png\" alt=\"02-git\" width=\"979\" height=\"514\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-git.png 979w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-git-300x158.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/02-git-390x205.png 390w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a><\/li>\n<li>Ensure you have VisualGDB 5.4 or later installed. Then locate and open the OpenOCD.sln file inside the repository. VisualGDB will ask for the location of the MinGW toolchain:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/tc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3774\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/tc.png\" alt=\"tc\" width=\"416\" height=\"367\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/tc.png 416w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/tc-300x265.png 300w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/a><\/li>\n<li>Proceed with the automatic toolchain installation.\u00a0Then build OpenOCD by pressing Ctrl-Shift-B:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3775\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd.png\" alt=\"oocd\" width=\"1055\" height=\"733\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd.png 1055w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd-300x208.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd-1024x711.png 1024w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd-392x272.png 392w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/oocd-130x90.png 130w\" sizes=\"(max-width: 1055px) 100vw, 1055px\" \/><\/a><strong>Warning: As of October 2019, our OpenOCD fork contains all the necessary dependencies and no longer requires a special toolchain, so the regular MinGW32 toolchain will suffice.<\/strong><\/li>\n<li>Open an embedded project that reproduces the issue you would like to diagnose, go to VisualGDB Project Properties -&gt; Debug Settings\u00a0and click &#8220;Test&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/05-startup.png\"><br \/>\n<\/a> <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3767\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-test.png\" alt=\"06-test\" width=\"946\" height=\"631\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-test.png 946w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/06-test-300x200.png 300w\" sizes=\"(max-width: 946px) 100vw, 946px\" \/><\/a><\/li>\n<li>The &#8220;Testing settings&#8221; will\u00a0show the command line used to launch OpenOCD. Copy it to clipboard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-cmdline.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3768\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-cmdline.png\" alt=\"07-cmdline\" width=\"823\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-cmdline.png 823w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/07-cmdline-300x216.png 300w\" sizes=\"(max-width: 823px) 100vw, 823px\" \/><\/a>A typical command line\u00a0for STM32F4 devices is shown below:\n<pre class=\"\">-c \"gdb_port 2594\" -c \"telnet_port 2595\" -f interface\/stlink-v2-1.cfg -f target\/stm32f4x.cfg -c init -c \"reset init\" -c \"echo VisualGDB_OpenOCD_Ready\"<\/pre>\n<\/li>\n<li>Switch back to OpenOCD.sln and open VS Properties for the <strong>openocd.exe<\/strong> target.\u00a0Remove the &#8220;gdb_port &#8221; and &#8220;telnet_port&#8221; commands (and the corresponding -&#8220;c&#8221; flags) from the command line and copy it to the &#8220;Command line arguments&#8221; field:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-copied.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3769\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-copied.png\" alt=\"08-copied\" width=\"822\" height=\"542\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-copied.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/08-copied-300x198.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Locate the original OpenOCD installation under <strong>%LOCALAPPDATA%\\VisualGDB\\EmbeddedDebugPackages\\com.sysprogs.arm.openocd\\bin<\/strong> and copy all <strong>.dll<\/strong> files from its directory to the <strong>VisualGDB\\Debug\\src<\/strong> subdirectory of the OpenOCD project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-libs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3770\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-libs.png\" alt=\"09-libs\" width=\"786\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-libs.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/09-libs-300x226.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Press F5 to start debugging. Confirm that OpenOCD starts listening on port 3333 for gdb\u00a0connections:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-openocd1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3776\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-openocd1.png\" alt=\"10-openocd\" width=\"928\" height=\"514\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-openocd1.png 928w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-openocd1-300x166.png 300w\" sizes=\"(max-width: 928px) 100vw, 928px\" \/><\/a><\/li>\n<li>Now we will show how to\u00a0step through the logic responsible for FLASH erasing.\u00a0Ensure your embedded project is built and\u00a0open its binary in gdb using the following command line:\n<pre class=\"\">&lt;sysgcc&gt;\\arm-eabi\\bin\\arm-eabi-gdb.exe\u00a0&lt;ELF file from your embedded project&gt;<\/pre>\n<p>The ELF file is typically located\u00a0in the <strong>VisualGDB\\Debug<\/strong> directory and has no extension. Then enter &#8220;target remote :3333&#8221; in the gdb command prompt in order to connect to OpenOCD:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/10-openocd.png\"><br \/>\n<\/a> <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-gdb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3772\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-gdb.png\" alt=\"11-gdb\" width=\"979\" height=\"514\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-gdb.png 979w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-gdb-300x158.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/11-gdb-390x205.png 390w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a><\/li>\n<li>Locate the FLASH driver for your device family (e.g. stm32f2x.c for STM32F4) and set a breakpoint in the erase function. Then switch to gdb and run the &#8220;load&#8221; command in order to program the memory manually. The breakpoint inside OpenOCD will hit:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3773\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase.png\" alt=\"12-erase\" width=\"1055\" height=\"733\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase.png 1055w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase-300x208.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase-1024x711.png 1024w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase-392x272.png 392w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/03\/12-erase-130x90.png 130w\" sizes=\"(max-width: 1055px) 100vw, 1055px\" \/><\/a>You can now\u00a0step through the code to double-check the\u00a0OpenOCD logic responsible for various parts of the erase process and fine-tune\u00a0parts of it.<\/li>\n<li>If you want to use a modified version of OpenOCD instead of the one shipped with VisualGDB, simply replace the <strong>%LOCALAPPDATA%\\VisualGDB\\EmbeddedDebugPackages\\com.sysprogs.arm.openocd\\bin\\openocd.exe\u00a0<\/strong>executable with the one you built and VisualGDB will use it instead.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to diagnose problems internal to OpenOCD using VisualGDB\u00a0and the Sysprogs OpenOCD fork\u00a0that includes convenient CMake build<\/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":[53,133,60,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3761"}],"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=3761"}],"version-history":[{"count":5,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3761\/revisions"}],"predecessor-version":[{"id":5305,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3761\/revisions\/5305"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=3761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=3761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=3761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}