{"id":8008,"date":"2023-03-27T18:05:06","date_gmt":"2023-03-28T01:05:06","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8008"},"modified":"2023-03-27T18:12:55","modified_gmt":"2023-03-28T01:12:55","slug":"using-st-link-gdb-server-to-debug-stm32-devices","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/st-link\/gdbserver\/","title":{"rendered":"Using ST-Link GDB Server to debug STM32 Devices"},"content":{"rendered":"<p>This tutorial shows how to use the ST-Link GDB server tool instead of OpenOCD in order to program and debug the STM32 devices via ST-Link. We will create a basic application and will show how to configure VisualGDB to run the ST-Link GDB server and connect gdb to it.<\/p>\n<p>Before you begin, install the <strong>STM32CubeIDE<\/strong> and locate the following files inside its directory (typically under<strong> &lt;STM32CubeIDE directory&gt;\\STM32CubeIDE\\plugins<\/strong>):<\/p>\n<table style=\"border-collapse: collapse; height: 144px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px;\">File<\/td>\n<td style=\"height: 24px;\">Description<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px;\">ST-LINK_gdbserver.exe<\/td>\n<td style=\"height: 24px;\">Interfaces the gdb debugger with the STM32Cube Programmer<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px;\">STM32_Programmer_CLI.exe<\/td>\n<td style=\"height: 24px;\">Provides programmatic access to the ST-Link devices<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li>Start Visual Studio and locate the <strong>VisualGDB Embedded Project Wizard<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8009\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/01-newprj.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Enter the name and location for your project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/02-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8010\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/02-prjname.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Select &#8220;<strong>Create a new project -&gt; Embedded Application<\/strong>&#8220;. We recommend using the Advanced CMake for all new projects, however the steps shown in this tutorial will also work for other project types.<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/03-app.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8011\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/03-app.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select your ARM toolchain and the exact device you would like to target:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/04-dev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8012\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/04-dev.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Pick a project sample that will be cloned and click &#8220;Next&#8221; to proceed to debug settings: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/05-blink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8013\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/05-blink.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>The recommended way to debug STM32 devices with ST-Link is using the <a href=\"https:\/\/visualgdb.com\/documentation\/openocd\/\">OpenOCD tool<\/a>, however it does not always support the latest devices and sometimes doesn&#8217;t work in edge cases. So, in this tutorial we will manually use the ST-Link GDB server that is included in the STM32CubeIDE. Switch the &#8220;<strong>Debug Using<\/strong>&#8221; view to &#8220;<strong>Debug Methods<\/strong>&#8221; and select &#8220;<strong>Custom GDB Stub<\/strong>&#8220;. Then enter the following parameters:<br \/>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">Parameter<\/td>\n<td style=\"width: 50%;\">Value<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Command<\/td>\n<td style=\"width: 50%;\">&lt;full path to ST-LINK_gdbserver.exe&gt;<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Arguments<\/td>\n<td style=\"width: 50%;\">-p 2000 -d -cp &lt;directory containing STM32_Programmer_CLI.exe&gt;<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">TCP port for GDB<\/td>\n<td style=\"width: 50%;\">2000<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Program FLASH command<\/td>\n<td style=\"width: 50%;\">load<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/06-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8014\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/06-debug.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>You can also use any other port for gdb, as long as the value in the &#8220;<strong>TCP port for GDB<\/strong>&#8221; field matches the &#8220;<strong>-p<\/strong>&#8221; argument to <strong>ST-LINK_gdbserver.exe<\/strong>.<\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Build it via <strong>Build-&gt;Build Solution<\/strong> and try setting a breakpoint inside the main loop:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/07-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8015\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/07-build.png\" alt=\"\" width=\"1226\" height=\"842\" \/><\/a><\/li>\n<li>Press F5 to begin debugging. VisualGDB will launch <strong>ST-LINK_gdbserver<\/strong>, display output from it in <strong>Debug-&gt;Windows-&gt;VisualGDB Output<\/strong>, and will automatically connect gdb to it. The FLASH memory will automatically get programmed and the breakpoint will trigger:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/08-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8016\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/08-bkpt.png\" alt=\"\" width=\"1226\" height=\"842\" \/><\/a><\/li>\n<li>You can send various raw commands to the <strong>ST-LINK_gdbserver<\/strong> by prefixing them with &#8220;<strong>monitor<\/strong>&#8221; in the GDB Session window, e.g. &#8220;<strong>monitor reset<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/09-monitor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8017\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/09-monitor.png\" alt=\"\" width=\"1226\" height=\"842\" \/><\/a><\/li>\n<li>You can configure VisualGDB to automatically run additional commands at the beginning of each debug session via <strong>VisualGDB Project Properties -&gt; AdditionalGDB Commands -&gt; After selecting a target<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/10-reset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8018\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/03\/10-reset.png\" alt=\"\" width=\"844\" height=\"582\" \/><\/a><\/li>\n<\/ol>\n<p>Some advanced features (e.g. live memory access) won&#8217;t work when using the ST-Link GDB server instead of OpenOCD, however it could be a good workaround for new devices that are not yet supported by OpenOCD, or if OpenOCD doesn&#8217;t work as expected.<\/p>\n<p>You can find a detailed reference of the ST-Link GDB Server commands and command-line options in <a href=\"https:\/\/www.st.com\/resource\/en\/user_manual\/um2576-stm32cubeide-stlink-gdb-server-stmicroelectronics.pdf\">this document<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the ST-Link GDB server tool instead of OpenOCD in order to program and debug<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[62,61],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8008"}],"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=8008"}],"version-history":[{"count":13,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8008\/revisions"}],"predecessor-version":[{"id":8031,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8008\/revisions\/8031"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}