Friday 1 February 2019

Using GDB with a BBC micro:bit

This note is about using Segger J-Link software to help debug code on a BBC micro:bit.

In this case, it was Ada code running Cortex GNAT RTS over FreeRTOS.

Some people use pyocd (pip install pyocd, probably: for example, here) but I don't like installing random Python packages, and setting up a virtual environment wasn't something I'd heard of when I started this hare! Also, I already had a J-Link (from Arduino Due work).

Segger's J-Link software and documentation package includes their command-line GDB server.

There is an alternative firmware for the micro:bit, which allows the J-Link GDB server to talk to the micro:bit over USB. There are instructions on how to upgrade.

The setup for this GDB server is sufficiently complicated that I set up a script for it (gdbserver-microbit):

/Applications/SEGGER/JLink/JLinkGDBServer       \
    -port 3333                                  \
    -device nrf51822                            \
    -endian little                              \
    -if swd                                     \
    -ir                                         \
    -vd                                         \
    -localhostonly                              \
    -halt

My .gdbinit says

target remote :3333
load

A typical session might start

$ arm-eabi-gdb simple_buttons
GNU gdb (GDB) 7.10 for GNAT GPL 2017 [rev=gdb-7.10-ref-199-g7cfc608]
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
See your support agreement for details of warranty and support.
If you do not have a current support agreement, then there is absolutely
no warranty for this version of GDB.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-apple-darwin15 --target=arm-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
/Users/simon/.gdbinit:2: Error in sourced command file:
No symbol table is loaded.  Use the "file" command.
Reading symbols from simple_buttons...done.
0x00000000 in _isr_vector ()
Loading section .isr_vector, size 0xa8 lma 0x0
Loading section .text, size 0xfc8c lma 0xb0
Loading section .rodata, size 0xcaa8 lma 0xfd3c
Loading section .ARM.exidx, size 0x8 lma 0x1c7e4
Loading section .data, size 0x268 lma 0x1c7ec
Start address 0xece0, load size 117324
Transfer rate: 22914 KB/sec, 3666 bytes/write.
(gdb) monitor reset
Resetting target
(gdb) c
Continuing.
^C
Program received signal SIGTRAP, Trace/breakpoint trap.
__gnat_last_chance_handler (
    message=0x19c80 "System.FreeRTOS.Tasks.Create_Task: couldn't create task",
    line=0) at /Users/simon/cortex-gnat-rts/common/last_chance_handler.c:35
35   while (1) {}

which was because of insufficient stack space for the main program, it turned out. Fixed that, then

(gdb) load
`/Users/simon/coldframe/examples/microbit/simple_buttons' has changed; re-reading symbols.
Loading section .isr_vector, size 0xa8 lma 0x0
Loading section .text, size 0xfc8c lma 0xb0
Loading section .rodata, size 0xcaa8 lma 0xfd3c
Loading section .ARM.exidx, size 0x8 lma 0x1c7e4
Loading section .data, size 0x268 lma 0x1c7ec
Start address 0xece0, load size 117324
Transfer rate: 38191 KB/sec, 3666 bytes/write.
(gdb) monitor reset
Resetting target
(gdb) c
Continuing.
^C
Program received signal SIGTRAP, Trace/breakpoint trap.
__gnat_last_chance_handler (message=0x19d54 "s-secsta.adb", line=49)
    at /Users/simon/cortex-gnat-rts/common/last_chance_handler.c:35
35   while (1) {}

and now I need to use the new GCC8 Secondary_Stack_Size attribute ... this is what happens when you take a program that fitted easily into an STM32F407 or an Arduino Due and try to squeeze it into an nrf51 with 16K of RAM!


I see you can get a J-Link Edu Mini for about $20/£15! As usual, there may be cable issues.

No comments:

Post a Comment