OpenOCD complies with the remote gdbserver protocol and, as such, can be used to debug remote targets. Setting up GDB to work with OpenOCD can involve several components:
Of course, the version of GDB you use will need to be one which has
been built to know about the target CPU you’re using. It’s probably
part of the tool chain you’re using. For example, if you are doing
cross-development for ARM on an x86 PC, instead of using the native
gdb command you might use
if that’s the tool chain used to compile your code.
Use GDB 6.7 or newer with OpenOCD if you run into trouble. For instance GDB 6.3 has a known bug that produces bogus memory access errors, which has since been fixed; see http://osdir.com/ml/gdb.bugs.discuss/2004-12/msg00018.html
OpenOCD can communicate with GDB in two ways:
target remote localhost:3333
This would cause GDB to connect to the gdbserver on the local pc using port 3333.
It is also possible to use the GDB extended remote protocol as follows:
target extended-remote localhost:3333
target remote | openocd -c "gdb_port pipe; log_output openocd.log"
This would cause GDB to run OpenOCD and communicate using pipes (stdin/stdout). Using this method has the advantage of GDB starting/stopping OpenOCD for the debug session. log_output sends the log output to a file to ensure that the pipe is not saturated when using higher debug level outputs.
To list the available OpenOCD commands type
monitor help on the
GDB command line.
With the remote protocol, GDB sessions start a little differently than they do when you’re debugging locally. Here’s an example showing how to start a debug session with a small ARM program. In this case the program was linked to be loaded into SRAM on a Cortex-M3. Most programs would be written into flash (address 0) and run from there.
$ arm-none-eabi-gdb example.elf (gdb) target remote localhost:3333 Remote debugging using localhost:3333 ... (gdb) monitor reset halt ... (gdb) load Loading section .vectors, size 0x100 lma 0x20000000 Loading section .text, size 0x5a0 lma 0x20000100 Loading section .data, size 0x18 lma 0x200006a0 Start address 0x2000061c, load size 1720 Transfer rate: 22 KB/sec, 573 bytes/write. (gdb) continue Continuing. ...
You could then interrupt the GDB session to make the program break,
where to show the stack,
list to show the
code around the program counter,
step through code,
set breakpoints or watchpoints, and so on.
OpenOCD supports the gdb qSupported packet, this enables information to be sent by the GDB remote server (i.e. OpenOCD) to GDB. Typical information includes packet size and the device’s memory map. You do not need to configure the packet size by hand, and the relevant parts of the memory map should be automatically set up when you declare (NOR) flash banks.
However, there are other things which GDB can’t currently query. You may need to set those up by hand. As OpenOCD starts up, you will often see a line reporting something like:
Info : lm3s.cpu: hardware has 6 breakpoints, 4 watchpoints
You can pass that information to GDB with these commands:
set remote hardware-breakpoint-limit 6 set remote hardware-watchpoint-limit 4
With that particular hardware (Cortex-M3) the hardware breakpoints only work for code running from flash memory. Most other ARM systems do not have such restrictions.
Another example of useful GDB configuration came from a user who found that single stepping his Cortex-M3 didn’t work well with IRQs and an RTOS until he told GDB to disable the IRQs while stepping:
define hook-step mon cortex_m maskisr on end define hookpost-step mon cortex_m maskisr off end
Rather than typing such commands interactively, you may prefer to
save them in a file and have GDB execute them as it starts, perhaps
using a .gdbinit in your project directory or starting GDB
gdb -x filename.
By default the target memory map is sent to GDB. This can be disabled by the following OpenOCD configuration option:
For this to function correctly a valid flash configuration must also be set in OpenOCD. For faster performance you should also configure a valid working area.
Informing GDB of the memory map of the target will enable GDB to protect any
flash areas of the target and use hardware breakpoints by default. This means
that the OpenOCD option
gdb_breakpoint_override is not required when
using a memory map. See gdb_breakpoint_override.
To view the configured memory map in GDB, use the GDB command info mem. All other unassigned addresses within GDB are treated as RAM.
GDB 6.8 and higher set any memory area not in the memory map as inaccessible. This can be changed to the old behaviour by using the following GDB command
set mem inaccessible-by-default off
gdb_flash_program enable is also used, GDB will be able to
program any flash memory using the vFlash interface.
GDB will look at the target memory map when a load command is given, if any areas to be programmed lie within the target flash area the vFlash packets will be used.
If the target needs configuring before GDB programming, an event script can be executed:
$_TARGETNAME configure -event EVENTNAME BODY
To verify any flash programming the GDB command compare-sections can be used.
For SMP support following GDB serial protocol packet have been defined :
OpenOCD implements :
Handling of this packet within GDB can be done :
set $_core 1 #Jc01 packet is sent print $_core #jc packet is sent and result is affected in $
# toggle0 : force display of coreid 0 define toggle0 maint packet Jc0 continue main packet Jc-1 end # toggle1 : force display of coreid 1 define toggle1 maint packet Jc1 continue main packet Jc-1 end
OpenOCD includes RTOS support, this will however need enabling as it defaults to disabled. It can be enabled by passing -rtos arg to the target. See RTOS Type.
See Debugging Programs with Multiple Threads in GDB manual, for details about relevant GDB commands.
An example setup is below:
$_TARGETNAME configure -rtos auto
This will attempt to auto detect the RTOS within your application.
Currently supported rtos’s include:
Note: Before an RTOS can be detected, it must export certain symbols; otherwise, it cannot be used by OpenOCD. Below is a list of the required symbols for each supported RTOS.
_tx_thread_current_ptr, _tx_thread_created_ptr, _tx_thread_created_count.
rlist, ch_debug, chSysInit.
Rtos::sCurrentTask, Rtos::sListReady, Rtos::sListSleep, Rtos::sListSuspended, Rtos::sMaxPriorities, Rtos::sCurrentTaskCount.
OSRunning, OSTCBCurPtr, OSTaskDbgListPtr, OSTaskQty
For most RTOS supported the above symbols will be exported by default. However for some, eg. FreeRTOS and uC/OS-III, extra steps must be taken.
These RTOSes may require additional OpenOCD-specific file to be linked along with the project: