Eclipse with GDB is great: it comes with a graphical front end for debugging. But sometimes it is all about to download a program. Is it really necessary to launch an IDE like Eclipse to program or quickly debug a board? With the GNU Debugger (GDB), the answer is ‘no’: GDB comes with a command line debugger which is designed exactly for this: providing a command line interface for programming/downloading and debugging, bypassing any GUI (Graphical User Interface).
Outline
In “OpenOCD/CMSIS-DAP Debugging with Eclipse and without an IDE” I have documented how this works with the combination of GDB+OpenOCD+CMSIS-DAP, but this works in a similar way with Segger J-Link and P&E Multilink, as both come with a GDB server implementation too (e.g. if I use the Freescale Kinetis Design Studio or a DIY Eclipse IDE).
What I need is:
- The GDB client: this comes with the GNU tools, e.g. the GNU ARM Embedded (launchpad).
- The GDB server (e.g. from P&E or Segger). Both come installed with the Kinetis Design Studio.
- And a board :-). I use here the FRDM-KL25Z board as it works with P&E, Segger, and OpenOCD/CMSIS-DAP GDB servers.
GDB Debugging Chain
To debug with GDB, I need a client, a server, a probe and of course a board:
The client is the gdb executable (arm-none-eabi-gdb.exe) on the host. It talks over a TCP/IP connection/port to the GDB server. The server is usually an executable running on the same machine, but it can run anywhere in the network too on a remote machine. Popular GDB servers are available from Segger, P&E or as well as open source (e.g. OpenOCD, see “OpenOCD/CMSIS-DAP Debugging with Eclipse and without an IDE“). Kinetis Design Studio v2.0.0 comes with all these three servers. The server needs to talk to the microcontroller/device/board using a JTAG/SWD connection: this can be a probe from vendors like Segger (J-Link, for example), P&E (USB Multilink, for example) or with CMSIS-DAP (e.g. on the FRDM-KL25Z board, see https:\\www.freescale.com\opensda). Both Segger and P&E provide firmware files for the FRDM boards to behave like a hardware probe, see “Segger J-Link Firmware for OpenSDAv2” and “New P&E OpenSDA Firmware v114“.
GDB Client
The GDB client for ARM is named arm-none-eabi-gdb.exe, and in the case of KDS V2.0.0 it is located inside the toolchain\bin folder:
start the GDB client with
arm-none-eabi-gdb.exe
The GDB client can be used with many GDB servers. The Segger and P&E specifics will be handled after this section.
P&E GDB Server
There is a GDB server from P&E available from http://www.pemicro.com/products/product_viewDetails.cfm?product_id=15320151. Alternatively, a P&E GDB server is included in the Freescale Kinetis Design Studio (KDS) (https://www.freescale.com/kds).
In case of KDS V2.0.0, the P&E GDB server is inside the eclipse\plugins\com.pemicro.debug.gdbjtag.pne folder. The folder name depends on the version, and there is a sub folder depending on Windows or Linux (win32 on Windows). The GDB server is named ‘pegdbserver_console.exe’:
The most important command line arguments are:
- -startserver: starts the server
- -device: specifies the device
- -devicelist: lists the supported devices
These and other command line options are documented in
<KDS_2.0.0>\pemicro\Kinetis_Design_Studio_Debug_Configuration_User_Guide.pdf
To find out the list of supported devices, use the -devicelist command:
π‘ the real path to the P&E gdb server depends on your version installed. I’m using KDS v2.0.0
pegdbserver_console.exe -devicelist
I start the server for the device on the FRDM-KL25Z on a cmd/DOS prompt with
pegdbserver_console.exe -startserver -device=KL25Z128M4
and it will report something like this:
P&E GDB Server, Version 5.13.02.00 Copyright 2014, P&E Microcomputer Systems Inc, All rights reserved Loading library C:\Freescale\KDS_2.0.0\eclipse\plugins\com.pemicro.debug.gdbjtag .pne_1.1.8.201411191655\win32\gdi\unit_ngs_arm_internal.dll ... Done. Command line arguments: -startserver -device=KL25Z128M4 Device selected is kl25z128m4 PE-ERROR: Unable to auto-detect debug hardware. Please specify on the command-line. Halting. C:\Freescale\KDS_2.0.0>eclipse\plugins\com.pemicro.debug.gdbjtag.pne_1.1.8.20141 1191655\win32\pegdbserver_console.exe -startserver -device=KL25Z128M4 P&E GDB Server, Version 5.13.02.00 Copyright 2014, P&E Microcomputer Systems Inc, All rights reserved Loading library C:\Freescale\KDS_2.0.0\eclipse\plugins\com.pemicro.debug.gdbjtag .pne_1.1.8.201411191655\win32\gdi\unit_ngs_arm_internal.dll ... Done. Command line arguments: -startserver -device=KL25Z128M4 Device selected is kl25z128m4 HW Auto-Selected : Interface=OPENSDA Port=DBA07E44Β Β ; USB1 : OpenSDA (DBA07E44) Connecting to target. OpenSDA detected - Flash Version 1.14 Device is KL25Z128M4. Mode is In-Circuit Debug. 'Kinetis' is a registered trademark of Freescale. (C)opyright 2012, P&E Microcomputer Systems, Inc. (www.pemicro.com) API version is 101 Server running on 127.0.0.1:7224
With this the server is running and waiting on port 7224 for a connection from the GDB client.
To connect to the P&E GDB server, use this in the gdb client:
target remote localhost:7224
Some monitor commands are documented here: http://www.pemicro.com/forums/forum.cfm?forum_topic_id=3990
π‘ I have not found a formal document (yet?) about the P&E monitor commands supported. A tip is to look in the Eclipse debug console view for hints π
_reset ; Reset the MCU _r0 ... _r15 ; Change CPU register Value (param : longvalue) _fill.w ;Fill memory words (params : startaddr endaddr wordvalue) _fill.l ;Fill memory longwords (params : startaddr endaddr longwordvalue) _pc ;Set program counter (param: longvalue)
So to reset the microcontroller, use
monitor _reset
Then I can use the load and file commands to load my application for debug:
Below is the log of a debug session with P&E GDB Server using the ARM GDB client:
Segger GDB Server
There is a GDB server from Segger available from https://www.segger.com/jlink-gdb-server.html. Alternatively, a Segger GDB server is included in the Freescale Kinetis Design Studio (KDS) (https://www.freescale.com/kds). Segger has a GUI version (on Windows only, as far as I know) and a console/command line version. I’m using the command line version here.
The Segger GDB server is named JLinkGDBServerCL.exe and located in the C:\Freescale\KDS_2.0.0\segger folder.
Start it with
JLinkGDBServerCL.exe
The Segger GDB server expects a connection on port 2331. In the GDB client, I use
target remote localhost:2331
With
monitor reset
I can reset the target from the GDB client.
π‘ Documentation will commands can be found here: https://www.segger.com/admin/uploads/productDocs/UM08005_JLinkGDBServer.pdf
Next, I select the device to be used:
monitor device = MKL25Z128xxx4
Then I can use the load and file commands to load my application for debug:
Below is the log of a debug session with Segger GDB Server using the ARM GDB client:
GDB Client Commands to Load/Program the Device
The following commands in the gdb client are generic for both P&E and Segger.
Load the application code with the load command:
load c:/tmp/KL25Z_blue.elf
Use the file command to load debug symbols in gdb:
file c:/tmp/KL25Z_blue.elf
Use the continue command to start/continue execution:
continue
With CTRL+C I can stop execution.
With the quit command I terminate the gdb session:
quit
π‘ There are plenty of tips and trick for GDB available, see as wellΒ http://haifux.org/lectures/222/GDB_haifux_David_Khosid.pdf. For example you can put default commands into a .gdbinit file. Have as well a look https://sourceware.org/gdb/current/onlinedocs/gdb/ for a list of gdb commands.
Automation
Now if it is about to flash/program a bunch of boards, it is much easier to do this from a script, batch or command file. For example I create a text file with the following content:
# Segger: listen on port target remote localhost:2331 # Segger: reset device monitor reset # Segger: specify flash of device monitor device = MKL25Z128xxx4 # load/flash application file load c:/tmp/KL25Z_green.elf # exit gdb quit
Now with the GDB server running, I can launch the gdb client with the -x option, passing that command file to it:
π‘ GDB uses a similar file named .gdbinit (if it exists) to be executed at gdb startup. So that would be a good place for common settings.
arm-none-eabi-gdb.exe -x gdb_flash.txt
That way it will flash my binary file to the board:
Be free to write your own script: that way you can add the download to your make file or anything else which uses a command line interface.
Summary
GDB comes with a nice command line interface, so no GUI or IDE is needed for debugging or just downloading/flashing an image to a device. The GNU tools come with a generic GDB client which needs to talk to a GDB server, which can be from P&E, Segger or something else like OpenOCD. The commands supported in the server differ from one GDB server to another. With little work, it is possible to setup a command line environment for programming and debugging which is very useful for automated testing or automation in general.
Happy Serving π
Links:
- GDB commands: https://sourceware.org/gdb/current/onlinedocs/gdb/
- Segger GDB Server and Documentation:Β https://www.segger.com/jlink-gdb-server.html
- PE Micro GDB Server: http://www.pemicro.com/products/product_viewDetails.cfm?product_id=15320151
- Freescale Kinetis Design Studio download: https://www.freescale.com/kds
- Command line debugging with gdb: http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_3.html and http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_16.html#SEC143
Do you know if it’s possible to download the contents of flash with the PEMicro tools?
LikeLike
Hi Joey,
yes, and I have documented the steps/commands how to do this in this article here.
LikeLike
Sorry, I meant the reverse, to get the flash contents from the MCU onto the computer.
LikeLike
Yes, this is possible too. See the gdb command
dump binary memory
I have used it here: https://mcuoneclipse.com/2015/01/05/updated-percepio-tracealyzer-and-trace-library-to-version-v2-7-0/
LikeLike
Any idea how to start excuting and then exit? Looking to use this for production and want to put this in a batch file. Currently when we execute “continue” we are stuck.
LikeLike
that might depend on the server implementation. But ‘detach’ should do what you need. Or try ‘disconnect’?
LikeLike
detach works and so does disconnect, but only when the code is halted. If we start executing the code with continue, we are stuck. I need something that can free run…
LikeLike
Hi Dave,
I’m using the P&E GDB Server in Kinetis Design Studio, and here when I do a ‘detach’, the target is started/running and it disconnects the gdb client. This is exactly what you need, right?
LikeLike
Hi Eric,
Sorry for the delay here. The detach works fine when inside of KDS, but when I am running the standalone script I cannot figure out how to detach after running or “continuing.”
LikeLike
Can you try ‘detach’ or ‘disconnect’? It could be dependent on the GDB server you are using.
LikeLike
detach or disconnect will not work because we are still stuck in the “continue” part of the script.
LikeLike
Hi Dave,
You should not use the ‘continue’ part. I quickly wrote a post how I’m using it, see https://mcuoneclipse.com/2015/08/26/batch-programming-with-gdb-segger-j-link-and-pe-multilink/
I hope this helps,
Erich
LikeLike
Hi Erich,
You rock. I was readying your latest article when you posted this. I will give this a try and let you know how it goes soon.
Thanks,
David
LikeLike
I had not in my mind to distract you from your reading π
LikeLike
Thanks for the details. I was searching for the way to run the commands from a file and here it is. Use ‘-x’.
LikeLike
Hello,
yes, the option -x let you execute an external script in a file. Thanks for sharing!
LikeLike
Pingback: Batch Programming with GDB: Segger J-Link and P&E Multilink | MCU on Eclipse
Pingback: Updating Segger Tools in Eclipse Kinetis Design Studio | MCU on Eclipse
Hi,
Great article.
Do you know if there is any way to use the eclipse variables in the GDB commands?
I want to do something like this:
load “${project_loc}\..\..\..\\\\bootloader.elf”
LikeLike
Hi Ohad,
I don’t think there is a way, at least not now. The problem is that the launch configuration has really not much knowledge about the eclipse variables at all.
LikeLike
Pingback: Debug Multiple Boards with GDB at the Same Time | MCU on Eclipse
Pingback: Flashing many ARM Boards without a Host PC | MCU on Eclipse
Hi Erich,
Thanks for your blog.
With ARM GNU Eclipse plugin and simply by putting GDB server commands in Startup tab in debug configuration, the GDB Server do the download job for you.
Only you need to put these commands:
monitor flash device
monitor flash download = 1
and to enable flash breakpoint add:
monitor flash breakpoints = 1
I have tested this, and it works fine,
LikeLike
I believe this might depend on the GDB server: which one did you use?
LikeLike
Wow, very nice documentation. I was able to program MKE04Z8xxx4 MCU, using segger J-LInk and Linux, in less than 5 minutes. Very very clear explain.
This is the bash script I’ve used to run JLinkGDBServer and gdb client, permitting to program many MCU just pressing ENTER , or q to exit from the programming loop:
#!/bin/bash
#bash script program.sh
#if JLinkGDBServer not running, run it (specify the right MCU device)
if [ -z `pidof JLinkGDBServer` ]; then
JLinkGDBServer -device MKE04Z8xxx4 -if SWD &
fi
# programming loop
while [ 1 ]; do
echo “Press a key to start programming, q to end”
read x
if [ “a$x” = “aq” ]; then
break
fi
arm-none-eabi-gdb -x program.gdb
if [ $? -eq 0 ]; then
beep
else
beep -l 100 -r 3
fi
done
#kill JLinkGDB server when exiting
killall JLinkGDBServer
===================================================================
And this is the program.gdb script:
# program.gdb script
target remote localhost:2331
monitor device MKE04Z8xxx4
monitor reset
# replace the elf file with yours
load ./tenda_buttons.elf
monitor go
disconnect
quit
LikeLike
Hi Paolo,
thanks for sharing, very useful!
Erich
LikeLike
Pingback: What is “Realtime Debugging”? | MCU on Eclipse
Hi, Erich,
Im having issues trying to debug with PE Micro Multilink universal and KDS. After configuring, exactly like described on: “https://mcuoneclipse.com/2015/05/11/debugging-the-frdm-k64f-with-pe-multilink/ ”
So, when i start to debug, it launches an error related to the selected remote port: “target-select remote localhost:7224”. What would it be?
Thank you.
LikeLike
Problem solved updating kds pe micro plugin, as described on: “https://mcuoneclipse.com/2015/04/16/updated-pe-gdb-server-for-eclipse-connectattach-and-advanced-flash-programming/”.
Thank you again!
LikeLike
Hi Diogo,
many thanks for posting that updated. I was just going to look into it, so you saved me some time π
Erich
LikeLike
Pingback: Seggers JLink | Andreas' Blog
Pingback: Bare Metal STM32 Development on Windows | David Albert
Pingback: Using GDB Server Monitor Commands from Eclipse GDB Console | MCU on Eclipse
what should i do if i want to select the option “preserve partitioning of the device” in S32ds.exe using command line.
LikeLike
I believe there is no option for this setting.
LikeLike
Pingback: Remote Debugging with USB based JTAG/SWD Debug Probes | MCU on Eclipse
Pingback: Tutorial: MCUXpresso SDK with Linux, Part 2: Commandline Debugging with GDB | MCU on Eclipse
Thank you. Super helpful.
I used this to build a Mac Bash script that starts JLink GDB server and GDB + its commands:
#!/usr/bin/env bash
echo “Run JLink GDB server”
osascript <<EOF
tell application "Terminal" to do script "/Applications/SEGGER/JLink_V634b/JLinkGDBServer -device stm32f031c6 -if swd"
EOF
echo "Start GDB"
/gcc-arm-none-eabi-8-2018-q4-major/bin/arm-none-eabi-gdb -x gdbcommands
LikeLiked by 1 person
Pingback: Black Magic Open Source Debug Probe for ARM with Eclipse and GDB | MCU on Eclipse
Pingback: TrustZone with ARMv8-M and the NXP LPC55S69-EVK | MCU on Eclipse
Hi Erich,
This stuff is really interesting. Thanks a lot for sharing this π
I want to use the P&E Multilink debugger for target microcontroller SPC58NHx.
I found that the PE debugger supports the SPC5 family but I can’t find any free IDE that supports this microntroller family . There’s SPC5 studio but it doesn’t support P&E debugger.
So, I wanted to ask following questions :
– How difficult is it to create a support for new microcontroller in Eclipse IDE ?
– Does the PE gdb server supports C++ source code debugging ?
LikeLike
You might have a read at https://mcuoneclipse.com/2017/07/30/breathing-with-oxygen-diy-arm-cortex-m-cc-ide-and-toolchain-with-eclipse-oxygen/ which describes how to build an IDE (in this case for ARM, but similar thing applies to PowerPC too).
And yes, with the P&E debug server you can debug C++ code too. Actually the debug connection it is not aware of any language (C, C++, …) at all: it is the debugger front end (e.g. Eclipse CDT) which handles this.
LikeLike
Pingback: OpenOCD with MCU-Link | MCU on Eclipse